| 1 |
/*************************************************************************** |
| 2 |
begin : Wed Jun 16 2010 |
| 3 |
copyright : (C) 2010 by Martin Preuss |
| 4 |
email : [email protected] |
| 5 |
|
| 6 |
*************************************************************************** |
| 7 |
* * |
| 8 |
* This library is free software; you can redistribute it and/or * |
| 9 |
* modify it under the terms of the GNU Lesser General Public * |
| 10 |
* License as published by the Free Software Foundation; either * |
| 11 |
* version 2.1 of the License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* This library is distributed in the hope that it will be useful, * |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 16 |
* Lesser General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Lesser General Public * |
| 19 |
* License along with this library; if not, write to the Free Software * |
| 20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
| 21 |
* MA 02111-1307 USA * |
| 22 |
* * |
| 23 |
***************************************************************************/ |
| 24 |
|
| 25 |
|
| 26 |
#include "Platform.h" |
| 27 |
|
| 28 |
|
| 29 |
#include "network.h" |
| 30 |
#include "message.h" |
| 31 |
|
| 32 |
#include <string.h> |
| 33 |
#include <errno.h> |
| 34 |
|
| 35 |
#include <sys/types.h> |
| 36 |
#include <sys/socket.h> |
| 37 |
#include <netinet/in.h> |
| 38 |
#include <arpa/inet.h> |
| 39 |
#include <sys/un.h> |
| 40 |
|
| 41 |
#include <sys/stat.h> |
| 42 |
#include <fcntl.h> |
| 43 |
#include <unistd.h> |
| 44 |
#include <stdio.h> |
| 45 |
#include <assert.h> |
| 46 |
|
| 47 |
#include <netdb.h> |
| 48 |
|
| 49 |
|
| 50 |
/* enable or disable error messages */ |
| 51 |
#define ENABLE_DEBUGPE |
| 52 |
|
| 53 |
/* enable or disable info messages */ |
| 54 |
/*#define ENABLE_DEBUGPI*/ |
| 55 |
|
| 56 |
/* enable or disable debug messages */ |
| 57 |
/*#define ENABLE_DEBUGPD*/ |
| 58 |
|
| 59 |
|
| 60 |
#ifdef ENABLE_DEBUGPE |
| 61 |
# define DEBUGPE(format, args...) \ |
| 62 |
fprintf(stderr, __FILE__":%5d:" format, __LINE__, ## args); |
| 63 |
#else |
| 64 |
# define DEBUGPE(format, args...) |
| 65 |
#endif |
| 66 |
|
| 67 |
|
| 68 |
#ifdef ENABLE_DEBUGPI |
| 69 |
# define DEBUGPI(format, args...) \ |
| 70 |
fprintf(stderr, __FILE__":%5d:" format, __LINE__, ## args); |
| 71 |
#else |
| 72 |
# define DEBUGPI(format, args...) |
| 73 |
#endif |
| 74 |
|
| 75 |
|
| 76 |
#ifdef ENABLE_DEBUGPD |
| 77 |
# define DEBUGPD(format, args...) \ |
| 78 |
fprintf(stderr, __FILE__":%5d:" format, __LINE__, ## args); |
| 79 |
#else |
| 80 |
# define DEBUGPD(format, args...) |
| 81 |
#endif |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
int rsct_net_listen_by_ip(const char *ip, int port) { |
| 88 |
union { |
| 89 |
struct sockaddr raw; |
| 90 |
struct sockaddr_in in; |
| 91 |
} addr; |
| 92 |
int s; |
| 93 |
int fl; |
| 94 |
|
| 95 |
memset(&addr, 0, sizeof(addr)); |
| 96 |
#if defined(PF_INET) |
| 97 |
addr.raw.sa_family=PF_INET; |
| 98 |
#elif defined (AF_INET) |
| 99 |
addr.raw.sa_family=AF_INET; |
| 100 |
#endif |
| 101 |
if (!inet_aton(ip, &addr.in.sin_addr)) { |
| 102 |
DEBUGPE("ERROR: inet_aton(): %d=%s\n", errno, strerror(errno)); |
| 103 |
return -1; |
| 104 |
} |
| 105 |
addr.in.sin_port=htons(port); |
| 106 |
|
| 107 |
#if defined(PF_INET) |
| 108 |
s=socket(PF_INET, SOCK_STREAM,0); |
| 109 |
#elif defined (AF_INET) |
| 110 |
s=socket(AF_INET, SOCK_STREAM,0); |
| 111 |
#endif |
| 112 |
if (s==-1) { |
| 113 |
DEBUGPE("ERROR: socket(): %d=%s\n", errno, strerror(errno)); |
| 114 |
return -1; |
| 115 |
} |
| 116 |
|
| 117 |
fl=1; |
| 118 |
if (setsockopt(s, |
| 119 |
SOL_SOCKET, |
| 120 |
SO_REUSEADDR, |
| 121 |
&fl, |
| 122 |
sizeof(fl))) { |
| 123 |
DEBUGPE("ERROR: setsockopt(): %s", strerror(errno)); |
| 124 |
return -1; |
| 125 |
} |
| 126 |
|
| 127 |
if (bind(s, &addr.raw, sizeof(struct sockaddr_in))) { |
| 128 |
DEBUGPE("ERROR: bind(): %d=%s\n", errno, strerror(errno)); |
| 129 |
close(s); |
| 130 |
return -1; |
| 131 |
} |
| 132 |
|
| 133 |
if (listen(s, 10)) { |
| 134 |
DEBUGPE("ERROR: listen(): %d=%s\n", errno, strerror(errno)); |
| 135 |
close(s); |
| 136 |
return -1; |
| 137 |
} |
| 138 |
|
| 139 |
return s; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
int rsct_net_listen_by_path(const char *name) { |
| 145 |
union { |
| 146 |
struct sockaddr addr; |
| 147 |
struct sockaddr_un un; |
| 148 |
char bin[512]; |
| 149 |
} a; |
| 150 |
int s; |
| 151 |
|
| 152 |
memset(&a.bin, 0, sizeof(a.bin)); |
| 153 |
#if defined(PF_UNIX) |
| 154 |
a.addr.sa_family=PF_UNIX; |
| 155 |
#elif defined (AF_UNIX) |
| 156 |
a.addr.sa_family=AF_UNIX; |
| 157 |
#endif |
| 158 |
|
| 159 |
#ifdef PF_UNIX |
| 160 |
a.un.sun_family=PF_UNIX; |
| 161 |
#elif defined (AF_UNIX) |
| 162 |
a.un.sun_family=AF_UNIX; |
| 163 |
#else |
| 164 |
fprintf(stderr, "No unix domain sockets available for this system\n"); |
| 165 |
return -1; |
| 166 |
#endif |
| 167 |
a.un.sun_path[0]=0; |
| 168 |
|
| 169 |
if (name) { |
| 170 |
/* ok, address to be set */ |
| 171 |
if ((strlen(name)+1)>sizeof(a.un.sun_path)) { |
| 172 |
/* bad address */ |
| 173 |
fprintf(stderr, "Path too long (%d>%d)\n", |
| 174 |
(int)(strlen(name)+1),(int)(sizeof(a.un.sun_path))); |
| 175 |
return -1; |
| 176 |
} |
| 177 |
strcpy(a.un.sun_path, name); |
| 178 |
} |
| 179 |
|
| 180 |
/* create socket */ |
| 181 |
#if defined(PF_UNIX) |
| 182 |
s=socket(PF_UNIX, SOCK_STREAM, 0); |
| 183 |
#elif defined (AF_UNIX) |
| 184 |
s=socket(AF_UNIX, SOCK_STREAM, 0); |
| 185 |
#endif |
| 186 |
if (s==-1) { |
| 187 |
fprintf(stderr, "ERROR: socket(): %d=%s\n", errno, strerror(errno)); |
| 188 |
return -1; |
| 189 |
} |
| 190 |
|
| 191 |
if (bind(s, &a.addr, sizeof(struct sockaddr_un))) { |
| 192 |
fprintf(stderr, "ERROR: bind(): %d=%s\n", errno, strerror(errno)); |
| 193 |
close(s); |
| 194 |
return -1; |
| 195 |
} |
| 196 |
|
| 197 |
if (listen(s, 10)) { |
| 198 |
fprintf(stderr, "ERROR: listen(): %d=%s\n", errno, strerror(errno)); |
| 199 |
close(s); |
| 200 |
return -1; |
| 201 |
} |
| 202 |
|
| 203 |
return s; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
int rsct_net_accept(int sk) { |
| 209 |
socklen_t addrLen; |
| 210 |
int newS; |
| 211 |
struct sockaddr peerAddr; |
| 212 |
|
| 213 |
addrLen=sizeof(peerAddr); |
| 214 |
newS=accept(sk, &peerAddr, &addrLen); |
| 215 |
if (newS!=-1) |
| 216 |
return newS; |
| 217 |
else { |
| 218 |
if (errno!=EINTR) { |
| 219 |
DEBUGPE("ERROR: accept(): %d=%s\n", errno, strerror(errno)); |
| 220 |
} |
| 221 |
return -1; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
int rsct_net_connect_by_ip(const char *ip, int port) { |
| 228 |
union { |
| 229 |
struct sockaddr raw; |
| 230 |
struct sockaddr_in in; |
| 231 |
} addr; |
| 232 |
int s; |
| 233 |
|
| 234 |
memset(&addr, 0, sizeof(addr)); |
| 235 |
#if defined(PF_INET) |
| 236 |
addr.raw.sa_family=PF_INET; |
| 237 |
#elif defined (AF_INET) |
| 238 |
addr.raw.sa_family=AF_INET; |
| 239 |
#endif |
| 240 |
if (!inet_aton(ip, &addr.in.sin_addr)) { |
| 241 |
struct hostent *he; |
| 242 |
|
| 243 |
he=gethostbyname(ip); |
| 244 |
if (!he) { |
| 245 |
DEBUGPE("ERROR: gethostbyname(%s): %d=%s\n", ip, errno, strerror(errno)); |
| 246 |
return -1; |
| 247 |
} |
| 248 |
memcpy(&(addr.in.sin_addr), |
| 249 |
he->h_addr_list[0], |
| 250 |
sizeof(struct in_addr)); |
| 251 |
} |
| 252 |
addr.in.sin_port=htons(port); |
| 253 |
|
| 254 |
#if defined(PF_INET) |
| 255 |
s=socket(PF_INET, SOCK_STREAM,0); |
| 256 |
#elif defined (AF_INET) |
| 257 |
s=socket(AF_INET, SOCK_STREAM,0); |
| 258 |
#endif |
| 259 |
if (s==-1) { |
| 260 |
DEBUGPE("ERROR: socket(): %d=%s\n", errno, strerror(errno)); |
| 261 |
return -1; |
| 262 |
} |
| 263 |
|
| 264 |
if (connect(s, &addr.raw, sizeof(struct sockaddr_in))) { |
| 265 |
DEBUGPE("ERROR: connect(): %d=%s\n", errno, strerror(errno)); |
| 266 |
close(s); |
| 267 |
return -1; |
| 268 |
} |
| 269 |
|
| 270 |
return s; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
int rsct_net_connect_by_path(const char *name) { |
| 276 |
union { |
| 277 |
struct sockaddr addr; |
| 278 |
struct sockaddr_un un; |
| 279 |
char bin[512]; |
| 280 |
} a; |
| 281 |
int s; |
| 282 |
|
| 283 |
memset(&a.bin, 0, sizeof(a.bin)); |
| 284 |
#if defined(PF_UNIX) |
| 285 |
a.addr.sa_family=PF_UNIX; |
| 286 |
#elif defined (AF_UNIX) |
| 287 |
a.addr.sa_family=AF_UNIX; |
| 288 |
#endif |
| 289 |
|
| 290 |
#ifdef PF_UNIX |
| 291 |
a.un.sun_family=PF_UNIX; |
| 292 |
#elif defined (AF_UNIX) |
| 293 |
a.un.sun_family=AF_UNIX; |
| 294 |
#else |
| 295 |
fprintf(stderr, "No unix domain sockets available for this system\n"); |
| 296 |
return -1; |
| 297 |
#endif |
| 298 |
a.un.sun_path[0]=0; |
| 299 |
|
| 300 |
if (name) { |
| 301 |
/* ok, address to be set */ |
| 302 |
if ((strlen(name)+1)>sizeof(a.un.sun_path)) { |
| 303 |
/* bad address */ |
| 304 |
fprintf(stderr, "Path too long (%d>%d)\n", |
| 305 |
(int)(strlen(name)+1),(int)(sizeof(a.un.sun_path))); |
| 306 |
return -1; |
| 307 |
} |
| 308 |
strcpy(a.un.sun_path,name); |
| 309 |
} |
| 310 |
|
| 311 |
/* create socket */ |
| 312 |
#if defined(PF_UNIX) |
| 313 |
s=socket(PF_UNIX, SOCK_STREAM, 0); |
| 314 |
#elif defined (AF_UNIX) |
| 315 |
s=socket(AF_UNIX, SOCK_STREAM, 0); |
| 316 |
#endif |
| 317 |
if (s==-1) { |
| 318 |
fprintf(stderr, "ERROR: socket(): %d=%s\n", errno, strerror(errno)); |
| 319 |
return -1; |
| 320 |
} |
| 321 |
|
| 322 |
if (connect(s, &a.addr, sizeof(struct sockaddr_un))) { |
| 323 |
fprintf(stderr, "ERROR: connect(): %d=%s\n", errno, strerror(errno)); |
| 324 |
close(s); |
| 325 |
return -1; |
| 326 |
} |
| 327 |
|
| 328 |
return s; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
int rsct_net_recv(int sk, RSCT_MESSAGE *msg) { |
| 334 |
char *p; |
| 335 |
int bytesRead; |
| 336 |
|
| 337 |
assert(msg); |
| 338 |
|
| 339 |
bytesRead=0; |
| 340 |
p=(char*) msg; |
| 341 |
|
| 342 |
/* read header */ |
| 343 |
while(bytesRead<sizeof(RSCT_MESSAGE_HEADER)) { |
| 344 |
ssize_t i; |
| 345 |
|
| 346 |
i=sizeof(RSCT_MESSAGE_HEADER)-bytesRead; |
| 347 |
i=read(sk, p, i); |
| 348 |
if (i<0) { |
| 349 |
if (errno!=EINTR) { |
| 350 |
DEBUGPE("ERROR: read(): %d=%s\n", errno, strerror(errno)); |
| 351 |
return -1; |
| 352 |
} |
| 353 |
} |
| 354 |
else if (i==0) { |
| 355 |
if (bytesRead==0) { |
| 356 |
DEBUGPI("INFO: peer disconnected (header)\n"); |
| 357 |
return 0; |
| 358 |
} |
| 359 |
else { |
| 360 |
DEBUGPE("ERROR: eof met prematurely (header, %d bytes)\n", bytesRead); |
| 361 |
} |
| 362 |
return -1; |
| 363 |
} |
| 364 |
bytesRead+=i; |
| 365 |
p+=i; |
| 366 |
DEBUGPI("INFO: Received %d bytes (header)\n", bytesRead); |
| 367 |
} |
| 368 |
|
| 369 |
/* check length */ |
| 370 |
if (msg->header.len>=RSCT_MAX_MESSAGE_LEN) { |
| 371 |
DEBUGPE("ERROR: Request too long (%d bytes)\n", msg->header.len); |
| 372 |
return -1; |
| 373 |
} |
| 374 |
DEBUGPI("INFO: Message has %d bytes (type: %s)\n", |
| 375 |
msg->header.len, |
| 376 |
RSCT_MessageTypeToString(msg->header.type)); |
| 377 |
|
| 378 |
/* read payload */ |
| 379 |
while(bytesRead<msg->header.len) { |
| 380 |
ssize_t i; |
| 381 |
|
| 382 |
i=msg->header.len-bytesRead; |
| 383 |
i=read(sk, p, i); |
| 384 |
if (i<0) { |
| 385 |
DEBUGPE("ERROR: read(): %d=%s\n", errno, strerror(errno)); |
| 386 |
return -1; |
| 387 |
} |
| 388 |
else if (i==0) { |
| 389 |
DEBUGPE("ERROR: eof met prematurely (payload, %d bytes)\n", |
| 390 |
bytesRead); |
| 391 |
return -1; |
| 392 |
} |
| 393 |
bytesRead+=i; |
| 394 |
p+=i; |
| 395 |
DEBUGPI("INFO: Received %d bytes (payload)\n", bytesRead); |
| 396 |
} |
| 397 |
|
| 398 |
return bytesRead; |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
int rsct_net_send(int sk, const RSCT_MESSAGE *msg) { |
| 404 |
int bytesLeft; |
| 405 |
const uint8_t *p; |
| 406 |
|
| 407 |
/* send */ |
| 408 |
DEBUGPI("INFO: Sending %d bytes (type %s)\n", |
| 409 |
msg->header.len, |
| 410 |
RSCT_MessageTypeToString(msg->header.type)); |
| 411 |
|
| 412 |
bytesLeft=msg->header.len; |
| 413 |
p=(const uint8_t*) msg; |
| 414 |
|
| 415 |
while(bytesLeft) { |
| 416 |
ssize_t i; |
| 417 |
|
| 418 |
i=send(sk, (const void*)p, bytesLeft, 0); |
| 419 |
|
| 420 |
/* evaluate */ |
| 421 |
if (i<0) { |
| 422 |
if (errno!=EINTR) { |
| 423 |
DEBUGPE("ERROR: send(): %d=%s\n", errno, strerror(errno)); |
| 424 |
return -1; |
| 425 |
} |
| 426 |
} |
| 427 |
else if (i==0) { |
| 428 |
} |
| 429 |
else { |
| 430 |
bytesLeft-=(int) i; |
| 431 |
p+=i; |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
return 0; |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|