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