source: roaraudio/libroar/socket.c @ 1443:4cbb3dc65278

Last change on this file since 1443:4cbb3dc65278 was 1443:4cbb3dc65278, checked in by phi, 15 years ago

AF_UNIX -> only if we have, BSD Socket options -> only if we have BSD Sockets

File size: 24.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#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)
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)
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 union {
459  struct sockaddr     sa;
460#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6)
461  struct sockaddr_in  in;
462#endif
463#ifdef ROAR_HAVE_UNIX
464  struct sockaddr_un  un;
465#endif
466#ifdef ROAR_HAVE_IPV6
467  struct sockaddr_in6 in6;
468#endif
469#ifdef ROAR_HAVE_IPX
470  struct sockaddr_ipx ipx;
471#endif
472 } socket_addr;
473#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6)
474 struct hostent     * he;
475#endif
476 //unsigned int host_div = 0;
477 int (*mode_func)(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen) = connect; // default is to connect
478#ifdef ROAR_HAVE_LIBDNET
479#define _NEED_OBJ
480#endif
481#ifdef _NEED_OBJ
482 char obj[80];
483 char * del;
484#endif
485
486 if ( mode == MODE_LISTEN )
487  mode_func = bind;
488
489 if ( type == ROAR_SOCKET_TYPE_UNKNOWN ) {
490  type = ROAR_SOCKET_TYPE_INET;
491  if ( *host == '/' ) {
492   type = ROAR_SOCKET_TYPE_UNIX;
493  } else if ( strcmp(host, "+fork") == 0 ) {
494   type = ROAR_SOCKET_TYPE_FORK;
495  } else if ( strstr(host, "::") != NULL ) {
496   type = ROAR_SOCKET_TYPE_DECNET;
497  } else if ( host[strlen(host)-1] == ')' ) {
498   type = ROAR_SOCKET_TYPE_IPX;
499  }
500 }
501
502
503 ROAR_DBG("roar_socket_open(*): type=%s, host='%s', port=%i",
504             type == ROAR_SOCKET_TYPE_UNIX ? "UNIX" : "INET", host, port);
505
506 if ( type == ROAR_SOCKET_TYPE_DECNET ) {
507#ifdef ROAR_HAVE_LIBDNET
508   ROAR_DBG("roar_socket_open(*): hostname for DECnet: host(%p)=%s", host, host);
509   del = strstr(host, "::");
510   ROAR_DBG("roar_socket_open(*): hostname for DECnet: del(%p)=%s", del, del);
511
512   if ( del == NULL ) {
513    ROAR_WARN("roar_socket_open(*): invalid hostname for DECnet: %s", host);
514    return -1;
515   }
516
517   *del = 0;
518
519   if ( *(del+2) == '#' ) { // assume we have node::#num
520    port = atoi(del+2);
521   }
522
523   if ( port ) {
524    snprintf(obj, 7, "%i", port); // no need for snprintf() as dec(port) is smaller than obj[]
525   } else {
526    *obj = 0;
527    strncat(obj, del+2, 79);
528   }
529
530  if ( mode == MODE_LISTEN ) {
531   fh = roar_socket_listen_decnet(obj, port);
532   *del = ':';
533   return fh;
534//   return -1; // listen sockets on DECnet are not supportet at the moment
535  } else {
536   // There is nothing wrong in this case to use dnet_conn() so we do.
537   fh = dnet_conn(host, obj, SOCK_STREAM, 0 ,0 ,0 , 0);
538   *del = ':';
539   return fh;
540  }
541#else
542  return -1; // no decnet support
543#endif
544 }
545
546#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6)
547 memset(&socket_addr,    0, sizeof(socket_addr));
548 memset(&he,             0, sizeof(he));               // FIXME: we have a valid pointer in here????
549#endif
550
551
552 if ( type == ROAR_SOCKET_TYPE_INET || type == ROAR_SOCKET_TYPE_INET6 ) {
553#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6)
554
555  if ( (he = gethostbyname(host)) == NULL ) {
556   ROAR_ERR("roar_socket_open(*): Can\'t resolve host name '%s'",
557                     host);
558   return -1;
559  }
560
561   memcpy((struct in_addr *)&socket_addr.in.sin_addr, he->h_addr, sizeof(struct in_addr));
562
563   /* set the connect information */
564   socket_addr.in.sin_family = AF_INET;
565   socket_addr.in.sin_port   = ROAR_HOST2NET16(port);
566
567   fh = roar_socket_new_tcp();
568
569   if ( mode_func(fh, (struct sockaddr *)&socket_addr.in, sizeof(struct sockaddr_in)) == -1 ) {
570    ROAR_DBG("roar_socket_open(*): Can not connect/bind: %s", strerror(errno));
571    close(fh);
572    return -1;
573   }
574  // hey! we have a socket...
575#else
576  return -1;
577#endif
578 } else if ( type == ROAR_SOCKET_TYPE_UNIX ) {
579#ifdef ROAR_HAVE_UNIX
580  socket_addr.un.sun_family = AF_UNIX;
581  strncpy(socket_addr.un.sun_path, host, sizeof(socket_addr.un.sun_path) - 1);
582
583  fh = roar_socket_new_unix();
584
585  if ( mode_func(fh, (struct sockaddr *)&socket_addr.un, sizeof(struct sockaddr_un)) == -1 ) {
586   ROAR_DBG("roar_socket_open(*): Can not connect/bind: %s", strerror(errno));
587   close(fh);
588   return -1;
589  }
590#else
591  ROAR_ERR("roar_socket_open(*): There is no UNIX Domain Socket support in win32, download a real OS.");
592  return -1;
593#endif
594 } else if ( type == ROAR_SOCKET_TYPE_IPX ) {
595#ifdef ROAR_HAVE_IPX
596  socket_addr.ipx.sipx_family = AF_IPX;
597
598  obj[0] = 0;
599
600  if ( (ret = sscanf(host, "%8x.%12s(%x)", &socket_addr.ipx.sipx_network, obj,
601                               (unsigned int *)&socket_addr.ipx.sipx_port)) < 2 ) {
602   return -1;
603  } else if ( ret == 2 ) {
604   socket_addr.ipx.sipx_port = port; // Network Byte Order?
605  }
606
607  memset(socket_addr.ipx.sipx_node, 0, IPX_NODE_LEN);
608  ret = strlen(obj);
609
610  if ( ret % 2 )  // needs to be even at the moment
611   return -1;
612
613  fh = roar_socket_new_ipx();
614
615  close(fh);
616  return -1;
617#else
618  return -1;
619#endif
620 } else if ( type == ROAR_SOCKET_TYPE_FORK ) {
621  return roar_socket_open_fork(mode, host, port);
622 } else if ( type == ROAR_SOCKET_TYPE_FILE ) {
623  return roar_socket_open_file(mode, host, port);
624 } else {
625  return -1;
626 }
627
628 if ( mode == MODE_LISTEN )
629  if ( listen(fh, ROAR_SOCKET_QUEUE_LEN) == -1 ) {
630   close(fh);
631   return -1;
632  }
633
634 return fh;
635}
636
637int roar_socket_open_fork  (int mode, char * host, int port) {
638#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
639 int socks[2];
640 int r;
641 char fhstr[8];
642
643 if ( mode == MODE_LISTEN )
644  return -1;
645
646 if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) {
647  return -1;
648 }
649
650 r = fork();
651
652 if ( r == -1 ) { // error!
653  ROAR_ERR("roar_socket_open_fork(*): Can not fork: %s", strerror(errno));
654  close(socks[0]);
655  close(socks[1]);
656  return -1;
657 } else if ( r == 0 ) { // we are the child
658  close(socks[0]);
659
660  close(ROAR_STDIN ); // we do not want roard to have any standard input
661  close(ROAR_STDOUT); // STDOUT is also not needed, so we close it,
662                      // but STDERR we keep open for error messages.
663
664  snprintf(fhstr, 7, "%i", socks[1]);
665
666  execlp("roard", "roard", "--no-listen", "--client-fh", fhstr, NULL);
667
668  // we are still alive?
669  ROAR_ERR("roar_socket_open_fork(*): alive after exec(), that's bad!");
670  _exit(1);
671 } else { // we are the parent
672  close(socks[1]);
673  return socks[0];
674 }
675
676 return -1;
677#else
678 ROAR_ERR("roar_socket_open_fork(*): There is no UNIX Domain Socket support in win32, download a real OS.");
679 return -1;
680#endif
681}
682
683int roar_socket_open_file  (int mode, char * host, int port) {
684 int fh;
685
686 if ( mode == MODE_LISTEN )
687  return -1;
688
689 if ( (fh = open(host, O_RDONLY, 0644)) == -1 ) {
690  ROAR_ERR("roar_socket_open_file(*): Can not open file %s: %s", host, strerror(errno));
691 }
692
693 return fh;
694}
695
696// --- [ PROXY CODE ] ---
697
698// generic proxy code:
699
700#ifdef ROAR_SUPPORT_PROXY
701int roar_socket_open_proxy (int mode, int type, char * host, int port, char * proxy_type) {
702 int    proxy_port = -1;
703 char   proxy_host[ROAR_SOCKET_MAX_HOSTNAMELEN];
704 char * proxy_addr = NULL;
705 int    i;
706 int    fh = -1;
707 char * user = NULL, * pw = NULL, * opts = NULL;
708 char * sep;
709 int    no_fh = 0;
710 char   proxy_addr_buf[1024];
711 static struct passwd * passwd;
712 int (* code)(int mode, int fh, char * host, int port, char * user, char * pw, char * opts) = NULL;
713
714 if ( passwd == NULL ) {
715  passwd = getpwuid(getuid());
716 }
717
718 if ( passwd != NULL )
719  user = passwd->pw_name;
720
721 if ( user == NULL )
722  user = getenv("USER");
723
724 // TODO: change this so we support listen() proxys (ssh -R)
725 if ( mode != MODE_CONNECT )
726  return -1;
727
728 if ( !strncmp(proxy_type, "socks", 5) ) {
729  proxy_addr = getenv("socks_proxy");
730
731  proxy_port = 9050; // TOR's default port
732 } else if ( !strcmp(proxy_type, "http") || !strcmp(proxy_type, "https") ) {
733  proxy_port = 8080;
734
735  if ( (proxy_addr = getenv("http_proxy")) == NULL )
736   proxy_addr = getenv("https_proxy");
737
738  if ( proxy_addr == NULL )
739   return -1;
740
741  if ( !strncmp(proxy_addr, "http://", 7) )
742   proxy_addr += 7;
743 } else if ( !strncmp(proxy_type, "ssh", 3) ) {
744  proxy_port = 22;
745  proxy_addr = getenv("ssh_proxy");
746  no_fh      = 1;
747 }
748
749 proxy_addr_buf[1023] = 0;
750 strncpy(proxy_addr_buf, proxy_addr, 1023);
751 proxy_addr = proxy_addr_buf;
752
753 if ( (sep = strstr(proxy_type, "/")) != NULL )
754  opts = sep+1;
755
756 if ( proxy_addr == NULL )
757  return -1;
758
759 if ( (sep = strstr(proxy_addr, "@")) != NULL ) {
760  *sep = 0;
761  user = proxy_addr;
762  proxy_addr = sep+1;
763
764  if ( (sep = strstr(user, ":")) != NULL ) {
765   *sep = 0;
766   pw = sep+1;
767  }
768 }
769
770 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);
771
772 for (i = 0; proxy_addr[i] != 0 && proxy_addr[i] != ':' && i < ROAR_SOCKET_MAX_HOSTNAMELEN; i++)
773  proxy_host[i] = proxy_addr[i];
774 proxy_host[i] = 0;
775
776 if ( i == 0 ) // no hostname found
777  return -1;
778
779 if ( proxy_addr[i] == ':' )
780  proxy_port = atoi(&proxy_addr[i+1]);
781
782 if ( ! no_fh ) {
783  if ( (fh = roar_socket_open(mode, type, proxy_host, proxy_port)) == -1) {
784   return -1;
785  }
786 }
787
788 if ( !strcmp(proxy_type, "socks4a") ) { // for TOR, the only supported type at the moment
789  code = roar_socket_open_socks4a;
790 } else if ( !strcmp(proxy_type, "socks4d") ) { // DECnet
791  code = roar_socket_open_socks4d;
792 } else if ( !strcmp(proxy_type, "socks4") ) { // good old SOCKS4
793  code = roar_socket_open_socks4;
794 } else if ( !strcmp(proxy_type, "http") ) { // HTTP CONNECT
795  code = roar_socket_open_http;
796 } else if ( !strncmp(proxy_type, "ssh", 3) ) { // SSH...
797#ifdef ROAR_HAVE_BIN_SSH
798  code = roar_socket_open_ssh;
799#else
800  ROAR_ERR("roar_socket_open_proxy(*): No SSH support compiled in");
801#endif
802 } else {
803  return -1; // unknown type
804 }
805
806 if ( code != NULL ) {
807  if ( no_fh ) {
808   fh = code(mode, fh, host, port, user, pw, opts);
809  } else {
810   if ( code(mode, fh, host, port, user, pw, opts) == -1 ) {
811    close(fh);
812    return -1;
813   }
814  }
815
816  return fh;
817 }
818
819 close(fh);
820 return -1;
821}
822
823// protocoll dependet proxy code:
824
825int roar_socket_open_socks4 (int mode, int fh, char * host, int port, char * user, char * pw, char * opts) {
826 struct hostent     * he;
827
828 if ( (he = gethostbyname(host)) == NULL ) {
829  ROAR_ERR("roar_socket_open_socks4(*): Can\'t resolve host name '%s'", host);
830  return -1;
831 }
832
833 return roar_socket_open_socks4x(mode, fh, he->h_addr, port, NULL, 0, user);
834}
835
836int roar_socket_open_socks4a(int mode, int fh, char * host, int port, char * user, char * pw, char * opts) {
837 return roar_socket_open_socks4x(mode, fh, "\0\0\0\1", port, host, strlen(host)+1, user);
838}
839
840int roar_socket_open_socks4d(int mode, int fh, char * host, int port, char * user, char * pw, char * opts) {
841 size_t len = strlen(host)+1;
842 char * dp;
843
844 if ( port == 0 ) {
845  if ( (dp = strstr(host, "::")) == NULL )
846   return -1;
847
848  len--;
849  *dp = 0;
850  memmove(dp+1, dp+2, len - (dp-host) - 1);
851 }
852
853 return roar_socket_open_socks4x(mode, fh, "\0\2\0\0", port, host, len, user);
854}
855
856int roar_socket_open_socks4x(int mode, int fh, char host[4], int port, char * app, size_t app_len, char * user) {
857 char buf[9];
858 int len;
859
860 buf[0] = 0x04;
861 buf[1] = mode == MODE_CONNECT ? 0x01 : 0x02;
862 *((uint16_t*)&buf[2]) = htons(port);
863 memcpy(buf+4, host, 4);
864
865 if ( user == NULL ) {
866  buf[8] = 0x00;
867  len = 9;
868 } else {
869  len = 8;
870 }
871
872 if ( write(fh, buf, len) != len )
873  return -1;
874
875 if ( user != NULL ) {
876  len = strlen(user) + 1;
877  if ( write(fh, user, len) != len )
878   return -1;
879 }
880
881 if ( app_len > 0 )
882  if ( write(fh, app, app_len) != app_len )
883   return -1;
884
885 if ( read(fh, buf, 8) != 8 )
886  return -1;
887
888 if ( buf[1] != 0x5a )
889  return -1;
890
891 return 0;
892}
893
894int roar_socket_open_http   (int mode, int fh, char * host, int port, char * user, char * pw, char * opts) {
895 char buf[1024];
896 int len;
897
898 if ( port == 0 || host == NULL )
899  return -1;
900
901 if ( *host == '/' ) // AF_UNIX
902  return -1;
903
904 if ( (len = snprintf(buf, 1024, "CONNECT %s:%i HTTP/1.0\r\nUser-Agent: libroar\r\n\r\n", host, port)) == -1 )
905  return -1;
906
907 if ( write(fh, buf, len) != len )
908  return -1;
909
910 while ( (len = read(fh, buf, 1024)) ) {
911  if ( len == 1024 ) { // overlong lion
912   return -1;
913  } else if ( len == 2 && buf[0] == '\r' && buf[1] == '\n' ) {
914   break;
915  } else if ( len == 1 && (buf[0] == '\r' || buf[0] == '\n') ) { // bad proxy or devel trying to debug ;)
916   break;
917  } else if ( len >= 4 && buf[len-4] == '\r' && buf[len-3] == '\n' && buf[len-2] == '\r' && buf[len-1] == '\n' ) {
918   break;
919  }
920 }
921
922 return 0;
923}
924
925
926#ifdef ROAR_HAVE_BIN_SSH
927int roar_socket_open_ssh    (int mode, int fh, char * host, int port, char * user, char * pw, char * opts) {
928 char * proxy_addr = getenv("ssh_proxy");
929 char * sep;
930 char   cmd[1024] = {0}, rcmd[1024] = {0};
931 int    proxy_port = 22;
932 int    use_socat = 0;
933 int r;
934 int socks[2];
935
936 if ( host == NULL )
937  return -1;
938
939 if ( *host == '/' )
940  use_socat = 1;
941
942 if ( mode == MODE_LISTEN )
943  return -1;
944
945 if ( proxy_addr == NULL )
946  return -1;
947
948 if ( opts != NULL ) {
949  if ( !strcmp(opts, "socat") ) {
950   use_socat = 1;
951  } else if ( !strcmp(opts, "netcat") ) {
952   use_socat = 0;
953  } else {
954   return -1;
955  }
956 }
957
958 ROAR_DBG("roar_socket_open_ssh(*): proxy_addr='%s'", proxy_addr);
959
960 if ( (sep = strstr(proxy_addr, "@")) != NULL )
961  proxy_addr = sep+1;
962
963 if ( (sep = strstr(proxy_addr, ":")) != NULL ) {
964  *sep = 0;
965  proxy_port = atoi(sep+1);
966 }
967
968
969 if ( !strcmp(host, "+fork") ) {
970  strncpy(rcmd, "roard --no-listen --client-fh 0", 32);
971 } else {
972  if ( use_socat ) {
973   if ( *host == '/' ) {
974    snprintf(rcmd, 1023, "socat stdio unix-connect:\"%s\"", host);
975   } else {
976    snprintf(rcmd, 1023, "socat stdio tcp:\"%s\":%i", host, port);
977   }
978  } else {
979   snprintf(rcmd, 1023, "$(which netcat nc 2> /dev/null | grep -v \" \" | head -n 1) \"%s\" %i", host, port);
980  }
981
982  rcmd[1023] = 0;
983 }
984
985 ROAR_DBG("roar_socket_open_ssh(*): proxy_port=%i, user='%s', proxy_addr='%s'", proxy_port, user, proxy_addr);
986 ROAR_DBG("roar_socket_open_ssh(*): rcmd: %s", rcmd);
987 snprintf(cmd, 1023, ROAR_HAVE_BIN_SSH " -p %i -l '%s' '%s' '%s'", proxy_port, user, proxy_addr, rcmd);
988 cmd[1023] = 0;
989
990
991 if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) {
992  return -1;
993 }
994
995 r = fork();
996
997 if ( r == -1 ) { // error!
998  ROAR_ERR("roar_socket_open_ssh(*): Can not fork: %s", strerror(errno));
999  close(socks[0]);
1000  close(socks[1]);
1001  return -1;
1002 } else if ( r == 0 ) { // we are the child
1003  close(socks[0]);
1004
1005  close(ROAR_STDIN ); // we do not want roard to have any standard input
1006  close(ROAR_STDOUT); // STDOUT is also not needed, so we close it,
1007                      // but STDERR we keep open for error messages.
1008
1009  dup2(socks[1], 0);
1010  dup2(socks[1], 1);
1011
1012  execlp("sh", "sh", "-c", cmd, NULL);
1013
1014  // we are still alive?
1015  ROAR_ERR("roar_socket_open_ssh(*): alive after exec(), that's bad!");
1016  _exit(1);
1017 } else { // we are the parent
1018  close(socks[1]);
1019  return socks[0];
1020 }
1021 return -1;
1022}
1023#endif
1024
1025#endif // ROAR_SUPPORT_PROXY
1026
1027//ll
Note: See TracBrowser for help on using the repository browser.