source: roaraudio/libroar/socket.c @ 1119:b273f96b915c

Last change on this file since 1119:b273f96b915c was 1119:b273f96b915c, checked in by phi, 15 years ago

don't change status if not needed, safes one system call

File size: 23.6 KB
Line 
1//socket.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 *  NOTE for everyone want's to change something and send patches:
24 *  read README and HACKING! There a addition information on
25 *  the license of this document you need to read before you send
26 *  any patches.
27 *
28 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
29 *  or libpulse*:
30 *  The libs libroaresd, libroararts and libroarpulse link this lib
31 *  and are therefore GPL. Because of this it may be illigal to use
32 *  them with any software that uses libesd, libartsc or libpulse*.
33 */
34
35#include "libroar.h"
36
37#define MODE_LISTEN  ROAR_SOCKET_MODE_LISTEN
38#define MODE_CONNECT ROAR_SOCKET_MODE_CONNECT
39
40int roar_socket_new_tcp (void) {
41 int fh;
42#ifndef ROAR_TARGET_WIN32
43 int opt = IPTOS_LOWDELAY;
44#endif
45
46 fh = socket(PF_INET, SOCK_STREAM, 0);
47
48#ifndef ROAR_TARGET_WIN32
49 setsockopt(fh, IPPROTO_IP, IP_TOS, &opt, sizeof(int));
50#endif
51
52 return fh;
53}
54
55int roar_socket_new_udp (void) {
56 int fh;
57#ifndef ROAR_TARGET_WIN32
58 int opt = IPTOS_LOWDELAY;
59#endif
60
61 fh = socket(PF_INET, SOCK_DGRAM, 0);
62
63#ifndef ROAR_TARGET_WIN32
64 setsockopt(fh, IPPROTO_IP, IP_TOS, &opt, sizeof(int));
65#endif
66
67 return fh;
68}
69
70int roar_socket_new_tcp6 (void) {
71#ifdef ROAR_HAVE_IPV6
72 int fh;
73 int opt = IPTOS_LOWDELAY;
74
75 fh = socket(PF_INET6, SOCK_STREAM, 0);
76
77 setsockopt(fh, IPPROTO_IP, IP_TOS, &opt, sizeof(int));
78
79 return fh;
80#else
81 return -1;
82#endif
83}
84
85int roar_socket_new_udp6 (void) {
86#ifdef ROAR_HAVE_IPV6
87 int fh;
88 int opt = IPTOS_LOWDELAY;
89
90 fh = socket(PF_INET6, SOCK_DGRAM, 0);
91
92 setsockopt(fh, IPPROTO_IP, IP_TOS, &opt, sizeof(int));
93
94 return fh;
95#else
96 return -1;
97#endif
98}
99
100int roar_socket_new_unix (void) {
101 int fh;
102/*
103#ifdef SO_PEERCRED
104 int opt = 1;
105#endif
106*/
107
108 fh = socket(AF_UNIX, SOCK_STREAM, 0);
109
110/*
111#ifdef SO_PEERCRED
112// setsockopt(fh, SOL_SOCKET, SO_PASSCRED, &opt, sizeof(int));
113#endif
114*/
115
116 return fh;
117}
118
119int roar_socket_decnet_set_timeout (int fh, time_t sec, int usec) {
120#ifdef ROAR_HAVE_LIBDNET
121 struct timeval timeout = {sec, usec};
122
123 return setsockopt(fh, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
124#else
125 return -1;
126#endif
127}
128
129int roar_socket_recvbuf(int fh, int len) {
130 if ( len < 256 ) len = 256;
131
132 return setsockopt(fh, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len));
133}
134
135int roar_socket_new_decnet_seqpacket (void) {
136#ifdef ROAR_HAVE_LIBDNET
137 int fh;
138
139 fh = socket(AF_DECnet, SOCK_SEQPACKET, DNPROTO_NSP);
140
141 roar_socket_decnet_set_timeout(fh, 300, 0);
142
143 return fh;
144#else
145 return -1;
146#endif
147}
148
149
150int roar_socket_new_decnet_stream (void) {
151#ifdef ROAR_HAVE_LIBDNET
152 int fh;
153
154 fh = socket(AF_DECnet, SOCK_STREAM, DNPROTO_NSP);
155
156 roar_socket_decnet_set_timeout(fh, 300, 0);
157
158 return fh;
159#else
160 return -1;
161#endif
162}
163
164int roar_socket_new_ipxspx (void) {
165 return -1;
166}
167
168int roar_socket_new_ipx    (void) {
169#ifdef ROAR_HAVE_IPX
170 return socket(AF_IPX, SOCK_DGRAM, AF_IPX);
171#else
172 return -1;
173#endif
174}
175
176
177int roar_socket_nonblock(int fh, int state) {
178#ifndef ROAR_TARGET_WIN32
179 int flags;
180
181 if ( (flags = fcntl(fh, F_GETFL, 0)) == -1 ) {
182  ROAR_ERR("roar_socket_nonblock(fh=%i, state=%i): Can not read flags: %s", fh, state, strerror(errno));
183  ROAR_DBG("roar_socket_nonblock(fh=%i, state=%i) = -1", fh, state);
184  return -1;
185 }
186
187 if ( !(flags & O_NONBLOCK) && state == ROAR_SOCKET_BLOCK )
188  return 0;
189
190 flags |= O_NONBLOCK;
191
192 if ( state == ROAR_SOCKET_BLOCK )
193  flags -= O_NONBLOCK;
194
195 if ( fcntl(fh, F_SETFL, flags) == -1 ) {
196  ROAR_ERR("roar_socket_nonblock(fh=%i, state=%i): Can not set flags: %s", fh, state, strerror(errno));
197  ROAR_DBG("roar_socket_nonblock(fh=%i, state=%i) = -1", fh, state);
198  return -1;
199 }
200
201 ROAR_DBG("roar_socket_nonblock(fh=%i, state=%i) = 0", fh, state);
202 return 0;
203#else
204 ROAR_WARN("roar_socket_nonblock(*): no nonblocking IO support on win32, use a real OS");
205 return -1;
206#endif
207}
208
209int roar_socket_dup_udp_local_end (int fh) {
210#ifndef ROAR_TARGET_WIN32
211 int                  n              = -1;
212 int                  flags          = -1;
213 struct sockaddr_in   socket_addr;
214 socklen_t            len            = sizeof(struct sockaddr_in);
215
216 if ( (flags = fcntl(fh, F_GETFL, 0)) == -1 ) {
217  ROAR_WARN("roar_socket_dup_udp_local_end(fh=%i): Can not read flags: %s", fh, strerror(errno));
218 }
219
220 if ( getsockname(fh, (struct sockaddr *)&socket_addr, &len) == -1 ) {
221  return -1;
222 }
223
224 if ( socket_addr.sin_family != AF_INET ) {
225  return -1;
226 }
227
228 n = roar_socket_new_udp();
229
230 if ( n == -1 )
231  return -1;
232
233//  if ( mode_func(fh, (struct sockaddr *)&socket_addr, sizeof(struct sockaddr_in)) == -1 ) {
234 if ( bind(n, (struct sockaddr *)&socket_addr, len) == -1 ) {
235  close(n);
236  return -1;
237 }
238
239 if ( flags != -1 ) {
240  if ( fcntl(fh, F_SETFL, flags) == -1 ) {
241   ROAR_WARN("roar_socket_dup_udp_local_end(fh=%i): Can not set flags: %s", fh, strerror(errno));
242   return -1;
243  }
244 }
245
246
247 return n;
248#else
249 ROAR_WARN("roar_socket_dup_udp_local_end(*): this function is not supported on win32, use a real OS");
250 return -1;
251#endif
252}
253
254
255#define _SCMR_CONTROLLEN (sizeof(struct cmsghdr) + sizeof(int))
256int roar_socket_send_fh (int sock, int fh, char * mes, size_t len) {
257#ifndef ROAR_TARGET_WIN32
258 struct iovec     iov[1];
259 struct msghdr    msg;
260 char             cmptr_buf[_SCMR_CONTROLLEN];
261 struct cmsghdr * cmptr = (struct cmsghdr *) cmptr_buf;
262 char             localmes[1] = {0};
263
264 ROAR_DBG("roar_socket_send_fh(sock=%i, fh=%i, mes=%p, len=%u) = ?", sock, fh, mes, len);
265
266 if ( sock < 0 || fh < 0 )
267  return -1;
268
269 if ( len == 0 ) {
270  len = 1;
271  mes = localmes;
272 }
273
274 memset(cmptr, 0, _SCMR_CONTROLLEN);
275
276 iov[0].iov_base = mes;
277 iov[0].iov_len  = len;
278 msg.msg_iov     = iov;
279 msg.msg_iovlen  = 1;
280 msg.msg_name    = NULL;
281 msg.msg_namelen = 0;
282
283 cmptr->cmsg_level        = SOL_SOCKET;
284 cmptr->cmsg_type         = SCM_RIGHTS;
285 cmptr->cmsg_len          = _SCMR_CONTROLLEN;
286 msg.msg_control          = (caddr_t) cmptr;
287 msg.msg_controllen       = _SCMR_CONTROLLEN;
288 *(int *)CMSG_DATA(cmptr) = fh;
289
290 return sendmsg(sock, &msg, 0);
291#else
292 ROAR_ERR("roar_socket_send_fh(*): There is no UNIX Domain Socket support in win32, download a real OS.");
293 return -1;
294#endif
295}
296
297int roar_socket_recv_fh (int sock,         char * mes, size_t * len) {
298#ifndef ROAR_TARGET_WIN32
299 struct iovec     iov[1];
300 struct msghdr    msg;
301 char             cmptr_buf[_SCMR_CONTROLLEN];
302 struct cmsghdr * cmptr = (struct cmsghdr *) cmptr_buf;
303 char             localmes[1];
304 size_t           locallen[1] = {1};
305
306 if ( sock < 0 )
307  return -1;
308
309 if ( len == NULL ) {
310  len = locallen;
311  mes = localmes;
312 }
313
314 iov[0].iov_base = mes;
315 iov[0].iov_len  = *len;
316 msg.msg_iov     = iov;
317 msg.msg_iovlen  = 1;
318 msg.msg_name    = NULL;
319 msg.msg_namelen = 0;
320
321 msg.msg_control    = (caddr_t) cmptr;
322 msg.msg_controllen = _SCMR_CONTROLLEN;
323
324 if ( (*len = recvmsg(sock, &msg, 0)) == -1 )
325  return -1;
326
327 if ( msg.msg_controllen != _SCMR_CONTROLLEN )
328  return -1;
329
330 return *(int *)CMSG_DATA(cmptr);
331#else
332 ROAR_ERR("roar_socket_recv_fh(*): There is no UNIX Domain Socket support in win32, download a real OS.");
333 return -1;
334#endif
335}
336
337int roar_socket_listen  (int type, char * host, int port) {
338 return roar_socket_open(MODE_LISTEN, type, host, port);
339}
340
341int roar_socket_connect (char * host, int port) {
342 char * proxy_type = getenv("ROAR_PROXY");
343
344 if ( proxy_type == NULL || strcmp(proxy_type, "") == 0 ) {
345  return roar_socket_open(MODE_CONNECT, ROAR_SOCKET_TYPE_UNKNOWN, host, port);
346 } else {
347#ifdef ROAR_SUPPORT_PROXY
348  return roar_socket_open_proxy(MODE_CONNECT, ROAR_SOCKET_TYPE_UNKNOWN, host, port, proxy_type);
349#else
350  ROAR_ERR("roar_socket_connect(host='%s', port=%i): no support for proxy code (proxy_type=%s)", host, port, proxy_type);
351  return -1;
352#endif
353 }
354}
355
356
357int roar_socket_listen_decnet (char * object, int num) {
358#ifdef ROAR_HAVE_LIBDNET
359 int fh = roar_socket_new_decnet_stream();
360 struct sockaddr_dn bind_sockaddr;
361
362 if ( fh == -1 )
363  return -1;
364
365 if ( !*object )
366  object = NULL;
367
368 if ( (object && num) || (!*object && !num) ) {
369  ROAR_WARN("roar_socket_listen_decnet(object='%s', num=%i): illegal address!", object, num);
370  close(fh);
371  return -1;
372 }
373
374 memset((void*)&bind_sockaddr, 0, sizeof(struct sockaddr_dn));
375
376 bind_sockaddr.sdn_family    = AF_DECnet;
377 bind_sockaddr.sdn_flags     = 0;
378 bind_sockaddr.sdn_objnum    = num;
379
380 if ( num ) {
381  bind_sockaddr.sdn_objnamel = 0;
382 } else {
383  bind_sockaddr.sdn_objnamel  = ROAR_dn_htons(strlen(object));
384  if ( bind_sockaddr.sdn_objnamel > DN_MAXOBJL )
385   bind_sockaddr.sdn_objnamel = DN_MAXOBJL;
386  strncpy((char*)bind_sockaddr.sdn_objname, object, DN_MAXOBJL);
387 }
388
389 if ( bind(fh, (struct sockaddr *) &bind_sockaddr, sizeof(bind_sockaddr)) == -1 ) {
390  close(fh);
391  return -1;
392 }
393
394 if ( listen(fh, 8) == -1 ) {
395  close(fh);
396  return -1;
397 }
398
399 return fh;
400#else
401 return -1;
402#endif
403}
404
405char * roar_socket_get_local_nodename(void) {
406#ifdef ROAR_HAVE_LIBDNET
407 static char node[16] = {0};
408 struct dn_naddr      *binaddr;
409 struct nodeent       *dp;
410
411 if ( !node[0] ) {
412  if ( (binaddr=getnodeadd()) == NULL)
413   return NULL;
414
415  if ( (dp=getnodebyaddr((char*)binaddr->a_addr, binaddr->a_len, PF_DECnet)) == NULL )
416   return NULL;
417
418  strncpy(node, dp->n_name, 15);
419  node[15] = 0;
420 }
421
422 return node;
423#else
424 return NULL;
425#endif
426}
427
428int roar_socket_open (int mode, int type, char * host, int port) {
429// int type = ROAR_SOCKET_TYPE_INET;
430 int fh;
431#ifdef ROAR_HAVE_IPX
432#define _NEED_OBJ
433 int i;
434 int ret;
435#endif
436 union {
437  struct sockaddr_in  in;
438#ifdef ROAR_HAVE_UNIX
439  struct sockaddr_un  un;
440#endif
441#ifdef ROAR_HAVE_IPV6
442  struct sockaddr_in6 in6;
443#endif
444#ifdef ROAR_HAVE_IPX
445  struct sockaddr_ipx ipx;
446#endif
447 } socket_addr;
448 struct hostent     * he;
449 //unsigned int host_div = 0;
450 int (*mode_func)(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen) = connect; // default is to connect
451#ifdef ROAR_HAVE_LIBDNET
452#define _NEED_OBJ
453#endif
454#ifdef _NEED_OBJ
455 char obj[80];
456 char * del;
457#endif
458
459 if ( mode == MODE_LISTEN )
460  mode_func = bind;
461
462 if ( type == ROAR_SOCKET_TYPE_UNKNOWN ) {
463  type = ROAR_SOCKET_TYPE_INET;
464  if ( *host == '/' ) {
465   type = ROAR_SOCKET_TYPE_UNIX;
466  } else if ( strcmp(host, "+fork") == 0 ) {
467   type = ROAR_SOCKET_TYPE_FORK;
468  } else if ( strstr(host, "::") != NULL ) {
469   type = ROAR_SOCKET_TYPE_DECNET;
470  } else if ( host[strlen(host)-1] == ')' ) {
471   type = ROAR_SOCKET_TYPE_IPX;
472  }
473 }
474
475
476 ROAR_DBG("roar_socket_open(*): type=%s, host='%s', port=%i",
477             type == ROAR_SOCKET_TYPE_UNIX ? "UNIX" : "INET", host, port);
478
479 if ( type == ROAR_SOCKET_TYPE_DECNET ) {
480#ifdef ROAR_HAVE_LIBDNET
481   ROAR_DBG("roar_socket_open(*): hostname for DECnet: host(%p)=%s", host, host);
482   del = strstr(host, "::");
483   ROAR_DBG("roar_socket_open(*): hostname for DECnet: del(%p)=%s", del, del);
484
485   if ( del == NULL ) {
486    ROAR_WARN("roar_socket_open(*): invalid hostname for DECnet: %s", host);
487    return -1;
488   }
489
490   *del = 0;
491
492   if ( *(del+2) == '#' ) { // assume we have node::#num
493    port = atoi(del+2);
494   }
495
496   if ( port ) {
497    snprintf(obj, 7, "%i", port); // no need for snprintf() as dec(port) is smaller than obj[]
498   } else {
499    *obj = 0;
500    strncat(obj, del+2, 79);
501   }
502
503  if ( mode == MODE_LISTEN ) {
504   fh = roar_socket_listen_decnet(obj, port);
505   *del = ':';
506   return fh;
507//   return -1; // listen sockets on DECnet are not supportet at the moment
508  } else {
509   // There is nothing wrong in this case to use dnet_conn() so we do.
510   fh = dnet_conn(host, obj, SOCK_STREAM, 0 ,0 ,0 , 0);
511   *del = ':';
512   return fh;
513  }
514#else
515  return -1; // no decnet support
516#endif
517 }
518
519 memset(&socket_addr,    0, sizeof(socket_addr));
520 memset(&he,             0, sizeof(he));               // FIXME: we have a valid pointer in here????
521
522
523 if ( type == ROAR_SOCKET_TYPE_INET || type == ROAR_SOCKET_TYPE_INET6 ) {
524
525  if ( (he = gethostbyname(host)) == NULL ) {
526   ROAR_ERR("roar_socket_open(*): Can\'t resolve host name '%s'",
527                     host);
528   return -1;
529  }
530
531   memcpy((struct in_addr *)&socket_addr.in.sin_addr, he->h_addr, sizeof(struct in_addr));
532
533   /* set the connect information */
534   socket_addr.in.sin_family = AF_INET;
535   socket_addr.in.sin_port   = ROAR_HOST2NET16(port);
536
537   fh = roar_socket_new_tcp();
538
539   if ( mode_func(fh, (struct sockaddr *)&socket_addr.in, sizeof(struct sockaddr_in)) == -1 ) {
540    ROAR_DBG("roar_socket_open(*): Can not connect/bind: %s", strerror(errno));
541    close(fh);
542    return -1;
543   }
544  // hey! we have a socket...
545 } else if ( type == ROAR_SOCKET_TYPE_UNIX ) {
546#ifdef ROAR_HAVE_UNIX
547  socket_addr.un.sun_family = AF_UNIX;
548  strncpy(socket_addr.un.sun_path, host, sizeof(socket_addr.un.sun_path) - 1);
549
550  fh = roar_socket_new_unix();
551
552  if ( mode_func(fh, (struct sockaddr *)&socket_addr.un, sizeof(struct sockaddr_un)) == -1 ) {
553   ROAR_DBG("roar_socket_open(*): Can not connect/bind: %s", strerror(errno));
554   close(fh);
555   return -1;
556  }
557#else
558  ROAR_ERR("roar_socket_open(*): There is no UNIX Domain Socket support in win32, download a real OS.");
559  return -1;
560#endif
561 } else if ( type == ROAR_SOCKET_TYPE_IPX ) {
562#ifdef ROAR_HAVE_IPX
563  socket_addr.ipx.sipx_family = AF_IPX;
564
565  obj[0] = 0;
566
567  if ( (ret = sscanf(host, "%8x.%12s(%x)", &socket_addr.ipx.sipx_network, obj,
568                               (unsigned int *)&socket_addr.ipx.sipx_port)) < 2 ) {
569   return -1;
570  } else if ( ret == 2 ) {
571   socket_addr.ipx.sipx_port = port; // Network Byte Order?
572  }
573
574  memset(socket_addr.ipx.sipx_node, 0, IPX_NODE_LEN);
575  ret = strlen(obj);
576
577  if ( ret % 2 )  // needs to be even at the moment
578   return -1;
579
580  fh = roar_socket_new_ipx();
581
582  close(fh);
583  return -1;
584#else
585  return -1;
586#endif
587 } else if ( type == ROAR_SOCKET_TYPE_FORK ) {
588  return roar_socket_open_fork(mode, host, port);
589 } else if ( type == ROAR_SOCKET_TYPE_FILE ) {
590  return roar_socket_open_file(mode, host, port);
591 } else {
592  return -1;
593 }
594
595 if ( mode == MODE_LISTEN )
596  if ( listen(fh, ROAR_SOCKET_QUEUE_LEN) == -1 ) {
597   close(fh);
598   return -1;
599  }
600
601 return fh;
602}
603
604int roar_socket_open_fork  (int mode, char * host, int port) {
605#ifndef ROAR_TARGET_WIN32
606 int socks[2];
607 int r;
608 char fhstr[8];
609
610 if ( mode == MODE_LISTEN )
611  return -1;
612
613 if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) {
614  return -1;
615 }
616
617 r = fork();
618
619 if ( r == -1 ) { // error!
620  ROAR_ERR("roar_socket_open_fork(*): Can not fork: %s", strerror(errno));
621  close(socks[0]);
622  close(socks[1]);
623  return -1;
624 } else if ( r == 0 ) { // we are the child
625  close(socks[0]);
626
627  close(ROAR_STDIN ); // we do not want roard to have any standard input
628  close(ROAR_STDOUT); // STDOUT is also not needed, so we close it,
629                      // but STDERR we keep open for error messages.
630
631  snprintf(fhstr, 7, "%i", socks[1]);
632
633  execlp("roard", "roard", "--no-listen", "--client-fh", fhstr, NULL);
634
635  // we are still alive?
636  ROAR_ERR("roar_socket_open_fork(*): alive after exec(), that's bad!");
637  _exit(1);
638 } else { // we are the parent
639  close(socks[1]);
640  return socks[0];
641 }
642
643 return -1;
644#else
645 ROAR_ERR("roar_socket_open_fork(*): There is no UNIX Domain Socket support in win32, download a real OS.");
646 return -1;
647#endif
648}
649
650int roar_socket_open_file  (int mode, char * host, int port) {
651 int fh;
652
653 if ( mode == MODE_LISTEN )
654  return -1;
655
656 if ( (fh = open(host, O_RDONLY, 0644)) == -1 ) {
657  ROAR_ERR("roar_socket_open_file(*): Can not open file %s: %s", host, strerror(errno));
658 }
659
660 return fh;
661}
662
663// --- [ PROXY CODE ] ---
664
665// generic proxy code:
666
667#ifdef ROAR_SUPPORT_PROXY
668int roar_socket_open_proxy (int mode, int type, char * host, int port, char * proxy_type) {
669 int    proxy_port = -1;
670 char   proxy_host[ROAR_SOCKET_MAX_HOSTNAMELEN];
671 char * proxy_addr = NULL;
672 int    i;
673 int    fh = -1;
674 char * user = NULL, * pw = NULL, * opts = NULL;
675 char * sep;
676 int    no_fh = 0;
677 char   proxy_addr_buf[1024];
678 static struct passwd * passwd;
679 int (* code)(int mode, int fh, char * host, int port, char * user, char * pw, char * opts) = NULL;
680
681 if ( passwd == NULL ) {
682  passwd = getpwuid(getuid());
683 }
684
685 if ( passwd != NULL )
686  user = passwd->pw_name;
687
688 if ( user == NULL )
689  user = getenv("USER");
690
691 // TODO: change this so we support listen() proxys (ssh -R)
692 if ( mode != MODE_CONNECT )
693  return -1;
694
695 if ( !strncmp(proxy_type, "socks", 5) ) {
696  proxy_addr = getenv("socks_proxy");
697
698  proxy_port = 9050; // TOR's default port
699 } else if ( !strcmp(proxy_type, "http") || !strcmp(proxy_type, "https") ) {
700  proxy_port = 8080;
701
702  if ( (proxy_addr = getenv("http_proxy")) == NULL )
703   proxy_addr = getenv("https_proxy");
704
705  if ( proxy_addr == NULL )
706   return -1;
707
708  if ( !strncmp(proxy_addr, "http://", 7) )
709   proxy_addr += 7;
710 } else if ( !strncmp(proxy_type, "ssh", 3) ) {
711  proxy_port = 22;
712  proxy_addr = getenv("ssh_proxy");
713  no_fh      = 1;
714 }
715
716 proxy_addr_buf[1023] = 0;
717 strncpy(proxy_addr_buf, proxy_addr, 1023);
718 proxy_addr = proxy_addr_buf;
719
720 if ( (sep = strstr(proxy_type, "/")) != NULL )
721  opts = sep+1;
722
723 if ( proxy_addr == NULL )
724  return -1;
725
726 if ( (sep = strstr(proxy_addr, "@")) != NULL ) {
727  *sep = 0;
728  user = proxy_addr;
729  proxy_addr = sep+1;
730
731  if ( (sep = strstr(user, ":")) != NULL ) {
732   *sep = 0;
733   pw = sep+1;
734  }
735 }
736
737 ROAR_DBG("roar_socket_open_proxy(*): proxy_type='%s', opts='%s', user='%s', pw=(not shown), proxy_addr='%s'", proxy_type, opts, user, proxy_addr);
738
739 for (i = 0; proxy_addr[i] != 0 && proxy_addr[i] != ':' && i < ROAR_SOCKET_MAX_HOSTNAMELEN; i++)
740  proxy_host[i] = proxy_addr[i];
741 proxy_host[i] = 0;
742
743 if ( i == 0 ) // no hostname found
744  return -1;
745
746 if ( proxy_addr[i] == ':' )
747  proxy_port = atoi(&proxy_addr[i+1]);
748
749 if ( ! no_fh ) {
750  if ( (fh = roar_socket_open(mode, type, proxy_host, proxy_port)) == -1) {
751   return -1;
752  }
753 }
754
755 if ( !strcmp(proxy_type, "socks4a") ) { // for TOR, the only supported type at the moment
756  code = roar_socket_open_socks4a;
757 } else if ( !strcmp(proxy_type, "socks4d") ) { // DECnet
758  code = roar_socket_open_socks4d;
759 } else if ( !strcmp(proxy_type, "socks4") ) { // good old SOCKS4
760  code = roar_socket_open_socks4;
761 } else if ( !strcmp(proxy_type, "http") ) { // HTTP CONNECT
762  code = roar_socket_open_http;
763 } else if ( !strncmp(proxy_type, "ssh", 3) ) { // SSH...
764#ifdef ROAR_HAVE_BIN_SSH
765  code = roar_socket_open_ssh;
766#else
767  ROAR_ERR("roar_socket_open_proxy(*): No SSH support compiled in");
768#endif
769 } else {
770  return -1; // unknown type
771 }
772
773 if ( code != NULL ) {
774  if ( no_fh ) {
775   fh = code(mode, fh, host, port, user, pw, opts);
776  } else {
777   if ( code(mode, fh, host, port, user, pw, opts) == -1 ) {
778    close(fh);
779    return -1;
780   }
781  }
782
783  return fh;
784 }
785
786 close(fh);
787 return -1;
788}
789
790// protocoll dependet proxy code:
791
792int roar_socket_open_socks4 (int mode, int fh, char * host, int port, char * user, char * pw, char * opts) {
793 struct hostent     * he;
794
795 if ( (he = gethostbyname(host)) == NULL ) {
796  ROAR_ERR("roar_socket_open_socks4(*): Can\'t resolve host name '%s'", host);
797  return -1;
798 }
799
800 return roar_socket_open_socks4x(mode, fh, he->h_addr, port, NULL, 0, user);
801}
802
803int roar_socket_open_socks4a(int mode, int fh, char * host, int port, char * user, char * pw, char * opts) {
804 return roar_socket_open_socks4x(mode, fh, "\0\0\0\1", port, host, strlen(host)+1, user);
805}
806
807int roar_socket_open_socks4d(int mode, int fh, char * host, int port, char * user, char * pw, char * opts) {
808 size_t len = strlen(host)+1;
809 char * dp;
810
811 if ( port == 0 ) {
812  if ( (dp = strstr(host, "::")) == NULL )
813   return -1;
814
815  len--;
816  *dp = 0;
817  memmove(dp+1, dp+2, len - (dp-host) - 1);
818 }
819
820 return roar_socket_open_socks4x(mode, fh, "\0\2\0\0", port, host, len, user);
821}
822
823int roar_socket_open_socks4x(int mode, int fh, char host[4], int port, char * app, size_t app_len, char * user) {
824 char buf[9];
825 int len;
826
827 buf[0] = 0x04;
828 buf[1] = mode == MODE_CONNECT ? 0x01 : 0x02;
829 *((uint16_t*)&buf[2]) = htons(port);
830 memcpy(buf+4, host, 4);
831
832 if ( user == NULL ) {
833  buf[8] = 0x00;
834  len = 9;
835 } else {
836  len = 8;
837 }
838
839 if ( write(fh, buf, len) != len )
840  return -1;
841
842 if ( user != NULL ) {
843  len = strlen(user) + 1;
844  if ( write(fh, user, len) != len )
845   return -1;
846 }
847
848 if ( app_len > 0 )
849  if ( write(fh, app, app_len) != app_len )
850   return -1;
851
852 if ( read(fh, buf, 8) != 8 )
853  return -1;
854
855 if ( buf[1] != 0x5a )
856  return -1;
857
858 return 0;
859}
860
861int roar_socket_open_http   (int mode, int fh, char * host, int port, char * user, char * pw, char * opts) {
862 char buf[1024];
863 int len;
864
865 if ( port == 0 || host == NULL )
866  return -1;
867
868 if ( *host == '/' ) // AF_UNIX
869  return -1;
870
871 if ( (len = snprintf(buf, 1024, "CONNECT %s:%i HTTP/1.0\r\nUser-Agent: libroar\r\n\r\n", host, port)) == -1 )
872  return -1;
873
874 if ( write(fh, buf, len) != len )
875  return -1;
876
877 while ( (len = read(fh, buf, 1024)) ) {
878  if ( len == 1024 ) { // overlong lion
879   return -1;
880  } else if ( len == 2 && buf[0] == '\r' && buf[1] == '\n' ) {
881   break;
882  } else if ( len == 1 && (buf[0] == '\r' || buf[0] == '\n') ) { // bad proxy or devel trying to debug ;)
883   break;
884  } else if ( len >= 4 && buf[len-4] == '\r' && buf[len-3] == '\n' && buf[len-2] == '\r' && buf[len-1] == '\n' ) {
885   break;
886  }
887 }
888
889 return 0;
890}
891
892
893#ifdef ROAR_HAVE_BIN_SSH
894int roar_socket_open_ssh    (int mode, int fh, char * host, int port, char * user, char * pw, char * opts) {
895 char * proxy_addr = getenv("ssh_proxy");
896 char * sep;
897 char   cmd[1024] = {0}, rcmd[1024] = {0};
898 int    proxy_port = 22;
899 int    use_socat = 0;
900 int r;
901 int socks[2];
902
903 if ( host == NULL )
904  return -1;
905
906 if ( *host == '/' )
907  use_socat = 1;
908
909 if ( mode == MODE_LISTEN )
910  return -1;
911
912 if ( proxy_addr == NULL )
913  return -1;
914
915 if ( opts != NULL ) {
916  if ( !strcmp(opts, "socat") ) {
917   use_socat = 1;
918  } else if ( !strcmp(opts, "netcat") ) {
919   use_socat = 0;
920  } else {
921   return -1;
922  }
923 }
924
925 ROAR_DBG("roar_socket_open_ssh(*): proxy_addr='%s'", proxy_addr);
926
927 if ( (sep = strstr(proxy_addr, "@")) != NULL )
928  proxy_addr = sep+1;
929
930 if ( (sep = strstr(proxy_addr, ":")) != NULL ) {
931  *sep = 0;
932  proxy_port = atoi(sep+1);
933 }
934
935
936 if ( !strcmp(host, "+fork") ) {
937  strncpy(rcmd, "roard --no-listen --client-fh 0", 32);
938 } else {
939  if ( use_socat ) {
940   if ( *host == '/' ) {
941    snprintf(rcmd, 1023, "socat stdio unix-connect:\"%s\"", host);
942   } else {
943    snprintf(rcmd, 1023, "socat stdio tcp:\"%s\":%i", host, port);
944   }
945  } else {
946   snprintf(rcmd, 1023, "$(which netcat nc 2> /dev/null | grep -v \" \" | head -n 1) \"%s\" %i", host, port);
947  }
948
949  rcmd[1023] = 0;
950 }
951
952 ROAR_DBG("roar_socket_open_ssh(*): proxy_port=%i, user='%s', proxy_addr='%s'", proxy_port, user, proxy_addr);
953 ROAR_DBG("roar_socket_open_ssh(*): rcmd: %s", rcmd);
954 snprintf(cmd, 1023, ROAR_HAVE_BIN_SSH " -p %i -l '%s' '%s' '%s'", proxy_port, user, proxy_addr, rcmd);
955 cmd[1023] = 0;
956
957
958 if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) {
959  return -1;
960 }
961
962 r = fork();
963
964 if ( r == -1 ) { // error!
965  ROAR_ERR("roar_socket_open_ssh(*): Can not fork: %s", strerror(errno));
966  close(socks[0]);
967  close(socks[1]);
968  return -1;
969 } else if ( r == 0 ) { // we are the child
970  close(socks[0]);
971
972  close(ROAR_STDIN ); // we do not want roard to have any standard input
973  close(ROAR_STDOUT); // STDOUT is also not needed, so we close it,
974                      // but STDERR we keep open for error messages.
975
976  dup2(socks[1], 0);
977  dup2(socks[1], 1);
978
979  execlp("sh", "sh", "-c", cmd, NULL);
980
981  // we are still alive?
982  ROAR_ERR("roar_socket_open_ssh(*): alive after exec(), that's bad!");
983  _exit(1);
984 } else { // we are the parent
985  close(socks[1]);
986  return socks[0];
987 }
988 return -1;
989}
990#endif
991
992#endif // ROAR_SUPPORT_PROXY
993
994//ll
Note: See TracBrowser for help on using the repository browser.