source: roaraudio/libroar/socket.c @ 1067:a2d7c244141b

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

make OpenBSD security warnings patch a bit happyer...: sprintf() -> snprintf()

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