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

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

fixed some gcc -Wextra warnings

File size: 32.1 KB
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 (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   ROAR_DBG("roar_socket_open(*): CALL dnet_conn('%s', '%s', SOCK_STREAM, 0 ,0 ,0 , 0)", host, obj);
604   dnet_node_buf = roar_mm_strdup(host);
605   if ( dnet_node_buf == NULL ) {
606    *del = ':';
607    return -1;
608   }
609   fh = dnet_conn(dnet_node_buf, obj, SOCK_STREAM, 0, 0, 0, 0);
610   roar_mm_free(dnet_node_buf);
611   ROAR_DBG("roar_socket_open(*): RET %i", fh);
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  return roar_socket_open_fork(mode, host, port);
798 } else if ( type == ROAR_SOCKET_TYPE_FILE ) {
799  return roar_socket_open_file(mode, host, port);
800 } else {
801  roar_err_set(ROAR_ERROR_AFNOTSUP);
802  return -1;
803 }
804
805 if ( mode == MODE_LISTEN ) {
806#if defined(ROAR_HAVE_BSDSOCKETS) || defined(ROAR_TARGET_WIN32)
807  if ( listen(fh, ROAR_SOCKET_QUEUE_LEN) == -1 ) {
808   roar_err_from_errno();
809   close(fh);
810   return -1;
811  }
812#else
813  roar_err_set(ROAR_ERROR_NOSYS);
814  return -1;
815#endif
816 }
817
818 return fh;
819}
820
821int roar_socket_open_fork  (int mode, const char * host, int port) {
822#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
823 char * daemonimage;
824 int socks[2];
825 int r;
826 char fhstr[8];
827
828 if ( mode == MODE_LISTEN )
829  return -1;
830
831 // TODO: FIXME: we should move this into the config structure.
832 daemonimage = getenv("ROAR_DAEMONIMAGE");
833
834 if ( daemonimage == NULL || *daemonimage == 0 )
835  daemonimage = "roard";
836
837 if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) {
838  return -1;
839 }
840
841 r = fork();
842
843 if ( r == -1 ) { // error!
844  ROAR_ERR("roar_socket_open_fork(*): Can not fork: %s", strerror(errno));
845  close(socks[0]);
846  close(socks[1]);
847  return -1;
848 } else if ( r == 0 ) { // we are the child
849  close(socks[0]);
850
851  close(ROAR_STDIN ); // we do not want roard to have any standard input
852  close(ROAR_STDOUT); // STDOUT is also not needed, so we close it,
853                      // but STDERR we keep open for error messages.
854
855  snprintf(fhstr, 7, "%i", socks[1]);
856
857  execlp(daemonimage, daemonimage, "--no-listen", "--client-fh", fhstr, (char*)NULL);
858
859  // we are still alive?
860  ROAR_ERR("roar_socket_open_fork(*): alive after exec(), that's bad!");
861  _exit(1);
862 } else { // we are the parent
863  close(socks[1]);
864  return socks[0];
865 }
866
867 return -1;
868#else
869 ROAR_ERR("roar_socket_open_fork(*): There is no UNIX Domain Socket support in win32, download a real OS.");
870 return -1;
871#endif
872}
873
874int roar_socket_open_file  (int mode, const char * host, int port) {
875#ifdef ROAR_HAVE_IO_POSIX
876 int fh;
877
878 if ( mode == MODE_LISTEN )
879  return -1;
880
881 if ( (fh = open(host, O_RDONLY, 0644)) == -1 ) {
882  ROAR_ERR("roar_socket_open_file(*): Can not open file %s: %s", host, strerror(errno));
883 }
884
885 return fh;
886#else
887 return -1;
888#endif
889}
890
891// --- [ PROXY CODE ] ---
892
893#ifndef ROAR_HAVE_IO_POSIX
894#ifdef  ROAR_SUPPORT_PROXY
895#undef  ROAR_SUPPORT_PROXY
896#endif
897#endif
898
899// generic proxy code:
900
901#ifdef ROAR_SUPPORT_PROXY
902int roar_socket_open_proxy (int mode, int type, const char * host, int port, const char * proxy_type) {
903 int    proxy_port = -1;
904 char   proxy_host[ROAR_SOCKET_MAX_HOSTNAMELEN];
905 char * proxy_addr = NULL;
906 int    i;
907 int    fh = -1;
908 char * user = NULL, * pw = NULL, * opts = NULL;
909 char * sep;
910 int    no_fh = 0;
911 char   proxy_addr_buf[1024];
912 static struct passwd * passwd;
913 int (* code)(int mode, int fh, const char * host, int port, const char * user, const char * pw, const char * opts) = NULL;
914
915 if ( passwd == NULL ) {
916  passwd = getpwuid(getuid());
917 }
918
919// TODO: fix this in a good way
920#ifndef ROAR_TARGET_MICROCONTROLLER
921 if ( passwd != NULL )
922  user = passwd->pw_name;
923#endif
924
925 if ( user == NULL )
926  user = getenv("USER");
927
928 // TODO: change this so we support listen() proxys (ssh -R)
929 if ( mode != MODE_CONNECT )
930  return -1;
931
932 if ( !strncmp(proxy_type, "socks", 5) ) {
933  proxy_addr = getenv("socks_proxy");
934
935  proxy_port = 9050; // TOR's default port
936 } else if ( !strcmp(proxy_type, "http") || !strcmp(proxy_type, "https") ) {
937  proxy_port = 8080;
938
939  if ( (proxy_addr = getenv("http_proxy")) == NULL )
940   proxy_addr = getenv("https_proxy");
941
942  if ( proxy_addr == NULL )
943   return -1;
944
945  if ( !strncmp(proxy_addr, "http://", 7) )
946   proxy_addr += 7;
947 } else if ( !strncmp(proxy_type, "ssh", 3) ) {
948  proxy_port = 22;
949  proxy_addr = getenv("ssh_proxy");
950  no_fh      = 1;
951 }
952
953 proxy_addr_buf[1023] = 0;
954 strncpy(proxy_addr_buf, proxy_addr, 1023);
955 proxy_addr = proxy_addr_buf;
956
957 if ( (sep = strstr(proxy_type, "/")) != NULL )
958  opts = sep+1;
959
960 if ( proxy_addr == NULL )
961  return -1;
962
963 if ( (sep = strstr(proxy_addr, "@")) != NULL ) {
964  *sep = 0;
965  user = proxy_addr;
966  proxy_addr = sep+1;
967
968  if ( (sep = strstr(user, ":")) != NULL ) {
969   *sep = 0;
970   pw = sep+1;
971  }
972 }
973
974 ROAR_DBG("roar_socket_open_proxy(*): proxy_type='%s', opts='%s', user='%s', pw=(not shown), proxy_addr='%s'", proxy_type, opts, user, proxy_addr);
975
976 for (i = 0; proxy_addr[i] != 0 && proxy_addr[i] != ':' && i < (ROAR_SOCKET_MAX_HOSTNAMELEN - 1); i++)
977  proxy_host[i] = proxy_addr[i];
978 proxy_host[i] = 0;
979
980 if ( i == 0 ) // no hostname found
981  return -1;
982
983 if ( proxy_addr[i] == ':' )
984  proxy_port = atoi(&proxy_addr[i+1]);
985
986 if ( ! no_fh ) {
987  if ( (fh = roar_socket_open(mode, type, proxy_host, proxy_port)) == -1) {
988   return -1;
989  }
990 }
991
992 if ( !strcmp(proxy_type, "socks4a") ) { // for TOR, the only supported type at the moment
993  code = roar_socket_open_socks4a;
994 } else if ( !strcmp(proxy_type, "socks4d") ) { // DECnet
995  code = roar_socket_open_socks4d;
996 } else if ( !strcmp(proxy_type, "socks4") ) { // good old SOCKS4
997  code = roar_socket_open_socks4;
998 } else if ( !strcmp(proxy_type, "http") ) { // HTTP CONNECT
999  code = roar_socket_open_http;
1000 } else if ( !strncmp(proxy_type, "ssh", 3) ) { // SSH...
1001#ifdef ROAR_HAVE_BIN_SSH
1002  code = roar_socket_open_ssh;
1003#else
1004  ROAR_ERR("roar_socket_open_proxy(*): No SSH support compiled in");
1005#endif
1006 } else {
1007  return -1; // unknown type
1008 }
1009
1010 if ( code != NULL ) {
1011  if ( no_fh ) {
1012   fh = code(mode, fh, host, port, user, pw, opts);
1013  } else {
1014   if ( code(mode, fh, host, port, user, pw, opts) == -1 ) {
1015    close(fh);
1016    return -1;
1017   }
1018  }
1019
1020  return fh;
1021 }
1022
1023 close(fh);
1024 return -1;
1025}
1026
1027// protocoll dependet proxy code:
1028
1029int roar_socket_open_socks4 (int mode, int fh, const char * host, int port, const char * user, const char * pw, const char * opts) {
1030#ifndef ROAR_TARGET_MICROCONTROLLER
1031 struct hostent     * he;
1032
1033 if ( (he = gethostbyname(host)) == NULL ) {
1034  ROAR_ERR("roar_socket_open_socks4(*): Can\'t resolve host name '%s'", host);
1035  return -1;
1036 }
1037
1038 return roar_socket_open_socks4x(mode, fh, he->h_addr, port, NULL, 0, user);
1039#else
1040 return -1;
1041#endif
1042}
1043
1044int roar_socket_open_socks4a(int mode, int fh, const char * host, int port, const char * user, const char * pw, const char * opts) {
1045 return roar_socket_open_socks4x(mode, fh, "\0\0\0\1", port, host, strlen(host)+1, user);
1046}
1047
1048int roar_socket_open_socks4d(int mode, int fh, const char * host, int port, const char * user, const char * pw, const char * opts) {
1049 size_t len = strlen(host)+1;
1050 char * dp;
1051
1052 if ( port == 0 ) {
1053  if ( (dp = strstr(host, "::")) == NULL )
1054   return -1;
1055
1056  len--;
1057  *dp = 0;
1058  memmove(dp+1, dp+2, len - (dp-host) - 1);
1059 }
1060
1061 return roar_socket_open_socks4x(mode, fh, "\0\2\0\0", port, host, len, user);
1062}
1063
1064int roar_socket_open_socks4x(int mode, int fh, char host[4], int port, const char * app, size_t app_len, const char * user) {
1065 char buf[9];
1066 int len;
1067
1068 buf[0] = 0x04;
1069 buf[1] = mode == MODE_CONNECT ? 0x01 : 0x02;
1070 *((uint16_t*)&buf[2]) = htons(port);
1071 memcpy(buf+4, host, 4);
1072
1073 if ( user == NULL ) {
1074  buf[8] = 0x00;
1075  len = 9;
1076 } else {
1077  len = 8;
1078 }
1079
1080 if ( write(fh, buf, len) != len )
1081  return -1;
1082
1083 if ( user != NULL ) {
1084  len = strlen(user) + 1;
1085  if ( write(fh, user, len) != len )
1086   return -1;
1087 }
1088
1089 if ( app_len > 0 )
1090  if ( write(fh, app, app_len) != (ssize_t)app_len )
1091   return -1;
1092
1093 if ( read(fh, buf, 8) != 8 )
1094  return -1;
1095
1096 if ( buf[1] != 0x5a )
1097  return -1;
1098
1099 return 0;
1100}
1101
1102int roar_socket_open_http   (int mode, int fh, const char * host, int port, const char * user, const char * pw, const char * opts) {
1103 char buf[1024];
1104 int len;
1105
1106 if ( port == 0 || host == NULL )
1107  return -1;
1108
1109 if ( *host == '/' ) // AF_UNIX
1110  return -1;
1111
1112 if ( (len = snprintf(buf, 1024, "CONNECT %s:%i HTTP/1.0\r\nUser-Agent: libroar\r\n\r\n", host, port)) == -1 )
1113  return -1;
1114
1115 if ( write(fh, buf, len) != len )
1116  return -1;
1117
1118 while ( (len = read(fh, buf, 1024)) ) {
1119  if ( len == 1024 ) { // overlong lion
1120   return -1;
1121  } else if ( len == 2 && buf[0] == '\r' && buf[1] == '\n' ) {
1122   break;
1123  } else if ( len == 1 && (buf[0] == '\r' || buf[0] == '\n') ) { // bad proxy or devel trying to debug ;)
1124   break;
1125  } else if ( len >= 4 && buf[len-4] == '\r' && buf[len-3] == '\n' && buf[len-2] == '\r' && buf[len-1] == '\n' ) {
1126   break;
1127  }
1128 }
1129
1130 return 0;
1131}
1132
1133
1134#ifdef ROAR_HAVE_BIN_SSH
1135int roar_socket_open_ssh    (int mode, int fh, const char * host, int port, const char * user, const char * pw, const char * opts) {
1136 char * proxy_addr = getenv("ssh_proxy");
1137 char * sep;
1138 char   cmd[1024] = {0}, rcmd[1024] = {0};
1139 int    proxy_port = 22;
1140 int    use_socat = 0;
1141 int r;
1142 int socks[2];
1143
1144 if ( host == NULL )
1145  return -1;
1146
1147 if ( *host == '/' )
1148  use_socat = 1;
1149
1150 if ( mode == MODE_LISTEN )
1151  return -1;
1152
1153 if ( proxy_addr == NULL )
1154  return -1;
1155
1156 if ( opts != NULL ) {
1157  if ( !strcmp(opts, "socat") ) {
1158   use_socat = 1;
1159  } else if ( !strcmp(opts, "netcat") ) {
1160   use_socat = 0;
1161  } else {
1162   return -1;
1163  }
1164 }
1165
1166 ROAR_DBG("roar_socket_open_ssh(*): proxy_addr='%s'", proxy_addr);
1167
1168 if ( (sep = strstr(proxy_addr, "@")) != NULL )
1169  proxy_addr = sep+1;
1170
1171 if ( (sep = strstr(proxy_addr, ":")) != NULL ) {
1172  *sep = 0;
1173  proxy_port = atoi(sep+1);
1174 }
1175
1176
1177 if ( !strcmp(host, "+fork") ) {
1178  strncpy(rcmd, "roard --no-listen --client-fh 0", 32);
1179 } else {
1180  if ( use_socat ) {
1181   if ( *host == '/' ) {
1182    snprintf(rcmd, 1023, "socat stdio unix-connect:\"%s\"", host);
1183   } else {
1184    snprintf(rcmd, 1023, "socat stdio tcp:\"%s\":%i", host, port);
1185   }
1186  } else {
1187   snprintf(rcmd, 1023, "$(which netcat nc 2> /dev/null | grep -v \" \" | head -n 1) \"%s\" %i", host, port);
1188  }
1189
1190  rcmd[1023] = 0;
1191 }
1192
1193 ROAR_DBG("roar_socket_open_ssh(*): proxy_port=%i, user='%s', proxy_addr='%s'", proxy_port, user, proxy_addr);
1194 ROAR_DBG("roar_socket_open_ssh(*): rcmd: %s", rcmd);
1195 snprintf(cmd, 1023, ROAR_HAVE_BIN_SSH " -p %i -l '%s' '%s' '%s'", proxy_port, user, proxy_addr, rcmd);
1196 cmd[1023] = 0;
1197
1198
1199// TODO: get this more portable!
1200#ifdef AF_UNIX
1201 if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) {
1202  return -1;
1203 }
1204#else
1205 return -1;
1206#endif
1207
1208 r = fork();
1209
1210 if ( r == -1 ) { // error!
1211  ROAR_ERR("roar_socket_open_ssh(*): Can not fork: %s", strerror(errno));
1212  close(socks[0]);
1213  close(socks[1]);
1214  return -1;
1215 } else if ( r == 0 ) { // we are the child
1216  close(socks[0]);
1217
1218  close(ROAR_STDIN ); // we do not want roard to have any standard input
1219  close(ROAR_STDOUT); // STDOUT is also not needed, so we close it,
1220                      // but STDERR we keep open for error messages.
1221
1222  dup2(socks[1], 0);
1223  dup2(socks[1], 1);
1224
1225  execlp("sh", "sh", "-c", cmd, (char*)NULL);
1226
1227  // we are still alive?
1228  ROAR_ERR("roar_socket_open_ssh(*): alive after exec(), that's bad!");
1229  _exit(1);
1230 } else { // we are the parent
1231  close(socks[1]);
1232  return socks[0];
1233 }
1234 return -1;
1235}
1236#endif
1237
1238#endif // ROAR_SUPPORT_PROXY
1239
1240//ll
Note: See TracBrowser for help on using the repository browser.