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