source: roaraudio/libroar/socket.c @ 3722:fb5a5cdc484a

Last change on this file since 3722:fb5a5cdc484a was 3643:e6200d571bb8, checked in by phi, 14 years ago

added support for abstract name space

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