source: roaraudio/libroar/socket.c @ 6052:d48765b2475e

Last change on this file since 6052:d48765b2475e was 6052:d48765b2475e, checked in by phi, 9 years ago

updated copyright headers

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