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