source: roaraudio/libroar/socket.c @ 5392:cb1666507792

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

only open sockets if we have BSD socket interface

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