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