source: roaraudio/libroar/socket.c @ 1440:2036187332e1

Last change on this file since 1440:2036187332e1 was 1440:2036187332e1, checked in by phi, 15 years ago

disable a lot things for microcontroller

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