source: roaraudio/libroar/socket.c @ 5109:4f9fc788fe91

Last change on this file since 5109:4f9fc788fe91 was 5109:4f9fc788fe91, checked in by phi, 13 years ago

Started to use compiler attributes (Also see: #130)

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