aboutsummaryrefslogtreecommitdiffstats
path: root/CommonLibs/Sockets.h
blob: c79f79aa33dceae0aa4643b77be27d27223ad1df (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
/*
* Copyright 2008, 2010 Free Software Foundation, Inc.
*
* This software is distributed under the terms of the GNU Affero Public License.
* See the COPYING file in the main directory for details.
*
* This use of this software may be subject to additional restrictions.
* See the LEGAL file in the main directory for details.

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Affero General Public License as published by
	the Free Software Foundation, either version 3 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 Affero General Public License for more details.

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

*/


#ifndef SOCKETS_H
#define SOCKETS_H

#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <errno.h>
#include <list>
#include <assert.h>
#include <stdint.h>
#include <stdio.h>





#define MAX_UDP_LENGTH 1500

/** A function to resolve IP host names. */
bool resolveAddress(struct sockaddr_in *address, const char *host, unsigned short port);

/** Resolve an address of the form "<host>:<port>". */
bool resolveAddress(struct sockaddr_in *address, const char *hostAndPort);

/** An exception to throw when a critical socket operation fails. */
class SocketError {};
#define SOCKET_ERROR {throw SocketError(); }

/** Abstract class for connectionless sockets. */
class DatagramSocket {

protected:

	int mSocketFD;				///< underlying file descriptor
	char mDestination[256];		///< address to which packets are sent
	char mSource[256];		///< return address of most recent received packet

public:

	/** An almost-does-nothing constructor. */
	DatagramSocket();

	virtual ~DatagramSocket();

	/** Return the address structure size for this socket type. */
	virtual size_t addressSize() const = 0;

	/**
		Send a binary packet.
		@param buffer The data bytes to send to mDestination.
		@param length Number of bytes to send, or strlen(buffer) if defaulted to -1.
		@return number of bytes written, or -1 on error.
	*/
	int write( const char * buffer, size_t length);

	/**
		Send a C-style string packet.
		@param buffer The data bytes to send to mDestination.
		@return number of bytes written, or -1 on error.
	*/
	int write( const char * buffer);

	/**
		Send a binary packet.
		@param buffer The data bytes to send to mSource.
		@param length Number of bytes to send, or strlen(buffer) if defaulted to -1.
		@return number of bytes written, or -1 on error.
	*/
	int writeBack(const char * buffer, size_t length);

	/**
		Send a C-style string packet.
		@param buffer The data bytes to send to mSource.
		@return number of bytes written, or -1 on error.
	*/
	int writeBack(const char * buffer);


	/**
		Receive a packet.
		@param buffer A char[MAX_UDP_LENGTH] procured by the caller.
		@return The number of bytes received or -1 on non-blocking pass.
	*/
	int read(char* buffer);

	/**
		Receive a packet with a timeout.
		@param buffer A char[MAX_UDP_LENGTH] procured by the caller.
		@param maximum wait time in milliseconds
		@return The number of bytes received or -1 on timeout.
	*/
	int read(char* buffer, unsigned timeout);


	/** Send a packet to a given destination, other than the default. */
	int send(const struct sockaddr *dest, const char * buffer, size_t length);

	/** Send a C-style string to a given destination, other than the default. */
	int send(const struct sockaddr *dest, const char * buffer);

	/** Make the socket non-blocking. */
	void nonblocking();

	/** Make the socket blocking (the default). */
	void blocking();

	/** Close the socket. */
	void close();

};



/** UDP/IP User Datagram Socket */
class UDPSocket : public DatagramSocket {

public:

	/** Open a USP socket with an OS-assigned port and no default destination. */
	UDPSocket( unsigned short localPort=0);

	/** Given a full specification, open the socket and set the dest address. */
	UDPSocket( 	unsigned short localPort, 
			const char * remoteIP, unsigned short remotePort);

	/** Set the destination port. */
	void destination( unsigned short wDestPort, const char * wDestIP );

	/** Return the actual port number in use. */
	unsigned short port() const;

	/** Open and bind the UDP socket to a local port. */
	void open(unsigned short localPort=0);

	/** Give the return address of the most recently received packet. */
	const struct sockaddr_in* source() const { return (const struct sockaddr_in*)mSource; }

	size_t addressSize() const { return sizeof(struct sockaddr_in); }

};


/** Unix Domain Datagram Socket */
class UDDSocket : public DatagramSocket {

public:

	UDDSocket(const char* localPath=NULL, const char* remotePath=NULL);

	void destination(const char* remotePath);

	void open(const char* localPath);

	/** Give the return address of the most recently received packet. */
	const struct sockaddr_un* source() const { return (const struct sockaddr_un*)mSource; }

	size_t addressSize() const { return sizeof(struct sockaddr_un); }

};


#endif



// vim:ts=4:sw=4