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