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