s3:net idmap: implement net idmap delete
[Samba.git] / lib / socket_wrapper / socket_wrapper.c
blob2985d007ece785a543c095d37771da34a726c771
1 /*
2 * Copyright (C) Jelmer Vernooij 2005,2008 <jelmer@samba.org>
3 * Copyright (C) Stefan Metzmacher 2006-2009 <metze@samba.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the author nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
37 Socket wrapper library. Passes all socket communication over
38 unix domain sockets if the environment variable SOCKET_WRAPPER_DIR
39 is set.
42 #ifdef _SAMBA_BUILD_
44 #define SOCKET_WRAPPER_NOT_REPLACE
45 #include "../replace/replace.h"
46 #include "system/network.h"
47 #include "system/filesys.h"
48 #include "system/time.h"
50 #else /* _SAMBA_BUILD_ */
52 #include <sys/types.h>
53 #include <sys/time.h>
54 #include <sys/stat.h>
55 #include <sys/socket.h>
56 #include <sys/ioctl.h>
57 #include <sys/filio.h>
58 #include <errno.h>
59 #include <sys/un.h>
60 #include <netinet/in.h>
61 #include <netinet/tcp.h>
62 #include <fcntl.h>
63 #include <stdlib.h>
64 #include <unistd.h>
65 #include <string.h>
66 #include <stdio.h>
67 #include <stdint.h>
69 #endif
71 #ifndef _PUBLIC_
72 #define _PUBLIC_
73 #endif
75 #define SWRAP_DLIST_ADD(list,item) do { \
76 if (!(list)) { \
77 (item)->prev = NULL; \
78 (item)->next = NULL; \
79 (list) = (item); \
80 } else { \
81 (item)->prev = NULL; \
82 (item)->next = (list); \
83 (list)->prev = (item); \
84 (list) = (item); \
85 } \
86 } while (0)
88 #define SWRAP_DLIST_REMOVE(list,item) do { \
89 if ((list) == (item)) { \
90 (list) = (item)->next; \
91 if (list) { \
92 (list)->prev = NULL; \
93 } \
94 } else { \
95 if ((item)->prev) { \
96 (item)->prev->next = (item)->next; \
97 } \
98 if ((item)->next) { \
99 (item)->next->prev = (item)->prev; \
102 (item)->prev = NULL; \
103 (item)->next = NULL; \
104 } while (0)
106 /* LD_PRELOAD doesn't work yet, so REWRITE_CALLS is all we support
107 * for now */
108 #define REWRITE_CALLS
110 #ifdef REWRITE_CALLS
111 #define real_accept accept
112 #define real_connect connect
113 #define real_bind bind
114 #define real_listen listen
115 #define real_getpeername getpeername
116 #define real_getsockname getsockname
117 #define real_getsockopt getsockopt
118 #define real_setsockopt setsockopt
119 #define real_recvfrom recvfrom
120 #define real_sendto sendto
121 #define real_sendmsg sendmsg
122 #define real_ioctl ioctl
123 #define real_recv recv
124 #define real_read read
125 #define real_send send
126 #define real_readv readv
127 #define real_writev writev
128 #define real_socket socket
129 #define real_close close
130 #endif
132 #ifdef HAVE_GETTIMEOFDAY_TZ
133 #define swrapGetTimeOfDay(tval) gettimeofday(tval,NULL)
134 #else
135 #define swrapGetTimeOfDay(tval) gettimeofday(tval)
136 #endif
138 /* we need to use a very terse format here as IRIX 6.4 silently
139 truncates names to 16 chars, so if we use a longer name then we
140 can't tell which port a packet came from with recvfrom()
142 with this format we have 8 chars left for the directory name
144 #define SOCKET_FORMAT "%c%02X%04X"
145 #define SOCKET_TYPE_CHAR_TCP 'T'
146 #define SOCKET_TYPE_CHAR_UDP 'U'
147 #define SOCKET_TYPE_CHAR_TCP_V6 'X'
148 #define SOCKET_TYPE_CHAR_UDP_V6 'Y'
150 #define MAX_WRAPPED_INTERFACES 16
152 #ifdef HAVE_IPV6
154 * FD00::5357:5FXX
156 static const struct in6_addr *swrap_ipv6(void)
158 static struct in6_addr v;
159 static int initialized;
160 int ret;
162 if (initialized) {
163 return &v;
165 initialized = 1;
167 ret = inet_pton(AF_INET6, "FD00::5357:5F00", &v);
168 if (ret <= 0) {
169 abort();
172 return &v;
174 #endif
176 static struct sockaddr *sockaddr_dup(const void *data, socklen_t len)
178 struct sockaddr *ret = (struct sockaddr *)malloc(len);
179 memcpy(ret, data, len);
180 return ret;
183 static void set_port(int family, int prt, struct sockaddr *addr)
185 switch (family) {
186 case AF_INET:
187 ((struct sockaddr_in *)addr)->sin_port = htons(prt);
188 break;
189 #ifdef HAVE_IPV6
190 case AF_INET6:
191 ((struct sockaddr_in6 *)addr)->sin6_port = htons(prt);
192 break;
193 #endif
197 static size_t socket_length(int family)
199 switch (family) {
200 case AF_INET:
201 return sizeof(struct sockaddr_in);
202 #ifdef HAVE_IPV6
203 case AF_INET6:
204 return sizeof(struct sockaddr_in6);
205 #endif
207 return 0;
212 struct socket_info
214 int fd;
216 int family;
217 int type;
218 int protocol;
219 int bound;
220 int bcast;
221 int is_server;
222 int connected;
223 int defer_connect;
225 char *path;
226 char *tmp_path;
228 struct sockaddr *myname;
229 socklen_t myname_len;
231 struct sockaddr *peername;
232 socklen_t peername_len;
234 struct {
235 unsigned long pck_snd;
236 unsigned long pck_rcv;
237 } io;
239 struct socket_info *prev, *next;
242 static struct socket_info *sockets;
244 const char *socket_wrapper_dir(void)
246 const char *s = getenv("SOCKET_WRAPPER_DIR");
247 if (s == NULL) {
248 return NULL;
250 if (strncmp(s, "./", 2) == 0) {
251 s += 2;
253 return s;
256 unsigned int socket_wrapper_default_iface(void)
258 const char *s = getenv("SOCKET_WRAPPER_DEFAULT_IFACE");
259 if (s) {
260 unsigned int iface;
261 if (sscanf(s, "%u", &iface) == 1) {
262 if (iface >= 1 && iface <= MAX_WRAPPED_INTERFACES) {
263 return iface;
268 return 1;/* 127.0.0.1 */
271 static int convert_un_in(const struct sockaddr_un *un, struct sockaddr *in, socklen_t *len)
273 unsigned int iface;
274 unsigned int prt;
275 const char *p;
276 char type;
278 p = strrchr(un->sun_path, '/');
279 if (p) p++; else p = un->sun_path;
281 if (sscanf(p, SOCKET_FORMAT, &type, &iface, &prt) != 3) {
282 errno = EINVAL;
283 return -1;
286 if (iface == 0 || iface > MAX_WRAPPED_INTERFACES) {
287 errno = EINVAL;
288 return -1;
291 if (prt > 0xFFFF) {
292 errno = EINVAL;
293 return -1;
296 switch(type) {
297 case SOCKET_TYPE_CHAR_TCP:
298 case SOCKET_TYPE_CHAR_UDP: {
299 struct sockaddr_in *in2 = (struct sockaddr_in *)in;
301 if ((*len) < sizeof(*in2)) {
302 errno = EINVAL;
303 return -1;
306 memset(in2, 0, sizeof(*in2));
307 in2->sin_family = AF_INET;
308 in2->sin_addr.s_addr = htonl((127<<24) | iface);
309 in2->sin_port = htons(prt);
311 *len = sizeof(*in2);
312 break;
314 #ifdef HAVE_IPV6
315 case SOCKET_TYPE_CHAR_TCP_V6:
316 case SOCKET_TYPE_CHAR_UDP_V6: {
317 struct sockaddr_in6 *in2 = (struct sockaddr_in6 *)in;
319 if ((*len) < sizeof(*in2)) {
320 errno = EINVAL;
321 return -1;
324 memset(in2, 0, sizeof(*in2));
325 in2->sin6_family = AF_INET6;
326 in2->sin6_addr = *swrap_ipv6();
327 in2->sin6_addr.s6_addr[15] = iface;
328 in2->sin6_port = htons(prt);
330 *len = sizeof(*in2);
331 break;
333 #endif
334 default:
335 errno = EINVAL;
336 return -1;
339 return 0;
342 static int convert_in_un_remote(struct socket_info *si, const struct sockaddr *inaddr, struct sockaddr_un *un,
343 int *bcast)
345 char type = '\0';
346 unsigned int prt;
347 unsigned int iface;
348 int is_bcast = 0;
350 if (bcast) *bcast = 0;
352 switch (inaddr->sa_family) {
353 case AF_INET: {
354 const struct sockaddr_in *in =
355 (const struct sockaddr_in *)inaddr;
356 unsigned int addr = ntohl(in->sin_addr.s_addr);
357 char u_type = '\0';
358 char b_type = '\0';
359 char a_type = '\0';
361 switch (si->type) {
362 case SOCK_STREAM:
363 u_type = SOCKET_TYPE_CHAR_TCP;
364 break;
365 case SOCK_DGRAM:
366 u_type = SOCKET_TYPE_CHAR_UDP;
367 a_type = SOCKET_TYPE_CHAR_UDP;
368 b_type = SOCKET_TYPE_CHAR_UDP;
369 break;
372 prt = ntohs(in->sin_port);
373 if (a_type && addr == 0xFFFFFFFF) {
374 /* 255.255.255.255 only udp */
375 is_bcast = 2;
376 type = a_type;
377 iface = socket_wrapper_default_iface();
378 } else if (b_type && addr == 0x7FFFFFFF) {
379 /* 127.255.255.255 only udp */
380 is_bcast = 1;
381 type = b_type;
382 iface = socket_wrapper_default_iface();
383 } else if ((addr & 0xFFFFFF00) == 0x7F000000) {
384 /* 127.0.0.X */
385 is_bcast = 0;
386 type = u_type;
387 iface = (addr & 0x000000FF);
388 } else {
389 errno = ENETUNREACH;
390 return -1;
392 if (bcast) *bcast = is_bcast;
393 break;
395 #ifdef HAVE_IPV6
396 case AF_INET6: {
397 const struct sockaddr_in6 *in =
398 (const struct sockaddr_in6 *)inaddr;
399 struct in6_addr cmp;
401 switch (si->type) {
402 case SOCK_STREAM:
403 type = SOCKET_TYPE_CHAR_TCP_V6;
404 break;
405 case SOCK_DGRAM:
406 type = SOCKET_TYPE_CHAR_UDP_V6;
407 break;
410 /* XXX no multicast/broadcast */
412 prt = ntohs(in->sin6_port);
414 cmp = in->sin6_addr;
415 cmp.s6_addr[15] = 0;
416 if (IN6_ARE_ADDR_EQUAL(swrap_ipv6(), &cmp)) {
417 iface = in->sin6_addr.s6_addr[15];
418 } else {
419 errno = ENETUNREACH;
420 return -1;
423 break;
425 #endif
426 default:
427 errno = ENETUNREACH;
428 return -1;
431 if (prt == 0) {
432 errno = EINVAL;
433 return -1;
436 if (is_bcast) {
437 snprintf(un->sun_path, sizeof(un->sun_path), "%s/EINVAL",
438 socket_wrapper_dir());
439 /* the caller need to do more processing */
440 return 0;
443 snprintf(un->sun_path, sizeof(un->sun_path), "%s/"SOCKET_FORMAT,
444 socket_wrapper_dir(), type, iface, prt);
446 return 0;
449 static int convert_in_un_alloc(struct socket_info *si, const struct sockaddr *inaddr, struct sockaddr_un *un,
450 int *bcast)
452 char type = '\0';
453 unsigned int prt;
454 unsigned int iface;
455 struct stat st;
456 int is_bcast = 0;
458 if (bcast) *bcast = 0;
460 switch (si->family) {
461 case AF_INET: {
462 const struct sockaddr_in *in =
463 (const struct sockaddr_in *)inaddr;
464 unsigned int addr = ntohl(in->sin_addr.s_addr);
465 char u_type = '\0';
466 char d_type = '\0';
467 char b_type = '\0';
468 char a_type = '\0';
470 prt = ntohs(in->sin_port);
472 switch (si->type) {
473 case SOCK_STREAM:
474 u_type = SOCKET_TYPE_CHAR_TCP;
475 d_type = SOCKET_TYPE_CHAR_TCP;
476 break;
477 case SOCK_DGRAM:
478 u_type = SOCKET_TYPE_CHAR_UDP;
479 d_type = SOCKET_TYPE_CHAR_UDP;
480 a_type = SOCKET_TYPE_CHAR_UDP;
481 b_type = SOCKET_TYPE_CHAR_UDP;
482 break;
485 if (addr == 0) {
486 /* 0.0.0.0 */
487 is_bcast = 0;
488 type = d_type;
489 iface = socket_wrapper_default_iface();
490 } else if (a_type && addr == 0xFFFFFFFF) {
491 /* 255.255.255.255 only udp */
492 is_bcast = 2;
493 type = a_type;
494 iface = socket_wrapper_default_iface();
495 } else if (b_type && addr == 0x7FFFFFFF) {
496 /* 127.255.255.255 only udp */
497 is_bcast = 1;
498 type = b_type;
499 iface = socket_wrapper_default_iface();
500 } else if ((addr & 0xFFFFFF00) == 0x7F000000) {
501 /* 127.0.0.X */
502 is_bcast = 0;
503 type = u_type;
504 iface = (addr & 0x000000FF);
505 } else {
506 errno = EADDRNOTAVAIL;
507 return -1;
509 break;
511 #ifdef HAVE_IPV6
512 case AF_INET6: {
513 const struct sockaddr_in6 *in =
514 (const struct sockaddr_in6 *)inaddr;
515 struct in6_addr cmp;
517 switch (si->type) {
518 case SOCK_STREAM:
519 type = SOCKET_TYPE_CHAR_TCP_V6;
520 break;
521 case SOCK_DGRAM:
522 type = SOCKET_TYPE_CHAR_UDP_V6;
523 break;
526 /* XXX no multicast/broadcast */
528 prt = ntohs(in->sin6_port);
530 cmp = in->sin6_addr;
531 cmp.s6_addr[15] = 0;
532 if (IN6_IS_ADDR_UNSPECIFIED(&in->sin6_addr)) {
533 iface = socket_wrapper_default_iface();
534 } else if (IN6_ARE_ADDR_EQUAL(swrap_ipv6(), &cmp)) {
535 iface = in->sin6_addr.s6_addr[15];
536 } else {
537 errno = EADDRNOTAVAIL;
538 return -1;
541 break;
543 #endif
544 default:
545 errno = EADDRNOTAVAIL;
546 return -1;
550 if (bcast) *bcast = is_bcast;
552 if (prt == 0) {
553 /* handle auto-allocation of ephemeral ports */
554 for (prt = 5001; prt < 10000; prt++) {
555 snprintf(un->sun_path, sizeof(un->sun_path), "%s/"SOCKET_FORMAT,
556 socket_wrapper_dir(), type, iface, prt);
557 if (stat(un->sun_path, &st) == 0) continue;
559 set_port(si->family, prt, si->myname);
560 break;
562 if (prt == 10000) {
563 errno = ENFILE;
564 return -1;
568 snprintf(un->sun_path, sizeof(un->sun_path), "%s/"SOCKET_FORMAT,
569 socket_wrapper_dir(), type, iface, prt);
570 return 0;
573 static struct socket_info *find_socket_info(int fd)
575 struct socket_info *i;
576 for (i = sockets; i; i = i->next) {
577 if (i->fd == fd)
578 return i;
581 return NULL;
584 static int sockaddr_convert_to_un(struct socket_info *si, const struct sockaddr *in_addr, socklen_t in_len,
585 struct sockaddr_un *out_addr, int alloc_sock, int *bcast)
587 struct sockaddr *out = (struct sockaddr *)(void *)out_addr;
588 if (!out_addr)
589 return 0;
591 out->sa_family = AF_UNIX;
592 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
593 out->sa_len = sizeof(*out_addr);
594 #endif
596 switch (in_addr->sa_family) {
597 case AF_INET:
598 #ifdef HAVE_IPV6
599 case AF_INET6:
600 #endif
601 switch (si->type) {
602 case SOCK_STREAM:
603 case SOCK_DGRAM:
604 break;
605 default:
606 errno = ESOCKTNOSUPPORT;
607 return -1;
609 if (alloc_sock) {
610 return convert_in_un_alloc(si, in_addr, out_addr, bcast);
611 } else {
612 return convert_in_un_remote(si, in_addr, out_addr, bcast);
614 default:
615 break;
618 errno = EAFNOSUPPORT;
619 return -1;
622 static int sockaddr_convert_from_un(const struct socket_info *si,
623 const struct sockaddr_un *in_addr,
624 socklen_t un_addrlen,
625 int family,
626 struct sockaddr *out_addr,
627 socklen_t *out_addrlen)
629 int ret;
631 if (out_addr == NULL || out_addrlen == NULL)
632 return 0;
634 if (un_addrlen == 0) {
635 *out_addrlen = 0;
636 return 0;
639 switch (family) {
640 case AF_INET:
641 #ifdef HAVE_IPV6
642 case AF_INET6:
643 #endif
644 switch (si->type) {
645 case SOCK_STREAM:
646 case SOCK_DGRAM:
647 break;
648 default:
649 errno = ESOCKTNOSUPPORT;
650 return -1;
652 ret = convert_un_in(in_addr, out_addr, out_addrlen);
653 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
654 out_addr->sa_len = *out_addrlen;
655 #endif
656 return ret;
657 default:
658 break;
661 errno = EAFNOSUPPORT;
662 return -1;
665 enum swrap_packet_type {
666 SWRAP_CONNECT_SEND,
667 SWRAP_CONNECT_UNREACH,
668 SWRAP_CONNECT_RECV,
669 SWRAP_CONNECT_ACK,
670 SWRAP_ACCEPT_SEND,
671 SWRAP_ACCEPT_RECV,
672 SWRAP_ACCEPT_ACK,
673 SWRAP_RECVFROM,
674 SWRAP_SENDTO,
675 SWRAP_SENDTO_UNREACH,
676 SWRAP_PENDING_RST,
677 SWRAP_RECV,
678 SWRAP_RECV_RST,
679 SWRAP_SEND,
680 SWRAP_SEND_RST,
681 SWRAP_CLOSE_SEND,
682 SWRAP_CLOSE_RECV,
683 SWRAP_CLOSE_ACK,
686 struct swrap_file_hdr {
687 uint32_t magic;
688 uint16_t version_major;
689 uint16_t version_minor;
690 int32_t timezone;
691 uint32_t sigfigs;
692 uint32_t frame_max_len;
693 #define SWRAP_FRAME_LENGTH_MAX 0xFFFF
694 uint32_t link_type;
696 #define SWRAP_FILE_HDR_SIZE 24
698 struct swrap_packet_frame {
699 uint32_t seconds;
700 uint32_t micro_seconds;
701 uint32_t recorded_length;
702 uint32_t full_length;
704 #define SWRAP_PACKET_FRAME_SIZE 16
706 union swrap_packet_ip {
707 struct {
708 uint8_t ver_hdrlen;
709 uint8_t tos;
710 uint16_t packet_length;
711 uint16_t identification;
712 uint8_t flags;
713 uint8_t fragment;
714 uint8_t ttl;
715 uint8_t protocol;
716 uint16_t hdr_checksum;
717 uint32_t src_addr;
718 uint32_t dest_addr;
719 } v4;
720 #define SWRAP_PACKET_IP_V4_SIZE 20
721 struct {
722 uint8_t ver_prio;
723 uint8_t flow_label_high;
724 uint16_t flow_label_low;
725 uint16_t payload_length;
726 uint8_t next_header;
727 uint8_t hop_limit;
728 uint8_t src_addr[16];
729 uint8_t dest_addr[16];
730 } v6;
731 #define SWRAP_PACKET_IP_V6_SIZE 40
733 #define SWRAP_PACKET_IP_SIZE 40
735 union swrap_packet_payload {
736 struct {
737 uint16_t source_port;
738 uint16_t dest_port;
739 uint32_t seq_num;
740 uint32_t ack_num;
741 uint8_t hdr_length;
742 uint8_t control;
743 uint16_t window;
744 uint16_t checksum;
745 uint16_t urg;
746 } tcp;
747 #define SWRAP_PACKET_PAYLOAD_TCP_SIZE 20
748 struct {
749 uint16_t source_port;
750 uint16_t dest_port;
751 uint16_t length;
752 uint16_t checksum;
753 } udp;
754 #define SWRAP_PACKET_PAYLOAD_UDP_SIZE 8
755 struct {
756 uint8_t type;
757 uint8_t code;
758 uint16_t checksum;
759 uint32_t unused;
760 } icmp4;
761 #define SWRAP_PACKET_PAYLOAD_ICMP4_SIZE 8
762 struct {
763 uint8_t type;
764 uint8_t code;
765 uint16_t checksum;
766 uint32_t unused;
767 } icmp6;
768 #define SWRAP_PACKET_PAYLOAD_ICMP6_SIZE 8
770 #define SWRAP_PACKET_PAYLOAD_SIZE 20
772 #define SWRAP_PACKET_MIN_ALLOC \
773 (SWRAP_PACKET_FRAME_SIZE + \
774 SWRAP_PACKET_IP_SIZE + \
775 SWRAP_PACKET_PAYLOAD_SIZE)
777 static const char *socket_wrapper_pcap_file(void)
779 static int initialized = 0;
780 static const char *s = NULL;
781 static const struct swrap_file_hdr h;
782 static const struct swrap_packet_frame f;
783 static const union swrap_packet_ip i;
784 static const union swrap_packet_payload p;
786 if (initialized == 1) {
787 return s;
789 initialized = 1;
792 * TODO: don't use the structs use plain buffer offsets
793 * and PUSH_U8(), PUSH_U16() and PUSH_U32()
795 * for now make sure we disable PCAP support
796 * if the struct has alignment!
798 if (sizeof(h) != SWRAP_FILE_HDR_SIZE) {
799 return NULL;
801 if (sizeof(f) != SWRAP_PACKET_FRAME_SIZE) {
802 return NULL;
804 if (sizeof(i) != SWRAP_PACKET_IP_SIZE) {
805 return NULL;
807 if (sizeof(i.v4) != SWRAP_PACKET_IP_V4_SIZE) {
808 return NULL;
810 if (sizeof(i.v6) != SWRAP_PACKET_IP_V6_SIZE) {
811 return NULL;
813 if (sizeof(p) != SWRAP_PACKET_PAYLOAD_SIZE) {
814 return NULL;
816 if (sizeof(p.tcp) != SWRAP_PACKET_PAYLOAD_TCP_SIZE) {
817 return NULL;
819 if (sizeof(p.udp) != SWRAP_PACKET_PAYLOAD_UDP_SIZE) {
820 return NULL;
822 if (sizeof(p.icmp4) != SWRAP_PACKET_PAYLOAD_ICMP4_SIZE) {
823 return NULL;
825 if (sizeof(p.icmp6) != SWRAP_PACKET_PAYLOAD_ICMP6_SIZE) {
826 return NULL;
829 s = getenv("SOCKET_WRAPPER_PCAP_FILE");
830 if (s == NULL) {
831 return NULL;
833 if (strncmp(s, "./", 2) == 0) {
834 s += 2;
836 return s;
839 static uint8_t *swrap_packet_init(struct timeval *tval,
840 const struct sockaddr *src,
841 const struct sockaddr *dest,
842 int socket_type,
843 const uint8_t *payload,
844 size_t payload_len,
845 unsigned long tcp_seqno,
846 unsigned long tcp_ack,
847 unsigned char tcp_ctl,
848 int unreachable,
849 size_t *_packet_len)
851 uint8_t *base;
852 uint8_t *buf;
853 struct swrap_packet_frame *frame;
854 union swrap_packet_ip *ip;
855 union swrap_packet_payload *pay;
856 size_t packet_len;
857 size_t alloc_len;
858 size_t nonwire_len = sizeof(*frame);
859 size_t wire_hdr_len = 0;
860 size_t wire_len = 0;
861 size_t ip_hdr_len = 0;
862 size_t icmp_hdr_len = 0;
863 size_t icmp_truncate_len = 0;
864 uint8_t protocol = 0, icmp_protocol = 0;
865 const struct sockaddr_in *src_in = NULL;
866 const struct sockaddr_in *dest_in = NULL;
867 #ifdef HAVE_IPV6
868 const struct sockaddr_in6 *src_in6 = NULL;
869 const struct sockaddr_in6 *dest_in6 = NULL;
870 #endif
871 uint16_t src_port;
872 uint16_t dest_port;
874 switch (src->sa_family) {
875 case AF_INET:
876 src_in = (const struct sockaddr_in *)src;
877 dest_in = (const struct sockaddr_in *)dest;
878 src_port = src_in->sin_port;
879 dest_port = dest_in->sin_port;
880 ip_hdr_len = sizeof(ip->v4);
881 break;
882 #ifdef HAVE_IPV6
883 case AF_INET6:
884 src_in6 = (const struct sockaddr_in6 *)src;
885 dest_in6 = (const struct sockaddr_in6 *)dest;
886 src_port = src_in6->sin6_port;
887 dest_port = dest_in6->sin6_port;
888 ip_hdr_len = sizeof(ip->v6);
889 break;
890 #endif
891 default:
892 return NULL;
895 switch (socket_type) {
896 case SOCK_STREAM:
897 protocol = 0x06; /* TCP */
898 wire_hdr_len = ip_hdr_len + sizeof(pay->tcp);
899 wire_len = wire_hdr_len + payload_len;
900 break;
902 case SOCK_DGRAM:
903 protocol = 0x11; /* UDP */
904 wire_hdr_len = ip_hdr_len + sizeof(pay->udp);
905 wire_len = wire_hdr_len + payload_len;
906 break;
908 default:
909 return NULL;
912 if (unreachable) {
913 icmp_protocol = protocol;
914 switch (src->sa_family) {
915 case AF_INET:
916 protocol = 0x01; /* ICMPv4 */
917 icmp_hdr_len = ip_hdr_len + sizeof(pay->icmp4);
918 break;
919 #ifdef HAVE_IPV6
920 case AF_INET6:
921 protocol = 0x3A; /* ICMPv6 */
922 icmp_hdr_len = ip_hdr_len + sizeof(pay->icmp6);
923 break;
924 #endif
926 if (wire_len > 64 ) {
927 icmp_truncate_len = wire_len - 64;
929 wire_hdr_len += icmp_hdr_len;
930 wire_len += icmp_hdr_len;
933 packet_len = nonwire_len + wire_len;
934 alloc_len = packet_len;
935 if (alloc_len < SWRAP_PACKET_MIN_ALLOC) {
936 alloc_len = SWRAP_PACKET_MIN_ALLOC;
939 base = (uint8_t *)malloc(alloc_len);
940 if (!base) return NULL;
942 buf = base;
944 frame = (struct swrap_packet_frame *)buf;
945 frame->seconds = tval->tv_sec;
946 frame->micro_seconds = tval->tv_usec;
947 frame->recorded_length = wire_len - icmp_truncate_len;
948 frame->full_length = wire_len - icmp_truncate_len;
949 buf += SWRAP_PACKET_FRAME_SIZE;
951 ip = (union swrap_packet_ip *)buf;
952 switch (src->sa_family) {
953 case AF_INET:
954 ip->v4.ver_hdrlen = 0x45; /* version 4 and 5 * 32 bit words */
955 ip->v4.tos = 0x00;
956 ip->v4.packet_length = htons(wire_len - icmp_truncate_len);
957 ip->v4.identification = htons(0xFFFF);
958 ip->v4.flags = 0x40; /* BIT 1 set - means don't fraqment */
959 ip->v4.fragment = htons(0x0000);
960 ip->v4.ttl = 0xFF;
961 ip->v4.protocol = protocol;
962 ip->v4.hdr_checksum = htons(0x0000);
963 ip->v4.src_addr = src_in->sin_addr.s_addr;
964 ip->v4.dest_addr = dest_in->sin_addr.s_addr;
965 buf += SWRAP_PACKET_IP_V4_SIZE;
966 break;
967 #ifdef HAVE_IPV6
968 case AF_INET6:
969 ip->v6.ver_prio = 0x60; /* version 4 and 5 * 32 bit words */
970 ip->v6.flow_label_high = 0x00;
971 ip->v6.flow_label_low = 0x0000;
972 ip->v6.payload_length = htons(wire_len - icmp_truncate_len);//TODO
973 ip->v6.next_header = protocol;
974 memcpy(ip->v6.src_addr, src_in6->sin6_addr.s6_addr, 16);
975 memcpy(ip->v6.dest_addr, dest_in6->sin6_addr.s6_addr, 16);
976 buf += SWRAP_PACKET_IP_V6_SIZE;
977 break;
978 #endif
981 if (unreachable) {
982 pay = (union swrap_packet_payload *)buf;
983 switch (src->sa_family) {
984 case AF_INET:
985 pay->icmp4.type = 0x03; /* destination unreachable */
986 pay->icmp4.code = 0x01; /* host unreachable */
987 pay->icmp4.checksum = htons(0x0000);
988 pay->icmp4.unused = htonl(0x00000000);
989 buf += SWRAP_PACKET_PAYLOAD_ICMP4_SIZE;
991 /* set the ip header in the ICMP payload */
992 ip = (union swrap_packet_ip *)buf;
993 ip->v4.ver_hdrlen = 0x45; /* version 4 and 5 * 32 bit words */
994 ip->v4.tos = 0x00;
995 ip->v4.packet_length = htons(wire_len - icmp_hdr_len);
996 ip->v4.identification = htons(0xFFFF);
997 ip->v4.flags = 0x40; /* BIT 1 set - means don't fraqment */
998 ip->v4.fragment = htons(0x0000);
999 ip->v4.ttl = 0xFF;
1000 ip->v4.protocol = icmp_protocol;
1001 ip->v4.hdr_checksum = htons(0x0000);
1002 ip->v4.src_addr = dest_in->sin_addr.s_addr;
1003 ip->v4.dest_addr = src_in->sin_addr.s_addr;
1004 buf += SWRAP_PACKET_IP_V4_SIZE;
1006 src_port = dest_in->sin_port;
1007 dest_port = src_in->sin_port;
1008 break;
1009 #ifdef HAVE_IPV6
1010 case AF_INET6:
1011 pay->icmp6.type = 0x01; /* destination unreachable */
1012 pay->icmp6.code = 0x03; /* address unreachable */
1013 pay->icmp6.checksum = htons(0x0000);
1014 pay->icmp6.unused = htonl(0x00000000);
1015 buf += SWRAP_PACKET_PAYLOAD_ICMP6_SIZE;
1017 /* set the ip header in the ICMP payload */
1018 ip = (union swrap_packet_ip *)buf;
1019 ip->v6.ver_prio = 0x60; /* version 4 and 5 * 32 bit words */
1020 ip->v6.flow_label_high = 0x00;
1021 ip->v6.flow_label_low = 0x0000;
1022 ip->v6.payload_length = htons(wire_len - icmp_truncate_len);//TODO
1023 ip->v6.next_header = protocol;
1024 memcpy(ip->v6.src_addr, dest_in6->sin6_addr.s6_addr, 16);
1025 memcpy(ip->v6.dest_addr, src_in6->sin6_addr.s6_addr, 16);
1026 buf += SWRAP_PACKET_IP_V6_SIZE;
1028 src_port = dest_in6->sin6_port;
1029 dest_port = src_in6->sin6_port;
1030 break;
1031 #endif
1035 pay = (union swrap_packet_payload *)buf;
1037 switch (socket_type) {
1038 case SOCK_STREAM:
1039 pay->tcp.source_port = src_port;
1040 pay->tcp.dest_port = dest_port;
1041 pay->tcp.seq_num = htonl(tcp_seqno);
1042 pay->tcp.ack_num = htonl(tcp_ack);
1043 pay->tcp.hdr_length = 0x50; /* 5 * 32 bit words */
1044 pay->tcp.control = tcp_ctl;
1045 pay->tcp.window = htons(0x7FFF);
1046 pay->tcp.checksum = htons(0x0000);
1047 pay->tcp.urg = htons(0x0000);
1048 buf += SWRAP_PACKET_PAYLOAD_TCP_SIZE;
1050 break;
1052 case SOCK_DGRAM:
1053 pay->udp.source_port = src_port;
1054 pay->udp.dest_port = dest_port;
1055 pay->udp.length = htons(8 + payload_len);
1056 pay->udp.checksum = htons(0x0000);
1057 buf += SWRAP_PACKET_PAYLOAD_UDP_SIZE;
1059 break;
1062 if (payload && payload_len > 0) {
1063 memcpy(buf, payload, payload_len);
1066 *_packet_len = packet_len - icmp_truncate_len;
1067 return base;
1070 static int swrap_get_pcap_fd(const char *fname)
1072 static int fd = -1;
1074 if (fd != -1) return fd;
1076 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_APPEND, 0644);
1077 if (fd != -1) {
1078 struct swrap_file_hdr file_hdr;
1079 file_hdr.magic = 0xA1B2C3D4;
1080 file_hdr.version_major = 0x0002;
1081 file_hdr.version_minor = 0x0004;
1082 file_hdr.timezone = 0x00000000;
1083 file_hdr.sigfigs = 0x00000000;
1084 file_hdr.frame_max_len = SWRAP_FRAME_LENGTH_MAX;
1085 file_hdr.link_type = 0x0065; /* 101 RAW IP */
1087 if (write(fd, &file_hdr, sizeof(file_hdr)) != sizeof(file_hdr)) {
1088 close(fd);
1089 fd = -1;
1091 return fd;
1094 fd = open(fname, O_WRONLY|O_APPEND, 0644);
1096 return fd;
1099 static uint8_t *swrap_marshall_packet(struct socket_info *si,
1100 const struct sockaddr *addr,
1101 enum swrap_packet_type type,
1102 const void *buf, size_t len,
1103 size_t *packet_len)
1105 const struct sockaddr *src_addr;
1106 const struct sockaddr *dest_addr;
1107 unsigned long tcp_seqno = 0;
1108 unsigned long tcp_ack = 0;
1109 unsigned char tcp_ctl = 0;
1110 int unreachable = 0;
1112 struct timeval tv;
1114 switch (si->family) {
1115 case AF_INET:
1116 break;
1117 #ifdef HAVE_IPV6
1118 case AF_INET6:
1119 break;
1120 #endif
1121 default:
1122 return NULL;
1125 switch (type) {
1126 case SWRAP_CONNECT_SEND:
1127 if (si->type != SOCK_STREAM) return NULL;
1129 src_addr = si->myname;
1130 dest_addr = addr;
1132 tcp_seqno = si->io.pck_snd;
1133 tcp_ack = si->io.pck_rcv;
1134 tcp_ctl = 0x02; /* SYN */
1136 si->io.pck_snd += 1;
1138 break;
1140 case SWRAP_CONNECT_RECV:
1141 if (si->type != SOCK_STREAM) return NULL;
1143 dest_addr = si->myname;
1144 src_addr = addr;
1146 tcp_seqno = si->io.pck_rcv;
1147 tcp_ack = si->io.pck_snd;
1148 tcp_ctl = 0x12; /** SYN,ACK */
1150 si->io.pck_rcv += 1;
1152 break;
1154 case SWRAP_CONNECT_UNREACH:
1155 if (si->type != SOCK_STREAM) return NULL;
1157 dest_addr = si->myname;
1158 src_addr = addr;
1160 /* Unreachable: resend the data of SWRAP_CONNECT_SEND */
1161 tcp_seqno = si->io.pck_snd - 1;
1162 tcp_ack = si->io.pck_rcv;
1163 tcp_ctl = 0x02; /* SYN */
1164 unreachable = 1;
1166 break;
1168 case SWRAP_CONNECT_ACK:
1169 if (si->type != SOCK_STREAM) return NULL;
1171 src_addr = si->myname;
1172 dest_addr = addr;
1174 tcp_seqno = si->io.pck_snd;
1175 tcp_ack = si->io.pck_rcv;
1176 tcp_ctl = 0x10; /* ACK */
1178 break;
1180 case SWRAP_ACCEPT_SEND:
1181 if (si->type != SOCK_STREAM) return NULL;
1183 dest_addr = si->myname;
1184 src_addr = addr;
1186 tcp_seqno = si->io.pck_rcv;
1187 tcp_ack = si->io.pck_snd;
1188 tcp_ctl = 0x02; /* SYN */
1190 si->io.pck_rcv += 1;
1192 break;
1194 case SWRAP_ACCEPT_RECV:
1195 if (si->type != SOCK_STREAM) return NULL;
1197 src_addr = si->myname;
1198 dest_addr = addr;
1200 tcp_seqno = si->io.pck_snd;
1201 tcp_ack = si->io.pck_rcv;
1202 tcp_ctl = 0x12; /* SYN,ACK */
1204 si->io.pck_snd += 1;
1206 break;
1208 case SWRAP_ACCEPT_ACK:
1209 if (si->type != SOCK_STREAM) return NULL;
1211 dest_addr = si->myname;
1212 src_addr = addr;
1214 tcp_seqno = si->io.pck_rcv;
1215 tcp_ack = si->io.pck_snd;
1216 tcp_ctl = 0x10; /* ACK */
1218 break;
1220 case SWRAP_SEND:
1221 src_addr = si->myname;
1222 dest_addr = si->peername;
1224 tcp_seqno = si->io.pck_snd;
1225 tcp_ack = si->io.pck_rcv;
1226 tcp_ctl = 0x18; /* PSH,ACK */
1228 si->io.pck_snd += len;
1230 break;
1232 case SWRAP_SEND_RST:
1233 dest_addr = si->myname;
1234 src_addr = si->peername;
1236 if (si->type == SOCK_DGRAM) {
1237 return swrap_marshall_packet(si, si->peername,
1238 SWRAP_SENDTO_UNREACH,
1239 buf, len, packet_len);
1242 tcp_seqno = si->io.pck_rcv;
1243 tcp_ack = si->io.pck_snd;
1244 tcp_ctl = 0x14; /** RST,ACK */
1246 break;
1248 case SWRAP_PENDING_RST:
1249 dest_addr = si->myname;
1250 src_addr = si->peername;
1252 if (si->type == SOCK_DGRAM) {
1253 return NULL;
1256 tcp_seqno = si->io.pck_rcv;
1257 tcp_ack = si->io.pck_snd;
1258 tcp_ctl = 0x14; /* RST,ACK */
1260 break;
1262 case SWRAP_RECV:
1263 dest_addr = si->myname;
1264 src_addr = si->peername;
1266 tcp_seqno = si->io.pck_rcv;
1267 tcp_ack = si->io.pck_snd;
1268 tcp_ctl = 0x18; /* PSH,ACK */
1270 si->io.pck_rcv += len;
1272 break;
1274 case SWRAP_RECV_RST:
1275 dest_addr = si->myname;
1276 src_addr = si->peername;
1278 if (si->type == SOCK_DGRAM) {
1279 return NULL;
1282 tcp_seqno = si->io.pck_rcv;
1283 tcp_ack = si->io.pck_snd;
1284 tcp_ctl = 0x14; /* RST,ACK */
1286 break;
1288 case SWRAP_SENDTO:
1289 src_addr = si->myname;
1290 dest_addr = addr;
1292 si->io.pck_snd += len;
1294 break;
1296 case SWRAP_SENDTO_UNREACH:
1297 dest_addr = si->myname;
1298 src_addr = addr;
1300 unreachable = 1;
1302 break;
1304 case SWRAP_RECVFROM:
1305 dest_addr = si->myname;
1306 src_addr = addr;
1308 si->io.pck_rcv += len;
1310 break;
1312 case SWRAP_CLOSE_SEND:
1313 if (si->type != SOCK_STREAM) return NULL;
1315 src_addr = si->myname;
1316 dest_addr = si->peername;
1318 tcp_seqno = si->io.pck_snd;
1319 tcp_ack = si->io.pck_rcv;
1320 tcp_ctl = 0x11; /* FIN, ACK */
1322 si->io.pck_snd += 1;
1324 break;
1326 case SWRAP_CLOSE_RECV:
1327 if (si->type != SOCK_STREAM) return NULL;
1329 dest_addr = si->myname;
1330 src_addr = si->peername;
1332 tcp_seqno = si->io.pck_rcv;
1333 tcp_ack = si->io.pck_snd;
1334 tcp_ctl = 0x11; /* FIN,ACK */
1336 si->io.pck_rcv += 1;
1338 break;
1340 case SWRAP_CLOSE_ACK:
1341 if (si->type != SOCK_STREAM) return NULL;
1343 src_addr = si->myname;
1344 dest_addr = si->peername;
1346 tcp_seqno = si->io.pck_snd;
1347 tcp_ack = si->io.pck_rcv;
1348 tcp_ctl = 0x10; /* ACK */
1350 break;
1351 default:
1352 return NULL;
1355 swrapGetTimeOfDay(&tv);
1357 return swrap_packet_init(&tv, src_addr, dest_addr, si->type,
1358 (const uint8_t *)buf, len,
1359 tcp_seqno, tcp_ack, tcp_ctl, unreachable,
1360 packet_len);
1363 static void swrap_dump_packet(struct socket_info *si,
1364 const struct sockaddr *addr,
1365 enum swrap_packet_type type,
1366 const void *buf, size_t len)
1368 const char *file_name;
1369 uint8_t *packet;
1370 size_t packet_len = 0;
1371 int fd;
1373 file_name = socket_wrapper_pcap_file();
1374 if (!file_name) {
1375 return;
1378 packet = swrap_marshall_packet(si, addr, type, buf, len, &packet_len);
1379 if (!packet) {
1380 return;
1383 fd = swrap_get_pcap_fd(file_name);
1384 if (fd != -1) {
1385 if (write(fd, packet, packet_len) != packet_len) {
1386 free(packet);
1387 return;
1391 free(packet);
1394 _PUBLIC_ int swrap_socket(int family, int type, int protocol)
1396 struct socket_info *si;
1397 int fd;
1398 int real_type = type;
1399 #ifdef SOCK_CLOEXEC
1400 real_type &= ~SOCK_CLOEXEC;
1401 #endif
1402 #ifdef SOCK_NONBLOCK
1403 real_type &= ~SOCK_NONBLOCK;
1404 #endif
1406 if (!socket_wrapper_dir()) {
1407 return real_socket(family, type, protocol);
1410 switch (family) {
1411 case AF_INET:
1412 #ifdef HAVE_IPV6
1413 case AF_INET6:
1414 #endif
1415 break;
1416 case AF_UNIX:
1417 return real_socket(family, type, protocol);
1418 default:
1419 errno = EAFNOSUPPORT;
1420 return -1;
1423 switch (real_type) {
1424 case SOCK_STREAM:
1425 break;
1426 case SOCK_DGRAM:
1427 break;
1428 default:
1429 errno = EPROTONOSUPPORT;
1430 return -1;
1433 switch (protocol) {
1434 case 0:
1435 break;
1436 case 6:
1437 if (real_type == SOCK_STREAM) {
1438 break;
1440 /*fall through*/
1441 case 17:
1442 if (real_type == SOCK_DGRAM) {
1443 break;
1445 /*fall through*/
1446 default:
1447 errno = EPROTONOSUPPORT;
1448 return -1;
1451 /* We must call real_socket with type, from the caller, not the version we removed
1452 SOCK_CLOEXEC and SOCK_NONBLOCK from */
1453 fd = real_socket(AF_UNIX, type, 0);
1455 if (fd == -1) return -1;
1457 si = (struct socket_info *)calloc(1, sizeof(struct socket_info));
1459 si->family = family;
1461 /* however, the rest of the socket_wrapper code expects just
1462 * the type, not the flags */
1463 si->type = real_type;
1464 si->protocol = protocol;
1465 si->fd = fd;
1467 SWRAP_DLIST_ADD(sockets, si);
1469 return si->fd;
1472 _PUBLIC_ int swrap_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
1474 struct socket_info *parent_si, *child_si;
1475 int fd;
1476 struct sockaddr_un un_addr;
1477 socklen_t un_addrlen = sizeof(un_addr);
1478 struct sockaddr_un un_my_addr;
1479 socklen_t un_my_addrlen = sizeof(un_my_addr);
1480 struct sockaddr *my_addr;
1481 socklen_t my_addrlen, len;
1482 int ret;
1484 parent_si = find_socket_info(s);
1485 if (!parent_si) {
1486 return real_accept(s, addr, addrlen);
1490 * assume out sockaddr have the same size as the in parent
1491 * socket family
1493 my_addrlen = socket_length(parent_si->family);
1494 if (my_addrlen <= 0) {
1495 errno = EINVAL;
1496 return -1;
1499 my_addr = (struct sockaddr *)malloc(my_addrlen);
1500 if (my_addr == NULL) {
1501 return -1;
1504 memset(&un_addr, 0, sizeof(un_addr));
1505 memset(&un_my_addr, 0, sizeof(un_my_addr));
1507 ret = real_accept(s, (struct sockaddr *)&un_addr, &un_addrlen);
1508 if (ret == -1) {
1509 free(my_addr);
1510 return ret;
1513 fd = ret;
1515 len = my_addrlen;
1516 ret = sockaddr_convert_from_un(parent_si, &un_addr, un_addrlen,
1517 parent_si->family, my_addr, &len);
1518 if (ret == -1) {
1519 free(my_addr);
1520 close(fd);
1521 return ret;
1524 child_si = (struct socket_info *)malloc(sizeof(struct socket_info));
1525 memset(child_si, 0, sizeof(*child_si));
1527 child_si->fd = fd;
1528 child_si->family = parent_si->family;
1529 child_si->type = parent_si->type;
1530 child_si->protocol = parent_si->protocol;
1531 child_si->bound = 1;
1532 child_si->is_server = 1;
1533 child_si->connected = 1;
1535 child_si->peername_len = len;
1536 child_si->peername = sockaddr_dup(my_addr, len);
1538 if (addr != NULL && addrlen != NULL) {
1539 *addrlen = len;
1540 if (*addrlen >= len)
1541 memcpy(addr, my_addr, len);
1542 *addrlen = 0;
1545 ret = real_getsockname(fd, (struct sockaddr *)&un_my_addr, &un_my_addrlen);
1546 if (ret == -1) {
1547 free(child_si);
1548 close(fd);
1549 return ret;
1552 len = my_addrlen;
1553 ret = sockaddr_convert_from_un(child_si, &un_my_addr, un_my_addrlen,
1554 child_si->family, my_addr, &len);
1555 if (ret == -1) {
1556 free(child_si);
1557 free(my_addr);
1558 close(fd);
1559 return ret;
1562 child_si->myname_len = len;
1563 child_si->myname = sockaddr_dup(my_addr, len);
1564 free(my_addr);
1566 SWRAP_DLIST_ADD(sockets, child_si);
1568 swrap_dump_packet(child_si, addr, SWRAP_ACCEPT_SEND, NULL, 0);
1569 swrap_dump_packet(child_si, addr, SWRAP_ACCEPT_RECV, NULL, 0);
1570 swrap_dump_packet(child_si, addr, SWRAP_ACCEPT_ACK, NULL, 0);
1572 return fd;
1575 static int autobind_start_init;
1576 static int autobind_start;
1578 /* using sendto() or connect() on an unbound socket would give the
1579 recipient no way to reply, as unlike UDP and TCP, a unix domain
1580 socket can't auto-assign emphemeral port numbers, so we need to
1581 assign it here.
1582 Note: this might change the family from ipv6 to ipv4
1584 static int swrap_auto_bind(struct socket_info *si, int family)
1586 struct sockaddr_un un_addr;
1587 int i;
1588 char type;
1589 int ret;
1590 int port;
1591 struct stat st;
1593 if (autobind_start_init != 1) {
1594 autobind_start_init = 1;
1595 autobind_start = getpid();
1596 autobind_start %= 50000;
1597 autobind_start += 10000;
1600 un_addr.sun_family = AF_UNIX;
1602 switch (family) {
1603 case AF_INET: {
1604 struct sockaddr_in in;
1606 switch (si->type) {
1607 case SOCK_STREAM:
1608 type = SOCKET_TYPE_CHAR_TCP;
1609 break;
1610 case SOCK_DGRAM:
1611 type = SOCKET_TYPE_CHAR_UDP;
1612 break;
1613 default:
1614 errno = ESOCKTNOSUPPORT;
1615 return -1;
1618 memset(&in, 0, sizeof(in));
1619 in.sin_family = AF_INET;
1620 in.sin_addr.s_addr = htonl(127<<24 |
1621 socket_wrapper_default_iface());
1623 si->myname_len = sizeof(in);
1624 si->myname = sockaddr_dup(&in, si->myname_len);
1625 break;
1627 #ifdef HAVE_IPV6
1628 case AF_INET6: {
1629 struct sockaddr_in6 in6;
1631 if (si->family != family) {
1632 errno = ENETUNREACH;
1633 return -1;
1636 switch (si->type) {
1637 case SOCK_STREAM:
1638 type = SOCKET_TYPE_CHAR_TCP_V6;
1639 break;
1640 case SOCK_DGRAM:
1641 type = SOCKET_TYPE_CHAR_UDP_V6;
1642 break;
1643 default:
1644 errno = ESOCKTNOSUPPORT;
1645 return -1;
1648 memset(&in6, 0, sizeof(in6));
1649 in6.sin6_family = AF_INET6;
1650 in6.sin6_addr = *swrap_ipv6();
1651 in6.sin6_addr.s6_addr[15] = socket_wrapper_default_iface();
1652 si->myname_len = sizeof(in6);
1653 si->myname = sockaddr_dup(&in6, si->myname_len);
1654 break;
1656 #endif
1657 default:
1658 errno = ESOCKTNOSUPPORT;
1659 return -1;
1662 if (autobind_start > 60000) {
1663 autobind_start = 10000;
1666 for (i=0;i<1000;i++) {
1667 port = autobind_start + i;
1668 snprintf(un_addr.sun_path, sizeof(un_addr.sun_path),
1669 "%s/"SOCKET_FORMAT, socket_wrapper_dir(),
1670 type, socket_wrapper_default_iface(), port);
1671 if (stat(un_addr.sun_path, &st) == 0) continue;
1673 ret = real_bind(si->fd, (struct sockaddr *)&un_addr, sizeof(un_addr));
1674 if (ret == -1) return ret;
1676 si->tmp_path = strdup(un_addr.sun_path);
1677 si->bound = 1;
1678 autobind_start = port + 1;
1679 break;
1681 if (i == 1000) {
1682 errno = ENFILE;
1683 return -1;
1686 si->family = family;
1687 set_port(si->family, port, si->myname);
1689 return 0;
1693 _PUBLIC_ int swrap_connect(int s, const struct sockaddr *serv_addr, socklen_t addrlen)
1695 int ret;
1696 struct sockaddr_un un_addr;
1697 struct socket_info *si = find_socket_info(s);
1699 if (!si) {
1700 return real_connect(s, serv_addr, addrlen);
1703 if (si->bound == 0) {
1704 ret = swrap_auto_bind(si, serv_addr->sa_family);
1705 if (ret == -1) return -1;
1708 if (si->family != serv_addr->sa_family) {
1709 errno = EINVAL;
1710 return -1;
1713 ret = sockaddr_convert_to_un(si, (const struct sockaddr *)serv_addr, addrlen, &un_addr, 0, NULL);
1714 if (ret == -1) return -1;
1716 if (si->type == SOCK_DGRAM) {
1717 si->defer_connect = 1;
1718 ret = 0;
1719 } else {
1720 swrap_dump_packet(si, serv_addr, SWRAP_CONNECT_SEND, NULL, 0);
1722 ret = real_connect(s, (struct sockaddr *)&un_addr,
1723 sizeof(struct sockaddr_un));
1726 /* to give better errors */
1727 if (ret == -1 && errno == ENOENT) {
1728 errno = EHOSTUNREACH;
1731 if (ret == 0) {
1732 si->peername_len = addrlen;
1733 si->peername = sockaddr_dup(serv_addr, addrlen);
1734 si->connected = 1;
1736 swrap_dump_packet(si, serv_addr, SWRAP_CONNECT_RECV, NULL, 0);
1737 swrap_dump_packet(si, serv_addr, SWRAP_CONNECT_ACK, NULL, 0);
1738 } else {
1739 swrap_dump_packet(si, serv_addr, SWRAP_CONNECT_UNREACH, NULL, 0);
1742 return ret;
1745 _PUBLIC_ int swrap_bind(int s, const struct sockaddr *myaddr, socklen_t addrlen)
1747 int ret;
1748 struct sockaddr_un un_addr;
1749 struct socket_info *si = find_socket_info(s);
1751 if (!si) {
1752 return real_bind(s, myaddr, addrlen);
1755 si->myname_len = addrlen;
1756 si->myname = sockaddr_dup(myaddr, addrlen);
1758 ret = sockaddr_convert_to_un(si, (const struct sockaddr *)myaddr, addrlen, &un_addr, 1, &si->bcast);
1759 if (ret == -1) return -1;
1761 unlink(un_addr.sun_path);
1763 ret = real_bind(s, (struct sockaddr *)&un_addr,
1764 sizeof(struct sockaddr_un));
1766 if (ret == 0) {
1767 si->bound = 1;
1770 return ret;
1773 _PUBLIC_ int swrap_listen(int s, int backlog)
1775 int ret;
1776 struct socket_info *si = find_socket_info(s);
1778 if (!si) {
1779 return real_listen(s, backlog);
1782 ret = real_listen(s, backlog);
1784 return ret;
1787 _PUBLIC_ int swrap_getpeername(int s, struct sockaddr *name, socklen_t *addrlen)
1789 struct socket_info *si = find_socket_info(s);
1791 if (!si) {
1792 return real_getpeername(s, name, addrlen);
1795 if (!si->peername)
1797 errno = ENOTCONN;
1798 return -1;
1801 memcpy(name, si->peername, si->peername_len);
1802 *addrlen = si->peername_len;
1804 return 0;
1807 _PUBLIC_ int swrap_getsockname(int s, struct sockaddr *name, socklen_t *addrlen)
1809 struct socket_info *si = find_socket_info(s);
1811 if (!si) {
1812 return real_getsockname(s, name, addrlen);
1815 memcpy(name, si->myname, si->myname_len);
1816 *addrlen = si->myname_len;
1818 return 0;
1821 _PUBLIC_ int swrap_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen)
1823 struct socket_info *si = find_socket_info(s);
1825 if (!si) {
1826 return real_getsockopt(s, level, optname, optval, optlen);
1829 if (level == SOL_SOCKET) {
1830 return real_getsockopt(s, level, optname, optval, optlen);
1833 errno = ENOPROTOOPT;
1834 return -1;
1837 _PUBLIC_ int swrap_setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen)
1839 struct socket_info *si = find_socket_info(s);
1841 if (!si) {
1842 return real_setsockopt(s, level, optname, optval, optlen);
1845 if (level == SOL_SOCKET) {
1846 return real_setsockopt(s, level, optname, optval, optlen);
1849 switch (si->family) {
1850 case AF_INET:
1851 return 0;
1852 #ifdef HAVE_IPV6
1853 case AF_INET6:
1854 return 0;
1855 #endif
1856 default:
1857 errno = ENOPROTOOPT;
1858 return -1;
1862 _PUBLIC_ ssize_t swrap_recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen)
1864 struct sockaddr_un un_addr;
1865 socklen_t un_addrlen = sizeof(un_addr);
1866 int ret;
1867 struct socket_info *si = find_socket_info(s);
1868 struct sockaddr_storage ss;
1869 socklen_t ss_len = sizeof(ss);
1871 if (!si) {
1872 return real_recvfrom(s, buf, len, flags, from, fromlen);
1875 if (!from) {
1876 from = (struct sockaddr *)&ss;
1877 fromlen = &ss_len;
1880 if (si->type == SOCK_STREAM) {
1881 /* cut down to 1500 byte packets for stream sockets,
1882 * which makes it easier to format PCAP capture files
1883 * (as the caller will simply continue from here) */
1884 len = MIN(len, 1500);
1887 /* irix 6.4 forgets to null terminate the sun_path string :-( */
1888 memset(&un_addr, 0, sizeof(un_addr));
1889 ret = real_recvfrom(s, buf, len, flags, (struct sockaddr *)&un_addr, &un_addrlen);
1890 if (ret == -1)
1891 return ret;
1893 if (sockaddr_convert_from_un(si, &un_addr, un_addrlen,
1894 si->family, from, fromlen) == -1) {
1895 return -1;
1898 swrap_dump_packet(si, from, SWRAP_RECVFROM, buf, ret);
1900 return ret;
1904 _PUBLIC_ ssize_t swrap_sendto(int s, const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen)
1906 struct sockaddr_un un_addr;
1907 int ret;
1908 struct socket_info *si = find_socket_info(s);
1909 int bcast = 0;
1911 if (!si) {
1912 return real_sendto(s, buf, len, flags, to, tolen);
1915 if (si->connected) {
1916 if (to) {
1917 errno = EISCONN;
1918 return -1;
1921 to = si->peername;
1922 tolen = si->peername_len;
1925 switch (si->type) {
1926 case SOCK_STREAM:
1927 /* cut down to 1500 byte packets for stream sockets,
1928 * which makes it easier to format PCAP capture files
1929 * (as the caller will simply continue from here) */
1930 len = MIN(len, 1500);
1932 ret = real_send(s, buf, len, flags);
1933 break;
1934 case SOCK_DGRAM:
1935 if (si->bound == 0) {
1936 ret = swrap_auto_bind(si, si->family);
1937 if (ret == -1) return -1;
1940 ret = sockaddr_convert_to_un(si, to, tolen, &un_addr, 0, &bcast);
1941 if (ret == -1) return -1;
1943 if (bcast) {
1944 struct stat st;
1945 unsigned int iface;
1946 unsigned int prt = ntohs(((const struct sockaddr_in *)to)->sin_port);
1947 char type;
1949 type = SOCKET_TYPE_CHAR_UDP;
1951 for(iface=0; iface <= MAX_WRAPPED_INTERFACES; iface++) {
1952 snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), "%s/"SOCKET_FORMAT,
1953 socket_wrapper_dir(), type, iface, prt);
1954 if (stat(un_addr.sun_path, &st) != 0) continue;
1956 /* ignore the any errors in broadcast sends */
1957 real_sendto(s, buf, len, flags, (struct sockaddr *)&un_addr, sizeof(un_addr));
1960 swrap_dump_packet(si, to, SWRAP_SENDTO, buf, len);
1962 return len;
1965 if (si->defer_connect) {
1966 ret = real_connect(s, (struct sockaddr *)&un_addr,
1967 sizeof(un_addr));
1969 /* to give better errors */
1970 if (ret == -1 && errno == ENOENT) {
1971 errno = EHOSTUNREACH;
1974 if (ret == -1) {
1975 return ret;
1977 si->defer_connect = 0;
1980 /* Man page for Linux says:
1981 * "the error EISONN may be returned when they are not NULL and 0"
1982 * But in practice it's not on x86/amd64, but on other unix it is
1983 * (ie. freebsd)
1984 * So if we are already connected we send NULL/0
1986 if (si->connected) {
1987 ret = real_sendto(s, buf, len, flags, NULL, 0);
1988 } else {
1989 ret = real_sendto(s, buf, len, flags, (struct sockaddr *)&un_addr, sizeof(un_addr));
1991 break;
1992 default:
1993 ret = -1;
1994 errno = EHOSTUNREACH;
1995 break;
1998 /* to give better errors */
1999 if (ret == -1 && errno == ENOENT) {
2000 errno = EHOSTUNREACH;
2003 if (ret == -1) {
2004 swrap_dump_packet(si, to, SWRAP_SENDTO, buf, len);
2005 swrap_dump_packet(si, to, SWRAP_SENDTO_UNREACH, buf, len);
2006 } else {
2007 swrap_dump_packet(si, to, SWRAP_SENDTO, buf, ret);
2010 return ret;
2013 _PUBLIC_ int swrap_ioctl(int s, int r, void *p)
2015 int ret;
2016 struct socket_info *si = find_socket_info(s);
2017 int value;
2019 if (!si) {
2020 return real_ioctl(s, r, p);
2023 ret = real_ioctl(s, r, p);
2025 switch (r) {
2026 case FIONREAD:
2027 value = *((int *)p);
2028 if (ret == -1 && errno != EAGAIN && errno != ENOBUFS) {
2029 swrap_dump_packet(si, NULL, SWRAP_PENDING_RST, NULL, 0);
2030 } else if (value == 0) { /* END OF FILE */
2031 swrap_dump_packet(si, NULL, SWRAP_PENDING_RST, NULL, 0);
2033 break;
2036 return ret;
2039 _PUBLIC_ ssize_t swrap_recv(int s, void *buf, size_t len, int flags)
2041 int ret;
2042 struct socket_info *si = find_socket_info(s);
2044 if (!si) {
2045 return real_recv(s, buf, len, flags);
2048 if (si->type == SOCK_STREAM) {
2049 /* cut down to 1500 byte packets for stream sockets,
2050 * which makes it easier to format PCAP capture files
2051 * (as the caller will simply continue from here) */
2052 len = MIN(len, 1500);
2055 ret = real_recv(s, buf, len, flags);
2056 if (ret == -1 && errno != EAGAIN && errno != ENOBUFS) {
2057 swrap_dump_packet(si, NULL, SWRAP_RECV_RST, NULL, 0);
2058 } else if (ret == 0) { /* END OF FILE */
2059 swrap_dump_packet(si, NULL, SWRAP_RECV_RST, NULL, 0);
2060 } else if (ret > 0) {
2061 swrap_dump_packet(si, NULL, SWRAP_RECV, buf, ret);
2064 return ret;
2067 _PUBLIC_ ssize_t swrap_read(int s, void *buf, size_t len)
2069 int ret;
2070 struct socket_info *si = find_socket_info(s);
2072 if (!si) {
2073 return real_read(s, buf, len);
2076 if (si->type == SOCK_STREAM) {
2077 /* cut down to 1500 byte packets for stream sockets,
2078 * which makes it easier to format PCAP capture files
2079 * (as the caller will simply continue from here) */
2080 len = MIN(len, 1500);
2083 ret = real_read(s, buf, len);
2084 if (ret == -1 && errno != EAGAIN && errno != ENOBUFS) {
2085 swrap_dump_packet(si, NULL, SWRAP_RECV_RST, NULL, 0);
2086 } else if (ret == 0) { /* END OF FILE */
2087 swrap_dump_packet(si, NULL, SWRAP_RECV_RST, NULL, 0);
2088 } else if (ret > 0) {
2089 swrap_dump_packet(si, NULL, SWRAP_RECV, buf, ret);
2092 return ret;
2096 _PUBLIC_ ssize_t swrap_send(int s, const void *buf, size_t len, int flags)
2098 int ret;
2099 struct socket_info *si = find_socket_info(s);
2101 if (!si) {
2102 return real_send(s, buf, len, flags);
2105 if (si->type == SOCK_STREAM) {
2106 /* cut down to 1500 byte packets for stream sockets,
2107 * which makes it easier to format PCAP capture files
2108 * (as the caller will simply continue from here) */
2109 len = MIN(len, 1500);
2112 if (si->defer_connect) {
2113 struct sockaddr_un un_addr;
2114 int bcast = 0;
2116 if (si->bound == 0) {
2117 ret = swrap_auto_bind(si, si->family);
2118 if (ret == -1) return -1;
2121 ret = sockaddr_convert_to_un(si, si->peername, si->peername_len,
2122 &un_addr, 0, &bcast);
2123 if (ret == -1) return -1;
2125 ret = real_connect(s, (struct sockaddr *)&un_addr,
2126 sizeof(un_addr));
2128 /* to give better errors */
2129 if (ret == -1 && errno == ENOENT) {
2130 errno = EHOSTUNREACH;
2133 if (ret == -1) {
2134 return ret;
2136 si->defer_connect = 0;
2139 ret = real_send(s, buf, len, flags);
2141 if (ret == -1) {
2142 swrap_dump_packet(si, NULL, SWRAP_SEND, buf, len);
2143 swrap_dump_packet(si, NULL, SWRAP_SEND_RST, NULL, 0);
2144 } else {
2145 swrap_dump_packet(si, NULL, SWRAP_SEND, buf, ret);
2148 return ret;
2151 _PUBLIC_ ssize_t swrap_sendmsg(int s, const struct msghdr *msg, int flags)
2153 int ret;
2154 uint8_t *buf;
2155 off_t ofs = 0;
2156 size_t i;
2157 size_t remain;
2159 struct socket_info *si = find_socket_info(s);
2161 if (!si) {
2162 return real_sendmsg(s, msg, flags);
2165 if (si->defer_connect) {
2166 struct sockaddr_un un_addr;
2167 int bcast = 0;
2169 if (si->bound == 0) {
2170 ret = swrap_auto_bind(si, si->family);
2171 if (ret == -1) return -1;
2174 ret = sockaddr_convert_to_un(si, si->peername, si->peername_len,
2175 &un_addr, 0, &bcast);
2176 if (ret == -1) return -1;
2178 ret = real_connect(s, (struct sockaddr *)&un_addr,
2179 sizeof(un_addr));
2181 /* to give better errors */
2182 if (ret == -1 && errno == ENOENT) {
2183 errno = EHOSTUNREACH;
2186 if (ret == -1) {
2187 return ret;
2189 si->defer_connect = 0;
2192 ret = real_sendmsg(s, msg, flags);
2193 remain = ret;
2195 /* we capture it as one single packet */
2196 buf = (uint8_t *)malloc(ret);
2197 if (!buf) {
2198 /* we just not capture the packet */
2199 errno = 0;
2200 return ret;
2203 for (i=0; i < msg->msg_iovlen; i++) {
2204 size_t this_time = MIN(remain, msg->msg_iov[i].iov_len);
2205 memcpy(buf + ofs,
2206 msg->msg_iov[i].iov_base,
2207 this_time);
2208 ofs += this_time;
2209 remain -= this_time;
2212 swrap_dump_packet(si, NULL, SWRAP_SEND, buf, ret);
2213 free(buf);
2214 if (ret == -1) {
2215 swrap_dump_packet(si, NULL, SWRAP_SEND_RST, NULL, 0);
2218 return ret;
2221 int swrap_readv(int s, const struct iovec *vector, size_t count)
2223 int ret;
2224 struct socket_info *si = find_socket_info(s);
2225 struct iovec v;
2227 if (!si) {
2228 return real_readv(s, vector, count);
2231 if (si->type == SOCK_STREAM && count > 0) {
2232 /* cut down to 1500 byte packets for stream sockets,
2233 * which makes it easier to format PCAP capture files
2234 * (as the caller will simply continue from here) */
2235 size_t i, len = 0;
2237 for (i=0; i < count; i++) {
2238 size_t nlen;
2239 nlen = len + vector[i].iov_len;
2240 if (nlen > 1500) {
2241 break;
2244 count = i;
2245 if (count == 0) {
2246 v = vector[0];
2247 v.iov_len = MIN(v.iov_len, 1500);
2248 vector = &v;
2249 count = 1;
2253 ret = real_readv(s, vector, count);
2254 if (ret == -1 && errno != EAGAIN && errno != ENOBUFS) {
2255 swrap_dump_packet(si, NULL, SWRAP_RECV_RST, NULL, 0);
2256 } else if (ret == 0) { /* END OF FILE */
2257 swrap_dump_packet(si, NULL, SWRAP_RECV_RST, NULL, 0);
2258 } else if (ret > 0) {
2259 uint8_t *buf;
2260 off_t ofs = 0;
2261 size_t i;
2262 size_t remain = ret;
2264 /* we capture it as one single packet */
2265 buf = (uint8_t *)malloc(ret);
2266 if (!buf) {
2267 /* we just not capture the packet */
2268 errno = 0;
2269 return ret;
2272 for (i=0; i < count; i++) {
2273 size_t this_time = MIN(remain, vector[i].iov_len);
2274 memcpy(buf + ofs,
2275 vector[i].iov_base,
2276 this_time);
2277 ofs += this_time;
2278 remain -= this_time;
2281 swrap_dump_packet(si, NULL, SWRAP_RECV, buf, ret);
2282 free(buf);
2285 return ret;
2288 int swrap_writev(int s, const struct iovec *vector, size_t count)
2290 int ret;
2291 struct socket_info *si = find_socket_info(s);
2292 struct iovec v;
2294 if (!si) {
2295 return real_writev(s, vector, count);
2298 if (si->type == SOCK_STREAM && count > 0) {
2299 /* cut down to 1500 byte packets for stream sockets,
2300 * which makes it easier to format PCAP capture files
2301 * (as the caller will simply continue from here) */
2302 size_t i, len = 0;
2304 for (i=0; i < count; i++) {
2305 size_t nlen;
2306 nlen = len + vector[i].iov_len;
2307 if (nlen > 1500) {
2308 break;
2311 count = i;
2312 if (count == 0) {
2313 v = vector[0];
2314 v.iov_len = MIN(v.iov_len, 1500);
2315 vector = &v;
2316 count = 1;
2320 ret = real_writev(s, vector, count);
2321 if (ret == -1) {
2322 swrap_dump_packet(si, NULL, SWRAP_SEND_RST, NULL, 0);
2323 } else {
2324 uint8_t *buf;
2325 off_t ofs = 0;
2326 size_t i;
2327 size_t remain = ret;
2329 /* we capture it as one single packet */
2330 buf = (uint8_t *)malloc(ret);
2331 if (!buf) {
2332 /* we just not capture the packet */
2333 errno = 0;
2334 return ret;
2337 for (i=0; i < count; i++) {
2338 size_t this_time = MIN(remain, vector[i].iov_len);
2339 memcpy(buf + ofs,
2340 vector[i].iov_base,
2341 this_time);
2342 ofs += this_time;
2343 remain -= this_time;
2346 swrap_dump_packet(si, NULL, SWRAP_SEND, buf, ret);
2347 free(buf);
2350 return ret;
2353 _PUBLIC_ int swrap_close(int fd)
2355 struct socket_info *si = find_socket_info(fd);
2356 int ret;
2358 if (!si) {
2359 return real_close(fd);
2362 SWRAP_DLIST_REMOVE(sockets, si);
2364 if (si->myname && si->peername) {
2365 swrap_dump_packet(si, NULL, SWRAP_CLOSE_SEND, NULL, 0);
2368 ret = real_close(fd);
2370 if (si->myname && si->peername) {
2371 swrap_dump_packet(si, NULL, SWRAP_CLOSE_RECV, NULL, 0);
2372 swrap_dump_packet(si, NULL, SWRAP_CLOSE_ACK, NULL, 0);
2375 if (si->path) free(si->path);
2376 if (si->myname) free(si->myname);
2377 if (si->peername) free(si->peername);
2378 if (si->tmp_path) {
2379 unlink(si->tmp_path);
2380 free(si->tmp_path);
2382 free(si);
2384 return ret;