source: roaraudio/libroar/socket.c @ 5951:b0e1eff45456

Last change on this file since 5951:b0e1eff45456 was 5951:b0e1eff45456, checked in by phi, 10 years ago

more header cleanup: cleanup of #includes

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