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#include <netinet/in.h>
30#include <sys/socket.h>
31#include <sys/types.h>
32// just to be safe nobody else can do it
33#include <sys/signal.h>
34
35#ifdef POLL_IN
36# undef POLL_IN
37#endif
38#ifdef POLL_OUT
39# undef POLL_OUT
40#endif
41#ifdef POLL_PRI
42# undef POLL_PRI
43#endif
44#ifdef POLL_RDHUP
45# undef POLL_RDHUP
46#endif
47#ifdef POLL_ERR
48# undef POLL_ERR
49#endif
50#ifdef POLL_HUP
51# undef POLL_HUP
52#endif
53
54namespace fawkes {
55
57{
58public:
59 SocketException(int _errno, const char *msg);
60 SocketException(const char *format, ...);
61};
62
63class Socket
64{
65public:
66 static const short POLL_IN;
67 static const short POLL_OUT;
68 static const short POLL_PRI;
69 static const short POLL_RDHUP;
70 static const short POLL_ERR;
71 static const short POLL_HUP;
72 static const short POLL_NVAL;
73
74 /** Address type specification. */
75 typedef enum {
76 UNSPECIFIED, /**< Yet unknown address type */
77 IPv4, /**< IPv4 */
78 IPv6 /**< IPv6 */
80
81 /** Socket type. */
82 typedef enum {
83 TCP, /**< TCP stream socket */
84 UDP /**< UDP datagram socket */
86
87 Socket(AddrType addr_type, SocketType sock_type, float timeout = 0.f);
88 Socket(Socket &socket);
89 virtual ~Socket();
90
91 Socket &operator=(Socket &socket);
92
93 virtual void connect(const char *hostname, const unsigned short int port);
94 virtual void connect(const struct ::sockaddr_storage &addr_port);
95 virtual void connect(const struct sockaddr *addr_port, socklen_t struct_size);
96
97 virtual void bind(const unsigned short int port);
98 virtual void bind(const unsigned short int port, const char *ipaddr);
99
100 virtual void listen(int backlog = 1);
101 virtual Socket *accept();
102 virtual void close();
103 virtual bool available();
104
105 virtual size_t read(void *buf, size_t count, bool read_all = true);
106 virtual void write(const void *buf, size_t count);
107 virtual void send(void *buf, size_t buf_len);
108 virtual void send(void *buf, size_t buf_len, const struct sockaddr *to_addr, socklen_t addr_len);
109 virtual size_t recv(void *buf, size_t buf_len);
110 virtual size_t recv(void *buf, size_t buf_len, struct sockaddr *from_addr, socklen_t *addr_len);
111
112 /** Clone socket.
113 * This method has to be implemented by subclass to correctly clone the instance.
114 * @return cloned socket
115 */
116 virtual Socket *clone() = 0;
117
118 virtual short poll(int timeout = -1, short what = POLL_IN | POLL_HUP | POLL_PRI | POLL_RDHUP);
119
120 virtual bool listening();
121
122 virtual unsigned int mtu();
123
124 /** Accept connection.
125 * This method works like accept() but it ensures that the returned socket is of
126 * the given type.
127 * @return socket to client
128 */
129 template <class SocketTypeC>
130 SocketTypeC *accept();
131
132protected:
133 Socket(SocketType sock_type, float timeout = 0.f);
134 Socket();
135
138 float timeout;
139 struct ::sockaddr_storage *client_addr;
140 unsigned int client_addr_len;
141
142private:
143 int socket_addr_family_;
144 int socket_type_;
145 int socket_protocol_;
146
147 void create();
148};
149
150template <class SocketTypeC>
151SocketTypeC *
153{
154 Socket *s = accept();
155 if (SocketTypeC *ts = dynamic_cast<SocketTypeC *>(s)) {
156 return ts;
157 } else {
158 delete s;
159 throw TypeMismatchException("Socket types do not match");
160 }
161}
162
163} // end namespace fawkes
164
165#endif
Base class for exceptions in Fawkes.
Definition: exception.h:36
int _errno
Error number, should be used if the error was caused by a method that supplies errno.
Definition: exception.h:113
Socket exception.
Definition: socket.h:57
SocketException(int _errno, const char *msg)
Constructor.
Definition: socket.cpp:87
Socket base class.
Definition: socket.h:64
virtual Socket * clone()=0
Clone socket.
virtual bool available()
Check if data is available.
Definition: socket.cpp:644
virtual void bind(const unsigned short int port)
Bind socket.
Definition: socket.cpp:465
static const short POLL_RDHUP
Stream socket peer closed connection, or shut down writing half of connection.
Definition: socket.h:69
int sock_fd
Socket file descriptor.
Definition: socket.h:137
static const short POLL_NVAL
Invalid request.
Definition: socket.h:72
Socket()
Constructor.
Definition: socket.cpp:211
AddrType
Address type specification.
Definition: socket.h:75
@ UNSPECIFIED
Yet unknown address type.
Definition: socket.h:76
@ IPv4
IPv4.
Definition: socket.h:77
@ IPv6
IPv6.
Definition: socket.h:78
virtual unsigned int mtu()
Maximum Transfer Unit (MTU) of socket.
Definition: socket.cpp:974
virtual void connect(const char *hostname, const unsigned short int port)
Connect socket.
Definition: socket.cpp:376
virtual size_t recv(void *buf, size_t buf_len)
Read from socket.
Definition: socket.cpp:865
SocketType
Socket type.
Definition: socket.h:82
@ TCP
TCP stream socket.
Definition: socket.h:83
@ UDP
UDP datagram socket.
Definition: socket.h:84
virtual size_t read(void *buf, size_t count, bool read_all=true)
Read from socket.
Definition: socket.cpp:760
static const short POLL_HUP
Hang up.
Definition: socket.h:71
virtual bool listening()
Is socket listening for connections?
Definition: socket.cpp:956
Socket & operator=(Socket &socket)
Copy constructor.
Definition: socket.cpp:250
static const short POLL_IN
Data can be read.
Definition: socket.h:66
virtual void send(void *buf, size_t buf_len)
Write to the socket.
Definition: socket.cpp:846
AddrType addr_type
Address type/family of socket.
Definition: socket.h:136
static const short POLL_OUT
Writing will not block.
Definition: socket.h:67
virtual short poll(int timeout=-1, short what=POLL_IN|POLL_HUP|POLL_PRI|POLL_RDHUP)
Wait for some event on socket.
Definition: socket.cpp:685
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:68
virtual ~Socket()
Destructor.
Definition: socket.cpp:300
float timeout
Timeout in seconds for various operations.
Definition: socket.h:138
struct::sockaddr_storage * client_addr
Client address, set if connected.
Definition: socket.h:139
virtual Socket * accept()
Accept connection.
Definition: socket.cpp:599
static const short POLL_ERR
Error condition.
Definition: socket.h:70
virtual void listen(int backlog=1)
Listen on socket.
Definition: socket.cpp:582
unsigned int client_addr_len
length in bytes of client address.
Definition: socket.h:140
virtual void close()
Close socket.
Definition: socket.cpp:311
virtual void write(const void *buf, size_t count)
Write to the socket.
Definition: socket.cpp:713
Fawkes library namespace.