source: roaraudio/libroar/socket.c @ 5248:0133acb5ae31

Last change on this file since 5248:0133acb5ae31 was 5248:0133acb5ae31, checked in by phi, 12 years ago

removed most roar_socket_new_*()

File size: 31.7 KB
RevLine 
[0]1//socket.c:
2
[690]3/*
[4708]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
[690]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
[3517]21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
[690]23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
[5109]36#define _LIBROAR_NOATTR_TO_STATIC /* ignore warnings for TO_STATIC functions */
[0]37#include "libroar.h"
38
[81]39#define MODE_LISTEN  ROAR_SOCKET_MODE_LISTEN
40#define MODE_CONNECT ROAR_SOCKET_MODE_CONNECT
[0]41
[1761]42#ifdef ROAR_TARGET_WIN32
43void roar_socket_win32_init (void) {
44 static int inited = 0;
45 WSADATA wsadata;
46
47 if ( !inited ) {
48  WSAStartup(MAKEWORD(1,1) , &wsadata);
49  inited++;
50 }
51}
52#else
53#define roar_socket_win32_init()
54#endif
55
[873]56int roar_socket_decnet_set_timeout (int fh, time_t sec, int usec) {
57#ifdef ROAR_HAVE_LIBDNET
58 struct timeval timeout = {sec, usec};
59
60 return setsockopt(fh, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
61#else
62 return -1;
63#endif
64}
65
[968]66int roar_socket_recvbuf(int fh, int len) {
[1443]67#ifdef ROAR_HAVE_BSDSOCKETS
[968]68 if ( len < 256 ) len = 256;
69
[4939]70 return setsockopt(fh, SOL_SOCKET, SO_RCVBUF, (void *)&len, sizeof(len));
[1443]71#else
72 return -1;
73#endif
[968]74}
75
[5113]76int roar_socket_set_tos(int fh) {
[5210]77#if defined(ROAR_HAVE_BSDSOCKETS) && !defined(ROAR_TARGET_WIN32)
[5113]78 int opt = IPTOS_LOWDELAY;
79 int ret;
80
81 roar_err_clear_errno();
82 ret = setsockopt(fh, IPPROTO_IP, IP_TOS, &opt, sizeof(int));
83 if ( ret < 0 )
84  roar_err_from_errno();
85
86 return ret;
87#else
88 return -1;
89#endif
90}
91
[5248]92#if 0
93static int roar_socket_new_decnet_seqpacket (void) {
[508]94#ifdef ROAR_HAVE_LIBDNET
95 int fh;
96
97 fh = socket(AF_DECnet, SOCK_SEQPACKET, DNPROTO_NSP);
98
[873]99 roar_socket_decnet_set_timeout(fh, 300, 0);
100
[508]101 return fh;
102#else
103 return -1;
104#endif
105}
[5248]106#endif
[508]107
108
[5248]109#if 0
110static int roar_socket_new_ipxspx (void) {
111 return -1;
112}
113#endif
114
115int roar_socket_new        (int type) {
116 int sys_domain = -1, sys_type = -1, sys_protocol = 0;
[508]117 int fh;
[5248]118#if defined(ROAR_HAVE_IPV4) && defined(TCP_NODELAY) && !defined(ROAR_TARGET_WIN32)
119 int t   = 1;
120#endif
[508]121
[5248]122 switch (type) {
123  case ROAR_SOCKET_TYPE_NONE:
124  case ROAR_SOCKET_TYPE_GENSTR:
125    roar_err_set(ROAR_ERROR_INVAL);
126    return -1;
127   break;
128  case ROAR_SOCKET_TYPE_FILE:
129  case ROAR_SOCKET_TYPE_FORK:
130    roar_err_set(ROAR_ERROR_PERM);
131    return -1;
132   break;
133#ifdef ROAR_HAVE_IPV4
134  case ROAR_SOCKET_TYPE_TCP:
135    sys_domain = AF_INET; sys_type = SOCK_STREAM;
136   break;
137  case ROAR_SOCKET_TYPE_UDP:
138    sys_domain = AF_INET; sys_type = SOCK_DGRAM;
139   break;
140#endif
141#ifdef ROAR_HAVE_UNIX
142  case ROAR_SOCKET_TYPE_UNIX:
143    sys_domain = AF_UNIX; sys_type = SOCK_STREAM;
144   break;
145#endif
146#ifdef ROAR_HAVE_LIBDNET
147  case ROAR_SOCKET_TYPE_DECNET:
148    sys_domain = AF_DECnet; sys_type = SOCK_STREAM; sys_protocol = DNPROTO_NSP;
149   break;
150#endif
151#ifdef ROAR_HAVE_IPV6
152  case ROAR_SOCKET_TYPE_TCP6:
153    sys_domain = AF_INET6; sys_type = SOCK_STREAM;
154   break;
155  case ROAR_SOCKET_TYPE_UDP6:
156    sys_domain = AF_INET6; sys_type = SOCK_DGRAM;
157   break;
158#endif
159#ifdef ROAR_HAVE_IPX
160  case ROAR_SOCKET_TYPE_IPX:
161    sys_domain = AF_IPX; sys_type = SOCK_DGRAM; sys_protocol = AF_IPX;
162   break;
163#endif
164  case ROAR_SOCKET_TYPE_IPXSPX:
165  case ROAR_SOCKET_TYPE_LAT_SERVICE:
166  case ROAR_SOCKET_TYPE_LAT_REVERSE_PORT:
167  default:
168    roar_err_set(ROAR_ERROR_AFNOTSUP);
169    return -1;
170   break;
171 }
[508]172
[5248]173 roar_socket_win32_init();
174
175 roar_err_clear_all();
176 fh = socket(sys_domain, sys_type, sys_protocol);
177 if ( fh == -1 ) {
178  roar_err_update();
179  return -1;
180 }
181
182 // do the extra work we need to do for some socket types:
183 switch (type) {
184#ifdef ROAR_HAVE_IPV4
185  case ROAR_SOCKET_TYPE_TCP:
186#if defined(TCP_NODELAY) && !defined(ROAR_TARGET_WIN32)
187   setsockopt(fh, IPPROTO_TCP, TCP_NODELAY, &t, sizeof(int));
188#endif
189  case ROAR_SOCKET_TYPE_UDP:
190#endif
191#ifdef ROAR_HAVE_IPV6
192  case ROAR_SOCKET_TYPE_TCP6:
193  case ROAR_SOCKET_TYPE_UDP6:
194#endif
195#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6)
196    roar_socket_set_tos(fh);
197   break;
198#endif
199#ifdef ROAR_HAVE_LIBDNET
200  case ROAR_SOCKET_TYPE_DECNET:
201    roar_socket_decnet_set_timeout(fh, 300, 0);
202   break;
203#endif
204 }
[873]205
[508]206 return fh;
[530]207}
208
209
[0]210int roar_socket_nonblock(int fh, int state) {
[4554]211#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) && defined(ROAR_HAVE_FCNTL)
[0]212 int flags;
213
214 if ( (flags = fcntl(fh, F_GETFL, 0)) == -1 ) {
[5148]215  roar_err_from_errno();
[0]216  ROAR_ERR("roar_socket_nonblock(fh=%i, state=%i): Can not read flags: %s", fh, state, strerror(errno));
217  ROAR_DBG("roar_socket_nonblock(fh=%i, state=%i) = -1", fh, state);
218  return -1;
219 }
220
[1119]221 if ( !(flags & O_NONBLOCK) && state == ROAR_SOCKET_BLOCK )
222  return 0;
223
[0]224 flags |= O_NONBLOCK;
225
226 if ( state == ROAR_SOCKET_BLOCK )
227  flags -= O_NONBLOCK;
228
229 if ( fcntl(fh, F_SETFL, flags) == -1 ) {
[5148]230  roar_err_from_errno();
[0]231  ROAR_ERR("roar_socket_nonblock(fh=%i, state=%i): Can not set flags: %s", fh, state, strerror(errno));
232  ROAR_DBG("roar_socket_nonblock(fh=%i, state=%i) = -1", fh, state);
233  return -1;
234 }
235
236 ROAR_DBG("roar_socket_nonblock(fh=%i, state=%i) = 0", fh, state);
237 return 0;
[1085]238#else
239 ROAR_WARN("roar_socket_nonblock(*): no nonblocking IO support on win32, use a real OS");
240 return -1;
241#endif
[0]242}
243
[375]244int roar_socket_dup_udp_local_end (int fh) {
[4554]245#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) && defined(ROAR_HAVE_FCNTL)
[376]246 int                  n              = -1;
247 int                  flags          = -1;
248 struct sockaddr_in   socket_addr;
249 socklen_t            len            = sizeof(struct sockaddr_in);
250
251 if ( (flags = fcntl(fh, F_GETFL, 0)) == -1 ) {
252  ROAR_WARN("roar_socket_dup_udp_local_end(fh=%i): Can not read flags: %s", fh, strerror(errno));
253 }
254
255 if ( getsockname(fh, (struct sockaddr *)&socket_addr, &len) == -1 ) {
[5148]256  roar_err_from_errno();
[376]257  return -1;
258 }
259
260 if ( socket_addr.sin_family != AF_INET ) {
[5148]261  roar_err_set(ROAR_ERROR_TYPEMM);
[376]262  return -1;
263 }
264
[5248]265 n = roar_socket_new(ROAR_SOCKET_TYPE_UDP);
[376]266
267 if ( n == -1 )
268  return -1;
269
270//  if ( mode_func(fh, (struct sockaddr *)&socket_addr, sizeof(struct sockaddr_in)) == -1 ) {
271 if ( bind(n, (struct sockaddr *)&socket_addr, len) == -1 ) {
[5148]272  roar_err_from_errno();
[376]273  close(n);
274  return -1;
275 }
276
277 if ( flags != -1 ) {
278  if ( fcntl(fh, F_SETFL, flags) == -1 ) {
279   ROAR_WARN("roar_socket_dup_udp_local_end(fh=%i): Can not set flags: %s", fh, strerror(errno));
280   return -1;
281  }
282 }
283
284
285 return n;
[1085]286#else
287 ROAR_WARN("roar_socket_dup_udp_local_end(*): this function is not supported on win32, use a real OS");
288 return -1;
289#endif
[375]290}
291
[753]292
293#define _SCMR_CONTROLLEN (sizeof(struct cmsghdr) + sizeof(int))
294int roar_socket_send_fh (int sock, int fh, char * mes, size_t len) {
[4554]295#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_OS_SUNOS) && !defined(ROAR_TARGET_OPENVMS)
[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] = {0};
[753]301
[759]302 ROAR_DBG("roar_socket_send_fh(sock=%i, fh=%i, mes=%p, len=%u) = ?", sock, fh, mes, len);
303
[754]304 if ( sock < 0 || fh < 0 )
[753]305  return -1;
306
[754]307 if ( len == 0 ) {
308  len = 1;
309  mes = localmes;
310 }
311
312 memset(cmptr, 0, _SCMR_CONTROLLEN);
313
[753]314 iov[0].iov_base = mes;
315 iov[0].iov_len  = len;
316 msg.msg_iov     = iov;
317 msg.msg_iovlen  = 1;
318 msg.msg_name    = NULL;
319 msg.msg_namelen = 0;
320
321 cmptr->cmsg_level        = SOL_SOCKET;
322 cmptr->cmsg_type         = SCM_RIGHTS;
323 cmptr->cmsg_len          = _SCMR_CONTROLLEN;
324 msg.msg_control          = (caddr_t) cmptr;
325 msg.msg_controllen       = _SCMR_CONTROLLEN;
326 *(int *)CMSG_DATA(cmptr) = fh;
327
328 return sendmsg(sock, &msg, 0);
[1085]329#else
330 ROAR_ERR("roar_socket_send_fh(*): There is no UNIX Domain Socket support in win32, download a real OS.");
331 return -1;
332#endif
[753]333}
334
335int roar_socket_recv_fh (int sock,         char * mes, size_t * len) {
[4554]336#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_OS_SUNOS) && !defined(ROAR_TARGET_OPENVMS)
[753]337 struct iovec     iov[1];
338 struct msghdr    msg;
339 char             cmptr_buf[_SCMR_CONTROLLEN];
340 struct cmsghdr * cmptr = (struct cmsghdr *) cmptr_buf;
[754]341 char             localmes[1];
342 size_t           locallen[1] = {1};
[753]343
[4256]344 ROAR_DBG("roar_socket_recv_fh(sock=%i, mes=%p, len=%p) = ?", sock, mes, len);
345
346 if ( sock < 0 ) {
347  ROAR_DBG("roar_socket_recv_fh(sock=%i, mes=%p, len=%p) = -1 // invalid socket", sock, mes, len);
[753]348  return -1;
[4256]349 }
[753]350
[754]351 if ( len == NULL ) {
352  len = locallen;
353  mes = localmes;
354 }
355
[753]356 iov[0].iov_base = mes;
357 iov[0].iov_len  = *len;
358 msg.msg_iov     = iov;
359 msg.msg_iovlen  = 1;
360 msg.msg_name    = NULL;
361 msg.msg_namelen = 0;
362
363 msg.msg_control    = (caddr_t) cmptr;
364 msg.msg_controllen = _SCMR_CONTROLLEN;
365
[4256]366 if ( (*len = recvmsg(sock, &msg, 0)) == -1 ) {
367  ROAR_DBG("roar_socket_recv_fh(sock=%i, mes=%p, len=%p) = -1 // can not read from socket", sock, mes, len);
[753]368  return -1;
[4256]369 }
[753]370
[4257]371 if ( msg.msg_controllen  < _SCMR_CONTROLLEN ||
372      cmptr->cmsg_len    != _SCMR_CONTROLLEN  ) {
[4256]373  ROAR_DBG("roar_socket_recv_fh(sock=%i, mes=%p, len=%p) = -1 // control len is wrong", sock, mes, len);
[753]374  return -1;
[4256]375 }
[753]376
[4256]377 ROAR_DBG("roar_socket_recv_fh(sock=%i, mes=%p, len=%p) = %i", sock, mes, len, *(int *)CMSG_DATA(cmptr));
[753]378 return *(int *)CMSG_DATA(cmptr);
[1085]379#else
380 ROAR_ERR("roar_socket_recv_fh(*): There is no UNIX Domain Socket support in win32, download a real OS.");
381 return -1;
382#endif
[753]383}
384
[0]385int roar_socket_listen  (int type, char * host, int port) {
386 return roar_socket_open(MODE_LISTEN, type, host, port);
387}
388
389int roar_socket_connect (char * host, int port) {
[2]390 char * proxy_type = getenv("ROAR_PROXY");
391
[4692]392 ROAR_DBG("roar_socket_connect(host='%s', port=%i) = ?", host, port);
393
[2]394 if ( proxy_type == NULL || strcmp(proxy_type, "") == 0 ) {
395  return roar_socket_open(MODE_CONNECT, ROAR_SOCKET_TYPE_UNKNOWN, host, port);
396 } else {
[1087]397#ifdef ROAR_SUPPORT_PROXY
[2]398  return roar_socket_open_proxy(MODE_CONNECT, ROAR_SOCKET_TYPE_UNKNOWN, host, port, proxy_type);
[1087]399#else
400  ROAR_ERR("roar_socket_connect(host='%s', port=%i): no support for proxy code (proxy_type=%s)", host, port, proxy_type);
401  return -1;
402#endif
[2]403 }
[0]404}
405
[508]406
407int roar_socket_listen_decnet (char * object, int num) {
408#ifdef ROAR_HAVE_LIBDNET
[5248]409 int fh = roar_socket_new(ROAR_SOCKET_TYPE_DECNET);
[508]410 struct sockaddr_dn bind_sockaddr;
411
412 if ( fh == -1 )
413  return -1;
414
415 if ( !*object )
416  object = NULL;
417
418 if ( (object && num) || (!*object && !num) ) {
419  ROAR_WARN("roar_socket_listen_decnet(object='%s', num=%i): illegal address!", object, num);
420  close(fh);
421  return -1;
422 }
423
424 memset((void*)&bind_sockaddr, 0, sizeof(struct sockaddr_dn));
425
426 bind_sockaddr.sdn_family    = AF_DECnet;
427 bind_sockaddr.sdn_flags     = 0;
428 bind_sockaddr.sdn_objnum    = num;
429
430 if ( num ) {
431  bind_sockaddr.sdn_objnamel = 0;
432 } else {
[4073]433  bind_sockaddr.sdn_objnamel  = ROAR_HOST2LE16(strlen(object));
[1068]434  if ( bind_sockaddr.sdn_objnamel > DN_MAXOBJL )
435   bind_sockaddr.sdn_objnamel = DN_MAXOBJL;
436  strncpy((char*)bind_sockaddr.sdn_objname, object, DN_MAXOBJL);
[508]437 }
438
439 if ( bind(fh, (struct sockaddr *) &bind_sockaddr, sizeof(bind_sockaddr)) == -1 ) {
440  close(fh);
441  return -1;
442 }
443
444 if ( listen(fh, 8) == -1 ) {
445  close(fh);
446  return -1;
447 }
448
449 return fh;
450#else
451 return -1;
452#endif
453}
454
[5121]455const char * roar_socket_get_local_nodename(void) {
[521]456#ifdef ROAR_HAVE_LIBDNET
457 static char node[16] = {0};
458 struct dn_naddr      *binaddr;
459 struct nodeent       *dp;
460
461 if ( !node[0] ) {
462  if ( (binaddr=getnodeadd()) == NULL)
463   return NULL;
464
[3835]465  if ( (dp=getnodebyaddr((char*)binaddr->a_addr, binaddr->a_len, AF_DECnet)) == NULL )
[521]466   return NULL;
467
468  strncpy(node, dp->n_name, 15);
469  node[15] = 0;
470 }
471
472 return node;
473#else
474 return NULL;
475#endif
476}
477
[0]478int roar_socket_open (int mode, int type, char * host, int port) {
479// int type = ROAR_SOCKET_TYPE_INET;
480 int fh;
[531]481#ifdef ROAR_HAVE_IPX
482#define _NEED_OBJ
[5184]483#endif
484#if defined(ROAR_HAVE_IPX) || defined(ROAR_HAVE_GETADDRINFO)
[531]485 int ret;
486#endif
[3788]487#ifdef ROAR_HAVE_IPX
488 unsigned int ipx_port;
489#endif
[3643]490#ifdef ROAR_HAVE_UNIX
491 int abstract = 0;
492#endif
[1476]493#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6) || defined(ROAR_HAVE_UNIX) || defined(ROAR_HAVE_IPX)
[512]494 union {
[1359]495  struct sockaddr     sa;
[1361]496#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6)
[512]497  struct sockaddr_in  in;
[1359]498#endif
[1090]499#ifdef ROAR_HAVE_UNIX
[512]500  struct sockaddr_un  un;
[1090]501#endif
[890]502#ifdef ROAR_HAVE_IPV6
[514]503  struct sockaddr_in6 in6;
[890]504#endif
[531]505#ifdef ROAR_HAVE_IPX
506  struct sockaddr_ipx ipx;
507#endif
[512]508 } socket_addr;
[4759]509 socklen_t addrlen;
[1476]510#endif
[1359]511#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6)
[0]512 struct hostent     * he;
[1359]513#endif
[0]514 //unsigned int host_div = 0;
[3788]515#ifdef ROAR_TARGET_WIN32
516 int PASCAL (*mode_func)(SOCKET,const struct sockaddr*,int) = connect; // default is to connect
517#else
[0]518 int (*mode_func)(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen) = connect; // default is to connect
[3788]519#endif
[501]520#ifdef ROAR_HAVE_LIBDNET
[531]521#define _NEED_OBJ
522#endif
523#ifdef _NEED_OBJ
[501]524 char obj[80];
525 char * del;
526#endif
[4759]527 int af_guessed = 0;
528#ifdef ROAR_HAVE_GETADDRINFO
529 struct addrinfo hints, *res = NULL;
530 char port_as_string[32];
531#endif
[5098]532#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6)
533 int is_numerical = 1;
534 const char * numptr;
535#endif
[0]536
[4692]537 ROAR_DBG("roar_socket_open(mode=%i, type=%i, host='%s', port=%i) = ?", mode, type, host, port);
538
[5184]539 roar_err_set(ROAR_ERROR_UNKNOWN);
540
[0]541 if ( mode == MODE_LISTEN )
542  mode_func = bind;
543
544 if ( type == ROAR_SOCKET_TYPE_UNKNOWN ) {
[4759]545  af_guessed = 1;
[0]546  type = ROAR_SOCKET_TYPE_INET;
[69]547  if ( *host == '/' ) {
[0]548   type = ROAR_SOCKET_TYPE_UNIX;
[69]549  } else if ( strcmp(host, "+fork") == 0 ) {
550   type = ROAR_SOCKET_TYPE_FORK;
[3643]551  } else if ( strcmp(host, "+abstract") == 0 ) {
552   type = ROAR_SOCKET_TYPE_UNIX;
[3785]553#ifdef ROAR_HAVE_UNIX
[3643]554   abstract = 1;
[3785]555#endif
[501]556  } else if ( strstr(host, "::") != NULL ) {
557   type = ROAR_SOCKET_TYPE_DECNET;
[531]558  } else if ( host[strlen(host)-1] == ')' ) {
559   type = ROAR_SOCKET_TYPE_IPX;
[69]560  }
[0]561 }
562
563
[4692]564 ROAR_DBG("roar_socket_open(mode=%i, type=%i, host='%s', port=%i) = ?", mode, type, host, port);
565
[0]566 ROAR_DBG("roar_socket_open(*): type=%s, host='%s', port=%i",
[3956]567             type == ROAR_SOCKET_TYPE_UNIX ? "UNIX" : "???", host, port);
[0]568
[501]569 if ( type == ROAR_SOCKET_TYPE_DECNET ) {
570#ifdef ROAR_HAVE_LIBDNET
[508]571   ROAR_DBG("roar_socket_open(*): hostname for DECnet: host(%p)=%s", host, host);
[501]572   del = strstr(host, "::");
[508]573   ROAR_DBG("roar_socket_open(*): hostname for DECnet: del(%p)=%s", del, del);
574
575   if ( del == NULL ) {
576    ROAR_WARN("roar_socket_open(*): invalid hostname for DECnet: %s", host);
[5184]577    roar_err_set(ROAR_ERROR_INVAL);
[508]578    return -1;
579   }
580
[501]581   *del = 0;
582
583   if ( *(del+2) == '#' ) { // assume we have node::#num
584    port = atoi(del+2);
585   }
586
587   if ( port ) {
[1067]588    snprintf(obj, 7, "%i", port); // no need for snprintf() as dec(port) is smaller than obj[]
[501]589   } else {
590    *obj = 0;
591    strncat(obj, del+2, 79);
592   }
593
[5137]594  ROAR_DBG("roar_socket_open(*): obj='%s', port=%i", obj, port);
595
[508]596  if ( mode == MODE_LISTEN ) {
597   fh = roar_socket_listen_decnet(obj, port);
598   *del = ':';
599   return fh;
600  } else {
601   // There is nothing wrong in this case to use dnet_conn() so we do.
[5137]602   ROAR_DBG("roar_socket_open(*): CALL dnet_conn('%s', '%s', SOCK_STREAM, 0 ,0 ,0 , 0)", host, obj);
[501]603   fh = dnet_conn(host, obj, SOCK_STREAM, 0 ,0 ,0 , 0);
[5137]604   ROAR_DBG("roar_socket_open(*): RET %i", fh);
[501]605   *del = ':';
606   return fh;
[533]607  }
[501]608#else
[5184]609  roar_err_set(ROAR_ERROR_AFNOTSUP);
[533]610  return -1; // no decnet support
[501]611#endif
612 }
613
[1359]614#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6)
[512]615 memset(&socket_addr,    0, sizeof(socket_addr));
[60]616 memset(&he,             0, sizeof(he));               // FIXME: we have a valid pointer in here????
[1359]617#endif
[0]618
619
[520]620 if ( type == ROAR_SOCKET_TYPE_INET || type == ROAR_SOCKET_TYPE_INET6 ) {
[1359]621#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6)
[0]622
[3956]623  ROAR_DBG("roar_socket_open(*): type=INET|INET6, host='%s', port=%i", host, port);
[2039]624
[1768]625  roar_socket_win32_init(); // we need to do this early as gethostbyname() requires this.
626
[3956]627  ROAR_DBG("roar_socket_open(*): type=INET|INET6, host='%s', port=%i", host, port);
628
[5098]629  if ( !!strcmp(host, "0.0.0.0") ) {
630   for (numptr = host; is_numerical && *numptr != 0; numptr++)
631    if ( ! ((*numptr >= '0' && *numptr <= '9') || *numptr == '.')  )
632     is_numerical = 0;
633
[5138]634   if ( is_numerical && roar_libroar_iswarn(NULL) ) {
[5098]635    ROAR_WARN("roar_socket_open(*): Hostname \"%s\" is numerical. Do not use IP addresses directly. Use Hostnames.", host);
636   }
637  }
638
[4759]639#ifdef ROAR_HAVE_GETADDRINFO
640  memset(&hints, 0, sizeof(hints));
641  hints.ai_socktype = SOCK_STREAM;
642  if ( af_guessed ) {
643   hints.ai_family   = AF_UNSPEC;
644  } else {
645   hints.ai_family   = type == ROAR_SOCKET_TYPE_INET ? AF_INET : AF_INET6;
646  }
647
648  snprintf(port_as_string, sizeof(port_as_string), "%i", port);
649
[5184]650  ret = getaddrinfo(host, port_as_string, &hints, &res);
651  switch (ret) {
652   case 0: /* no error */; break;
[5203]653#ifdef EAI_ADDRFAMILY
[5184]654   case EAI_ADDRFAMILY: roar_err_set(ROAR_ERROR_NOENT);    break;
[5203]655#endif
[5184]656   case EAI_AGAIN:      roar_err_set(ROAR_ERROR_AGAIN);    break;
657   case EAI_BADFLAGS:   roar_err_set(ROAR_ERROR_INVAL);    break;
658   case EAI_FAIL:       roar_err_set(ROAR_ERROR_RIO);      break;
659   case EAI_FAMILY:     roar_err_set(ROAR_ERROR_AFNOTSUP); break;
660   case EAI_MEMORY:     roar_err_set(ROAR_ERROR_NOMEM);    break;
[5203]661#ifdef EAI_NODATA
[5184]662   case EAI_NODATA:     roar_err_set(ROAR_ERROR_NODATA);   break;
[5203]663#endif
[5184]664   case EAI_NONAME:     roar_err_set(ROAR_ERROR_NOENT);    break;
665   case EAI_SERVICE:    roar_err_set(ROAR_ERROR_PROTONOSUP); break;
666   case EAI_SOCKTYPE:   roar_err_set(ROAR_ERROR_INVAL);    break;
667   case EAI_SYSTEM:     roar_err_from_errno();             break;
668   default:
669     roar_err_set(ROAR_ERROR_UNKNOWN);
670    break;
671  }
672  if ( ret != 0 )
[4759]673   return -1;
674
675  if ( af_guessed ) {
676   type = res->ai_family == AF_INET ? ROAR_SOCKET_TYPE_INET : ROAR_SOCKET_TYPE_INET6;
677  }
678
679  if ( type == ROAR_SOCKET_TYPE_INET ) {
[5248]680   fh = roar_socket_new(ROAR_SOCKET_TYPE_TCP);
[4759]681  } else {
[5248]682   fh = roar_socket_new(ROAR_SOCKET_TYPE_TCP6);
[4759]683  }
684
685  memcpy(&(socket_addr.sa), res->ai_addr, res->ai_addrlen);
686  addrlen = res->ai_addrlen;
687
688  if ( res != NULL )
689   freeaddrinfo(res);
690#else
[0]691  if ( (he = gethostbyname(host)) == NULL ) {
692   ROAR_ERR("roar_socket_open(*): Can\'t resolve host name '%s'",
693                     host);
[5184]694   roar_err_from_errno();
[0]695   return -1;
696  }
697
[4692]698   ROAR_DBG("roar_socket_open(*): he=%p", he);
699
[4759]700   memcpy((struct in_addr *)&(socket_addr.in.sin_addr), he->h_addr, sizeof(struct in_addr));
[0]701
[520]702   /* set the connect information */
703   socket_addr.in.sin_family = AF_INET;
704   socket_addr.in.sin_port   = ROAR_HOST2NET16(port);
705
[5248]706  fh = roar_socket_new(ROAR_SOCKET_TYPE_TCP);
[4759]707  addrlen = sizeof(struct sockaddr_in);
708#endif
[2039]709
[4692]710  ROAR_DBG("roar_socket_open(*): fh=%i", fh);
711
[2039]712  if ( fh == -1 ) {
713   ROAR_ERR("roar_socket_open(*): Can\'t create TCP socket: %s", strerror(errno));
714   return -1;
715  }
716
717  ROAR_DBG("roar_socket_open(*) = ?");
[0]718
[4692]719  ROAR_DBG("roar_socket_open(*): fh=%i", fh);
720
[4759]721   if ( mode_func(fh, (struct sockaddr *)&socket_addr.sa, addrlen) == -1 ) {
[520]722    ROAR_DBG("roar_socket_open(*): Can not connect/bind: %s", strerror(errno));
[5184]723    roar_err_from_errno();
[520]724    close(fh);
725    return -1;
726   }
[4692]727
728  ROAR_DBG("roar_socket_open(*): fh=%i", fh);
729
[0]730  // hey! we have a socket...
[2040]731  ROAR_DBG("roar_socket_open(*) = ? // we have a socket :)");
[1359]732#else
[4692]733  ROAR_DBG("roar_socket_open(*) = -1 // no IPv4 or IPv6 support");
[5184]734  roar_err_set(ROAR_ERROR_AFNOTSUP);
[1359]735  return -1;
736#endif
[69]737 } else if ( type == ROAR_SOCKET_TYPE_UNIX ) {
[1090]738#ifdef ROAR_HAVE_UNIX
[512]739  socket_addr.un.sun_family = AF_UNIX;
[3643]740
741  if ( abstract ) {
742   memset(socket_addr.un.sun_path, 0, sizeof(socket_addr.un.sun_path));
743   snprintf(socket_addr.un.sun_path+1, sizeof(socket_addr.un.sun_path)-1, "RoarAudio/UNIX/Abstract/%i", abstract);
744  } else {
745   strncpy(socket_addr.un.sun_path, host, sizeof(socket_addr.un.sun_path) - 1);
746  }
[60]747
[5248]748  fh = roar_socket_new(ROAR_SOCKET_TYPE_UNIX);
[60]749
[512]750  if ( mode_func(fh, (struct sockaddr *)&socket_addr.un, sizeof(struct sockaddr_un)) == -1 ) {
[60]751   ROAR_DBG("roar_socket_open(*): Can not connect/bind: %s", strerror(errno));
[5184]752   roar_err_from_errno();
[60]753   close(fh);
754   return -1;
755  }
[1090]756#else
757  ROAR_ERR("roar_socket_open(*): There is no UNIX Domain Socket support in win32, download a real OS.");
[5184]758  roar_err_set(ROAR_ERROR_AFNOTSUP);
[1090]759  return -1;
760#endif
[531]761 } else if ( type == ROAR_SOCKET_TYPE_IPX ) {
[534]762#ifdef ROAR_HAVE_IPX
[531]763  socket_addr.ipx.sipx_family = AF_IPX;
764
765  obj[0] = 0;
766
[3788]767  if ( (ret = sscanf(host, "%8x.%12s(%x)", &socket_addr.ipx.sipx_network, obj, &ipx_port)) < 2 ) {
[531]768   return -1;
[3788]769   socket_addr.ipx.sipx_port = ipx_port;
[531]770  } else if ( ret == 2 ) {
771   socket_addr.ipx.sipx_port = port; // Network Byte Order?
772  }
773
774  memset(socket_addr.ipx.sipx_node, 0, IPX_NODE_LEN);
775  ret = strlen(obj);
776
777  if ( ret % 2 )  // needs to be even at the moment
778   return -1;
779
[5248]780  fh = roar_socket_new(ROAR_SOCKET_TYPE_IPX);
[531]781
782  close(fh);
[5248]783  roar_err_set(ROAR_ERROR_AFNOTSUP);
[531]784  return -1;
[534]785#else
[5184]786  roar_err_set(ROAR_ERROR_AFNOTSUP);
[534]787  return -1;
788#endif
[69]789 } else if ( type == ROAR_SOCKET_TYPE_FORK ) {
790  return roar_socket_open_fork(mode, host, port);
[75]791 } else if ( type == ROAR_SOCKET_TYPE_FILE ) {
792  return roar_socket_open_file(mode, host, port);
[69]793 } else {
[5184]794  roar_err_set(ROAR_ERROR_AFNOTSUP);
[69]795  return -1;
[0]796 }
797
[1476]798 if ( mode == MODE_LISTEN ) {
[2040]799#if defined(ROAR_HAVE_BSDSOCKETS) || defined(ROAR_TARGET_WIN32)
[0]800  if ( listen(fh, ROAR_SOCKET_QUEUE_LEN) == -1 ) {
[5184]801   roar_err_from_errno();
[0]802   close(fh);
803   return -1;
804  }
[1476]805#else
[5184]806  roar_err_set(ROAR_ERROR_NOSYS);
[1476]807  return -1;
808#endif
809 }
[0]810
811 return fh;
812}
813
[69]814int roar_socket_open_fork  (int mode, char * host, int port) {
[1440]815#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
[3628]816 char * daemonimage;
[69]817 int socks[2];
818 int r;
819 char fhstr[8];
820
821 if ( mode == MODE_LISTEN )
822  return -1;
823
[3628]824 // TODO: FIXME: we should move this into the config structure.
825 daemonimage = getenv("ROAR_DAEMONIMAGE");
826
827 if ( daemonimage == NULL || *daemonimage == 0 )
828  daemonimage = "roard";
829
[69]830 if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) {
831  return -1;
832 }
833
834 r = fork();
835
836 if ( r == -1 ) { // error!
837  ROAR_ERR("roar_socket_open_fork(*): Can not fork: %s", strerror(errno));
838  close(socks[0]);
839  close(socks[1]);
840  return -1;
841 } else if ( r == 0 ) { // we are the child
842  close(socks[0]);
843
[70]844  close(ROAR_STDIN ); // we do not want roard to have any standard input
845  close(ROAR_STDOUT); // STDOUT is also not needed, so we close it,
846                      // but STDERR we keep open for error messages.
847
[69]848  snprintf(fhstr, 7, "%i", socks[1]);
849
[3628]850  execlp(daemonimage, daemonimage, "--no-listen", "--client-fh", fhstr, (char*)NULL);
[69]851
852  // we are still alive?
853  ROAR_ERR("roar_socket_open_fork(*): alive after exec(), that's bad!");
854  _exit(1);
855 } else { // we are the parent
856  close(socks[1]);
857  return socks[0];
858 }
859
860 return -1;
[1093]861#else
862 ROAR_ERR("roar_socket_open_fork(*): There is no UNIX Domain Socket support in win32, download a real OS.");
863 return -1;
864#endif
[69]865}
866
[75]867int roar_socket_open_file  (int mode, char * host, int port) {
[1476]868#ifdef ROAR_HAVE_IO_POSIX
[75]869 int fh;
870
871 if ( mode == MODE_LISTEN )
872  return -1;
873
874 if ( (fh = open(host, O_RDONLY, 0644)) == -1 ) {
875  ROAR_ERR("roar_socket_open_file(*): Can not open file %s: %s", host, strerror(errno));
876 }
877
878 return fh;
[1476]879#else
880 return -1;
881#endif
[75]882}
883
[2]884// --- [ PROXY CODE ] ---
885
[3901]886#ifndef ROAR_HAVE_IO_POSIX
887#ifdef  ROAR_SUPPORT_PROXY
888#undef  ROAR_SUPPORT_PROXY
889#endif
890#endif
891
[2]892// generic proxy code:
893
[1087]894#ifdef ROAR_SUPPORT_PROXY
[2]895int roar_socket_open_proxy (int mode, int type, char * host, int port, char * proxy_type) {
[829]896 int    proxy_port = -1;
[2]897 char   proxy_host[ROAR_SOCKET_MAX_HOSTNAMELEN];
[829]898 char * proxy_addr = NULL;
[2]899 int    i;
[829]900 int    fh = -1;
[835]901 char * user = NULL, * pw = NULL, * opts = NULL;
[836]902 char * sep;
[838]903 int    no_fh = 0;
[1008]904 char   proxy_addr_buf[1024];
[835]905 static struct passwd * passwd;
[834]906 int (* code)(int mode, int fh, char * host, int port, char * user, char * pw, char * opts) = NULL;
[2]907
[835]908 if ( passwd == NULL ) {
909  passwd = getpwuid(getuid());
910 }
911
[3897]912// TODO: fix this in a good way
913#ifndef ROAR_TARGET_MICROCONTROLLER
[835]914 if ( passwd != NULL )
915  user = passwd->pw_name;
[3897]916#endif
[835]917
918 if ( user == NULL )
919  user = getenv("USER");
920
[2]921 // TODO: change this so we support listen() proxys (ssh -R)
922 if ( mode != MODE_CONNECT )
923  return -1;
924
[829]925 if ( !strncmp(proxy_type, "socks", 5) ) {
[2]926  proxy_addr = getenv("socks_proxy");
927
928  proxy_port = 9050; // TOR's default port
[832]929 } else if ( !strcmp(proxy_type, "http") || !strcmp(proxy_type, "https") ) {
[830]930  proxy_port = 8080;
[2]931
[832]932  if ( (proxy_addr = getenv("http_proxy")) == NULL )
933   proxy_addr = getenv("https_proxy");
934
[2]935  if ( proxy_addr == NULL )
936   return -1;
937
[830]938  if ( !strncmp(proxy_addr, "http://", 7) )
939   proxy_addr += 7;
[838]940 } else if ( !strncmp(proxy_type, "ssh", 3) ) {
941  proxy_port = 22;
942  proxy_addr = getenv("ssh_proxy");
943  no_fh      = 1;
[830]944 }
[2]945
[1008]946 proxy_addr_buf[1023] = 0;
947 strncpy(proxy_addr_buf, proxy_addr, 1023);
948 proxy_addr = proxy_addr_buf;
949
[837]950 if ( (sep = strstr(proxy_type, "/")) != NULL )
951  opts = sep+1;
952
[830]953 if ( proxy_addr == NULL )
954  return -1;
[2]955
[836]956 if ( (sep = strstr(proxy_addr, "@")) != NULL ) {
957  *sep = 0;
958  user = proxy_addr;
959  proxy_addr = sep+1;
960
961  if ( (sep = strstr(user, ":")) != NULL ) {
962   *sep = 0;
963   pw = sep+1;
964  }
965 }
966
[1008]967 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);
968
[4028]969 for (i = 0; proxy_addr[i] != 0 && proxy_addr[i] != ':' && i < (ROAR_SOCKET_MAX_HOSTNAMELEN - 1); i++)
[830]970  proxy_host[i] = proxy_addr[i];
971 proxy_host[i] = 0;
[2]972
[830]973 if ( i == 0 ) // no hostname found
974  return -1;
975
976 if ( proxy_addr[i] == ':' )
977  proxy_port = atoi(&proxy_addr[i+1]);
978
[838]979 if ( ! no_fh ) {
980  if ( (fh = roar_socket_open(mode, type, proxy_host, proxy_port)) == -1) {
981   return -1;
982  }
[829]983 }
[2]984
[829]985 if ( !strcmp(proxy_type, "socks4a") ) { // for TOR, the only supported type at the moment
[833]986  code = roar_socket_open_socks4a;
987 } else if ( !strcmp(proxy_type, "socks4d") ) { // DECnet
988  code = roar_socket_open_socks4d;
989 } else if ( !strcmp(proxy_type, "socks4") ) { // good old SOCKS4
990  code = roar_socket_open_socks4;
991 } else if ( !strcmp(proxy_type, "http") ) { // HTTP CONNECT
992  code = roar_socket_open_http;
[839]993 } else if ( !strncmp(proxy_type, "ssh", 3) ) { // SSH...
[1063]994#ifdef ROAR_HAVE_BIN_SSH
[839]995  code = roar_socket_open_ssh;
[1063]996#else
997  ROAR_ERR("roar_socket_open_proxy(*): No SSH support compiled in");
998#endif
[833]999 } else {
1000  return -1; // unknown type
1001 }
[2]1002
[833]1003 if ( code != NULL ) {
[838]1004  if ( no_fh ) {
1005   fh = code(mode, fh, host, port, user, pw, opts);
1006  } else {
1007   if ( code(mode, fh, host, port, user, pw, opts) == -1 ) {
1008    close(fh);
1009    return -1;
1010   }
[829]1011  }
1012
1013  return fh;
[833]1014 }
[829]1015
[833]1016 close(fh);
1017 return -1;
[2]1018}
1019
1020// protocoll dependet proxy code:
1021
[834]1022int roar_socket_open_socks4 (int mode, int fh, char * host, int port, char * user, char * pw, char * opts) {
[3897]1023#ifndef ROAR_TARGET_MICROCONTROLLER
[829]1024 struct hostent     * he;
1025
1026 if ( (he = gethostbyname(host)) == NULL ) {
1027  ROAR_ERR("roar_socket_open_socks4(*): Can\'t resolve host name '%s'", host);
1028  return -1;
1029 }
1030
[834]1031 return roar_socket_open_socks4x(mode, fh, he->h_addr, port, NULL, 0, user);
[3897]1032#else
1033 return -1;
1034#endif
[829]1035}
1036
[834]1037int roar_socket_open_socks4a(int mode, int fh, char * host, int port, char * user, char * pw, char * opts) {
1038 return roar_socket_open_socks4x(mode, fh, "\0\0\0\1", port, host, strlen(host)+1, user);
[829]1039}
1040
[834]1041int roar_socket_open_socks4d(int mode, int fh, char * host, int port, char * user, char * pw, char * opts) {
[829]1042 size_t len = strlen(host)+1;
1043 char * dp;
1044
1045 if ( port == 0 ) {
1046  if ( (dp = strstr(host, "::")) == NULL )
1047   return -1;
1048
1049  len--;
1050  *dp = 0;
1051  memmove(dp+1, dp+2, len - (dp-host) - 1);
1052 }
1053
[834]1054 return roar_socket_open_socks4x(mode, fh, "\0\2\0\0", port, host, len, user);
[829]1055}
1056
[834]1057int roar_socket_open_socks4x(int mode, int fh, char host[4], int port, char * app, size_t app_len, char * user) {
[2]1058 char buf[9];
[835]1059 int len;
[2]1060
1061 buf[0] = 0x04;
1062 buf[1] = mode == MODE_CONNECT ? 0x01 : 0x02;
1063 *((uint16_t*)&buf[2]) = htons(port);
[829]1064 memcpy(buf+4, host, 4);
[835]1065
1066 if ( user == NULL ) {
1067  buf[8] = 0x00;
1068  len = 9;
1069 } else {
1070  len = 8;
1071 }
[2]1072
[835]1073 if ( write(fh, buf, len) != len )
[2]1074  return -1;
1075
[835]1076 if ( user != NULL ) {
1077  len = strlen(user) + 1;
1078  if ( write(fh, user, len) != len )
1079   return -1;
1080 }
1081
[829]1082 if ( app_len > 0 )
1083  if ( write(fh, app, app_len) != app_len )
1084   return -1;
[2]1085
1086 if ( read(fh, buf, 8) != 8 )
1087  return -1;
1088
1089 if ( buf[1] != 0x5a )
1090  return -1;
1091
1092 return 0;
1093}
1094
[834]1095int roar_socket_open_http   (int mode, int fh, char * host, int port, char * user, char * pw, char * opts) {
[831]1096 char buf[1024];
1097 int len;
1098
1099 if ( port == 0 || host == NULL )
1100  return -1;
1101
1102 if ( *host == '/' ) // AF_UNIX
1103  return -1;
1104
1105 if ( (len = snprintf(buf, 1024, "CONNECT %s:%i HTTP/1.0\r\nUser-Agent: libroar\r\n\r\n", host, port)) == -1 )
1106  return -1;
1107
1108 if ( write(fh, buf, len) != len )
1109  return -1;
1110
1111 while ( (len = read(fh, buf, 1024)) ) {
1112  if ( len == 1024 ) { // overlong lion
1113   return -1;
1114  } else if ( len == 2 && buf[0] == '\r' && buf[1] == '\n' ) {
1115   break;
1116  } else if ( len == 1 && (buf[0] == '\r' || buf[0] == '\n') ) { // bad proxy or devel trying to debug ;)
1117   break;
1118  } else if ( len >= 4 && buf[len-4] == '\r' && buf[len-3] == '\n' && buf[len-2] == '\r' && buf[len-1] == '\n' ) {
1119   break;
1120  }
1121 }
1122
1123 return 0;
[830]1124}
1125
[839]1126
[1063]1127#ifdef ROAR_HAVE_BIN_SSH
[839]1128int roar_socket_open_ssh    (int mode, int fh, char * host, int port, char * user, char * pw, char * opts) {
1129 char * proxy_addr = getenv("ssh_proxy");
1130 char * sep;
1131 char   cmd[1024] = {0}, rcmd[1024] = {0};
1132 int    proxy_port = 22;
[840]1133 int    use_socat = 0;
[839]1134 int r;
1135 int socks[2];
1136
1137 if ( host == NULL )
1138  return -1;
1139
1140 if ( *host == '/' )
[840]1141  use_socat = 1;
[839]1142
1143 if ( mode == MODE_LISTEN )
1144  return -1;
1145
1146 if ( proxy_addr == NULL )
1147  return -1;
1148
[840]1149 if ( opts != NULL ) {
1150  if ( !strcmp(opts, "socat") ) {
1151   use_socat = 1;
1152  } else if ( !strcmp(opts, "netcat") ) {
1153   use_socat = 0;
1154  } else {
1155   return -1;
1156  }
1157 }
1158
[1008]1159 ROAR_DBG("roar_socket_open_ssh(*): proxy_addr='%s'", proxy_addr);
1160
[839]1161 if ( (sep = strstr(proxy_addr, "@")) != NULL )
1162  proxy_addr = sep+1;
1163
1164 if ( (sep = strstr(proxy_addr, ":")) != NULL ) {
1165  *sep = 0;
1166  proxy_port = atoi(sep+1);
1167 }
1168
1169
1170 if ( !strcmp(host, "+fork") ) {
[1068]1171  strncpy(rcmd, "roard --no-listen --client-fh 0", 32);
[839]1172 } else {
[840]1173  if ( use_socat ) {
1174   if ( *host == '/' ) {
1175    snprintf(rcmd, 1023, "socat stdio unix-connect:\"%s\"", host);
1176   } else {
1177    snprintf(rcmd, 1023, "socat stdio tcp:\"%s\":%i", host, port);
1178   }
1179  } else {
1180   snprintf(rcmd, 1023, "$(which netcat nc 2> /dev/null | grep -v \" \" | head -n 1) \"%s\" %i", host, port);
1181  }
1182
[839]1183  rcmd[1023] = 0;
1184 }
1185
[1008]1186 ROAR_DBG("roar_socket_open_ssh(*): proxy_port=%i, user='%s', proxy_addr='%s'", proxy_port, user, proxy_addr);
[1033]1187 ROAR_DBG("roar_socket_open_ssh(*): rcmd: %s", rcmd);
[1063]1188 snprintf(cmd, 1023, ROAR_HAVE_BIN_SSH " -p %i -l '%s' '%s' '%s'", proxy_port, user, proxy_addr, rcmd);
[839]1189 cmd[1023] = 0;
1190
1191
[3897]1192// TODO: get this more portable!
1193#ifdef AF_UNIX
[839]1194 if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) {
1195  return -1;
1196 }
[3897]1197#else
1198 return -1;
1199#endif
[839]1200
1201 r = fork();
1202
1203 if ( r == -1 ) { // error!
1204  ROAR_ERR("roar_socket_open_ssh(*): Can not fork: %s", strerror(errno));
1205  close(socks[0]);
1206  close(socks[1]);
1207  return -1;
1208 } else if ( r == 0 ) { // we are the child
1209  close(socks[0]);
1210
1211  close(ROAR_STDIN ); // we do not want roard to have any standard input
1212  close(ROAR_STDOUT); // STDOUT is also not needed, so we close it,
1213                      // but STDERR we keep open for error messages.
1214
1215  dup2(socks[1], 0);
1216  dup2(socks[1], 1);
1217
[1652]1218  execlp("sh", "sh", "-c", cmd, (char*)NULL);
[839]1219
1220  // we are still alive?
1221  ROAR_ERR("roar_socket_open_ssh(*): alive after exec(), that's bad!");
1222  _exit(1);
1223 } else { // we are the parent
1224  close(socks[1]);
1225  return socks[0];
1226 }
1227 return -1;
1228}
[1063]1229#endif
[839]1230
[1087]1231#endif // ROAR_SUPPORT_PROXY
1232
[0]1233//ll
Note: See TracBrowser for help on using the repository browser.