blob: c79f79aa33dceae0aa4643b77be27d27223ad1df [file] [log] [blame]
dburgess82c46ff2011-10-07 02:40:51 +00001/*
2* Copyright 2008, 2010 Free Software Foundation, Inc.
3*
4* This software is distributed under the terms of the GNU Affero Public License.
5* See the COPYING file in the main directory for details.
6*
7* This use of this software may be subject to additional restrictions.
8* See the LEGAL file in the main directory for details.
9
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU Affero General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Affero General Public License for more details.
19
20 You should have received a copy of the GNU Affero General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
22
23*/
24
25
26#ifndef SOCKETS_H
27#define SOCKETS_H
28
29#include <arpa/inet.h>
30#include <netdb.h>
31#include <sys/socket.h>
32#include <sys/types.h>
33#include <sys/un.h>
34#include <errno.h>
35#include <list>
36#include <assert.h>
37#include <stdint.h>
38#include <stdio.h>
39
40
41
42
43
44#define MAX_UDP_LENGTH 1500
45
46/** A function to resolve IP host names. */
47bool resolveAddress(struct sockaddr_in *address, const char *host, unsigned short port);
48
49/** Resolve an address of the form "<host>:<port>". */
50bool resolveAddress(struct sockaddr_in *address, const char *hostAndPort);
51
52/** An exception to throw when a critical socket operation fails. */
53class SocketError {};
54#define SOCKET_ERROR {throw SocketError(); }
55
56/** Abstract class for connectionless sockets. */
57class DatagramSocket {
58
59protected:
60
61 int mSocketFD; ///< underlying file descriptor
62 char mDestination[256]; ///< address to which packets are sent
63 char mSource[256]; ///< return address of most recent received packet
64
65public:
66
67 /** An almost-does-nothing constructor. */
68 DatagramSocket();
69
70 virtual ~DatagramSocket();
71
72 /** Return the address structure size for this socket type. */
73 virtual size_t addressSize() const = 0;
74
75 /**
76 Send a binary packet.
77 @param buffer The data bytes to send to mDestination.
78 @param length Number of bytes to send, or strlen(buffer) if defaulted to -1.
79 @return number of bytes written, or -1 on error.
80 */
81 int write( const char * buffer, size_t length);
82
83 /**
84 Send a C-style string packet.
85 @param buffer The data bytes to send to mDestination.
86 @return number of bytes written, or -1 on error.
87 */
88 int write( const char * buffer);
89
90 /**
91 Send a binary packet.
92 @param buffer The data bytes to send to mSource.
93 @param length Number of bytes to send, or strlen(buffer) if defaulted to -1.
94 @return number of bytes written, or -1 on error.
95 */
96 int writeBack(const char * buffer, size_t length);
97
98 /**
99 Send a C-style string packet.
100 @param buffer The data bytes to send to mSource.
101 @return number of bytes written, or -1 on error.
102 */
103 int writeBack(const char * buffer);
104
105
106 /**
107 Receive a packet.
108 @param buffer A char[MAX_UDP_LENGTH] procured by the caller.
109 @return The number of bytes received or -1 on non-blocking pass.
110 */
111 int read(char* buffer);
112
113 /**
114 Receive a packet with a timeout.
115 @param buffer A char[MAX_UDP_LENGTH] procured by the caller.
116 @param maximum wait time in milliseconds
117 @return The number of bytes received or -1 on timeout.
118 */
119 int read(char* buffer, unsigned timeout);
120
121
122 /** Send a packet to a given destination, other than the default. */
123 int send(const struct sockaddr *dest, const char * buffer, size_t length);
124
125 /** Send a C-style string to a given destination, other than the default. */
126 int send(const struct sockaddr *dest, const char * buffer);
127
128 /** Make the socket non-blocking. */
129 void nonblocking();
130
131 /** Make the socket blocking (the default). */
132 void blocking();
133
134 /** Close the socket. */
135 void close();
136
137};
138
139
140
141/** UDP/IP User Datagram Socket */
142class UDPSocket : public DatagramSocket {
143
144public:
145
146 /** Open a USP socket with an OS-assigned port and no default destination. */
147 UDPSocket( unsigned short localPort=0);
148
149 /** Given a full specification, open the socket and set the dest address. */
150 UDPSocket( unsigned short localPort,
151 const char * remoteIP, unsigned short remotePort);
152
153 /** Set the destination port. */
154 void destination( unsigned short wDestPort, const char * wDestIP );
155
156 /** Return the actual port number in use. */
157 unsigned short port() const;
158
159 /** Open and bind the UDP socket to a local port. */
160 void open(unsigned short localPort=0);
161
162 /** Give the return address of the most recently received packet. */
163 const struct sockaddr_in* source() const { return (const struct sockaddr_in*)mSource; }
164
165 size_t addressSize() const { return sizeof(struct sockaddr_in); }
166
167};
168
169
170/** Unix Domain Datagram Socket */
171class UDDSocket : public DatagramSocket {
172
173public:
174
175 UDDSocket(const char* localPath=NULL, const char* remotePath=NULL);
176
177 void destination(const char* remotePath);
178
179 void open(const char* localPath);
180
181 /** Give the return address of the most recently received packet. */
182 const struct sockaddr_un* source() const { return (const struct sockaddr_un*)mSource; }
183
184 size_t addressSize() const { return sizeof(struct sockaddr_un); }
185
186};
187
188
189#endif
190
191
192
193// vim:ts=4:sw=4