source: roaraudio/libroar/socket.c @ 5373:8da157c10483

Last change on this file since 5373:8da157c10483 was 5373:8da157c10483, checked in by phi, 12 years ago
  • Updated config structure
  • Added a flag to ask libroar to only connect to local servers
  • Added a way to override fork()
  • added support for +internal
File size: 30.8 KB
Line 
1//socket.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
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
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
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
36#define _LIBROAR_NOATTR_TO_STATIC /* ignore warnings for TO_STATIC functions */
37#include "libroar.h"
38
39#define MODE_LISTEN  ROAR_SOCKET_MODE_LISTEN
40#define MODE_CONNECT ROAR_SOCKET_MODE_CONNECT
41
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
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
66int roar_socket_recvbuf(int fh, int len) {
67#ifdef ROAR_HAVE_BSDSOCKETS
68 if ( len < 256 ) len = 256;
69
70 return setsockopt(fh, SOL_SOCKET, SO_RCVBUF, (void *)&len, sizeof(len));
71#else
72 return -1;
73#endif
74}
75
76int roar_socket_set_tos(int fh) {
77#if defined(ROAR_HAVE_BSDSOCKETS) && !defined(ROAR_TARGET_WIN32)
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
92#if 0
93static int roar_socket_new_decnet_seqpacket (void) {
94#ifdef ROAR_HAVE_LIBDNET
95 int fh;
96
97 fh = socket(AF_DECnet, SOCK_SEQPACKET, DNPROTO_NSP);
98
99 roar_socket_decnet_set_timeout(fh, 300, 0);
100
101 return fh;
102#else
103 return -1;
104#endif
105}
106#endif
107
108
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;
117 int fh;
118#if defined(ROAR_HAVE_IPV4) && defined(TCP_NODELAY) && !defined(ROAR_TARGET_WIN32)
119 int t   = 1;
120#endif
121
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 }
172
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 }
205
206 return fh;
207}
208
209
210int roar_socket_nonblock(int fh, int state) {
211#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) && defined(ROAR_HAVE_FCNTL)
212 int flags;
213
214 if ( (flags = fcntl(fh, F_GETFL, 0)) == -1 ) {
215  roar_err_from_errno();
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
221 if ( !(flags & O_NONBLOCK) && state == ROAR_SOCKET_BLOCK )
222  return 0;
223
224 flags |= O_NONBLOCK;
225
226 if ( state == ROAR_SOCKET_BLOCK )
227  flags -= O_NONBLOCK;
228
229 if ( fcntl(fh, F_SETFL, flags) == -1 ) {
230  roar_err_from_errno();
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;
238#else
239 ROAR_WARN("roar_socket_nonblock(*): no nonblocking IO support on win32, use a real OS");
240 return -1;
241#endif
242}
243
244int roar_socket_dup_udp_local_end (int fh) {
245#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) && defined(ROAR_HAVE_FCNTL)
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 ) {
256  roar_err_from_errno();
257  return -1;
258 }
259
260 if ( socket_addr.sin_family != AF_INET ) {
261  roar_err_set(ROAR_ERROR_TYPEMM);
262  return -1;
263 }
264
265 n = roar_socket_new(ROAR_SOCKET_TYPE_UDP);
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 ) {
272  roar_err_from_errno();
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;
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
290}
291
292
293#define _SCMR_CONTROLLEN (sizeof(struct cmsghdr) + sizeof(int))
294int roar_socket_send_fh (int sock, int fh, char * mes, size_t len) {
295#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_OS_SUNOS) && !defined(ROAR_TARGET_OPENVMS)
296 struct iovec     iov[1];
297 struct msghdr    msg;
298 char             cmptr_buf[_SCMR_CONTROLLEN];
299 struct cmsghdr * cmptr = (struct cmsghdr *) cmptr_buf;
300 char             localmes[1] = {0};
301
302 ROAR_DBG("roar_socket_send_fh(sock=%i, fh=%i, mes=%p, len=%u) = ?", sock, fh, mes, len);
303
304 if ( sock < 0 || fh < 0 )
305  return -1;
306
307 if ( len == 0 ) {
308  len = 1;
309  mes = localmes;
310 }
311
312 memset(cmptr, 0, _SCMR_CONTROLLEN);
313
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);
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
333}
334
335int roar_socket_recv_fh (int sock,         char * mes, size_t * len) {
336#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_OS_SUNOS) && !defined(ROAR_TARGET_OPENVMS)
337 struct iovec     iov[1];
338 struct msghdr    msg;
339 char             cmptr_buf[_SCMR_CONTROLLEN];
340 struct cmsghdr * cmptr = (struct cmsghdr *) cmptr_buf;
341 char             localmes[1];
342 size_t           locallen[1] = {1};
343
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);
348  return -1;
349 }
350
351 if ( len == NULL ) {
352  len = locallen;
353  mes = localmes;
354 }
355
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
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);
368  return -1;
369 }
370
371 if ( msg.msg_controllen  < _SCMR_CONTROLLEN ||
372      cmptr->cmsg_len    != _SCMR_CONTROLLEN  ) {
373  ROAR_DBG("roar_socket_recv_fh(sock=%i, mes=%p, len=%p) = -1 // control len is wrong", sock, mes, len);
374  return -1;
375 }
376
377 ROAR_DBG("roar_socket_recv_fh(sock=%i, mes=%p, len=%p) = %i", sock, mes, len, *(int *)CMSG_DATA(cmptr));
378 return *(int *)CMSG_DATA(cmptr);
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
383}
384
385int roar_socket_listen  (int type, const char * host, int port) {
386 return roar_socket_open(MODE_LISTEN, type, host, port);
387}
388
389int roar_socket_connect (const char * host, int port) {
390 char * proxy_type = getenv("ROAR_PROXY");
391
392 ROAR_DBG("roar_socket_connect(host='%s', port=%i) = ?", host, port);
393
394 if ( proxy_type == NULL || strcmp(proxy_type, "") == 0 ) {
395  return roar_socket_open(MODE_CONNECT, ROAR_SOCKET_TYPE_UNKNOWN, host, port);
396 } else {
397#ifdef ROAR_SUPPORT_PROXY
398  return roar_socket_open_proxy(MODE_CONNECT, ROAR_SOCKET_TYPE_UNKNOWN, host, port, proxy_type);
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
403 }
404}
405
406
407int roar_socket_listen_decnet (const char * object, int num) {
408#ifdef ROAR_HAVE_LIBDNET
409 int fh = roar_socket_new(ROAR_SOCKET_TYPE_DECNET);
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 {
433  bind_sockaddr.sdn_objnamel  = ROAR_HOST2LE16(strlen(object));
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);
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
455const char * roar_socket_get_local_nodename(void) {
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
465  if ( (dp=getnodebyaddr((char*)binaddr->a_addr, binaddr->a_len, AF_DECnet)) == NULL )
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
478int roar_socket_open (int mode, int type, const char * host, int port) {
479// int type = ROAR_SOCKET_TYPE_INET;
480 int fh;
481#ifdef ROAR_HAVE_IPX
482#define _NEED_OBJ
483#endif
484#if defined(ROAR_HAVE_IPX) || defined(ROAR_HAVE_GETADDRINFO)
485 int ret;
486#endif
487#ifdef ROAR_HAVE_IPX
488 unsigned int ipx_port;
489#endif
490#ifdef ROAR_HAVE_UNIX
491 int abstract = 0;
492#endif
493#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6) || defined(ROAR_HAVE_UNIX) || defined(ROAR_HAVE_IPX)
494 union {
495  struct sockaddr     sa;
496#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6)
497  struct sockaddr_in  in;
498#endif
499#ifdef ROAR_HAVE_UNIX
500  struct sockaddr_un  un;
501#endif
502#ifdef ROAR_HAVE_IPV6
503  struct sockaddr_in6 in6;
504#endif
505#ifdef ROAR_HAVE_IPX
506  struct sockaddr_ipx ipx;
507#endif
508 } socket_addr;
509 socklen_t addrlen;
510#endif
511#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6)
512 struct hostent     * he;
513#endif
514 //unsigned int host_div = 0;
515#ifdef ROAR_TARGET_WIN32
516 int PASCAL (*mode_func)(SOCKET,const struct sockaddr*,int) = connect; // default is to connect
517#else
518 int (*mode_func)(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen) = connect; // default is to connect
519#endif
520#ifdef ROAR_HAVE_LIBDNET
521#define _NEED_OBJ
522 char * dnet_node_buf;
523#endif
524#ifdef _NEED_OBJ
525 char obj[80];
526 char * del;
527#endif
528 int af_guessed = 0;
529#ifdef ROAR_HAVE_GETADDRINFO
530 struct addrinfo hints, *res = NULL;
531 char port_as_string[32];
532#endif
533#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6)
534 int is_numerical = 1;
535 const char * numptr;
536#endif
537
538 ROAR_DBG("roar_socket_open(mode=%i, type=%i, host='%s', port=%i) = ?", mode, type, host, port);
539
540 roar_err_set(ROAR_ERROR_UNKNOWN);
541
542 if ( mode == MODE_LISTEN )
543  mode_func = bind;
544
545 if ( type == ROAR_SOCKET_TYPE_UNKNOWN ) {
546  af_guessed = 1;
547  type = ROAR_SOCKET_TYPE_INET;
548  if ( *host == '/' ) {
549   type = ROAR_SOCKET_TYPE_UNIX;
550  } else if ( strcmp(host, "+fork") == 0 ) {
551   type = ROAR_SOCKET_TYPE_FORK;
552  } else if ( strcmp(host, "+abstract") == 0 ) {
553   type = ROAR_SOCKET_TYPE_UNIX;
554#ifdef ROAR_HAVE_UNIX
555   abstract = 1;
556#endif
557  } else if ( strstr(host, "::") != NULL ) {
558   type = ROAR_SOCKET_TYPE_DECNET;
559  } else if ( host[strlen(host)-1] == ')' ) {
560   type = ROAR_SOCKET_TYPE_IPX;
561  }
562 }
563
564
565 ROAR_DBG("roar_socket_open(mode=%i, type=%i, host='%s', port=%i) = ?", mode, type, host, port);
566
567 ROAR_DBG("roar_socket_open(*): type=%s, host='%s', port=%i",
568             type == ROAR_SOCKET_TYPE_UNIX ? "UNIX" : "???", host, port);
569
570 if ( type == ROAR_SOCKET_TYPE_DECNET ) {
571#ifdef ROAR_HAVE_LIBDNET
572   ROAR_DBG("roar_socket_open(*): hostname for DECnet: host(%p)=%s", host, host);
573   del = strstr(host, "::");
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);
578    roar_err_set(ROAR_ERROR_INVAL);
579    return -1;
580   }
581
582   *del = 0;
583
584   if ( *(del+2) == '#' ) { // assume we have node::#num
585    port = atoi(del+2);
586   }
587
588   if ( port ) {
589    snprintf(obj, 7, "%i", port); // no need for snprintf() as dec(port) is smaller than obj[]
590   } else {
591    *obj = 0;
592    strncat(obj, del+2, 79);
593   }
594
595  ROAR_DBG("roar_socket_open(*): obj='%s', port=%i", obj, port);
596
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.
603   dnet_node_buf = roar_mm_strdup(host);
604   if ( dnet_node_buf == NULL ) {
605    *del = ':';
606    return -1;
607   }
608   ROAR_DBG("roar_socket_open(*): CALL dnet_conn('%s', '%s', SOCK_STREAM, 0 ,0 ,0 , 0)", dnet_node_buf, obj);
609   fh = dnet_conn(dnet_node_buf, obj, SOCK_STREAM, 0, 0, 0, 0);
610   ROAR_DBG("roar_socket_open(*): RET %i", fh);
611   roar_mm_free(dnet_node_buf);
612   *del = ':';
613   return fh;
614  }
615#else
616  roar_err_set(ROAR_ERROR_AFNOTSUP);
617  return -1; // no decnet support
618#endif
619 }
620
621#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6)
622 memset(&socket_addr,    0, sizeof(socket_addr));
623 memset(&he,             0, sizeof(he));               // FIXME: we have a valid pointer in here????
624#endif
625
626
627 if ( type == ROAR_SOCKET_TYPE_INET || type == ROAR_SOCKET_TYPE_INET6 ) {
628#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6)
629
630  ROAR_DBG("roar_socket_open(*): type=INET|INET6, host='%s', port=%i", host, port);
631
632  roar_socket_win32_init(); // we need to do this early as gethostbyname() requires this.
633
634  ROAR_DBG("roar_socket_open(*): type=INET|INET6, host='%s', port=%i", host, port);
635
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
641   if ( is_numerical && roar_libroar_iswarn(NULL) ) {
642    ROAR_WARN("roar_socket_open(*): Hostname \"%s\" is numerical. Do not use IP addresses directly. Use Hostnames.", host);
643   }
644  }
645
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
657  ret = getaddrinfo(host, port_as_string, &hints, &res);
658  switch (ret) {
659   case 0: /* no error */; break;
660#ifdef EAI_ADDRFAMILY
661   case EAI_ADDRFAMILY: roar_err_set(ROAR_ERROR_NOENT);    break;
662#endif
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;
668#ifdef EAI_NODATA
669   case EAI_NODATA:     roar_err_set(ROAR_ERROR_NODATA);   break;
670#endif
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 )
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 ) {
687   fh = roar_socket_new(ROAR_SOCKET_TYPE_TCP);
688  } else {
689   fh = roar_socket_new(ROAR_SOCKET_TYPE_TCP6);
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
698  if ( (he = gethostbyname(host)) == NULL ) {
699   ROAR_ERR("roar_socket_open(*): Can\'t resolve host name '%s'",
700                     host);
701   roar_err_from_errno();
702   return -1;
703  }
704
705   ROAR_DBG("roar_socket_open(*): he=%p", he);
706
707   memcpy((struct in_addr *)&(socket_addr.in.sin_addr), he->h_addr, sizeof(struct in_addr));
708
709   /* set the connect information */
710   socket_addr.in.sin_family = AF_INET;
711   socket_addr.in.sin_port   = ROAR_HOST2NET16(port);
712
713  fh = roar_socket_new(ROAR_SOCKET_TYPE_TCP);
714  addrlen = sizeof(struct sockaddr_in);
715#endif
716
717  ROAR_DBG("roar_socket_open(*): fh=%i", fh);
718
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(*) = ?");
725
726  ROAR_DBG("roar_socket_open(*): fh=%i", fh);
727
728   if ( mode_func(fh, (struct sockaddr *)&socket_addr.sa, addrlen) == -1 ) {
729    ROAR_DBG("roar_socket_open(*): Can not connect/bind: %s", strerror(errno));
730    roar_err_from_errno();
731    close(fh);
732    return -1;
733   }
734
735  ROAR_DBG("roar_socket_open(*): fh=%i", fh);
736
737  // hey! we have a socket...
738  ROAR_DBG("roar_socket_open(*) = ? // we have a socket :)");
739#else
740  ROAR_DBG("roar_socket_open(*) = -1 // no IPv4 or IPv6 support");
741  roar_err_set(ROAR_ERROR_AFNOTSUP);
742  return -1;
743#endif
744 } else if ( type == ROAR_SOCKET_TYPE_UNIX ) {
745#ifdef ROAR_HAVE_UNIX
746  socket_addr.un.sun_family = AF_UNIX;
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  }
754
755  fh = roar_socket_new(ROAR_SOCKET_TYPE_UNIX);
756
757  if ( mode_func(fh, (struct sockaddr *)&socket_addr.un, sizeof(struct sockaddr_un)) == -1 ) {
758   ROAR_DBG("roar_socket_open(*): Can not connect/bind: %s", strerror(errno));
759   roar_err_from_errno();
760   close(fh);
761   return -1;
762  }
763#else
764  ROAR_ERR("roar_socket_open(*): There is no UNIX Domain Socket support in win32, download a real OS.");
765  roar_err_set(ROAR_ERROR_AFNOTSUP);
766  return -1;
767#endif
768 } else if ( type == ROAR_SOCKET_TYPE_IPX ) {
769#ifdef ROAR_HAVE_IPX
770  socket_addr.ipx.sipx_family = AF_IPX;
771
772  obj[0] = 0;
773
774  if ( (ret = sscanf(host, "%8x.%12s(%x)", &socket_addr.ipx.sipx_network, obj, &ipx_port)) < 2 ) {
775   return -1;
776   socket_addr.ipx.sipx_port = ipx_port;
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
787  fh = roar_socket_new(ROAR_SOCKET_TYPE_IPX);
788
789  close(fh);
790  roar_err_set(ROAR_ERROR_AFNOTSUP);
791  return -1;
792#else
793  roar_err_set(ROAR_ERROR_AFNOTSUP);
794  return -1;
795#endif
796 } else if ( type == ROAR_SOCKET_TYPE_FORK ) {
797  roar_err_set(ROAR_ERROR_INVAL);
798  return -1;
799 } else if ( type == ROAR_SOCKET_TYPE_FILE ) {
800  return roar_socket_open_file(mode, host, port);
801 } else {
802  roar_err_set(ROAR_ERROR_AFNOTSUP);
803  return -1;
804 }
805
806 if ( mode == MODE_LISTEN ) {
807#if defined(ROAR_HAVE_BSDSOCKETS) || defined(ROAR_TARGET_WIN32)
808  if ( listen(fh, ROAR_SOCKET_QUEUE_LEN) == -1 ) {
809   roar_err_from_errno();
810   close(fh);
811   return -1;
812  }
813#else
814  roar_err_set(ROAR_ERROR_NOSYS);
815  return -1;
816#endif
817 }
818
819 return fh;
820}
821
822int roar_socket_open_file  (int mode, const char * host, int port) {
823#ifdef ROAR_HAVE_IO_POSIX
824 int fh;
825
826 if ( mode == MODE_LISTEN )
827  return -1;
828
829 if ( (fh = open(host, O_RDONLY, 0644)) == -1 ) {
830  ROAR_ERR("roar_socket_open_file(*): Can not open file %s: %s", host, strerror(errno));
831 }
832
833 return fh;
834#else
835 return -1;
836#endif
837}
838
839// --- [ PROXY CODE ] ---
840
841#ifndef ROAR_HAVE_IO_POSIX
842#ifdef  ROAR_SUPPORT_PROXY
843#undef  ROAR_SUPPORT_PROXY
844#endif
845#endif
846
847// generic proxy code:
848
849#ifdef ROAR_SUPPORT_PROXY
850int roar_socket_open_proxy (int mode, int type, const char * host, int port, const char * proxy_type) {
851 int    proxy_port = -1;
852 char   proxy_host[ROAR_SOCKET_MAX_HOSTNAMELEN];
853 char * proxy_addr = NULL;
854 int    i;
855 int    fh = -1;
856 char * user = NULL, * pw = NULL, * opts = NULL;
857 char * sep;
858 int    no_fh = 0;
859 char   proxy_addr_buf[1024];
860 static struct passwd * passwd;
861 int (* code)(int mode, int fh, const char * host, int port, const char * user, const char * pw, const char * opts) = NULL;
862
863 if ( passwd == NULL ) {
864  passwd = getpwuid(getuid());
865 }
866
867// TODO: fix this in a good way
868#ifndef ROAR_TARGET_MICROCONTROLLER
869 if ( passwd != NULL )
870  user = passwd->pw_name;
871#endif
872
873 if ( user == NULL )
874  user = getenv("USER");
875
876 // TODO: change this so we support listen() proxys (ssh -R)
877 if ( mode != MODE_CONNECT )
878  return -1;
879
880 if ( !strncmp(proxy_type, "socks", 5) ) {
881  proxy_addr = getenv("socks_proxy");
882
883  proxy_port = 9050; // TOR's default port
884 } else if ( !strcmp(proxy_type, "http") || !strcmp(proxy_type, "https") ) {
885  proxy_port = 8080;
886
887  if ( (proxy_addr = getenv("http_proxy")) == NULL )
888   proxy_addr = getenv("https_proxy");
889
890  if ( proxy_addr == NULL )
891   return -1;
892
893  if ( !strncmp(proxy_addr, "http://", 7) )
894   proxy_addr += 7;
895 } else if ( !strncmp(proxy_type, "ssh", 3) ) {
896  proxy_port = 22;
897  proxy_addr = getenv("ssh_proxy");
898  no_fh      = 1;
899 }
900
901 proxy_addr_buf[1023] = 0;
902 strncpy(proxy_addr_buf, proxy_addr, 1023);
903 proxy_addr = proxy_addr_buf;
904
905 if ( (sep = strstr(proxy_type, "/")) != NULL )
906  opts = sep+1;
907
908 if ( proxy_addr == NULL )
909  return -1;
910
911 if ( (sep = strstr(proxy_addr, "@")) != NULL ) {
912  *sep = 0;
913  user = proxy_addr;
914  proxy_addr = sep+1;
915
916  if ( (sep = strstr(user, ":")) != NULL ) {
917   *sep = 0;
918   pw = sep+1;
919  }
920 }
921
922 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);
923
924 for (i = 0; proxy_addr[i] != 0 && proxy_addr[i] != ':' && i < (ROAR_SOCKET_MAX_HOSTNAMELEN - 1); i++)
925  proxy_host[i] = proxy_addr[i];
926 proxy_host[i] = 0;
927
928 if ( i == 0 ) // no hostname found
929  return -1;
930
931 if ( proxy_addr[i] == ':' )
932  proxy_port = atoi(&proxy_addr[i+1]);
933
934 if ( ! no_fh ) {
935  if ( (fh = roar_socket_open(mode, type, proxy_host, proxy_port)) == -1) {
936   return -1;
937  }
938 }
939
940 if ( !strcmp(proxy_type, "socks4a") ) { // for TOR, the only supported type at the moment
941  code = roar_socket_open_socks4a;
942 } else if ( !strcmp(proxy_type, "socks4d") ) { // DECnet
943  code = roar_socket_open_socks4d;
944 } else if ( !strcmp(proxy_type, "socks4") ) { // good old SOCKS4
945  code = roar_socket_open_socks4;
946 } else if ( !strcmp(proxy_type, "http") ) { // HTTP CONNECT
947  code = roar_socket_open_http;
948 } else if ( !strncmp(proxy_type, "ssh", 3) ) { // SSH...
949#ifdef ROAR_HAVE_BIN_SSH
950  code = roar_socket_open_ssh;
951#else
952  ROAR_ERR("roar_socket_open_proxy(*): No SSH support compiled in");
953#endif
954 } else {
955  return -1; // unknown type
956 }
957
958 if ( code != NULL ) {
959  if ( no_fh ) {
960   fh = code(mode, fh, host, port, user, pw, opts);
961  } else {
962   if ( code(mode, fh, host, port, user, pw, opts) == -1 ) {
963    close(fh);
964    return -1;
965   }
966  }
967
968  return fh;
969 }
970
971 close(fh);
972 return -1;
973}
974
975// protocoll dependet proxy code:
976
977int roar_socket_open_socks4 (int mode, int fh, const char * host, int port, const char * user, const char * pw, const char * opts) {
978#ifndef ROAR_TARGET_MICROCONTROLLER
979 struct hostent     * he;
980
981 if ( (he = gethostbyname(host)) == NULL ) {
982  ROAR_ERR("roar_socket_open_socks4(*): Can\'t resolve host name '%s'", host);
983  return -1;
984 }
985
986 return roar_socket_open_socks4x(mode, fh, he->h_addr, port, NULL, 0, user);
987#else
988 return -1;
989#endif
990}
991
992int roar_socket_open_socks4a(int mode, int fh, const char * host, int port, const char * user, const char * pw, const char * opts) {
993 return roar_socket_open_socks4x(mode, fh, "\0\0\0\1", port, host, strlen(host)+1, user);
994}
995
996int roar_socket_open_socks4d(int mode, int fh, const char * host, int port, const char * user, const char * pw, const char * opts) {
997 size_t len = strlen(host)+1;
998 char * dp;
999
1000 if ( port == 0 ) {
1001  if ( (dp = strstr(host, "::")) == NULL )
1002   return -1;
1003
1004  len--;
1005  *dp = 0;
1006  memmove(dp+1, dp+2, len - (dp-host) - 1);
1007 }
1008
1009 return roar_socket_open_socks4x(mode, fh, "\0\2\0\0", port, host, len, user);
1010}
1011
1012int roar_socket_open_socks4x(int mode, int fh, char host[4], int port, const char * app, size_t app_len, const char * user) {
1013 char buf[9];
1014 int len;
1015
1016 buf[0] = 0x04;
1017 buf[1] = mode == MODE_CONNECT ? 0x01 : 0x02;
1018 *((uint16_t*)&buf[2]) = htons(port);
1019 memcpy(buf+4, host, 4);
1020
1021 if ( user == NULL ) {
1022  buf[8] = 0x00;
1023  len = 9;
1024 } else {
1025  len = 8;
1026 }
1027
1028 if ( write(fh, buf, len) != len )
1029  return -1;
1030
1031 if ( user != NULL ) {
1032  len = strlen(user) + 1;
1033  if ( write(fh, user, len) != len )
1034   return -1;
1035 }
1036
1037 if ( app_len > 0 )
1038  if ( write(fh, app, app_len) != (ssize_t)app_len )
1039   return -1;
1040
1041 if ( read(fh, buf, 8) != 8 )
1042  return -1;
1043
1044 if ( buf[1] != 0x5a )
1045  return -1;
1046
1047 return 0;
1048}
1049
1050int roar_socket_open_http   (int mode, int fh, const char * host, int port, const char * user, const char * pw, const char * opts) {
1051 char buf[1024];
1052 int len;
1053
1054 if ( port == 0 || host == NULL )
1055  return -1;
1056
1057 if ( *host == '/' ) // AF_UNIX
1058  return -1;
1059
1060 if ( (len = snprintf(buf, 1024, "CONNECT %s:%i HTTP/1.0\r\nUser-Agent: libroar\r\n\r\n", host, port)) == -1 )
1061  return -1;
1062
1063 if ( write(fh, buf, len) != len )
1064  return -1;
1065
1066 while ( (len = read(fh, buf, 1024)) ) {
1067  if ( len == 1024 ) { // overlong lion
1068   return -1;
1069  } else if ( len == 2 && buf[0] == '\r' && buf[1] == '\n' ) {
1070   break;
1071  } else if ( len == 1 && (buf[0] == '\r' || buf[0] == '\n') ) { // bad proxy or devel trying to debug ;)
1072   break;
1073  } else if ( len >= 4 && buf[len-4] == '\r' && buf[len-3] == '\n' && buf[len-2] == '\r' && buf[len-1] == '\n' ) {
1074   break;
1075  }
1076 }
1077
1078 return 0;
1079}
1080
1081
1082#ifdef ROAR_HAVE_BIN_SSH
1083int roar_socket_open_ssh    (int mode, int fh, const char * host, int port, const char * user, const char * pw, const char * opts) {
1084 char * proxy_addr = getenv("ssh_proxy");
1085 char * sep;
1086 char   cmd[1024] = {0}, rcmd[1024] = {0};
1087 int    proxy_port = 22;
1088 int    use_socat = 0;
1089 int r;
1090 int socks[2];
1091
1092 if ( host == NULL )
1093  return -1;
1094
1095 if ( *host == '/' )
1096  use_socat = 1;
1097
1098 if ( mode == MODE_LISTEN )
1099  return -1;
1100
1101 if ( proxy_addr == NULL )
1102  return -1;
1103
1104 if ( opts != NULL ) {
1105  if ( !strcmp(opts, "socat") ) {
1106   use_socat = 1;
1107  } else if ( !strcmp(opts, "netcat") ) {
1108   use_socat = 0;
1109  } else {
1110   return -1;
1111  }
1112 }
1113
1114 ROAR_DBG("roar_socket_open_ssh(*): proxy_addr='%s'", proxy_addr);
1115
1116 if ( (sep = strstr(proxy_addr, "@")) != NULL )
1117  proxy_addr = sep+1;
1118
1119 if ( (sep = strstr(proxy_addr, ":")) != NULL ) {
1120  *sep = 0;
1121  proxy_port = atoi(sep+1);
1122 }
1123
1124
1125 if ( !strcmp(host, "+fork") ) {
1126  strncpy(rcmd, "roard --no-listen --client-fh 0", 32);
1127 } else {
1128  if ( use_socat ) {
1129   if ( *host == '/' ) {
1130    snprintf(rcmd, 1023, "socat stdio unix-connect:\"%s\"", host);
1131   } else {
1132    snprintf(rcmd, 1023, "socat stdio tcp:\"%s\":%i", host, port);
1133   }
1134  } else {
1135   snprintf(rcmd, 1023, "$(which netcat nc 2> /dev/null | grep -v \" \" | head -n 1) \"%s\" %i", host, port);
1136  }
1137
1138  rcmd[1023] = 0;
1139 }
1140
1141 ROAR_DBG("roar_socket_open_ssh(*): proxy_port=%i, user='%s', proxy_addr='%s'", proxy_port, user, proxy_addr);
1142 ROAR_DBG("roar_socket_open_ssh(*): rcmd: %s", rcmd);
1143 snprintf(cmd, 1023, ROAR_HAVE_BIN_SSH " -p %i -l '%s' '%s' '%s'", proxy_port, user, proxy_addr, rcmd);
1144 cmd[1023] = 0;
1145
1146
1147// TODO: get this more portable!
1148#ifdef AF_UNIX
1149 if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) {
1150  return -1;
1151 }
1152#else
1153 return -1;
1154#endif
1155
1156 r = roar_fork(NULL);
1157
1158 if ( r == -1 ) { // error!
1159  ROAR_ERR("roar_socket_open_ssh(*): Can not fork: %s", strerror(errno));
1160  close(socks[0]);
1161  close(socks[1]);
1162  return -1;
1163 } else if ( r == 0 ) { // we are the child
1164  close(socks[0]);
1165
1166  close(ROAR_STDIN ); // we do not want roard to have any standard input
1167  close(ROAR_STDOUT); // STDOUT is also not needed, so we close it,
1168                      // but STDERR we keep open for error messages.
1169
1170  dup2(socks[1], 0);
1171  dup2(socks[1], 1);
1172
1173  execlp("sh", "sh", "-c", cmd, (_LIBROAR_GOOD_CAST char*)NULL);
1174
1175  // we are still alive?
1176  ROAR_ERR("roar_socket_open_ssh(*): alive after exec(), that's bad!");
1177  _exit(1);
1178 } else { // we are the parent
1179  close(socks[1]);
1180  return socks[0];
1181 }
1182 return -1;
1183}
1184#endif
1185
1186#endif // ROAR_SUPPORT_PROXY
1187
1188//ll
Note: See TracBrowser for help on using the repository browser.