source: roaraudio/libroar/socket.c @ 5231:8b30ddb689b8

Last change on this file since 5231:8b30ddb689b8 was 5210:8eb738dee9d4, checked in by phi, 13 years ago

Updated ports to minimal, win32 and avr (pr2)

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