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