source: roaraudio/libroar/socket.c @ 530:8578b85b0077

Last change on this file since 530:8578b85b0077 was 530:8578b85b0077, checked in by phi, 16 years ago

added pure IPX basic support

File size: 12.2 KB
Line 
1//socket.c:
2
3#include "libroar.h"
4
5#define MODE_LISTEN  ROAR_SOCKET_MODE_LISTEN
6#define MODE_CONNECT ROAR_SOCKET_MODE_CONNECT
7
8int roar_socket_new_tcp (void) {
9 int fh;
10 int opt = IPTOS_LOWDELAY;
11
12 fh = socket(PF_INET, SOCK_STREAM, 0);
13
14 setsockopt(fh, IPPROTO_IP, IP_TOS, &opt, sizeof(int));
15
16 return fh;
17}
18
19int roar_socket_new_udp (void) {
20 int fh;
21 int opt = IPTOS_LOWDELAY;
22
23 fh = socket(PF_INET, SOCK_DGRAM, 0);
24
25 setsockopt(fh, IPPROTO_IP, IP_TOS, &opt, sizeof(int));
26
27 return fh;
28}
29
30int roar_socket_new_tcp6 (void) {
31#ifdef PF_INET6
32 int fh;
33 int opt = IPTOS_LOWDELAY;
34
35 fh = socket(PF_INET6, SOCK_STREAM, 0);
36
37 setsockopt(fh, IPPROTO_IP, IP_TOS, &opt, sizeof(int));
38
39 return fh;
40#else
41 return -1;
42#endif
43}
44
45int roar_socket_new_udp6 (void) {
46#ifdef PF_INET6
47 int fh;
48 int opt = IPTOS_LOWDELAY;
49
50 fh = socket(PF_INET6, SOCK_DGRAM, 0);
51
52 setsockopt(fh, IPPROTO_IP, IP_TOS, &opt, sizeof(int));
53
54 return fh;
55#else
56 return -1;
57#endif
58}
59
60int roar_socket_new_unix (void) {
61 int fh;
62#ifdef SO_PEERCRED
63 int opt = 1;
64#endif
65
66 fh = socket(AF_UNIX, SOCK_STREAM, 0);
67
68#ifdef SO_PEERCRED
69 setsockopt(fh, SOL_SOCKET, SO_PASSCRED, &opt, sizeof(int));
70#endif
71
72 return fh;
73}
74
75int roar_socket_new_decnet_seqpacket (void) {
76#ifdef ROAR_HAVE_LIBDNET
77 int fh;
78
79 fh = socket(AF_DECnet, SOCK_SEQPACKET, DNPROTO_NSP);
80
81 return fh;
82#else
83 return -1;
84#endif
85}
86
87
88int roar_socket_new_decnet_stream (void) {
89#ifdef ROAR_HAVE_LIBDNET
90 int fh;
91
92 fh = socket(AF_DECnet, SOCK_STREAM, DNPROTO_NSP);
93
94 return fh;
95#else
96 return -1;
97#endif
98}
99
100int roar_socket_new_ipxspx (void) {
101 return -1;
102}
103
104int roar_socket_new_ipx    (void) {
105#ifdef ROAR_HAVE_IPX
106 return socket(AF_IPX, SOCK_DGRAM, AF_IPX);
107#else
108 return -1;
109#endif
110}
111
112
113int roar_socket_nonblock(int fh, int state) {
114 int flags;
115
116 if ( (flags = fcntl(fh, F_GETFL, 0)) == -1 ) {
117  ROAR_ERR("roar_socket_nonblock(fh=%i, state=%i): Can not read flags: %s", fh, state, strerror(errno));
118  ROAR_DBG("roar_socket_nonblock(fh=%i, state=%i) = -1", fh, state);
119  return -1;
120 }
121
122 flags |= O_NONBLOCK;
123
124 if ( state == ROAR_SOCKET_BLOCK )
125  flags -= O_NONBLOCK;
126
127 if ( fcntl(fh, F_SETFL, flags) == -1 ) {
128  ROAR_ERR("roar_socket_nonblock(fh=%i, state=%i): Can not set flags: %s", fh, state, strerror(errno));
129  ROAR_DBG("roar_socket_nonblock(fh=%i, state=%i) = -1", fh, state);
130  return -1;
131 }
132
133 ROAR_DBG("roar_socket_nonblock(fh=%i, state=%i) = 0", fh, state);
134 return 0;
135}
136
137int roar_socket_dup_udp_local_end (int fh) {
138 int                  n              = -1;
139 int                  flags          = -1;
140 struct sockaddr_in   socket_addr;
141 socklen_t            len            = sizeof(struct sockaddr_in);
142
143 if ( (flags = fcntl(fh, F_GETFL, 0)) == -1 ) {
144  ROAR_WARN("roar_socket_dup_udp_local_end(fh=%i): Can not read flags: %s", fh, strerror(errno));
145 }
146
147 if ( getsockname(fh, (struct sockaddr *)&socket_addr, &len) == -1 ) {
148  return -1;
149 }
150
151 if ( socket_addr.sin_family != AF_INET ) {
152  return -1;
153 }
154
155 n = roar_socket_new_udp();
156
157 if ( n == -1 )
158  return -1;
159
160//  if ( mode_func(fh, (struct sockaddr *)&socket_addr, sizeof(struct sockaddr_in)) == -1 ) {
161 if ( bind(n, (struct sockaddr *)&socket_addr, len) == -1 ) {
162  close(n);
163  return -1;
164 }
165
166 if ( flags != -1 ) {
167  if ( fcntl(fh, F_SETFL, flags) == -1 ) {
168   ROAR_WARN("roar_socket_dup_udp_local_end(fh=%i): Can not set flags: %s", fh, strerror(errno));
169   return -1;
170  }
171 }
172
173
174 return n;
175}
176
177int roar_socket_listen  (int type, char * host, int port) {
178 return roar_socket_open(MODE_LISTEN, type, host, port);
179}
180
181int roar_socket_connect (char * host, int port) {
182 char * proxy_type = getenv("ROAR_PROXY");
183
184 if ( proxy_type == NULL || strcmp(proxy_type, "") == 0 ) {
185  return roar_socket_open(MODE_CONNECT, ROAR_SOCKET_TYPE_UNKNOWN, host, port);
186 } else {
187  return roar_socket_open_proxy(MODE_CONNECT, ROAR_SOCKET_TYPE_UNKNOWN, host, port, proxy_type);
188 }
189}
190
191
192int roar_socket_listen_decnet (char * object, int num) {
193#ifdef ROAR_HAVE_LIBDNET
194 int fh = roar_socket_new_decnet_stream();
195 struct sockaddr_dn bind_sockaddr;
196
197 if ( fh == -1 )
198  return -1;
199
200 if ( !*object )
201  object = NULL;
202
203 if ( (object && num) || (!*object && !num) ) {
204  ROAR_WARN("roar_socket_listen_decnet(object='%s', num=%i): illegal address!", object, num);
205  close(fh);
206  return -1;
207 }
208
209 memset((void*)&bind_sockaddr, 0, sizeof(struct sockaddr_dn));
210
211 bind_sockaddr.sdn_family    = AF_DECnet;
212 bind_sockaddr.sdn_flags     = 0;
213 bind_sockaddr.sdn_objnum    = num;
214
215 if ( num ) {
216  bind_sockaddr.sdn_objnamel = 0;
217 } else {
218  bind_sockaddr.sdn_objnamel  = ROAR_dn_htons(strlen(object));
219  strcpy((char*)bind_sockaddr.sdn_objname, object); // FIXME: shouldn't we use strncpy()?
220 }
221
222 if ( bind(fh, (struct sockaddr *) &bind_sockaddr, sizeof(bind_sockaddr)) == -1 ) {
223  close(fh);
224  return -1;
225 }
226
227 if ( listen(fh, 8) == -1 ) {
228  close(fh);
229  return -1;
230 }
231
232 return fh;
233#else
234 return -1;
235#endif
236}
237
238char * roar_socket_get_local_nodename(void) {
239#ifdef ROAR_HAVE_LIBDNET
240 static char node[16] = {0};
241 struct dn_naddr      *binaddr;
242 struct nodeent       *dp;
243
244 if ( !node[0] ) {
245  if ( (binaddr=getnodeadd()) == NULL)
246   return NULL;
247
248  if ( (dp=getnodebyaddr((char*)binaddr->a_addr, binaddr->a_len, PF_DECnet)) == NULL )
249   return NULL;
250
251  strncpy(node, dp->n_name, 15);
252  node[15] = 0;
253 }
254
255 return node;
256#else
257 return NULL;
258#endif
259}
260
261int roar_socket_open (int mode, int type, char * host, int port) {
262// int type = ROAR_SOCKET_TYPE_INET;
263 int fh;
264 union {
265  struct sockaddr_in  in;
266  struct sockaddr_un  un;
267  struct sockaddr_in6 in6;
268 } socket_addr;
269 struct hostent     * he;
270 //unsigned int host_div = 0;
271 int (*mode_func)(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen) = connect; // default is to connect
272#ifdef ROAR_HAVE_LIBDNET
273 char obj[80];
274 char * del;
275#endif
276
277 if ( mode == MODE_LISTEN )
278  mode_func = bind;
279
280 if ( type == ROAR_SOCKET_TYPE_UNKNOWN ) {
281  type = ROAR_SOCKET_TYPE_INET;
282  if ( *host == '/' ) {
283   type = ROAR_SOCKET_TYPE_UNIX;
284  } else if ( strcmp(host, "+fork") == 0 ) {
285   type = ROAR_SOCKET_TYPE_FORK;
286  } else if ( strstr(host, "::") != NULL ) {
287   type = ROAR_SOCKET_TYPE_DECNET;
288  }
289 }
290
291
292 ROAR_DBG("roar_socket_open(*): type=%s, host='%s', port=%i",
293             type == ROAR_SOCKET_TYPE_UNIX ? "UNIX" : "INET", host, port);
294
295 if ( type == ROAR_SOCKET_TYPE_DECNET ) {
296#ifdef ROAR_HAVE_LIBDNET
297   ROAR_DBG("roar_socket_open(*): hostname for DECnet: host(%p)=%s", host, host);
298   del = strstr(host, "::");
299   ROAR_DBG("roar_socket_open(*): hostname for DECnet: del(%p)=%s", del, del);
300
301   if ( del == NULL ) {
302    ROAR_WARN("roar_socket_open(*): invalid hostname for DECnet: %s", host);
303    return -1;
304   }
305
306   *del = 0;
307
308   if ( *(del+2) == '#' ) { // assume we have node::#num
309    port = atoi(del+2);
310   }
311
312   if ( port ) {
313    sprintf(obj, "%i", port); // no need for snprintf() as dec(port) is smaller than obj[]
314   } else {
315    *obj = 0;
316    strncat(obj, del+2, 79);
317   }
318
319  if ( mode == MODE_LISTEN ) {
320   fh = roar_socket_listen_decnet(obj, port);
321   *del = ':';
322   return fh;
323//   return -1; // listen sockets on DECnet are not supportet at the moment
324  } else {
325   // There is nothing wrong in this case to use dnet_conn() so we do.
326   fh = dnet_conn(host, obj, SOCK_STREAM, 0 ,0 ,0 , 0);
327   *del = ':';
328   return fh;
329#else
330   return -1; // no decnet support
331#endif
332  }
333 }
334
335 memset(&socket_addr,    0, sizeof(socket_addr));
336 memset(&he,             0, sizeof(he));               // FIXME: we have a valid pointer in here????
337
338
339 if ( type == ROAR_SOCKET_TYPE_INET || type == ROAR_SOCKET_TYPE_INET6 ) {
340
341  if ( (he = gethostbyname(host)) == NULL ) {
342   ROAR_ERR("roar_socket_open(*): Can\'t resolve host name '%s'",
343                     host);
344   return -1;
345  }
346
347  if ( he->h_addrtype == AF_INET ) {
348   if ( type != ROAR_SOCKET_TYPE_INET )
349    return -1;
350
351   memcpy((struct in_addr *)&socket_addr.in.sin_addr, he->h_addr, sizeof(struct in_addr));
352
353   /* set the connect information */
354   socket_addr.in.sin_family = AF_INET;
355   socket_addr.in.sin_port   = ROAR_HOST2NET16(port);
356
357   fh = roar_socket_new_tcp();
358
359   if ( mode_func(fh, (struct sockaddr *)&socket_addr.in, sizeof(struct sockaddr_in)) == -1 ) {
360    ROAR_DBG("roar_socket_open(*): Can not connect/bind: %s", strerror(errno));
361    close(fh);
362    return -1;
363   }
364  } if ( he->h_addrtype == AF_INET6 ) {
365   if ( type != ROAR_SOCKET_TYPE_INET6 )
366    return -1;
367
368   memcpy((struct in6_addr *)&socket_addr.in6.sin6_addr, he->h_addr, sizeof(struct in6_addr));
369
370   /* set the connect information */
371   socket_addr.in6.sin6_family = AF_INET6;
372   socket_addr.in6.sin6_port   = ROAR_HOST2NET16(port);
373
374   fh = roar_socket_new_tcp6();
375
376   if ( mode_func(fh, (struct sockaddr *)&socket_addr.in6, sizeof(struct sockaddr_in6)) == -1 ) {
377    ROAR_DBG("roar_socket_open(*): Can not connect/bind: %s", strerror(errno));
378    close(fh);
379    return -1;
380   }
381  } else {
382   return -1;
383  }
384  // hey! we have a socket...
385 } else if ( type == ROAR_SOCKET_TYPE_UNIX ) {
386  socket_addr.un.sun_family = AF_UNIX;
387  strncpy(socket_addr.un.sun_path, host, sizeof(socket_addr.un.sun_path) - 1);
388
389  fh = roar_socket_new_unix();
390
391  if ( mode_func(fh, (struct sockaddr *)&socket_addr.un, sizeof(struct sockaddr_un)) == -1 ) {
392   ROAR_DBG("roar_socket_open(*): Can not connect/bind: %s", strerror(errno));
393   close(fh);
394   return -1;
395  }
396 } else if ( type == ROAR_SOCKET_TYPE_FORK ) {
397  return roar_socket_open_fork(mode, host, port);
398 } else if ( type == ROAR_SOCKET_TYPE_FILE ) {
399  return roar_socket_open_file(mode, host, port);
400 } else {
401  return -1;
402 }
403
404 if ( mode == MODE_LISTEN )
405  if ( listen(fh, ROAR_SOCKET_QUEUE_LEN) == -1 ) {
406   close(fh);
407   return -1;
408  }
409
410 return fh;
411}
412
413int roar_socket_open_fork  (int mode, char * host, int port) {
414 int socks[2];
415 int r;
416 char fhstr[8];
417
418 if ( mode == MODE_LISTEN )
419  return -1;
420
421 if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) {
422  return -1;
423 }
424
425 r = fork();
426
427 if ( r == -1 ) { // error!
428  ROAR_ERR("roar_socket_open_fork(*): Can not fork: %s", strerror(errno));
429  close(socks[0]);
430  close(socks[1]);
431  return -1;
432 } else if ( r == 0 ) { // we are the child
433  close(socks[0]);
434
435  close(ROAR_STDIN ); // we do not want roard to have any standard input
436  close(ROAR_STDOUT); // STDOUT is also not needed, so we close it,
437                      // but STDERR we keep open for error messages.
438
439  snprintf(fhstr, 7, "%i", socks[1]);
440
441  execlp("roard", "roard", "--terminate", "--no-listen", "--client-fh", fhstr, NULL);
442
443  // we are still alive?
444  ROAR_ERR("roar_socket_open_fork(*): alive after exec(), that's bad!");
445  _exit(1);
446 } else { // we are the parent
447  close(socks[1]);
448  return socks[0];
449 }
450
451 return -1;
452}
453
454int roar_socket_open_file  (int mode, char * host, int port) {
455 int fh;
456
457 if ( mode == MODE_LISTEN )
458  return -1;
459
460 if ( (fh = open(host, O_RDONLY, 0644)) == -1 ) {
461  ROAR_ERR("roar_socket_open_file(*): Can not open file %s: %s", host, strerror(errno));
462 }
463
464 return fh;
465}
466
467// --- [ PROXY CODE ] ---
468
469// generic proxy code:
470
471int roar_socket_open_proxy (int mode, int type, char * host, int port, char * proxy_type) {
472 int    proxy_port;
473 char   proxy_host[ROAR_SOCKET_MAX_HOSTNAMELEN];
474 char * proxy_addr;
475 int    i;
476 int    fh;
477
478 // TODO: change this so we support listen() proxys (ssh -R)
479 if ( mode != MODE_CONNECT )
480  return -1;
481
482 if ( !strcmp(proxy_type, "socks4a") ) { // for TOR, the only supported type at the moment
483  proxy_addr = getenv("socks_proxy");
484
485  proxy_port = 9050; // TOR's default port
486
487  if ( proxy_addr == NULL )
488   return -1;
489
490  for (i = 0; proxy_addr[i] != 0 && proxy_addr[i] != ':' && i < ROAR_SOCKET_MAX_HOSTNAMELEN; i++)
491   proxy_host[i] = proxy_addr[i];
492  proxy_host[i] = 0;
493
494  if ( i == 0 ) // no hostname found
495   return -1;
496
497  if ( proxy_addr[i] == ':' )
498   proxy_port = atoi(&proxy_addr[i+1]);
499
500  if ( (fh = roar_socket_open(mode, type, proxy_host, proxy_port)) == -1) {
501   return -1;
502  }
503
504  if ( roar_socket_open_socks4a(mode, fh, host, port) == -1 ) {
505   close(fh);
506   return -1;
507  }
508
509  return fh;
510 } else {
511  return -1; // unknown type
512 }
513}
514
515// protocoll dependet proxy code:
516
517int roar_socket_open_socks4a(int mode, int fh, char * host, int port) {
518 char buf[9];
519 int  len;
520
521 buf[0] = 0x04;
522 buf[1] = mode == MODE_CONNECT ? 0x01 : 0x02;
523 *((uint16_t*)&buf[2]) = htons(port);
524 buf[4] = 0x00;
525 buf[5] = 0x00;
526 buf[6] = 0x00;
527 buf[7] = 0x01;
528 buf[8] = 0x00;
529
530 if ( write(fh, buf, 9) != 9 )
531  return -1;
532
533 len = strlen(host);
534
535 if ( write(fh, host, len) != len )
536  return -1;
537
538 if ( write(fh, "\0", 1) != 1 )
539  return -1;
540
541 if ( read(fh, buf, 8) != 8 )
542  return -1;
543
544 if ( buf[1] != 0x5a )
545  return -1;
546
547 return 0;
548}
549
550//ll
Note: See TracBrowser for help on using the repository browser.