source: roaraudio/libroar/socket.c @ 5427:543c052527b2

Last change on this file since 5427:543c052527b2 was 5419:655954f5b684, checked in by phi, 12 years ago

fixed some compiler warnings

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