source: roaraudio/libroar/socket.c @ 520:97b25e01e6a7

Last change on this file since 520:97b25e01e6a7 was 520:97b25e01e6a7, checked in by phi, 16 years ago

this code changes seems not to work for me but are 100% correct acccording to the manpages I have here, as far as I understand, strange.

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