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