blob: ab38c6c58f6f7885e925ac1fe6aa66cfda973788 [file] [log] [blame]
Austin Schuh0a0a8272021-12-08 13:19:32 -08001#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
9int 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}