source: roaraudio/libroar/socket.c @ 1359:6a59a3cbc91a

Last change on this file since 1359:6a59a3cbc91a was 1359:6a59a3cbc91a, checked in by phi, 15 years ago

make IPv4 optional in most libroar files

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