source: roaraudio/libroar/socket.c @ 6036:3068a825a29e

Last change on this file since 6036:3068a825a29e was 6036:3068a825a29e, checked in by phi, 10 years ago

killed some compiler warnings

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