Revised OpenFlow Library  v0.6.0dev
 All Classes Files Functions Variables Friends Groups Pages
csocket_plain.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_PLAIN_H
6 #define CSOCKET_PLAIN_H
7 
8 #include <list>
9 #include <bitset>
10 #include <stdio.h>
11 
12 #include <netinet/in.h>
13 #include <sys/un.h>
14 #include <fcntl.h>
15 #include <netinet/tcp.h>
16 #include <sys/ioctl.h>
17 #include <sys/uio.h>
18 #include <pthread.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <assert.h>
22 #include <net/if.h>
23 
24 #include "rofl/common/csocket.h"
25 #include "rofl/common/ctimerid.h"
26 #include "rofl/common/caddrinfos.h"
27 #include "rofl/common/openflow/openflow_common.h"
28 
29 namespace rofl {
30 
31 
52  public csocket
53 {
54 protected:
55 
56  struct pout_entry_t {
57  cmemory *mem;
58  csockaddr dest;
59  size_t msg_bytes_sent;
60  pout_entry_t(cmemory *mem = 0, csockaddr const& dest = csockaddr()) :
61  mem(mem), dest(dest), msg_bytes_sent(0) {};
62  pout_entry_t(pout_entry_t const& e) :
63  mem(0), msg_bytes_sent(0) {
64  *this = e;
65  };
66  struct pout_entry_t&
67  operator= (pout_entry_t const& e) {
68  if (this == &e) return *this;
69  mem = e.mem;
70  dest = e.dest;
71  msg_bytes_sent = e.msg_bytes_sent;
72  return *this;
73  };
74  friend std::ostream&
75  operator<< (std::ostream& os, struct pout_entry_t const& entry) {
76  os << indent(0) << "<struct pout_entry_t >" << std::endl;
77  os << indent(2) << "<mem:0x" << entry.mem << " >" << std::endl;
78  os << indent(2) << "<dest:" << entry.dest << " >" << std::endl;
79  os << indent(2) << "<msg_bytes_sent:" << entry.msg_bytes_sent << " >" << std::endl;
80  return os;
81  };
82  };
83 
84  bool had_short_write;
85  pthread_rwlock_t pout_squeue_lock;
86  std::list<pout_entry_t> pout_squeue;
88  //Defaults
89  static bool const PARAM_DEFAULT_VALUE_DO_RECONNECT;
90  static std::string const PARAM_DEFAULT_VALUE_REMOTE_HOSTNAME;
91  static std::string const PARAM_DEFAULT_VALUE_REMOTE_PORT;
92  static std::string const PARAM_DEFAULT_VALUE_LOCAL_HOSTNAME;
93  static std::string const PARAM_DEFAULT_VALUE_LOCAL_PORT;
94  static std::string const PARAM_DEFAULT_VALUE_DOMAIN;
95  static std::string const PARAM_DEFAULT_VALUE_TYPE;
96  static std::string const PARAM_DEFAULT_VALUE_PROTOCOL;
97 
98 private:
99 
100  enum socket_flag_t {
101  FLAG_LISTENING = 1,
102  FLAG_CONNECTING = 2,
103  FLAG_RAW_SOCKET = 3,
104  FLAG_CONNECTED = 4,
105  FLAG_ACTIVE_SOCKET = 5,
106  FLAG_DO_RECONNECT = 6,
107  FLAG_CLOSING = 7,
108  FLAG_TX_WOULD_BLOCK = 8,
109  FLAG_TX_WOULD_BLOCK_NOTIFIED = 9,
110  };
111 
112  std::bitset<16> sockflags;
114  enum csocket_plain_timer_t {
115  TIMER_RECONNECT = 1,
116  };
117 
118  enum csocket_plain_event_t {
119  EVENT_CONN_RESET = 1,
120  EVENT_DISCONNECTED = 2,
121  };
122 
123  static const unsigned int DEFAULT_MAX_TXQUEUE_SIZE;
124  unsigned int max_txqueue_size; // limit for pout_squeue
125 
126  ctimerid reconnect_timerid;
127  int reconnect_start_timeout;
128  int reconnect_in_seconds; // reconnect in x seconds
129  int reconnect_counter;
130 
131 #define RECONNECT_START_TIMEOUT 1 // start reconnect timeout (default 1s)
132 
133 public:
134 
135 
142  csocket_env *owner);
143 
144 
149  virtual
150  ~csocket_plain();
151 
152 
161  virtual void
162  listen(
163  cparams const& params);
164 
165 
169  virtual void
170  accept(
171  cparams const& socket_params, int sd);
172 
173 
181  virtual void
182  connect(
183  cparams const& params);
184 
185 
194  virtual void
195  reconnect();
196 
197 
205  virtual void
206  close();
207 
208 
213  virtual ssize_t
215  void *buf, size_t count) {
216  csockaddr from;
217  return recv(buf, count, 0, from);
218  };
219 
220  virtual ssize_t
221  recv(
222  void *buf, size_t count, int flags, rofl::csockaddr& from);
223 
224 
241  virtual void
242  send(
243  cmemory *mem, rofl::csockaddr const& dest = rofl::csockaddr());
244 
245 
249  virtual bool
250  is_established() const { return sockflags.test(FLAG_CONNECTED); };
251 
255  virtual bool
256  write_would_block() const { return sockflags.test(FLAG_TX_WOULD_BLOCK); };
257 
258 
262  static cparams
263  get_default_params();
264 
265 
266 protected:
267 
283  virtual void
284  listen(
285  const csockaddr& la,
286  int domain = PF_INET,
287  int type = SOCK_STREAM,
288  int protocol = 0,
289  int backlog = 10,
290  std::string devname = std::string(""));
291 
292 
293 
307  virtual void
308  connect(
309  csockaddr ra,
310  csockaddr la = csockaddr(),
311  int domain = PF_INET,
312  int type = SOCK_STREAM,
313  int protocol = 0,
314  bool do_reconnect = false);
315 
316 
317 protected:
318 
319  //
320  // socket specific methods, must be overloaded in derived class
321  //
322 
330  virtual void
332  if (socket_env) {
333  socket_env->handle_accepted(*this);
334  }
335  };
336 
344  virtual void
346  if (socket_env) {
348  }
349  };
350 
358  virtual void
360  if (socket_env) {
362  }
363  };
364 
372  virtual void
374  if (socket_env) {
376  }
377  };
378 
386  virtual void
388  if (socket_env) {
390  }
391  };
392 
402  virtual void
403  handle_listen(int newsd) {
404  if (socket_env) {
405  socket_env->handle_listen(*this, newsd);
406  }
407  };
408 
415  virtual void
417  if (socket_env) {
418  socket_env->handle_closed(*this);
419  }
420  };
421 
430  virtual void
432  if (socket_env) {
433  socket_env->handle_read(*this);
434  }
435  };
436 
445  virtual void
447  if (socket_env) {
448  socket_env->handle_write(*this);
449  }
450  };
451 
452 
453 
454 private:
455 
456 
460  void
461  backoff_reconnect(
462  bool reset_timeout = false);
463 
464 
465  /*
466  * inherited from ciosrv
467  */
468 
469 
473  virtual void
474  handle_timeout(
475  int opaque, void *data = (void*)0);
476 
480  virtual void
481  handle_event(
482  cevent const& ev);
483 
484 
494  virtual void
495  handle_revent(int fd);
496 
497 
506  virtual void
507  handle_wevent(int fd);
508 
509 
514  virtual void
515  handle_xevent(int fd);
516 
517 
518 protected:
519 
520 
527  virtual void
528  dequeue_packet();
529 
530 public:
531 
532  friend std::ostream&
533  operator<< (std::ostream& os, csocket_plain const& sock) {
534  os << dynamic_cast<csocket const&>( sock );
535  os << rofl::indent(2) << "<csocket_plain #tx-queue:" << sock.pout_squeue.size() << ">" << std::endl;
536  os << rofl::indent(4) << "<flags: ";
537  if (sock.sockflags.test(FLAG_LISTENING)) {
538  os << "LISTENING ";
539  }
540  if (sock.sockflags.test(FLAG_CONNECTING)) {
541  os << "CONNECTING ";
542  }
543  if (sock.sockflags.test(FLAG_RAW_SOCKET)) {
544  os << "RAW-SOCKET ";
545  }
546  if (sock.sockflags.test(FLAG_CONNECTED)) {
547  os << "CONNECTED ";
548  }
549  if (sock.sockflags.test(FLAG_ACTIVE_SOCKET)) {
550  os << "ACTIVE-SOCKET ";
551  }
552  if (sock.sockflags.test(FLAG_DO_RECONNECT)) {
553  os << "DO-RECONNECT ";
554  }
555  if (sock.sockflags.test(FLAG_CLOSING)) {
556  os << "CLOSING ";
557  }
558  if (sock.sockflags.test(FLAG_TX_WOULD_BLOCK)) {
559  os << "TX-WOULD-BLOCK ";
560  }
561  os << ">" << std::endl;
562  return os;
563  };
564 
565  std::string
566  str() const {
567  std::stringstream sstr;
568  sstr << "sd:" << sd << " local:" << laddr.str() << " remote:" << raddr.str()
569  << " #tx-queue:" << pout_squeue.size() << " flags: ";
570  if (sockflags.test(FLAG_LISTENING)) {
571  sstr << "LISTENING, ";
572  }
573  if (sockflags.test(FLAG_CONNECTING)) {
574  sstr << "CONNECTING, ";
575  }
576  if (sockflags.test(FLAG_RAW_SOCKET)) {
577  sstr << "RAW-SOCKET, ";
578  }
579  if (sockflags.test(FLAG_CONNECTED)) {
580  sstr << "CONNECTED, ";
581  }
582  if (sockflags.test(FLAG_ACTIVE_SOCKET)) {
583  sstr << "ACTIVE-SOCKET, ";
584  }
585  if (sockflags.test(FLAG_DO_RECONNECT)) {
586  sstr << "DO-RECONNECT, ";
587  }
588  if (sockflags.test(FLAG_CLOSING)) {
589  sstr << "CLOSING, ";
590  }
591  if (sockflags.test(FLAG_TX_WOULD_BLOCK)) {
592  sstr << "TX-WOULD-BLOCK, ";
593  }
594  return sstr.str();
595  };
596 };
597 
598 }; // end of namespace
599 
600 #endif
csocket_plain(csocket_env *owner)
Constructor for new empty csocket_plain instances.
Definition: csocket_plain.cc:45
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 void handle_read()
Definition: csocket_plain.h:431
virtual void handle_accept_refused()
Definition: csocket_plain.h:345
virtual void close()
Closes this socket.
Definition: csocket_plain.cc:926
virtual void reconnect()
Reconnect this socket.
Definition: csocket_plain.cc:910
virtual void handle_accepted(rofl::csocket &socket)=0
Called once this csocket entity has succeeded its accept() method.
std::list< pout_entry_t > pout_squeue
Definition: csocket_plain.h:86
A single socket.
Definition: csocket.h:182
virtual ssize_t recv(void *buf, size_t count)
Reads bytes from socket.
Definition: csocket_plain.h:214
Definition: csockaddr.h:38
virtual void handle_write()
Definition: csocket_plain.h:446
virtual void listen(cparams const &params)
Open socket in listening mode (server side).
Definition: csocket_plain.cc:275
virtual void handle_conn_refused()
Definition: csocket_plain.h:373
int backlog
Definition: csocket.h:487
A single unencrypted socket.This class provides basic support for socket based communication. Its aim is to encapsulate functionality for establishing a socket in active and passive mode. For using a socket, the owning class must implement the interface defined in csocket_plain_owner.
Definition: csocket_plain.h:51
virtual ~csocket_plain()
Destructor.
Definition: csocket_plain.cc:62
virtual void handle_read(rofl::csocket &socket)=0
Called once new data is available for reading from the socket.
pthread_rwlock_t pout_squeue_lock
Definition: csocket_plain.h:85
Definition: csocket_plain.h:56
int domain
Definition: csocket.h:484
virtual void dequeue_packet()
Definition: csocket_plain.cc:1081
cparams socket_params
Definition: csocket.h:488
virtual void handle_conn_failed()
Definition: csocket_plain.h:387
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.
Single event used internally by class crofl::cioloop.
Definition: cevent.h:20
csocket_env * socket_env
Definition: csocket.h:475
virtual void handle_listen(int newsd)
Definition: csocket_plain.h:403
virtual void send(cmemory *mem, rofl::csockaddr const &dest=rofl::csockaddr())
Store a packet for transmission.
Definition: csocket_plain.cc:1038
int sd
Definition: csocket.h:481
virtual void accept(cparams const &socket_params, int sd)
Handle accepted socket descriptor obtained from external listening socket.
Definition: csocket_plain.cc:523
virtual void handle_accepted()
Definition: csocket_plain.h:331
virtual void handle_connected(rofl::csocket &socket)=0
Called once a connection request has succeeded its connect() method.
Definition: logging.h:76
virtual void handle_connected()
Definition: csocket_plain.h:359
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 handle_closed()
Definition: csocket_plain.h:416
csockaddr laddr
Definition: csocket.h:482
int protocol
Definition: csocket.h:486
virtual void handle_write(rofl::csocket &socket)=0
Called once the socket accept additional data for sending.
virtual void connect(cparams const &params)
Open socket and connect to peer entity (client side).
Definition: csocket_plain.cc:588