[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)); |
---|
[1068] | 348 | if ( bind_sockaddr.sdn_objnamel > DN_MAXOBJL ) |
---|
| 349 | bind_sockaddr.sdn_objnamel = DN_MAXOBJL; |
---|
| 350 | strncpy((char*)bind_sockaddr.sdn_objname, object, DN_MAXOBJL); |
---|
[508] | 351 | } |
---|
| 352 | |
---|
| 353 | if ( bind(fh, (struct sockaddr *) &bind_sockaddr, sizeof(bind_sockaddr)) == -1 ) { |
---|
| 354 | close(fh); |
---|
| 355 | return -1; |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | if ( listen(fh, 8) == -1 ) { |
---|
| 359 | close(fh); |
---|
| 360 | return -1; |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | return fh; |
---|
| 364 | #else |
---|
| 365 | return -1; |
---|
| 366 | #endif |
---|
| 367 | } |
---|
| 368 | |
---|
[521] | 369 | char * roar_socket_get_local_nodename(void) { |
---|
| 370 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 371 | static char node[16] = {0}; |
---|
| 372 | struct dn_naddr *binaddr; |
---|
| 373 | struct nodeent *dp; |
---|
| 374 | |
---|
| 375 | if ( !node[0] ) { |
---|
| 376 | if ( (binaddr=getnodeadd()) == NULL) |
---|
| 377 | return NULL; |
---|
| 378 | |
---|
| 379 | if ( (dp=getnodebyaddr((char*)binaddr->a_addr, binaddr->a_len, PF_DECnet)) == NULL ) |
---|
| 380 | return NULL; |
---|
| 381 | |
---|
| 382 | strncpy(node, dp->n_name, 15); |
---|
| 383 | node[15] = 0; |
---|
| 384 | } |
---|
| 385 | |
---|
| 386 | return node; |
---|
| 387 | #else |
---|
| 388 | return NULL; |
---|
| 389 | #endif |
---|
| 390 | } |
---|
| 391 | |
---|
[0] | 392 | int roar_socket_open (int mode, int type, char * host, int port) { |
---|
| 393 | // int type = ROAR_SOCKET_TYPE_INET; |
---|
| 394 | int fh; |
---|
[531] | 395 | #ifdef ROAR_HAVE_IPX |
---|
| 396 | #define _NEED_OBJ |
---|
| 397 | int i; |
---|
| 398 | int ret; |
---|
| 399 | #endif |
---|
[512] | 400 | union { |
---|
| 401 | struct sockaddr_in in; |
---|
| 402 | struct sockaddr_un un; |
---|
[890] | 403 | #ifdef ROAR_HAVE_IPV6 |
---|
[514] | 404 | struct sockaddr_in6 in6; |
---|
[890] | 405 | #endif |
---|
[531] | 406 | #ifdef ROAR_HAVE_IPX |
---|
| 407 | struct sockaddr_ipx ipx; |
---|
| 408 | #endif |
---|
[512] | 409 | } socket_addr; |
---|
[0] | 410 | struct hostent * he; |
---|
| 411 | //unsigned int host_div = 0; |
---|
| 412 | int (*mode_func)(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen) = connect; // default is to connect |
---|
[501] | 413 | #ifdef ROAR_HAVE_LIBDNET |
---|
[531] | 414 | #define _NEED_OBJ |
---|
| 415 | #endif |
---|
| 416 | #ifdef _NEED_OBJ |
---|
[501] | 417 | char obj[80]; |
---|
| 418 | char * del; |
---|
| 419 | #endif |
---|
[0] | 420 | |
---|
| 421 | if ( mode == MODE_LISTEN ) |
---|
| 422 | mode_func = bind; |
---|
| 423 | |
---|
| 424 | if ( type == ROAR_SOCKET_TYPE_UNKNOWN ) { |
---|
| 425 | type = ROAR_SOCKET_TYPE_INET; |
---|
[69] | 426 | if ( *host == '/' ) { |
---|
[0] | 427 | type = ROAR_SOCKET_TYPE_UNIX; |
---|
[69] | 428 | } else if ( strcmp(host, "+fork") == 0 ) { |
---|
| 429 | type = ROAR_SOCKET_TYPE_FORK; |
---|
[501] | 430 | } else if ( strstr(host, "::") != NULL ) { |
---|
| 431 | type = ROAR_SOCKET_TYPE_DECNET; |
---|
[531] | 432 | } else if ( host[strlen(host)-1] == ')' ) { |
---|
| 433 | type = ROAR_SOCKET_TYPE_IPX; |
---|
[69] | 434 | } |
---|
[0] | 435 | } |
---|
| 436 | |
---|
| 437 | |
---|
| 438 | ROAR_DBG("roar_socket_open(*): type=%s, host='%s', port=%i", |
---|
| 439 | type == ROAR_SOCKET_TYPE_UNIX ? "UNIX" : "INET", host, port); |
---|
| 440 | |
---|
[501] | 441 | if ( type == ROAR_SOCKET_TYPE_DECNET ) { |
---|
| 442 | #ifdef ROAR_HAVE_LIBDNET |
---|
[508] | 443 | ROAR_DBG("roar_socket_open(*): hostname for DECnet: host(%p)=%s", host, host); |
---|
[501] | 444 | del = strstr(host, "::"); |
---|
[508] | 445 | ROAR_DBG("roar_socket_open(*): hostname for DECnet: del(%p)=%s", del, del); |
---|
| 446 | |
---|
| 447 | if ( del == NULL ) { |
---|
| 448 | ROAR_WARN("roar_socket_open(*): invalid hostname for DECnet: %s", host); |
---|
| 449 | return -1; |
---|
| 450 | } |
---|
| 451 | |
---|
[501] | 452 | *del = 0; |
---|
| 453 | |
---|
| 454 | if ( *(del+2) == '#' ) { // assume we have node::#num |
---|
| 455 | port = atoi(del+2); |
---|
| 456 | } |
---|
| 457 | |
---|
| 458 | if ( port ) { |
---|
[1067] | 459 | snprintf(obj, 7, "%i", port); // no need for snprintf() as dec(port) is smaller than obj[] |
---|
[501] | 460 | } else { |
---|
| 461 | *obj = 0; |
---|
| 462 | strncat(obj, del+2, 79); |
---|
| 463 | } |
---|
| 464 | |
---|
[508] | 465 | if ( mode == MODE_LISTEN ) { |
---|
| 466 | fh = roar_socket_listen_decnet(obj, port); |
---|
| 467 | *del = ':'; |
---|
| 468 | return fh; |
---|
| 469 | // return -1; // listen sockets on DECnet are not supportet at the moment |
---|
| 470 | } else { |
---|
| 471 | // There is nothing wrong in this case to use dnet_conn() so we do. |
---|
[501] | 472 | fh = dnet_conn(host, obj, SOCK_STREAM, 0 ,0 ,0 , 0); |
---|
| 473 | *del = ':'; |
---|
| 474 | return fh; |
---|
[533] | 475 | } |
---|
[501] | 476 | #else |
---|
[533] | 477 | return -1; // no decnet support |
---|
[501] | 478 | #endif |
---|
| 479 | } |
---|
| 480 | |
---|
[512] | 481 | memset(&socket_addr, 0, sizeof(socket_addr)); |
---|
[60] | 482 | memset(&he, 0, sizeof(he)); // FIXME: we have a valid pointer in here???? |
---|
[0] | 483 | |
---|
| 484 | |
---|
[520] | 485 | if ( type == ROAR_SOCKET_TYPE_INET || type == ROAR_SOCKET_TYPE_INET6 ) { |
---|
[0] | 486 | |
---|
| 487 | if ( (he = gethostbyname(host)) == NULL ) { |
---|
| 488 | ROAR_ERR("roar_socket_open(*): Can\'t resolve host name '%s'", |
---|
| 489 | host); |
---|
| 490 | return -1; |
---|
| 491 | } |
---|
| 492 | |
---|
[520] | 493 | memcpy((struct in_addr *)&socket_addr.in.sin_addr, he->h_addr, sizeof(struct in_addr)); |
---|
[0] | 494 | |
---|
[520] | 495 | /* set the connect information */ |
---|
| 496 | socket_addr.in.sin_family = AF_INET; |
---|
| 497 | socket_addr.in.sin_port = ROAR_HOST2NET16(port); |
---|
| 498 | |
---|
| 499 | fh = roar_socket_new_tcp(); |
---|
[0] | 500 | |
---|
[520] | 501 | if ( mode_func(fh, (struct sockaddr *)&socket_addr.in, sizeof(struct sockaddr_in)) == -1 ) { |
---|
| 502 | ROAR_DBG("roar_socket_open(*): Can not connect/bind: %s", strerror(errno)); |
---|
| 503 | close(fh); |
---|
| 504 | return -1; |
---|
| 505 | } |
---|
[0] | 506 | // hey! we have a socket... |
---|
[69] | 507 | } else if ( type == ROAR_SOCKET_TYPE_UNIX ) { |
---|
[512] | 508 | socket_addr.un.sun_family = AF_UNIX; |
---|
| 509 | strncpy(socket_addr.un.sun_path, host, sizeof(socket_addr.un.sun_path) - 1); |
---|
[60] | 510 | |
---|
[0] | 511 | fh = roar_socket_new_unix(); |
---|
[60] | 512 | |
---|
[512] | 513 | if ( mode_func(fh, (struct sockaddr *)&socket_addr.un, sizeof(struct sockaddr_un)) == -1 ) { |
---|
[60] | 514 | ROAR_DBG("roar_socket_open(*): Can not connect/bind: %s", strerror(errno)); |
---|
| 515 | close(fh); |
---|
| 516 | return -1; |
---|
| 517 | } |
---|
[531] | 518 | } else if ( type == ROAR_SOCKET_TYPE_IPX ) { |
---|
[534] | 519 | #ifdef ROAR_HAVE_IPX |
---|
[531] | 520 | socket_addr.ipx.sipx_family = AF_IPX; |
---|
| 521 | |
---|
| 522 | obj[0] = 0; |
---|
| 523 | |
---|
| 524 | if ( (ret = sscanf(host, "%8x.%12s(%x)", &socket_addr.ipx.sipx_network, obj, |
---|
| 525 | (unsigned int *)&socket_addr.ipx.sipx_port)) < 2 ) { |
---|
| 526 | return -1; |
---|
| 527 | } else if ( ret == 2 ) { |
---|
| 528 | socket_addr.ipx.sipx_port = port; // Network Byte Order? |
---|
| 529 | } |
---|
| 530 | |
---|
| 531 | memset(socket_addr.ipx.sipx_node, 0, IPX_NODE_LEN); |
---|
| 532 | ret = strlen(obj); |
---|
| 533 | |
---|
| 534 | if ( ret % 2 ) // needs to be even at the moment |
---|
| 535 | return -1; |
---|
| 536 | |
---|
| 537 | fh = roar_socket_new_ipx(); |
---|
| 538 | |
---|
| 539 | close(fh); |
---|
| 540 | return -1; |
---|
[534] | 541 | #else |
---|
| 542 | return -1; |
---|
| 543 | #endif |
---|
[69] | 544 | } else if ( type == ROAR_SOCKET_TYPE_FORK ) { |
---|
| 545 | return roar_socket_open_fork(mode, host, port); |
---|
[75] | 546 | } else if ( type == ROAR_SOCKET_TYPE_FILE ) { |
---|
| 547 | return roar_socket_open_file(mode, host, port); |
---|
[69] | 548 | } else { |
---|
| 549 | return -1; |
---|
[0] | 550 | } |
---|
| 551 | |
---|
| 552 | if ( mode == MODE_LISTEN ) |
---|
| 553 | if ( listen(fh, ROAR_SOCKET_QUEUE_LEN) == -1 ) { |
---|
| 554 | close(fh); |
---|
| 555 | return -1; |
---|
| 556 | } |
---|
| 557 | |
---|
| 558 | return fh; |
---|
| 559 | } |
---|
| 560 | |
---|
[69] | 561 | int roar_socket_open_fork (int mode, char * host, int port) { |
---|
| 562 | int socks[2]; |
---|
| 563 | int r; |
---|
| 564 | char fhstr[8]; |
---|
| 565 | |
---|
| 566 | if ( mode == MODE_LISTEN ) |
---|
| 567 | return -1; |
---|
| 568 | |
---|
| 569 | if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) { |
---|
| 570 | return -1; |
---|
| 571 | } |
---|
| 572 | |
---|
| 573 | r = fork(); |
---|
| 574 | |
---|
| 575 | if ( r == -1 ) { // error! |
---|
| 576 | ROAR_ERR("roar_socket_open_fork(*): Can not fork: %s", strerror(errno)); |
---|
| 577 | close(socks[0]); |
---|
| 578 | close(socks[1]); |
---|
| 579 | return -1; |
---|
| 580 | } else if ( r == 0 ) { // we are the child |
---|
| 581 | close(socks[0]); |
---|
| 582 | |
---|
[70] | 583 | close(ROAR_STDIN ); // we do not want roard to have any standard input |
---|
| 584 | close(ROAR_STDOUT); // STDOUT is also not needed, so we close it, |
---|
| 585 | // but STDERR we keep open for error messages. |
---|
| 586 | |
---|
[69] | 587 | snprintf(fhstr, 7, "%i", socks[1]); |
---|
| 588 | |
---|
[548] | 589 | execlp("roard", "roard", "--no-listen", "--client-fh", fhstr, NULL); |
---|
[69] | 590 | |
---|
| 591 | // we are still alive? |
---|
| 592 | ROAR_ERR("roar_socket_open_fork(*): alive after exec(), that's bad!"); |
---|
| 593 | _exit(1); |
---|
| 594 | } else { // we are the parent |
---|
| 595 | close(socks[1]); |
---|
| 596 | return socks[0]; |
---|
| 597 | } |
---|
| 598 | |
---|
| 599 | return -1; |
---|
| 600 | } |
---|
| 601 | |
---|
[75] | 602 | int roar_socket_open_file (int mode, char * host, int port) { |
---|
| 603 | int fh; |
---|
| 604 | |
---|
| 605 | if ( mode == MODE_LISTEN ) |
---|
| 606 | return -1; |
---|
| 607 | |
---|
| 608 | if ( (fh = open(host, O_RDONLY, 0644)) == -1 ) { |
---|
| 609 | ROAR_ERR("roar_socket_open_file(*): Can not open file %s: %s", host, strerror(errno)); |
---|
| 610 | } |
---|
| 611 | |
---|
| 612 | return fh; |
---|
| 613 | } |
---|
| 614 | |
---|
[2] | 615 | // --- [ PROXY CODE ] --- |
---|
| 616 | |
---|
| 617 | // generic proxy code: |
---|
| 618 | |
---|
| 619 | int roar_socket_open_proxy (int mode, int type, char * host, int port, char * proxy_type) { |
---|
[829] | 620 | int proxy_port = -1; |
---|
[2] | 621 | char proxy_host[ROAR_SOCKET_MAX_HOSTNAMELEN]; |
---|
[829] | 622 | char * proxy_addr = NULL; |
---|
[2] | 623 | int i; |
---|
[829] | 624 | int fh = -1; |
---|
[835] | 625 | char * user = NULL, * pw = NULL, * opts = NULL; |
---|
[836] | 626 | char * sep; |
---|
[838] | 627 | int no_fh = 0; |
---|
[1008] | 628 | char proxy_addr_buf[1024]; |
---|
[835] | 629 | static struct passwd * passwd; |
---|
[834] | 630 | int (* code)(int mode, int fh, char * host, int port, char * user, char * pw, char * opts) = NULL; |
---|
[2] | 631 | |
---|
[835] | 632 | if ( passwd == NULL ) { |
---|
| 633 | passwd = getpwuid(getuid()); |
---|
| 634 | } |
---|
| 635 | |
---|
| 636 | if ( passwd != NULL ) |
---|
| 637 | user = passwd->pw_name; |
---|
| 638 | |
---|
| 639 | if ( user == NULL ) |
---|
| 640 | user = getenv("USER"); |
---|
| 641 | |
---|
[2] | 642 | // TODO: change this so we support listen() proxys (ssh -R) |
---|
| 643 | if ( mode != MODE_CONNECT ) |
---|
| 644 | return -1; |
---|
| 645 | |
---|
[829] | 646 | if ( !strncmp(proxy_type, "socks", 5) ) { |
---|
[2] | 647 | proxy_addr = getenv("socks_proxy"); |
---|
| 648 | |
---|
| 649 | proxy_port = 9050; // TOR's default port |
---|
[832] | 650 | } else if ( !strcmp(proxy_type, "http") || !strcmp(proxy_type, "https") ) { |
---|
[830] | 651 | proxy_port = 8080; |
---|
[2] | 652 | |
---|
[832] | 653 | if ( (proxy_addr = getenv("http_proxy")) == NULL ) |
---|
| 654 | proxy_addr = getenv("https_proxy"); |
---|
| 655 | |
---|
[2] | 656 | if ( proxy_addr == NULL ) |
---|
| 657 | return -1; |
---|
| 658 | |
---|
[830] | 659 | if ( !strncmp(proxy_addr, "http://", 7) ) |
---|
| 660 | proxy_addr += 7; |
---|
[838] | 661 | } else if ( !strncmp(proxy_type, "ssh", 3) ) { |
---|
| 662 | proxy_port = 22; |
---|
| 663 | proxy_addr = getenv("ssh_proxy"); |
---|
| 664 | no_fh = 1; |
---|
[830] | 665 | } |
---|
[2] | 666 | |
---|
[1008] | 667 | proxy_addr_buf[1023] = 0; |
---|
| 668 | strncpy(proxy_addr_buf, proxy_addr, 1023); |
---|
| 669 | proxy_addr = proxy_addr_buf; |
---|
| 670 | |
---|
[837] | 671 | if ( (sep = strstr(proxy_type, "/")) != NULL ) |
---|
| 672 | opts = sep+1; |
---|
| 673 | |
---|
[830] | 674 | if ( proxy_addr == NULL ) |
---|
| 675 | return -1; |
---|
[2] | 676 | |
---|
[836] | 677 | if ( (sep = strstr(proxy_addr, "@")) != NULL ) { |
---|
| 678 | *sep = 0; |
---|
| 679 | user = proxy_addr; |
---|
| 680 | proxy_addr = sep+1; |
---|
| 681 | |
---|
| 682 | if ( (sep = strstr(user, ":")) != NULL ) { |
---|
| 683 | *sep = 0; |
---|
| 684 | pw = sep+1; |
---|
| 685 | } |
---|
| 686 | } |
---|
| 687 | |
---|
[1008] | 688 | 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); |
---|
| 689 | |
---|
[830] | 690 | for (i = 0; proxy_addr[i] != 0 && proxy_addr[i] != ':' && i < ROAR_SOCKET_MAX_HOSTNAMELEN; i++) |
---|
| 691 | proxy_host[i] = proxy_addr[i]; |
---|
| 692 | proxy_host[i] = 0; |
---|
[2] | 693 | |
---|
[830] | 694 | if ( i == 0 ) // no hostname found |
---|
| 695 | return -1; |
---|
| 696 | |
---|
| 697 | if ( proxy_addr[i] == ':' ) |
---|
| 698 | proxy_port = atoi(&proxy_addr[i+1]); |
---|
| 699 | |
---|
[838] | 700 | if ( ! no_fh ) { |
---|
| 701 | if ( (fh = roar_socket_open(mode, type, proxy_host, proxy_port)) == -1) { |
---|
| 702 | return -1; |
---|
| 703 | } |
---|
[829] | 704 | } |
---|
[2] | 705 | |
---|
[829] | 706 | if ( !strcmp(proxy_type, "socks4a") ) { // for TOR, the only supported type at the moment |
---|
[833] | 707 | code = roar_socket_open_socks4a; |
---|
| 708 | } else if ( !strcmp(proxy_type, "socks4d") ) { // DECnet |
---|
| 709 | code = roar_socket_open_socks4d; |
---|
| 710 | } else if ( !strcmp(proxy_type, "socks4") ) { // good old SOCKS4 |
---|
| 711 | code = roar_socket_open_socks4; |
---|
| 712 | } else if ( !strcmp(proxy_type, "http") ) { // HTTP CONNECT |
---|
| 713 | code = roar_socket_open_http; |
---|
[839] | 714 | } else if ( !strncmp(proxy_type, "ssh", 3) ) { // SSH... |
---|
[1063] | 715 | #ifdef ROAR_HAVE_BIN_SSH |
---|
[839] | 716 | code = roar_socket_open_ssh; |
---|
[1063] | 717 | #else |
---|
| 718 | ROAR_ERR("roar_socket_open_proxy(*): No SSH support compiled in"); |
---|
| 719 | #endif |
---|
[833] | 720 | } else { |
---|
| 721 | return -1; // unknown type |
---|
| 722 | } |
---|
[2] | 723 | |
---|
[833] | 724 | if ( code != NULL ) { |
---|
[838] | 725 | if ( no_fh ) { |
---|
| 726 | fh = code(mode, fh, host, port, user, pw, opts); |
---|
| 727 | } else { |
---|
| 728 | if ( code(mode, fh, host, port, user, pw, opts) == -1 ) { |
---|
| 729 | close(fh); |
---|
| 730 | return -1; |
---|
| 731 | } |
---|
[829] | 732 | } |
---|
| 733 | |
---|
| 734 | return fh; |
---|
[833] | 735 | } |
---|
[829] | 736 | |
---|
[833] | 737 | close(fh); |
---|
| 738 | return -1; |
---|
[2] | 739 | } |
---|
| 740 | |
---|
| 741 | // protocoll dependet proxy code: |
---|
| 742 | |
---|
[834] | 743 | int roar_socket_open_socks4 (int mode, int fh, char * host, int port, char * user, char * pw, char * opts) { |
---|
[829] | 744 | struct hostent * he; |
---|
| 745 | |
---|
| 746 | if ( (he = gethostbyname(host)) == NULL ) { |
---|
| 747 | ROAR_ERR("roar_socket_open_socks4(*): Can\'t resolve host name '%s'", host); |
---|
| 748 | return -1; |
---|
| 749 | } |
---|
| 750 | |
---|
[834] | 751 | return roar_socket_open_socks4x(mode, fh, he->h_addr, port, NULL, 0, user); |
---|
[829] | 752 | } |
---|
| 753 | |
---|
[834] | 754 | int roar_socket_open_socks4a(int mode, int fh, char * host, int port, char * user, char * pw, char * opts) { |
---|
| 755 | return roar_socket_open_socks4x(mode, fh, "\0\0\0\1", port, host, strlen(host)+1, user); |
---|
[829] | 756 | } |
---|
| 757 | |
---|
[834] | 758 | int roar_socket_open_socks4d(int mode, int fh, char * host, int port, char * user, char * pw, char * opts) { |
---|
[829] | 759 | size_t len = strlen(host)+1; |
---|
| 760 | char * dp; |
---|
| 761 | |
---|
| 762 | if ( port == 0 ) { |
---|
| 763 | if ( (dp = strstr(host, "::")) == NULL ) |
---|
| 764 | return -1; |
---|
| 765 | |
---|
| 766 | len--; |
---|
| 767 | *dp = 0; |
---|
| 768 | memmove(dp+1, dp+2, len - (dp-host) - 1); |
---|
| 769 | } |
---|
| 770 | |
---|
[834] | 771 | return roar_socket_open_socks4x(mode, fh, "\0\2\0\0", port, host, len, user); |
---|
[829] | 772 | } |
---|
| 773 | |
---|
[834] | 774 | int roar_socket_open_socks4x(int mode, int fh, char host[4], int port, char * app, size_t app_len, char * user) { |
---|
[2] | 775 | char buf[9]; |
---|
[835] | 776 | int len; |
---|
[2] | 777 | |
---|
| 778 | buf[0] = 0x04; |
---|
| 779 | buf[1] = mode == MODE_CONNECT ? 0x01 : 0x02; |
---|
| 780 | *((uint16_t*)&buf[2]) = htons(port); |
---|
[829] | 781 | memcpy(buf+4, host, 4); |
---|
[835] | 782 | |
---|
| 783 | if ( user == NULL ) { |
---|
| 784 | buf[8] = 0x00; |
---|
| 785 | len = 9; |
---|
| 786 | } else { |
---|
| 787 | len = 8; |
---|
| 788 | } |
---|
[2] | 789 | |
---|
[835] | 790 | if ( write(fh, buf, len) != len ) |
---|
[2] | 791 | return -1; |
---|
| 792 | |
---|
[835] | 793 | if ( user != NULL ) { |
---|
| 794 | len = strlen(user) + 1; |
---|
| 795 | if ( write(fh, user, len) != len ) |
---|
| 796 | return -1; |
---|
| 797 | } |
---|
| 798 | |
---|
[829] | 799 | if ( app_len > 0 ) |
---|
| 800 | if ( write(fh, app, app_len) != app_len ) |
---|
| 801 | return -1; |
---|
[2] | 802 | |
---|
| 803 | if ( read(fh, buf, 8) != 8 ) |
---|
| 804 | return -1; |
---|
| 805 | |
---|
| 806 | if ( buf[1] != 0x5a ) |
---|
| 807 | return -1; |
---|
| 808 | |
---|
| 809 | return 0; |
---|
| 810 | } |
---|
| 811 | |
---|
[834] | 812 | int roar_socket_open_http (int mode, int fh, char * host, int port, char * user, char * pw, char * opts) { |
---|
[831] | 813 | char buf[1024]; |
---|
| 814 | int len; |
---|
| 815 | |
---|
| 816 | if ( port == 0 || host == NULL ) |
---|
| 817 | return -1; |
---|
| 818 | |
---|
| 819 | if ( *host == '/' ) // AF_UNIX |
---|
| 820 | return -1; |
---|
| 821 | |
---|
| 822 | if ( (len = snprintf(buf, 1024, "CONNECT %s:%i HTTP/1.0\r\nUser-Agent: libroar\r\n\r\n", host, port)) == -1 ) |
---|
| 823 | return -1; |
---|
| 824 | |
---|
| 825 | if ( write(fh, buf, len) != len ) |
---|
| 826 | return -1; |
---|
| 827 | |
---|
| 828 | while ( (len = read(fh, buf, 1024)) ) { |
---|
| 829 | if ( len == 1024 ) { // overlong lion |
---|
| 830 | return -1; |
---|
| 831 | } else if ( len == 2 && buf[0] == '\r' && buf[1] == '\n' ) { |
---|
| 832 | break; |
---|
| 833 | } else if ( len == 1 && (buf[0] == '\r' || buf[0] == '\n') ) { // bad proxy or devel trying to debug ;) |
---|
| 834 | break; |
---|
| 835 | } else if ( len >= 4 && buf[len-4] == '\r' && buf[len-3] == '\n' && buf[len-2] == '\r' && buf[len-1] == '\n' ) { |
---|
| 836 | break; |
---|
| 837 | } |
---|
| 838 | } |
---|
| 839 | |
---|
| 840 | return 0; |
---|
[830] | 841 | } |
---|
| 842 | |
---|
[839] | 843 | |
---|
[1063] | 844 | #ifdef ROAR_HAVE_BIN_SSH |
---|
[839] | 845 | int roar_socket_open_ssh (int mode, int fh, char * host, int port, char * user, char * pw, char * opts) { |
---|
| 846 | char * proxy_addr = getenv("ssh_proxy"); |
---|
| 847 | char * sep; |
---|
| 848 | char cmd[1024] = {0}, rcmd[1024] = {0}; |
---|
| 849 | int proxy_port = 22; |
---|
[840] | 850 | int use_socat = 0; |
---|
[839] | 851 | int r; |
---|
| 852 | int socks[2]; |
---|
| 853 | |
---|
| 854 | if ( host == NULL ) |
---|
| 855 | return -1; |
---|
| 856 | |
---|
| 857 | if ( *host == '/' ) |
---|
[840] | 858 | use_socat = 1; |
---|
[839] | 859 | |
---|
| 860 | if ( mode == MODE_LISTEN ) |
---|
| 861 | return -1; |
---|
| 862 | |
---|
| 863 | if ( proxy_addr == NULL ) |
---|
| 864 | return -1; |
---|
| 865 | |
---|
[840] | 866 | if ( opts != NULL ) { |
---|
| 867 | if ( !strcmp(opts, "socat") ) { |
---|
| 868 | use_socat = 1; |
---|
| 869 | } else if ( !strcmp(opts, "netcat") ) { |
---|
| 870 | use_socat = 0; |
---|
| 871 | } else { |
---|
| 872 | return -1; |
---|
| 873 | } |
---|
| 874 | } |
---|
| 875 | |
---|
[1008] | 876 | ROAR_DBG("roar_socket_open_ssh(*): proxy_addr='%s'", proxy_addr); |
---|
| 877 | |
---|
[839] | 878 | if ( (sep = strstr(proxy_addr, "@")) != NULL ) |
---|
| 879 | proxy_addr = sep+1; |
---|
| 880 | |
---|
| 881 | if ( (sep = strstr(proxy_addr, ":")) != NULL ) { |
---|
| 882 | *sep = 0; |
---|
| 883 | proxy_port = atoi(sep+1); |
---|
| 884 | } |
---|
| 885 | |
---|
| 886 | |
---|
| 887 | if ( !strcmp(host, "+fork") ) { |
---|
[1068] | 888 | strncpy(rcmd, "roard --no-listen --client-fh 0", 32); |
---|
[839] | 889 | } else { |
---|
[840] | 890 | if ( use_socat ) { |
---|
| 891 | if ( *host == '/' ) { |
---|
| 892 | snprintf(rcmd, 1023, "socat stdio unix-connect:\"%s\"", host); |
---|
| 893 | } else { |
---|
| 894 | snprintf(rcmd, 1023, "socat stdio tcp:\"%s\":%i", host, port); |
---|
| 895 | } |
---|
| 896 | } else { |
---|
| 897 | snprintf(rcmd, 1023, "$(which netcat nc 2> /dev/null | grep -v \" \" | head -n 1) \"%s\" %i", host, port); |
---|
| 898 | } |
---|
| 899 | |
---|
[839] | 900 | rcmd[1023] = 0; |
---|
| 901 | } |
---|
| 902 | |
---|
[1008] | 903 | ROAR_DBG("roar_socket_open_ssh(*): proxy_port=%i, user='%s', proxy_addr='%s'", proxy_port, user, proxy_addr); |
---|
[1033] | 904 | ROAR_DBG("roar_socket_open_ssh(*): rcmd: %s", rcmd); |
---|
[1063] | 905 | snprintf(cmd, 1023, ROAR_HAVE_BIN_SSH " -p %i -l '%s' '%s' '%s'", proxy_port, user, proxy_addr, rcmd); |
---|
[839] | 906 | cmd[1023] = 0; |
---|
| 907 | |
---|
| 908 | |
---|
| 909 | if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) { |
---|
| 910 | return -1; |
---|
| 911 | } |
---|
| 912 | |
---|
| 913 | r = fork(); |
---|
| 914 | |
---|
| 915 | if ( r == -1 ) { // error! |
---|
| 916 | ROAR_ERR("roar_socket_open_ssh(*): Can not fork: %s", strerror(errno)); |
---|
| 917 | close(socks[0]); |
---|
| 918 | close(socks[1]); |
---|
| 919 | return -1; |
---|
| 920 | } else if ( r == 0 ) { // we are the child |
---|
| 921 | close(socks[0]); |
---|
| 922 | |
---|
| 923 | close(ROAR_STDIN ); // we do not want roard to have any standard input |
---|
| 924 | close(ROAR_STDOUT); // STDOUT is also not needed, so we close it, |
---|
| 925 | // but STDERR we keep open for error messages. |
---|
| 926 | |
---|
| 927 | dup2(socks[1], 0); |
---|
| 928 | dup2(socks[1], 1); |
---|
| 929 | |
---|
| 930 | execlp("sh", "sh", "-c", cmd, NULL); |
---|
| 931 | |
---|
| 932 | // we are still alive? |
---|
| 933 | ROAR_ERR("roar_socket_open_ssh(*): alive after exec(), that's bad!"); |
---|
| 934 | _exit(1); |
---|
| 935 | } else { // we are the parent |
---|
| 936 | close(socks[1]); |
---|
| 937 | return socks[0]; |
---|
| 938 | } |
---|
| 939 | return -1; |
---|
| 940 | } |
---|
[1063] | 941 | #endif |
---|
[839] | 942 | |
---|
[0] | 943 | //ll |
---|