Fawkes API  Fawkes Development Version
socket.h
1 
2 /***************************************************************************
3  * socket.h - Fawkes socket base class
4  *
5  * Created: Thu Nov 09 12:55:25 2006
6  * Copyright 2006 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef __NETCOMM_SOCKET_SOCKET_H_
25 #define __NETCOMM_SOCKET_SOCKET_H_
26 
27 #include <core/exception.h>
28 #include <core/exceptions/software.h>
29 
30 #include <sys/socket.h>
31 #include <sys/types.h>
32 #include <netinet/in.h>
33 // just to be safe nobody else can do it
34 #include <sys/signal.h>
35 
36 #ifdef POLL_IN
37 # undef POLL_IN
38 #endif
39 #ifdef POLL_OUT
40 # undef POLL_OUT
41 #endif
42 #ifdef POLL_PRI
43 # undef POLL_PRI
44 #endif
45 #ifdef POLL_RDHUP
46 # undef POLL_RDHUP
47 #endif
48 #ifdef POLL_ERR
49 # undef POLL_ERR
50 #endif
51 #ifdef POLL_HUP
52 # undef POLL_HUP
53 #endif
54 
55 
56 namespace fawkes {
57 
58 class SocketException : public Exception
59 {
60  public:
61  SocketException(int _errno, const char *msg);
62  SocketException(const char *format, ...);
63 };
64 
65 class Socket
66 {
67  public:
68 
69  static const short POLL_IN;
70  static const short POLL_OUT;
71  static const short POLL_PRI;
72  static const short POLL_RDHUP;
73  static const short POLL_ERR;
74  static const short POLL_HUP;
75  static const short POLL_NVAL;
76 
77  /** Address type specification. */
78  typedef enum {
79  UNSPECIFIED, /**< Yet unknown address type */
80  IPv4, /**< IPv4 */
81  IPv6 /**< IPv6 */
82  } AddrType;
83 
84  /** Socket type. */
85  typedef enum {
86  TCP, /**< TCP stream socket */
87  UDP /**< UDP datagram socket */
88  } SocketType;
89 
90  Socket(AddrType addr_type, SocketType sock_type, float timeout = 0.f);
91  Socket(Socket &socket);
92  virtual ~Socket();
93 
94  virtual void connect(const char *hostname, const unsigned short int port);
95  virtual void connect(const struct ::sockaddr_storage &addr_port);
96  virtual void connect(const struct sockaddr *addr_port, socklen_t struct_size);
97 
98  virtual void bind(const unsigned short int port);
99  virtual void bind(const unsigned short int port, const char *ipaddr);
100 
101  virtual void listen(int backlog = 1);
102  virtual Socket * accept();
103  virtual void close();
104  virtual bool available();
105 
106  virtual size_t read(void *buf, size_t count, bool read_all = true);
107  virtual void write(const void *buf, size_t count);
108  virtual void send(void *buf, size_t buf_len);
109  virtual void send(void *buf, size_t buf_len,
110  const struct sockaddr *to_addr, socklen_t addr_len);
111  virtual size_t recv(void *buf, size_t buf_len);
112  virtual size_t recv(void *buf, size_t buf_len,
113  struct sockaddr *from_addr, socklen_t *addr_len);
114 
115  /** Clone socket.
116  * This method has to be implemented by subclass to correctly clone the instance.
117  * @return cloned socket
118  */
119  virtual Socket * clone() = 0;
120 
121  virtual short poll(int timeout = -1, short what = POLL_IN | POLL_HUP | POLL_PRI | POLL_RDHUP);
122 
123  virtual bool listening();
124 
125  virtual unsigned int mtu();
126 
127  /** Accept connection.
128  * This method works like accept() but it ensures that the returned socket is of
129  * the given type.
130  * @return socket to client
131  */
132  template <class SocketTypeC>
133  SocketTypeC * accept();
134 
135  protected:
136  Socket(SocketType sock_type, float timeout = 0.f);
137  Socket();
138 
139  AddrType addr_type;
140  int sock_fd;
141  float timeout;
142  struct ::sockaddr_storage *client_addr;
143  unsigned int client_addr_len;
144 
145 
146  private:
147  int socket_addr_family_;
148  int socket_type_;
149  int socket_protocol_;
150 
151  void create();
152 
153 };
154 
155 
156 template <class SocketTypeC>
157 SocketTypeC *
159 {
160  Socket *s = accept();
161  if (SocketTypeC *ts = dynamic_cast<SocketTypeC *>(s)) {
162  return ts;
163  } else {
164  delete s;
165  throw TypeMismatchException("Socket types do not match");
166  }
167 }
168 
169 } // end namespace fawkes
170 
171 #endif
static const short POLL_ERR
Error condition.
Definition: socket.h:73
Yet unknown address type.
Definition: socket.h:79
TCP stream socket.
Definition: socket.h:86
static const short POLL_PRI
There is urgent data to read (e.g., out-of-band data on TCP socket; pseudo-terminal master in packet ...
Definition: socket.h:71
Fawkes library namespace.
static const short POLL_IN
Data can be read.
Definition: socket.h:69
struct ::sockaddr_storage * client_addr
Client address, set if connected.
Definition: socket.h:142
AddrType
Address type specification.
Definition: socket.h:78
virtual const char * what() const
Get primary string.
Definition: exception.cpp:661
int _errno
Error number, should be used if the error was caused by a method that supplies errno.
Definition: exception.h:111
Socket base class.
Definition: socket.h:65
unsigned int client_addr_len
length in bytes of client address.
Definition: socket.h:143
virtual Socket * accept()
Accept connection.
Definition: socket.cpp:565
Base class for exceptions in Fawkes.
Definition: exception.h:36
static const short POLL_RDHUP
Stream socket peer closed connection, or shut down writing half of connection.
Definition: socket.h:72
AddrType addr_type
Address type/family of socket.
Definition: socket.h:139
int sock_fd
Socket file descriptor.
Definition: socket.h:140
SocketType
Socket type.
Definition: socket.h:85
static const short POLL_HUP
Hang up.
Definition: socket.h:74
static const short POLL_NVAL
Invalid request.
Definition: socket.h:75
float timeout
Timeout in seconds for various operations.
Definition: socket.h:141
SocketException(int _errno, const char *msg)
Constructor.
Definition: socket.cpp:89
Socket exception.
Definition: socket.h:58
static const short POLL_OUT
Writing will not block.
Definition: socket.h:70