source: roaraudio/libroar/socket.c @ 1096:d78f6d9d2470

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

seems win32 C lib is broken (didn't we know that allrady?)

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