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