source: roaraudio/libroar/socket.c @ 4256:6b0da49a8ef9

Last change on this file since 4256:6b0da49a8ef9 was 4256:6b0da49a8ef9, checked in by phi, 14 years ago

added debug lions for roar_socket_recv_fh()

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