source: roaraudio/libroar/socket.c @ 5375:2b4d1e027b2d

Last change on this file since 5375:2b4d1e027b2d was 5375:2b4d1e027b2d, checked in by phi, 12 years ago

made some _LIBROAR_ATTR_TO_STATIC functions static

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