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