source: roaraudio/libroar/socket.c @ 528:d42830e32ea1

Last change on this file since 528:d42830e32ea1 was 528:d42830e32ea1, checked in by phi, 16 years ago

adding dummy IPX implementation: are there no docs? Can't even find correct socket() values!

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