source: roaraudio/libroar/socket.c @ 5951:b0e1eff45456

Last change on this file since 5951:b0e1eff45456 was 5951:b0e1eff45456, checked in by phi, 11 years ago

more header cleanup: cleanup of #includes

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