summaryrefslogtreecommitdiffstats
path: root/src/sockets/socket.h
blob: 5bfef9f9d4efe75a9493fadcf7851814b4cd6468 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
    Copyright (C) 2005-2009  Michel de Boer <michel@twinklephone.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

/** 
 * @file
 * Socket operations
 */

#ifndef _H_SOCKET
#define _H_SOCKET

#include <string>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>

using namespace std;

// ports and addresses should be in host order

/** ICMP message */
class t_icmp_msg {
public:
	short		type;
	short		code;
	
	// ICMP source IP address
	unsigned long	icmp_src_ipaddr;
	
	// Destination IP address/port of packet causing the ICMP message.
	unsigned long	ipaddr;
	unsigned short	port;
	
	t_icmp_msg() {};
	t_icmp_msg(short _type, short _code, unsigned long _icmp_src_ipaddr,
		unsigned long _ipaddr, unsigned short _port);
};

/** Abstract socket */
class t_socket {
protected:
	int	sd; /**< Socket descriptor. */
	
	/**
	 * Constructor. This constructor does not create a valid socket.
	 * The subclasses will create the real socket.
	 */
	t_socket();

	/** 
	 * Constructor.
	 * @param _sd Socket desciptor.
	 */
	t_socket(int _sd);
	
public:
	/** Destructor */
	virtual ~t_socket();
	
	/**
	 * Get the socket descriptor.
	 * @return The socket descriptor.
	 */
	int get_descriptor(void) const;
	
	/** Get socket options */
	int getsockopt(int level, int optname, void *optval, socklen_t *optlen);
	
	/** Set socket options */
	int setsockopt(int level, int optname, const void *optval, socklen_t optlen);
	
	/** Receive data */
	virtual ssize_t recv(void *buf, int buf_size) = 0;
	
	/** Send data */
	virtual ssize_t send(const void *data, int data_size) = 0;
};

/** UDP socket */
class t_socket_udp : public t_socket {
public:
	// Create a socket and bind it to any port.
	// Throws an int exception if it fails. The int thrown is the value
	// of errno as set by 'socket' or 'bind'.
	t_socket_udp();

	// Create a socket and bind it to port.
	// Throws an int exception if it fails. The int thrown is the value
	// of errno as set by 'socket' or 'bind'.
	t_socket_udp(unsigned short port);
	
	// Connect a socket
	// Throws an int exception if it fails (errno as set by 'sendto')
	int connect(unsigned long dest_addr, unsigned short dest_port);

	// Throws an int exception if it fails (errno as set by 'sendto')
	int sendto(unsigned long dest_addr, unsigned short dest_port,
	           const char *data, int data_size);
	virtual ssize_t send(const void *data, int data_size);

	// Throws an int exception if it fails (errno as set by 'recvfrom')
	// On success the length of the data in buf is returned. After the
	// data in buf there will be a 0.
	int recvfrom(unsigned long &src_addr, unsigned short &src_port,
		     char *buf, int buf_size);
	
	virtual ssize_t recv(void *buf, int buf_size);
	
	// Do a select on the socket in read mode. timeout is in ms.
	// Returns true if the socket becomes unblocked. Returns false
	// on time out. Throws an int exception if select fails
	// (errno as set by 'select')
	bool select_read(unsigned long timeout);
	
	// Enable reception of ICMP errors on this socket.
	// Returns false if ICMP reception cannot be enabled.
	bool enable_icmp(void);
	
	// Get an ICMP message that was received on this socket.
	// Returns false if no ICMP message can be retrieved.
	bool get_icmp(t_icmp_msg &icmp);
};

/** TCP socket */
class t_socket_tcp : public t_socket {
public:
	/** 
	 * Constructor. Create a socket. 
	 * @throw int The errno value
	 */
	t_socket_tcp();
	
	/** 
	 * Constructor. Create a socket and bind it to a port.
	 * @throw int The errno value
	 */
	t_socket_tcp(unsigned short port);
	
	/**
	 * Constructor. Create a socket from an existing descriptor.
	 */
	t_socket_tcp(int _sd);
	
	/**
	 * Listen for a connection.
	 * @throw int Errno
	 */
	void listen(int backlog);
	 
	
	/** 
	 * Accept a connection 
	 * @param src_addr [out] Source IPv4 address of the connection.
	 * @param src_port [out] Source port of the connection.
	 * @return A socket for the new connection
	 * @throw int Errno
	 */
	t_socket_tcp *accept(unsigned long &src_addr, unsigned short &src_port);
	
	/**
	 * Connect to a destination
	 * @param dest_addr [in] Destination IPv4 address.
	 * @param dest_port [in] Destination port.
	 * @throw int Errno
	 */
	void connect(unsigned long dest_addr, unsigned short dest_port);
	
	/** Send data */
	virtual ssize_t send(const void *data, int data_size);
	
	
	/** Receive data */
	virtual ssize_t recv(void *buf, int buf_size);
	
	/**
	 * Get the remote address of a connection.
	 * @param remote_addr [out] Source IPv4 address of the connection.
	 * @param remote_port [out] Source port of the connection.
	 * @throw int Errno
	 */
	void get_remote_address(unsigned long &remote_addr, unsigned short &remote_port);
};

/** Local socket */
class t_socket_local : public t_socket {	
public:
	// Throws an int exception if it fails. The int thrown is the value
	// of errno as set by 'socket'
	t_socket_local();
	
	t_socket_local(int _sd);
	
	void bind(const string &name);
	void listen(int backlog);
	int accept(void);
	void connect(const string &name);
	int read(void *buf, int count);
	virtual ssize_t recv(void *buf, int buf_size);
	int write(const void *buf, int count);
	virtual ssize_t send(const void *buf, int count);
};

// Convert an IP address in host order to a string.
string h_ip2str(unsigned long ipaddr);

#endif