source: roaraudio/libroar/socket.c @ 968:82561f5f0063

Last change on this file since 968:82561f5f0063 was 968:82561f5f0063, checked in by phi, 15 years ago

set recv buffer to minimum for streams

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