source: roaraudio/libroar/socket.c @ 1644:cae19c83efbb

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

SCM_RIGHTS does not work on Solaris

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