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