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