source: roaraudio/libroar/socket.c @ 5376:c8f94c6ad2a3

Last change on this file since 5376:c8f94c6ad2a3 was 5376:c8f94c6ad2a3, checked in by phi, 12 years ago

finished rewrite of socket.[ch] for this release. Closes: #187

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