Austin Schuh | 0a0a827 | 2021-12-08 13:19:32 -0800 | [diff] [blame^] | 1 | #define _GNU_SOURCE |
| 2 | #include <dlfcn.h> |
| 3 | #include <errno.h> |
| 4 | #include <stddef.h> |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <sys/socket.h> |
| 8 | |
| 9 | int socket(int domain, int type, int protocol) { |
| 10 | static int (*libsocket)(int domain, int type, int protocol) = NULL; |
| 11 | const char *error; |
| 12 | if (!libsocket) { |
| 13 | libsocket = dlsym(RTLD_NEXT, "socket"); |
| 14 | if ((error = dlerror()) != NULL) { |
| 15 | fprintf(stderr, "shim socket: %s\n", error); |
| 16 | exit(1); |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | if (getenv("has_ipv6")[0] != 'y' && domain == AF_INET6) { |
| 21 | errno = EAFNOSUPPORT; |
| 22 | return -1; |
| 23 | } |
| 24 | // Force AF_INET since we don't actually know whether this system |
| 25 | // supports IPv6 and we're just trying to create a socket for the |
| 26 | // caller to immediately close again. |
| 27 | return libsocket(AF_INET, type, protocol); |
| 28 | } |