-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook.c
124 lines (97 loc) · 2.4 KB
/
hook.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "system.h"
#include "hook.h"
#include "cleanup.h"
#include "socket.h"
#include "protocol.h"
#include "xsocket.h"
#include "switch.h"
char* xsocket_address;
ssize_t xb_nport;
in_port_t* xb_ports;
volatile bind_fn xb_bind;
__attribute__((constructor))
void xbind_initialize(void)
{
const char* value = getenv("XSOCKET");
xsocket_address = value && *value ? strdup(value) : NULL;
value = getenv("XBIND");
if (!value || !*value)
return;
if (!strcmp(value, "*"))
{
xb_nport = -1;
return;
}
AUTO_FREE char* buffer = strdup(value);
size_t count = 0, capacity = 0;
in_port_t* ports = NULL;
const char* separator = " ";
char *token, *dummy;
for (token = strtok_r(buffer, separator, &dummy); token; token = strtok_r(NULL, separator, &dummy))
{
char* endptr;
unsigned long port = strtoul(token, &endptr, 10);
if (*endptr)
continue;
if (port <= 0 || port >= 0x10000)
continue;
if (count >= capacity)
{
capacity += 8;
ports = realloc(ports, capacity * sizeof(in_port_t));
}
ports[count++] = port;
}
if (count < capacity)
ports = realloc(ports, count * sizeof(in_port_t));
xb_ports = ports;
xb_nport = count;
}
__attribute__((visibility("default")))
int bind(fd_t sockfd, const struct sockaddr* address, socklen_t addrlen)
{
int domain, type, protocol;
int check = check_socket(sockfd, &domain, &type, &protocol);
if (check < 0)
return -1;
if (!check || !check_address(address, addrlen, xb_ports, xb_nport))
return xbind_forward(sockfd, address, addrlen);
type |= SOCK_CLOEXEC;
int flags = fcntl(sockfd, F_GETFL, 0);
if (flags < 0)
return -1;
if (flags & O_NONBLOCK)
type |= SOCK_NONBLOCK;
AUTO_CLOSE fd_t newfd = xsocket(xsocket_address, domain, type, protocol);
if (newfd < 0)
return -1;
if (!switcheroo(sockfd, newfd))
return -1;
close_p(&newfd);
return xbind_forward(sockfd, address, addrlen);
}
int xbind_forward(fd_t sockfd, const struct sockaddr* address, socklen_t addrlen)
{
const bind_fn invalid = (bind_fn)(intptr_t)(-1);
bind_fn ptr = xb_bind;
if (__builtin_expect(!ptr, 0))
{
ptr = (bind_fn)dlsym(RTLD_NEXT, "bind");
if (!ptr)
ptr = invalid;
xb_bind = ptr;
}
if (__builtin_expect(ptr != invalid, 1))
return ptr(sockfd, address, addrlen);
errno = ENOSYS;
return -1;
}
__attribute__((destructor))
void xbind_terminate(void)
{
free(xb_ports);
xb_ports = NULL;
xb_nport = 0;
free(xsocket_address);
xsocket_address = NULL;
}