source: roaraudio/libroar/socket.c @ 5393:f50243718494

Last change on this file since 5393:f50243718494 was 5392:cb1666507792, checked in by phi, 12 years ago

only open sockets if we have BSD socket interface

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