IBR-DTN  1.0.0
KeyExchanger.h
Go to the documentation of this file.
1 /*
2  * KeyExchanger.h
3  *
4  * Copyright (C) 2014 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  * Thomas Schrader <schrader.thomas@gmail.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22 
23 #ifndef KEYEXCHANGER_H_
24 #define KEYEXCHANGER_H_
25 
26 #include "Configuration.h"
27 #include "Component.h"
28 #include "core/AbstractWorker.h"
29 #include "core/EventReceiver.h"
30 #include "core/TimeEvent.h"
35 
36 #include <ibrcommon/data/File.h>
37 #include <ibrcommon/thread/Queue.h>
38 
39 #include <openssl/bn.h>
40 
41 #include <string>
42 #include <sstream>
43 #include <iostream>
44 
45 namespace dtn
46 {
47  namespace security
48  {
50  public dtn::core::EventReceiver<dtn::security::KeyExchangeEvent>,
51  public dtn::core::EventReceiver<dtn::core::TimeEvent>
52  {
53  static const std::string TAG;
54 
55  public:
56  KeyExchanger();
57  virtual ~KeyExchanger();
58 
64  virtual void __cancellation() throw ();
65 
70  virtual void componentUp() throw ();
71 
76  virtual void componentRun() throw ();
77 
82  virtual void componentDown() throw ();
83 
88  virtual const std::string getName() const;
89 
93  virtual void callbackBundleReceived(const dtn::data::Bundle &b);
94 
98  virtual void raiseEvent(const dtn::security::KeyExchangeEvent &evt) throw ();
99  virtual void raiseEvent(const dtn::core::TimeEvent &evt) throw ();
100 
104  virtual void submit(KeyExchangeSession &session, const KeyExchangeData &data);
105  virtual void finish(KeyExchangeSession &session);
106  virtual void error(KeyExchangeSession &session, bool reportError);
107 
108  private:
109  class Task
110  {
111  public:
112  virtual ~Task() = 0;
113 
117  virtual void execute(KeyExchanger &exchanger) throw () = 0;
118  };
119 
120  class ExchangeTask : public Task
121  {
122  public:
123  ExchangeTask(const dtn::data::EID &peer, const dtn::security::KeyExchangeData &data);
124  virtual ~ExchangeTask() {};
125 
129  virtual void execute(KeyExchanger &exchanger) throw ();
130 
131  private:
132  const dtn::data::EID _peer;
134  };
135 
136  class ExpireTask : public Task
137  {
138  public:
139  ExpireTask(const dtn::data::Timestamp timestamp);
140  virtual ~ExpireTask() {};
141 
145  virtual void execute(KeyExchanger &exchanger) throw ();
146 
147  private:
148  const dtn::data::Timestamp _timestamp;
149  };
150 
151  KeyExchangeSession& createSession(KeyExchangeProtocol &p, const dtn::data::EID &peer);
152  KeyExchangeSession& createSession(KeyExchangeProtocol &p, const dtn::data::EID &peer, const dtn::security::KeyExchangeData &data);
153  KeyExchangeSession& getSession(const dtn::data::EID &peer, const dtn::security::KeyExchangeData &data) throw (ibrcommon::Exception);
154  void freeSession(const dtn::data::EID &peer, const unsigned int uniqueId);
155 
156  void expire(const dtn::data::Timestamp timestamp);
157 
158  // queue to hand-over object to the main-thread
159  ibrcommon::Queue<KeyExchanger::Task*> _queue;
160 
161  std::map<std::string, KeyExchangeSession*> _sessionmap;
162 
163  std::map<int, KeyExchangeProtocol*> _protocols;
164 
165  ibrcommon::Mutex _expiration_lock;
166  dtn::data::Timestamp _next_expiration;
167  };
168  } /* namespace security */
169 } /* namespace dtn */
170 #endif /* KEYEXCHANGER_H_ */
virtual void callbackBundleReceived(const dtn::data::Bundle &b)
virtual void error(KeyExchangeSession &session, bool reportError)
virtual void raiseEvent(const dtn::security::KeyExchangeEvent &evt)
virtual void finish(KeyExchangeSession &session)
virtual const std::string getName() const
virtual void submit(KeyExchangeSession &session, const KeyExchangeData &data)