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