1 1.1 elric /* $NetBSD: socket_wrapper.c,v 1.2 2017/01/28 21:31:50 christos Exp $ */ 2 1.1 elric 3 1.1 elric /* 4 1.1 elric * Copyright (C) Jelmer Vernooij 2005 <jelmer (at) samba.org> 5 1.1 elric * Copyright (C) Stefan Metzmacher 2006 <metze (at) samba.org> 6 1.1 elric * 7 1.1 elric * All rights reserved. 8 1.1 elric * 9 1.1 elric * Redistribution and use in source and binary forms, with or without 10 1.1 elric * modification, are permitted provided that the following conditions 11 1.1 elric * are met: 12 1.1 elric * 13 1.1 elric * 1. Redistributions of source code must retain the above copyright 14 1.1 elric * notice, this list of conditions and the following disclaimer. 15 1.1 elric * 16 1.1 elric * 2. Redistributions in binary form must reproduce the above copyright 17 1.1 elric * notice, this list of conditions and the following disclaimer in the 18 1.1 elric * documentation and/or other materials provided with the distribution. 19 1.1 elric * 20 1.1 elric * 3. Neither the name of the author nor the names of its contributors 21 1.1 elric * may be used to endorse or promote products derived from this software 22 1.1 elric * without specific prior written permission. 23 1.1 elric * 24 1.1 elric * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 25 1.1 elric * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 1.1 elric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 1.1 elric * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 28 1.1 elric * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 1.1 elric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 1.1 elric * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 1.1 elric * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 1.1 elric * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 1.1 elric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 1.1 elric * SUCH DAMAGE. 35 1.1 elric * 36 1.1 elric */ 37 1.1 elric 38 1.1 elric /* 39 1.1 elric Socket wrapper library. Passes all socket communication over 40 1.1 elric unix domain sockets if the environment variable SOCKET_WRAPPER_DIR 41 1.1 elric is set. 42 1.1 elric */ 43 1.1 elric 44 1.1 elric #define SOCKET_WRAPPER_NOT_REPLACE 45 1.1 elric 46 1.1 elric #ifdef _SAMBA_BUILD_ 47 1.1 elric 48 1.1 elric #include "includes.h" 49 1.1 elric #include "system/network.h" 50 1.1 elric #include "system/filesys.h" 51 1.1 elric 52 1.1 elric #ifdef malloc 53 1.1 elric #undef malloc 54 1.1 elric #endif 55 1.1 elric #ifdef calloc 56 1.1 elric #undef calloc 57 1.1 elric #endif 58 1.1 elric #ifdef strdup 59 1.1 elric #undef strdup 60 1.1 elric #endif 61 1.1 elric 62 1.1 elric #else /* _SAMBA_BUILD_ */ 63 1.1 elric 64 1.1 elric #include <config.h> 65 1.1 elric #undef SOCKET_WRAPPER_REPLACE 66 1.1 elric 67 1.1 elric #include <sys/types.h> 68 1.1 elric #ifdef TIME_WITH_SYS_TIME 69 1.1 elric #include <sys/time.h> 70 1.1 elric #include <time.h> 71 1.1 elric #elif defined(HAVE_SYS_TIME_H) 72 1.1 elric #include <sys/time.h> 73 1.1 elric #else 74 1.1 elric #include <time.h> 75 1.1 elric #endif 76 1.1 elric #include <sys/stat.h> 77 1.1 elric #include <sys/socket.h> 78 1.1 elric #include <sys/ioctl.h> 79 1.1 elric #ifdef HAVE_SYS_FILIO_H 80 1.1 elric #include <sys/filio.h> 81 1.1 elric #endif 82 1.1 elric #include <errno.h> 83 1.1 elric #include <sys/un.h> 84 1.1 elric #include <netinet/in.h> 85 1.1 elric #include <netinet/tcp.h> 86 1.1 elric #include <fcntl.h> 87 1.1 elric #include <stdlib.h> 88 1.1 elric #include <unistd.h> 89 1.1 elric #include <string.h> 90 1.1 elric #include <stdio.h> 91 1.1 elric #include <krb5/roken.h> 92 1.1 elric 93 1.1 elric #include "socket_wrapper.h" 94 1.1 elric 95 1.1 elric #define HAVE_GETTIMEOFDAY_TZ 1 96 1.1 elric 97 1.1 elric #define _PUBLIC_ 98 1.1 elric 99 1.1 elric #endif 100 1.1 elric 101 1.1 elric #define SWRAP_DLIST_ADD(list,item) do { \ 102 1.1 elric if (!(list)) { \ 103 1.1 elric (item)->prev = NULL; \ 104 1.1 elric (item)->next = NULL; \ 105 1.1 elric (list) = (item); \ 106 1.1 elric } else { \ 107 1.1 elric (item)->prev = NULL; \ 108 1.1 elric (item)->next = (list); \ 109 1.1 elric (list)->prev = (item); \ 110 1.1 elric (list) = (item); \ 111 1.1 elric } \ 112 1.1 elric } while (0) 113 1.1 elric 114 1.1 elric #define SWRAP_DLIST_REMOVE(list,item) do { \ 115 1.1 elric if ((list) == (item)) { \ 116 1.1 elric (list) = (item)->next; \ 117 1.1 elric if (list) { \ 118 1.1 elric (list)->prev = NULL; \ 119 1.1 elric } \ 120 1.1 elric } else { \ 121 1.1 elric if ((item)->prev) { \ 122 1.1 elric (item)->prev->next = (item)->next; \ 123 1.1 elric } \ 124 1.1 elric if ((item)->next) { \ 125 1.1 elric (item)->next->prev = (item)->prev; \ 126 1.1 elric } \ 127 1.1 elric } \ 128 1.1 elric (item)->prev = NULL; \ 129 1.1 elric (item)->next = NULL; \ 130 1.1 elric } while (0) 131 1.1 elric 132 1.1 elric /* LD_PRELOAD doesn't work yet, so REWRITE_CALLS is all we support 133 1.1 elric * for now */ 134 1.1 elric #define REWRITE_CALLS 135 1.1 elric 136 1.1 elric #ifdef REWRITE_CALLS 137 1.1 elric #define real_accept accept 138 1.1 elric #define real_connect connect 139 1.1 elric #define real_bind bind 140 1.1 elric #define real_listen listen 141 1.1 elric #define real_getpeername getpeername 142 1.1 elric #define real_getsockname getsockname 143 1.1 elric #define real_getsockopt getsockopt 144 1.1 elric #define real_setsockopt setsockopt 145 1.1 elric #define real_recvfrom recvfrom 146 1.1 elric #define real_sendto sendto 147 1.1 elric #define real_ioctl ioctl 148 1.1 elric #define real_recv recv 149 1.1 elric #define real_send send 150 1.1 elric #define real_socket socket 151 1.1 elric #define real_close close 152 1.1 elric #define real_dup dup 153 1.1 elric #define real_dup2 dup2 154 1.1 elric #endif 155 1.1 elric 156 1.1 elric #ifdef HAVE_GETTIMEOFDAY_TZ 157 1.1 elric #define swrapGetTimeOfDay(tval) gettimeofday(tval,NULL) 158 1.1 elric #else 159 1.1 elric #define swrapGetTimeOfDay(tval) gettimeofday(tval) 160 1.1 elric #endif 161 1.1 elric 162 1.1 elric /* we need to use a very terse format here as IRIX 6.4 silently 163 1.1 elric truncates names to 16 chars, so if we use a longer name then we 164 1.1 elric can't tell which port a packet came from with recvfrom() 165 1.1 elric 166 1.1 elric with this format we have 8 chars left for the directory name 167 1.1 elric */ 168 1.1 elric #define SOCKET_FORMAT "%c%02X%04X" 169 1.1 elric #define SOCKET_TYPE_CHAR_TCP 'T' 170 1.1 elric #define SOCKET_TYPE_CHAR_UDP 'U' 171 1.1 elric #define SOCKET_TYPE_CHAR_TCP_V6 'X' 172 1.1 elric #define SOCKET_TYPE_CHAR_UDP_V6 'Y' 173 1.1 elric 174 1.1 elric #define MAX_WRAPPED_INTERFACES 16 175 1.1 elric 176 1.1 elric #define SW_IPV6_ADDRESS 1 177 1.1 elric 178 1.1 elric static struct sockaddr *sockaddr_dup(const void *data, socklen_t len) 179 1.1 elric { 180 1.1 elric struct sockaddr *ret = (struct sockaddr *)malloc(len); 181 1.1 elric memcpy(ret, data, len); 182 1.1 elric return ret; 183 1.1 elric } 184 1.1 elric 185 1.1 elric static void set_port(int family, int prt, struct sockaddr *addr) 186 1.1 elric { 187 1.1 elric switch (family) { 188 1.1 elric case AF_INET: 189 1.1 elric ((struct sockaddr_in *)addr)->sin_port = htons(prt); 190 1.1 elric break; 191 1.1 elric #ifdef HAVE_IPV6 192 1.1 elric case AF_INET6: 193 1.1 elric ((struct sockaddr_in6 *)addr)->sin6_port = htons(prt); 194 1.1 elric break; 195 1.1 elric #endif 196 1.1 elric } 197 1.1 elric } 198 1.1 elric 199 1.1 elric static int socket_length(int family) 200 1.1 elric { 201 1.1 elric switch (family) { 202 1.1 elric case AF_INET: 203 1.1 elric return sizeof(struct sockaddr_in); 204 1.1 elric #ifdef HAVE_IPV6 205 1.1 elric case AF_INET6: 206 1.1 elric return sizeof(struct sockaddr_in6); 207 1.1 elric #endif 208 1.1 elric } 209 1.1 elric return -1; 210 1.1 elric } 211 1.1 elric 212 1.1 elric 213 1.1 elric 214 1.1 elric struct socket_info 215 1.1 elric { 216 1.1 elric int fd; 217 1.1 elric 218 1.1 elric int family; 219 1.1 elric int type; 220 1.1 elric int protocol; 221 1.1 elric int bound; 222 1.1 elric int bcast; 223 1.1 elric int is_server; 224 1.1 elric 225 1.1 elric char *path; 226 1.1 elric char *tmp_path; 227 1.1 elric 228 1.1 elric struct sockaddr *myname; 229 1.1 elric socklen_t myname_len; 230 1.1 elric 231 1.1 elric struct sockaddr *peername; 232 1.1 elric socklen_t peername_len; 233 1.1 elric 234 1.1 elric struct { 235 1.1 elric unsigned long pck_snd; 236 1.1 elric unsigned long pck_rcv; 237 1.1 elric } io; 238 1.1 elric 239 1.1 elric struct socket_info *prev, *next; 240 1.1 elric }; 241 1.1 elric 242 1.1 elric static struct socket_info *sockets; 243 1.1 elric 244 1.1 elric 245 1.1 elric static const char *socket_wrapper_dir(void) 246 1.1 elric { 247 1.1 elric const char *s = getenv("SOCKET_WRAPPER_DIR"); 248 1.1 elric if (s == NULL) { 249 1.1 elric return NULL; 250 1.1 elric } 251 1.1 elric if (strncmp(s, "./", 2) == 0) { 252 1.1 elric s += 2; 253 1.1 elric } 254 1.1 elric return s; 255 1.1 elric } 256 1.1 elric 257 1.1 elric static unsigned int socket_wrapper_default_iface(void) 258 1.1 elric { 259 1.1 elric const char *s = getenv("SOCKET_WRAPPER_DEFAULT_IFACE"); 260 1.1 elric if (s) { 261 1.1 elric unsigned int iface; 262 1.1 elric if (sscanf(s, "%u", &iface) == 1) { 263 1.1 elric if (iface >= 1 && iface <= MAX_WRAPPED_INTERFACES) { 264 1.1 elric return iface; 265 1.1 elric } 266 1.1 elric } 267 1.1 elric } 268 1.1 elric 269 1.1 elric return 1;/* 127.0.0.1 */ 270 1.1 elric } 271 1.1 elric 272 1.1 elric static int convert_un_in(const struct sockaddr_un *un, struct sockaddr *in, socklen_t *len) 273 1.1 elric { 274 1.1 elric unsigned int iface; 275 1.1 elric unsigned int prt; 276 1.1 elric const char *p; 277 1.1 elric char type; 278 1.1 elric 279 1.1 elric p = strrchr(un->sun_path, '/'); 280 1.1 elric if (p) p++; else p = un->sun_path; 281 1.1 elric 282 1.1 elric if (sscanf(p, SOCKET_FORMAT, &type, &iface, &prt) != 3) { 283 1.1 elric errno = EINVAL; 284 1.1 elric return -1; 285 1.1 elric } 286 1.1 elric 287 1.1 elric if (iface == 0 || iface > MAX_WRAPPED_INTERFACES) { 288 1.1 elric errno = EINVAL; 289 1.1 elric return -1; 290 1.1 elric } 291 1.1 elric 292 1.1 elric if (prt > 0xFFFF) { 293 1.1 elric errno = EINVAL; 294 1.1 elric return -1; 295 1.1 elric } 296 1.1 elric 297 1.1 elric switch(type) { 298 1.1 elric case SOCKET_TYPE_CHAR_TCP: 299 1.1 elric case SOCKET_TYPE_CHAR_UDP: { 300 1.1 elric struct sockaddr_in *in2 = (struct sockaddr_in *)in; 301 1.2 christos 302 1.1 elric if ((*len) < sizeof(*in2)) { 303 1.1 elric errno = EINVAL; 304 1.1 elric return -1; 305 1.1 elric } 306 1.1 elric 307 1.1 elric memset(in2, 0, sizeof(*in2)); 308 1.1 elric in2->sin_family = AF_INET; 309 1.1 elric in2->sin_addr.s_addr = htonl((127<<24) | iface); 310 1.1 elric in2->sin_port = htons(prt); 311 1.1 elric 312 1.1 elric *len = sizeof(*in2); 313 1.1 elric break; 314 1.1 elric } 315 1.1 elric #ifdef HAVE_IPV6 316 1.1 elric case SOCKET_TYPE_CHAR_TCP_V6: 317 1.1 elric case SOCKET_TYPE_CHAR_UDP_V6: { 318 1.1 elric struct sockaddr_in6 *in2 = (struct sockaddr_in6 *)in; 319 1.2 christos 320 1.1 elric if ((*len) < sizeof(*in2)) { 321 1.1 elric errno = EINVAL; 322 1.1 elric return -1; 323 1.1 elric } 324 1.1 elric 325 1.1 elric memset(in2, 0, sizeof(*in2)); 326 1.1 elric in2->sin6_family = AF_INET6; 327 1.1 elric in2->sin6_addr.s6_addr[0] = SW_IPV6_ADDRESS; 328 1.1 elric in2->sin6_port = htons(prt); 329 1.1 elric 330 1.1 elric *len = sizeof(*in2); 331 1.1 elric break; 332 1.1 elric } 333 1.1 elric #endif 334 1.1 elric default: 335 1.1 elric errno = EINVAL; 336 1.1 elric return -1; 337 1.1 elric } 338 1.1 elric 339 1.1 elric return 0; 340 1.1 elric } 341 1.1 elric 342 1.1 elric static int convert_in_un_remote(struct socket_info *si, const struct sockaddr *inaddr, struct sockaddr_un *un, 343 1.1 elric int *bcast) 344 1.1 elric { 345 1.1 elric char type = '\0'; 346 1.1 elric unsigned int prt; 347 1.1 elric unsigned int iface; 348 1.1 elric int is_bcast = 0; 349 1.1 elric 350 1.1 elric if (bcast) *bcast = 0; 351 1.1 elric 352 1.1 elric switch (si->family) { 353 1.1 elric case AF_INET: { 354 1.1 elric const struct sockaddr_in *in = 355 1.1 elric (const struct sockaddr_in *)inaddr; 356 1.1 elric unsigned int addr = ntohl(in->sin_addr.s_addr); 357 1.1 elric char u_type = '\0'; 358 1.1 elric char b_type = '\0'; 359 1.1 elric char a_type = '\0'; 360 1.1 elric 361 1.1 elric switch (si->type) { 362 1.1 elric case SOCK_STREAM: 363 1.1 elric u_type = SOCKET_TYPE_CHAR_TCP; 364 1.1 elric break; 365 1.1 elric case SOCK_DGRAM: 366 1.1 elric u_type = SOCKET_TYPE_CHAR_UDP; 367 1.1 elric a_type = SOCKET_TYPE_CHAR_UDP; 368 1.1 elric b_type = SOCKET_TYPE_CHAR_UDP; 369 1.1 elric break; 370 1.1 elric } 371 1.1 elric 372 1.1 elric prt = ntohs(in->sin_port); 373 1.1 elric if (a_type && addr == 0xFFFFFFFF) { 374 1.1 elric /* 255.255.255.255 only udp */ 375 1.1 elric is_bcast = 2; 376 1.1 elric type = a_type; 377 1.1 elric iface = socket_wrapper_default_iface(); 378 1.1 elric } else if (b_type && addr == 0x7FFFFFFF) { 379 1.1 elric /* 127.255.255.255 only udp */ 380 1.1 elric is_bcast = 1; 381 1.1 elric type = b_type; 382 1.1 elric iface = socket_wrapper_default_iface(); 383 1.1 elric } else if ((addr & 0xFFFFFF00) == 0x7F000000) { 384 1.1 elric /* 127.0.0.X */ 385 1.1 elric is_bcast = 0; 386 1.1 elric type = u_type; 387 1.1 elric iface = (addr & 0x000000FF); 388 1.1 elric } else { 389 1.1 elric errno = ENETUNREACH; 390 1.1 elric return -1; 391 1.1 elric } 392 1.1 elric if (bcast) *bcast = is_bcast; 393 1.1 elric break; 394 1.1 elric } 395 1.1 elric #ifdef HAVE_IPV6 396 1.1 elric case AF_INET6: { 397 1.1 elric const struct sockaddr_in6 *in = 398 1.1 elric (const struct sockaddr_in6 *)inaddr; 399 1.1 elric 400 1.1 elric switch (si->type) { 401 1.1 elric case SOCK_STREAM: 402 1.1 elric type = SOCKET_TYPE_CHAR_TCP_V6; 403 1.1 elric break; 404 1.1 elric case SOCK_DGRAM: 405 1.1 elric type = SOCKET_TYPE_CHAR_UDP_V6; 406 1.1 elric break; 407 1.1 elric } 408 1.1 elric 409 1.1 elric /* XXX no multicast/broadcast */ 410 1.1 elric 411 1.1 elric prt = ntohs(in->sin6_port); 412 1.1 elric iface = SW_IPV6_ADDRESS; 413 1.2 christos 414 1.1 elric break; 415 1.1 elric } 416 1.1 elric #endif 417 1.1 elric default: 418 1.1 elric errno = ENETUNREACH; 419 1.1 elric return -1; 420 1.1 elric } 421 1.1 elric 422 1.1 elric if (prt == 0) { 423 1.1 elric errno = EINVAL; 424 1.1 elric return -1; 425 1.1 elric } 426 1.1 elric 427 1.1 elric if (is_bcast) { 428 1.1 elric snprintf(un->sun_path, sizeof(un->sun_path), "%s/EINVAL", 429 1.1 elric socket_wrapper_dir()); 430 1.1 elric /* the caller need to do more processing */ 431 1.1 elric return 0; 432 1.1 elric } 433 1.1 elric 434 1.1 elric snprintf(un->sun_path, sizeof(un->sun_path), "%s/"SOCKET_FORMAT, 435 1.1 elric socket_wrapper_dir(), type, iface, prt); 436 1.1 elric 437 1.1 elric return 0; 438 1.1 elric } 439 1.1 elric 440 1.1 elric static int convert_in_un_alloc(struct socket_info *si, const struct sockaddr *inaddr, struct sockaddr_un *un, 441 1.1 elric int *bcast) 442 1.1 elric { 443 1.1 elric char type = '\0'; 444 1.1 elric unsigned int prt; 445 1.1 elric unsigned int iface; 446 1.1 elric struct stat st; 447 1.1 elric int is_bcast = 0; 448 1.1 elric 449 1.1 elric if (bcast) *bcast = 0; 450 1.1 elric 451 1.1 elric switch (si->family) { 452 1.1 elric case AF_INET: { 453 1.1 elric const struct sockaddr_in *in = 454 1.1 elric (const struct sockaddr_in *)inaddr; 455 1.1 elric unsigned int addr = ntohl(in->sin_addr.s_addr); 456 1.1 elric char u_type = '\0'; 457 1.1 elric char d_type = '\0'; 458 1.1 elric char b_type = '\0'; 459 1.1 elric char a_type = '\0'; 460 1.1 elric 461 1.1 elric prt = ntohs(in->sin_port); 462 1.1 elric 463 1.1 elric switch (si->type) { 464 1.1 elric case SOCK_STREAM: 465 1.1 elric u_type = SOCKET_TYPE_CHAR_TCP; 466 1.1 elric d_type = SOCKET_TYPE_CHAR_TCP; 467 1.1 elric break; 468 1.1 elric case SOCK_DGRAM: 469 1.1 elric u_type = SOCKET_TYPE_CHAR_UDP; 470 1.1 elric d_type = SOCKET_TYPE_CHAR_UDP; 471 1.1 elric a_type = SOCKET_TYPE_CHAR_UDP; 472 1.1 elric b_type = SOCKET_TYPE_CHAR_UDP; 473 1.1 elric break; 474 1.1 elric } 475 1.1 elric 476 1.1 elric if (addr == 0) { 477 1.1 elric /* 0.0.0.0 */ 478 1.1 elric is_bcast = 0; 479 1.1 elric type = d_type; 480 1.1 elric iface = socket_wrapper_default_iface(); 481 1.1 elric } else if (a_type && addr == 0xFFFFFFFF) { 482 1.1 elric /* 255.255.255.255 only udp */ 483 1.1 elric is_bcast = 2; 484 1.1 elric type = a_type; 485 1.1 elric iface = socket_wrapper_default_iface(); 486 1.1 elric } else if (b_type && addr == 0x7FFFFFFF) { 487 1.1 elric /* 127.255.255.255 only udp */ 488 1.1 elric is_bcast = 1; 489 1.1 elric type = b_type; 490 1.1 elric iface = socket_wrapper_default_iface(); 491 1.1 elric } else if ((addr & 0xFFFFFF00) == 0x7F000000) { 492 1.1 elric /* 127.0.0.X */ 493 1.1 elric is_bcast = 0; 494 1.1 elric type = u_type; 495 1.1 elric iface = (addr & 0x000000FF); 496 1.1 elric } else { 497 1.1 elric errno = EADDRNOTAVAIL; 498 1.1 elric return -1; 499 1.1 elric } 500 1.1 elric break; 501 1.1 elric } 502 1.1 elric #ifdef HAVE_IPV6 503 1.1 elric case AF_INET6: { 504 1.1 elric const struct sockaddr_in6 *in = 505 1.1 elric (const struct sockaddr_in6 *)inaddr; 506 1.1 elric 507 1.1 elric switch (si->type) { 508 1.1 elric case SOCK_STREAM: 509 1.1 elric type = SOCKET_TYPE_CHAR_TCP_V6; 510 1.1 elric break; 511 1.1 elric case SOCK_DGRAM: 512 1.1 elric type = SOCKET_TYPE_CHAR_UDP_V6; 513 1.1 elric break; 514 1.1 elric } 515 1.1 elric 516 1.1 elric /* XXX no multicast/broadcast */ 517 1.1 elric 518 1.1 elric prt = ntohs(in->sin6_port); 519 1.1 elric iface = SW_IPV6_ADDRESS; 520 1.2 christos 521 1.1 elric break; 522 1.1 elric } 523 1.1 elric #endif 524 1.1 elric default: 525 1.1 elric errno = ENETUNREACH; 526 1.1 elric return -1; 527 1.1 elric } 528 1.1 elric 529 1.1 elric 530 1.1 elric if (bcast) *bcast = is_bcast; 531 1.1 elric 532 1.1 elric if (prt == 0) { 533 1.1 elric /* handle auto-allocation of ephemeral ports */ 534 1.1 elric for (prt = 5001; prt < 10000; prt++) { 535 1.1 elric snprintf(un->sun_path, sizeof(un->sun_path), "%s/"SOCKET_FORMAT, 536 1.1 elric socket_wrapper_dir(), type, iface, prt); 537 1.1 elric if (stat(un->sun_path, &st) == 0) continue; 538 1.1 elric 539 1.1 elric set_port(si->family, prt, si->myname); 540 1.1 elric } 541 1.1 elric } 542 1.1 elric 543 1.1 elric snprintf(un->sun_path, sizeof(un->sun_path), "%s/"SOCKET_FORMAT, 544 1.1 elric socket_wrapper_dir(), type, iface, prt); 545 1.1 elric return 0; 546 1.1 elric } 547 1.1 elric 548 1.1 elric static struct socket_info *find_socket_info(int fd) 549 1.1 elric { 550 1.1 elric struct socket_info *i; 551 1.1 elric for (i = sockets; i; i = i->next) { 552 1.1 elric if (i->fd == fd) 553 1.1 elric return i; 554 1.1 elric } 555 1.1 elric 556 1.1 elric return NULL; 557 1.1 elric } 558 1.1 elric 559 1.1 elric static int sockaddr_convert_to_un(struct socket_info *si, const struct sockaddr *in_addr, socklen_t in_len, 560 1.1 elric struct sockaddr_un *out_addr, int alloc_sock, int *bcast) 561 1.1 elric { 562 1.1 elric if (!out_addr) 563 1.1 elric return 0; 564 1.1 elric 565 1.1 elric out_addr->sun_family = AF_UNIX; 566 1.1 elric 567 1.1 elric switch (in_addr->sa_family) { 568 1.1 elric case AF_INET: 569 1.1 elric #ifdef HAVE_IPV6 570 1.1 elric case AF_INET6: 571 1.1 elric #endif 572 1.1 elric switch (si->type) { 573 1.1 elric case SOCK_STREAM: 574 1.1 elric case SOCK_DGRAM: 575 1.1 elric break; 576 1.1 elric default: 577 1.1 elric errno = ESOCKTNOSUPPORT; 578 1.1 elric return -1; 579 1.1 elric } 580 1.1 elric if (alloc_sock) { 581 1.1 elric return convert_in_un_alloc(si, in_addr, out_addr, bcast); 582 1.1 elric } else { 583 1.1 elric return convert_in_un_remote(si, in_addr, out_addr, bcast); 584 1.1 elric } 585 1.1 elric default: 586 1.1 elric break; 587 1.1 elric } 588 1.2 christos 589 1.1 elric errno = EAFNOSUPPORT; 590 1.1 elric return -1; 591 1.1 elric } 592 1.1 elric 593 1.1 elric static int sockaddr_convert_from_un(const struct socket_info *si, 594 1.1 elric const struct sockaddr_un *in_addr, 595 1.1 elric socklen_t un_addrlen, 596 1.1 elric int family, 597 1.1 elric struct sockaddr *out_addr, 598 1.1 elric socklen_t *out_addrlen) 599 1.1 elric { 600 1.1 elric if (out_addr == NULL || out_addrlen == NULL) 601 1.1 elric return 0; 602 1.1 elric 603 1.1 elric if (un_addrlen == 0) { 604 1.1 elric *out_addrlen = 0; 605 1.1 elric return 0; 606 1.1 elric } 607 1.1 elric 608 1.1 elric switch (family) { 609 1.1 elric case AF_INET: 610 1.1 elric #ifdef HAVE_IPV6 611 1.1 elric case AF_INET6: 612 1.1 elric #endif 613 1.1 elric switch (si->type) { 614 1.1 elric case SOCK_STREAM: 615 1.1 elric case SOCK_DGRAM: 616 1.1 elric break; 617 1.1 elric default: 618 1.1 elric errno = ESOCKTNOSUPPORT; 619 1.1 elric return -1; 620 1.1 elric } 621 1.1 elric return convert_un_in(in_addr, out_addr, out_addrlen); 622 1.1 elric default: 623 1.1 elric break; 624 1.1 elric } 625 1.1 elric 626 1.1 elric errno = EAFNOSUPPORT; 627 1.1 elric return -1; 628 1.1 elric } 629 1.1 elric 630 1.1 elric enum swrap_packet_type { 631 1.1 elric SWRAP_CONNECT_SEND, 632 1.1 elric SWRAP_CONNECT_UNREACH, 633 1.1 elric SWRAP_CONNECT_RECV, 634 1.1 elric SWRAP_CONNECT_ACK, 635 1.1 elric SWRAP_ACCEPT_SEND, 636 1.1 elric SWRAP_ACCEPT_RECV, 637 1.1 elric SWRAP_ACCEPT_ACK, 638 1.1 elric SWRAP_RECVFROM, 639 1.1 elric SWRAP_SENDTO, 640 1.1 elric SWRAP_SENDTO_UNREACH, 641 1.1 elric SWRAP_PENDING_RST, 642 1.1 elric SWRAP_RECV, 643 1.1 elric SWRAP_RECV_RST, 644 1.1 elric SWRAP_SEND, 645 1.1 elric SWRAP_SEND_RST, 646 1.1 elric SWRAP_CLOSE_SEND, 647 1.1 elric SWRAP_CLOSE_RECV, 648 1.1 elric SWRAP_CLOSE_ACK 649 1.1 elric }; 650 1.1 elric 651 1.1 elric struct swrap_file_hdr { 652 1.1 elric unsigned long magic; 653 1.2 christos unsigned short version_major; 654 1.1 elric unsigned short version_minor; 655 1.1 elric long timezone; 656 1.1 elric unsigned long sigfigs; 657 1.1 elric unsigned long frame_max_len; 658 1.1 elric #define SWRAP_FRAME_LENGTH_MAX 0xFFFF 659 1.1 elric unsigned long link_type; 660 1.1 elric }; 661 1.1 elric #define SWRAP_FILE_HDR_SIZE 24 662 1.1 elric 663 1.1 elric struct swrap_packet { 664 1.1 elric struct { 665 1.1 elric unsigned long seconds; 666 1.1 elric unsigned long micro_seconds; 667 1.1 elric unsigned long recorded_length; 668 1.1 elric unsigned long full_length; 669 1.1 elric } frame; 670 1.1 elric #define SWRAP_PACKET__FRAME_SIZE 16 671 1.1 elric 672 1.1 elric struct { 673 1.1 elric struct { 674 1.1 elric unsigned char ver_hdrlen; 675 1.1 elric unsigned char tos; 676 1.1 elric unsigned short packet_length; 677 1.1 elric unsigned short identification; 678 1.1 elric unsigned char flags; 679 1.1 elric unsigned char fragment; 680 1.1 elric unsigned char ttl; 681 1.1 elric unsigned char protocol; 682 1.1 elric unsigned short hdr_checksum; 683 1.1 elric unsigned long src_addr; 684 1.1 elric unsigned long dest_addr; 685 1.1 elric } hdr; 686 1.1 elric #define SWRAP_PACKET__IP_HDR_SIZE 20 687 1.1 elric 688 1.1 elric union { 689 1.1 elric struct { 690 1.1 elric unsigned short source_port; 691 1.1 elric unsigned short dest_port; 692 1.1 elric unsigned long seq_num; 693 1.1 elric unsigned long ack_num; 694 1.1 elric unsigned char hdr_length; 695 1.1 elric unsigned char control; 696 1.1 elric unsigned short window; 697 1.1 elric unsigned short checksum; 698 1.1 elric unsigned short urg; 699 1.1 elric } tcp; 700 1.1 elric #define SWRAP_PACKET__IP_P_TCP_SIZE 20 701 1.1 elric struct { 702 1.1 elric unsigned short source_port; 703 1.1 elric unsigned short dest_port; 704 1.1 elric unsigned short length; 705 1.1 elric unsigned short checksum; 706 1.1 elric } udp; 707 1.1 elric #define SWRAP_PACKET__IP_P_UDP_SIZE 8 708 1.1 elric struct { 709 1.1 elric unsigned char type; 710 1.1 elric unsigned char code; 711 1.1 elric unsigned short checksum; 712 1.1 elric unsigned long unused; 713 1.1 elric } icmp; 714 1.1 elric #define SWRAP_PACKET__IP_P_ICMP_SIZE 8 715 1.1 elric } p; 716 1.1 elric } ip; 717 1.1 elric }; 718 1.1 elric #define SWRAP_PACKET_SIZE 56 719 1.1 elric 720 1.1 elric static const char *socket_wrapper_pcap_file(void) 721 1.1 elric { 722 1.1 elric static int initialized = 0; 723 1.1 elric static const char *s = NULL; 724 1.1 elric static const struct swrap_file_hdr h; 725 1.1 elric static const struct swrap_packet p; 726 1.1 elric 727 1.1 elric if (initialized == 1) { 728 1.1 elric return s; 729 1.1 elric } 730 1.1 elric initialized = 1; 731 1.1 elric 732 1.1 elric /* 733 1.1 elric * TODO: don't use the structs use plain buffer offsets 734 1.1 elric * and PUSH_U8(), PUSH_U16() and PUSH_U32() 735 1.1 elric * 736 1.1 elric * for now make sure we disable PCAP support 737 1.1 elric * if the struct has alignment! 738 1.1 elric */ 739 1.1 elric if (sizeof(h) != SWRAP_FILE_HDR_SIZE) { 740 1.1 elric return NULL; 741 1.1 elric } 742 1.1 elric if (sizeof(p) != SWRAP_PACKET_SIZE) { 743 1.1 elric return NULL; 744 1.1 elric } 745 1.1 elric if (sizeof(p.frame) != SWRAP_PACKET__FRAME_SIZE) { 746 1.1 elric return NULL; 747 1.1 elric } 748 1.1 elric if (sizeof(p.ip.hdr) != SWRAP_PACKET__IP_HDR_SIZE) { 749 1.1 elric return NULL; 750 1.1 elric } 751 1.1 elric if (sizeof(p.ip.p.tcp) != SWRAP_PACKET__IP_P_TCP_SIZE) { 752 1.1 elric return NULL; 753 1.1 elric } 754 1.1 elric if (sizeof(p.ip.p.udp) != SWRAP_PACKET__IP_P_UDP_SIZE) { 755 1.1 elric return NULL; 756 1.1 elric } 757 1.1 elric if (sizeof(p.ip.p.icmp) != SWRAP_PACKET__IP_P_ICMP_SIZE) { 758 1.1 elric return NULL; 759 1.1 elric } 760 1.1 elric 761 1.1 elric s = getenv("SOCKET_WRAPPER_PCAP_FILE"); 762 1.1 elric if (s == NULL) { 763 1.1 elric return NULL; 764 1.1 elric } 765 1.1 elric if (strncmp(s, "./", 2) == 0) { 766 1.1 elric s += 2; 767 1.1 elric } 768 1.1 elric return s; 769 1.1 elric } 770 1.1 elric 771 1.1 elric static struct swrap_packet *swrap_packet_init(struct timeval *tval, 772 1.1 elric const struct sockaddr_in *src_addr, 773 1.1 elric const struct sockaddr_in *dest_addr, 774 1.1 elric int socket_type, 775 1.1 elric const unsigned char *payload, 776 1.1 elric size_t payload_len, 777 1.1 elric unsigned long tcp_seq, 778 1.1 elric unsigned long tcp_ack, 779 1.1 elric unsigned char tcp_ctl, 780 1.1 elric int unreachable, 781 1.1 elric size_t *_packet_len) 782 1.1 elric { 783 1.1 elric struct swrap_packet *ret; 784 1.1 elric struct swrap_packet *packet; 785 1.1 elric size_t packet_len; 786 1.1 elric size_t alloc_len; 787 1.1 elric size_t nonwire_len = sizeof(packet->frame); 788 1.1 elric size_t wire_hdr_len = 0; 789 1.1 elric size_t wire_len = 0; 790 1.1 elric size_t icmp_hdr_len = 0; 791 1.1 elric size_t icmp_truncate_len = 0; 792 1.1 elric unsigned char protocol = 0, icmp_protocol = 0; 793 1.1 elric unsigned short src_port = src_addr->sin_port; 794 1.1 elric unsigned short dest_port = dest_addr->sin_port; 795 1.1 elric 796 1.1 elric switch (socket_type) { 797 1.1 elric case SOCK_STREAM: 798 1.1 elric protocol = 0x06; /* TCP */ 799 1.1 elric wire_hdr_len = sizeof(packet->ip.hdr) + sizeof(packet->ip.p.tcp); 800 1.1 elric wire_len = wire_hdr_len + payload_len; 801 1.1 elric break; 802 1.1 elric 803 1.1 elric case SOCK_DGRAM: 804 1.1 elric protocol = 0x11; /* UDP */ 805 1.1 elric wire_hdr_len = sizeof(packet->ip.hdr) + sizeof(packet->ip.p.udp); 806 1.1 elric wire_len = wire_hdr_len + payload_len; 807 1.1 elric break; 808 1.1 elric } 809 1.1 elric 810 1.1 elric if (unreachable) { 811 1.1 elric icmp_protocol = protocol; 812 1.1 elric protocol = 0x01; /* ICMP */ 813 1.1 elric if (wire_len > 64 ) { 814 1.1 elric icmp_truncate_len = wire_len - 64; 815 1.1 elric } 816 1.1 elric icmp_hdr_len = sizeof(packet->ip.hdr) + sizeof(packet->ip.p.icmp); 817 1.1 elric wire_hdr_len += icmp_hdr_len; 818 1.1 elric wire_len += icmp_hdr_len; 819 1.1 elric } 820 1.1 elric 821 1.1 elric packet_len = nonwire_len + wire_len; 822 1.1 elric alloc_len = packet_len; 823 1.1 elric if (alloc_len < sizeof(struct swrap_packet)) { 824 1.1 elric alloc_len = sizeof(struct swrap_packet); 825 1.1 elric } 826 1.1 elric ret = (struct swrap_packet *)malloc(alloc_len); 827 1.1 elric if (!ret) return NULL; 828 1.1 elric 829 1.1 elric packet = ret; 830 1.1 elric 831 1.1 elric packet->frame.seconds = tval->tv_sec; 832 1.1 elric packet->frame.micro_seconds = tval->tv_usec; 833 1.1 elric packet->frame.recorded_length = wire_len - icmp_truncate_len; 834 1.1 elric packet->frame.full_length = wire_len - icmp_truncate_len; 835 1.1 elric 836 1.1 elric packet->ip.hdr.ver_hdrlen = 0x45; /* version 4 and 5 * 32 bit words */ 837 1.1 elric packet->ip.hdr.tos = 0x00; 838 1.1 elric packet->ip.hdr.packet_length = htons(wire_len - icmp_truncate_len); 839 1.1 elric packet->ip.hdr.identification = htons(0xFFFF); 840 1.1 elric packet->ip.hdr.flags = 0x40; /* BIT 1 set - means don't fraqment */ 841 1.1 elric packet->ip.hdr.fragment = htons(0x0000); 842 1.1 elric packet->ip.hdr.ttl = 0xFF; 843 1.1 elric packet->ip.hdr.protocol = protocol; 844 1.1 elric packet->ip.hdr.hdr_checksum = htons(0x0000); 845 1.1 elric packet->ip.hdr.src_addr = src_addr->sin_addr.s_addr; 846 1.1 elric packet->ip.hdr.dest_addr = dest_addr->sin_addr.s_addr; 847 1.1 elric 848 1.1 elric if (unreachable) { 849 1.1 elric packet->ip.p.icmp.type = 0x03; /* destination unreachable */ 850 1.1 elric packet->ip.p.icmp.code = 0x01; /* host unreachable */ 851 1.1 elric packet->ip.p.icmp.checksum = htons(0x0000); 852 1.1 elric packet->ip.p.icmp.unused = htonl(0x00000000); 853 1.1 elric 854 1.1 elric /* set the ip header in the ICMP payload */ 855 1.1 elric packet = (struct swrap_packet *)(((unsigned char *)ret) + icmp_hdr_len); 856 1.1 elric packet->ip.hdr.ver_hdrlen = 0x45; /* version 4 and 5 * 32 bit words */ 857 1.1 elric packet->ip.hdr.tos = 0x00; 858 1.1 elric packet->ip.hdr.packet_length = htons(wire_len - icmp_hdr_len); 859 1.1 elric packet->ip.hdr.identification = htons(0xFFFF); 860 1.1 elric packet->ip.hdr.flags = 0x40; /* BIT 1 set - means don't fraqment */ 861 1.1 elric packet->ip.hdr.fragment = htons(0x0000); 862 1.1 elric packet->ip.hdr.ttl = 0xFF; 863 1.1 elric packet->ip.hdr.protocol = icmp_protocol; 864 1.1 elric packet->ip.hdr.hdr_checksum = htons(0x0000); 865 1.1 elric packet->ip.hdr.src_addr = dest_addr->sin_addr.s_addr; 866 1.1 elric packet->ip.hdr.dest_addr = src_addr->sin_addr.s_addr; 867 1.1 elric 868 1.1 elric src_port = dest_addr->sin_port; 869 1.1 elric dest_port = src_addr->sin_port; 870 1.1 elric } 871 1.1 elric 872 1.1 elric switch (socket_type) { 873 1.1 elric case SOCK_STREAM: 874 1.1 elric packet->ip.p.tcp.source_port = src_port; 875 1.1 elric packet->ip.p.tcp.dest_port = dest_port; 876 1.1 elric packet->ip.p.tcp.seq_num = htonl(tcp_seq); 877 1.1 elric packet->ip.p.tcp.ack_num = htonl(tcp_ack); 878 1.1 elric packet->ip.p.tcp.hdr_length = 0x50; /* 5 * 32 bit words */ 879 1.1 elric packet->ip.p.tcp.control = tcp_ctl; 880 1.1 elric packet->ip.p.tcp.window = htons(0x7FFF); 881 1.1 elric packet->ip.p.tcp.checksum = htons(0x0000); 882 1.1 elric packet->ip.p.tcp.urg = htons(0x0000); 883 1.1 elric 884 1.1 elric break; 885 1.1 elric 886 1.1 elric case SOCK_DGRAM: 887 1.1 elric packet->ip.p.udp.source_port = src_addr->sin_port; 888 1.1 elric packet->ip.p.udp.dest_port = dest_addr->sin_port; 889 1.1 elric packet->ip.p.udp.length = htons(8 + payload_len); 890 1.1 elric packet->ip.p.udp.checksum = htons(0x0000); 891 1.1 elric 892 1.1 elric break; 893 1.1 elric } 894 1.1 elric 895 1.1 elric if (payload && payload_len > 0) { 896 1.1 elric unsigned char *p = (unsigned char *)ret; 897 1.1 elric p += nonwire_len; 898 1.1 elric p += wire_hdr_len; 899 1.1 elric memcpy(p, payload, payload_len); 900 1.1 elric } 901 1.1 elric 902 1.1 elric *_packet_len = packet_len - icmp_truncate_len; 903 1.1 elric return ret; 904 1.1 elric } 905 1.1 elric 906 1.1 elric static int swrap_get_pcap_fd(const char *fname) 907 1.1 elric { 908 1.1 elric static int fd = -1; 909 1.1 elric 910 1.1 elric if (fd != -1) return fd; 911 1.1 elric 912 1.1 elric fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_APPEND, 0644); 913 1.1 elric if (fd != -1) { 914 1.1 elric struct swrap_file_hdr file_hdr; 915 1.1 elric file_hdr.magic = 0xA1B2C3D4; 916 1.2 christos file_hdr.version_major = 0x0002; 917 1.1 elric file_hdr.version_minor = 0x0004; 918 1.1 elric file_hdr.timezone = 0x00000000; 919 1.1 elric file_hdr.sigfigs = 0x00000000; 920 1.1 elric file_hdr.frame_max_len = SWRAP_FRAME_LENGTH_MAX; 921 1.1 elric file_hdr.link_type = 0x0065; /* 101 RAW IP */ 922 1.1 elric 923 1.1 elric write(fd, &file_hdr, sizeof(file_hdr)); 924 1.1 elric return fd; 925 1.1 elric } 926 1.1 elric 927 1.1 elric fd = open(fname, O_WRONLY|O_APPEND, 0644); 928 1.1 elric 929 1.1 elric return fd; 930 1.1 elric } 931 1.1 elric 932 1.1 elric static void swrap_dump_packet(struct socket_info *si, const struct sockaddr *addr, 933 1.1 elric enum swrap_packet_type type, 934 1.1 elric const void *buf, size_t len) 935 1.1 elric { 936 1.1 elric const struct sockaddr_in *src_addr; 937 1.1 elric const struct sockaddr_in *dest_addr; 938 1.1 elric const char *file_name; 939 1.1 elric unsigned long tcp_seq = 0; 940 1.1 elric unsigned long tcp_ack = 0; 941 1.1 elric unsigned char tcp_ctl = 0; 942 1.1 elric int unreachable = 0; 943 1.1 elric struct timeval tv; 944 1.1 elric struct swrap_packet *packet; 945 1.1 elric size_t packet_len = 0; 946 1.1 elric int fd; 947 1.1 elric 948 1.1 elric file_name = socket_wrapper_pcap_file(); 949 1.1 elric if (!file_name) { 950 1.1 elric return; 951 1.1 elric } 952 1.1 elric 953 1.1 elric switch (si->family) { 954 1.1 elric case AF_INET: 955 1.1 elric #ifdef HAVE_IPV6 956 1.1 elric case AF_INET6: 957 1.1 elric #endif 958 1.1 elric break; 959 1.1 elric default: 960 1.1 elric return; 961 1.1 elric } 962 1.1 elric 963 1.1 elric switch (type) { 964 1.1 elric case SWRAP_CONNECT_SEND: 965 1.1 elric if (si->type != SOCK_STREAM) return; 966 1.1 elric 967 1.1 elric src_addr = (const struct sockaddr_in *)si->myname; 968 1.1 elric dest_addr = (const struct sockaddr_in *)addr; 969 1.1 elric 970 1.1 elric tcp_seq = si->io.pck_snd; 971 1.1 elric tcp_ack = si->io.pck_rcv; 972 1.1 elric tcp_ctl = 0x02; /* SYN */ 973 1.1 elric 974 1.1 elric si->io.pck_snd += 1; 975 1.1 elric 976 1.1 elric break; 977 1.1 elric 978 1.1 elric case SWRAP_CONNECT_RECV: 979 1.1 elric if (si->type != SOCK_STREAM) return; 980 1.1 elric 981 1.1 elric dest_addr = (const struct sockaddr_in *)si->myname; 982 1.1 elric src_addr = (const struct sockaddr_in *)addr; 983 1.1 elric 984 1.1 elric tcp_seq = si->io.pck_rcv; 985 1.1 elric tcp_ack = si->io.pck_snd; 986 1.1 elric tcp_ctl = 0x12; /** SYN,ACK */ 987 1.1 elric 988 1.1 elric si->io.pck_rcv += 1; 989 1.1 elric 990 1.1 elric break; 991 1.1 elric 992 1.1 elric case SWRAP_CONNECT_UNREACH: 993 1.1 elric if (si->type != SOCK_STREAM) return; 994 1.1 elric 995 1.1 elric dest_addr = (const struct sockaddr_in *)si->myname; 996 1.1 elric src_addr = (const struct sockaddr_in *)addr; 997 1.1 elric 998 1.1 elric /* Unreachable: resend the data of SWRAP_CONNECT_SEND */ 999 1.1 elric tcp_seq = si->io.pck_snd - 1; 1000 1.1 elric tcp_ack = si->io.pck_rcv; 1001 1.1 elric tcp_ctl = 0x02; /* SYN */ 1002 1.1 elric unreachable = 1; 1003 1.1 elric 1004 1.1 elric break; 1005 1.1 elric 1006 1.1 elric case SWRAP_CONNECT_ACK: 1007 1.1 elric if (si->type != SOCK_STREAM) return; 1008 1.1 elric 1009 1.1 elric src_addr = (const struct sockaddr_in *)si->myname; 1010 1.1 elric dest_addr = (const struct sockaddr_in *)addr; 1011 1.1 elric 1012 1.1 elric tcp_seq = si->io.pck_snd; 1013 1.1 elric tcp_ack = si->io.pck_rcv; 1014 1.1 elric tcp_ctl = 0x10; /* ACK */ 1015 1.1 elric 1016 1.1 elric break; 1017 1.1 elric 1018 1.1 elric case SWRAP_ACCEPT_SEND: 1019 1.1 elric if (si->type != SOCK_STREAM) return; 1020 1.1 elric 1021 1.1 elric dest_addr = (const struct sockaddr_in *)si->myname; 1022 1.1 elric src_addr = (const struct sockaddr_in *)addr; 1023 1.1 elric 1024 1.1 elric tcp_seq = si->io.pck_rcv; 1025 1.1 elric tcp_ack = si->io.pck_snd; 1026 1.1 elric tcp_ctl = 0x02; /* SYN */ 1027 1.1 elric 1028 1.1 elric si->io.pck_rcv += 1; 1029 1.1 elric 1030 1.1 elric break; 1031 1.1 elric 1032 1.1 elric case SWRAP_ACCEPT_RECV: 1033 1.1 elric if (si->type != SOCK_STREAM) return; 1034 1.1 elric 1035 1.1 elric src_addr = (const struct sockaddr_in *)si->myname; 1036 1.1 elric dest_addr = (const struct sockaddr_in *)addr; 1037 1.1 elric 1038 1.1 elric tcp_seq = si->io.pck_snd; 1039 1.1 elric tcp_ack = si->io.pck_rcv; 1040 1.1 elric tcp_ctl = 0x12; /* SYN,ACK */ 1041 1.1 elric 1042 1.1 elric si->io.pck_snd += 1; 1043 1.1 elric 1044 1.1 elric break; 1045 1.1 elric 1046 1.1 elric case SWRAP_ACCEPT_ACK: 1047 1.1 elric if (si->type != SOCK_STREAM) return; 1048 1.1 elric 1049 1.1 elric dest_addr = (const struct sockaddr_in *)si->myname; 1050 1.1 elric src_addr = (const struct sockaddr_in *)addr; 1051 1.1 elric 1052 1.1 elric tcp_seq = si->io.pck_rcv; 1053 1.1 elric tcp_ack = si->io.pck_snd; 1054 1.1 elric tcp_ctl = 0x10; /* ACK */ 1055 1.1 elric 1056 1.1 elric break; 1057 1.1 elric 1058 1.1 elric case SWRAP_SEND: 1059 1.1 elric src_addr = (const struct sockaddr_in *)si->myname; 1060 1.1 elric dest_addr = (const struct sockaddr_in *)si->peername; 1061 1.1 elric 1062 1.1 elric tcp_seq = si->io.pck_snd; 1063 1.1 elric tcp_ack = si->io.pck_rcv; 1064 1.1 elric tcp_ctl = 0x18; /* PSH,ACK */ 1065 1.1 elric 1066 1.1 elric si->io.pck_snd += len; 1067 1.1 elric 1068 1.1 elric break; 1069 1.1 elric 1070 1.1 elric case SWRAP_SEND_RST: 1071 1.1 elric dest_addr = (const struct sockaddr_in *)si->myname; 1072 1.1 elric src_addr = (const struct sockaddr_in *)si->peername; 1073 1.1 elric 1074 1.1 elric if (si->type == SOCK_DGRAM) { 1075 1.1 elric swrap_dump_packet(si, si->peername, 1076 1.1 elric SWRAP_SENDTO_UNREACH, 1077 1.1 elric buf, len); 1078 1.1 elric return; 1079 1.1 elric } 1080 1.1 elric 1081 1.1 elric tcp_seq = si->io.pck_rcv; 1082 1.1 elric tcp_ack = si->io.pck_snd; 1083 1.1 elric tcp_ctl = 0x14; /** RST,ACK */ 1084 1.1 elric 1085 1.1 elric break; 1086 1.1 elric 1087 1.1 elric case SWRAP_PENDING_RST: 1088 1.1 elric dest_addr = (const struct sockaddr_in *)si->myname; 1089 1.1 elric src_addr = (const struct sockaddr_in *)si->peername; 1090 1.1 elric 1091 1.1 elric if (si->type == SOCK_DGRAM) { 1092 1.1 elric return; 1093 1.1 elric } 1094 1.1 elric 1095 1.1 elric tcp_seq = si->io.pck_rcv; 1096 1.1 elric tcp_ack = si->io.pck_snd; 1097 1.1 elric tcp_ctl = 0x14; /* RST,ACK */ 1098 1.1 elric 1099 1.1 elric break; 1100 1.1 elric 1101 1.1 elric case SWRAP_RECV: 1102 1.1 elric dest_addr = (const struct sockaddr_in *)si->myname; 1103 1.1 elric src_addr = (const struct sockaddr_in *)si->peername; 1104 1.1 elric 1105 1.1 elric tcp_seq = si->io.pck_rcv; 1106 1.1 elric tcp_ack = si->io.pck_snd; 1107 1.1 elric tcp_ctl = 0x18; /* PSH,ACK */ 1108 1.1 elric 1109 1.1 elric si->io.pck_rcv += len; 1110 1.1 elric 1111 1.1 elric break; 1112 1.1 elric 1113 1.1 elric case SWRAP_RECV_RST: 1114 1.1 elric dest_addr = (const struct sockaddr_in *)si->myname; 1115 1.1 elric src_addr = (const struct sockaddr_in *)si->peername; 1116 1.1 elric 1117 1.1 elric if (si->type == SOCK_DGRAM) { 1118 1.1 elric return; 1119 1.1 elric } 1120 1.1 elric 1121 1.1 elric tcp_seq = si->io.pck_rcv; 1122 1.1 elric tcp_ack = si->io.pck_snd; 1123 1.1 elric tcp_ctl = 0x14; /* RST,ACK */ 1124 1.1 elric 1125 1.1 elric break; 1126 1.1 elric 1127 1.1 elric case SWRAP_SENDTO: 1128 1.1 elric src_addr = (const struct sockaddr_in *)si->myname; 1129 1.1 elric dest_addr = (const struct sockaddr_in *)addr; 1130 1.1 elric 1131 1.1 elric si->io.pck_snd += len; 1132 1.1 elric 1133 1.1 elric break; 1134 1.1 elric 1135 1.1 elric case SWRAP_SENDTO_UNREACH: 1136 1.1 elric dest_addr = (const struct sockaddr_in *)si->myname; 1137 1.1 elric src_addr = (const struct sockaddr_in *)addr; 1138 1.1 elric 1139 1.1 elric unreachable = 1; 1140 1.1 elric 1141 1.1 elric break; 1142 1.1 elric 1143 1.1 elric case SWRAP_RECVFROM: 1144 1.1 elric dest_addr = (const struct sockaddr_in *)si->myname; 1145 1.1 elric src_addr = (const struct sockaddr_in *)addr; 1146 1.1 elric 1147 1.1 elric si->io.pck_rcv += len; 1148 1.1 elric 1149 1.1 elric break; 1150 1.1 elric 1151 1.1 elric case SWRAP_CLOSE_SEND: 1152 1.1 elric if (si->type != SOCK_STREAM) return; 1153 1.1 elric 1154 1.1 elric src_addr = (const struct sockaddr_in *)si->myname; 1155 1.1 elric dest_addr = (const struct sockaddr_in *)si->peername; 1156 1.1 elric 1157 1.1 elric tcp_seq = si->io.pck_snd; 1158 1.1 elric tcp_ack = si->io.pck_rcv; 1159 1.1 elric tcp_ctl = 0x11; /* FIN, ACK */ 1160 1.1 elric 1161 1.1 elric si->io.pck_snd += 1; 1162 1.1 elric 1163 1.1 elric break; 1164 1.1 elric 1165 1.1 elric case SWRAP_CLOSE_RECV: 1166 1.1 elric if (si->type != SOCK_STREAM) return; 1167 1.1 elric 1168 1.1 elric dest_addr = (const struct sockaddr_in *)si->myname; 1169 1.1 elric src_addr = (const struct sockaddr_in *)si->peername; 1170 1.1 elric 1171 1.1 elric tcp_seq = si->io.pck_rcv; 1172 1.1 elric tcp_ack = si->io.pck_snd; 1173 1.1 elric tcp_ctl = 0x11; /* FIN,ACK */ 1174 1.1 elric 1175 1.1 elric si->io.pck_rcv += 1; 1176 1.1 elric 1177 1.1 elric break; 1178 1.1 elric 1179 1.1 elric case SWRAP_CLOSE_ACK: 1180 1.1 elric if (si->type != SOCK_STREAM) return; 1181 1.1 elric 1182 1.1 elric src_addr = (const struct sockaddr_in *)si->myname; 1183 1.1 elric dest_addr = (const struct sockaddr_in *)si->peername; 1184 1.1 elric 1185 1.1 elric tcp_seq = si->io.pck_snd; 1186 1.1 elric tcp_ack = si->io.pck_rcv; 1187 1.1 elric tcp_ctl = 0x10; /* ACK */ 1188 1.1 elric 1189 1.1 elric break; 1190 1.1 elric default: 1191 1.1 elric return; 1192 1.1 elric } 1193 1.1 elric 1194 1.1 elric swrapGetTimeOfDay(&tv); 1195 1.1 elric 1196 1.1 elric packet = swrap_packet_init(&tv, src_addr, dest_addr, si->type, 1197 1.1 elric (const unsigned char *)buf, len, 1198 1.1 elric tcp_seq, tcp_ack, tcp_ctl, unreachable, 1199 1.1 elric &packet_len); 1200 1.1 elric if (!packet) { 1201 1.1 elric return; 1202 1.1 elric } 1203 1.1 elric 1204 1.1 elric fd = swrap_get_pcap_fd(file_name); 1205 1.1 elric if (fd != -1) { 1206 1.1 elric write(fd, packet, packet_len); 1207 1.1 elric } 1208 1.1 elric 1209 1.1 elric free(packet); 1210 1.1 elric } 1211 1.1 elric 1212 1.1 elric _PUBLIC_ int swrap_socket(int family, int type, int protocol) 1213 1.1 elric { 1214 1.1 elric struct socket_info *si; 1215 1.1 elric int fd; 1216 1.1 elric 1217 1.1 elric if (!socket_wrapper_dir()) { 1218 1.1 elric return real_socket(family, type, protocol); 1219 1.1 elric } 1220 1.1 elric 1221 1.1 elric switch (family) { 1222 1.1 elric case AF_INET: 1223 1.1 elric #ifdef HAVE_IPV6 1224 1.1 elric case AF_INET6: 1225 1.1 elric #endif 1226 1.1 elric break; 1227 1.1 elric case AF_UNIX: 1228 1.1 elric return real_socket(family, type, protocol); 1229 1.1 elric default: 1230 1.1 elric errno = EAFNOSUPPORT; 1231 1.1 elric return -1; 1232 1.1 elric } 1233 1.1 elric 1234 1.1 elric switch (type) { 1235 1.1 elric case SOCK_STREAM: 1236 1.1 elric break; 1237 1.1 elric case SOCK_DGRAM: 1238 1.1 elric break; 1239 1.1 elric default: 1240 1.1 elric errno = EPROTONOSUPPORT; 1241 1.1 elric return -1; 1242 1.1 elric } 1243 1.1 elric 1244 1.1 elric #if 0 1245 1.1 elric switch (protocol) { 1246 1.1 elric case 0: 1247 1.1 elric break; 1248 1.1 elric default: 1249 1.1 elric errno = EPROTONOSUPPORT; 1250 1.1 elric return -1; 1251 1.1 elric } 1252 1.1 elric #endif 1253 1.1 elric 1254 1.1 elric fd = real_socket(AF_UNIX, type, 0); 1255 1.1 elric 1256 1.1 elric if (fd == -1) return -1; 1257 1.1 elric 1258 1.1 elric si = (struct socket_info *)calloc(1, sizeof(struct socket_info)); 1259 1.1 elric 1260 1.1 elric si->family = family; 1261 1.1 elric si->type = type; 1262 1.1 elric si->protocol = protocol; 1263 1.1 elric si->fd = fd; 1264 1.1 elric 1265 1.1 elric SWRAP_DLIST_ADD(sockets, si); 1266 1.1 elric 1267 1.1 elric return si->fd; 1268 1.1 elric } 1269 1.1 elric 1270 1.1 elric _PUBLIC_ int swrap_accept(int s, struct sockaddr *addr, socklen_t *addrlen) 1271 1.1 elric { 1272 1.1 elric struct socket_info *parent_si, *child_si; 1273 1.1 elric int fd; 1274 1.1 elric struct sockaddr_un un_addr; 1275 1.1 elric socklen_t un_addrlen = sizeof(un_addr); 1276 1.1 elric struct sockaddr_un un_my_addr; 1277 1.1 elric socklen_t un_my_addrlen = sizeof(un_my_addr); 1278 1.1 elric struct sockaddr *my_addr; 1279 1.1 elric socklen_t my_addrlen, len; 1280 1.1 elric int ret; 1281 1.1 elric 1282 1.1 elric parent_si = find_socket_info(s); 1283 1.1 elric if (!parent_si) { 1284 1.1 elric return real_accept(s, addr, addrlen); 1285 1.1 elric } 1286 1.1 elric 1287 1.1 elric /* 1288 1.1 elric * assume out sockaddr have the same size as the in parent 1289 1.1 elric * socket family 1290 1.1 elric */ 1291 1.1 elric my_addrlen = socket_length(parent_si->family); 1292 1.1 elric if (my_addrlen < 0) { 1293 1.1 elric errno = EINVAL; 1294 1.1 elric return -1; 1295 1.1 elric } 1296 1.1 elric 1297 1.1 elric my_addr = malloc(my_addrlen); 1298 1.1 elric if (my_addr == NULL) { 1299 1.1 elric return -1; 1300 1.1 elric } 1301 1.1 elric 1302 1.1 elric memset(&un_addr, 0, sizeof(un_addr)); 1303 1.1 elric memset(&un_my_addr, 0, sizeof(un_my_addr)); 1304 1.1 elric 1305 1.1 elric ret = real_accept(s, (struct sockaddr *)&un_addr, &un_addrlen); 1306 1.1 elric if (ret == -1) { 1307 1.1 elric free(my_addr); 1308 1.1 elric return ret; 1309 1.1 elric } 1310 1.1 elric 1311 1.1 elric fd = ret; 1312 1.1 elric 1313 1.1 elric len = my_addrlen; 1314 1.1 elric ret = sockaddr_convert_from_un(parent_si, &un_addr, un_addrlen, 1315 1.1 elric parent_si->family, my_addr, &len); 1316 1.1 elric if (ret == -1) { 1317 1.1 elric free(my_addr); 1318 1.1 elric close(fd); 1319 1.1 elric return ret; 1320 1.1 elric } 1321 1.1 elric 1322 1.1 elric child_si = (struct socket_info *)malloc(sizeof(struct socket_info)); 1323 1.1 elric memset(child_si, 0, sizeof(*child_si)); 1324 1.1 elric 1325 1.1 elric child_si->fd = fd; 1326 1.1 elric child_si->family = parent_si->family; 1327 1.1 elric child_si->type = parent_si->type; 1328 1.1 elric child_si->protocol = parent_si->protocol; 1329 1.1 elric child_si->bound = 1; 1330 1.1 elric child_si->is_server = 1; 1331 1.1 elric 1332 1.1 elric child_si->peername_len = len; 1333 1.1 elric child_si->peername = sockaddr_dup(my_addr, len); 1334 1.1 elric 1335 1.1 elric if (addr != NULL && addrlen != NULL) { 1336 1.1 elric *addrlen = len; 1337 1.1 elric if (*addrlen >= len) 1338 1.1 elric memcpy(addr, my_addr, len); 1339 1.1 elric *addrlen = 0; 1340 1.1 elric } 1341 1.1 elric 1342 1.1 elric ret = real_getsockname(fd, (struct sockaddr *)&un_my_addr, &un_my_addrlen); 1343 1.1 elric if (ret == -1) { 1344 1.1 elric free(child_si); 1345 1.1 elric close(fd); 1346 1.1 elric return ret; 1347 1.1 elric } 1348 1.1 elric 1349 1.1 elric len = my_addrlen; 1350 1.1 elric ret = sockaddr_convert_from_un(child_si, &un_my_addr, un_my_addrlen, 1351 1.1 elric child_si->family, my_addr, &len); 1352 1.1 elric if (ret == -1) { 1353 1.1 elric free(child_si); 1354 1.1 elric free(my_addr); 1355 1.1 elric close(fd); 1356 1.1 elric return ret; 1357 1.1 elric } 1358 1.1 elric 1359 1.1 elric child_si->myname_len = len; 1360 1.1 elric child_si->myname = sockaddr_dup(my_addr, len); 1361 1.1 elric free(my_addr); 1362 1.1 elric 1363 1.1 elric SWRAP_DLIST_ADD(sockets, child_si); 1364 1.1 elric 1365 1.1 elric swrap_dump_packet(child_si, addr, SWRAP_ACCEPT_SEND, NULL, 0); 1366 1.1 elric swrap_dump_packet(child_si, addr, SWRAP_ACCEPT_RECV, NULL, 0); 1367 1.1 elric swrap_dump_packet(child_si, addr, SWRAP_ACCEPT_ACK, NULL, 0); 1368 1.1 elric 1369 1.1 elric return fd; 1370 1.1 elric } 1371 1.1 elric 1372 1.1 elric static int autobind_start_init; 1373 1.1 elric static int autobind_start; 1374 1.1 elric 1375 1.1 elric /* using sendto() or connect() on an unbound socket would give the 1376 1.1 elric recipient no way to reply, as unlike UDP and TCP, a unix domain 1377 1.1 elric socket can't auto-assign emphemeral port numbers, so we need to 1378 1.1 elric assign it here */ 1379 1.1 elric static int swrap_auto_bind(struct socket_info *si) 1380 1.1 elric { 1381 1.1 elric struct sockaddr_un un_addr; 1382 1.1 elric int i; 1383 1.1 elric char type; 1384 1.1 elric int ret; 1385 1.1 elric int port; 1386 1.1 elric struct stat st; 1387 1.1 elric 1388 1.1 elric if (autobind_start_init != 1) { 1389 1.1 elric autobind_start_init = 1; 1390 1.1 elric autobind_start = getpid(); 1391 1.1 elric autobind_start %= 50000; 1392 1.1 elric autobind_start += 10000; 1393 1.1 elric } 1394 1.1 elric 1395 1.1 elric un_addr.sun_family = AF_UNIX; 1396 1.1 elric 1397 1.1 elric switch (si->family) { 1398 1.1 elric case AF_INET: { 1399 1.1 elric struct sockaddr_in in; 1400 1.1 elric 1401 1.1 elric switch (si->type) { 1402 1.1 elric case SOCK_STREAM: 1403 1.1 elric type = SOCKET_TYPE_CHAR_TCP; 1404 1.1 elric break; 1405 1.1 elric case SOCK_DGRAM: 1406 1.1 elric type = SOCKET_TYPE_CHAR_UDP; 1407 1.1 elric break; 1408 1.1 elric default: 1409 1.1 elric errno = ESOCKTNOSUPPORT; 1410 1.1 elric return -1; 1411 1.1 elric } 1412 1.1 elric 1413 1.1 elric memset(&in, 0, sizeof(in)); 1414 1.1 elric in.sin_family = AF_INET; 1415 1.1 elric in.sin_addr.s_addr = htonl(127<<24 | 1416 1.1 elric socket_wrapper_default_iface()); 1417 1.1 elric 1418 1.1 elric si->myname_len = sizeof(in); 1419 1.1 elric si->myname = sockaddr_dup(&in, si->myname_len); 1420 1.1 elric break; 1421 1.1 elric } 1422 1.1 elric #ifdef HAVE_IPV6 1423 1.1 elric case AF_INET6: { 1424 1.1 elric struct sockaddr_in6 in6; 1425 1.1 elric 1426 1.1 elric switch (si->type) { 1427 1.1 elric case SOCK_STREAM: 1428 1.1 elric type = SOCKET_TYPE_CHAR_TCP_V6; 1429 1.1 elric break; 1430 1.1 elric case SOCK_DGRAM: 1431 1.1 elric type = SOCKET_TYPE_CHAR_UDP_V6; 1432 1.1 elric break; 1433 1.1 elric default: 1434 1.1 elric errno = ESOCKTNOSUPPORT; 1435 1.1 elric return -1; 1436 1.1 elric } 1437 1.1 elric 1438 1.1 elric memset(&in6, 0, sizeof(in6)); 1439 1.1 elric in6.sin6_family = AF_INET6; 1440 1.1 elric in6.sin6_addr.s6_addr[0] = SW_IPV6_ADDRESS; 1441 1.1 elric si->myname_len = sizeof(in6); 1442 1.1 elric si->myname = sockaddr_dup(&in6, si->myname_len); 1443 1.1 elric break; 1444 1.1 elric } 1445 1.1 elric #endif 1446 1.1 elric default: 1447 1.1 elric errno = ESOCKTNOSUPPORT; 1448 1.1 elric return -1; 1449 1.1 elric } 1450 1.1 elric 1451 1.1 elric if (autobind_start > 60000) { 1452 1.1 elric autobind_start = 10000; 1453 1.1 elric } 1454 1.1 elric 1455 1.1 elric for (i=0;i<1000;i++) { 1456 1.1 elric port = autobind_start + i; 1457 1.1 elric snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), 1458 1.1 elric "%s/"SOCKET_FORMAT, socket_wrapper_dir(), 1459 1.1 elric type, socket_wrapper_default_iface(), port); 1460 1.1 elric if (stat(un_addr.sun_path, &st) == 0) continue; 1461 1.2 christos 1462 1.1 elric ret = real_bind(si->fd, (struct sockaddr *)&un_addr, sizeof(un_addr)); 1463 1.1 elric if (ret == -1) return ret; 1464 1.1 elric 1465 1.1 elric si->tmp_path = strdup(un_addr.sun_path); 1466 1.1 elric si->bound = 1; 1467 1.1 elric autobind_start = port + 1; 1468 1.1 elric break; 1469 1.1 elric } 1470 1.1 elric if (i == 1000) { 1471 1.1 elric errno = ENFILE; 1472 1.1 elric return -1; 1473 1.1 elric } 1474 1.1 elric 1475 1.1 elric set_port(si->family, port, si->myname); 1476 1.1 elric 1477 1.1 elric return 0; 1478 1.1 elric } 1479 1.1 elric 1480 1.1 elric 1481 1.1 elric _PUBLIC_ int swrap_connect(int s, const struct sockaddr *serv_addr, socklen_t addrlen) 1482 1.1 elric { 1483 1.1 elric int ret; 1484 1.1 elric struct sockaddr_un un_addr; 1485 1.1 elric struct socket_info *si = find_socket_info(s); 1486 1.1 elric 1487 1.1 elric if (!si) { 1488 1.1 elric return real_connect(s, serv_addr, addrlen); 1489 1.1 elric } 1490 1.1 elric 1491 1.1 elric if (si->bound == 0) { 1492 1.1 elric ret = swrap_auto_bind(si); 1493 1.1 elric if (ret == -1) return -1; 1494 1.1 elric } 1495 1.1 elric 1496 1.1 elric if (si->family != serv_addr->sa_family) { 1497 1.1 elric errno = EINVAL; 1498 1.1 elric return -1; 1499 1.1 elric } 1500 1.1 elric 1501 1.1 elric ret = sockaddr_convert_to_un(si, (const struct sockaddr *)serv_addr, addrlen, &un_addr, 0, NULL); 1502 1.1 elric if (ret == -1) return -1; 1503 1.1 elric 1504 1.1 elric swrap_dump_packet(si, serv_addr, SWRAP_CONNECT_SEND, NULL, 0); 1505 1.1 elric 1506 1.1 elric ret = real_connect(s, (struct sockaddr *)&un_addr, 1507 1.1 elric sizeof(struct sockaddr_un)); 1508 1.1 elric 1509 1.1 elric /* to give better errors */ 1510 1.1 elric if (ret == -1 && errno == ENOENT) { 1511 1.1 elric errno = EHOSTUNREACH; 1512 1.1 elric } 1513 1.1 elric 1514 1.1 elric if (ret == 0) { 1515 1.1 elric si->peername_len = addrlen; 1516 1.1 elric si->peername = sockaddr_dup(serv_addr, addrlen); 1517 1.1 elric 1518 1.1 elric swrap_dump_packet(si, serv_addr, SWRAP_CONNECT_RECV, NULL, 0); 1519 1.1 elric swrap_dump_packet(si, serv_addr, SWRAP_CONNECT_ACK, NULL, 0); 1520 1.1 elric } else { 1521 1.1 elric swrap_dump_packet(si, serv_addr, SWRAP_CONNECT_UNREACH, NULL, 0); 1522 1.1 elric } 1523 1.1 elric 1524 1.1 elric return ret; 1525 1.1 elric } 1526 1.1 elric 1527 1.1 elric _PUBLIC_ int swrap_bind(int s, const struct sockaddr *myaddr, socklen_t addrlen) 1528 1.1 elric { 1529 1.1 elric int ret; 1530 1.1 elric struct sockaddr_un un_addr; 1531 1.1 elric struct socket_info *si = find_socket_info(s); 1532 1.1 elric 1533 1.1 elric if (!si) { 1534 1.1 elric return real_bind(s, myaddr, addrlen); 1535 1.1 elric } 1536 1.1 elric 1537 1.1 elric si->myname_len = addrlen; 1538 1.1 elric si->myname = sockaddr_dup(myaddr, addrlen); 1539 1.1 elric 1540 1.1 elric ret = sockaddr_convert_to_un(si, (const struct sockaddr *)myaddr, addrlen, &un_addr, 1, &si->bcast); 1541 1.1 elric if (ret == -1) return -1; 1542 1.1 elric 1543 1.1 elric unlink(un_addr.sun_path); 1544 1.1 elric 1545 1.1 elric ret = real_bind(s, (struct sockaddr *)&un_addr, 1546 1.1 elric sizeof(struct sockaddr_un)); 1547 1.1 elric 1548 1.1 elric if (ret == 0) { 1549 1.1 elric si->bound = 1; 1550 1.1 elric } 1551 1.1 elric 1552 1.1 elric return ret; 1553 1.1 elric } 1554 1.1 elric 1555 1.1 elric _PUBLIC_ int swrap_listen(int s, int backlog) 1556 1.1 elric { 1557 1.1 elric int ret; 1558 1.1 elric struct socket_info *si = find_socket_info(s); 1559 1.1 elric 1560 1.1 elric if (!si) { 1561 1.1 elric return real_listen(s, backlog); 1562 1.1 elric } 1563 1.1 elric 1564 1.1 elric ret = real_listen(s, backlog); 1565 1.1 elric 1566 1.1 elric return ret; 1567 1.1 elric } 1568 1.1 elric 1569 1.1 elric _PUBLIC_ int swrap_getpeername(int s, struct sockaddr *name, socklen_t *addrlen) 1570 1.1 elric { 1571 1.1 elric struct socket_info *si = find_socket_info(s); 1572 1.1 elric 1573 1.1 elric if (!si) { 1574 1.1 elric return real_getpeername(s, name, addrlen); 1575 1.1 elric } 1576 1.1 elric 1577 1.1 elric if (!si->peername) 1578 1.1 elric { 1579 1.1 elric errno = ENOTCONN; 1580 1.1 elric return -1; 1581 1.1 elric } 1582 1.1 elric 1583 1.1 elric memcpy(name, si->peername, si->peername_len); 1584 1.1 elric *addrlen = si->peername_len; 1585 1.1 elric 1586 1.1 elric return 0; 1587 1.1 elric } 1588 1.1 elric 1589 1.1 elric _PUBLIC_ int swrap_getsockname(int s, struct sockaddr *name, socklen_t *addrlen) 1590 1.1 elric { 1591 1.1 elric struct socket_info *si = find_socket_info(s); 1592 1.1 elric 1593 1.1 elric if (!si) { 1594 1.1 elric return real_getsockname(s, name, addrlen); 1595 1.1 elric } 1596 1.1 elric 1597 1.1 elric memcpy(name, si->myname, si->myname_len); 1598 1.1 elric *addrlen = si->myname_len; 1599 1.1 elric 1600 1.1 elric return 0; 1601 1.1 elric } 1602 1.1 elric 1603 1.1 elric _PUBLIC_ int swrap_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen) 1604 1.1 elric { 1605 1.1 elric struct socket_info *si = find_socket_info(s); 1606 1.1 elric 1607 1.1 elric if (!si) { 1608 1.1 elric return real_getsockopt(s, level, optname, optval, optlen); 1609 1.1 elric } 1610 1.1 elric 1611 1.1 elric if (level == SOL_SOCKET) { 1612 1.1 elric return real_getsockopt(s, level, optname, optval, optlen); 1613 1.1 elric } 1614 1.1 elric 1615 1.1 elric errno = ENOPROTOOPT; 1616 1.1 elric return -1; 1617 1.1 elric } 1618 1.1 elric 1619 1.1 elric _PUBLIC_ int swrap_setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen) 1620 1.1 elric { 1621 1.1 elric struct socket_info *si = find_socket_info(s); 1622 1.1 elric 1623 1.1 elric if (!si) { 1624 1.1 elric return real_setsockopt(s, level, optname, optval, optlen); 1625 1.1 elric } 1626 1.1 elric 1627 1.1 elric if (level == SOL_SOCKET) { 1628 1.1 elric return real_setsockopt(s, level, optname, optval, optlen); 1629 1.1 elric } 1630 1.1 elric 1631 1.1 elric switch (si->family) { 1632 1.1 elric case AF_INET: 1633 1.1 elric return 0; 1634 1.1 elric default: 1635 1.1 elric errno = ENOPROTOOPT; 1636 1.1 elric return -1; 1637 1.1 elric } 1638 1.1 elric } 1639 1.1 elric 1640 1.1 elric _PUBLIC_ ssize_t swrap_recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen) 1641 1.1 elric { 1642 1.1 elric struct sockaddr_un un_addr; 1643 1.1 elric socklen_t un_addrlen = sizeof(un_addr); 1644 1.1 elric int ret; 1645 1.1 elric struct socket_info *si = find_socket_info(s); 1646 1.1 elric 1647 1.1 elric if (!si) { 1648 1.1 elric return real_recvfrom(s, buf, len, flags, from, fromlen); 1649 1.1 elric } 1650 1.1 elric 1651 1.1 elric /* irix 6.4 forgets to null terminate the sun_path string :-( */ 1652 1.1 elric memset(&un_addr, 0, sizeof(un_addr)); 1653 1.1 elric ret = real_recvfrom(s, buf, len, flags, (struct sockaddr *)&un_addr, &un_addrlen); 1654 1.1 elric if (ret == -1) 1655 1.1 elric return ret; 1656 1.1 elric 1657 1.1 elric if (sockaddr_convert_from_un(si, &un_addr, un_addrlen, 1658 1.1 elric si->family, from, fromlen) == -1) { 1659 1.1 elric return -1; 1660 1.1 elric } 1661 1.1 elric 1662 1.1 elric swrap_dump_packet(si, from, SWRAP_RECVFROM, buf, ret); 1663 1.1 elric 1664 1.1 elric return ret; 1665 1.1 elric } 1666 1.1 elric 1667 1.1 elric 1668 1.1 elric _PUBLIC_ ssize_t swrap_sendto(int s, const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen) 1669 1.1 elric { 1670 1.1 elric struct sockaddr_un un_addr; 1671 1.1 elric int ret; 1672 1.1 elric struct socket_info *si = find_socket_info(s); 1673 1.1 elric int bcast = 0; 1674 1.1 elric 1675 1.1 elric if (!si) { 1676 1.1 elric return real_sendto(s, buf, len, flags, to, tolen); 1677 1.1 elric } 1678 1.1 elric 1679 1.1 elric switch (si->type) { 1680 1.1 elric case SOCK_STREAM: 1681 1.1 elric ret = real_send(s, buf, len, flags); 1682 1.1 elric break; 1683 1.1 elric case SOCK_DGRAM: 1684 1.1 elric if (si->bound == 0) { 1685 1.1 elric ret = swrap_auto_bind(si); 1686 1.1 elric if (ret == -1) return -1; 1687 1.1 elric } 1688 1.2 christos 1689 1.1 elric ret = sockaddr_convert_to_un(si, to, tolen, &un_addr, 0, &bcast); 1690 1.1 elric if (ret == -1) return -1; 1691 1.2 christos 1692 1.1 elric if (bcast) { 1693 1.1 elric struct stat st; 1694 1.1 elric unsigned int iface; 1695 1.1 elric unsigned int prt = ntohs(((const struct sockaddr_in *)to)->sin_port); 1696 1.1 elric char type; 1697 1.2 christos 1698 1.1 elric type = SOCKET_TYPE_CHAR_UDP; 1699 1.2 christos 1700 1.1 elric for(iface=0; iface <= MAX_WRAPPED_INTERFACES; iface++) { 1701 1.1 elric snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), "%s/"SOCKET_FORMAT, 1702 1.1 elric socket_wrapper_dir(), type, iface, prt); 1703 1.1 elric if (stat(un_addr.sun_path, &st) != 0) continue; 1704 1.2 christos 1705 1.1 elric /* ignore the any errors in broadcast sends */ 1706 1.1 elric real_sendto(s, buf, len, flags, (struct sockaddr *)&un_addr, sizeof(un_addr)); 1707 1.1 elric } 1708 1.2 christos 1709 1.1 elric swrap_dump_packet(si, to, SWRAP_SENDTO, buf, len); 1710 1.2 christos 1711 1.1 elric return len; 1712 1.1 elric } 1713 1.2 christos 1714 1.1 elric ret = real_sendto(s, buf, len, flags, (struct sockaddr *)&un_addr, sizeof(un_addr)); 1715 1.1 elric break; 1716 1.1 elric default: 1717 1.1 elric ret = -1; 1718 1.1 elric errno = EHOSTUNREACH; 1719 1.1 elric break; 1720 1.1 elric } 1721 1.2 christos 1722 1.1 elric /* to give better errors */ 1723 1.1 elric if (ret == -1 && errno == ENOENT) { 1724 1.1 elric errno = EHOSTUNREACH; 1725 1.1 elric } 1726 1.1 elric 1727 1.1 elric if (ret == -1) { 1728 1.1 elric swrap_dump_packet(si, to, SWRAP_SENDTO, buf, len); 1729 1.1 elric swrap_dump_packet(si, to, SWRAP_SENDTO_UNREACH, buf, len); 1730 1.1 elric } else { 1731 1.1 elric swrap_dump_packet(si, to, SWRAP_SENDTO, buf, ret); 1732 1.1 elric } 1733 1.1 elric 1734 1.1 elric return ret; 1735 1.1 elric } 1736 1.1 elric 1737 1.1 elric _PUBLIC_ int swrap_ioctl(int s, int r, void *p) 1738 1.1 elric { 1739 1.1 elric int ret; 1740 1.1 elric struct socket_info *si = find_socket_info(s); 1741 1.1 elric int value; 1742 1.1 elric 1743 1.1 elric if (!si) { 1744 1.1 elric return real_ioctl(s, r, p); 1745 1.1 elric } 1746 1.1 elric 1747 1.1 elric ret = real_ioctl(s, r, p); 1748 1.1 elric 1749 1.1 elric switch (r) { 1750 1.1 elric case FIONREAD: 1751 1.1 elric value = *((int *)p); 1752 1.1 elric if (ret == -1 && errno != EAGAIN && errno != ENOBUFS) { 1753 1.1 elric swrap_dump_packet(si, NULL, SWRAP_PENDING_RST, NULL, 0); 1754 1.1 elric } else if (value == 0) { /* END OF FILE */ 1755 1.1 elric swrap_dump_packet(si, NULL, SWRAP_PENDING_RST, NULL, 0); 1756 1.1 elric } 1757 1.1 elric break; 1758 1.1 elric } 1759 1.1 elric 1760 1.1 elric return ret; 1761 1.1 elric } 1762 1.1 elric 1763 1.1 elric _PUBLIC_ ssize_t swrap_recv(int s, void *buf, size_t len, int flags) 1764 1.1 elric { 1765 1.1 elric int ret; 1766 1.1 elric struct socket_info *si = find_socket_info(s); 1767 1.1 elric 1768 1.1 elric if (!si) { 1769 1.1 elric return real_recv(s, buf, len, flags); 1770 1.1 elric } 1771 1.1 elric 1772 1.1 elric ret = real_recv(s, buf, len, flags); 1773 1.1 elric if (ret == -1 && errno != EAGAIN && errno != ENOBUFS) { 1774 1.1 elric swrap_dump_packet(si, NULL, SWRAP_RECV_RST, NULL, 0); 1775 1.1 elric } else if (ret == 0) { /* END OF FILE */ 1776 1.1 elric swrap_dump_packet(si, NULL, SWRAP_RECV_RST, NULL, 0); 1777 1.1 elric } else { 1778 1.1 elric swrap_dump_packet(si, NULL, SWRAP_RECV, buf, ret); 1779 1.1 elric } 1780 1.1 elric 1781 1.1 elric return ret; 1782 1.1 elric } 1783 1.1 elric 1784 1.1 elric 1785 1.1 elric _PUBLIC_ ssize_t swrap_send(int s, const void *buf, size_t len, int flags) 1786 1.1 elric { 1787 1.1 elric int ret; 1788 1.1 elric struct socket_info *si = find_socket_info(s); 1789 1.1 elric 1790 1.1 elric if (!si) { 1791 1.1 elric return real_send(s, buf, len, flags); 1792 1.1 elric } 1793 1.1 elric 1794 1.1 elric ret = real_send(s, buf, len, flags); 1795 1.1 elric 1796 1.1 elric if (ret == -1) { 1797 1.1 elric swrap_dump_packet(si, NULL, SWRAP_SEND, buf, len); 1798 1.1 elric swrap_dump_packet(si, NULL, SWRAP_SEND_RST, NULL, 0); 1799 1.1 elric } else { 1800 1.1 elric swrap_dump_packet(si, NULL, SWRAP_SEND, buf, ret); 1801 1.1 elric } 1802 1.1 elric 1803 1.1 elric return ret; 1804 1.1 elric } 1805 1.1 elric 1806 1.1 elric _PUBLIC_ int swrap_close(int fd) 1807 1.1 elric { 1808 1.1 elric struct socket_info *si = find_socket_info(fd); 1809 1.1 elric int ret; 1810 1.1 elric 1811 1.1 elric if (!si) { 1812 1.1 elric return real_close(fd); 1813 1.1 elric } 1814 1.1 elric 1815 1.1 elric SWRAP_DLIST_REMOVE(sockets, si); 1816 1.1 elric 1817 1.1 elric if (si->myname && si->peername) { 1818 1.1 elric swrap_dump_packet(si, NULL, SWRAP_CLOSE_SEND, NULL, 0); 1819 1.1 elric } 1820 1.1 elric 1821 1.1 elric ret = real_close(fd); 1822 1.1 elric 1823 1.1 elric if (si->myname && si->peername) { 1824 1.1 elric swrap_dump_packet(si, NULL, SWRAP_CLOSE_RECV, NULL, 0); 1825 1.1 elric swrap_dump_packet(si, NULL, SWRAP_CLOSE_ACK, NULL, 0); 1826 1.1 elric } 1827 1.1 elric 1828 1.1 elric if (si->path) free(si->path); 1829 1.1 elric if (si->myname) free(si->myname); 1830 1.1 elric if (si->peername) free(si->peername); 1831 1.1 elric if (si->tmp_path) { 1832 1.1 elric unlink(si->tmp_path); 1833 1.1 elric free(si->tmp_path); 1834 1.1 elric } 1835 1.1 elric free(si); 1836 1.1 elric 1837 1.1 elric return ret; 1838 1.1 elric } 1839 1.1 elric 1840 1.1 elric static int 1841 1.1 elric dup_internal(const struct socket_info *si_oldd, int fd) 1842 1.1 elric { 1843 1.1 elric struct socket_info *si_newd; 1844 1.1 elric 1845 1.1 elric si_newd = (struct socket_info *)calloc(1, sizeof(struct socket_info)); 1846 1.1 elric 1847 1.1 elric si_newd->fd = fd; 1848 1.1 elric 1849 1.1 elric si_newd->family = si_oldd->family; 1850 1.1 elric si_newd->type = si_oldd->type; 1851 1.1 elric si_newd->protocol = si_oldd->protocol; 1852 1.1 elric si_newd->bound = si_oldd->bound; 1853 1.1 elric si_newd->bcast = si_oldd->bcast; 1854 1.1 elric if (si_oldd->path) 1855 1.1 elric si_newd->path = strdup(si_oldd->path); 1856 1.1 elric if (si_oldd->tmp_path) 1857 1.1 elric si_newd->tmp_path = strdup(si_oldd->tmp_path); 1858 1.1 elric si_newd->myname = 1859 1.1 elric sockaddr_dup(si_oldd->myname, si_oldd->myname_len); 1860 1.1 elric si_newd->myname_len = si_oldd->myname_len; 1861 1.1 elric si_newd->peername = 1862 1.1 elric sockaddr_dup(si_oldd->peername, si_oldd->peername_len); 1863 1.1 elric si_newd->peername_len = si_oldd->peername_len; 1864 1.1 elric 1865 1.1 elric si_newd->io = si_oldd->io; 1866 1.1 elric 1867 1.1 elric SWRAP_DLIST_ADD(sockets, si_newd); 1868 1.1 elric 1869 1.1 elric return fd; 1870 1.1 elric } 1871 1.1 elric 1872 1.1 elric 1873 1.1 elric _PUBLIC_ int swrap_dup(int oldd) 1874 1.1 elric { 1875 1.1 elric struct socket_info *si; 1876 1.1 elric int fd; 1877 1.1 elric 1878 1.1 elric si = find_socket_info(oldd); 1879 1.1 elric if (si == NULL) 1880 1.1 elric return real_dup(oldd); 1881 1.1 elric 1882 1.1 elric fd = real_dup(si->fd); 1883 1.1 elric if (fd < 0) 1884 1.1 elric return fd; 1885 1.1 elric 1886 1.1 elric return dup_internal(si, fd); 1887 1.1 elric } 1888 1.1 elric 1889 1.1 elric 1890 1.1 elric _PUBLIC_ int swrap_dup2(int oldd, int newd) 1891 1.1 elric { 1892 1.1 elric struct socket_info *si_newd, *si_oldd; 1893 1.1 elric int fd; 1894 1.1 elric 1895 1.1 elric if (newd == oldd) 1896 1.1 elric return newd; 1897 1.1 elric 1898 1.1 elric si_oldd = find_socket_info(oldd); 1899 1.1 elric si_newd = find_socket_info(newd); 1900 1.1 elric 1901 1.1 elric if (si_oldd == NULL && si_newd == NULL) 1902 1.1 elric return real_dup2(oldd, newd); 1903 1.1 elric 1904 1.1 elric fd = real_dup2(si_oldd->fd, newd); 1905 1.1 elric if (fd < 0) 1906 1.1 elric return fd; 1907 1.1 elric 1908 1.1 elric /* close new socket first */ 1909 1.1 elric if (si_newd) 1910 1.1 elric swrap_close(newd); 1911 1.1 elric 1912 1.1 elric return dup_internal(si_oldd, fd); 1913 1.1 elric } 1914