source: roaraudio/libroar/socket.c @ 890:c3559be0bc00

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

added check for IPv6

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