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