Revised OpenFlow Library  v0.6.0dev
 All Classes Files Functions Variables Friends Groups Pages
csocket.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #ifndef CSOCKET_H
6 #define CSOCKET_H
7 
8 #include <list>
9 #include <bitset>
10 #include <stdio.h>
11 #include <netinet/in.h>
12 #include <sys/un.h>
13 #include <fcntl.h>
14 #include <netinet/tcp.h>
15 #include <sys/ioctl.h>
16 #include <sys/uio.h>
17 #include <pthread.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <assert.h>
21 
22 #include "rofl/common/croflexception.h"
23 #include "rofl/common/ciosrv.h"
24 #include "rofl/common/csockaddr.h"
25 #include "rofl/common/logging.h"
26 #include "rofl/common/cparams.h"
27 
28 namespace rofl {
29 
30 class eSocketBase : public RoflException {};
31 class eSocketRxAgain : public eSocketBase {};
32 class eSocketTxAgain : public eSocketBase {};
36 class eSocketNotConnected : public eSocketBase {};
37 class eSocketTypeNotFound : public eSocketBase {};
39 
40 class csocket; // forward declaration
41 
58 class csocket_env {
59  friend class csocket;
60 public:
61 
65  virtual
67  {};
68 
76  virtual void
78  rofl::csocket& socket,
79  int newsd) = 0;
80 
88  virtual void
90  rofl::csocket& socket) = 0;
91 
97  virtual void
99  rofl::csocket& socket) = 0;
100 
106  virtual void
108  rofl::csocket& socket) = 0;
109 
116  virtual void
118  rofl::csocket& socket) = 0;
119 
126  virtual void
128  rofl::csocket& socket) = 0;
129 
136  virtual void
137  handle_read(
138  rofl::csocket& socket) = 0;
139 
146  virtual void
147  handle_write(
148  rofl::csocket& socket) = 0;
149 
156  virtual void
158  rofl::csocket& socket) = 0;
159 };
160 
161 
182 class csocket :
183  public virtual ciosrv
184 {
185 public:
186 
187  /* supported socket types */
188  enum socket_type_t {
189  SOCKET_TYPE_UNKNOWN = 0,
190  SOCKET_TYPE_PLAIN = 1,
191  SOCKET_TYPE_OPENSSL = 2,
192  };
193 
194 public:
195 
199  static csocket*
200  csocket_factory(
201  enum socket_type_t socket_type, csocket_env *owner);
202 
206  static cparams
207  get_default_params(
208  enum socket_type_t socket_type);
209 
210 public:
211 
218  csocket_env *env,
219  enum socket_type_t socket_type) :
220  socket_env(env),
221  socket_type(socket_type),
222  sd(-1),
223  domain(0),
224  type(0),
225  protocol(0),
226  backlog(0)
227  {};
228 
232  virtual
234  {};
235 
244  virtual void
245  listen(
246  const rofl::cparams& params) = 0;
247 
251  virtual void
252  accept(
254  int sd) = 0;
255 
263  virtual void
264  connect(
265  const rofl::cparams& params) = 0;
266 
275  virtual void
276  reconnect() = 0;
277 
278 
286  virtual void
287  close() = 0;
288 
289 
294  virtual ssize_t
295  recv(void *buf, size_t count) = 0;
296  virtual ssize_t
297  recv(void *buf, size_t count, int flags, rofl::csockaddr& from) = 0;
298 
315  virtual void
316  send(cmemory *mem, rofl::csockaddr const& dest = rofl::csockaddr()) = 0;
317 
318 
322  virtual bool
323  is_established() const = 0;
324 
328  virtual bool
329  write_would_block() const = 0;
330 
331 public:
332 
336  int
337  get_sd() const
338  { return sd; };
339 
343  enum rofl::csocket::socket_type_t
344  get_socket_type() const
345  { return socket_type; };
346 
350  const rofl::cparams&
351  get_socket_params() const
352  { return socket_params; };
353 
358  set_laddr()
359  { return laddr; };
360 
364  rofl::csockaddr const&
365  get_laddr() const
366  { return laddr; };
367 
372  set_raddr()
373  { return raddr; };
374 
378  rofl::csockaddr const&
379  get_raddr() const
380  { return raddr; };
381 
385  void
386  set_domain(int domain)
387  { this->domain = domain; };
388 
392  int
393  get_domain() const
394  { return domain; };
395 
399  void
400  set_type(int type)
401  { this->type = type; };
402 
406  int
407  get_type() const
408  { return type; };
409 
413  void
414  set_protocol(int protocol)
415  { this->protocol = protocol; };
416 
420  int
421  get_protocol() const
422  { return protocol; };
423 
424 
428  static bool
429  supports_socket_type(enum socket_type_t socket_type);
430 
431  //Common Keys
432  static std::string const PARAM_KEY_DO_RECONNECT;
433  static std::string const PARAM_KEY_REMOTE_HOSTNAME;
434  static std::string const PARAM_KEY_REMOTE_PORT;
435  static std::string const PARAM_KEY_LOCAL_HOSTNAME;
436  static std::string const PARAM_KEY_LOCAL_PORT;
437  static std::string const PARAM_KEY_DOMAIN;
438  static std::string const PARAM_KEY_TYPE;
439  static std::string const PARAM_KEY_PROTOCOL;
440 
441  //Common values (non-numeric)
442  static std::string const PARAM_DOMAIN_VALUE_INET_ANY;
443  static std::string const PARAM_DOMAIN_VALUE_INET;
444  static std::string const PARAM_DOMAIN_VALUE_INET6;
445  static std::string const PARAM_TYPE_VALUE_STREAM;
446  static std::string const PARAM_TYPE_VALUE_DGRAM;
447  static std::string const PARAM_PROTOCOL_VALUE_TCP;
448  static std::string const PARAM_PROTOCOL_VALUE_UDP;
449 
450  //Socket type specific keys
451  static std::string const PARAM_SSL_KEY_CA_PATH;
452  static std::string const PARAM_SSL_KEY_CA_FILE;
453  static std::string const PARAM_SSL_KEY_CERT;
454  static std::string const PARAM_SSL_KEY_PRIVATE_KEY;
455  static std::string const PARAM_SSL_KEY_PRIVATE_KEY_PASSWORD;
456  static std::string const PARAM_SSL_KEY_VERIFY_MODE;
457  static std::string const PARAM_SSL_KEY_VERIFY_DEPTH;
458  static std::string const PARAM_SSL_KEY_CIPHERS;
459 
460 public:
461 
462  friend std::ostream&
463  operator<< (std::ostream& os, csocket const& sock) {
464  os << rofl::indent(0) << "<csocket "
465  << "sd:" << sock.sd << " "
466  << "domain:" << sock.domain << " "
467  << "type:" << sock.type << " "
468  << "protocol:" << sock.protocol << " ";
469  os << ">" << std::endl;
470  os << rofl::indent(2) << "<raddr: >" << std::endl;
471  { rofl::indent i(4); os << sock.raddr; };
472  os << rofl::indent(2) << "<laddr: >" << std::endl;
473  { rofl::indent i(4); os << sock.laddr; };
474  return os;
475  };
476 
477 protected:
478 
480  enum socket_type_t socket_type;
481  int sd;
484  int domain;
485  int type;
486  int protocol;
487  int backlog;
489 };
490 
491 }; // end of namespace
492 
493 #endif
int type
Definition: csocket.h:485
virtual void handle_accept_refused(rofl::csocket &socket)=0
Called once accepting a request from a remote entity failed.
virtual ssize_t recv(void *buf, size_t count)=0
Reads bytes from socket.
Definition: cparams.h:20
virtual void handle_accepted(rofl::csocket &socket)=0
Called once this csocket entity has succeeded its accept() method.
Definition: csocket.h:34
virtual ~csocket()
csocket destructor
Definition: csocket.h:233
A single socket.
Definition: csocket.h:182
virtual ~csocket_env()
Destructor.
Definition: csocket.h:66
Definition: csocket.h:32
Definition: csocket.h:33
virtual void accept(const rofl::cparams &socket_params, int sd)=0
Handle accepted socket descriptor obtained from external listening socket.
Definition: csockaddr.h:38
int backlog
Definition: csocket.h:487
virtual void reconnect()=0
Reconnect this socket.
virtual void handle_read(rofl::csocket &socket)=0
Called once new data is available for reading from the socket.
Definition: csocket.h:35
virtual void close()=0
Closes this socket.
int domain
Definition: csocket.h:484
cparams socket_params
Definition: csocket.h:488
Definition: csocket.h:30
virtual void handle_closed(rofl::csocket &socket)=0
Called once the socket has been shutdown and closed.
C++ abstraction for malloc'ed memory areas.
Definition: cmemory.h:44
virtual void handle_listen(rofl::csocket &socket, int newsd)=0
Called once a listening socket has accepted a connection request from a remote peer entity...
virtual void handle_connect_refused(rofl::csocket &socket)=0
Called once a connection request to a remote entity failed.
csocket_env * socket_env
Definition: csocket.h:475
An abstract interface defining the consumer side of a csocket.
Definition: csocket.h:58
int sd
Definition: csocket.h:481
virtual void connect(const rofl::cparams &params)=0
Open socket and connect to peer entity (client side).
Definition: csocket.h:31
virtual void handle_connected(rofl::csocket &socket)=0
Called once a connection request has succeeded its connect() method.
Base class for IO services.
Definition: ciosrv.h:491
Definition: logging.h:76
Definition: csocket.h:36
virtual void handle_connect_failed(rofl::csocket &socket)=0
Called once a connection request to a remote entity failed.
csockaddr raddr
Definition: csocket.h:483
virtual void send(cmemory *mem, rofl::csockaddr const &dest=rofl::csockaddr())=0
Store a packet for transmission.
Definition: csocket.h:38
csockaddr laddr
Definition: csocket.h:482
virtual void listen(const rofl::cparams &params)=0
Open socket in listening mode (server side).
int protocol
Definition: csocket.h:486
enum socket_type_t socket_type
Definition: csocket.h:480
virtual void handle_write(rofl::csocket &socket)=0
Called once the socket accept additional data for sending.
csocket(csocket_env *env, enum socket_type_t socket_type)
csocket constructor
Definition: csocket.h:217
Definition: csocket.h:37
Definition: croflexception.h:27