dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 1 | /* |
| 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. */ |
| 47 | bool resolveAddress(struct sockaddr_in *address, const char *host, unsigned short port); |
| 48 | |
| 49 | /** Resolve an address of the form "<host>:<port>". */ |
| 50 | bool resolveAddress(struct sockaddr_in *address, const char *hostAndPort); |
| 51 | |
| 52 | /** An exception to throw when a critical socket operation fails. */ |
| 53 | class SocketError {}; |
| 54 | #define SOCKET_ERROR {throw SocketError(); } |
| 55 | |
| 56 | /** Abstract class for connectionless sockets. */ |
| 57 | class DatagramSocket { |
| 58 | |
| 59 | protected: |
| 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 | |
| 65 | public: |
| 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 | */ |
Tom Tsou | 2c650a6 | 2016-04-28 21:55:17 -0700 | [diff] [blame] | 111 | int read(char* buffer, size_t length); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 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 | */ |
Tom Tsou | 2c650a6 | 2016-04-28 21:55:17 -0700 | [diff] [blame] | 119 | int read(char* buffer, size_t length, unsigned timeout); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 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 */ |
| 142 | class UDPSocket : public DatagramSocket { |
| 143 | |
| 144 | public: |
| 145 | |
| 146 | /** Open a USP socket with an OS-assigned port and no default destination. */ |
Pau Espin Pedrol | 8c80095 | 2017-08-16 16:53:23 +0200 | [diff] [blame] | 147 | UDPSocket(const char *localIP, unsigned short localPort); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 148 | |
| 149 | /** Given a full specification, open the socket and set the dest address. */ |
Pau Espin Pedrol | 8c80095 | 2017-08-16 16:53:23 +0200 | [diff] [blame] | 150 | UDPSocket(const char *localIP, unsigned short localPort, |
| 151 | const char *remoteIP, unsigned short remotePort); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 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. */ |
Pau Espin Pedrol | 8c80095 | 2017-08-16 16:53:23 +0200 | [diff] [blame] | 160 | void open(unsigned short localPort=0, const char *wlocalIP="127.0.0.1"); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 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 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 169 | #endif |
| 170 | |
| 171 | |
| 172 | |
| 173 | // vim:ts=4:sw=4 |