source: roaraudio/libroar/socket.c @ 5311:778a3e7b2b66

Last change on this file since 5311:778a3e7b2b66 was 5270:e25346c13638, checked in by phi, 12 years ago

fixed some gcc -Wextra warnings

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