swrap: Replace linked list of socket_info with preallocated array of structures
[socket_wrapper.git] / src / socket_wrapper.c
1 /*
2  * Copyright (c) 2005-2008 Jelmer Vernooij <jelmer@samba.org>
3  * Copyright (C) 2006-2014 Stefan Metzmacher <metze@samba.org>
4  * Copyright (C) 2013-2014 Andreas Schneider <asn@samba.org>
5  *
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the author nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  */
36
37 /*
38    Socket wrapper library. Passes all socket communication over
39    unix domain sockets if the environment variable SOCKET_WRAPPER_DIR
40    is set.
41 */
42
43 #include "config.h"
44
45 #include <sys/types.h>
46 #include <sys/time.h>
47 #include <sys/stat.h>
48 #include <sys/socket.h>
49 #include <sys/ioctl.h>
50 #ifdef HAVE_SYS_FILIO_H
51 #include <sys/filio.h>
52 #endif
53 #ifdef HAVE_SYS_SIGNALFD_H
54 #include <sys/signalfd.h>
55 #endif
56 #ifdef HAVE_SYS_EVENTFD_H
57 #include <sys/eventfd.h>
58 #endif
59 #ifdef HAVE_SYS_TIMERFD_H
60 #include <sys/timerfd.h>
61 #endif
62 #include <sys/uio.h>
63 #include <errno.h>
64 #include <sys/un.h>
65 #include <netinet/in.h>
66 #include <netinet/tcp.h>
67 #include <arpa/inet.h>
68 #include <fcntl.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <stdio.h>
72 #include <stdint.h>
73 #include <stdarg.h>
74 #include <stdbool.h>
75 #include <unistd.h>
76 #ifdef HAVE_GNU_LIB_NAMES_H
77 #include <gnu/lib-names.h>
78 #endif
79 #ifdef HAVE_RPC_RPC_H
80 #include <rpc/rpc.h>
81 #endif
82
83 enum swrap_dbglvl_e {
84         SWRAP_LOG_ERROR = 0,
85         SWRAP_LOG_WARN,
86         SWRAP_LOG_DEBUG,
87         SWRAP_LOG_TRACE
88 };
89
90 /* GCC have printf type attribute check. */
91 #ifdef HAVE_FUNCTION_ATTRIBUTE_FORMAT
92 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
93 #else
94 #define PRINTF_ATTRIBUTE(a,b)
95 #endif /* HAVE_FUNCTION_ATTRIBUTE_FORMAT */
96
97 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
98 #define DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor))
99 #else
100 #define DESTRUCTOR_ATTRIBUTE
101 #endif
102
103 #ifdef HAVE_ADDRESS_SANITIZER_ATTRIBUTE
104 #define DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE __attribute__((no_sanitize_address))
105 #else
106 #define DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE
107 #endif
108
109 #ifdef HAVE_GCC_THREAD_LOCAL_STORAGE
110 # define SWRAP_THREAD __thread
111 #else
112 # define SWRAP_THREAD
113 #endif
114
115 #ifndef MIN
116 #define MIN(a,b) ((a)<(b)?(a):(b))
117 #endif
118
119 #ifndef ZERO_STRUCT
120 #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
121 #endif
122
123 #ifndef ZERO_STRUCTP
124 #define ZERO_STRUCTP(x) do { \
125                 if ((x) != NULL) \
126                         memset((char *)(x), 0, sizeof(*(x))); \
127         } while(0)
128 #endif
129
130 #ifndef discard_const
131 #define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
132 #endif
133
134 #ifndef discard_const_p
135 #define discard_const_p(type, ptr) ((type *)discard_const(ptr))
136 #endif
137
138 #ifdef IPV6_PKTINFO
139 # ifndef IPV6_RECVPKTINFO
140 #  define IPV6_RECVPKTINFO IPV6_PKTINFO
141 # endif /* IPV6_RECVPKTINFO */
142 #endif /* IPV6_PKTINFO */
143
144 /*
145  * On BSD IP_PKTINFO has a different name because during
146  * the time when they implemented it, there was no RFC.
147  * The name for IPv6 is the same as on Linux.
148  */
149 #ifndef IP_PKTINFO
150 # ifdef IP_RECVDSTADDR
151 #  define IP_PKTINFO IP_RECVDSTADDR
152 # endif
153 #endif
154
155
156 #define SWRAP_DLIST_ADD(list,item) do { \
157         if (!(list)) { \
158                 (item)->prev    = NULL; \
159                 (item)->next    = NULL; \
160                 (list)          = (item); \
161         } else { \
162                 (item)->prev    = NULL; \
163                 (item)->next    = (list); \
164                 (list)->prev    = (item); \
165                 (list)          = (item); \
166         } \
167 } while (0)
168
169 #define SWRAP_DLIST_REMOVE(list,item) do { \
170         if ((list) == (item)) { \
171                 (list)          = (item)->next; \
172                 if (list) { \
173                         (list)->prev    = NULL; \
174                 } \
175         } else { \
176                 if ((item)->prev) { \
177                         (item)->prev->next      = (item)->next; \
178                 } \
179                 if ((item)->next) { \
180                         (item)->next->prev      = (item)->prev; \
181                 } \
182         } \
183         (item)->prev    = NULL; \
184         (item)->next    = NULL; \
185 } while (0)
186
187 #define SWRAP_DLIST_ADD_AFTER(list, item, el) \
188 do { \
189         if ((list) != NULL || (el) != NULL) { \
190                 SWRAP_DLIST_ADD(list, item); \
191         } else { \
192                 (item)->prev = (el); \
193                 (item)->next = (el)->next; \
194                 (el)->next = (item); \
195                 if ((item)->next != NULL) { \
196                         (item)->next->prev = (item); \
197                 } \
198         } \
199 } while (0)
200
201 #if defined(HAVE_GETTIMEOFDAY_TZ) || defined(HAVE_GETTIMEOFDAY_TZ_VOID)
202 #define swrapGetTimeOfDay(tval) gettimeofday(tval,NULL)
203 #else
204 #define swrapGetTimeOfDay(tval) gettimeofday(tval)
205 #endif
206
207 /* we need to use a very terse format here as IRIX 6.4 silently
208    truncates names to 16 chars, so if we use a longer name then we
209    can't tell which port a packet came from with recvfrom()
210
211    with this format we have 8 chars left for the directory name
212 */
213 #define SOCKET_FORMAT "%c%02X%04X"
214 #define SOCKET_TYPE_CHAR_TCP            'T'
215 #define SOCKET_TYPE_CHAR_UDP            'U'
216 #define SOCKET_TYPE_CHAR_TCP_V6         'X'
217 #define SOCKET_TYPE_CHAR_UDP_V6         'Y'
218
219 /*
220  * Set the packet MTU to 1500 bytes for stream sockets to make it it easier to
221  * format PCAP capture files (as the caller will simply continue from here).
222  */
223 #define SOCKET_WRAPPER_MTU_DEFAULT 1500
224 #define SOCKET_WRAPPER_MTU_MIN     512
225 #define SOCKET_WRAPPER_MTU_MAX     32768
226
227 #define SOCKET_MAX_SOCKETS 1024
228
229
230 /*
231  * Maximum number of socket_info structures that can
232  * be used. Can be overriden by the environment variable
233  * SOCKET_WRAPPER_MAX_SOCKETS.
234  */
235 #define SOCKET_WRAPPER_MAX_SOCKETS_DEFAULT 65535
236
237 /* This limit is to avoid broadcast sendto() needing to stat too many
238  * files.  It may be raised (with a performance cost) to up to 254
239  * without changing the format above */
240 #define MAX_WRAPPED_INTERFACES 40
241
242 struct swrap_address {
243         socklen_t sa_socklen;
244         union {
245                 struct sockaddr s;
246                 struct sockaddr_in in;
247 #ifdef HAVE_IPV6
248                 struct sockaddr_in6 in6;
249 #endif
250                 struct sockaddr_un un;
251                 struct sockaddr_storage ss;
252         } sa;
253 };
254
255 struct socket_info_fd {
256         struct socket_info_fd *prev, *next;
257         int fd;
258
259         /*
260          * Points to corresponding index in array of
261          * socket_info structures
262          */
263         int si_index;
264 };
265
266 struct socket_info
267 {
268         unsigned int refcount;
269
270         int family;
271         int type;
272         int protocol;
273         int bound;
274         int bcast;
275         int is_server;
276         int connected;
277         int defer_connect;
278         int pktinfo;
279         int tcp_nodelay;
280
281         /* The unix path so we can unlink it on close() */
282         struct sockaddr_un un_addr;
283
284         struct swrap_address bindname;
285         struct swrap_address myname;
286         struct swrap_address peername;
287
288         struct {
289                 unsigned long pck_snd;
290                 unsigned long pck_rcv;
291         } io;
292 };
293
294 static struct socket_info *sockets;
295 static size_t max_sockets = 0;
296
297 /*
298  * While socket file descriptors are passed among different processes, the
299  * numerical value gets changed. So its better to store it locally to each
300  * process rather than including it within socket_info which will be shared.
301  */
302 static struct socket_info_fd *socket_fds;
303
304 /* Function prototypes */
305
306 bool socket_wrapper_enabled(void);
307 void swrap_destructor(void) DESTRUCTOR_ATTRIBUTE;
308
309 #ifdef NDEBUG
310 # define SWRAP_LOG(...)
311 #else
312
313 static void swrap_log(enum swrap_dbglvl_e dbglvl, const char *func, const char *format, ...) PRINTF_ATTRIBUTE(3, 4);
314 # define SWRAP_LOG(dbglvl, ...) swrap_log((dbglvl), __func__, __VA_ARGS__)
315
316 static void swrap_log(enum swrap_dbglvl_e dbglvl,
317                       const char *func,
318                       const char *format, ...)
319 {
320         char buffer[1024];
321         va_list va;
322         const char *d;
323         unsigned int lvl = 0;
324
325         d = getenv("SOCKET_WRAPPER_DEBUGLEVEL");
326         if (d != NULL) {
327                 lvl = atoi(d);
328         }
329
330         va_start(va, format);
331         vsnprintf(buffer, sizeof(buffer), format, va);
332         va_end(va);
333
334         if (lvl >= dbglvl) {
335                 switch (dbglvl) {
336                         case SWRAP_LOG_ERROR:
337                                 fprintf(stderr,
338                                         "SWRAP_ERROR(%d) - %s: %s\n",
339                                         (int)getpid(), func, buffer);
340                                 break;
341                         case SWRAP_LOG_WARN:
342                                 fprintf(stderr,
343                                         "SWRAP_WARN(%d) - %s: %s\n",
344                                         (int)getpid(), func, buffer);
345                                 break;
346                         case SWRAP_LOG_DEBUG:
347                                 fprintf(stderr,
348                                         "SWRAP_DEBUG(%d) - %s: %s\n",
349                                         (int)getpid(), func, buffer);
350                                 break;
351                         case SWRAP_LOG_TRACE:
352                                 fprintf(stderr,
353                                         "SWRAP_TRACE(%d) - %s: %s\n",
354                                         (int)getpid(), func, buffer);
355                                 break;
356                 }
357         }
358 }
359 #endif
360
361 /*********************************************************
362  * SWRAP LOADING LIBC FUNCTIONS
363  *********************************************************/
364
365 #include <dlfcn.h>
366
367 struct swrap_libc_fns {
368 #ifdef HAVE_ACCEPT4
369         int (*libc_accept4)(int sockfd,
370                            struct sockaddr *addr,
371                            socklen_t *addrlen,
372                            int flags);
373 #else
374         int (*libc_accept)(int sockfd,
375                            struct sockaddr *addr,
376                            socklen_t *addrlen);
377 #endif
378         int (*libc_bind)(int sockfd,
379                          const struct sockaddr *addr,
380                          socklen_t addrlen);
381         int (*libc_close)(int fd);
382         int (*libc_connect)(int sockfd,
383                             const struct sockaddr *addr,
384                             socklen_t addrlen);
385         int (*libc_dup)(int fd);
386         int (*libc_dup2)(int oldfd, int newfd);
387         int (*libc_fcntl)(int fd, int cmd, ...);
388         FILE *(*libc_fopen)(const char *name, const char *mode);
389 #ifdef HAVE_EVENTFD
390         int (*libc_eventfd)(int count, int flags);
391 #endif
392         int (*libc_getpeername)(int sockfd,
393                                 struct sockaddr *addr,
394                                 socklen_t *addrlen);
395         int (*libc_getsockname)(int sockfd,
396                                 struct sockaddr *addr,
397                                 socklen_t *addrlen);
398         int (*libc_getsockopt)(int sockfd,
399                                int level,
400                                int optname,
401                                void *optval,
402                                socklen_t *optlen);
403         int (*libc_ioctl)(int d, unsigned long int request, ...);
404         int (*libc_listen)(int sockfd, int backlog);
405         int (*libc_open)(const char *pathname, int flags, mode_t mode);
406         int (*libc_pipe)(int pipefd[2]);
407         int (*libc_read)(int fd, void *buf, size_t count);
408         ssize_t (*libc_readv)(int fd, const struct iovec *iov, int iovcnt);
409         int (*libc_recv)(int sockfd, void *buf, size_t len, int flags);
410         int (*libc_recvfrom)(int sockfd,
411                              void *buf,
412                              size_t len,
413                              int flags,
414                              struct sockaddr *src_addr,
415                              socklen_t *addrlen);
416         int (*libc_recvmsg)(int sockfd, const struct msghdr *msg, int flags);
417         int (*libc_send)(int sockfd, const void *buf, size_t len, int flags);
418         int (*libc_sendmsg)(int sockfd, const struct msghdr *msg, int flags);
419         int (*libc_sendto)(int sockfd,
420                            const void *buf,
421                            size_t len,
422                            int flags,
423                            const  struct sockaddr *dst_addr,
424                            socklen_t addrlen);
425         int (*libc_setsockopt)(int sockfd,
426                                int level,
427                                int optname,
428                                const void *optval,
429                                socklen_t optlen);
430 #ifdef HAVE_SIGNALFD
431         int (*libc_signalfd)(int fd, const sigset_t *mask, int flags);
432 #endif
433         int (*libc_socket)(int domain, int type, int protocol);
434         int (*libc_socketpair)(int domain, int type, int protocol, int sv[2]);
435 #ifdef HAVE_TIMERFD_CREATE
436         int (*libc_timerfd_create)(int clockid, int flags);
437 #endif
438         ssize_t (*libc_write)(int fd, const void *buf, size_t count);
439         ssize_t (*libc_writev)(int fd, const struct iovec *iov, int iovcnt);
440 };
441
442 struct swrap {
443         void *libc_handle;
444         void *libsocket_handle;
445
446         struct swrap_libc_fns fns;
447 };
448
449 static struct swrap swrap;
450
451 /* prototypes */
452 static const char *socket_wrapper_dir(void);
453
454 #define LIBC_NAME "libc.so"
455
456 enum swrap_lib {
457     SWRAP_LIBC,
458     SWRAP_LIBNSL,
459     SWRAP_LIBSOCKET,
460 };
461
462 #ifndef NDEBUG
463 static const char *swrap_str_lib(enum swrap_lib lib)
464 {
465         switch (lib) {
466         case SWRAP_LIBC:
467                 return "libc";
468         case SWRAP_LIBNSL:
469                 return "libnsl";
470         case SWRAP_LIBSOCKET:
471                 return "libsocket";
472         }
473
474         /* Compiler would warn us about unhandled enum value if we get here */
475         return "unknown";
476 }
477 #endif
478
479 static void *swrap_load_lib_handle(enum swrap_lib lib)
480 {
481         int flags = RTLD_LAZY;
482         void *handle = NULL;
483         int i;
484
485 #ifdef RTLD_DEEPBIND
486         flags |= RTLD_DEEPBIND;
487 #endif
488
489         switch (lib) {
490         case SWRAP_LIBNSL:
491                 /* FALL TROUGH */
492         case SWRAP_LIBSOCKET:
493 #ifdef HAVE_LIBSOCKET
494                 handle = swrap.libsocket_handle;
495                 if (handle == NULL) {
496                         for (i = 10; i >= 0; i--) {
497                                 char soname[256] = {0};
498
499                                 snprintf(soname, sizeof(soname), "libsocket.so.%d", i);
500                                 handle = dlopen(soname, flags);
501                                 if (handle != NULL) {
502                                         break;
503                                 }
504                         }
505
506                         swrap.libsocket_handle = handle;
507                 }
508                 break;
509 #endif
510                 /* FALL TROUGH */
511         case SWRAP_LIBC:
512                 handle = swrap.libc_handle;
513 #ifdef LIBC_SO
514                 if (handle == NULL) {
515                         handle = dlopen(LIBC_SO, flags);
516
517                         swrap.libc_handle = handle;
518                 }
519 #endif
520                 if (handle == NULL) {
521                         for (i = 10; i >= 0; i--) {
522                                 char soname[256] = {0};
523
524                                 snprintf(soname, sizeof(soname), "libc.so.%d", i);
525                                 handle = dlopen(soname, flags);
526                                 if (handle != NULL) {
527                                         break;
528                                 }
529                         }
530
531                         swrap.libc_handle = handle;
532                 }
533                 break;
534         }
535
536         if (handle == NULL) {
537 #ifdef RTLD_NEXT
538                 handle = swrap.libc_handle = swrap.libsocket_handle = RTLD_NEXT;
539 #else
540                 SWRAP_LOG(SWRAP_LOG_ERROR,
541                           "Failed to dlopen library: %s\n",
542                           dlerror());
543                 exit(-1);
544 #endif
545         }
546
547         return handle;
548 }
549
550 static void *_swrap_load_lib_function(enum swrap_lib lib, const char *fn_name)
551 {
552         void *handle;
553         void *func;
554
555         handle = swrap_load_lib_handle(lib);
556
557         func = dlsym(handle, fn_name);
558         if (func == NULL) {
559                 SWRAP_LOG(SWRAP_LOG_ERROR,
560                                 "Failed to find %s: %s\n",
561                                 fn_name, dlerror());
562                 exit(-1);
563         }
564
565         SWRAP_LOG(SWRAP_LOG_TRACE,
566                         "Loaded %s from %s",
567                         fn_name, swrap_str_lib(lib));
568         return func;
569 }
570
571 #define swrap_load_lib_function(lib, fn_name) \
572         if (swrap.fns.libc_##fn_name == NULL) { \
573                 void *swrap_cast_ptr = _swrap_load_lib_function(lib, #fn_name); \
574                 *(void **) (&swrap.fns.libc_##fn_name) = \
575                         swrap_cast_ptr; \
576         }
577
578
579 /*
580  * IMPORTANT
581  *
582  * Functions especially from libc need to be loaded individually, you can't load
583  * all at once or gdb will segfault at startup. The same applies to valgrind and
584  * has probably something todo with with the linker.
585  * So we need load each function at the point it is called the first time.
586  */
587 #ifdef HAVE_ACCEPT4
588 static int libc_accept4(int sockfd,
589                         struct sockaddr *addr,
590                         socklen_t *addrlen,
591                         int flags)
592 {
593         swrap_load_lib_function(SWRAP_LIBSOCKET, accept4);
594
595         return swrap.fns.libc_accept4(sockfd, addr, addrlen, flags);
596 }
597
598 #else /* HAVE_ACCEPT4 */
599
600 static int libc_accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
601 {
602         swrap_load_lib_function(SWRAP_LIBSOCKET, accept);
603
604         return swrap.fns.libc_accept(sockfd, addr, addrlen);
605 }
606 #endif /* HAVE_ACCEPT4 */
607
608 static int libc_bind(int sockfd,
609                      const struct sockaddr *addr,
610                      socklen_t addrlen)
611 {
612         swrap_load_lib_function(SWRAP_LIBSOCKET, bind);
613
614         return swrap.fns.libc_bind(sockfd, addr, addrlen);
615 }
616
617 static int libc_close(int fd)
618 {
619         swrap_load_lib_function(SWRAP_LIBC, close);
620
621         return swrap.fns.libc_close(fd);
622 }
623
624 static int libc_connect(int sockfd,
625                         const struct sockaddr *addr,
626                         socklen_t addrlen)
627 {
628         swrap_load_lib_function(SWRAP_LIBSOCKET, connect);
629
630         return swrap.fns.libc_connect(sockfd, addr, addrlen);
631 }
632
633 static int libc_dup(int fd)
634 {
635         swrap_load_lib_function(SWRAP_LIBC, dup);
636
637         return swrap.fns.libc_dup(fd);
638 }
639
640 static int libc_dup2(int oldfd, int newfd)
641 {
642         swrap_load_lib_function(SWRAP_LIBC, dup2);
643
644         return swrap.fns.libc_dup2(oldfd, newfd);
645 }
646
647 #ifdef HAVE_EVENTFD
648 static int libc_eventfd(int count, int flags)
649 {
650         swrap_load_lib_function(SWRAP_LIBC, eventfd);
651
652         return swrap.fns.libc_eventfd(count, flags);
653 }
654 #endif
655
656 DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE
657 static int libc_vfcntl(int fd, int cmd, va_list ap)
658 {
659         long int args[4];
660         int rc;
661         int i;
662
663         swrap_load_lib_function(SWRAP_LIBC, fcntl);
664
665         for (i = 0; i < 4; i++) {
666                 args[i] = va_arg(ap, long int);
667         }
668
669         rc = swrap.fns.libc_fcntl(fd,
670                                   cmd,
671                                   args[0],
672                                   args[1],
673                                   args[2],
674                                   args[3]);
675
676         return rc;
677 }
678
679 static int libc_getpeername(int sockfd,
680                             struct sockaddr *addr,
681                             socklen_t *addrlen)
682 {
683         swrap_load_lib_function(SWRAP_LIBSOCKET, getpeername);
684
685         return swrap.fns.libc_getpeername(sockfd, addr, addrlen);
686 }
687
688 static int libc_getsockname(int sockfd,
689                             struct sockaddr *addr,
690                             socklen_t *addrlen)
691 {
692         swrap_load_lib_function(SWRAP_LIBSOCKET, getsockname);
693
694         return swrap.fns.libc_getsockname(sockfd, addr, addrlen);
695 }
696
697 static int libc_getsockopt(int sockfd,
698                            int level,
699                            int optname,
700                            void *optval,
701                            socklen_t *optlen)
702 {
703         swrap_load_lib_function(SWRAP_LIBSOCKET, getsockopt);
704
705         return swrap.fns.libc_getsockopt(sockfd, level, optname, optval, optlen);
706 }
707
708 DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE
709 static int libc_vioctl(int d, unsigned long int request, va_list ap)
710 {
711         long int args[4];
712         int rc;
713         int i;
714
715         swrap_load_lib_function(SWRAP_LIBC, ioctl);
716
717         for (i = 0; i < 4; i++) {
718                 args[i] = va_arg(ap, long int);
719         }
720
721         rc = swrap.fns.libc_ioctl(d,
722                                   request,
723                                   args[0],
724                                   args[1],
725                                   args[2],
726                                   args[3]);
727
728         return rc;
729 }
730
731 static int libc_listen(int sockfd, int backlog)
732 {
733         swrap_load_lib_function(SWRAP_LIBSOCKET, listen);
734
735         return swrap.fns.libc_listen(sockfd, backlog);
736 }
737
738 static FILE *libc_fopen(const char *name, const char *mode)
739 {
740         swrap_load_lib_function(SWRAP_LIBC, fopen);
741
742         return swrap.fns.libc_fopen(name, mode);
743 }
744
745 static int libc_vopen(const char *pathname, int flags, va_list ap)
746 {
747         long int mode = 0;
748         int fd;
749
750         swrap_load_lib_function(SWRAP_LIBC, open);
751
752         mode = va_arg(ap, long int);
753
754         fd = swrap.fns.libc_open(pathname, flags, (mode_t)mode);
755
756         return fd;
757 }
758
759 static int libc_open(const char *pathname, int flags, ...)
760 {
761         va_list ap;
762         int fd;
763
764         va_start(ap, flags);
765         fd = libc_vopen(pathname, flags, ap);
766         va_end(ap);
767
768         return fd;
769 }
770
771 static int libc_pipe(int pipefd[2])
772 {
773         swrap_load_lib_function(SWRAP_LIBSOCKET, pipe);
774
775         return swrap.fns.libc_pipe(pipefd);
776 }
777
778 static int libc_read(int fd, void *buf, size_t count)
779 {
780         swrap_load_lib_function(SWRAP_LIBC, read);
781
782         return swrap.fns.libc_read(fd, buf, count);
783 }
784
785 static ssize_t libc_readv(int fd, const struct iovec *iov, int iovcnt)
786 {
787         swrap_load_lib_function(SWRAP_LIBSOCKET, readv);
788
789         return swrap.fns.libc_readv(fd, iov, iovcnt);
790 }
791
792 static int libc_recv(int sockfd, void *buf, size_t len, int flags)
793 {
794         swrap_load_lib_function(SWRAP_LIBSOCKET, recv);
795
796         return swrap.fns.libc_recv(sockfd, buf, len, flags);
797 }
798
799 static int libc_recvfrom(int sockfd,
800                          void *buf,
801                          size_t len,
802                          int flags,
803                          struct sockaddr *src_addr,
804                          socklen_t *addrlen)
805 {
806         swrap_load_lib_function(SWRAP_LIBSOCKET, recvfrom);
807
808         return swrap.fns.libc_recvfrom(sockfd, buf, len, flags, src_addr, addrlen);
809 }
810
811 static int libc_recvmsg(int sockfd, struct msghdr *msg, int flags)
812 {
813         swrap_load_lib_function(SWRAP_LIBSOCKET, recvmsg);
814
815         return swrap.fns.libc_recvmsg(sockfd, msg, flags);
816 }
817
818 static int libc_send(int sockfd, const void *buf, size_t len, int flags)
819 {
820         swrap_load_lib_function(SWRAP_LIBSOCKET, send);
821
822         return swrap.fns.libc_send(sockfd, buf, len, flags);
823 }
824
825 static int libc_sendmsg(int sockfd, const struct msghdr *msg, int flags)
826 {
827         swrap_load_lib_function(SWRAP_LIBSOCKET, sendmsg);
828
829         return swrap.fns.libc_sendmsg(sockfd, msg, flags);
830 }
831
832 static int libc_sendto(int sockfd,
833                        const void *buf,
834                        size_t len,
835                        int flags,
836                        const  struct sockaddr *dst_addr,
837                        socklen_t addrlen)
838 {
839         swrap_load_lib_function(SWRAP_LIBSOCKET, sendto);
840
841         return swrap.fns.libc_sendto(sockfd, buf, len, flags, dst_addr, addrlen);
842 }
843
844 static int libc_setsockopt(int sockfd,
845                            int level,
846                            int optname,
847                            const void *optval,
848                            socklen_t optlen)
849 {
850         swrap_load_lib_function(SWRAP_LIBSOCKET, setsockopt);
851
852         return swrap.fns.libc_setsockopt(sockfd, level, optname, optval, optlen);
853 }
854
855 #ifdef HAVE_SIGNALFD
856 static int libc_signalfd(int fd, const sigset_t *mask, int flags)
857 {
858         swrap_load_lib_function(SWRAP_LIBSOCKET, signalfd);
859
860         return swrap.fns.libc_signalfd(fd, mask, flags);
861 }
862 #endif
863
864 static int libc_socket(int domain, int type, int protocol)
865 {
866         swrap_load_lib_function(SWRAP_LIBSOCKET, socket);
867
868         return swrap.fns.libc_socket(domain, type, protocol);
869 }
870
871 static int libc_socketpair(int domain, int type, int protocol, int sv[2])
872 {
873         swrap_load_lib_function(SWRAP_LIBSOCKET, socketpair);
874
875         return swrap.fns.libc_socketpair(domain, type, protocol, sv);
876 }
877
878 #ifdef HAVE_TIMERFD_CREATE
879 static int libc_timerfd_create(int clockid, int flags)
880 {
881         swrap_load_lib_function(SWRAP_LIBC, timerfd_create);
882
883         return swrap.fns.libc_timerfd_create(clockid, flags);
884 }
885 #endif
886
887 static ssize_t libc_write(int fd, const void *buf, size_t count)
888 {
889         swrap_load_lib_function(SWRAP_LIBC, write);
890
891         return swrap.fns.libc_write(fd, buf, count);
892 }
893
894 static ssize_t libc_writev(int fd, const struct iovec *iov, int iovcnt)
895 {
896         swrap_load_lib_function(SWRAP_LIBSOCKET, writev);
897
898         return swrap.fns.libc_writev(fd, iov, iovcnt);
899 }
900
901 /*********************************************************
902  * SWRAP HELPER FUNCTIONS
903  *********************************************************/
904
905 #ifdef HAVE_IPV6
906 /*
907  * FD00::5357:5FXX
908  */
909 static const struct in6_addr *swrap_ipv6(void)
910 {
911         static struct in6_addr v;
912         static int initialized;
913         int ret;
914
915         if (initialized) {
916                 return &v;
917         }
918         initialized = 1;
919
920         ret = inet_pton(AF_INET6, "FD00::5357:5F00", &v);
921         if (ret <= 0) {
922                 abort();
923         }
924
925         return &v;
926 }
927 #endif
928
929 static void set_port(int family, int prt, struct swrap_address *addr)
930 {
931         switch (family) {
932         case AF_INET:
933                 addr->sa.in.sin_port = htons(prt);
934                 break;
935 #ifdef HAVE_IPV6
936         case AF_INET6:
937                 addr->sa.in6.sin6_port = htons(prt);
938                 break;
939 #endif
940         }
941 }
942
943 static size_t socket_length(int family)
944 {
945         switch (family) {
946         case AF_INET:
947                 return sizeof(struct sockaddr_in);
948 #ifdef HAVE_IPV6
949         case AF_INET6:
950                 return sizeof(struct sockaddr_in6);
951 #endif
952         }
953         return 0;
954 }
955
956 static const char *socket_wrapper_dir(void)
957 {
958         const char *s = getenv("SOCKET_WRAPPER_DIR");
959         if (s == NULL) {
960                 return NULL;
961         }
962         /* TODO use realpath(3) here, when we add support for threads */
963         if (strncmp(s, "./", 2) == 0) {
964                 s += 2;
965         }
966
967         SWRAP_LOG(SWRAP_LOG_TRACE, "socket_wrapper_dir: %s", s);
968         return s;
969 }
970
971 static unsigned int socket_wrapper_mtu(void)
972 {
973         static unsigned int max_mtu = 0;
974         unsigned int tmp;
975         const char *s;
976         char *endp;
977
978         if (max_mtu != 0) {
979                 return max_mtu;
980         }
981
982         max_mtu = SOCKET_WRAPPER_MTU_DEFAULT;
983
984         s = getenv("SOCKET_WRAPPER_MTU");
985         if (s == NULL) {
986                 goto done;
987         }
988
989         tmp = strtol(s, &endp, 10);
990         if (s == endp) {
991                 goto done;
992         }
993
994         if (tmp < SOCKET_WRAPPER_MTU_MIN || tmp > SOCKET_WRAPPER_MTU_MAX) {
995                 goto done;
996         }
997         max_mtu = tmp;
998
999 done:
1000         return max_mtu;
1001 }
1002
1003 static size_t socket_wrapper_max_sockets(void)
1004 {
1005         const char *s;
1006         unsigned long tmp;
1007         char *endp;
1008
1009         if (max_sockets != 0) {
1010                 return max_sockets;
1011         }
1012
1013         max_sockets = SOCKET_WRAPPER_MAX_SOCKETS_DEFAULT;
1014
1015         s = getenv("SOCKET_WRAPPER_MAX_SOCKETS");
1016         if (s == NULL || s[0] == '\0') {
1017                 goto done;
1018         }
1019
1020         tmp = strtoul(s, &endp, 10);
1021         if (s == endp) {
1022                 goto done;
1023         }
1024
1025         max_sockets = tmp;
1026
1027 done:
1028         return max_sockets;
1029 }
1030
1031 static void socket_wrapper_init_sockets(void)
1032 {
1033
1034         if (sockets != NULL) {
1035                 return;
1036         }
1037
1038         max_sockets = socket_wrapper_max_sockets();
1039
1040         sockets = (struct socket_info *)calloc(max_sockets,
1041                                                sizeof(struct socket_info));
1042
1043         if (sockets == NULL) {
1044                 SWRAP_LOG(SWRAP_LOG_ERROR,
1045                           "Failed to allocate sockets array.\n");
1046                 exit(-1);
1047         }
1048 }
1049
1050 bool socket_wrapper_enabled(void)
1051 {
1052         const char *s = socket_wrapper_dir();
1053
1054         if (s == NULL) {
1055                 return false;
1056         }
1057
1058         socket_wrapper_init_sockets();
1059
1060         return true;
1061 }
1062
1063 static unsigned int socket_wrapper_default_iface(void)
1064 {
1065         const char *s = getenv("SOCKET_WRAPPER_DEFAULT_IFACE");
1066         if (s) {
1067                 unsigned int iface;
1068                 if (sscanf(s, "%u", &iface) == 1) {
1069                         if (iface >= 1 && iface <= MAX_WRAPPED_INTERFACES) {
1070                                 return iface;
1071                         }
1072                 }
1073         }
1074
1075         return 1;/* 127.0.0.1 */
1076 }
1077
1078 static int socket_wrapper_first_free_index(void)
1079 {
1080         unsigned int i;
1081
1082         for (i = 0; i < max_sockets; ++i) {
1083                 if (sockets[i].refcount == 0) {
1084                         ZERO_STRUCT(sockets[i]);
1085                         return i;
1086                 }
1087         }
1088
1089         return -1;
1090 }
1091
1092 static int convert_un_in(const struct sockaddr_un *un, struct sockaddr *in, socklen_t *len)
1093 {
1094         unsigned int iface;
1095         unsigned int prt;
1096         const char *p;
1097         char type;
1098
1099         p = strrchr(un->sun_path, '/');
1100         if (p) p++; else p = un->sun_path;
1101
1102         if (sscanf(p, SOCKET_FORMAT, &type, &iface, &prt) != 3) {
1103                 errno = EINVAL;
1104                 return -1;
1105         }
1106
1107         SWRAP_LOG(SWRAP_LOG_TRACE, "type %c iface %u port %u",
1108                         type, iface, prt);
1109
1110         if (iface == 0 || iface > MAX_WRAPPED_INTERFACES) {
1111                 errno = EINVAL;
1112                 return -1;
1113         }
1114
1115         if (prt > 0xFFFF) {
1116                 errno = EINVAL;
1117                 return -1;
1118         }
1119
1120         switch(type) {
1121         case SOCKET_TYPE_CHAR_TCP:
1122         case SOCKET_TYPE_CHAR_UDP: {
1123                 struct sockaddr_in *in2 = (struct sockaddr_in *)(void *)in;
1124
1125                 if ((*len) < sizeof(*in2)) {
1126                     errno = EINVAL;
1127                     return -1;
1128                 }
1129
1130                 memset(in2, 0, sizeof(*in2));
1131                 in2->sin_family = AF_INET;
1132                 in2->sin_addr.s_addr = htonl((127<<24) | iface);
1133                 in2->sin_port = htons(prt);
1134
1135                 *len = sizeof(*in2);
1136                 break;
1137         }
1138 #ifdef HAVE_IPV6
1139         case SOCKET_TYPE_CHAR_TCP_V6:
1140         case SOCKET_TYPE_CHAR_UDP_V6: {
1141                 struct sockaddr_in6 *in2 = (struct sockaddr_in6 *)(void *)in;
1142
1143                 if ((*len) < sizeof(*in2)) {
1144                         errno = EINVAL;
1145                         return -1;
1146                 }
1147
1148                 memset(in2, 0, sizeof(*in2));
1149                 in2->sin6_family = AF_INET6;
1150                 in2->sin6_addr = *swrap_ipv6();
1151                 in2->sin6_addr.s6_addr[15] = iface;
1152                 in2->sin6_port = htons(prt);
1153
1154                 *len = sizeof(*in2);
1155                 break;
1156         }
1157 #endif
1158         default:
1159                 errno = EINVAL;
1160                 return -1;
1161         }
1162
1163         return 0;
1164 }
1165
1166 static int convert_in_un_remote(struct socket_info *si, const struct sockaddr *inaddr, struct sockaddr_un *un,
1167                                 int *bcast)
1168 {
1169         char type = '\0';
1170         unsigned int prt;
1171         unsigned int iface;
1172         int is_bcast = 0;
1173
1174         if (bcast) *bcast = 0;
1175
1176         switch (inaddr->sa_family) {
1177         case AF_INET: {
1178                 const struct sockaddr_in *in =
1179                     (const struct sockaddr_in *)(const void *)inaddr;
1180                 unsigned int addr = ntohl(in->sin_addr.s_addr);
1181                 char u_type = '\0';
1182                 char b_type = '\0';
1183                 char a_type = '\0';
1184
1185                 switch (si->type) {
1186                 case SOCK_STREAM:
1187                         u_type = SOCKET_TYPE_CHAR_TCP;
1188                         break;
1189                 case SOCK_DGRAM:
1190                         u_type = SOCKET_TYPE_CHAR_UDP;
1191                         a_type = SOCKET_TYPE_CHAR_UDP;
1192                         b_type = SOCKET_TYPE_CHAR_UDP;
1193                         break;
1194                 default:
1195                         SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!\n");
1196                         errno = ESOCKTNOSUPPORT;
1197                         return -1;
1198                 }
1199
1200                 prt = ntohs(in->sin_port);
1201                 if (a_type && addr == 0xFFFFFFFF) {
1202                         /* 255.255.255.255 only udp */
1203                         is_bcast = 2;
1204                         type = a_type;
1205                         iface = socket_wrapper_default_iface();
1206                 } else if (b_type && addr == 0x7FFFFFFF) {
1207                         /* 127.255.255.255 only udp */
1208                         is_bcast = 1;
1209                         type = b_type;
1210                         iface = socket_wrapper_default_iface();
1211                 } else if ((addr & 0xFFFFFF00) == 0x7F000000) {
1212                         /* 127.0.0.X */
1213                         is_bcast = 0;
1214                         type = u_type;
1215                         iface = (addr & 0x000000FF);
1216                 } else {
1217                         errno = ENETUNREACH;
1218                         return -1;
1219                 }
1220                 if (bcast) *bcast = is_bcast;
1221                 break;
1222         }
1223 #ifdef HAVE_IPV6
1224         case AF_INET6: {
1225                 const struct sockaddr_in6 *in =
1226                     (const struct sockaddr_in6 *)(const void *)inaddr;
1227                 struct in6_addr cmp1, cmp2;
1228
1229                 switch (si->type) {
1230                 case SOCK_STREAM:
1231                         type = SOCKET_TYPE_CHAR_TCP_V6;
1232                         break;
1233                 case SOCK_DGRAM:
1234                         type = SOCKET_TYPE_CHAR_UDP_V6;
1235                         break;
1236                 default:
1237                         SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!\n");
1238                         errno = ESOCKTNOSUPPORT;
1239                         return -1;
1240                 }
1241
1242                 /* XXX no multicast/broadcast */
1243
1244                 prt = ntohs(in->sin6_port);
1245
1246                 cmp1 = *swrap_ipv6();
1247                 cmp2 = in->sin6_addr;
1248                 cmp2.s6_addr[15] = 0;
1249                 if (IN6_ARE_ADDR_EQUAL(&cmp1, &cmp2)) {
1250                         iface = in->sin6_addr.s6_addr[15];
1251                 } else {
1252                         errno = ENETUNREACH;
1253                         return -1;
1254                 }
1255
1256                 break;
1257         }
1258 #endif
1259         default:
1260                 SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family!\n");
1261                 errno = ENETUNREACH;
1262                 return -1;
1263         }
1264
1265         if (prt == 0) {
1266                 SWRAP_LOG(SWRAP_LOG_WARN, "Port not set\n");
1267                 errno = EINVAL;
1268                 return -1;
1269         }
1270
1271         if (is_bcast) {
1272                 snprintf(un->sun_path, sizeof(un->sun_path), "%s/EINVAL",
1273                          socket_wrapper_dir());
1274                 SWRAP_LOG(SWRAP_LOG_DEBUG, "un path [%s]", un->sun_path);
1275                 /* the caller need to do more processing */
1276                 return 0;
1277         }
1278
1279         snprintf(un->sun_path, sizeof(un->sun_path), "%s/"SOCKET_FORMAT,
1280                  socket_wrapper_dir(), type, iface, prt);
1281         SWRAP_LOG(SWRAP_LOG_DEBUG, "un path [%s]", un->sun_path);
1282
1283         return 0;
1284 }
1285
1286 static int convert_in_un_alloc(struct socket_info *si, const struct sockaddr *inaddr, struct sockaddr_un *un,
1287                                int *bcast)
1288 {
1289         char type = '\0';
1290         unsigned int prt;
1291         unsigned int iface;
1292         struct stat st;
1293         int is_bcast = 0;
1294
1295         if (bcast) *bcast = 0;
1296
1297         switch (si->family) {
1298         case AF_INET: {
1299                 const struct sockaddr_in *in =
1300                     (const struct sockaddr_in *)(const void *)inaddr;
1301                 unsigned int addr = ntohl(in->sin_addr.s_addr);
1302                 char u_type = '\0';
1303                 char d_type = '\0';
1304                 char b_type = '\0';
1305                 char a_type = '\0';
1306
1307                 prt = ntohs(in->sin_port);
1308
1309                 switch (si->type) {
1310                 case SOCK_STREAM:
1311                         u_type = SOCKET_TYPE_CHAR_TCP;
1312                         d_type = SOCKET_TYPE_CHAR_TCP;
1313                         break;
1314                 case SOCK_DGRAM:
1315                         u_type = SOCKET_TYPE_CHAR_UDP;
1316                         d_type = SOCKET_TYPE_CHAR_UDP;
1317                         a_type = SOCKET_TYPE_CHAR_UDP;
1318                         b_type = SOCKET_TYPE_CHAR_UDP;
1319                         break;
1320                 default:
1321                         SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!\n");
1322                         errno = ESOCKTNOSUPPORT;
1323                         return -1;
1324                 }
1325
1326                 if (addr == 0) {
1327                         /* 0.0.0.0 */
1328                         is_bcast = 0;
1329                         type = d_type;
1330                         iface = socket_wrapper_default_iface();
1331                 } else if (a_type && addr == 0xFFFFFFFF) {
1332                         /* 255.255.255.255 only udp */
1333                         is_bcast = 2;
1334                         type = a_type;
1335                         iface = socket_wrapper_default_iface();
1336                 } else if (b_type && addr == 0x7FFFFFFF) {
1337                         /* 127.255.255.255 only udp */
1338                         is_bcast = 1;
1339                         type = b_type;
1340                         iface = socket_wrapper_default_iface();
1341                 } else if ((addr & 0xFFFFFF00) == 0x7F000000) {
1342                         /* 127.0.0.X */
1343                         is_bcast = 0;
1344                         type = u_type;
1345                         iface = (addr & 0x000000FF);
1346                 } else {
1347                         errno = EADDRNOTAVAIL;
1348                         return -1;
1349                 }
1350
1351                 /* Store the bind address for connect() */
1352                 if (si->bindname.sa_socklen == 0) {
1353                         struct sockaddr_in bind_in;
1354                         socklen_t blen = sizeof(struct sockaddr_in);
1355
1356                         ZERO_STRUCT(bind_in);
1357                         bind_in.sin_family = in->sin_family;
1358                         bind_in.sin_port = in->sin_port;
1359                         bind_in.sin_addr.s_addr = htonl(0x7F000000 | iface);
1360
1361                         si->bindname.sa_socklen = blen;
1362                         memcpy(&si->bindname.sa.in, &bind_in, blen);
1363                 }
1364
1365                 break;
1366         }
1367 #ifdef HAVE_IPV6
1368         case AF_INET6: {
1369                 const struct sockaddr_in6 *in =
1370                     (const struct sockaddr_in6 *)(const void *)inaddr;
1371                 struct in6_addr cmp1, cmp2;
1372
1373                 switch (si->type) {
1374                 case SOCK_STREAM:
1375                         type = SOCKET_TYPE_CHAR_TCP_V6;
1376                         break;
1377                 case SOCK_DGRAM:
1378                         type = SOCKET_TYPE_CHAR_UDP_V6;
1379                         break;
1380                 default:
1381                         SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!\n");
1382                         errno = ESOCKTNOSUPPORT;
1383                         return -1;
1384                 }
1385
1386                 /* XXX no multicast/broadcast */
1387
1388                 prt = ntohs(in->sin6_port);
1389
1390                 cmp1 = *swrap_ipv6();
1391                 cmp2 = in->sin6_addr;
1392                 cmp2.s6_addr[15] = 0;
1393                 if (IN6_IS_ADDR_UNSPECIFIED(&in->sin6_addr)) {
1394                         iface = socket_wrapper_default_iface();
1395                 } else if (IN6_ARE_ADDR_EQUAL(&cmp1, &cmp2)) {
1396                         iface = in->sin6_addr.s6_addr[15];
1397                 } else {
1398                         errno = EADDRNOTAVAIL;
1399                         return -1;
1400                 }
1401
1402                 /* Store the bind address for connect() */
1403                 if (si->bindname.sa_socklen == 0) {
1404                         struct sockaddr_in6 bind_in;
1405                         socklen_t blen = sizeof(struct sockaddr_in6);
1406
1407                         ZERO_STRUCT(bind_in);
1408                         bind_in.sin6_family = in->sin6_family;
1409                         bind_in.sin6_port = in->sin6_port;
1410
1411                         bind_in.sin6_addr = *swrap_ipv6();
1412                         bind_in.sin6_addr.s6_addr[15] = iface;
1413
1414                         memcpy(&si->bindname.sa.in6, &bind_in, blen);
1415                         si->bindname.sa_socklen = blen;
1416                 }
1417
1418                 break;
1419         }
1420 #endif
1421         default:
1422                 SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family\n");
1423                 errno = EADDRNOTAVAIL;
1424                 return -1;
1425         }
1426
1427
1428         if (bcast) *bcast = is_bcast;
1429
1430         if (iface == 0 || iface > MAX_WRAPPED_INTERFACES) {
1431                 errno = EINVAL;
1432                 return -1;
1433         }
1434
1435         if (prt == 0) {
1436                 /* handle auto-allocation of ephemeral ports */
1437                 for (prt = 5001; prt < 10000; prt++) {
1438                         snprintf(un->sun_path, sizeof(un->sun_path), "%s/"SOCKET_FORMAT,
1439                                  socket_wrapper_dir(), type, iface, prt);
1440                         if (stat(un->sun_path, &st) == 0) continue;
1441
1442                         set_port(si->family, prt, &si->myname);
1443                         set_port(si->family, prt, &si->bindname);
1444
1445                         break;
1446                 }
1447                 if (prt == 10000) {
1448                         errno = ENFILE;
1449                         return -1;
1450                 }
1451         }
1452
1453         snprintf(un->sun_path, sizeof(un->sun_path), "%s/"SOCKET_FORMAT,
1454                  socket_wrapper_dir(), type, iface, prt);
1455         SWRAP_LOG(SWRAP_LOG_DEBUG, "un path [%s]", un->sun_path);
1456         return 0;
1457 }
1458
1459 static struct socket_info_fd *find_socket_info_fd(int fd)
1460 {
1461         struct socket_info_fd *f;
1462
1463         for (f = socket_fds; f; f = f->next) {
1464                 if (f->fd == fd) {
1465                         return f;
1466                 }
1467         }
1468
1469         return NULL;
1470 }
1471
1472 static int find_socket_info_index(int fd)
1473 {
1474         struct socket_info_fd *fi = find_socket_info_fd(fd);
1475
1476         if (fi == NULL) {
1477                 return -1;
1478         }
1479
1480         return fi->si_index;
1481 }
1482
1483 static struct socket_info *find_socket_info(int fd)
1484 {
1485         int idx = find_socket_info_index(fd);
1486
1487         if (idx == -1) {
1488                 return NULL;
1489         }
1490
1491         return &sockets[idx];
1492 }
1493
1494 #if 0 /* FIXME */
1495 static bool check_addr_port_in_use(const struct sockaddr *sa, socklen_t len)
1496 {
1497         struct socket_info_fd *f;
1498         const struct socket_info *last_s = NULL;
1499
1500         /* first catch invalid input */
1501         switch (sa->sa_family) {
1502         case AF_INET:
1503                 if (len < sizeof(struct sockaddr_in)) {
1504                         return false;
1505                 }
1506                 break;
1507 #if HAVE_IPV6
1508         case AF_INET6:
1509                 if (len < sizeof(struct sockaddr_in6)) {
1510                         return false;
1511                 }
1512                 break;
1513 #endif
1514         default:
1515                 return false;
1516                 break;
1517         }
1518
1519         for (f = socket_fds; f; f = f->next) {
1520                 struct socket_info *s = &sockets[f->si_index];
1521
1522                 if (s == last_s) {
1523                         continue;
1524                 }
1525                 last_s = s;
1526
1527                 if (s->myname == NULL) {
1528                         continue;
1529                 }
1530                 if (s->myname->sa_family != sa->sa_family) {
1531                         continue;
1532                 }
1533                 switch (s->myname->sa_family) {
1534                 case AF_INET: {
1535                         struct sockaddr_in *sin1, *sin2;
1536
1537                         sin1 = (struct sockaddr_in *)s->myname;
1538                         sin2 = (struct sockaddr_in *)sa;
1539
1540                         if (sin1->sin_addr.s_addr == htonl(INADDR_ANY)) {
1541                                 continue;
1542                         }
1543                         if (sin1->sin_port != sin2->sin_port) {
1544                                 continue;
1545                         }
1546                         if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr) {
1547                                 continue;
1548                         }
1549
1550                         /* found */
1551                         return true;
1552                         break;
1553                 }
1554 #if HAVE_IPV6
1555                 case AF_INET6: {
1556                         struct sockaddr_in6 *sin1, *sin2;
1557
1558                         sin1 = (struct sockaddr_in6 *)s->myname;
1559                         sin2 = (struct sockaddr_in6 *)sa;
1560
1561                         if (sin1->sin6_port != sin2->sin6_port) {
1562                                 continue;
1563                         }
1564                         if (!IN6_ARE_ADDR_EQUAL(&sin1->sin6_addr,
1565                                                 &sin2->sin6_addr))
1566                         {
1567                                 continue;
1568                         }
1569
1570                         /* found */
1571                         return true;
1572                         break;
1573                 }
1574 #endif
1575                 default:
1576                         continue;
1577                         break;
1578
1579                 }
1580         }
1581
1582         return false;
1583 }
1584 #endif
1585
1586 static void swrap_remove_stale(int fd)
1587 {
1588         struct socket_info_fd *fi = find_socket_info_fd(fd);
1589         struct socket_info *si;
1590
1591         if (fi == NULL) {
1592                 return;
1593         }
1594
1595         si = &sockets[fi->si_index];
1596
1597         SWRAP_LOG(SWRAP_LOG_TRACE, "remove stale wrapper for %d", fd);
1598         SWRAP_DLIST_REMOVE(socket_fds, fi);
1599         free(fi);
1600
1601         si->refcount--;
1602
1603         if (si->refcount > 0) {
1604                 return;
1605         }
1606
1607         if (si->un_addr.sun_path[0] != '\0') {
1608                 unlink(si->un_addr.sun_path);
1609         }
1610 }
1611
1612 static int sockaddr_convert_to_un(struct socket_info *si,
1613                                   const struct sockaddr *in_addr,
1614                                   socklen_t in_len,
1615                                   struct sockaddr_un *out_addr,
1616                                   int alloc_sock,
1617                                   int *bcast)
1618 {
1619         struct sockaddr *out = (struct sockaddr *)(void *)out_addr;
1620
1621         (void) in_len; /* unused */
1622
1623         if (out_addr == NULL) {
1624                 return 0;
1625         }
1626
1627         out->sa_family = AF_UNIX;
1628 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1629         out->sa_len = sizeof(*out_addr);
1630 #endif
1631
1632         switch (in_addr->sa_family) {
1633         case AF_UNSPEC: {
1634                 const struct sockaddr_in *sin;
1635                 if (si->family != AF_INET) {
1636                         break;
1637                 }
1638                 if (in_len < sizeof(struct sockaddr_in)) {
1639                         break;
1640                 }
1641                 sin = (const struct sockaddr_in *)(const void *)in_addr;
1642                 if(sin->sin_addr.s_addr != htonl(INADDR_ANY)) {
1643                         break;
1644                 }
1645
1646                 /*
1647                  * Note: in the special case of AF_UNSPEC and INADDR_ANY,
1648                  * AF_UNSPEC is mapped to AF_INET and must be treated here.
1649                  */
1650
1651                 /* FALL THROUGH */
1652         }
1653         case AF_INET:
1654 #ifdef HAVE_IPV6
1655         case AF_INET6:
1656 #endif
1657                 switch (si->type) {
1658                 case SOCK_STREAM:
1659                 case SOCK_DGRAM:
1660                         break;
1661                 default:
1662                         SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!\n");
1663                         errno = ESOCKTNOSUPPORT;
1664                         return -1;
1665                 }
1666                 if (alloc_sock) {
1667                         return convert_in_un_alloc(si, in_addr, out_addr, bcast);
1668                 } else {
1669                         return convert_in_un_remote(si, in_addr, out_addr, bcast);
1670                 }
1671         default:
1672                 break;
1673         }
1674
1675         errno = EAFNOSUPPORT;
1676         SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family\n");
1677         return -1;
1678 }
1679
1680 static int sockaddr_convert_from_un(const struct socket_info *si,
1681                                     const struct sockaddr_un *in_addr,
1682                                     socklen_t un_addrlen,
1683                                     int family,
1684                                     struct sockaddr *out_addr,
1685                                     socklen_t *out_addrlen)
1686 {
1687         int ret;
1688
1689         if (out_addr == NULL || out_addrlen == NULL)
1690                 return 0;
1691
1692         if (un_addrlen == 0) {
1693                 *out_addrlen = 0;
1694                 return 0;
1695         }
1696
1697         switch (family) {
1698         case AF_INET:
1699 #ifdef HAVE_IPV6
1700         case AF_INET6:
1701 #endif
1702                 switch (si->type) {
1703                 case SOCK_STREAM:
1704                 case SOCK_DGRAM:
1705                         break;
1706                 default:
1707                         SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!\n");
1708                         errno = ESOCKTNOSUPPORT;
1709                         return -1;
1710                 }
1711                 ret = convert_un_in(in_addr, out_addr, out_addrlen);
1712 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1713                 out_addr->sa_len = *out_addrlen;
1714 #endif
1715                 return ret;
1716         default:
1717                 break;
1718         }
1719
1720         SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family\n");
1721         errno = EAFNOSUPPORT;
1722         return -1;
1723 }
1724
1725 enum swrap_packet_type {
1726         SWRAP_CONNECT_SEND,
1727         SWRAP_CONNECT_UNREACH,
1728         SWRAP_CONNECT_RECV,
1729         SWRAP_CONNECT_ACK,
1730         SWRAP_ACCEPT_SEND,
1731         SWRAP_ACCEPT_RECV,
1732         SWRAP_ACCEPT_ACK,
1733         SWRAP_RECVFROM,
1734         SWRAP_SENDTO,
1735         SWRAP_SENDTO_UNREACH,
1736         SWRAP_PENDING_RST,
1737         SWRAP_RECV,
1738         SWRAP_RECV_RST,
1739         SWRAP_SEND,
1740         SWRAP_SEND_RST,
1741         SWRAP_CLOSE_SEND,
1742         SWRAP_CLOSE_RECV,
1743         SWRAP_CLOSE_ACK,
1744 };
1745
1746 struct swrap_file_hdr {
1747         uint32_t        magic;
1748         uint16_t        version_major;
1749         uint16_t        version_minor;
1750         int32_t         timezone;
1751         uint32_t        sigfigs;
1752         uint32_t        frame_max_len;
1753 #define SWRAP_FRAME_LENGTH_MAX 0xFFFF
1754         uint32_t        link_type;
1755 };
1756 #define SWRAP_FILE_HDR_SIZE 24
1757
1758 struct swrap_packet_frame {
1759         uint32_t seconds;
1760         uint32_t micro_seconds;
1761         uint32_t recorded_length;
1762         uint32_t full_length;
1763 };
1764 #define SWRAP_PACKET_FRAME_SIZE 16
1765
1766 union swrap_packet_ip {
1767         struct {
1768                 uint8_t         ver_hdrlen;
1769                 uint8_t         tos;
1770                 uint16_t        packet_length;
1771                 uint16_t        identification;
1772                 uint8_t         flags;
1773                 uint8_t         fragment;
1774                 uint8_t         ttl;
1775                 uint8_t         protocol;
1776                 uint16_t        hdr_checksum;
1777                 uint32_t        src_addr;
1778                 uint32_t        dest_addr;
1779         } v4;
1780 #define SWRAP_PACKET_IP_V4_SIZE 20
1781         struct {
1782                 uint8_t         ver_prio;
1783                 uint8_t         flow_label_high;
1784                 uint16_t        flow_label_low;
1785                 uint16_t        payload_length;
1786                 uint8_t         next_header;
1787                 uint8_t         hop_limit;
1788                 uint8_t         src_addr[16];
1789                 uint8_t         dest_addr[16];
1790         } v6;
1791 #define SWRAP_PACKET_IP_V6_SIZE 40
1792 };
1793 #define SWRAP_PACKET_IP_SIZE 40
1794
1795 union swrap_packet_payload {
1796         struct {
1797                 uint16_t        source_port;
1798                 uint16_t        dest_port;
1799                 uint32_t        seq_num;
1800                 uint32_t        ack_num;
1801                 uint8_t         hdr_length;
1802                 uint8_t         control;
1803                 uint16_t        window;
1804                 uint16_t        checksum;
1805                 uint16_t        urg;
1806         } tcp;
1807 #define SWRAP_PACKET_PAYLOAD_TCP_SIZE 20
1808         struct {
1809                 uint16_t        source_port;
1810                 uint16_t        dest_port;
1811                 uint16_t        length;
1812                 uint16_t        checksum;
1813         } udp;
1814 #define SWRAP_PACKET_PAYLOAD_UDP_SIZE 8
1815         struct {
1816                 uint8_t         type;
1817                 uint8_t         code;
1818                 uint16_t        checksum;
1819                 uint32_t        unused;
1820         } icmp4;
1821 #define SWRAP_PACKET_PAYLOAD_ICMP4_SIZE 8
1822         struct {
1823                 uint8_t         type;
1824                 uint8_t         code;
1825                 uint16_t        checksum;
1826                 uint32_t        unused;
1827         } icmp6;
1828 #define SWRAP_PACKET_PAYLOAD_ICMP6_SIZE 8
1829 };
1830 #define SWRAP_PACKET_PAYLOAD_SIZE 20
1831
1832 #define SWRAP_PACKET_MIN_ALLOC \
1833         (SWRAP_PACKET_FRAME_SIZE + \
1834          SWRAP_PACKET_IP_SIZE + \
1835          SWRAP_PACKET_PAYLOAD_SIZE)
1836
1837 static const char *swrap_pcap_init_file(void)
1838 {
1839         static int initialized = 0;
1840         static const char *s = NULL;
1841         static const struct swrap_file_hdr h;
1842         static const struct swrap_packet_frame f;
1843         static const union swrap_packet_ip i;
1844         static const union swrap_packet_payload p;
1845
1846         if (initialized == 1) {
1847                 return s;
1848         }
1849         initialized = 1;
1850
1851         /*
1852          * TODO: don't use the structs use plain buffer offsets
1853          *       and PUSH_U8(), PUSH_U16() and PUSH_U32()
1854          *
1855          * for now make sure we disable PCAP support
1856          * if the struct has alignment!
1857          */
1858         if (sizeof(h) != SWRAP_FILE_HDR_SIZE) {
1859                 return NULL;
1860         }
1861         if (sizeof(f) != SWRAP_PACKET_FRAME_SIZE) {
1862                 return NULL;
1863         }
1864         if (sizeof(i) != SWRAP_PACKET_IP_SIZE) {
1865                 return NULL;
1866         }
1867         if (sizeof(i.v4) != SWRAP_PACKET_IP_V4_SIZE) {
1868                 return NULL;
1869         }
1870         if (sizeof(i.v6) != SWRAP_PACKET_IP_V6_SIZE) {
1871                 return NULL;
1872         }
1873         if (sizeof(p) != SWRAP_PACKET_PAYLOAD_SIZE) {
1874                 return NULL;
1875         }
1876         if (sizeof(p.tcp) != SWRAP_PACKET_PAYLOAD_TCP_SIZE) {
1877                 return NULL;
1878         }
1879         if (sizeof(p.udp) != SWRAP_PACKET_PAYLOAD_UDP_SIZE) {
1880                 return NULL;
1881         }
1882         if (sizeof(p.icmp4) != SWRAP_PACKET_PAYLOAD_ICMP4_SIZE) {
1883                 return NULL;
1884         }
1885         if (sizeof(p.icmp6) != SWRAP_PACKET_PAYLOAD_ICMP6_SIZE) {
1886                 return NULL;
1887         }
1888
1889         s = getenv("SOCKET_WRAPPER_PCAP_FILE");
1890         if (s == NULL) {
1891                 return NULL;
1892         }
1893         if (strncmp(s, "./", 2) == 0) {
1894                 s += 2;
1895         }
1896         return s;
1897 }
1898
1899 static uint8_t *swrap_pcap_packet_init(struct timeval *tval,
1900                                        const struct sockaddr *src,
1901                                        const struct sockaddr *dest,
1902                                        int socket_type,
1903                                        const uint8_t *payload,
1904                                        size_t payload_len,
1905                                        unsigned long tcp_seqno,
1906                                        unsigned long tcp_ack,
1907                                        unsigned char tcp_ctl,
1908                                        int unreachable,
1909                                        size_t *_packet_len)
1910 {
1911         uint8_t *base;
1912         uint8_t *buf;
1913         struct swrap_packet_frame *frame;
1914         union swrap_packet_ip *ip;
1915         union swrap_packet_payload *pay;
1916         size_t packet_len;
1917         size_t alloc_len;
1918         size_t nonwire_len = sizeof(*frame);
1919         size_t wire_hdr_len = 0;
1920         size_t wire_len = 0;
1921         size_t ip_hdr_len = 0;
1922         size_t icmp_hdr_len = 0;
1923         size_t icmp_truncate_len = 0;
1924         uint8_t protocol = 0, icmp_protocol = 0;
1925         const struct sockaddr_in *src_in = NULL;
1926         const struct sockaddr_in *dest_in = NULL;
1927 #ifdef HAVE_IPV6
1928         const struct sockaddr_in6 *src_in6 = NULL;
1929         const struct sockaddr_in6 *dest_in6 = NULL;
1930 #endif
1931         uint16_t src_port;
1932         uint16_t dest_port;
1933
1934         switch (src->sa_family) {
1935         case AF_INET:
1936                 src_in = (const struct sockaddr_in *)(const void *)src;
1937                 dest_in = (const struct sockaddr_in *)(const void *)dest;
1938                 src_port = src_in->sin_port;
1939                 dest_port = dest_in->sin_port;
1940                 ip_hdr_len = sizeof(ip->v4);
1941                 break;
1942 #ifdef HAVE_IPV6
1943         case AF_INET6:
1944                 src_in6 = (const struct sockaddr_in6 *)(const void *)src;
1945                 dest_in6 = (const struct sockaddr_in6 *)(const void *)dest;
1946                 src_port = src_in6->sin6_port;
1947                 dest_port = dest_in6->sin6_port;
1948                 ip_hdr_len = sizeof(ip->v6);
1949                 break;
1950 #endif
1951         default:
1952                 return NULL;
1953         }
1954
1955         switch (socket_type) {
1956         case SOCK_STREAM:
1957                 protocol = 0x06; /* TCP */
1958                 wire_hdr_len = ip_hdr_len + sizeof(pay->tcp);
1959                 wire_len = wire_hdr_len + payload_len;
1960                 break;
1961
1962         case SOCK_DGRAM:
1963                 protocol = 0x11; /* UDP */
1964                 wire_hdr_len = ip_hdr_len + sizeof(pay->udp);
1965                 wire_len = wire_hdr_len + payload_len;
1966                 break;
1967
1968         default:
1969                 return NULL;
1970         }
1971
1972         if (unreachable) {
1973                 icmp_protocol = protocol;
1974                 switch (src->sa_family) {
1975                 case AF_INET:
1976                         protocol = 0x01; /* ICMPv4 */
1977                         icmp_hdr_len = ip_hdr_len + sizeof(pay->icmp4);
1978                         break;
1979 #ifdef HAVE_IPV6
1980                 case AF_INET6:
1981                         protocol = 0x3A; /* ICMPv6 */
1982                         icmp_hdr_len = ip_hdr_len + sizeof(pay->icmp6);
1983                         break;
1984 #endif
1985                 }
1986                 if (wire_len > 64 ) {
1987                         icmp_truncate_len = wire_len - 64;
1988                 }
1989                 wire_hdr_len += icmp_hdr_len;
1990                 wire_len += icmp_hdr_len;
1991         }
1992
1993         packet_len = nonwire_len + wire_len;
1994         alloc_len = packet_len;
1995         if (alloc_len < SWRAP_PACKET_MIN_ALLOC) {
1996                 alloc_len = SWRAP_PACKET_MIN_ALLOC;
1997         }
1998
1999         base = (uint8_t *)calloc(1, alloc_len);
2000         if (base == NULL) {
2001                 return NULL;
2002         }
2003
2004         buf = base;
2005
2006         frame = (struct swrap_packet_frame *)(void *)buf;
2007         frame->seconds          = tval->tv_sec;
2008         frame->micro_seconds    = tval->tv_usec;
2009         frame->recorded_length  = wire_len - icmp_truncate_len;
2010         frame->full_length      = wire_len - icmp_truncate_len;
2011         buf += SWRAP_PACKET_FRAME_SIZE;
2012
2013         ip = (union swrap_packet_ip *)(void *)buf;
2014         switch (src->sa_family) {
2015         case AF_INET:
2016                 ip->v4.ver_hdrlen       = 0x45; /* version 4 and 5 * 32 bit words */
2017                 ip->v4.tos              = 0x00;
2018                 ip->v4.packet_length    = htons(wire_len - icmp_truncate_len);
2019                 ip->v4.identification   = htons(0xFFFF);
2020                 ip->v4.flags            = 0x40; /* BIT 1 set - means don't fragment */
2021                 ip->v4.fragment         = htons(0x0000);
2022                 ip->v4.ttl              = 0xFF;
2023                 ip->v4.protocol         = protocol;
2024                 ip->v4.hdr_checksum     = htons(0x0000);
2025                 ip->v4.src_addr         = src_in->sin_addr.s_addr;
2026                 ip->v4.dest_addr        = dest_in->sin_addr.s_addr;
2027                 buf += SWRAP_PACKET_IP_V4_SIZE;
2028                 break;
2029 #ifdef HAVE_IPV6
2030         case AF_INET6:
2031                 ip->v6.ver_prio         = 0x60; /* version 4 and 5 * 32 bit words */
2032                 ip->v6.flow_label_high  = 0x00;
2033                 ip->v6.flow_label_low   = 0x0000;
2034                 ip->v6.payload_length   = htons(wire_len - icmp_truncate_len); /* TODO */
2035                 ip->v6.next_header      = protocol;
2036                 memcpy(ip->v6.src_addr, src_in6->sin6_addr.s6_addr, 16);
2037                 memcpy(ip->v6.dest_addr, dest_in6->sin6_addr.s6_addr, 16);
2038                 buf += SWRAP_PACKET_IP_V6_SIZE;
2039                 break;
2040 #endif
2041         }
2042
2043         if (unreachable) {
2044                 pay = (union swrap_packet_payload *)(void *)buf;
2045                 switch (src->sa_family) {
2046                 case AF_INET:
2047                         pay->icmp4.type         = 0x03; /* destination unreachable */
2048                         pay->icmp4.code         = 0x01; /* host unreachable */
2049                         pay->icmp4.checksum     = htons(0x0000);
2050                         pay->icmp4.unused       = htonl(0x00000000);
2051                         buf += SWRAP_PACKET_PAYLOAD_ICMP4_SIZE;
2052
2053                         /* set the ip header in the ICMP payload */
2054                         ip = (union swrap_packet_ip *)(void *)buf;
2055                         ip->v4.ver_hdrlen       = 0x45; /* version 4 and 5 * 32 bit words */
2056                         ip->v4.tos              = 0x00;
2057                         ip->v4.packet_length    = htons(wire_len - icmp_hdr_len);
2058                         ip->v4.identification   = htons(0xFFFF);
2059                         ip->v4.flags            = 0x40; /* BIT 1 set - means don't fragment */
2060                         ip->v4.fragment         = htons(0x0000);
2061                         ip->v4.ttl              = 0xFF;
2062                         ip->v4.protocol         = icmp_protocol;
2063                         ip->v4.hdr_checksum     = htons(0x0000);
2064                         ip->v4.src_addr         = dest_in->sin_addr.s_addr;
2065                         ip->v4.dest_addr        = src_in->sin_addr.s_addr;
2066                         buf += SWRAP_PACKET_IP_V4_SIZE;
2067
2068                         src_port = dest_in->sin_port;
2069                         dest_port = src_in->sin_port;
2070                         break;
2071 #ifdef HAVE_IPV6
2072                 case AF_INET6:
2073                         pay->icmp6.type         = 0x01; /* destination unreachable */
2074                         pay->icmp6.code         = 0x03; /* address unreachable */
2075                         pay->icmp6.checksum     = htons(0x0000);
2076                         pay->icmp6.unused       = htonl(0x00000000);
2077                         buf += SWRAP_PACKET_PAYLOAD_ICMP6_SIZE;
2078
2079                         /* set the ip header in the ICMP payload */
2080                         ip = (union swrap_packet_ip *)(void *)buf;
2081                         ip->v6.ver_prio         = 0x60; /* version 4 and 5 * 32 bit words */
2082                         ip->v6.flow_label_high  = 0x00;
2083                         ip->v6.flow_label_low   = 0x0000;
2084                         ip->v6.payload_length   = htons(wire_len - icmp_truncate_len); /* TODO */
2085                         ip->v6.next_header      = protocol;
2086                         memcpy(ip->v6.src_addr, dest_in6->sin6_addr.s6_addr, 16);
2087                         memcpy(ip->v6.dest_addr, src_in6->sin6_addr.s6_addr, 16);
2088                         buf += SWRAP_PACKET_IP_V6_SIZE;
2089
2090                         src_port = dest_in6->sin6_port;
2091                         dest_port = src_in6->sin6_port;
2092                         break;
2093 #endif
2094                 }
2095         }
2096
2097         pay = (union swrap_packet_payload *)(void *)buf;
2098
2099         switch (socket_type) {
2100         case SOCK_STREAM:
2101                 pay->tcp.source_port    = src_port;
2102                 pay->tcp.dest_port      = dest_port;
2103                 pay->tcp.seq_num        = htonl(tcp_seqno);
2104                 pay->tcp.ack_num        = htonl(tcp_ack);
2105                 pay->tcp.hdr_length     = 0x50; /* 5 * 32 bit words */
2106                 pay->tcp.control        = tcp_ctl;
2107                 pay->tcp.window         = htons(0x7FFF);
2108                 pay->tcp.checksum       = htons(0x0000);
2109                 pay->tcp.urg            = htons(0x0000);
2110                 buf += SWRAP_PACKET_PAYLOAD_TCP_SIZE;
2111
2112                 break;
2113
2114         case SOCK_DGRAM:
2115                 pay->udp.source_port    = src_port;
2116                 pay->udp.dest_port      = dest_port;
2117                 pay->udp.length         = htons(8 + payload_len);
2118                 pay->udp.checksum       = htons(0x0000);
2119                 buf += SWRAP_PACKET_PAYLOAD_UDP_SIZE;
2120
2121                 break;
2122         }
2123
2124         if (payload && payload_len > 0) {
2125                 memcpy(buf, payload, payload_len);
2126         }
2127
2128         *_packet_len = packet_len - icmp_truncate_len;
2129         return base;
2130 }
2131
2132 static int swrap_pcap_get_fd(const char *fname)
2133 {
2134         static int fd = -1;
2135
2136         if (fd != -1) return fd;
2137
2138         fd = libc_open(fname, O_WRONLY|O_CREAT|O_EXCL|O_APPEND, 0644);
2139         if (fd != -1) {
2140                 struct swrap_file_hdr file_hdr;
2141                 file_hdr.magic          = 0xA1B2C3D4;
2142                 file_hdr.version_major  = 0x0002;       
2143                 file_hdr.version_minor  = 0x0004;
2144                 file_hdr.timezone       = 0x00000000;
2145                 file_hdr.sigfigs        = 0x00000000;
2146                 file_hdr.frame_max_len  = SWRAP_FRAME_LENGTH_MAX;
2147                 file_hdr.link_type      = 0x0065; /* 101 RAW IP */
2148
2149                 if (write(fd, &file_hdr, sizeof(file_hdr)) != sizeof(file_hdr)) {
2150                         close(fd);
2151                         fd = -1;
2152                 }
2153                 return fd;
2154         }
2155
2156         fd = libc_open(fname, O_WRONLY|O_APPEND, 0644);
2157
2158         return fd;
2159 }
2160
2161 static uint8_t *swrap_pcap_marshall_packet(struct socket_info *si,
2162                                            const struct sockaddr *addr,
2163                                            enum swrap_packet_type type,
2164                                            const void *buf, size_t len,
2165                                            size_t *packet_len)
2166 {
2167         const struct sockaddr *src_addr;
2168         const struct sockaddr *dest_addr;
2169         unsigned long tcp_seqno = 0;
2170         unsigned long tcp_ack = 0;
2171         unsigned char tcp_ctl = 0;
2172         int unreachable = 0;
2173
2174         struct timeval tv;
2175
2176         switch (si->family) {
2177         case AF_INET:
2178                 break;
2179 #ifdef HAVE_IPV6
2180         case AF_INET6:
2181                 break;
2182 #endif
2183         default:
2184                 return NULL;
2185         }
2186
2187         switch (type) {
2188         case SWRAP_CONNECT_SEND:
2189                 if (si->type != SOCK_STREAM) return NULL;
2190
2191                 src_addr  = &si->myname.sa.s;
2192                 dest_addr = addr;
2193
2194                 tcp_seqno = si->io.pck_snd;
2195                 tcp_ack = si->io.pck_rcv;
2196                 tcp_ctl = 0x02; /* SYN */
2197
2198                 si->io.pck_snd += 1;
2199
2200                 break;
2201
2202         case SWRAP_CONNECT_RECV:
2203                 if (si->type != SOCK_STREAM) return NULL;
2204
2205                 dest_addr = &si->myname.sa.s;
2206                 src_addr = addr;
2207
2208                 tcp_seqno = si->io.pck_rcv;
2209                 tcp_ack = si->io.pck_snd;
2210                 tcp_ctl = 0x12; /** SYN,ACK */
2211
2212                 si->io.pck_rcv += 1;
2213
2214                 break;
2215
2216         case SWRAP_CONNECT_UNREACH:
2217                 if (si->type != SOCK_STREAM) return NULL;
2218
2219                 dest_addr = &si->myname.sa.s;
2220                 src_addr  = addr;
2221
2222                 /* Unreachable: resend the data of SWRAP_CONNECT_SEND */
2223                 tcp_seqno = si->io.pck_snd - 1;
2224                 tcp_ack = si->io.pck_rcv;
2225                 tcp_ctl = 0x02; /* SYN */
2226                 unreachable = 1;
2227
2228                 break;
2229
2230         case SWRAP_CONNECT_ACK:
2231                 if (si->type != SOCK_STREAM) return NULL;
2232
2233                 src_addr  = &si->myname.sa.s;
2234                 dest_addr = addr;
2235
2236                 tcp_seqno = si->io.pck_snd;
2237                 tcp_ack = si->io.pck_rcv;
2238                 tcp_ctl = 0x10; /* ACK */
2239
2240                 break;
2241
2242         case SWRAP_ACCEPT_SEND:
2243                 if (si->type != SOCK_STREAM) return NULL;
2244
2245                 dest_addr = &si->myname.sa.s;
2246                 src_addr = addr;
2247
2248                 tcp_seqno = si->io.pck_rcv;
2249                 tcp_ack = si->io.pck_snd;
2250                 tcp_ctl = 0x02; /* SYN */
2251
2252                 si->io.pck_rcv += 1;
2253
2254                 break;
2255
2256         case SWRAP_ACCEPT_RECV:
2257                 if (si->type != SOCK_STREAM) return NULL;
2258
2259                 src_addr = &si->myname.sa.s;
2260                 dest_addr = addr;
2261
2262                 tcp_seqno = si->io.pck_snd;
2263                 tcp_ack = si->io.pck_rcv;
2264                 tcp_ctl = 0x12; /* SYN,ACK */
2265
2266                 si->io.pck_snd += 1;
2267
2268                 break;
2269
2270         case SWRAP_ACCEPT_ACK:
2271                 if (si->type != SOCK_STREAM) return NULL;
2272
2273                 dest_addr = &si->myname.sa.s;
2274                 src_addr = addr;
2275
2276                 tcp_seqno = si->io.pck_rcv;
2277                 tcp_ack = si->io.pck_snd;
2278                 tcp_ctl = 0x10; /* ACK */
2279
2280                 break;
2281
2282         case SWRAP_SEND:
2283                 src_addr  = &si->myname.sa.s;
2284                 dest_addr = &si->peername.sa.s;
2285
2286                 tcp_seqno = si->io.pck_snd;
2287                 tcp_ack = si->io.pck_rcv;
2288                 tcp_ctl = 0x18; /* PSH,ACK */
2289
2290                 si->io.pck_snd += len;
2291
2292                 break;
2293
2294         case SWRAP_SEND_RST:
2295                 dest_addr = &si->myname.sa.s;
2296                 src_addr  = &si->peername.sa.s;
2297
2298                 if (si->type == SOCK_DGRAM) {
2299                         return swrap_pcap_marshall_packet(si,
2300                                                           &si->peername.sa.s,
2301                                                           SWRAP_SENDTO_UNREACH,
2302                                                           buf,
2303                                                           len,
2304                                                           packet_len);
2305                 }
2306
2307                 tcp_seqno = si->io.pck_rcv;
2308                 tcp_ack = si->io.pck_snd;
2309                 tcp_ctl = 0x14; /** RST,ACK */
2310
2311                 break;
2312
2313         case SWRAP_PENDING_RST:
2314                 dest_addr = &si->myname.sa.s;
2315                 src_addr  = &si->peername.sa.s;
2316
2317                 if (si->type == SOCK_DGRAM) {
2318                         return NULL;
2319                 }
2320
2321                 tcp_seqno = si->io.pck_rcv;
2322                 tcp_ack = si->io.pck_snd;
2323                 tcp_ctl = 0x14; /* RST,ACK */
2324
2325                 break;
2326
2327         case SWRAP_RECV:
2328                 dest_addr = &si->myname.sa.s;
2329                 src_addr  = &si->peername.sa.s;
2330
2331                 tcp_seqno = si->io.pck_rcv;
2332                 tcp_ack = si->io.pck_snd;
2333                 tcp_ctl = 0x18; /* PSH,ACK */
2334
2335                 si->io.pck_rcv += len;
2336
2337                 break;
2338
2339         case SWRAP_RECV_RST:
2340                 dest_addr = &si->myname.sa.s;
2341                 src_addr  = &si->peername.sa.s;
2342
2343                 if (si->type == SOCK_DGRAM) {
2344                         return NULL;
2345                 }
2346
2347                 tcp_seqno = si->io.pck_rcv;
2348                 tcp_ack = si->io.pck_snd;
2349                 tcp_ctl = 0x14; /* RST,ACK */
2350
2351                 break;
2352
2353         case SWRAP_SENDTO:
2354                 src_addr = &si->myname.sa.s;
2355                 dest_addr = addr;
2356
2357                 si->io.pck_snd += len;
2358
2359                 break;
2360
2361         case SWRAP_SENDTO_UNREACH:
2362                 dest_addr = &si->myname.sa.s;
2363                 src_addr = addr;
2364
2365                 unreachable = 1;
2366
2367                 break;
2368
2369         case SWRAP_RECVFROM:
2370                 dest_addr = &si->myname.sa.s;
2371                 src_addr = addr;
2372
2373                 si->io.pck_rcv += len;
2374
2375                 break;
2376
2377         case SWRAP_CLOSE_SEND:
2378                 if (si->type != SOCK_STREAM) return NULL;
2379
2380                 src_addr  = &si->myname.sa.s;
2381                 dest_addr = &si->peername.sa.s;
2382
2383                 tcp_seqno = si->io.pck_snd;
2384                 tcp_ack = si->io.pck_rcv;
2385                 tcp_ctl = 0x11; /* FIN, ACK */
2386
2387                 si->io.pck_snd += 1;
2388
2389                 break;
2390
2391         case SWRAP_CLOSE_RECV:
2392                 if (si->type != SOCK_STREAM) return NULL;
2393
2394                 dest_addr = &si->myname.sa.s;
2395                 src_addr  = &si->peername.sa.s;
2396
2397                 tcp_seqno = si->io.pck_rcv;
2398                 tcp_ack = si->io.pck_snd;
2399                 tcp_ctl = 0x11; /* FIN,ACK */
2400
2401                 si->io.pck_rcv += 1;
2402
2403                 break;
2404
2405         case SWRAP_CLOSE_ACK:
2406                 if (si->type != SOCK_STREAM) return NULL;
2407
2408                 src_addr  = &si->myname.sa.s;
2409                 dest_addr = &si->peername.sa.s;
2410
2411                 tcp_seqno = si->io.pck_snd;
2412                 tcp_ack = si->io.pck_rcv;
2413                 tcp_ctl = 0x10; /* ACK */
2414
2415                 break;
2416         default:
2417                 return NULL;
2418         }
2419
2420         swrapGetTimeOfDay(&tv);
2421
2422         return swrap_pcap_packet_init(&tv,
2423                                       src_addr,
2424                                       dest_addr,
2425                                       si->type,
2426                                       (const uint8_t *)buf,
2427                                       len,
2428                                       tcp_seqno,
2429                                       tcp_ack,
2430                                       tcp_ctl,
2431                                       unreachable,
2432                                       packet_len);
2433 }
2434
2435 static void swrap_pcap_dump_packet(struct socket_info *si,
2436                                    const struct sockaddr *addr,
2437                                    enum swrap_packet_type type,
2438                                    const void *buf, size_t len)
2439 {
2440         const char *file_name;
2441         uint8_t *packet;
2442         size_t packet_len = 0;
2443         int fd;
2444
2445         file_name = swrap_pcap_init_file();
2446         if (!file_name) {
2447                 return;
2448         }
2449
2450         packet = swrap_pcap_marshall_packet(si,
2451                                             addr,
2452                                             type,
2453                                             buf,
2454                                             len,
2455                                             &packet_len);
2456         if (packet == NULL) {
2457                 return;
2458         }
2459
2460         fd = swrap_pcap_get_fd(file_name);
2461         if (fd != -1) {
2462                 if (write(fd, packet, packet_len) != (ssize_t)packet_len) {
2463                         free(packet);
2464                         return;
2465                 }
2466         }
2467
2468         free(packet);
2469 }
2470
2471 /****************************************************************************
2472  *   SIGNALFD
2473  ***************************************************************************/
2474
2475 #ifdef HAVE_SIGNALFD
2476 static int swrap_signalfd(int fd, const sigset_t *mask, int flags)
2477 {
2478         int rc;
2479
2480         rc = libc_signalfd(fd, mask, flags);
2481         if (rc != -1) {
2482                 swrap_remove_stale(fd);
2483         }
2484
2485         return rc;
2486 }
2487
2488 int signalfd(int fd, const sigset_t *mask, int flags)
2489 {
2490         return swrap_signalfd(fd, mask, flags);
2491 }
2492 #endif
2493
2494 /****************************************************************************
2495  *   SOCKET
2496  ***************************************************************************/
2497
2498 static int swrap_socket(int family, int type, int protocol)
2499 {
2500         struct socket_info *si;
2501         struct socket_info_fd *fi;
2502         int fd;
2503         int idx;
2504         int real_type = type;
2505
2506         /*
2507          * Remove possible addition flags passed to socket() so
2508          * do not fail checking the type.
2509          * See https://lwn.net/Articles/281965/
2510          */
2511 #ifdef SOCK_CLOEXEC
2512         real_type &= ~SOCK_CLOEXEC;
2513 #endif
2514 #ifdef SOCK_NONBLOCK
2515         real_type &= ~SOCK_NONBLOCK;
2516 #endif
2517
2518         if (!socket_wrapper_enabled()) {
2519                 return libc_socket(family, type, protocol);
2520         }
2521
2522         switch (family) {
2523         case AF_INET:
2524 #ifdef HAVE_IPV6
2525         case AF_INET6:
2526 #endif
2527                 break;
2528 #ifdef AF_NETLINK
2529         case AF_NETLINK:
2530 #endif /* AF_NETLINK */
2531 #ifdef AF_PACKET
2532         case AF_PACKET:
2533 #endif /* AF_PACKET */
2534         case AF_UNIX:
2535                 return libc_socket(family, type, protocol);
2536         default:
2537                 errno = EAFNOSUPPORT;
2538                 return -1;
2539         }
2540
2541         switch (real_type) {
2542         case SOCK_STREAM:
2543                 break;
2544         case SOCK_DGRAM:
2545                 break;
2546         default:
2547                 errno = EPROTONOSUPPORT;
2548                 return -1;
2549         }
2550
2551         switch (protocol) {
2552         case 0:
2553                 break;
2554         case 6:
2555                 if (real_type == SOCK_STREAM) {
2556                         break;
2557                 }
2558                 /*fall through*/
2559         case 17:
2560                 if (real_type == SOCK_DGRAM) {
2561                         break;
2562                 }
2563                 /*fall through*/
2564         default:
2565                 errno = EPROTONOSUPPORT;
2566                 return -1;
2567         }
2568
2569         /*
2570          * We must call libc_socket with type, from the caller, not the version
2571          * we removed SOCK_CLOEXEC and SOCK_NONBLOCK from
2572          */
2573         fd = libc_socket(AF_UNIX, type, 0);
2574
2575         if (fd == -1) {
2576                 return -1;
2577         }
2578
2579         /* Check if we have a stale fd and remove it */
2580         swrap_remove_stale(fd);
2581
2582         idx = socket_wrapper_first_free_index();
2583         if (idx == -1) {
2584                 errno = ENOMEM;
2585                 return -1;
2586         }
2587
2588         si = &sockets[idx];
2589
2590         si->family = family;
2591
2592         /* however, the rest of the socket_wrapper code expects just
2593          * the type, not the flags */
2594         si->type = real_type;
2595         si->protocol = protocol;
2596
2597         /*
2598          * Setup myname so getsockname() can succeed to find out the socket
2599          * type.
2600          */
2601         switch(si->family) {
2602         case AF_INET: {
2603                 struct sockaddr_in sin = {
2604                         .sin_family = AF_INET,
2605                 };
2606
2607                 si->myname.sa_socklen = sizeof(struct sockaddr_in);
2608                 memcpy(&si->myname.sa.in, &sin, si->myname.sa_socklen);
2609                 break;
2610         }
2611         case AF_INET6: {
2612                 struct sockaddr_in6 sin6 = {
2613                         .sin6_family = AF_INET6,
2614                 };
2615
2616                 si->myname.sa_socklen = sizeof(struct sockaddr_in6);
2617                 memcpy(&si->myname.sa.in6, &sin6, si->myname.sa_socklen);
2618                 break;
2619         }
2620         default:
2621                 errno = EINVAL;
2622                 return -1;
2623         }
2624
2625         fi = (struct socket_info_fd *)calloc(1, sizeof(struct socket_info_fd));
2626         if (fi == NULL) {
2627                 errno = ENOMEM;
2628                 return -1;
2629         }
2630
2631         si->refcount = 1;
2632         fi->fd = fd;
2633         fi->si_index = idx;
2634
2635         SWRAP_DLIST_ADD(socket_fds, fi);
2636
2637         SWRAP_LOG(SWRAP_LOG_TRACE,
2638                   "Created %s socket for protocol %s",
2639                   si->family == AF_INET ? "IPv4" : "IPv6",
2640                   si->type == SOCK_DGRAM ? "UDP" : "TCP");
2641
2642         return fd;
2643 }
2644
2645 int socket(int family, int type, int protocol)
2646 {
2647         return swrap_socket(family, type, protocol);
2648 }
2649
2650 /****************************************************************************
2651  *   SOCKETPAIR
2652  ***************************************************************************/
2653
2654 static int swrap_socketpair(int family, int type, int protocol, int sv[2])
2655 {
2656         int rc;
2657
2658         rc = libc_socketpair(family, type, protocol, sv);
2659         if (rc != -1) {
2660                 swrap_remove_stale(sv[0]);
2661                 swrap_remove_stale(sv[1]);
2662         }
2663
2664         return rc;
2665 }
2666
2667 int socketpair(int family, int type, int protocol, int sv[2])
2668 {
2669         return swrap_socketpair(family, type, protocol, sv);
2670 }
2671
2672 /****************************************************************************
2673  *   SOCKETPAIR
2674  ***************************************************************************/
2675
2676 #ifdef HAVE_TIMERFD_CREATE
2677 static int swrap_timerfd_create(int clockid, int flags)
2678 {
2679         int fd;
2680
2681         fd = libc_timerfd_create(clockid, flags);
2682         if (fd != -1) {
2683                 swrap_remove_stale(fd);
2684         }
2685
2686         return fd;
2687 }
2688
2689 int timerfd_create(int clockid, int flags)
2690 {
2691         return swrap_timerfd_create(clockid, flags);
2692 }
2693 #endif
2694
2695 /****************************************************************************
2696  *   PIPE
2697  ***************************************************************************/
2698
2699 static int swrap_pipe(int pipefd[2])
2700 {
2701         int rc;
2702
2703         rc = libc_pipe(pipefd);
2704         if (rc != -1) {
2705                 swrap_remove_stale(pipefd[0]);
2706                 swrap_remove_stale(pipefd[1]);
2707         }
2708
2709         return rc;
2710 }
2711
2712 int pipe(int pipefd[2])
2713 {
2714         return swrap_pipe(pipefd);
2715 }
2716
2717 /****************************************************************************
2718  *   ACCEPT
2719  ***************************************************************************/
2720
2721 static int swrap_accept(int s,
2722                         struct sockaddr *addr,
2723                         socklen_t *addrlen,
2724                         int flags)
2725 {
2726         struct socket_info *parent_si, *child_si;
2727         struct socket_info_fd *child_fi;
2728         int fd;
2729         int idx;
2730         struct swrap_address un_addr = {
2731                 .sa_socklen = sizeof(struct sockaddr_un),
2732         };
2733         struct swrap_address un_my_addr = {
2734                 .sa_socklen = sizeof(struct sockaddr_un),
2735         };
2736         struct swrap_address in_addr = {
2737                 .sa_socklen = sizeof(struct sockaddr_storage),
2738         };
2739         struct swrap_address in_my_addr = {
2740                 .sa_socklen = sizeof(struct sockaddr_storage),
2741         };
2742         int ret;
2743
2744         parent_si = find_socket_info(s);
2745         if (!parent_si) {
2746 #ifdef HAVE_ACCEPT4
2747                 return libc_accept4(s, addr, addrlen, flags);
2748 #else
2749                 return libc_accept(s, addr, addrlen);
2750 #endif
2751         }
2752
2753         /*
2754          * assume out sockaddr have the same size as the in parent
2755          * socket family
2756          */
2757         in_addr.sa_socklen = socket_length(parent_si->family);
2758         if (in_addr.sa_socklen <= 0) {
2759                 errno = EINVAL;
2760                 return -1;
2761         }
2762
2763 #ifdef HAVE_ACCEPT4
2764         ret = libc_accept4(s, &un_addr.sa.s, &un_addr.sa_socklen, flags);
2765 #else
2766         ret = libc_accept(s, &un_addr.sa.s, &un_addr.sa_socklen);
2767 #endif
2768         if (ret == -1) {
2769                 if (errno == ENOTSOCK) {
2770                         /* Remove stale fds */
2771                         swrap_remove_stale(s);
2772                 }
2773                 return ret;
2774         }
2775
2776         fd = ret;
2777
2778         ret = sockaddr_convert_from_un(parent_si,
2779                                        &un_addr.sa.un,
2780                                        un_addr.sa_socklen,
2781                                        parent_si->family,
2782                                        &in_addr.sa.s,
2783                                        &in_addr.sa_socklen);
2784         if (ret == -1) {
2785                 close(fd);
2786                 return ret;
2787         }
2788
2789         idx = socket_wrapper_first_free_index();
2790         if (idx == -1) {
2791                 errno = ENOMEM;
2792                 return -1;
2793         }
2794
2795         child_si = &sockets[idx];
2796
2797         child_fi = (struct socket_info_fd *)calloc(1, sizeof(struct socket_info_fd));
2798         if (child_fi == NULL) {
2799                 close(fd);
2800                 errno = ENOMEM;
2801                 return -1;
2802         }
2803
2804         child_fi->fd = fd;
2805
2806         child_si->family = parent_si->family;
2807         child_si->type = parent_si->type;
2808         child_si->protocol = parent_si->protocol;
2809         child_si->bound = 1;
2810         child_si->is_server = 1;
2811         child_si->connected = 1;
2812
2813         child_si->peername = (struct swrap_address) {
2814                 .sa_socklen = in_addr.sa_socklen,
2815         };
2816         memcpy(&child_si->peername.sa.ss, &in_addr.sa.ss, in_addr.sa_socklen);
2817
2818         if (addr != NULL && addrlen != NULL) {
2819                 size_t copy_len = MIN(*addrlen, in_addr.sa_socklen);
2820                 if (copy_len > 0) {
2821                         memcpy(addr, &in_addr.sa.ss, copy_len);
2822                 }
2823                 *addrlen = in_addr.sa_socklen;
2824         }
2825
2826         ret = libc_getsockname(fd,
2827                                &un_my_addr.sa.s,
2828                                &un_my_addr.sa_socklen);
2829         if (ret == -1) {
2830                 free(child_fi);
2831                 close(fd);
2832                 return ret;
2833         }
2834
2835         ret = sockaddr_convert_from_un(child_si,
2836                                        &un_my_addr.sa.un,
2837                                        un_my_addr.sa_socklen,
2838                                        child_si->family,
2839                                        &in_my_addr.sa.s,
2840                                        &in_my_addr.sa_socklen);
2841         if (ret == -1) {
2842                 free(child_fi);
2843                 close(fd);
2844                 return ret;
2845         }
2846
2847         SWRAP_LOG(SWRAP_LOG_TRACE,
2848                   "accept() path=%s, fd=%d",
2849                   un_my_addr.sa.un.sun_path, s);
2850
2851         child_si->myname = (struct swrap_address) {
2852                 .sa_socklen = in_my_addr.sa_socklen,
2853         };
2854         memcpy(&child_si->myname.sa.ss, &in_my_addr.sa.ss, in_my_addr.sa_socklen);
2855
2856         child_si->refcount = 1;
2857         child_fi->si_index = idx;
2858
2859         SWRAP_DLIST_ADD(socket_fds, child_fi);
2860
2861         if (addr != NULL) {
2862                 swrap_pcap_dump_packet(child_si, addr, SWRAP_ACCEPT_SEND, NULL, 0);
2863                 swrap_pcap_dump_packet(child_si, addr, SWRAP_ACCEPT_RECV, NULL, 0);
2864                 swrap_pcap_dump_packet(child_si, addr, SWRAP_ACCEPT_ACK, NULL, 0);
2865         }
2866
2867         return fd;
2868 }
2869
2870 #ifdef HAVE_ACCEPT4
2871 int accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags)
2872 {
2873         return swrap_accept(s, addr, (socklen_t *)addrlen, flags);
2874 }
2875 #endif
2876
2877 #ifdef HAVE_ACCEPT_PSOCKLEN_T
2878 int accept(int s, struct sockaddr *addr, Psocklen_t addrlen)
2879 #else
2880 int accept(int s, struct sockaddr *addr, socklen_t *addrlen)
2881 #endif
2882 {
2883         return swrap_accept(s, addr, (socklen_t *)addrlen, 0);
2884 }
2885
2886 static int autobind_start_init;
2887 static int autobind_start;
2888
2889 /* using sendto() or connect() on an unbound socket would give the
2890    recipient no way to reply, as unlike UDP and TCP, a unix domain
2891    socket can't auto-assign ephemeral port numbers, so we need to
2892    assign it here.
2893    Note: this might change the family from ipv6 to ipv4
2894 */
2895 static int swrap_auto_bind(int fd, struct socket_info *si, int family)
2896 {
2897         struct swrap_address un_addr = {
2898                 .sa_socklen = sizeof(struct sockaddr_un),
2899         };
2900         int i;
2901         char type;
2902         int ret;
2903         int port;
2904         struct stat st;
2905
2906         if (autobind_start_init != 1) {
2907                 autobind_start_init = 1;
2908                 autobind_start = getpid();
2909                 autobind_start %= 50000;
2910                 autobind_start += 10000;
2911         }
2912
2913         un_addr.sa.un.sun_family = AF_UNIX;
2914
2915         switch (family) {
2916         case AF_INET: {
2917                 struct sockaddr_in in;
2918
2919                 switch (si->type) {
2920                 case SOCK_STREAM:
2921                         type = SOCKET_TYPE_CHAR_TCP;
2922                         break;
2923                 case SOCK_DGRAM:
2924                         type = SOCKET_TYPE_CHAR_UDP;
2925                         break;
2926                 default:
2927                     errno = ESOCKTNOSUPPORT;
2928                     return -1;
2929                 }
2930
2931                 memset(&in, 0, sizeof(in));
2932                 in.sin_family = AF_INET;
2933                 in.sin_addr.s_addr = htonl(127<<24 |
2934                                            socket_wrapper_default_iface());
2935
2936                 si->myname = (struct swrap_address) {
2937                         .sa_socklen = sizeof(in),
2938                 };
2939                 memcpy(&si->myname.sa.in, &in, si->myname.sa_socklen);
2940                 break;
2941         }
2942 #ifdef HAVE_IPV6
2943         case AF_INET6: {
2944                 struct sockaddr_in6 in6;
2945
2946                 if (si->family != family) {
2947                         errno = ENETUNREACH;
2948                         return -1;
2949                 }
2950
2951                 switch (si->type) {
2952                 case SOCK_STREAM:
2953                         type = SOCKET_TYPE_CHAR_TCP_V6;
2954                         break;
2955                 case SOCK_DGRAM:
2956                         type = SOCKET_TYPE_CHAR_UDP_V6;
2957                         break;
2958                 default:
2959                         errno = ESOCKTNOSUPPORT;
2960                         return -1;
2961                 }
2962
2963                 memset(&in6, 0, sizeof(in6));
2964                 in6.sin6_family = AF_INET6;
2965                 in6.sin6_addr = *swrap_ipv6();
2966                 in6.sin6_addr.s6_addr[15] = socket_wrapper_default_iface();
2967
2968                 si->myname = (struct swrap_address) {
2969                         .sa_socklen = sizeof(in6),
2970                 };
2971                 memcpy(&si->myname.sa.in6, &in6, si->myname.sa_socklen);
2972                 break;
2973         }
2974 #endif
2975         default:
2976                 errno = ESOCKTNOSUPPORT;
2977                 return -1;
2978         }
2979
2980         if (autobind_start > 60000) {
2981                 autobind_start = 10000;
2982         }
2983
2984         for (i = 0; i < SOCKET_MAX_SOCKETS; i++) {
2985                 port = autobind_start + i;
2986                 snprintf(un_addr.sa.un.sun_path, sizeof(un_addr.sa.un.sun_path),
2987                          "%s/"SOCKET_FORMAT, socket_wrapper_dir(),
2988                          type, socket_wrapper_default_iface(), port);
2989                 if (stat(un_addr.sa.un.sun_path, &st) == 0) continue;
2990
2991                 ret = libc_bind(fd, &un_addr.sa.s, un_addr.sa_socklen);
2992                 if (ret == -1) return ret;
2993
2994                 si->un_addr = un_addr.sa.un;
2995
2996                 si->bound = 1;
2997                 autobind_start = port + 1;
2998                 break;
2999         }
3000         if (i == SOCKET_MAX_SOCKETS) {
3001                 SWRAP_LOG(SWRAP_LOG_ERROR, "Too many open unix sockets (%u) for "
3002                                            "interface "SOCKET_FORMAT,
3003                                            SOCKET_MAX_SOCKETS,
3004                                            type,
3005                                            socket_wrapper_default_iface(),
3006                                            0);
3007                 errno = ENFILE;
3008                 return -1;
3009         }
3010
3011         si->family = family;
3012         set_port(si->family, port, &si->myname);
3013
3014         return 0;
3015 }
3016
3017 /****************************************************************************
3018  *   CONNECT
3019  ***************************************************************************/
3020
3021 static int swrap_connect(int s, const struct sockaddr *serv_addr,
3022                          socklen_t addrlen)
3023 {
3024         int ret;
3025         struct swrap_address un_addr = {
3026                 .sa_socklen = sizeof(struct sockaddr_un),
3027         };
3028         struct socket_info *si = find_socket_info(s);
3029         int bcast = 0;
3030
3031         if (!si) {
3032                 return libc_connect(s, serv_addr, addrlen);
3033         }
3034
3035         if (si->bound == 0) {
3036                 ret = swrap_auto_bind(s, si, serv_addr->sa_family);
3037                 if (ret == -1) return -1;
3038         }
3039
3040         if (si->family != serv_addr->sa_family) {
3041                 errno = EINVAL;
3042                 return -1;
3043         }
3044
3045         ret = sockaddr_convert_to_un(si, serv_addr,
3046                                      addrlen, &un_addr.sa.un, 0, &bcast);
3047         if (ret == -1) return -1;
3048
3049         if (bcast) {
3050                 errno = ENETUNREACH;
3051                 return -1;
3052         }
3053
3054         if (si->type == SOCK_DGRAM) {
3055                 si->defer_connect = 1;
3056                 ret = 0;
3057         } else {
3058                 swrap_pcap_dump_packet(si, serv_addr, SWRAP_CONNECT_SEND, NULL, 0);
3059
3060                 ret = libc_connect(s,
3061                                    &un_addr.sa.s,
3062                                    un_addr.sa_socklen);
3063         }
3064
3065         SWRAP_LOG(SWRAP_LOG_TRACE,
3066                   "connect() path=%s, fd=%d",
3067                   un_addr.sa.un.sun_path, s);
3068
3069
3070         /* to give better errors */
3071         if (ret == -1 && errno == ENOENT) {
3072                 errno = EHOSTUNREACH;
3073         }
3074
3075         if (ret == 0) {
3076                 si->peername = (struct swrap_address) {
3077                         .sa_socklen = addrlen,
3078                 };
3079
3080                 memcpy(&si->peername.sa.ss, serv_addr, addrlen);
3081                 si->connected = 1;
3082
3083                 /*
3084                  * When we connect() on a socket than we have to bind the
3085                  * outgoing connection on the interface we use for the
3086                  * transport. We already bound it on the right interface
3087                  * but here we have to update the name so getsockname()
3088                  * returns correct information.
3089                  */
3090                 if (si->bindname.sa_socklen > 0) {
3091                         si->myname = (struct swrap_address) {
3092                                 .sa_socklen = si->bindname.sa_socklen,
3093                         };
3094
3095                         memcpy(&si->myname.sa.ss,
3096                                &si->bindname.sa.ss,
3097                                si->bindname.sa_socklen);
3098
3099                         /* Cleanup bindname */
3100                         si->bindname = (struct swrap_address) {
3101                                 .sa_socklen = 0,
3102                         };
3103                 }
3104
3105                 swrap_pcap_dump_packet(si, serv_addr, SWRAP_CONNECT_RECV, NULL, 0);
3106                 swrap_pcap_dump_packet(si, serv_addr, SWRAP_CONNECT_ACK, NULL, 0);
3107         } else {
3108                 swrap_pcap_dump_packet(si, serv_addr, SWRAP_CONNECT_UNREACH, NULL, 0);
3109         }
3110
3111         return ret;
3112 }
3113
3114 int connect(int s, const struct sockaddr *serv_addr, socklen_t addrlen)
3115 {
3116         return swrap_connect(s, serv_addr, addrlen);
3117 }
3118
3119 /****************************************************************************
3120  *   BIND
3121  ***************************************************************************/
3122
3123 static int swrap_bind(int s, const struct sockaddr *myaddr, socklen_t addrlen)
3124 {
3125         int ret;
3126         struct swrap_address un_addr = {
3127                 .sa_socklen = sizeof(struct sockaddr_un),
3128         };
3129         struct socket_info *si = find_socket_info(s);
3130         int bind_error = 0;
3131 #if 0 /* FIXME */
3132         bool in_use;
3133 #endif
3134
3135         if (!si) {
3136                 return libc_bind(s, myaddr, addrlen);
3137         }
3138
3139         switch (si->family) {
3140         case AF_INET: {
3141                 const struct sockaddr_in *sin;
3142                 if (addrlen < sizeof(struct sockaddr_in)) {
3143                         bind_error = EINVAL;
3144                         break;
3145                 }
3146
3147                 sin = (const struct sockaddr_in *)(const void *)myaddr;
3148
3149                 if (sin->sin_family != AF_INET) {
3150                         bind_error = EAFNOSUPPORT;
3151                 }
3152
3153                 /* special case for AF_UNSPEC */
3154                 if (sin->sin_family == AF_UNSPEC &&
3155                     (sin->sin_addr.s_addr == htonl(INADDR_ANY)))
3156                 {
3157                         bind_error = 0;
3158                 }
3159
3160                 break;
3161         }
3162 #ifdef HAVE_IPV6
3163         case AF_INET6: {
3164                 const struct sockaddr_in6 *sin6;
3165                 if (addrlen < sizeof(struct sockaddr_in6)) {
3166                         bind_error = EINVAL;
3167                         break;
3168                 }
3169
3170                 sin6 = (const struct sockaddr_in6 *)(const void *)myaddr;
3171
3172                 if (sin6->sin6_family != AF_INET6) {
3173                         bind_error = EAFNOSUPPORT;
3174                 }
3175
3176                 break;
3177         }
3178 #endif
3179         default:
3180                 bind_error = EINVAL;
3181                 break;
3182         }
3183
3184         if (bind_error != 0) {
3185                 errno = bind_error;
3186                 return -1;
3187         }
3188
3189 #if 0 /* FIXME */
3190         in_use = check_addr_port_in_use(myaddr, addrlen);
3191         if (in_use) {
3192                 errno = EADDRINUSE;
3193                 return -1;
3194         }
3195 #endif
3196
3197         si->myname.sa_socklen = addrlen;
3198         memcpy(&si->myname.sa.ss, myaddr, addrlen);
3199
3200         ret = sockaddr_convert_to_un(si,
3201                                      myaddr,
3202                                      addrlen,
3203                                      &un_addr.sa.un,
3204                                      1,
3205                                      &si->bcast);
3206         if (ret == -1) return -1;
3207
3208         unlink(un_addr.sa.un.sun_path);
3209
3210         ret = libc_bind(s, &un_addr.sa.s, un_addr.sa_socklen);
3211
3212         SWRAP_LOG(SWRAP_LOG_TRACE,
3213                   "bind() path=%s, fd=%d",
3214                   un_addr.sa.un.sun_path, s);
3215
3216         if (ret == 0) {
3217                 si->bound = 1;
3218         }
3219
3220         return ret;
3221 }
3222
3223 int bind(int s, const struct sockaddr *myaddr, socklen_t addrlen)
3224 {
3225         return swrap_bind(s, myaddr, addrlen);
3226 }
3227
3228 /****************************************************************************
3229  *   BINDRESVPORT
3230  ***************************************************************************/
3231
3232 #ifdef HAVE_BINDRESVPORT
3233 static int swrap_getsockname(int s, struct sockaddr *name, socklen_t *addrlen);
3234
3235 static int swrap_bindresvport_sa(int sd, struct sockaddr *sa)
3236 {
3237         struct swrap_address myaddr = {
3238                 .sa_socklen = sizeof(struct sockaddr_storage),
3239         };
3240         socklen_t salen;
3241         static uint16_t port;
3242         uint16_t i;
3243         int rc = -1;
3244         int af;
3245
3246 #define SWRAP_STARTPORT 600
3247 #define SWRAP_ENDPORT (IPPORT_RESERVED - 1)
3248 #define SWRAP_NPORTS (SWRAP_ENDPORT - SWRAP_STARTPORT + 1)
3249
3250         if (port == 0) {
3251                 port = (getpid() % SWRAP_NPORTS) + SWRAP_STARTPORT;
3252         }
3253
3254         if (sa == NULL) {
3255                 salen = myaddr.sa_socklen;
3256                 sa = &myaddr.sa.s;
3257
3258                 rc = swrap_getsockname(sd, &myaddr.sa.s, &salen);
3259                 if (rc < 0) {
3260                         return -1;
3261                 }
3262
3263                 af = sa->sa_family;
3264                 memset(&myaddr.sa.ss, 0, salen);
3265         } else {
3266                 af = sa->sa_family;
3267         }
3268
3269         for (i = 0; i < SWRAP_NPORTS; i++, port++) {
3270                 switch(af) {
3271                 case AF_INET: {
3272                         struct sockaddr_in *sinp = (struct sockaddr_in *)(void *)sa;
3273
3274                         salen = sizeof(struct sockaddr_in);
3275                         sinp->sin_port = htons(port);
3276                         break;
3277                 }
3278                 case AF_INET6: {
3279                         struct sockaddr_in6 *sin6p = (struct sockaddr_in6 *)(void *)sa;
3280
3281                         salen = sizeof(struct sockaddr_in6);
3282                         sin6p->sin6_port = htons(port);
3283                         break;
3284                 }
3285                 default:
3286                         errno = EAFNOSUPPORT;
3287                         return -1;
3288                 }
3289                 sa->sa_family = af;
3290
3291                 if (port > SWRAP_ENDPORT) {
3292                         port = SWRAP_STARTPORT;
3293                 }
3294
3295                 rc = swrap_bind(sd, (struct sockaddr *)sa, salen);
3296                 if (rc == 0 || errno != EADDRINUSE) {
3297                         break;
3298                 }
3299         }
3300
3301         return rc;
3302 }
3303
3304 int bindresvport(int sockfd, struct sockaddr_in *sinp)
3305 {
3306         return swrap_bindresvport_sa(sockfd, (struct sockaddr *)sinp);
3307 }
3308 #endif
3309
3310 /****************************************************************************
3311  *   LISTEN
3312  ***************************************************************************/
3313
3314 static int swrap_listen(int s, int backlog)
3315 {
3316         int ret;
3317         struct socket_info *si = find_socket_info(s);
3318
3319         if (!si) {
3320                 return libc_listen(s, backlog);
3321         }
3322
3323         if (si->bound == 0) {
3324                 ret = swrap_auto_bind(s, si, si->family);
3325                 if (ret == -1) {
3326                         errno = EADDRINUSE;
3327                         return ret;
3328                 }
3329         }
3330
3331         ret = libc_listen(s, backlog);
3332
3333         return ret;
3334 }
3335
3336 int listen(int s, int backlog)
3337 {
3338         return swrap_listen(s, backlog);
3339 }
3340
3341 /****************************************************************************
3342  *   FOPEN
3343  ***************************************************************************/
3344
3345 static FILE *swrap_fopen(const char *name, const char *mode)
3346 {
3347         FILE *fp;
3348
3349         fp = libc_fopen(name, mode);
3350         if (fp != NULL) {
3351                 int fd = fileno(fp);
3352
3353                 swrap_remove_stale(fd);
3354         }
3355
3356         return fp;
3357 }
3358
3359 FILE *fopen(const char *name, const char *mode)
3360 {
3361         return swrap_fopen(name, mode);
3362 }
3363
3364 /****************************************************************************
3365  *   OPEN
3366  ***************************************************************************/
3367
3368 static int swrap_vopen(const char *pathname, int flags, va_list ap)
3369 {
3370         int ret;
3371
3372         ret = libc_vopen(pathname, flags, ap);
3373         if (ret != -1) {
3374                 /*
3375                  * There are methods for closing descriptors (libc-internal code
3376                  * paths, direct syscalls) which close descriptors in ways that
3377                  * we can't intercept, so try to recover when we notice that
3378                  * that's happened
3379                  */
3380                 swrap_remove_stale(ret);
3381         }
3382         return ret;
3383 }
3384
3385 int open(const char *pathname, int flags, ...)
3386 {
3387         va_list ap;
3388         int fd;
3389
3390         va_start(ap, flags);
3391         fd = swrap_vopen(pathname, flags, ap);
3392         va_end(ap);
3393
3394         return fd;
3395 }
3396
3397 /****************************************************************************
3398  *   GETPEERNAME
3399  ***************************************************************************/
3400
3401 static int swrap_getpeername(int s, struct sockaddr *name, socklen_t *addrlen)
3402 {
3403         struct socket_info *si = find_socket_info(s);
3404         socklen_t len;
3405
3406         if (!si) {
3407                 return libc_getpeername(s, name, addrlen);
3408         }
3409
3410         if (si->peername.sa_socklen == 0)
3411         {
3412                 errno = ENOTCONN;
3413                 return -1;
3414         }
3415
3416         len = MIN(*addrlen, si->peername.sa_socklen);
3417         if (len == 0) {
3418                 return 0;
3419         }
3420
3421         memcpy(name, &si->peername.sa.ss, len);
3422         *addrlen = si->peername.sa_socklen;
3423
3424         return 0;
3425 }
3426
3427 #ifdef HAVE_ACCEPT_PSOCKLEN_T
3428 int getpeername(int s, struct sockaddr *name, Psocklen_t addrlen)
3429 #else
3430 int getpeername(int s, struct sockaddr *name, socklen_t *addrlen)
3431 #endif
3432 {
3433         return swrap_getpeername(s, name, (socklen_t *)addrlen);
3434 }
3435
3436 /****************************************************************************
3437  *   GETSOCKNAME
3438  ***************************************************************************/
3439
3440 static int swrap_getsockname(int s, struct sockaddr *name, socklen_t *addrlen)
3441 {
3442         struct socket_info *si = find_socket_info(s);
3443         socklen_t len;
3444
3445         if (!si) {
3446                 return libc_getsockname(s, name, addrlen);
3447         }
3448
3449         len = MIN(*addrlen, si->myname.sa_socklen);
3450         if (len == 0) {
3451                 return 0;
3452         }
3453
3454         memcpy(name, &si->myname.sa.ss, len);
3455         *addrlen = si->myname.sa_socklen;
3456
3457         return 0;
3458 }
3459
3460 #ifdef HAVE_ACCEPT_PSOCKLEN_T
3461 int getsockname(int s, struct sockaddr *name, Psocklen_t addrlen)
3462 #else
3463 int getsockname(int s, struct sockaddr *name, socklen_t *addrlen)
3464 #endif
3465 {
3466         return swrap_getsockname(s, name, (socklen_t *)addrlen);
3467 }
3468
3469 /****************************************************************************
3470  *   GETSOCKOPT
3471  ***************************************************************************/
3472
3473 #ifndef SO_PROTOCOL
3474 # ifdef SO_PROTOTYPE /* The Solaris name */
3475 #  define SO_PROTOCOL SO_PROTOTYPE
3476 # endif /* SO_PROTOTYPE */
3477 #endif /* SO_PROTOCOL */
3478
3479 static int swrap_getsockopt(int s, int level, int optname,
3480                             void *optval, socklen_t *optlen)
3481 {
3482         struct socket_info *si = find_socket_info(s);
3483
3484         if (!si) {
3485                 return libc_getsockopt(s,
3486                                        level,
3487                                        optname,
3488                                        optval,
3489                                        optlen);
3490         }
3491
3492         if (level == SOL_SOCKET) {
3493                 switch (optname) {
3494 #ifdef SO_DOMAIN
3495                 case SO_DOMAIN:
3496                         if (optval == NULL || optlen == NULL ||
3497                             *optlen < (socklen_t)sizeof(int)) {
3498                                 errno = EINVAL;
3499                                 return -1;
3500                         }
3501
3502                         *optlen = sizeof(int);
3503                         *(int *)optval = si->family;
3504                         return 0;
3505 #endif /* SO_DOMAIN */
3506
3507 #ifdef SO_PROTOCOL
3508                 case SO_PROTOCOL:
3509                         if (optval == NULL || optlen == NULL ||
3510                             *optlen < (socklen_t)sizeof(int)) {
3511                                 errno = EINVAL;
3512                                 return -1;
3513                         }
3514
3515                         *optlen = sizeof(int);
3516                         *(int *)optval = si->protocol;
3517                         return 0;
3518 #endif /* SO_PROTOCOL */
3519                 case SO_TYPE:
3520                         if (optval == NULL || optlen == NULL ||
3521                             *optlen < (socklen_t)sizeof(int)) {
3522                                 errno = EINVAL;
3523                                 return -1;
3524                         }
3525
3526                         *optlen = sizeof(int);
3527                         *(int *)optval = si->type;
3528                         return 0;
3529                 default:
3530                         return libc_getsockopt(s,
3531                                                level,
3532                                                optname,
3533                                                optval,
3534                                                optlen);
3535                 }
3536         } else if (level == IPPROTO_TCP) {
3537                 switch (optname) {
3538 #ifdef TCP_NODELAY
3539                 case TCP_NODELAY:
3540                         /*
3541                          * This enables sending packets directly out over TCP.
3542                          * As a unix socket is doing that any way, report it as
3543                          * enabled.
3544                          */
3545                         if (optval == NULL || optlen == NULL ||
3546                             *optlen < (socklen_t)sizeof(int)) {
3547                                 errno = EINVAL;
3548                                 return -1;
3549                         }
3550
3551                         *optlen = sizeof(int);
3552                         *(int *)optval = si->tcp_nodelay;
3553
3554                         return 0;
3555 #endif /* TCP_NODELAY */
3556                 default:
3557                         break;
3558                 }
3559         }
3560
3561         errno = ENOPROTOOPT;
3562         return -1;
3563 }
3564
3565 #ifdef HAVE_ACCEPT_PSOCKLEN_T
3566 int getsockopt(int s, int level, int optname, void *optval, Psocklen_t optlen)
3567 #else
3568 int getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen)
3569 #endif
3570 {
3571         return swrap_getsockopt(s, level, optname, optval, (socklen_t *)optlen);
3572 }
3573
3574 /****************************************************************************
3575  *   SETSOCKOPT
3576  ***************************************************************************/
3577
3578 static int swrap_setsockopt(int s, int level, int optname,
3579                             const void *optval, socklen_t optlen)
3580 {
3581         struct socket_info *si = find_socket_info(s);
3582
3583         if (!si) {
3584                 return libc_setsockopt(s,
3585                                        level,
3586                                        optname,
3587                                        optval,
3588                                        optlen);
3589         }
3590
3591         if (level == SOL_SOCKET) {
3592                 return libc_setsockopt(s,
3593                                        level,
3594                                        optname,
3595                                        optval,
3596                                        optlen);
3597         } else if (level == IPPROTO_TCP) {
3598                 switch (optname) {
3599 #ifdef TCP_NODELAY
3600                 case TCP_NODELAY: {
3601                         int i;
3602
3603                         /*
3604                          * This enables sending packets directly out over TCP.
3605                          * A unix socket is doing that any way.
3606                          */
3607                         if (optval == NULL || optlen == 0 ||
3608                             optlen < (socklen_t)sizeof(int)) {
3609                                 errno = EINVAL;
3610                                 return -1;
3611                         }
3612
3613                         i = *discard_const_p(int, optval);
3614                         if (i != 0 && i != 1) {
3615                                 errno = EINVAL;
3616                                 return -1;
3617                         }
3618                         si->tcp_nodelay = i;
3619
3620                         return 0;
3621                 }
3622 #endif /* TCP_NODELAY */
3623                 default:
3624                         break;
3625                 }
3626         }
3627
3628         switch (si->family) {
3629         case AF_INET:
3630                 if (level == IPPROTO_IP) {
3631 #ifdef IP_PKTINFO
3632                         if (optname == IP_PKTINFO) {
3633                                 si->pktinfo = AF_INET;
3634                         }
3635 #endif /* IP_PKTINFO */
3636                 }
3637                 return 0;
3638 #ifdef HAVE_IPV6
3639         case AF_INET6:
3640                 if (level == IPPROTO_IPV6) {
3641 #ifdef IPV6_RECVPKTINFO
3642                         if (optname == IPV6_RECVPKTINFO) {
3643                                 si->pktinfo = AF_INET6;
3644                         }
3645 #endif /* IPV6_PKTINFO */
3646                 }
3647                 return 0;
3648 #endif
3649         default:
3650                 errno = ENOPROTOOPT;
3651                 return -1;
3652         }
3653 }
3654
3655 int setsockopt(int s, int level, int optname,
3656                const void *optval, socklen_t optlen)
3657 {
3658         return swrap_setsockopt(s, level, optname, optval, optlen);
3659 }
3660
3661 /****************************************************************************
3662  *   IOCTL
3663  ***************************************************************************/
3664
3665 static int swrap_vioctl(int s, unsigned long int r, va_list va)
3666 {
3667         struct socket_info *si = find_socket_info(s);
3668         va_list ap;
3669         int value;
3670         int rc;
3671
3672         if (!si) {
3673                 return libc_vioctl(s, r, va);
3674         }
3675
3676         va_copy(ap, va);
3677
3678         rc = libc_vioctl(s, r, va);
3679
3680         switch (r) {
3681         case FIONREAD:
3682                 value = *((int *)va_arg(ap, int *));
3683
3684                 if (rc == -1 && errno != EAGAIN && errno != ENOBUFS) {
3685                         swrap_pcap_dump_packet(si, NULL, SWRAP_PENDING_RST, NULL, 0);
3686                 } else if (value == 0) { /* END OF FILE */
3687                         swrap_pcap_dump_packet(si, NULL, SWRAP_PENDING_RST, NULL, 0);
3688                 }
3689                 break;
3690         }
3691
3692         va_end(ap);
3693
3694         return rc;
3695 }
3696
3697 #ifdef HAVE_IOCTL_INT
3698 int ioctl(int s, int r, ...)
3699 #else
3700 int ioctl(int s, unsigned long int r, ...)
3701 #endif
3702 {
3703         va_list va;
3704         int rc;
3705
3706         va_start(va, r);
3707
3708         rc = swrap_vioctl(s, (unsigned long int) r, va);
3709
3710         va_end(va);
3711
3712         return rc;
3713 }
3714
3715 /*****************
3716  * CMSG
3717  *****************/
3718
3719 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
3720
3721 #ifndef CMSG_ALIGN
3722 # ifdef _ALIGN /* BSD */
3723 #define CMSG_ALIGN _ALIGN
3724 # else
3725 #define CMSG_ALIGN(len) (((len) + sizeof(size_t) - 1) & ~(sizeof(size_t) - 1))
3726 # endif /* _ALIGN */
3727 #endif /* CMSG_ALIGN */
3728
3729 /**
3730  * @brief Add a cmsghdr to a msghdr.
3731  *
3732  * This is an function to add any type of cmsghdr. It will operate on the
3733  * msg->msg_control and msg->msg_controllen you pass in by adapting them to
3734  * the buffer position after the added cmsg element. Hence, this function is
3735  * intended to be used with an intermediate msghdr and not on the original
3736  * one handed in by the client.
3737  *
3738  * @param[in]  msg      The msghdr to which to add the cmsg.
3739  *
3740  * @param[in]  level    The cmsg level to set.
3741  *
3742  * @param[in]  type     The cmsg type to set.
3743  *
3744  * @param[in]  data     The cmsg data to set.
3745  *
3746  * @param[in]  len      the length of the data to set.
3747  */
3748 static void swrap_msghdr_add_cmsghdr(struct msghdr *msg,
3749                                      int level,
3750                                      int type,
3751                                      const void *data,
3752                                      size_t len)
3753 {
3754         size_t cmlen = CMSG_LEN(len);
3755         size_t cmspace = CMSG_SPACE(len);
3756         uint8_t cmbuf[cmspace];
3757         void *cast_ptr = (void *)cmbuf;
3758         struct cmsghdr *cm = (struct cmsghdr *)cast_ptr;
3759         uint8_t *p;
3760
3761         memset(cmbuf, 0, cmspace);
3762
3763         if (msg->msg_controllen < cmlen) {
3764                 cmlen = msg->msg_controllen;
3765                 msg->msg_flags |= MSG_CTRUNC;
3766         }
3767
3768         if (msg->msg_controllen < cmspace) {
3769                 cmspace = msg->msg_controllen;
3770         }
3771
3772         /*
3773          * We copy the full input data into an intermediate cmsghdr first
3774          * in order to more easily cope with truncation.
3775          */
3776         cm->cmsg_len = cmlen;
3777         cm->cmsg_level = level;
3778         cm->cmsg_type = type;
3779         memcpy(CMSG_DATA(cm), data, len);
3780
3781         /*
3782          * We now copy the possibly truncated buffer.
3783          * We copy cmlen bytes, but consume cmspace bytes,
3784          * leaving the possible padding uninitialiazed.
3785          */
3786         p = (uint8_t *)msg->msg_control;
3787         memcpy(p, cm, cmlen);
3788         p += cmspace;
3789         msg->msg_control = p;
3790         msg->msg_controllen -= cmspace;
3791
3792         return;
3793 }
3794
3795 static int swrap_msghdr_add_pktinfo(struct socket_info *si,
3796                                     struct msghdr *msg)
3797 {
3798         /* Add packet info */
3799         switch (si->pktinfo) {
3800 #if defined(IP_PKTINFO) && (defined(HAVE_STRUCT_IN_PKTINFO) || defined(IP_RECVDSTADDR))
3801         case AF_INET: {
3802                 struct sockaddr_in *sin;
3803 #if defined(HAVE_STRUCT_IN_PKTINFO)
3804                 struct in_pktinfo pkt;
3805 #elif defined(IP_RECVDSTADDR)
3806                 struct in_addr pkt;
3807 #endif
3808
3809                 if (si->bindname.sa_socklen == sizeof(struct sockaddr_in)) {
3810                         sin = &si->bindname.sa.in;
3811                 } else {
3812                         if (si->myname.sa_socklen != sizeof(struct sockaddr_in)) {
3813                                 return 0;
3814                         }
3815                         sin = &si->myname.sa.in;
3816                 }
3817
3818                 ZERO_STRUCT(pkt);
3819
3820 #if defined(HAVE_STRUCT_IN_PKTINFO)
3821                 pkt.ipi_ifindex = socket_wrapper_default_iface();
3822                 pkt.ipi_addr.s_addr = sin->sin_addr.s_addr;
3823 #elif defined(IP_RECVDSTADDR)
3824                 pkt = sin->sin_addr;
3825 #endif
3826
3827                 swrap_msghdr_add_cmsghdr(msg, IPPROTO_IP, IP_PKTINFO,
3828                                          &pkt, sizeof(pkt));
3829
3830                 break;
3831         }
3832 #endif /* IP_PKTINFO */
3833 #if defined(HAVE_IPV6)
3834         case AF_INET6: {
3835 #if defined(IPV6_PKTINFO) && defined(HAVE_STRUCT_IN6_PKTINFO)
3836                 struct sockaddr_in6 *sin6;
3837                 struct in6_pktinfo pkt6;
3838
3839                 if (si->bindname.sa_socklen == sizeof(struct sockaddr_in6)) {
3840                         sin6 = &si->bindname.sa.in6;
3841                 } else {
3842                         if (si->myname.sa_socklen != sizeof(struct sockaddr_in6)) {
3843                                 return 0;
3844                         }
3845                         sin6 = &si->myname.sa.in6;
3846                 }
3847
3848                 ZERO_STRUCT(pkt6);
3849
3850                 pkt6.ipi6_ifindex = socket_wrapper_default_iface();
3851                 pkt6.ipi6_addr = sin6->sin6_addr;
3852
3853                 swrap_msghdr_add_cmsghdr(msg, IPPROTO_IPV6, IPV6_PKTINFO,
3854                                         &pkt6, sizeof(pkt6));
3855 #endif /* HAVE_STRUCT_IN6_PKTINFO */
3856
3857                 break;
3858         }
3859 #endif /* IPV6_PKTINFO */
3860         default:
3861                 return -1;
3862         }
3863
3864         return 0;
3865 }
3866
3867 static int swrap_msghdr_add_socket_info(struct socket_info *si,
3868                                         struct msghdr *omsg)
3869 {
3870         int rc = 0;
3871
3872         if (si->pktinfo > 0) {
3873                 rc = swrap_msghdr_add_pktinfo(si, omsg);
3874         }
3875
3876         return rc;
3877 }
3878
3879 static int swrap_sendmsg_copy_cmsg(struct cmsghdr *cmsg,
3880                                    uint8_t **cm_data,
3881                                    size_t *cm_data_space);
3882 static int swrap_sendmsg_filter_cmsg_socket(struct cmsghdr *cmsg,
3883                                             uint8_t **cm_data,
3884                                             size_t *cm_data_space);
3885
3886 static int swrap_sendmsg_filter_cmsghdr(struct msghdr *msg,
3887                                         uint8_t **cm_data,
3888                                         size_t *cm_data_space) {
3889         struct cmsghdr *cmsg;
3890         int rc = -1;
3891
3892         /* Nothing to do */
3893         if (msg->msg_controllen == 0 || msg->msg_control == NULL) {
3894                 return 0;
3895         }
3896
3897         for (cmsg = CMSG_FIRSTHDR(msg);
3898              cmsg != NULL;
3899              cmsg = CMSG_NXTHDR(msg, cmsg)) {
3900                 switch (cmsg->cmsg_level) {
3901                 case IPPROTO_IP:
3902                         rc = swrap_sendmsg_filter_cmsg_socket(cmsg,
3903                                                               cm_data,
3904                                                               cm_data_space);
3905                         break;
3906                 default:
3907                         rc = swrap_sendmsg_copy_cmsg(cmsg,
3908                                                      cm_data,
3909                                                      cm_data_space);
3910                         break;
3911                 }
3912         }
3913
3914         return rc;
3915 }
3916
3917 static int swrap_sendmsg_copy_cmsg(struct cmsghdr *cmsg,
3918                                    uint8_t **cm_data,
3919                                    size_t *cm_data_space)
3920 {
3921         size_t cmspace;
3922         uint8_t *p;
3923
3924         cmspace = *cm_data_space + CMSG_ALIGN(cmsg->cmsg_len);
3925
3926         p = realloc((*cm_data), cmspace);
3927         if (p == NULL) {
3928                 return -1;
3929         }
3930         (*cm_data) = p;
3931
3932         p = (*cm_data) + (*cm_data_space);
3933         *cm_data_space = cmspace;
3934
3935         memcpy(p, cmsg, cmsg->cmsg_len);
3936
3937         return 0;
3938 }
3939
3940 static int swrap_sendmsg_filter_cmsg_pktinfo(struct cmsghdr *cmsg,
3941                                             uint8_t **cm_data,
3942                                             size_t *cm_data_space);
3943
3944
3945 static int swrap_sendmsg_filter_cmsg_socket(struct cmsghdr *cmsg,
3946                                             uint8_t **cm_data,
3947                                             size_t *cm_data_space)
3948 {
3949         int rc = -1;
3950
3951         switch(cmsg->cmsg_type) {
3952 #ifdef IP_PKTINFO
3953         case IP_PKTINFO:
3954                 rc = swrap_sendmsg_filter_cmsg_pktinfo(cmsg,
3955                                                        cm_data,
3956                                                        cm_data_space);
3957                 break;
3958 #endif
3959 #ifdef IPV6_PKTINFO
3960         case IPV6_PKTINFO:
3961                 rc = swrap_sendmsg_filter_cmsg_pktinfo(cmsg,
3962                                                        cm_data,
3963                                                        cm_data_space);
3964                 break;
3965 #endif
3966         default:
3967                 break;
3968         }
3969
3970         return rc;
3971 }
3972
3973 static int swrap_sendmsg_filter_cmsg_pktinfo(struct cmsghdr *cmsg,
3974                                              uint8_t **cm_data,
3975                                              size_t *cm_data_space)
3976 {
3977         (void)cmsg; /* unused */
3978         (void)cm_data; /* unused */
3979         (void)cm_data_space; /* unused */
3980
3981         /*
3982          * Passing a IP pktinfo to a unix socket might be rejected by the
3983          * Kernel, at least on FreeBSD. So skip this cmsg.
3984          */
3985         return 0;
3986 }
3987 #endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
3988
3989 static ssize_t swrap_sendmsg_before(int fd,
3990                                     struct socket_info *si,
3991                                     struct msghdr *msg,
3992                                     struct iovec *tmp_iov,
3993                                     struct sockaddr_un *tmp_un,
3994                                     const struct sockaddr_un **to_un,
3995                                     const struct sockaddr **to,
3996                                     int *bcast)
3997 {
3998         size_t i, len = 0;
3999         ssize_t ret;
4000
4001         if (to_un) {
4002                 *to_un = NULL;
4003         }
4004         if (to) {
4005                 *to = NULL;
4006         }
4007         if (bcast) {
4008                 *bcast = 0;
4009         }
4010
4011         switch (si->type) {
4012         case SOCK_STREAM: {
4013                 unsigned long mtu;
4014
4015                 if (!si->connected) {
4016                         errno = ENOTCONN;
4017                         return -1;
4018                 }
4019
4020                 if (msg->msg_iovlen == 0) {
4021                         break;
4022                 }
4023
4024                 mtu = socket_wrapper_mtu();
4025                 for (i = 0; i < (size_t)msg->msg_iovlen; i++) {
4026                         size_t nlen;
4027                         nlen = len + msg->msg_iov[i].iov_len;
4028                         if (nlen > mtu) {
4029                                 break;
4030                         }
4031                 }
4032                 msg->msg_iovlen = i;
4033                 if (msg->msg_iovlen == 0) {
4034                         *tmp_iov = msg->msg_iov[0];
4035                         tmp_iov->iov_len = MIN((size_t)tmp_iov->iov_len,
4036                                                (size_t)mtu);
4037                         msg->msg_iov = tmp_iov;
4038                         msg->msg_iovlen = 1;
4039                 }
4040                 break;
4041         }
4042         case SOCK_DGRAM:
4043                 if (si->connected) {
4044                         if (msg->msg_name != NULL) {
4045                                 /*
4046                                  * We are dealing with unix sockets and if we
4047                                  * are connected, we should only talk to the
4048                                  * connected unix path. Using the fd to send
4049                                  * to another server would be hard to achieve.
4050                                  */
4051                                 msg->msg_name = NULL;
4052                                 msg->msg_namelen = 0;
4053                         }
4054                 } else {
4055                         const struct sockaddr *msg_name;
4056                         msg_name = (const struct sockaddr *)msg->msg_name;
4057
4058                         if (msg_name == NULL) {
4059                                 errno = ENOTCONN;
4060                                 return -1;
4061                         }
4062
4063
4064                         ret = sockaddr_convert_to_un(si, msg_name, msg->msg_namelen,
4065                                                      tmp_un, 0, bcast);
4066                         if (ret == -1) return -1;
4067
4068                         if (to_un) {
4069                                 *to_un = tmp_un;
4070                         }
4071                         if (to) {
4072                                 *to = msg_name;
4073                         }
4074                         msg->msg_name = tmp_un;
4075                         msg->msg_namelen = sizeof(*tmp_un);
4076                 }
4077
4078                 if (si->bound == 0) {
4079                         ret = swrap_auto_bind(fd, si, si->family);
4080                         if (ret == -1) {
4081                                 if (errno == ENOTSOCK) {
4082                                         swrap_remove_stale(fd);
4083                                         return -ENOTSOCK;
4084                                 } else {
4085                                         SWRAP_LOG(SWRAP_LOG_ERROR, "swrap_sendmsg_before failed");
4086                                         return -1;
4087                                 }
4088                         }
4089                 }
4090
4091                 if (!si->defer_connect) {
4092                         break;
4093                 }
4094
4095                 ret = sockaddr_convert_to_un(si,
4096                                              &si->peername.sa.s,
4097                                              si->peername.sa_socklen,
4098                                              tmp_un,
4099                                              0,
4100                                              NULL);
4101                 if (ret == -1) return -1;
4102
4103                 ret = libc_connect(fd,
4104                                    (struct sockaddr *)(void *)tmp_un,
4105                                    sizeof(*tmp_un));
4106
4107                 /* to give better errors */
4108                 if (ret == -1 && errno == ENOENT) {
4109                         errno = EHOSTUNREACH;
4110                 }
4111
4112                 if (ret == -1) {
4113                         return ret;
4114                 }
4115
4116                 si->defer_connect = 0;
4117                 break;
4118         default:
4119                 errno = EHOSTUNREACH;
4120                 return -1;
4121         }
4122
4123 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
4124         if (msg->msg_controllen > 0 && msg->msg_control != NULL) {
4125                 uint8_t *cmbuf = NULL;
4126                 size_t cmlen = 0;
4127
4128                 ret = swrap_sendmsg_filter_cmsghdr(msg, &cmbuf, &cmlen);
4129                 if (ret < 0) {
4130                         free(cmbuf);
4131                         return -1;
4132                 }
4133
4134                 if (cmlen == 0) {
4135                         msg->msg_controllen = 0;
4136                         msg->msg_control = NULL;
4137                 } else if (cmlen < msg->msg_controllen && cmbuf != NULL) {
4138                         memcpy(msg->msg_control, cmbuf, cmlen);
4139                         msg->msg_controllen = cmlen;
4140                 }
4141                 free(cmbuf);
4142         }
4143 #endif
4144
4145         return 0;
4146 }
4147
4148 static void swrap_sendmsg_after(int fd,
4149                                 struct socket_info *si,
4150                                 struct msghdr *msg,
4151                                 const struct sockaddr *to,
4152                                 ssize_t ret)
4153 {
4154         int saved_errno = errno;
4155         size_t i, len = 0;
4156         uint8_t *buf;
4157         off_t ofs = 0;
4158         size_t avail = 0;
4159         size_t remain;
4160
4161         /* to give better errors */
4162         if (ret == -1) {
4163                 if (saved_errno == ENOENT) {
4164                         saved_errno = EHOSTUNREACH;
4165                 } else if (saved_errno == ENOTSOCK) {
4166                         /* If the fd is not a socket, remove it */
4167                         swrap_remove_stale(fd);
4168                 }
4169         }
4170
4171         for (i = 0; i < (size_t)msg->msg_iovlen; i++) {
4172                 avail += msg->msg_iov[i].iov_len;
4173         }
4174
4175         if (ret == -1) {
4176                 remain = MIN(80, avail);
4177         } else {
4178                 remain = ret;
4179         }
4180
4181         /* we capture it as one single packet */
4182         buf = (uint8_t *)malloc(remain);
4183         if (!buf) {
4184                 /* we just not capture the packet */
4185                 errno = saved_errno;
4186                 return;
4187         }
4188
4189         for (i = 0; i < (size_t)msg->msg_iovlen; i++) {
4190                 size_t this_time = MIN(remain, (size_t)msg->msg_iov[i].iov_len);
4191                 memcpy(buf + ofs,
4192                        msg->msg_iov[i].iov_base,
4193                        this_time);
4194                 ofs += this_time;
4195                 remain -= this_time;
4196         }
4197         len = ofs;
4198
4199         switch (si->type) {
4200         case SOCK_STREAM:
4201                 if (ret == -1) {
4202                         swrap_pcap_dump_packet(si, NULL, SWRAP_SEND, buf, len);
4203                         swrap_pcap_dump_packet(si, NULL, SWRAP_SEND_RST, NULL, 0);
4204                 } else {
4205                         swrap_pcap_dump_packet(si, NULL, SWRAP_SEND, buf, len);
4206                 }
4207                 break;
4208
4209         case SOCK_DGRAM:
4210                 if (si->connected) {
4211                         to = &si->peername.sa.s;
4212                 }
4213                 if (ret == -1) {
4214                         swrap_pcap_dump_packet(si, to, SWRAP_SENDTO, buf, len);
4215                         swrap_pcap_dump_packet(si, to, SWRAP_SENDTO_UNREACH, buf, len);
4216                 } else {
4217                         swrap_pcap_dump_packet(si, to, SWRAP_SENDTO, buf, len);
4218                 }
4219                 break;
4220         }
4221
4222         free(buf);
4223         errno = saved_errno;
4224 }
4225
4226 static int swrap_recvmsg_before(int fd,
4227                                 struct socket_info *si,
4228                                 struct msghdr *msg,
4229                                 struct iovec *tmp_iov)
4230 {
4231         size_t i, len = 0;
4232         ssize_t ret;
4233
4234         (void)fd; /* unused */
4235
4236         switch (si->type) {
4237         case SOCK_STREAM: {
4238                 unsigned int mtu;
4239                 if (!si->connected) {
4240                         errno = ENOTCONN;
4241                         return -1;
4242                 }
4243
4244                 if (msg->msg_iovlen == 0) {
4245                         break;
4246                 }
4247
4248                 mtu = socket_wrapper_mtu();
4249                 for (i = 0; i < (size_t)msg->msg_iovlen; i++) {
4250                         size_t nlen;
4251                         nlen = len + msg->msg_iov[i].iov_len;
4252                         if (nlen > mtu) {
4253                                 break;
4254                         }
4255                 }
4256                 msg->msg_iovlen = i;
4257                 if (msg->msg_iovlen == 0) {
4258                         *tmp_iov = msg->msg_iov[0];
4259                         tmp_iov->iov_len = MIN((size_t)tmp_iov->iov_len,
4260                                                (size_t)mtu);
4261                         msg->msg_iov = tmp_iov;
4262                         msg->msg_iovlen = 1;
4263                 }
4264                 break;
4265         }
4266         case SOCK_DGRAM:
4267                 if (msg->msg_name == NULL) {
4268                         errno = EINVAL;
4269                         return -1;
4270                 }
4271
4272                 if (msg->msg_iovlen == 0) {
4273                         break;
4274                 }
4275
4276                 if (si->bound == 0) {
4277                         ret = swrap_auto_bind(fd, si, si->family);
4278                         if (ret == -1) {
4279                                 /*
4280                                  * When attempting to read or write to a
4281                                  * descriptor, if an underlying autobind fails
4282                                  * because it's not a socket, stop intercepting
4283                                  * uses of that descriptor.
4284                                  */
4285                                 if (errno == ENOTSOCK) {
4286                                         swrap_remove_stale(fd);
4287                                         return -ENOTSOCK;
4288                                 } else {
4289                                         SWRAP_LOG(SWRAP_LOG_ERROR,
4290                                                   "swrap_recvmsg_before failed");
4291                                         return -1;
4292                                 }
4293                         }
4294                 }
4295                 break;
4296         default:
4297                 errno = EHOSTUNREACH;
4298                 return -1;
4299         }
4300
4301         return 0;
4302 }
4303
4304 static int swrap_recvmsg_after(int fd,
4305                                struct socket_info *si,
4306                                struct msghdr *msg,
4307                                const struct sockaddr_un *un_addr,
4308                                socklen_t un_addrlen,
4309                                ssize_t ret)
4310 {
4311         int saved_errno = errno;
4312         size_t i;
4313         uint8_t *buf = NULL;
4314         off_t ofs = 0;
4315         size_t avail = 0;
4316         size_t remain;
4317         int rc;
4318
4319         /* to give better errors */
4320         if (ret == -1) {
4321                 if (saved_errno == ENOENT) {
4322                         saved_errno = EHOSTUNREACH;
4323                 } else if (saved_errno == ENOTSOCK) {
4324                         /* If the fd is not a socket, remove it */
4325                         swrap_remove_stale(fd);
4326                 }
4327         }
4328
4329         for (i = 0; i < (size_t)msg->msg_iovlen; i++) {
4330                 avail += msg->msg_iov[i].iov_len;
4331         }
4332
4333         /* Convert the socket address before we leave */
4334         if (si->type == SOCK_DGRAM && un_addr != NULL) {
4335                 rc = sockaddr_convert_from_un(si,
4336                                               un_addr,
4337                                               un_addrlen,
4338                                               si->family,
4339                                               msg->msg_name,
4340                                               &msg->msg_namelen);
4341                 if (rc == -1) {
4342                         goto done;
4343                 }
4344         }
4345
4346         if (avail == 0) {
4347                 rc = 0;
4348                 goto done;
4349         }
4350
4351         if (ret == -1) {
4352                 remain = MIN(80, avail);
4353         } else {
4354                 remain = ret;
4355         }
4356
4357         /* we capture it as one single packet */
4358         buf = (uint8_t *)malloc(remain);
4359         if (buf == NULL) {
4360                 /* we just not capture the packet */
4361                 errno = saved_errno;
4362                 return -1;
4363         }
4364
4365         for (i = 0; i < (size_t)msg->msg_iovlen; i++) {
4366                 size_t this_time = MIN(remain, (size_t)msg->msg_iov[i].iov_len);
4367                 memcpy(buf + ofs,
4368                        msg->msg_iov[i].iov_base,
4369                        this_time);
4370                 ofs += this_time;
4371                 remain -= this_time;
4372         }
4373
4374         switch (si->type) {
4375         case SOCK_STREAM:
4376                 if (ret == -1 && saved_errno != EAGAIN && saved_errno != ENOBUFS) {
4377                         swrap_pcap_dump_packet(si, NULL, SWRAP_RECV_RST, NULL, 0);
4378                 } else if (ret == 0) { /* END OF FILE */
4379                         swrap_pcap_dump_packet(si, NULL, SWRAP_RECV_RST, NULL, 0);
4380                 } else if (ret > 0) {
4381                         swrap_pcap_dump_packet(si, NULL, SWRAP_RECV, buf, ret);
4382                 }
4383                 break;
4384
4385         case SOCK_DGRAM:
4386                 if (ret == -1) {
4387                         break;
4388                 }
4389
4390                 if (un_addr != NULL) {
4391                         swrap_pcap_dump_packet(si,
4392                                           msg->msg_name,
4393                                           SWRAP_RECVFROM,
4394                                           buf,
4395                                           ret);
4396                 } else {
4397                         swrap_pcap_dump_packet(si,
4398                                           msg->msg_name,
4399                                           SWRAP_RECV,
4400                                           buf,
4401                                           ret);
4402                 }
4403
4404                 break;
4405         }
4406
4407         rc = 0;
4408 done:
4409         free(buf);
4410         errno = saved_errno;
4411
4412 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
4413         if (rc == 0 &&
4414             msg->msg_controllen > 0 &&
4415             msg->msg_control != NULL) {
4416                 rc = swrap_msghdr_add_socket_info(si, msg);
4417                 if (rc < 0) {
4418                         return -1;
4419                 }
4420         }
4421 #endif
4422
4423         return rc;
4424 }
4425
4426 /****************************************************************************
4427  *   RECVFROM
4428  ***************************************************************************/
4429
4430 static ssize_t swrap_recvfrom(int s, void *buf, size_t len, int flags,
4431                               struct sockaddr *from, socklen_t *fromlen)
4432 {
4433         struct swrap_address from_addr = {
4434                 .sa_socklen = sizeof(struct sockaddr_un),
4435         };
4436         ssize_t ret;
4437         struct socket_info *si = find_socket_info(s);
4438         struct swrap_address saddr = {
4439                 .sa_socklen = sizeof(struct sockaddr_storage),
4440         };
4441         struct msghdr msg;
4442         struct iovec tmp;
4443         int tret;
4444
4445         if (!si) {
4446                 return libc_recvfrom(s,
4447                                      buf,
4448                                      len,
4449                                      flags,
4450                                      from,
4451                                      fromlen);
4452         }
4453
4454         tmp.iov_base = buf;
4455         tmp.iov_len = len;
4456
4457         ZERO_STRUCT(msg);
4458         if (from != NULL && fromlen != NULL) {
4459                 msg.msg_name = from;   /* optional address */
4460                 msg.msg_namelen = *fromlen; /* size of address */
4461         } else {
4462                 msg.msg_name = &saddr.sa.s; /* optional address */
4463                 msg.msg_namelen = saddr.sa_socklen; /* size of address */
4464         }
4465         msg.msg_iov = &tmp;            /* scatter/gather array */
4466         msg.msg_iovlen = 1;            /* # elements in msg_iov */
4467 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
4468         msg.msg_control = NULL;        /* ancillary data, see below */
4469         msg.msg_controllen = 0;        /* ancillary data buffer len */
4470         msg.msg_flags = 0;             /* flags on received message */
4471 #endif
4472
4473         tret = swrap_recvmsg_before(s, si, &msg, &tmp);
4474         if (tret < 0) {
4475                 return -1;
4476         }
4477
4478         buf = msg.msg_iov[0].iov_base;
4479         len = msg.msg_iov[0].iov_len;
4480
4481         ret = libc_recvfrom(s,
4482                             buf,
4483                             len,
4484                             flags,
4485                             &from_addr.sa.s,
4486                             &from_addr.sa_socklen);
4487         if (ret == -1) {
4488                 return ret;
4489         }
4490
4491         tret = swrap_recvmsg_after(s,
4492                                    si,
4493                                    &msg,
4494                                    &from_addr.sa.un,
4495                                    from_addr.sa_socklen,
4496                                    ret);
4497         if (tret != 0) {
4498                 return tret;
4499         }
4500
4501         if (from != NULL && fromlen != NULL) {
4502                 *fromlen = msg.msg_namelen;
4503         }
4504
4505         return ret;
4506 }
4507
4508 #ifdef HAVE_ACCEPT_PSOCKLEN_T
4509 ssize_t recvfrom(int s, void *buf, size_t len, int flags,
4510                  struct sockaddr *from, Psocklen_t fromlen)
4511 #else
4512 ssize_t recvfrom(int s, void *buf, size_t len, int flags,
4513                  struct sockaddr *from, socklen_t *fromlen)
4514 #endif
4515 {
4516         return swrap_recvfrom(s, buf, len, flags, from, (socklen_t *)fromlen);
4517 }
4518
4519 /****************************************************************************
4520  *   SENDTO
4521  ***************************************************************************/
4522
4523 static ssize_t swrap_sendto(int s, const void *buf, size_t len, int flags,
4524                             const struct sockaddr *to, socklen_t tolen)
4525 {
4526         struct msghdr msg;
4527         struct iovec tmp;
4528         struct swrap_address un_addr = {
4529                 .sa_socklen = sizeof(struct sockaddr_un),
4530         };
4531         const struct sockaddr_un *to_un = NULL;
4532         ssize_t ret;
4533         int rc;
4534         struct socket_info *si = find_socket_info(s);
4535         int bcast = 0;
4536
4537         if (!si) {
4538                 return libc_sendto(s, buf, len, flags, to, tolen);
4539         }
4540
4541         tmp.iov_base = discard_const_p(char, buf);
4542         tmp.iov_len = len;
4543
4544         ZERO_STRUCT(msg);
4545         msg.msg_name = discard_const_p(struct sockaddr, to); /* optional address */
4546         msg.msg_namelen = tolen;       /* size of address */
4547         msg.msg_iov = &tmp;            /* scatter/gather array */
4548         msg.msg_iovlen = 1;            /* # elements in msg_iov */
4549 #if HAVE_STRUCT_MSGHDR_MSG_CONTROL
4550         msg.msg_control = NULL;        /* ancillary data, see below */
4551         msg.msg_controllen = 0;        /* ancillary data buffer len */
4552         msg.msg_flags = 0;             /* flags on received message */
4553 #endif
4554
4555         rc = swrap_sendmsg_before(s,
4556                                   si,
4557                                   &msg,
4558                                   &tmp,
4559                                   &un_addr.sa.un,
4560                                   &to_un,
4561                                   &to,
4562                                   &bcast);
4563         if (rc < 0) {
4564                 return -1;
4565         }
4566
4567         buf = msg.msg_iov[0].iov_base;
4568         len = msg.msg_iov[0].iov_len;
4569
4570         if (bcast) {
4571                 struct stat st;
4572                 unsigned int iface;
4573                 unsigned int prt = ntohs(((const struct sockaddr_in *)(const void *)to)->sin_port);
4574                 char type;
4575
4576                 type = SOCKET_TYPE_CHAR_UDP;
4577
4578                 for(iface=0; iface <= MAX_WRAPPED_INTERFACES; iface++) {
4579                         snprintf(un_addr.sa.un.sun_path,
4580                                  sizeof(un_addr.sa.un.sun_path),
4581                                  "%s/"SOCKET_FORMAT,
4582                                  socket_wrapper_dir(), type, iface, prt);
4583                         if (stat(un_addr.sa.un.sun_path, &st) != 0) continue;
4584
4585                         /* ignore the any errors in broadcast sends */
4586                         libc_sendto(s,
4587                                     buf,
4588                                     len,
4589                                     flags,
4590                                     &un_addr.sa.s,
4591                                     un_addr.sa_socklen);
4592                 }
4593
4594                 swrap_pcap_dump_packet(si, to, SWRAP_SENDTO, buf, len);
4595
4596                 return len;
4597         }
4598
4599         /*
4600          * If it is a dgram socket and we are connected, don't include the
4601          * 'to' address.
4602          */
4603         if (si->type == SOCK_DGRAM && si->connected) {
4604                 ret = libc_sendto(s,
4605                                   buf,
4606                                   len,
4607                                   flags,
4608                                   NULL,
4609                                   0);
4610         } else {
4611                 ret = libc_sendto(s,
4612                                   buf,
4613                                   len,
4614                                   flags,
4615                                   (struct sockaddr *)msg.msg_name,
4616                                   msg.msg_namelen);
4617         }
4618
4619         swrap_sendmsg_after(s, si, &msg, to, ret);
4620
4621         return ret;
4622 }
4623
4624 ssize_t sendto(int s, const void *buf, size_t len, int flags,
4625                const struct sockaddr *to, socklen_t tolen)
4626 {
4627         return swrap_sendto(s, buf, len, flags, to, tolen);
4628 }
4629
4630 /****************************************************************************
4631  *   READV
4632  ***************************************************************************/
4633
4634 static ssize_t swrap_recv(int s, void *buf, size_t len, int flags)
4635 {
4636         struct socket_info *si;
4637         struct msghdr msg;
4638         struct swrap_address saddr = {
4639                 .sa_socklen = sizeof(struct sockaddr_storage),
4640         };
4641         struct iovec tmp;
4642         ssize_t ret;
4643         int tret;
4644
4645         si = find_socket_info(s);
4646         if (si == NULL) {
4647                 return libc_recv(s, buf, len, flags);
4648         }
4649
4650         tmp.iov_base = buf;
4651         tmp.iov_len = len;
4652
4653         ZERO_STRUCT(msg);
4654         msg.msg_name = &saddr.sa.s;    /* optional address */
4655         msg.msg_namelen = saddr.sa_socklen; /* size of address */
4656         msg.msg_iov = &tmp;            /* scatter/gather array */
4657         msg.msg_iovlen = 1;            /* # elements in msg_iov */
4658 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
4659         msg.msg_control = NULL;        /* ancillary data, see below */
4660         msg.msg_controllen = 0;        /* ancillary data buffer len */
4661         msg.msg_flags = 0;             /* flags on received message */
4662 #endif
4663
4664         tret = swrap_recvmsg_before(s, si, &msg, &tmp);
4665         if (tret < 0) {
4666                 return -1;
4667         }
4668
4669         buf = msg.msg_iov[0].iov_base;
4670         len = msg.msg_iov[0].iov_len;
4671
4672         ret = libc_recv(s, buf, len, flags);
4673
4674         tret = swrap_recvmsg_after(s, si, &msg, NULL, 0, ret);
4675         if (tret != 0) {
4676                 return tret;
4677         }
4678
4679         return ret;
4680 }
4681
4682 ssize_t recv(int s, void *buf, size_t len, int flags)
4683 {
4684         return swrap_recv(s, buf, len, flags);
4685 }
4686
4687 /****************************************************************************
4688  *   READ
4689  ***************************************************************************/
4690
4691 static ssize_t swrap_read(int s, void *buf, size_t len)
4692 {
4693         struct socket_info *si;
4694         struct msghdr msg;
4695         struct iovec tmp;
4696         struct swrap_address saddr = {
4697                 .sa_socklen = sizeof(struct sockaddr_storage),
4698         };
4699         ssize_t ret;
4700         int tret;
4701
4702         si = find_socket_info(s);
4703         if (si == NULL) {
4704                 return libc_read(s, buf, len);
4705         }
4706
4707         tmp.iov_base = buf;
4708         tmp.iov_len = len;
4709
4710         ZERO_STRUCT(msg);
4711         msg.msg_name = &saddr.sa.ss;   /* optional address */
4712         msg.msg_namelen = saddr.sa_socklen; /* size of address */
4713         msg.msg_iov = &tmp;            /* scatter/gather array */
4714         msg.msg_iovlen = 1;            /* # elements in msg_iov */
4715 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
4716         msg.msg_control = NULL;        /* ancillary data, see below */
4717         msg.msg_controllen = 0;        /* ancillary data buffer len */
4718         msg.msg_flags = 0;             /* flags on received message */
4719 #endif
4720
4721         tret = swrap_recvmsg_before(s, si, &msg, &tmp);
4722         if (tret < 0) {
4723                 if (tret == -ENOTSOCK) {
4724                         return libc_read(s, buf, len);
4725                 }
4726                 return -1;
4727         }
4728
4729         buf = msg.msg_iov[0].iov_base;
4730         len = msg.msg_iov[0].iov_len;
4731
4732         ret = libc_read(s, buf, len);
4733
4734         tret = swrap_recvmsg_after(s, si, &msg, NULL, 0, ret);
4735         if (tret != 0) {
4736                 return tret;
4737         }
4738
4739         return ret;
4740 }
4741
4742 ssize_t read(int s, void *buf, size_t len)
4743 {
4744         return swrap_read(s, buf, len);
4745 }
4746
4747 /****************************************************************************
4748  *   WRITE
4749  ***************************************************************************/
4750
4751 static ssize_t swrap_write(int s, const void *buf, size_t len)
4752 {
4753         struct msghdr msg;
4754         struct iovec tmp;
4755         struct sockaddr_un un_addr;
4756         ssize_t ret;
4757         int rc;
4758         struct socket_info *si;
4759
4760         si = find_socket_info(s);
4761         if (si == NULL) {
4762                 return libc_write(s, buf, len);
4763         }
4764
4765         tmp.iov_base = discard_const_p(char, buf);
4766         tmp.iov_len = len;
4767
4768         ZERO_STRUCT(msg);
4769         msg.msg_name = NULL;           /* optional address */
4770         msg.msg_namelen = 0;           /* size of address */
4771         msg.msg_iov = &tmp;            /* scatter/gather array */
4772         msg.msg_iovlen = 1;            /* # elements in msg_iov */
4773 #if HAVE_STRUCT_MSGHDR_MSG_CONTROL
4774         msg.msg_control = NULL;        /* ancillary data, see below */
4775         msg.msg_controllen = 0;        /* ancillary data buffer len */
4776         msg.msg_flags = 0;             /* flags on received message */
4777 #endif
4778
4779         rc = swrap_sendmsg_before(s, si, &msg, &tmp, &un_addr, NULL, NULL, NULL);
4780         if (rc < 0) {
4781                 return -1;
4782         }
4783
4784         buf = msg.msg_iov[0].iov_base;
4785         len = msg.msg_iov[0].iov_len;
4786
4787         ret = libc_write(s, buf, len);
4788
4789         swrap_sendmsg_after(s, si, &msg, NULL, ret);
4790
4791         return ret;
4792 }
4793
4794 ssize_t write(int s, const void *buf, size_t len)
4795 {
4796         return swrap_write(s, buf, len);
4797 }
4798
4799 /****************************************************************************
4800  *   SEND
4801  ***************************************************************************/
4802
4803 static ssize_t swrap_send(int s, const void *buf, size_t len, int flags)
4804 {
4805         struct msghdr msg;
4806         struct iovec tmp;
4807         struct sockaddr_un un_addr;
4808         ssize_t ret;
4809         int rc;
4810         struct socket_info *si = find_socket_info(s);
4811
4812         if (!si) {
4813                 return libc_send(s, buf, len, flags);
4814         }
4815
4816         tmp.iov_base = discard_const_p(char, buf);
4817         tmp.iov_len = len;
4818
4819         ZERO_STRUCT(msg);
4820         msg.msg_name = NULL;           /* optional address */
4821         msg.msg_namelen = 0;           /* size of address */
4822         msg.msg_iov = &tmp;            /* scatter/gather array */
4823         msg.msg_iovlen = 1;            /* # elements in msg_iov */
4824 #if HAVE_STRUCT_MSGHDR_MSG_CONTROL
4825         msg.msg_control = NULL;        /* ancillary data, see below */
4826         msg.msg_controllen = 0;        /* ancillary data buffer len */
4827         msg.msg_flags = 0;             /* flags on received message */
4828 #endif
4829
4830         rc = swrap_sendmsg_before(s, si, &msg, &tmp, &un_addr, NULL, NULL, NULL);
4831         if (rc < 0) {
4832                 return -1;
4833         }
4834
4835         buf = msg.msg_iov[0].iov_base;
4836         len = msg.msg_iov[0].iov_len;
4837
4838         ret = libc_send(s, buf, len, flags);
4839
4840         swrap_sendmsg_after(s, si, &msg, NULL, ret);
4841
4842         return ret;
4843 }
4844
4845 ssize_t send(int s, const void *buf, size_t len, int flags)
4846 {
4847         return swrap_send(s, buf, len, flags);
4848 }
4849
4850 /****************************************************************************
4851  *   RECVMSG
4852  ***************************************************************************/
4853
4854 static ssize_t swrap_recvmsg(int s, struct msghdr *omsg, int flags)
4855 {
4856         struct swrap_address from_addr = {
4857                 .sa_socklen = sizeof(struct sockaddr_un),
4858         };
4859         struct swrap_address convert_addr = {
4860                 .sa_socklen = sizeof(struct sockaddr_storage),
4861         };
4862         struct socket_info *si;
4863         struct msghdr msg;
4864         struct iovec tmp;
4865 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
4866         size_t msg_ctrllen_filled;
4867         size_t msg_ctrllen_left;
4868 #endif
4869
4870         ssize_t ret;
4871         int rc;
4872
4873         si = find_socket_info(s);
4874         if (si == NULL) {
4875                 return libc_recvmsg(s, omsg, flags);
4876         }
4877
4878         tmp.iov_base = NULL;
4879         tmp.iov_len = 0;
4880
4881         ZERO_STRUCT(msg);
4882         msg.msg_name = &from_addr.sa;              /* optional address */
4883         msg.msg_namelen = from_addr.sa_socklen;    /* size of address */
4884         msg.msg_iov = omsg->msg_iov;               /* scatter/gather array */
4885         msg.msg_iovlen = omsg->msg_iovlen;         /* # elements in msg_iov */
4886 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
4887         msg_ctrllen_filled = 0;
4888         msg_ctrllen_left = omsg->msg_controllen;
4889
4890         msg.msg_control = omsg->msg_control;       /* ancillary data, see below */
4891         msg.msg_controllen = omsg->msg_controllen; /* ancillary data buffer len */
4892         msg.msg_flags = omsg->msg_flags;           /* flags on received message */
4893 #endif
4894
4895         rc = swrap_recvmsg_before(s, si, &msg, &tmp);
4896         if (rc < 0) {
4897                 return -1;
4898         }
4899
4900         ret = libc_recvmsg(s, &msg, flags);
4901
4902 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
4903         msg_ctrllen_filled += msg.msg_controllen;
4904         msg_ctrllen_left -= msg.msg_controllen;
4905
4906         if (omsg->msg_control != NULL) {
4907                 uint8_t *p;
4908
4909                 p = omsg->msg_control;
4910                 p += msg_ctrllen_filled;
4911
4912                 msg.msg_control = p;
4913                 msg.msg_controllen = msg_ctrllen_left;
4914         } else {
4915                 msg.msg_control = NULL;
4916                 msg.msg_controllen = 0;
4917         }
4918 #endif
4919
4920         /*
4921          * We convert the unix address to a IP address so we need a buffer
4922          * which can store the address in case of SOCK_DGRAM, see below.
4923          */
4924         msg.msg_name = &convert_addr.sa;
4925         msg.msg_namelen = convert_addr.sa_socklen;
4926
4927         rc = swrap_recvmsg_after(s,
4928                                  si,
4929                                  &msg,
4930                                  &from_addr.sa.un,
4931                                  from_addr.sa_socklen,
4932                                  ret);
4933         if (rc != 0) {
4934                 return rc;
4935         }
4936
4937 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
4938         if (omsg->msg_control != NULL) {
4939                 /* msg.msg_controllen = space left */
4940                 msg_ctrllen_left = msg.msg_controllen;
4941                 msg_ctrllen_filled = omsg->msg_controllen - msg_ctrllen_left;
4942         }
4943
4944         /* Update the original message length */
4945         omsg->msg_controllen = msg_ctrllen_filled;
4946         omsg->msg_flags = msg.msg_flags;
4947 #endif
4948         omsg->msg_iovlen = msg.msg_iovlen;
4949
4950         /*
4951          * From the manpage:
4952          *
4953          * The  msg_name  field  points  to a caller-allocated buffer that is
4954          * used to return the source address if the socket is unconnected.  The
4955          * caller should set msg_namelen to the size of this buffer before this
4956          * call; upon return from a successful call, msg_name will contain the
4957          * length of the returned address.  If the application  does  not  need
4958          * to know the source address, msg_name can be specified as NULL.
4959          */
4960         if (si->type == SOCK_STREAM) {
4961                 omsg->msg_namelen = 0;
4962         } else if (omsg->msg_name != NULL &&
4963                    omsg->msg_namelen != 0 &&
4964                    omsg->msg_namelen >= msg.msg_namelen) {
4965                 memcpy(omsg->msg_name, msg.msg_name, msg.msg_namelen);
4966                 omsg->msg_namelen = msg.msg_namelen;
4967         }
4968
4969         return ret;
4970 }
4971
4972 ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags)
4973 {
4974         return swrap_recvmsg(sockfd, msg, flags);
4975 }
4976
4977 /****************************************************************************
4978  *   SENDMSG
4979  ***************************************************************************/
4980
4981 static ssize_t swrap_sendmsg(int s, const struct msghdr *omsg, int flags)
4982 {
4983         struct msghdr msg;
4984         struct iovec tmp;
4985         struct sockaddr_un un_addr;
4986         const struct sockaddr_un *to_un = NULL;
4987         const struct sockaddr *to = NULL;
4988         ssize_t ret;
4989         int rc;
4990         struct socket_info *si = find_socket_info(s);
4991         int bcast = 0;
4992
4993         if (!si) {
4994                 return libc_sendmsg(s, omsg, flags);
4995         }
4996
4997         ZERO_STRUCT(un_addr);
4998
4999         tmp.iov_base = NULL;
5000         tmp.iov_len = 0;
5001
5002         ZERO_STRUCT(msg);
5003
5004         if (si->connected == 0) {
5005                 msg.msg_name = omsg->msg_name;             /* optional address */
5006                 msg.msg_namelen = omsg->msg_namelen;       /* size of address */
5007         }
5008         msg.msg_iov = omsg->msg_iov;               /* scatter/gather array */
5009         msg.msg_iovlen = omsg->msg_iovlen;         /* # elements in msg_iov */
5010 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
5011         if (msg.msg_controllen > 0 && msg.msg_control != NULL) {
5012                 /* omsg is a const so use a local buffer for modifications */
5013                 uint8_t cmbuf[omsg->msg_controllen];
5014
5015                 memcpy(cmbuf, omsg->msg_control, omsg->msg_controllen);
5016
5017                 msg.msg_control = cmbuf;       /* ancillary data, see below */
5018                 msg.msg_controllen = omsg->msg_controllen; /* ancillary data buffer len */
5019         }
5020         msg.msg_flags = omsg->msg_flags;           /* flags on received message */
5021 #endif
5022
5023         rc = swrap_sendmsg_before(s, si, &msg, &tmp, &un_addr, &to_un, &to, &bcast);
5024         if (rc < 0) {
5025                 return -1;
5026         }
5027
5028         if (bcast) {
5029                 struct stat st;
5030                 unsigned int iface;
5031                 unsigned int prt = ntohs(((const struct sockaddr_in *)(const void *)to)->sin_port);
5032                 char type;
5033                 size_t i, len = 0;
5034                 uint8_t *buf;
5035                 off_t ofs = 0;
5036                 size_t avail = 0;
5037                 size_t remain;
5038
5039                 for (i = 0; i < (size_t)msg.msg_iovlen; i++) {
5040                         avail += msg.msg_iov[i].iov_len;
5041                 }
5042
5043                 len = avail;
5044                 remain = avail;
5045
5046                 /* we capture it as one single packet */
5047                 buf = (uint8_t *)malloc(remain);
5048                 if (!buf) {
5049                         return -1;
5050                 }
5051
5052                 for (i = 0; i < (size_t)msg.msg_iovlen; i++) {
5053                         size_t this_time = MIN(remain, (size_t)msg.msg_iov[i].iov_len);
5054                         memcpy(buf + ofs,
5055                                msg.msg_iov[i].iov_base,
5056                                this_time);
5057                         ofs += this_time;
5058                         remain -= this_time;
5059                 }
5060
5061                 type = SOCKET_TYPE_CHAR_UDP;
5062
5063                 for(iface=0; iface <= MAX_WRAPPED_INTERFACES; iface++) {
5064                         snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), "%s/"SOCKET_FORMAT,
5065                                  socket_wrapper_dir(), type, iface, prt);
5066                         if (stat(un_addr.sun_path, &st) != 0) continue;
5067
5068                         msg.msg_name = &un_addr;           /* optional address */
5069                         msg.msg_namelen = sizeof(un_addr); /* size of address */
5070
5071                         /* ignore the any errors in broadcast sends */
5072                         libc_sendmsg(s, &msg, flags);
5073                 }
5074
5075                 swrap_pcap_dump_packet(si, to, SWRAP_SENDTO, buf, len);
5076                 free(buf);
5077
5078                 return len;
5079         }
5080
5081         ret = libc_sendmsg(s, &msg, flags);
5082
5083         swrap_sendmsg_after(s, si, &msg, to, ret);
5084
5085         return ret;
5086 }
5087
5088 ssize_t sendmsg(int s, const struct msghdr *omsg, int flags)
5089 {
5090         return swrap_sendmsg(s, omsg, flags);
5091 }
5092
5093 /****************************************************************************
5094  *   READV
5095  ***************************************************************************/
5096
5097 static ssize_t swrap_readv(int s, const struct iovec *vector, int count)
5098 {
5099         struct socket_info *si;
5100         struct msghdr msg;
5101         struct iovec tmp;
5102         struct swrap_address saddr = {
5103                 .sa_socklen = sizeof(struct sockaddr_storage)
5104         };
5105         ssize_t ret;
5106         int rc;
5107
5108         si = find_socket_info(s);
5109         if (si == NULL) {
5110                 return libc_readv(s, vector, count);
5111         }
5112
5113         tmp.iov_base = NULL;
5114         tmp.iov_len = 0;
5115
5116         ZERO_STRUCT(msg);
5117         msg.msg_name = &saddr.sa.s; /* optional address */
5118         msg.msg_namelen = saddr.sa_socklen;      /* size of address */
5119         msg.msg_iov = discard_const_p(struct iovec, vector); /* scatter/gather array */
5120         msg.msg_iovlen = count;        /* # elements in msg_iov */
5121 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
5122         msg.msg_control = NULL;        /* ancillary data, see below */
5123         msg.msg_controllen = 0;        /* ancillary data buffer len */
5124         msg.msg_flags = 0;             /* flags on received message */
5125 #endif
5126
5127         rc = swrap_recvmsg_before(s, si, &msg, &tmp);
5128         if (rc < 0) {
5129                 if (rc == -ENOTSOCK) {
5130                         return libc_readv(s, vector, count);
5131                 }
5132                 return -1;
5133         }
5134
5135         ret = libc_readv(s, msg.msg_iov, msg.msg_iovlen);
5136
5137         rc = swrap_recvmsg_after(s, si, &msg, NULL, 0, ret);
5138         if (rc != 0) {
5139                 return rc;
5140         }
5141
5142         return ret;
5143 }
5144
5145 ssize_t readv(int s, const struct iovec *vector, int count)
5146 {
5147         return swrap_readv(s, vector, count);
5148 }
5149
5150 /****************************************************************************
5151  *   WRITEV
5152  ***************************************************************************/
5153
5154 static ssize_t swrap_writev(int s, const struct iovec *vector, int count)
5155 {
5156         struct msghdr msg;
5157         struct iovec tmp;
5158         struct sockaddr_un un_addr;
5159         ssize_t ret;
5160         int rc;
5161         struct socket_info *si = find_socket_info(s);
5162
5163         if (!si) {
5164                 return libc_writev(s, vector, count);
5165         }
5166
5167         tmp.iov_base = NULL;
5168         tmp.iov_len = 0;
5169
5170         ZERO_STRUCT(msg);
5171         msg.msg_name = NULL;           /* optional address */
5172         msg.msg_namelen = 0;           /* size of address */
5173         msg.msg_iov = discard_const_p(struct iovec, vector); /* scatter/gather array */
5174         msg.msg_iovlen = count;        /* # elements in msg_iov */
5175 #if HAVE_STRUCT_MSGHDR_MSG_CONTROL
5176         msg.msg_control = NULL;        /* ancillary data, see below */
5177         msg.msg_controllen = 0;        /* ancillary data buffer len */
5178         msg.msg_flags = 0;             /* flags on received message */
5179 #endif
5180
5181         rc = swrap_sendmsg_before(s, si, &msg, &tmp, &un_addr, NULL, NULL, NULL);
5182         if (rc < 0) {
5183                 if (rc == -ENOTSOCK) {
5184                         return libc_readv(s, vector, count);
5185                 }
5186                 return -1;
5187         }
5188
5189         ret = libc_writev(s, msg.msg_iov, msg.msg_iovlen);
5190
5191         swrap_sendmsg_after(s, si, &msg, NULL, ret);
5192
5193         return ret;
5194 }
5195
5196 ssize_t writev(int s, const struct iovec *vector, int count)
5197 {
5198         return swrap_writev(s, vector, count);
5199 }
5200
5201 /****************************
5202  * CLOSE
5203  ***************************/
5204
5205 static int swrap_close(int fd)
5206 {
5207         struct socket_info_fd *fi = find_socket_info_fd(fd);
5208         struct socket_info *si = NULL;
5209         int ret;
5210
5211         if (fi == NULL) {
5212                 return libc_close(fd);
5213         }
5214
5215         si = &sockets[fi->si_index];
5216
5217         SWRAP_DLIST_REMOVE(socket_fds, fi);
5218         free(fi);
5219
5220         si->refcount--;
5221
5222         if (si->refcount > 0) {
5223                 /* there are still references left */
5224                 return libc_close(fd);
5225         }
5226
5227         if (si->myname.sa_socklen > 0 && si->peername.sa_socklen > 0) {
5228                 swrap_pcap_dump_packet(si, NULL, SWRAP_CLOSE_SEND, NULL, 0);
5229         }
5230
5231         ret = libc_close(fd);
5232
5233         if (si->myname.sa_socklen > 0 && si->peername.sa_socklen > 0) {
5234                 swrap_pcap_dump_packet(si, NULL, SWRAP_CLOSE_RECV, NULL, 0);
5235                 swrap_pcap_dump_packet(si, NULL, SWRAP_CLOSE_ACK, NULL, 0);
5236         }
5237
5238         if (si->un_addr.sun_path[0] != '\0') {
5239                 unlink(si->un_addr.sun_path);
5240         }
5241
5242         return ret;
5243 }
5244
5245 int close(int fd)
5246 {
5247         return swrap_close(fd);
5248 }
5249
5250 /****************************
5251  * DUP
5252  ***************************/
5253
5254 static int swrap_dup(int fd)
5255 {
5256         struct socket_info *si;
5257         struct socket_info_fd *src_fi, *fi;
5258
5259         src_fi = find_socket_info_fd(fd);
5260         if (src_fi == NULL) {
5261                 return libc_dup(fd);
5262         }
5263
5264         si = &sockets[src_fi->si_index];
5265
5266         fi = (struct socket_info_fd *)calloc(1, sizeof(struct socket_info_fd));
5267         if (fi == NULL) {
5268                 errno = ENOMEM;
5269                 return -1;
5270         }
5271
5272         fi->fd = libc_dup(fd);
5273         if (fi->fd == -1) {
5274                 int saved_errno = errno;
5275                 free(fi);
5276                 errno = saved_errno;
5277                 return -1;
5278         }
5279
5280         si->refcount++;
5281         fi->si_index = src_fi->si_index;
5282
5283         /* Make sure we don't have an entry for the fd */
5284         swrap_remove_stale(fi->fd);
5285
5286         SWRAP_DLIST_ADD_AFTER(socket_fds, fi, src_fi);
5287         return fi->fd;
5288 }
5289
5290 int dup(int fd)
5291 {
5292         return swrap_dup(fd);
5293 }
5294
5295 /****************************
5296  * DUP2
5297  ***************************/
5298
5299 static int swrap_dup2(int fd, int newfd)
5300 {
5301         struct socket_info *si;
5302         struct socket_info_fd *src_fi, *fi;
5303
5304         src_fi = find_socket_info_fd(fd);
5305         if (src_fi == NULL) {
5306                 return libc_dup2(fd, newfd);
5307         }
5308
5309         si = &sockets[src_fi->si_index];
5310
5311         if (fd == newfd) {
5312                 /*
5313                  * According to the manpage:
5314                  *
5315                  * "If oldfd is a valid file descriptor, and newfd has the same
5316                  * value as oldfd, then dup2() does nothing, and returns newfd."
5317                  */
5318                 return newfd;
5319         }
5320
5321         if (find_socket_info(newfd)) {
5322                 /* dup2() does an implicit close of newfd, which we
5323                  * need to emulate */
5324                 swrap_close(newfd);
5325         }
5326
5327         fi = (struct socket_info_fd *)calloc(1, sizeof(struct socket_info_fd));
5328         if (fi == NULL) {
5329                 errno = ENOMEM;
5330                 return -1;
5331         }
5332
5333         fi->fd = libc_dup2(fd, newfd);
5334         if (fi->fd == -1) {
5335                 int saved_errno = errno;
5336                 free(fi);
5337                 errno = saved_errno;
5338                 return -1;
5339         }
5340
5341         si->refcount++;
5342         fi->si_index = src_fi->si_index;
5343
5344         /* Make sure we don't have an entry for the fd */
5345         swrap_remove_stale(fi->fd);
5346
5347         SWRAP_DLIST_ADD_AFTER(socket_fds, fi, src_fi);
5348         return fi->fd;
5349 }
5350
5351 int dup2(int fd, int newfd)
5352 {
5353         return swrap_dup2(fd, newfd);
5354 }
5355
5356 /****************************
5357  * FCNTL
5358  ***************************/
5359
5360 static int swrap_vfcntl(int fd, int cmd, va_list va)
5361 {
5362         struct socket_info_fd *src_fi, *fi;
5363         struct socket_info *si;
5364         int rc;
5365
5366         src_fi = find_socket_info_fd(fd);
5367         if (src_fi == NULL) {
5368                 return libc_vfcntl(fd, cmd, va);
5369         }
5370
5371         si = &sockets[src_fi->si_index];
5372
5373         switch (cmd) {
5374         case F_DUPFD:
5375                 fi = (struct socket_info_fd *)calloc(1, sizeof(struct socket_info_fd));
5376                 if (fi == NULL) {
5377                         errno = ENOMEM;
5378                         return -1;
5379                 }
5380
5381                 fi->fd = libc_vfcntl(fd, cmd, va);
5382                 if (fi->fd == -1) {
5383                         int saved_errno = errno;
5384                         free(fi);
5385                         errno = saved_errno;
5386                         return -1;
5387                 }
5388
5389                 si->refcount++;
5390                 fi->si_index = src_fi->si_index;
5391
5392                 /* Make sure we don't have an entry for the fd */
5393                 swrap_remove_stale(fi->fd);
5394
5395                 SWRAP_DLIST_ADD_AFTER(socket_fds, fi, src_fi);
5396
5397                 rc = fi->fd;
5398                 break;
5399         default:
5400                 rc = libc_vfcntl(fd, cmd, va);
5401                 break;
5402         }
5403
5404         return rc;
5405 }
5406
5407 int fcntl(int fd, int cmd, ...)
5408 {
5409         va_list va;
5410         int rc;
5411
5412         va_start(va, cmd);
5413
5414         rc = swrap_vfcntl(fd, cmd, va);
5415
5416         va_end(va);
5417
5418         return rc;
5419 }
5420
5421 /****************************
5422  * EVENTFD
5423  ***************************/
5424
5425 #ifdef HAVE_EVENTFD
5426 static int swrap_eventfd(int count, int flags)
5427 {
5428         int fd;
5429
5430         fd = libc_eventfd(count, flags);
5431         if (fd != -1) {
5432                 swrap_remove_stale(fd);
5433         }
5434
5435         return fd;
5436 }
5437
5438 #ifdef HAVE_EVENTFD_UNSIGNED_INT
5439 int eventfd(unsigned int count, int flags)
5440 #else
5441 int eventfd(int count, int flags)
5442 #endif
5443 {
5444         return swrap_eventfd(count, flags);
5445 }
5446 #endif
5447
5448 #ifdef HAVE_PLEDGE
5449 int pledge(const char *promises, const char *paths[])
5450 {
5451         (void)promises; /* unused */
5452         (void)paths; /* unused */
5453
5454         return 0;
5455 }
5456 #endif /* HAVE_PLEDGE */
5457
5458 /****************************
5459  * DESTRUCTOR
5460  ***************************/
5461
5462 /*
5463  * This function is called when the library is unloaded and makes sure that
5464  * sockets get closed and the unix file for the socket are unlinked.
5465  */
5466 void swrap_destructor(void)
5467 {
5468         struct socket_info_fd *s = socket_fds;
5469
5470         while (s != NULL) {
5471                 swrap_close(s->fd);
5472                 s = socket_fds;
5473         }
5474
5475         free(sockets);
5476
5477         if (swrap.libc_handle != NULL) {
5478                 dlclose(swrap.libc_handle);
5479         }
5480         if (swrap.libsocket_handle) {
5481                 dlclose(swrap.libsocket_handle);
5482         }
5483 }