[0] | 1 | //socket.c: |
---|
| 2 | |
---|
[690] | 3 | /* |
---|
[5381] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012 |
---|
[690] | 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 |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[690] | 23 | * |
---|
| 24 | * NOTE for everyone want's to change something and send patches: |
---|
| 25 | * read README and HACKING! There a addition information on |
---|
| 26 | * the license of this document you need to read before you send |
---|
| 27 | * any patches. |
---|
| 28 | * |
---|
| 29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 30 | * or libpulse*: |
---|
| 31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
| 32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 34 | */ |
---|
| 35 | |
---|
[5109] | 36 | #define _LIBROAR_NOATTR_TO_STATIC /* ignore warnings for TO_STATIC functions */ |
---|
[0] | 37 | #include "libroar.h" |
---|
| 38 | |
---|
[81] | 39 | #define MODE_LISTEN ROAR_SOCKET_MODE_LISTEN |
---|
| 40 | #define MODE_CONNECT ROAR_SOCKET_MODE_CONNECT |
---|
[0] | 41 | |
---|
[5375] | 42 | static int roar_socket_open_file (int mode, const char * host, int port); |
---|
[5527] | 43 | |
---|
| 44 | #ifdef ROAR_SUPPORT_PROXY |
---|
[5375] | 45 | static int roar_socket_open_proxy (int mode, int type, const char * host, int port, const char * proxy_type); |
---|
| 46 | |
---|
| 47 | static int roar_socket_open_socks4 (int mode, int fh, const char * host, int port, const char * user, const char * pw, const char * opts); |
---|
| 48 | static int roar_socket_open_socks4a(int mode, int fh, const char * host, int port, const char * user, const char * pw, const char * opts); |
---|
| 49 | static int roar_socket_open_socks4d(int mode, int fh, const char * host, int port, const char * user, const char * pw, const char * opts); |
---|
| 50 | static int roar_socket_open_socks4x(int mode, int fh, char host[4], int port, const char * app, size_t app_len, const char * user); |
---|
| 51 | |
---|
| 52 | static int roar_socket_open_http (int mode, int fh, const char * host, int port, const char * user, const char * pw, const char * opts); |
---|
| 53 | |
---|
[5528] | 54 | #ifdef ROAR_HAVE_BIN_SSH |
---|
[5375] | 55 | static int roar_socket_open_ssh (int mode, int fh, const char * host, int port, const char * user, const char * pw, const char * opts); |
---|
[5527] | 56 | #endif |
---|
| 57 | #endif |
---|
[5375] | 58 | |
---|
| 59 | |
---|
[1761] | 60 | #ifdef ROAR_TARGET_WIN32 |
---|
| 61 | void roar_socket_win32_init (void) { |
---|
| 62 | static int inited = 0; |
---|
| 63 | WSADATA wsadata; |
---|
| 64 | |
---|
| 65 | if ( !inited ) { |
---|
| 66 | WSAStartup(MAKEWORD(1,1) , &wsadata); |
---|
| 67 | inited++; |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | #else |
---|
| 71 | #define roar_socket_win32_init() |
---|
| 72 | #endif |
---|
| 73 | |
---|
[5527] | 74 | #ifdef ROAR_HAVE_LIBDNET |
---|
[5376] | 75 | static int roar_socket_decnet_set_timeout (int fh, time_t sec, int_least32_t usec) { |
---|
[873] | 76 | struct timeval timeout = {sec, usec}; |
---|
| 77 | |
---|
| 78 | return setsockopt(fh, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); |
---|
[5527] | 79 | } |
---|
[873] | 80 | #endif |
---|
| 81 | |
---|
[5375] | 82 | static int roar_socket_set_tos(int fh) { |
---|
[5210] | 83 | #if defined(ROAR_HAVE_BSDSOCKETS) && !defined(ROAR_TARGET_WIN32) |
---|
[5113] | 84 | int opt = IPTOS_LOWDELAY; |
---|
| 85 | int ret; |
---|
| 86 | |
---|
| 87 | roar_err_clear_errno(); |
---|
| 88 | ret = setsockopt(fh, IPPROTO_IP, IP_TOS, &opt, sizeof(int)); |
---|
| 89 | if ( ret < 0 ) |
---|
| 90 | roar_err_from_errno(); |
---|
| 91 | |
---|
| 92 | return ret; |
---|
| 93 | #else |
---|
[5375] | 94 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
[5113] | 95 | return -1; |
---|
| 96 | #endif |
---|
| 97 | } |
---|
| 98 | |
---|
[5248] | 99 | #if 0 |
---|
| 100 | static int roar_socket_new_decnet_seqpacket (void) { |
---|
[508] | 101 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 102 | int fh; |
---|
| 103 | |
---|
| 104 | fh = socket(AF_DECnet, SOCK_SEQPACKET, DNPROTO_NSP); |
---|
| 105 | |
---|
[873] | 106 | roar_socket_decnet_set_timeout(fh, 300, 0); |
---|
| 107 | |
---|
[508] | 108 | return fh; |
---|
| 109 | #else |
---|
| 110 | return -1; |
---|
| 111 | #endif |
---|
| 112 | } |
---|
[5248] | 113 | #endif |
---|
[508] | 114 | |
---|
| 115 | |
---|
[5248] | 116 | #if 0 |
---|
| 117 | static int roar_socket_new_ipxspx (void) { |
---|
| 118 | return -1; |
---|
| 119 | } |
---|
| 120 | #endif |
---|
| 121 | |
---|
| 122 | int roar_socket_new (int type) { |
---|
[5392] | 123 | #ifdef ROAR_HAVE_BSDSOCKETS |
---|
[5248] | 124 | int sys_domain = -1, sys_type = -1, sys_protocol = 0; |
---|
[508] | 125 | int fh; |
---|
[5248] | 126 | #if defined(ROAR_HAVE_IPV4) && defined(TCP_NODELAY) && !defined(ROAR_TARGET_WIN32) |
---|
| 127 | int t = 1; |
---|
| 128 | #endif |
---|
[508] | 129 | |
---|
[5248] | 130 | switch (type) { |
---|
| 131 | case ROAR_SOCKET_TYPE_NONE: |
---|
| 132 | case ROAR_SOCKET_TYPE_GENSTR: |
---|
| 133 | roar_err_set(ROAR_ERROR_INVAL); |
---|
| 134 | return -1; |
---|
| 135 | break; |
---|
| 136 | case ROAR_SOCKET_TYPE_FILE: |
---|
| 137 | case ROAR_SOCKET_TYPE_FORK: |
---|
| 138 | roar_err_set(ROAR_ERROR_PERM); |
---|
| 139 | return -1; |
---|
| 140 | break; |
---|
| 141 | #ifdef ROAR_HAVE_IPV4 |
---|
| 142 | case ROAR_SOCKET_TYPE_TCP: |
---|
| 143 | sys_domain = AF_INET; sys_type = SOCK_STREAM; |
---|
| 144 | break; |
---|
| 145 | case ROAR_SOCKET_TYPE_UDP: |
---|
| 146 | sys_domain = AF_INET; sys_type = SOCK_DGRAM; |
---|
| 147 | break; |
---|
| 148 | #endif |
---|
| 149 | #ifdef ROAR_HAVE_UNIX |
---|
| 150 | case ROAR_SOCKET_TYPE_UNIX: |
---|
| 151 | sys_domain = AF_UNIX; sys_type = SOCK_STREAM; |
---|
| 152 | break; |
---|
| 153 | #endif |
---|
| 154 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 155 | case ROAR_SOCKET_TYPE_DECNET: |
---|
| 156 | sys_domain = AF_DECnet; sys_type = SOCK_STREAM; sys_protocol = DNPROTO_NSP; |
---|
| 157 | break; |
---|
| 158 | #endif |
---|
| 159 | #ifdef ROAR_HAVE_IPV6 |
---|
| 160 | case ROAR_SOCKET_TYPE_TCP6: |
---|
| 161 | sys_domain = AF_INET6; sys_type = SOCK_STREAM; |
---|
| 162 | break; |
---|
| 163 | case ROAR_SOCKET_TYPE_UDP6: |
---|
| 164 | sys_domain = AF_INET6; sys_type = SOCK_DGRAM; |
---|
| 165 | break; |
---|
| 166 | #endif |
---|
| 167 | #ifdef ROAR_HAVE_IPX |
---|
| 168 | case ROAR_SOCKET_TYPE_IPX: |
---|
| 169 | sys_domain = AF_IPX; sys_type = SOCK_DGRAM; sys_protocol = AF_IPX; |
---|
| 170 | break; |
---|
| 171 | #endif |
---|
| 172 | case ROAR_SOCKET_TYPE_IPXSPX: |
---|
| 173 | case ROAR_SOCKET_TYPE_LAT_SERVICE: |
---|
| 174 | case ROAR_SOCKET_TYPE_LAT_REVERSE_PORT: |
---|
| 175 | default: |
---|
| 176 | roar_err_set(ROAR_ERROR_AFNOTSUP); |
---|
| 177 | return -1; |
---|
| 178 | break; |
---|
| 179 | } |
---|
[508] | 180 | |
---|
[5248] | 181 | roar_socket_win32_init(); |
---|
| 182 | |
---|
| 183 | roar_err_clear_all(); |
---|
| 184 | fh = socket(sys_domain, sys_type, sys_protocol); |
---|
| 185 | if ( fh == -1 ) { |
---|
| 186 | roar_err_update(); |
---|
| 187 | return -1; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | // do the extra work we need to do for some socket types: |
---|
| 191 | switch (type) { |
---|
| 192 | #ifdef ROAR_HAVE_IPV4 |
---|
| 193 | case ROAR_SOCKET_TYPE_TCP: |
---|
| 194 | #if defined(TCP_NODELAY) && !defined(ROAR_TARGET_WIN32) |
---|
| 195 | setsockopt(fh, IPPROTO_TCP, TCP_NODELAY, &t, sizeof(int)); |
---|
| 196 | #endif |
---|
| 197 | case ROAR_SOCKET_TYPE_UDP: |
---|
| 198 | #endif |
---|
| 199 | #ifdef ROAR_HAVE_IPV6 |
---|
| 200 | case ROAR_SOCKET_TYPE_TCP6: |
---|
| 201 | case ROAR_SOCKET_TYPE_UDP6: |
---|
| 202 | #endif |
---|
| 203 | #if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6) |
---|
| 204 | roar_socket_set_tos(fh); |
---|
| 205 | break; |
---|
| 206 | #endif |
---|
| 207 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 208 | case ROAR_SOCKET_TYPE_DECNET: |
---|
| 209 | roar_socket_decnet_set_timeout(fh, 300, 0); |
---|
| 210 | break; |
---|
| 211 | #endif |
---|
| 212 | } |
---|
[873] | 213 | |
---|
[508] | 214 | return fh; |
---|
[5392] | 215 | #else |
---|
| 216 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
| 217 | return -1; |
---|
| 218 | #endif |
---|
[530] | 219 | } |
---|
| 220 | |
---|
| 221 | |
---|
[0] | 222 | int roar_socket_nonblock(int fh, int state) { |
---|
[4554] | 223 | #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) && defined(ROAR_HAVE_FCNTL) |
---|
[0] | 224 | int flags; |
---|
| 225 | |
---|
| 226 | if ( (flags = fcntl(fh, F_GETFL, 0)) == -1 ) { |
---|
[5148] | 227 | roar_err_from_errno(); |
---|
[0] | 228 | ROAR_ERR("roar_socket_nonblock(fh=%i, state=%i): Can not read flags: %s", fh, state, strerror(errno)); |
---|
| 229 | ROAR_DBG("roar_socket_nonblock(fh=%i, state=%i) = -1", fh, state); |
---|
| 230 | return -1; |
---|
| 231 | } |
---|
| 232 | |
---|
[1119] | 233 | if ( !(flags & O_NONBLOCK) && state == ROAR_SOCKET_BLOCK ) |
---|
| 234 | return 0; |
---|
| 235 | |
---|
[0] | 236 | flags |= O_NONBLOCK; |
---|
| 237 | |
---|
| 238 | if ( state == ROAR_SOCKET_BLOCK ) |
---|
| 239 | flags -= O_NONBLOCK; |
---|
| 240 | |
---|
| 241 | if ( fcntl(fh, F_SETFL, flags) == -1 ) { |
---|
[5148] | 242 | roar_err_from_errno(); |
---|
[0] | 243 | ROAR_ERR("roar_socket_nonblock(fh=%i, state=%i): Can not set flags: %s", fh, state, strerror(errno)); |
---|
| 244 | ROAR_DBG("roar_socket_nonblock(fh=%i, state=%i) = -1", fh, state); |
---|
| 245 | return -1; |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | ROAR_DBG("roar_socket_nonblock(fh=%i, state=%i) = 0", fh, state); |
---|
| 249 | return 0; |
---|
[1085] | 250 | #else |
---|
| 251 | ROAR_WARN("roar_socket_nonblock(*): no nonblocking IO support on win32, use a real OS"); |
---|
| 252 | return -1; |
---|
| 253 | #endif |
---|
[0] | 254 | } |
---|
| 255 | |
---|
[375] | 256 | int roar_socket_dup_udp_local_end (int fh) { |
---|
[4554] | 257 | #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) && defined(ROAR_HAVE_FCNTL) |
---|
[376] | 258 | int n = -1; |
---|
| 259 | int flags = -1; |
---|
| 260 | struct sockaddr_in socket_addr; |
---|
| 261 | socklen_t len = sizeof(struct sockaddr_in); |
---|
| 262 | |
---|
| 263 | if ( (flags = fcntl(fh, F_GETFL, 0)) == -1 ) { |
---|
| 264 | ROAR_WARN("roar_socket_dup_udp_local_end(fh=%i): Can not read flags: %s", fh, strerror(errno)); |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | if ( getsockname(fh, (struct sockaddr *)&socket_addr, &len) == -1 ) { |
---|
[5148] | 268 | roar_err_from_errno(); |
---|
[376] | 269 | return -1; |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | if ( socket_addr.sin_family != AF_INET ) { |
---|
[5148] | 273 | roar_err_set(ROAR_ERROR_TYPEMM); |
---|
[376] | 274 | return -1; |
---|
| 275 | } |
---|
| 276 | |
---|
[5248] | 277 | n = roar_socket_new(ROAR_SOCKET_TYPE_UDP); |
---|
[376] | 278 | |
---|
| 279 | if ( n == -1 ) |
---|
| 280 | return -1; |
---|
| 281 | |
---|
| 282 | // if ( mode_func(fh, (struct sockaddr *)&socket_addr, sizeof(struct sockaddr_in)) == -1 ) { |
---|
| 283 | if ( bind(n, (struct sockaddr *)&socket_addr, len) == -1 ) { |
---|
[5148] | 284 | roar_err_from_errno(); |
---|
[376] | 285 | close(n); |
---|
| 286 | return -1; |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | if ( flags != -1 ) { |
---|
| 290 | if ( fcntl(fh, F_SETFL, flags) == -1 ) { |
---|
| 291 | ROAR_WARN("roar_socket_dup_udp_local_end(fh=%i): Can not set flags: %s", fh, strerror(errno)); |
---|
| 292 | return -1; |
---|
| 293 | } |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | |
---|
| 297 | return n; |
---|
[1085] | 298 | #else |
---|
| 299 | ROAR_WARN("roar_socket_dup_udp_local_end(*): this function is not supported on win32, use a real OS"); |
---|
| 300 | return -1; |
---|
| 301 | #endif |
---|
[375] | 302 | } |
---|
| 303 | |
---|
[753] | 304 | |
---|
| 305 | #define _SCMR_CONTROLLEN (sizeof(struct cmsghdr) + sizeof(int)) |
---|
| 306 | int roar_socket_send_fh (int sock, int fh, char * mes, size_t len) { |
---|
[4554] | 307 | #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_OS_SUNOS) && !defined(ROAR_TARGET_OPENVMS) |
---|
[753] | 308 | struct iovec iov[1]; |
---|
| 309 | struct msghdr msg; |
---|
| 310 | char cmptr_buf[_SCMR_CONTROLLEN]; |
---|
| 311 | struct cmsghdr * cmptr = (struct cmsghdr *) cmptr_buf; |
---|
[754] | 312 | char localmes[1] = {0}; |
---|
[753] | 313 | |
---|
[759] | 314 | ROAR_DBG("roar_socket_send_fh(sock=%i, fh=%i, mes=%p, len=%u) = ?", sock, fh, mes, len); |
---|
| 315 | |
---|
[754] | 316 | if ( sock < 0 || fh < 0 ) |
---|
[753] | 317 | return -1; |
---|
| 318 | |
---|
[754] | 319 | if ( len == 0 ) { |
---|
| 320 | len = 1; |
---|
| 321 | mes = localmes; |
---|
| 322 | } |
---|
| 323 | |
---|
[5394] | 324 | memset(&msg, 0, sizeof(msg)); |
---|
[754] | 325 | memset(cmptr, 0, _SCMR_CONTROLLEN); |
---|
| 326 | |
---|
[753] | 327 | iov[0].iov_base = mes; |
---|
| 328 | iov[0].iov_len = len; |
---|
| 329 | msg.msg_iov = iov; |
---|
| 330 | msg.msg_iovlen = 1; |
---|
| 331 | msg.msg_name = NULL; |
---|
| 332 | msg.msg_namelen = 0; |
---|
| 333 | |
---|
| 334 | cmptr->cmsg_level = SOL_SOCKET; |
---|
| 335 | cmptr->cmsg_type = SCM_RIGHTS; |
---|
| 336 | cmptr->cmsg_len = _SCMR_CONTROLLEN; |
---|
| 337 | msg.msg_control = (caddr_t) cmptr; |
---|
| 338 | msg.msg_controllen = _SCMR_CONTROLLEN; |
---|
| 339 | *(int *)CMSG_DATA(cmptr) = fh; |
---|
| 340 | |
---|
| 341 | return sendmsg(sock, &msg, 0); |
---|
[1085] | 342 | #else |
---|
| 343 | ROAR_ERR("roar_socket_send_fh(*): There is no UNIX Domain Socket support in win32, download a real OS."); |
---|
| 344 | return -1; |
---|
| 345 | #endif |
---|
[753] | 346 | } |
---|
| 347 | |
---|
| 348 | int roar_socket_recv_fh (int sock, char * mes, size_t * len) { |
---|
[4554] | 349 | #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_OS_SUNOS) && !defined(ROAR_TARGET_OPENVMS) |
---|
[753] | 350 | struct iovec iov[1]; |
---|
| 351 | struct msghdr msg; |
---|
| 352 | char cmptr_buf[_SCMR_CONTROLLEN]; |
---|
| 353 | struct cmsghdr * cmptr = (struct cmsghdr *) cmptr_buf; |
---|
[754] | 354 | char localmes[1]; |
---|
| 355 | size_t locallen[1] = {1}; |
---|
[753] | 356 | |
---|
[4256] | 357 | ROAR_DBG("roar_socket_recv_fh(sock=%i, mes=%p, len=%p) = ?", sock, mes, len); |
---|
| 358 | |
---|
| 359 | if ( sock < 0 ) { |
---|
| 360 | ROAR_DBG("roar_socket_recv_fh(sock=%i, mes=%p, len=%p) = -1 // invalid socket", sock, mes, len); |
---|
[753] | 361 | return -1; |
---|
[4256] | 362 | } |
---|
[753] | 363 | |
---|
[754] | 364 | if ( len == NULL ) { |
---|
| 365 | len = locallen; |
---|
| 366 | mes = localmes; |
---|
| 367 | } |
---|
| 368 | |
---|
[753] | 369 | iov[0].iov_base = mes; |
---|
| 370 | iov[0].iov_len = *len; |
---|
| 371 | msg.msg_iov = iov; |
---|
| 372 | msg.msg_iovlen = 1; |
---|
| 373 | msg.msg_name = NULL; |
---|
| 374 | msg.msg_namelen = 0; |
---|
| 375 | |
---|
| 376 | msg.msg_control = (caddr_t) cmptr; |
---|
| 377 | msg.msg_controllen = _SCMR_CONTROLLEN; |
---|
| 378 | |
---|
[4256] | 379 | if ( (*len = recvmsg(sock, &msg, 0)) == -1 ) { |
---|
| 380 | ROAR_DBG("roar_socket_recv_fh(sock=%i, mes=%p, len=%p) = -1 // can not read from socket", sock, mes, len); |
---|
[753] | 381 | return -1; |
---|
[4256] | 382 | } |
---|
[753] | 383 | |
---|
[4257] | 384 | if ( msg.msg_controllen < _SCMR_CONTROLLEN || |
---|
| 385 | cmptr->cmsg_len != _SCMR_CONTROLLEN ) { |
---|
[4256] | 386 | ROAR_DBG("roar_socket_recv_fh(sock=%i, mes=%p, len=%p) = -1 // control len is wrong", sock, mes, len); |
---|
[753] | 387 | return -1; |
---|
[4256] | 388 | } |
---|
[753] | 389 | |
---|
[4256] | 390 | ROAR_DBG("roar_socket_recv_fh(sock=%i, mes=%p, len=%p) = %i", sock, mes, len, *(int *)CMSG_DATA(cmptr)); |
---|
[753] | 391 | return *(int *)CMSG_DATA(cmptr); |
---|
[1085] | 392 | #else |
---|
| 393 | ROAR_ERR("roar_socket_recv_fh(*): There is no UNIX Domain Socket support in win32, download a real OS."); |
---|
| 394 | return -1; |
---|
| 395 | #endif |
---|
[753] | 396 | } |
---|
| 397 | |
---|
[5260] | 398 | int roar_socket_listen (int type, const char * host, int port) { |
---|
[0] | 399 | return roar_socket_open(MODE_LISTEN, type, host, port); |
---|
| 400 | } |
---|
| 401 | |
---|
[5375] | 402 | int roar_socket_connect (int type, const char * host, int port) { |
---|
[5754] | 403 | const char * proxy_type = roar_env_get("ROAR_PROXY"); |
---|
[2] | 404 | |
---|
[4692] | 405 | ROAR_DBG("roar_socket_connect(host='%s', port=%i) = ?", host, port); |
---|
| 406 | |
---|
[2] | 407 | if ( proxy_type == NULL || strcmp(proxy_type, "") == 0 ) { |
---|
[5375] | 408 | return roar_socket_open(MODE_CONNECT, type, host, port); |
---|
[2] | 409 | } else { |
---|
[1087] | 410 | #ifdef ROAR_SUPPORT_PROXY |
---|
[5375] | 411 | return roar_socket_open_proxy(MODE_CONNECT, type, host, port, proxy_type); |
---|
[1087] | 412 | #else |
---|
| 413 | ROAR_ERR("roar_socket_connect(host='%s', port=%i): no support for proxy code (proxy_type=%s)", host, port, proxy_type); |
---|
| 414 | return -1; |
---|
| 415 | #endif |
---|
[2] | 416 | } |
---|
[0] | 417 | } |
---|
| 418 | |
---|
[508] | 419 | |
---|
[5527] | 420 | #ifdef ROAR_HAVE_LIBDNET |
---|
[5376] | 421 | static int roar_socket_listen_decnet (const char * object, int num) { |
---|
[5248] | 422 | int fh = roar_socket_new(ROAR_SOCKET_TYPE_DECNET); |
---|
[508] | 423 | struct sockaddr_dn bind_sockaddr; |
---|
| 424 | |
---|
| 425 | if ( fh == -1 ) |
---|
| 426 | return -1; |
---|
| 427 | |
---|
[5376] | 428 | if ( object != NULL && !*object ) |
---|
[508] | 429 | object = NULL; |
---|
| 430 | |
---|
[5376] | 431 | if ( (object != NULL && num) || (object != NULL && !*object && !num) || (object == NULL && !num) ) { |
---|
[508] | 432 | ROAR_WARN("roar_socket_listen_decnet(object='%s', num=%i): illegal address!", object, num); |
---|
| 433 | close(fh); |
---|
[5376] | 434 | roar_err_set(ROAR_ERROR_INVAL); |
---|
[508] | 435 | return -1; |
---|
| 436 | } |
---|
| 437 | |
---|
| 438 | memset((void*)&bind_sockaddr, 0, sizeof(struct sockaddr_dn)); |
---|
| 439 | |
---|
| 440 | bind_sockaddr.sdn_family = AF_DECnet; |
---|
| 441 | bind_sockaddr.sdn_flags = 0; |
---|
| 442 | bind_sockaddr.sdn_objnum = num; |
---|
| 443 | |
---|
| 444 | if ( num ) { |
---|
| 445 | bind_sockaddr.sdn_objnamel = 0; |
---|
| 446 | } else { |
---|
[4073] | 447 | bind_sockaddr.sdn_objnamel = ROAR_HOST2LE16(strlen(object)); |
---|
[1068] | 448 | if ( bind_sockaddr.sdn_objnamel > DN_MAXOBJL ) |
---|
| 449 | bind_sockaddr.sdn_objnamel = DN_MAXOBJL; |
---|
| 450 | strncpy((char*)bind_sockaddr.sdn_objname, object, DN_MAXOBJL); |
---|
[508] | 451 | } |
---|
| 452 | |
---|
| 453 | if ( bind(fh, (struct sockaddr *) &bind_sockaddr, sizeof(bind_sockaddr)) == -1 ) { |
---|
[5376] | 454 | roar_err_from_errno(); |
---|
[508] | 455 | close(fh); |
---|
| 456 | return -1; |
---|
| 457 | } |
---|
| 458 | |
---|
| 459 | if ( listen(fh, 8) == -1 ) { |
---|
[5376] | 460 | roar_err_from_errno(); |
---|
[508] | 461 | close(fh); |
---|
| 462 | return -1; |
---|
| 463 | } |
---|
| 464 | |
---|
| 465 | return fh; |
---|
[5527] | 466 | } |
---|
[508] | 467 | #endif |
---|
| 468 | |
---|
[5121] | 469 | const char * roar_socket_get_local_nodename(void) { |
---|
[521] | 470 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 471 | static char node[16] = {0}; |
---|
| 472 | struct dn_naddr *binaddr; |
---|
| 473 | struct nodeent *dp; |
---|
| 474 | |
---|
| 475 | if ( !node[0] ) { |
---|
[5376] | 476 | |
---|
| 477 | // TODO: This does this function not use getnodename()? |
---|
| 478 | |
---|
| 479 | // Those strange workarounds for the error codes are because those functions currently |
---|
| 480 | // don't set errno. This way we will use errno as soon as it starts to set it. |
---|
[521] | 481 | |
---|
[5376] | 482 | roar_err_update(); |
---|
| 483 | if ( (binaddr=getnodeadd()) == NULL) { |
---|
| 484 | roar_err_update(); |
---|
| 485 | if ( roar_error == ROAR_ERROR_NONE ) |
---|
| 486 | roar_err_set(ROAR_ERROR_NOENT); |
---|
[521] | 487 | return NULL; |
---|
[5376] | 488 | } |
---|
| 489 | |
---|
| 490 | roar_err_update(); |
---|
| 491 | if ( (dp=getnodebyaddr((char*)binaddr->a_addr, binaddr->a_len, AF_DECnet)) == NULL ) { |
---|
| 492 | roar_err_update(); |
---|
| 493 | if ( roar_error == ROAR_ERROR_NONE ) |
---|
| 494 | roar_err_set(ROAR_ERROR_NOENT); |
---|
| 495 | return NULL; |
---|
| 496 | } |
---|
[521] | 497 | |
---|
| 498 | strncpy(node, dp->n_name, 15); |
---|
| 499 | node[15] = 0; |
---|
| 500 | } |
---|
| 501 | |
---|
| 502 | return node; |
---|
| 503 | #else |
---|
[5376] | 504 | roar_err_set(ROAR_ERROR_AFNOTSUP); |
---|
[521] | 505 | return NULL; |
---|
| 506 | #endif |
---|
| 507 | } |
---|
| 508 | |
---|
[5260] | 509 | int roar_socket_open (int mode, int type, const char * host, int port) { |
---|
[0] | 510 | // int type = ROAR_SOCKET_TYPE_INET; |
---|
| 511 | int fh; |
---|
[531] | 512 | #ifdef ROAR_HAVE_IPX |
---|
| 513 | #define _NEED_OBJ |
---|
[5184] | 514 | #endif |
---|
| 515 | #if defined(ROAR_HAVE_IPX) || defined(ROAR_HAVE_GETADDRINFO) |
---|
[531] | 516 | int ret; |
---|
| 517 | #endif |
---|
[3788] | 518 | #ifdef ROAR_HAVE_IPX |
---|
| 519 | unsigned int ipx_port; |
---|
| 520 | #endif |
---|
[3643] | 521 | #ifdef ROAR_HAVE_UNIX |
---|
| 522 | int abstract = 0; |
---|
| 523 | #endif |
---|
[1476] | 524 | #if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6) || defined(ROAR_HAVE_UNIX) || defined(ROAR_HAVE_IPX) |
---|
[512] | 525 | union { |
---|
[1359] | 526 | struct sockaddr sa; |
---|
[1361] | 527 | #if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6) |
---|
[512] | 528 | struct sockaddr_in in; |
---|
[1359] | 529 | #endif |
---|
[1090] | 530 | #ifdef ROAR_HAVE_UNIX |
---|
[512] | 531 | struct sockaddr_un un; |
---|
[1090] | 532 | #endif |
---|
[890] | 533 | #ifdef ROAR_HAVE_IPV6 |
---|
[514] | 534 | struct sockaddr_in6 in6; |
---|
[890] | 535 | #endif |
---|
[531] | 536 | #ifdef ROAR_HAVE_IPX |
---|
| 537 | struct sockaddr_ipx ipx; |
---|
| 538 | #endif |
---|
[512] | 539 | } socket_addr; |
---|
[4759] | 540 | socklen_t addrlen; |
---|
[1476] | 541 | #endif |
---|
[5419] | 542 | #if (defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6)) && !defined(ROAR_HAVE_GETADDRINFO) |
---|
[5379] | 543 | struct hostent * he = NULL; |
---|
[1359] | 544 | #endif |
---|
[0] | 545 | //unsigned int host_div = 0; |
---|
[3788] | 546 | #ifdef ROAR_TARGET_WIN32 |
---|
| 547 | int PASCAL (*mode_func)(SOCKET,const struct sockaddr*,int) = connect; // default is to connect |
---|
| 548 | #else |
---|
[0] | 549 | int (*mode_func)(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen) = connect; // default is to connect |
---|
[3788] | 550 | #endif |
---|
[501] | 551 | #ifdef ROAR_HAVE_LIBDNET |
---|
[531] | 552 | #define _NEED_OBJ |
---|
[5260] | 553 | char * dnet_node_buf; |
---|
[531] | 554 | #endif |
---|
| 555 | #ifdef _NEED_OBJ |
---|
[501] | 556 | char obj[80]; |
---|
| 557 | char * del; |
---|
| 558 | #endif |
---|
[5745] | 559 | #ifdef ROAR_HAVE_GETADDRINFO |
---|
[4759] | 560 | int af_guessed = 0; |
---|
| 561 | struct addrinfo hints, *res = NULL; |
---|
| 562 | char port_as_string[32]; |
---|
| 563 | #endif |
---|
[5098] | 564 | #if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6) |
---|
| 565 | int is_numerical = 1; |
---|
| 566 | const char * numptr; |
---|
| 567 | #endif |
---|
[0] | 568 | |
---|
[4692] | 569 | ROAR_DBG("roar_socket_open(mode=%i, type=%i, host='%s', port=%i) = ?", mode, type, host, port); |
---|
| 570 | |
---|
[5714] | 571 | if ( host == NULL ) { |
---|
| 572 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 573 | return -1; |
---|
| 574 | } |
---|
| 575 | |
---|
[5184] | 576 | roar_err_set(ROAR_ERROR_UNKNOWN); |
---|
| 577 | |
---|
[0] | 578 | if ( mode == MODE_LISTEN ) |
---|
| 579 | mode_func = bind; |
---|
| 580 | |
---|
| 581 | if ( type == ROAR_SOCKET_TYPE_UNKNOWN ) { |
---|
[5745] | 582 | #ifdef ROAR_HAVE_GETADDRINFO |
---|
[4759] | 583 | af_guessed = 1; |
---|
[5745] | 584 | #endif |
---|
[0] | 585 | type = ROAR_SOCKET_TYPE_INET; |
---|
[69] | 586 | if ( *host == '/' ) { |
---|
[0] | 587 | type = ROAR_SOCKET_TYPE_UNIX; |
---|
[69] | 588 | } else if ( strcmp(host, "+fork") == 0 ) { |
---|
| 589 | type = ROAR_SOCKET_TYPE_FORK; |
---|
[3643] | 590 | } else if ( strcmp(host, "+abstract") == 0 ) { |
---|
| 591 | type = ROAR_SOCKET_TYPE_UNIX; |
---|
[3785] | 592 | #ifdef ROAR_HAVE_UNIX |
---|
[3643] | 593 | abstract = 1; |
---|
[3785] | 594 | #endif |
---|
[501] | 595 | } else if ( strstr(host, "::") != NULL ) { |
---|
| 596 | type = ROAR_SOCKET_TYPE_DECNET; |
---|
[531] | 597 | } else if ( host[strlen(host)-1] == ')' ) { |
---|
| 598 | type = ROAR_SOCKET_TYPE_IPX; |
---|
[69] | 599 | } |
---|
[0] | 600 | } |
---|
| 601 | |
---|
| 602 | |
---|
[4692] | 603 | ROAR_DBG("roar_socket_open(mode=%i, type=%i, host='%s', port=%i) = ?", mode, type, host, port); |
---|
| 604 | |
---|
[0] | 605 | ROAR_DBG("roar_socket_open(*): type=%s, host='%s', port=%i", |
---|
[3956] | 606 | type == ROAR_SOCKET_TYPE_UNIX ? "UNIX" : "???", host, port); |
---|
[0] | 607 | |
---|
[501] | 608 | if ( type == ROAR_SOCKET_TYPE_DECNET ) { |
---|
| 609 | #ifdef ROAR_HAVE_LIBDNET |
---|
[5687] | 610 | ROAR_DBG("roar_socket_open(*): nodename for DECnet: host(%p)=%s", host, host); |
---|
| 611 | dnet_node_buf = roar_mm_strdup(host); |
---|
| 612 | // roar_error is still set. |
---|
| 613 | if ( dnet_node_buf == NULL ) |
---|
| 614 | return -1; |
---|
| 615 | |
---|
| 616 | host = dnet_node_buf; |
---|
| 617 | |
---|
[501] | 618 | del = strstr(host, "::"); |
---|
[5687] | 619 | ROAR_DBG("roar_socket_open(*): nodename for DECnet: del(%p)=%s", del, del); |
---|
[508] | 620 | |
---|
| 621 | if ( del == NULL ) { |
---|
[5687] | 622 | ROAR_WARN("roar_socket_open(*): invalid nodename for DECnet: %s", host); |
---|
| 623 | roar_mm_free(dnet_node_buf); |
---|
[5184] | 624 | roar_err_set(ROAR_ERROR_INVAL); |
---|
[508] | 625 | return -1; |
---|
| 626 | } |
---|
| 627 | |
---|
[501] | 628 | *del = 0; |
---|
| 629 | |
---|
| 630 | if ( *(del+2) == '#' ) { // assume we have node::#num |
---|
| 631 | port = atoi(del+2); |
---|
| 632 | } |
---|
| 633 | |
---|
| 634 | if ( port ) { |
---|
[1067] | 635 | snprintf(obj, 7, "%i", port); // no need for snprintf() as dec(port) is smaller than obj[] |
---|
[501] | 636 | } else { |
---|
| 637 | *obj = 0; |
---|
| 638 | strncat(obj, del+2, 79); |
---|
| 639 | } |
---|
| 640 | |
---|
[5137] | 641 | ROAR_DBG("roar_socket_open(*): obj='%s', port=%i", obj, port); |
---|
| 642 | |
---|
[508] | 643 | if ( mode == MODE_LISTEN ) { |
---|
| 644 | fh = roar_socket_listen_decnet(obj, port); |
---|
[5687] | 645 | roar_mm_free(dnet_node_buf); |
---|
[508] | 646 | return fh; |
---|
| 647 | } else { |
---|
| 648 | // There is nothing wrong in this case to use dnet_conn() so we do. |
---|
[5326] | 649 | ROAR_DBG("roar_socket_open(*): CALL dnet_conn('%s', '%s', SOCK_STREAM, 0 ,0 ,0 , 0)", dnet_node_buf, obj); |
---|
[5261] | 650 | fh = dnet_conn(dnet_node_buf, obj, SOCK_STREAM, 0, 0, 0, 0); |
---|
[5326] | 651 | ROAR_DBG("roar_socket_open(*): RET %i", fh); |
---|
[5260] | 652 | roar_mm_free(dnet_node_buf); |
---|
[501] | 653 | return fh; |
---|
[533] | 654 | } |
---|
[501] | 655 | #else |
---|
[5184] | 656 | roar_err_set(ROAR_ERROR_AFNOTSUP); |
---|
[533] | 657 | return -1; // no decnet support |
---|
[501] | 658 | #endif |
---|
| 659 | } |
---|
| 660 | |
---|
[1359] | 661 | #if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6) |
---|
[512] | 662 | memset(&socket_addr, 0, sizeof(socket_addr)); |
---|
[1359] | 663 | #endif |
---|
[0] | 664 | |
---|
| 665 | |
---|
[520] | 666 | if ( type == ROAR_SOCKET_TYPE_INET || type == ROAR_SOCKET_TYPE_INET6 ) { |
---|
[1359] | 667 | #if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6) |
---|
[0] | 668 | |
---|
[3956] | 669 | ROAR_DBG("roar_socket_open(*): type=INET|INET6, host='%s', port=%i", host, port); |
---|
[2039] | 670 | |
---|
[1768] | 671 | roar_socket_win32_init(); // we need to do this early as gethostbyname() requires this. |
---|
| 672 | |
---|
[3956] | 673 | ROAR_DBG("roar_socket_open(*): type=INET|INET6, host='%s', port=%i", host, port); |
---|
| 674 | |
---|
[5098] | 675 | if ( !!strcmp(host, "0.0.0.0") ) { |
---|
| 676 | for (numptr = host; is_numerical && *numptr != 0; numptr++) |
---|
| 677 | if ( ! ((*numptr >= '0' && *numptr <= '9') || *numptr == '.') ) |
---|
| 678 | is_numerical = 0; |
---|
| 679 | |
---|
[5138] | 680 | if ( is_numerical && roar_libroar_iswarn(NULL) ) { |
---|
[5098] | 681 | ROAR_WARN("roar_socket_open(*): Hostname \"%s\" is numerical. Do not use IP addresses directly. Use Hostnames.", host); |
---|
| 682 | } |
---|
| 683 | } |
---|
| 684 | |
---|
[4759] | 685 | #ifdef ROAR_HAVE_GETADDRINFO |
---|
| 686 | memset(&hints, 0, sizeof(hints)); |
---|
| 687 | hints.ai_socktype = SOCK_STREAM; |
---|
| 688 | if ( af_guessed ) { |
---|
| 689 | hints.ai_family = AF_UNSPEC; |
---|
| 690 | } else { |
---|
| 691 | hints.ai_family = type == ROAR_SOCKET_TYPE_INET ? AF_INET : AF_INET6; |
---|
| 692 | } |
---|
| 693 | |
---|
| 694 | snprintf(port_as_string, sizeof(port_as_string), "%i", port); |
---|
| 695 | |
---|
[5184] | 696 | ret = getaddrinfo(host, port_as_string, &hints, &res); |
---|
[5554] | 697 | if ( ret == EAI_SYSTEM ) { |
---|
| 698 | roar_err_from_errno(); |
---|
| 699 | return -1; |
---|
| 700 | } else if ( ret != 0 ) { |
---|
| 701 | roar_err_convert(&roar_error, ROAR_ERROR_TYPE_ROARAUDIO, ret, ROAR_ERROR_TYPE_EAI); |
---|
| 702 | return -1; |
---|
[5184] | 703 | } |
---|
[4759] | 704 | |
---|
| 705 | if ( af_guessed ) { |
---|
| 706 | type = res->ai_family == AF_INET ? ROAR_SOCKET_TYPE_INET : ROAR_SOCKET_TYPE_INET6; |
---|
| 707 | } |
---|
| 708 | |
---|
| 709 | if ( type == ROAR_SOCKET_TYPE_INET ) { |
---|
[5248] | 710 | fh = roar_socket_new(ROAR_SOCKET_TYPE_TCP); |
---|
[4759] | 711 | } else { |
---|
[5248] | 712 | fh = roar_socket_new(ROAR_SOCKET_TYPE_TCP6); |
---|
[4759] | 713 | } |
---|
| 714 | |
---|
| 715 | memcpy(&(socket_addr.sa), res->ai_addr, res->ai_addrlen); |
---|
| 716 | addrlen = res->ai_addrlen; |
---|
| 717 | |
---|
| 718 | if ( res != NULL ) |
---|
| 719 | freeaddrinfo(res); |
---|
| 720 | #else |
---|
[0] | 721 | if ( (he = gethostbyname(host)) == NULL ) { |
---|
| 722 | ROAR_ERR("roar_socket_open(*): Can\'t resolve host name '%s'", |
---|
| 723 | host); |
---|
[5184] | 724 | roar_err_from_errno(); |
---|
[0] | 725 | return -1; |
---|
| 726 | } |
---|
| 727 | |
---|
[4692] | 728 | ROAR_DBG("roar_socket_open(*): he=%p", he); |
---|
| 729 | |
---|
[4759] | 730 | memcpy((struct in_addr *)&(socket_addr.in.sin_addr), he->h_addr, sizeof(struct in_addr)); |
---|
[0] | 731 | |
---|
[520] | 732 | /* set the connect information */ |
---|
| 733 | socket_addr.in.sin_family = AF_INET; |
---|
| 734 | socket_addr.in.sin_port = ROAR_HOST2NET16(port); |
---|
| 735 | |
---|
[5248] | 736 | fh = roar_socket_new(ROAR_SOCKET_TYPE_TCP); |
---|
[4759] | 737 | addrlen = sizeof(struct sockaddr_in); |
---|
| 738 | #endif |
---|
[2039] | 739 | |
---|
[4692] | 740 | ROAR_DBG("roar_socket_open(*): fh=%i", fh); |
---|
| 741 | |
---|
[2039] | 742 | if ( fh == -1 ) { |
---|
| 743 | ROAR_ERR("roar_socket_open(*): Can\'t create TCP socket: %s", strerror(errno)); |
---|
| 744 | return -1; |
---|
| 745 | } |
---|
| 746 | |
---|
| 747 | ROAR_DBG("roar_socket_open(*) = ?"); |
---|
[0] | 748 | |
---|
[4692] | 749 | ROAR_DBG("roar_socket_open(*): fh=%i", fh); |
---|
| 750 | |
---|
[4759] | 751 | if ( mode_func(fh, (struct sockaddr *)&socket_addr.sa, addrlen) == -1 ) { |
---|
[520] | 752 | ROAR_DBG("roar_socket_open(*): Can not connect/bind: %s", strerror(errno)); |
---|
[5184] | 753 | roar_err_from_errno(); |
---|
[520] | 754 | close(fh); |
---|
| 755 | return -1; |
---|
| 756 | } |
---|
[4692] | 757 | |
---|
| 758 | ROAR_DBG("roar_socket_open(*): fh=%i", fh); |
---|
| 759 | |
---|
[0] | 760 | // hey! we have a socket... |
---|
[2040] | 761 | ROAR_DBG("roar_socket_open(*) = ? // we have a socket :)"); |
---|
[1359] | 762 | #else |
---|
[4692] | 763 | ROAR_DBG("roar_socket_open(*) = -1 // no IPv4 or IPv6 support"); |
---|
[5184] | 764 | roar_err_set(ROAR_ERROR_AFNOTSUP); |
---|
[1359] | 765 | return -1; |
---|
| 766 | #endif |
---|
[69] | 767 | } else if ( type == ROAR_SOCKET_TYPE_UNIX ) { |
---|
[1090] | 768 | #ifdef ROAR_HAVE_UNIX |
---|
[512] | 769 | socket_addr.un.sun_family = AF_UNIX; |
---|
[3643] | 770 | |
---|
| 771 | if ( abstract ) { |
---|
| 772 | memset(socket_addr.un.sun_path, 0, sizeof(socket_addr.un.sun_path)); |
---|
| 773 | snprintf(socket_addr.un.sun_path+1, sizeof(socket_addr.un.sun_path)-1, "RoarAudio/UNIX/Abstract/%i", abstract); |
---|
| 774 | } else { |
---|
| 775 | strncpy(socket_addr.un.sun_path, host, sizeof(socket_addr.un.sun_path) - 1); |
---|
| 776 | } |
---|
[60] | 777 | |
---|
[5248] | 778 | fh = roar_socket_new(ROAR_SOCKET_TYPE_UNIX); |
---|
[60] | 779 | |
---|
[512] | 780 | if ( mode_func(fh, (struct sockaddr *)&socket_addr.un, sizeof(struct sockaddr_un)) == -1 ) { |
---|
[60] | 781 | ROAR_DBG("roar_socket_open(*): Can not connect/bind: %s", strerror(errno)); |
---|
[5184] | 782 | roar_err_from_errno(); |
---|
[60] | 783 | close(fh); |
---|
| 784 | return -1; |
---|
| 785 | } |
---|
[1090] | 786 | #else |
---|
| 787 | ROAR_ERR("roar_socket_open(*): There is no UNIX Domain Socket support in win32, download a real OS."); |
---|
[5184] | 788 | roar_err_set(ROAR_ERROR_AFNOTSUP); |
---|
[1090] | 789 | return -1; |
---|
| 790 | #endif |
---|
[531] | 791 | } else if ( type == ROAR_SOCKET_TYPE_IPX ) { |
---|
[534] | 792 | #ifdef ROAR_HAVE_IPX |
---|
[531] | 793 | socket_addr.ipx.sipx_family = AF_IPX; |
---|
| 794 | |
---|
| 795 | obj[0] = 0; |
---|
| 796 | |
---|
[3788] | 797 | if ( (ret = sscanf(host, "%8x.%12s(%x)", &socket_addr.ipx.sipx_network, obj, &ipx_port)) < 2 ) { |
---|
[531] | 798 | return -1; |
---|
[3788] | 799 | socket_addr.ipx.sipx_port = ipx_port; |
---|
[531] | 800 | } else if ( ret == 2 ) { |
---|
| 801 | socket_addr.ipx.sipx_port = port; // Network Byte Order? |
---|
| 802 | } |
---|
| 803 | |
---|
| 804 | memset(socket_addr.ipx.sipx_node, 0, IPX_NODE_LEN); |
---|
| 805 | ret = strlen(obj); |
---|
| 806 | |
---|
| 807 | if ( ret % 2 ) // needs to be even at the moment |
---|
| 808 | return -1; |
---|
| 809 | |
---|
[5248] | 810 | fh = roar_socket_new(ROAR_SOCKET_TYPE_IPX); |
---|
[531] | 811 | |
---|
| 812 | close(fh); |
---|
[5248] | 813 | roar_err_set(ROAR_ERROR_AFNOTSUP); |
---|
[531] | 814 | return -1; |
---|
[534] | 815 | #else |
---|
[5184] | 816 | roar_err_set(ROAR_ERROR_AFNOTSUP); |
---|
[534] | 817 | return -1; |
---|
| 818 | #endif |
---|
[69] | 819 | } else if ( type == ROAR_SOCKET_TYPE_FORK ) { |
---|
[5369] | 820 | roar_err_set(ROAR_ERROR_INVAL); |
---|
| 821 | return -1; |
---|
[75] | 822 | } else if ( type == ROAR_SOCKET_TYPE_FILE ) { |
---|
| 823 | return roar_socket_open_file(mode, host, port); |
---|
[69] | 824 | } else { |
---|
[5184] | 825 | roar_err_set(ROAR_ERROR_AFNOTSUP); |
---|
[69] | 826 | return -1; |
---|
[0] | 827 | } |
---|
| 828 | |
---|
[1476] | 829 | if ( mode == MODE_LISTEN ) { |
---|
[2040] | 830 | #if defined(ROAR_HAVE_BSDSOCKETS) || defined(ROAR_TARGET_WIN32) |
---|
[0] | 831 | if ( listen(fh, ROAR_SOCKET_QUEUE_LEN) == -1 ) { |
---|
[5184] | 832 | roar_err_from_errno(); |
---|
[0] | 833 | close(fh); |
---|
| 834 | return -1; |
---|
| 835 | } |
---|
[1476] | 836 | #else |
---|
[5184] | 837 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
[1476] | 838 | return -1; |
---|
| 839 | #endif |
---|
| 840 | } |
---|
[0] | 841 | |
---|
| 842 | return fh; |
---|
| 843 | } |
---|
| 844 | |
---|
[5375] | 845 | static int roar_socket_open_file (int mode, const char * host, int port) { |
---|
[1476] | 846 | #ifdef ROAR_HAVE_IO_POSIX |
---|
[75] | 847 | int fh; |
---|
| 848 | |
---|
| 849 | if ( mode == MODE_LISTEN ) |
---|
| 850 | return -1; |
---|
| 851 | |
---|
| 852 | if ( (fh = open(host, O_RDONLY, 0644)) == -1 ) { |
---|
| 853 | ROAR_ERR("roar_socket_open_file(*): Can not open file %s: %s", host, strerror(errno)); |
---|
| 854 | } |
---|
| 855 | |
---|
| 856 | return fh; |
---|
[1476] | 857 | #else |
---|
| 858 | return -1; |
---|
| 859 | #endif |
---|
[75] | 860 | } |
---|
| 861 | |
---|
[2] | 862 | // --- [ PROXY CODE ] --- |
---|
| 863 | |
---|
[3901] | 864 | #ifndef ROAR_HAVE_IO_POSIX |
---|
| 865 | #ifdef ROAR_SUPPORT_PROXY |
---|
| 866 | #undef ROAR_SUPPORT_PROXY |
---|
| 867 | #endif |
---|
| 868 | #endif |
---|
| 869 | |
---|
[2] | 870 | // generic proxy code: |
---|
| 871 | |
---|
[1087] | 872 | #ifdef ROAR_SUPPORT_PROXY |
---|
[5375] | 873 | static int roar_socket_open_proxy (int mode, int type, const char * host, int port, const char * proxy_type) { |
---|
[829] | 874 | int proxy_port = -1; |
---|
[2] | 875 | char proxy_host[ROAR_SOCKET_MAX_HOSTNAMELEN]; |
---|
[5754] | 876 | const char * proxy_addr = NULL; |
---|
[2] | 877 | int i; |
---|
[829] | 878 | int fh = -1; |
---|
[5754] | 879 | const char * user = NULL, * pw = NULL, * opts = NULL; |
---|
[836] | 880 | char * sep; |
---|
[838] | 881 | int no_fh = 0; |
---|
[1008] | 882 | char proxy_addr_buf[1024]; |
---|
[835] | 883 | static struct passwd * passwd; |
---|
[5260] | 884 | int (* code)(int mode, int fh, const char * host, int port, const char * user, const char * pw, const char * opts) = NULL; |
---|
[2] | 885 | |
---|
[835] | 886 | if ( passwd == NULL ) { |
---|
| 887 | passwd = getpwuid(getuid()); |
---|
| 888 | } |
---|
| 889 | |
---|
[3897] | 890 | // TODO: fix this in a good way |
---|
| 891 | #ifndef ROAR_TARGET_MICROCONTROLLER |
---|
[835] | 892 | if ( passwd != NULL ) |
---|
| 893 | user = passwd->pw_name; |
---|
[3897] | 894 | #endif |
---|
[835] | 895 | |
---|
| 896 | if ( user == NULL ) |
---|
[5754] | 897 | user = roar_env_get("USER"); |
---|
[835] | 898 | |
---|
[2] | 899 | // TODO: change this so we support listen() proxys (ssh -R) |
---|
| 900 | if ( mode != MODE_CONNECT ) |
---|
| 901 | return -1; |
---|
| 902 | |
---|
[829] | 903 | if ( !strncmp(proxy_type, "socks", 5) ) { |
---|
[5754] | 904 | proxy_addr = roar_env_get("socks_proxy"); |
---|
[2] | 905 | |
---|
| 906 | proxy_port = 9050; // TOR's default port |
---|
[832] | 907 | } else if ( !strcmp(proxy_type, "http") || !strcmp(proxy_type, "https") ) { |
---|
[830] | 908 | proxy_port = 8080; |
---|
[2] | 909 | |
---|
[5754] | 910 | if ( (proxy_addr = roar_env_get("http_proxy")) == NULL ) |
---|
| 911 | proxy_addr = roar_env_get("https_proxy"); |
---|
[832] | 912 | |
---|
[2] | 913 | if ( proxy_addr == NULL ) |
---|
| 914 | return -1; |
---|
| 915 | |
---|
[830] | 916 | if ( !strncmp(proxy_addr, "http://", 7) ) |
---|
| 917 | proxy_addr += 7; |
---|
[838] | 918 | } else if ( !strncmp(proxy_type, "ssh", 3) ) { |
---|
| 919 | proxy_port = 22; |
---|
[5754] | 920 | proxy_addr = roar_env_get("ssh_proxy"); |
---|
[838] | 921 | no_fh = 1; |
---|
[830] | 922 | } |
---|
[2] | 923 | |
---|
[1008] | 924 | proxy_addr_buf[1023] = 0; |
---|
| 925 | strncpy(proxy_addr_buf, proxy_addr, 1023); |
---|
| 926 | proxy_addr = proxy_addr_buf; |
---|
| 927 | |
---|
[837] | 928 | if ( (sep = strstr(proxy_type, "/")) != NULL ) |
---|
| 929 | opts = sep+1; |
---|
| 930 | |
---|
[830] | 931 | if ( proxy_addr == NULL ) |
---|
| 932 | return -1; |
---|
[2] | 933 | |
---|
[836] | 934 | if ( (sep = strstr(proxy_addr, "@")) != NULL ) { |
---|
| 935 | *sep = 0; |
---|
| 936 | user = proxy_addr; |
---|
| 937 | proxy_addr = sep+1; |
---|
| 938 | |
---|
| 939 | if ( (sep = strstr(user, ":")) != NULL ) { |
---|
| 940 | *sep = 0; |
---|
| 941 | pw = sep+1; |
---|
| 942 | } |
---|
| 943 | } |
---|
| 944 | |
---|
[1008] | 945 | 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); |
---|
| 946 | |
---|
[4028] | 947 | for (i = 0; proxy_addr[i] != 0 && proxy_addr[i] != ':' && i < (ROAR_SOCKET_MAX_HOSTNAMELEN - 1); i++) |
---|
[830] | 948 | proxy_host[i] = proxy_addr[i]; |
---|
| 949 | proxy_host[i] = 0; |
---|
[2] | 950 | |
---|
[830] | 951 | if ( i == 0 ) // no hostname found |
---|
| 952 | return -1; |
---|
| 953 | |
---|
| 954 | if ( proxy_addr[i] == ':' ) |
---|
| 955 | proxy_port = atoi(&proxy_addr[i+1]); |
---|
| 956 | |
---|
[838] | 957 | if ( ! no_fh ) { |
---|
| 958 | if ( (fh = roar_socket_open(mode, type, proxy_host, proxy_port)) == -1) { |
---|
| 959 | return -1; |
---|
| 960 | } |
---|
[829] | 961 | } |
---|
[2] | 962 | |
---|
[829] | 963 | if ( !strcmp(proxy_type, "socks4a") ) { // for TOR, the only supported type at the moment |
---|
[833] | 964 | code = roar_socket_open_socks4a; |
---|
| 965 | } else if ( !strcmp(proxy_type, "socks4d") ) { // DECnet |
---|
| 966 | code = roar_socket_open_socks4d; |
---|
| 967 | } else if ( !strcmp(proxy_type, "socks4") ) { // good old SOCKS4 |
---|
| 968 | code = roar_socket_open_socks4; |
---|
| 969 | } else if ( !strcmp(proxy_type, "http") ) { // HTTP CONNECT |
---|
| 970 | code = roar_socket_open_http; |
---|
[839] | 971 | } else if ( !strncmp(proxy_type, "ssh", 3) ) { // SSH... |
---|
[1063] | 972 | #ifdef ROAR_HAVE_BIN_SSH |
---|
[839] | 973 | code = roar_socket_open_ssh; |
---|
[1063] | 974 | #else |
---|
| 975 | ROAR_ERR("roar_socket_open_proxy(*): No SSH support compiled in"); |
---|
| 976 | #endif |
---|
[833] | 977 | } else { |
---|
| 978 | return -1; // unknown type |
---|
| 979 | } |
---|
[2] | 980 | |
---|
[833] | 981 | if ( code != NULL ) { |
---|
[838] | 982 | if ( no_fh ) { |
---|
| 983 | fh = code(mode, fh, host, port, user, pw, opts); |
---|
| 984 | } else { |
---|
| 985 | if ( code(mode, fh, host, port, user, pw, opts) == -1 ) { |
---|
| 986 | close(fh); |
---|
| 987 | return -1; |
---|
| 988 | } |
---|
[829] | 989 | } |
---|
| 990 | |
---|
| 991 | return fh; |
---|
[833] | 992 | } |
---|
[829] | 993 | |
---|
[833] | 994 | close(fh); |
---|
| 995 | return -1; |
---|
[2] | 996 | } |
---|
| 997 | |
---|
| 998 | // protocoll dependet proxy code: |
---|
| 999 | |
---|
[5375] | 1000 | static int roar_socket_open_socks4 (int mode, int fh, const char * host, int port, const char * user, const char * pw, const char * opts) { |
---|
[3897] | 1001 | #ifndef ROAR_TARGET_MICROCONTROLLER |
---|
[829] | 1002 | struct hostent * he; |
---|
| 1003 | |
---|
| 1004 | if ( (he = gethostbyname(host)) == NULL ) { |
---|
| 1005 | ROAR_ERR("roar_socket_open_socks4(*): Can\'t resolve host name '%s'", host); |
---|
| 1006 | return -1; |
---|
| 1007 | } |
---|
| 1008 | |
---|
[834] | 1009 | return roar_socket_open_socks4x(mode, fh, he->h_addr, port, NULL, 0, user); |
---|
[3897] | 1010 | #else |
---|
| 1011 | return -1; |
---|
| 1012 | #endif |
---|
[829] | 1013 | } |
---|
| 1014 | |
---|
[5375] | 1015 | static int roar_socket_open_socks4a(int mode, int fh, const char * host, int port, const char * user, const char * pw, const char * opts) { |
---|
[834] | 1016 | return roar_socket_open_socks4x(mode, fh, "\0\0\0\1", port, host, strlen(host)+1, user); |
---|
[829] | 1017 | } |
---|
| 1018 | |
---|
[5375] | 1019 | static int roar_socket_open_socks4d(int mode, int fh, const char * host, int port, const char * user, const char * pw, const char * opts) { |
---|
[829] | 1020 | size_t len = strlen(host)+1; |
---|
| 1021 | char * dp; |
---|
| 1022 | |
---|
| 1023 | if ( port == 0 ) { |
---|
| 1024 | if ( (dp = strstr(host, "::")) == NULL ) |
---|
| 1025 | return -1; |
---|
| 1026 | |
---|
| 1027 | len--; |
---|
| 1028 | *dp = 0; |
---|
| 1029 | memmove(dp+1, dp+2, len - (dp-host) - 1); |
---|
| 1030 | } |
---|
| 1031 | |
---|
[834] | 1032 | return roar_socket_open_socks4x(mode, fh, "\0\2\0\0", port, host, len, user); |
---|
[829] | 1033 | } |
---|
| 1034 | |
---|
[5375] | 1035 | static int roar_socket_open_socks4x(int mode, int fh, char host[4], int port, const char * app, size_t app_len, const char * user) { |
---|
[2] | 1036 | char buf[9]; |
---|
[835] | 1037 | int len; |
---|
[2] | 1038 | |
---|
| 1039 | buf[0] = 0x04; |
---|
| 1040 | buf[1] = mode == MODE_CONNECT ? 0x01 : 0x02; |
---|
| 1041 | *((uint16_t*)&buf[2]) = htons(port); |
---|
[829] | 1042 | memcpy(buf+4, host, 4); |
---|
[835] | 1043 | |
---|
| 1044 | if ( user == NULL ) { |
---|
| 1045 | buf[8] = 0x00; |
---|
| 1046 | len = 9; |
---|
| 1047 | } else { |
---|
| 1048 | len = 8; |
---|
| 1049 | } |
---|
[2] | 1050 | |
---|
[835] | 1051 | if ( write(fh, buf, len) != len ) |
---|
[2] | 1052 | return -1; |
---|
| 1053 | |
---|
[835] | 1054 | if ( user != NULL ) { |
---|
| 1055 | len = strlen(user) + 1; |
---|
| 1056 | if ( write(fh, user, len) != len ) |
---|
| 1057 | return -1; |
---|
| 1058 | } |
---|
| 1059 | |
---|
[829] | 1060 | if ( app_len > 0 ) |
---|
[5270] | 1061 | if ( write(fh, app, app_len) != (ssize_t)app_len ) |
---|
[829] | 1062 | return -1; |
---|
[2] | 1063 | |
---|
| 1064 | if ( read(fh, buf, 8) != 8 ) |
---|
| 1065 | return -1; |
---|
| 1066 | |
---|
| 1067 | if ( buf[1] != 0x5a ) |
---|
| 1068 | return -1; |
---|
| 1069 | |
---|
| 1070 | return 0; |
---|
| 1071 | } |
---|
| 1072 | |
---|
[5375] | 1073 | static int roar_socket_open_http (int mode, int fh, const char * host, int port, const char * user, const char * pw, const char * opts) { |
---|
[831] | 1074 | char buf[1024]; |
---|
| 1075 | int len; |
---|
| 1076 | |
---|
| 1077 | if ( port == 0 || host == NULL ) |
---|
| 1078 | return -1; |
---|
| 1079 | |
---|
| 1080 | if ( *host == '/' ) // AF_UNIX |
---|
| 1081 | return -1; |
---|
| 1082 | |
---|
| 1083 | if ( (len = snprintf(buf, 1024, "CONNECT %s:%i HTTP/1.0\r\nUser-Agent: libroar\r\n\r\n", host, port)) == -1 ) |
---|
| 1084 | return -1; |
---|
| 1085 | |
---|
| 1086 | if ( write(fh, buf, len) != len ) |
---|
| 1087 | return -1; |
---|
| 1088 | |
---|
| 1089 | while ( (len = read(fh, buf, 1024)) ) { |
---|
| 1090 | if ( len == 1024 ) { // overlong lion |
---|
| 1091 | return -1; |
---|
| 1092 | } else if ( len == 2 && buf[0] == '\r' && buf[1] == '\n' ) { |
---|
| 1093 | break; |
---|
| 1094 | } else if ( len == 1 && (buf[0] == '\r' || buf[0] == '\n') ) { // bad proxy or devel trying to debug ;) |
---|
| 1095 | break; |
---|
| 1096 | } else if ( len >= 4 && buf[len-4] == '\r' && buf[len-3] == '\n' && buf[len-2] == '\r' && buf[len-1] == '\n' ) { |
---|
| 1097 | break; |
---|
| 1098 | } |
---|
| 1099 | } |
---|
| 1100 | |
---|
| 1101 | return 0; |
---|
[830] | 1102 | } |
---|
| 1103 | |
---|
[839] | 1104 | |
---|
[1063] | 1105 | #ifdef ROAR_HAVE_BIN_SSH |
---|
[5375] | 1106 | static int roar_socket_open_ssh (int mode, int fh, const char * host, int port, const char * user, const char * pw, const char * opts) { |
---|
[5754] | 1107 | const char * proxy_addr = roar_env_get("ssh_proxy"); |
---|
[839] | 1108 | char * sep; |
---|
| 1109 | char cmd[1024] = {0}, rcmd[1024] = {0}; |
---|
| 1110 | int proxy_port = 22; |
---|
[840] | 1111 | int use_socat = 0; |
---|
[839] | 1112 | int r; |
---|
| 1113 | int socks[2]; |
---|
| 1114 | |
---|
| 1115 | if ( host == NULL ) |
---|
| 1116 | return -1; |
---|
| 1117 | |
---|
| 1118 | if ( *host == '/' ) |
---|
[840] | 1119 | use_socat = 1; |
---|
[839] | 1120 | |
---|
| 1121 | if ( mode == MODE_LISTEN ) |
---|
| 1122 | return -1; |
---|
| 1123 | |
---|
| 1124 | if ( proxy_addr == NULL ) |
---|
| 1125 | return -1; |
---|
| 1126 | |
---|
[840] | 1127 | if ( opts != NULL ) { |
---|
| 1128 | if ( !strcmp(opts, "socat") ) { |
---|
| 1129 | use_socat = 1; |
---|
| 1130 | } else if ( !strcmp(opts, "netcat") ) { |
---|
| 1131 | use_socat = 0; |
---|
| 1132 | } else { |
---|
| 1133 | return -1; |
---|
| 1134 | } |
---|
| 1135 | } |
---|
| 1136 | |
---|
[1008] | 1137 | ROAR_DBG("roar_socket_open_ssh(*): proxy_addr='%s'", proxy_addr); |
---|
| 1138 | |
---|
[839] | 1139 | if ( (sep = strstr(proxy_addr, "@")) != NULL ) |
---|
| 1140 | proxy_addr = sep+1; |
---|
| 1141 | |
---|
| 1142 | if ( (sep = strstr(proxy_addr, ":")) != NULL ) { |
---|
| 1143 | *sep = 0; |
---|
| 1144 | proxy_port = atoi(sep+1); |
---|
| 1145 | } |
---|
| 1146 | |
---|
| 1147 | |
---|
| 1148 | if ( !strcmp(host, "+fork") ) { |
---|
[1068] | 1149 | strncpy(rcmd, "roard --no-listen --client-fh 0", 32); |
---|
[839] | 1150 | } else { |
---|
[840] | 1151 | if ( use_socat ) { |
---|
| 1152 | if ( *host == '/' ) { |
---|
| 1153 | snprintf(rcmd, 1023, "socat stdio unix-connect:\"%s\"", host); |
---|
| 1154 | } else { |
---|
| 1155 | snprintf(rcmd, 1023, "socat stdio tcp:\"%s\":%i", host, port); |
---|
| 1156 | } |
---|
| 1157 | } else { |
---|
| 1158 | snprintf(rcmd, 1023, "$(which netcat nc 2> /dev/null | grep -v \" \" | head -n 1) \"%s\" %i", host, port); |
---|
| 1159 | } |
---|
| 1160 | |
---|
[839] | 1161 | rcmd[1023] = 0; |
---|
| 1162 | } |
---|
| 1163 | |
---|
[1008] | 1164 | ROAR_DBG("roar_socket_open_ssh(*): proxy_port=%i, user='%s', proxy_addr='%s'", proxy_port, user, proxy_addr); |
---|
[1033] | 1165 | ROAR_DBG("roar_socket_open_ssh(*): rcmd: %s", rcmd); |
---|
[1063] | 1166 | snprintf(cmd, 1023, ROAR_HAVE_BIN_SSH " -p %i -l '%s' '%s' '%s'", proxy_port, user, proxy_addr, rcmd); |
---|
[839] | 1167 | cmd[1023] = 0; |
---|
| 1168 | |
---|
| 1169 | |
---|
[3897] | 1170 | // TODO: get this more portable! |
---|
| 1171 | #ifdef AF_UNIX |
---|
[839] | 1172 | if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) { |
---|
| 1173 | return -1; |
---|
| 1174 | } |
---|
[3897] | 1175 | #else |
---|
| 1176 | return -1; |
---|
| 1177 | #endif |
---|
[839] | 1178 | |
---|
[5373] | 1179 | r = roar_fork(NULL); |
---|
[839] | 1180 | |
---|
| 1181 | if ( r == -1 ) { // error! |
---|
| 1182 | ROAR_ERR("roar_socket_open_ssh(*): Can not fork: %s", strerror(errno)); |
---|
| 1183 | close(socks[0]); |
---|
| 1184 | close(socks[1]); |
---|
| 1185 | return -1; |
---|
| 1186 | } else if ( r == 0 ) { // we are the child |
---|
[5619] | 1187 | roar_watchdog_stop(); |
---|
| 1188 | |
---|
[839] | 1189 | close(socks[0]); |
---|
| 1190 | |
---|
| 1191 | close(ROAR_STDIN ); // we do not want roard to have any standard input |
---|
| 1192 | close(ROAR_STDOUT); // STDOUT is also not needed, so we close it, |
---|
| 1193 | // but STDERR we keep open for error messages. |
---|
| 1194 | |
---|
| 1195 | dup2(socks[1], 0); |
---|
| 1196 | dup2(socks[1], 1); |
---|
| 1197 | |
---|
[5364] | 1198 | execlp("sh", "sh", "-c", cmd, (_LIBROAR_GOOD_CAST char*)NULL); |
---|
[839] | 1199 | |
---|
| 1200 | // we are still alive? |
---|
| 1201 | ROAR_ERR("roar_socket_open_ssh(*): alive after exec(), that's bad!"); |
---|
[5619] | 1202 | ROAR_U_EXIT(1); |
---|
[839] | 1203 | } else { // we are the parent |
---|
| 1204 | close(socks[1]); |
---|
| 1205 | return socks[0]; |
---|
| 1206 | } |
---|
| 1207 | return -1; |
---|
| 1208 | } |
---|
[1063] | 1209 | #endif |
---|
[839] | 1210 | |
---|
[1087] | 1211 | #endif // ROAR_SUPPORT_PROXY |
---|
| 1212 | |
---|
[0] | 1213 | //ll |
---|