source: roaraudio/libroar/socket.c @ 1085:fe83ee077214

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

disabled some functions completly because of lag of support on win32

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