dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008, 2010 Free Software Foundation, Inc. |
| 3 | * |
| 4 | * |
| 5 | * This software is distributed under the terms of the GNU Affero Public License. |
| 6 | * See the COPYING file in the main directory for details. |
| 7 | * |
| 8 | * This use of this software may be subject to additional restrictions. |
| 9 | * See the LEGAL file in the main directory for details. |
| 10 | |
| 11 | This program is free software: you can redistribute it and/or modify |
| 12 | it under the terms of the GNU Affero General Public License as published by |
| 13 | the Free Software Foundation, either version 3 of the License, or |
| 14 | (at your option) any later version. |
| 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 Affero General Public License for more details. |
| 20 | |
| 21 | You should have received a copy of the GNU Affero General Public License |
| 22 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | |
| 28 | #include <unistd.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <cstdio> |
| 31 | #include <sys/select.h> |
| 32 | |
| 33 | #include "Threads.h" |
| 34 | #include "Sockets.h" |
| 35 | #include <stdio.h> |
| 36 | #include <unistd.h> |
| 37 | #include <fcntl.h> |
| 38 | |
| 39 | #include <string.h> |
| 40 | #include <stdlib.h> |
| 41 | |
| 42 | |
kurtis.heimerl | d009319 | 2011-12-24 02:53:10 +0000 | [diff] [blame] | 43 | //mutex for protecting non-thread safe gethostbyname |
| 44 | static Mutex sgGethostbynameLock; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 45 | |
| 46 | bool resolveAddress(struct sockaddr_in *address, const char *hostAndPort) |
| 47 | { |
| 48 | assert(address); |
| 49 | assert(hostAndPort); |
| 50 | char *copy = strdup(hostAndPort); |
| 51 | char *colon = strchr(copy,':'); |
| 52 | if (!colon) return false; |
| 53 | *colon = '\0'; |
| 54 | char *host = copy; |
| 55 | unsigned port = strtol(colon+1,NULL,10); |
| 56 | bool retVal = resolveAddress(address,host,port); |
| 57 | free(copy); |
| 58 | return retVal; |
| 59 | } |
| 60 | |
| 61 | bool resolveAddress(struct sockaddr_in *address, const char *host, unsigned short port) |
| 62 | { |
| 63 | assert(address); |
| 64 | assert(host); |
kurtis.heimerl | d009319 | 2011-12-24 02:53:10 +0000 | [diff] [blame] | 65 | //gethostbyname not thread safe |
| 66 | ScopedLock lock(sgGethostbynameLock); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 67 | // FIXME -- Need to ignore leading/trailing spaces in hostname. |
| 68 | struct hostent *hp = gethostbyname(host); |
| 69 | if (hp==NULL) { |
| 70 | CERR("WARNING -- gethostbyname() failed for " << host << ", " << hstrerror(h_errno)); |
| 71 | return false; |
| 72 | } |
| 73 | address->sin_family = AF_INET; |
| 74 | bcopy(hp->h_addr, &(address->sin_addr), hp->h_length); |
| 75 | address->sin_port = htons(port); |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | |
| 81 | DatagramSocket::DatagramSocket() |
| 82 | { |
| 83 | bzero(mDestination,sizeof(mDestination)); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | void DatagramSocket::nonblocking() |
| 91 | { |
| 92 | fcntl(mSocketFD,F_SETFL,O_NONBLOCK); |
| 93 | } |
| 94 | |
| 95 | void DatagramSocket::blocking() |
| 96 | { |
| 97 | fcntl(mSocketFD,F_SETFL,0); |
| 98 | } |
| 99 | |
| 100 | void DatagramSocket::close() |
| 101 | { |
| 102 | ::close(mSocketFD); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | DatagramSocket::~DatagramSocket() |
| 107 | { |
| 108 | close(); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | int DatagramSocket::write( const char * message, size_t length ) |
| 116 | { |
| 117 | assert(length<=MAX_UDP_LENGTH); |
| 118 | int retVal = sendto(mSocketFD, message, length, 0, |
| 119 | (struct sockaddr *)mDestination, addressSize()); |
| 120 | if (retVal == -1 ) perror("DatagramSocket::write() failed"); |
| 121 | return retVal; |
| 122 | } |
| 123 | |
| 124 | int DatagramSocket::writeBack( const char * message, size_t length ) |
| 125 | { |
| 126 | assert(length<=MAX_UDP_LENGTH); |
| 127 | int retVal = sendto(mSocketFD, message, length, 0, |
| 128 | (struct sockaddr *)mSource, addressSize()); |
| 129 | if (retVal == -1 ) perror("DatagramSocket::write() failed"); |
| 130 | return retVal; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | |
| 135 | int DatagramSocket::write( const char * message) |
| 136 | { |
| 137 | size_t length=strlen(message)+1; |
| 138 | return write(message,length); |
| 139 | } |
| 140 | |
| 141 | int DatagramSocket::writeBack( const char * message) |
| 142 | { |
| 143 | size_t length=strlen(message)+1; |
| 144 | return writeBack(message,length); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | |
| 149 | int DatagramSocket::send(const struct sockaddr* dest, const char * message, size_t length ) |
| 150 | { |
| 151 | assert(length<=MAX_UDP_LENGTH); |
| 152 | int retVal = sendto(mSocketFD, message, length, 0, dest, addressSize()); |
| 153 | if (retVal == -1 ) perror("DatagramSocket::send() failed"); |
| 154 | return retVal; |
| 155 | } |
| 156 | |
| 157 | int DatagramSocket::send(const struct sockaddr* dest, const char * message) |
| 158 | { |
| 159 | size_t length=strlen(message)+1; |
| 160 | return send(dest,message,length); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | int DatagramSocket::read(char* buffer) |
| 168 | { |
| 169 | socklen_t temp_len = sizeof(mSource); |
| 170 | int length = recvfrom(mSocketFD, (void*)buffer, MAX_UDP_LENGTH, 0, |
| 171 | (struct sockaddr*)&mSource,&temp_len); |
| 172 | if ((length==-1) && (errno!=EAGAIN)) { |
| 173 | perror("DatagramSocket::read() failed"); |
| 174 | throw SocketError(); |
| 175 | } |
| 176 | return length; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | int DatagramSocket::read(char* buffer, unsigned timeout) |
| 181 | { |
| 182 | fd_set fds; |
kurtis.heimerl | 0aaabd4 | 2012-11-23 08:35:46 +0000 | [diff] [blame] | 183 | FD_ZERO(&fds); |
kurtis.heimerl | 9eafbe9 | 2012-11-23 08:38:04 +0000 | [diff] [blame^] | 184 | FD_SET(mSocketFD,&fds); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 185 | struct timeval tv; |
| 186 | tv.tv_sec = timeout/1000; |
| 187 | tv.tv_usec = (timeout%1000)*1000; |
| 188 | int sel = select(mSocketFD+1,&fds,NULL,NULL,&tv); |
| 189 | if (sel<0) { |
| 190 | perror("DatagramSocket::read() select() failed"); |
| 191 | throw SocketError(); |
| 192 | } |
| 193 | if (sel==0) return -1; |
kurtis.heimerl | a7fee63 | 2012-11-23 08:36:18 +0000 | [diff] [blame] | 194 | if (FD_ISSET(mSocketFD,&fds)) return read(buffer); |
| 195 | return -1; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | UDPSocket::UDPSocket(unsigned short wSrcPort) |
| 204 | :DatagramSocket() |
| 205 | { |
| 206 | open(wSrcPort); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | UDPSocket::UDPSocket(unsigned short wSrcPort, |
| 211 | const char * wDestIP, unsigned short wDestPort ) |
| 212 | :DatagramSocket() |
| 213 | { |
| 214 | open(wSrcPort); |
| 215 | destination(wDestPort, wDestIP); |
| 216 | } |
| 217 | |
| 218 | |
| 219 | |
| 220 | void UDPSocket::destination( unsigned short wDestPort, const char * wDestIP ) |
| 221 | { |
| 222 | resolveAddress((sockaddr_in*)mDestination, wDestIP, wDestPort ); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | void UDPSocket::open(unsigned short localPort) |
| 227 | { |
| 228 | // create |
| 229 | mSocketFD = socket(AF_INET,SOCK_DGRAM,0); |
| 230 | if (mSocketFD<0) { |
| 231 | perror("socket() failed"); |
| 232 | throw SocketError(); |
| 233 | } |
| 234 | |
| 235 | // bind |
| 236 | struct sockaddr_in address; |
| 237 | size_t length = sizeof(address); |
| 238 | bzero(&address,length); |
| 239 | address.sin_family = AF_INET; |
| 240 | address.sin_addr.s_addr = INADDR_ANY; |
| 241 | address.sin_port = htons(localPort); |
| 242 | if (bind(mSocketFD,(struct sockaddr*)&address,length)<0) { |
| 243 | perror("bind() failed"); |
| 244 | throw SocketError(); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | |
| 249 | |
| 250 | unsigned short UDPSocket::port() const |
| 251 | { |
| 252 | struct sockaddr_in name; |
| 253 | socklen_t nameSize = sizeof(name); |
| 254 | int retVal = getsockname(mSocketFD, (struct sockaddr*)&name, &nameSize); |
| 255 | if (retVal==-1) throw SocketError(); |
| 256 | return ntohs(name.sin_port); |
| 257 | } |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | UDDSocket::UDDSocket(const char* localPath, const char* remotePath) |
| 264 | :DatagramSocket() |
| 265 | { |
| 266 | if (localPath!=NULL) open(localPath); |
| 267 | if (remotePath!=NULL) destination(remotePath); |
| 268 | } |
| 269 | |
| 270 | |
| 271 | |
| 272 | void UDDSocket::open(const char* localPath) |
| 273 | { |
| 274 | // create |
| 275 | mSocketFD = socket(AF_UNIX,SOCK_DGRAM,0); |
| 276 | if (mSocketFD<0) { |
| 277 | perror("socket() failed"); |
| 278 | throw SocketError(); |
| 279 | } |
| 280 | |
| 281 | // bind |
| 282 | struct sockaddr_un address; |
| 283 | size_t length = sizeof(address); |
| 284 | bzero(&address,length); |
| 285 | address.sun_family = AF_UNIX; |
| 286 | strcpy(address.sun_path,localPath); |
| 287 | unlink(localPath); |
| 288 | if (bind(mSocketFD,(struct sockaddr*)&address,length)<0) { |
| 289 | perror("bind() failed"); |
| 290 | throw SocketError(); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | |
| 295 | |
| 296 | void UDDSocket::destination(const char* remotePath) |
| 297 | { |
| 298 | struct sockaddr_un* unAddr = (struct sockaddr_un*)mDestination; |
| 299 | strcpy(unAddr->sun_path,remotePath); |
| 300 | } |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | // vim:ts=4:sw=4 |