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