-
Notifications
You must be signed in to change notification settings - Fork 5
/
fd_info.c
240 lines (193 loc) · 4.98 KB
/
fd_info.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <string.h>
#include <stdlib.h>
#include "preload.h"
#include "fd_info.h"
#include "hashtable.h"
#include "dbg.h"
static DEFINE_HASHTABLE(fds, 8);
static LIST_HEAD(listeners);
void fd_listener_add(const struct fd_listener *l)
{
struct fd_listener *newl;
newl = malloc(sizeof(*newl));
memcpy(newl, l, sizeof(*l));
list_add(&newl->node, &listeners);
}
static struct fd_info *fd_find(int fd)
{
struct fd_info *info;
hash_for_each_possible(fds, info, node, fd)
if (info->fd == fd) return info;
return NULL;
}
void fd_grab(int fd, const struct fd_listener *l)
{
struct fd_info *info;
dbg("%s(%d)\n", __func__, fd);
info = malloc(sizeof(*info));
memset(info, 0, sizeof(*info));
info->listener = (struct fd_listener *) l;
info->fd = fd;
hash_add(fds, &info->node, fd);
}
int fd_open(const char *pathname)
{
struct fd_info *fd_info;
struct fd_listener *l;
int ret = FD_NONE;
fd_info = malloc(sizeof(*fd_info));
memset(fd_info, 0, sizeof(*fd_info));
list_for_each_entry(l, &listeners, node) {
if (!l->open) continue;
ret = l->open(fd_info, pathname);
if (ret == FD_NONE) continue;
if (ret < 0) break;
fd_info->listener = l;
fd_info->fd = ret;
hash_add(fds, &fd_info->node, ret);
return ret;
}
free(fd_info);
return ret;
}
int fd_socket(int domain, int type, int protocol)
{
struct fd_info *fd_info;
struct fd_listener *l;
int ret = FD_NONE;
fd_info = malloc(sizeof(*fd_info));
memset(fd_info, 0, sizeof(*fd_info));
list_for_each_entry(l, &listeners, node) {
if (!l->socket) continue;
ret = l->socket(fd_info, domain, type, protocol);
if (ret == FD_NONE) continue;
if (ret < 0) break;
fd_info->listener = l;
fd_info->fd = ret;
hash_add(fds, &fd_info->node, ret);
return ret;
}
free(fd_info);
return ret;
}
int fd_ioctl(int fd, int request, char *argp)
{
struct fd_info *fd_info;
int ret = FD_NONE;
fd_info = fd_find(fd);
if (fd_info && fd_info->listener->ioctl)
ret = fd_info->listener->ioctl(fd_info, request, argp);
return ret;
}
int fd_accept(int fd, struct sockaddr *addr, socklen_t *addr_len)
{
struct fd_info *fd_info;
int ret = FD_NONE;
fd_info = fd_find(fd);
if (fd_info && fd_info->listener->accept)
ret = fd_info->listener->accept(fd_info, addr, addr_len);
return ret;
}
int fd_bind(int fd, const struct sockaddr *addr, socklen_t len)
{
struct fd_info *fd_info;
int ret = FD_NONE;
fd_info = fd_find(fd);
if (fd_info && fd_info->listener->bind)
ret = fd_info->listener->bind(fd_info, addr, len);
return ret;
}
int fd_connect(int fd, const struct sockaddr *addr, socklen_t len)
{
struct fd_info *fd_info;
int ret = FD_NONE;
fd_info = fd_find(fd);
if (fd_info && fd_info->listener->connect)
ret = fd_info->listener->connect(fd_info, addr, len);
return ret;
}
int fd_listen(int fd, int n)
{
struct fd_info *fd_info;
int ret = FD_NONE;
fd_info = fd_find(fd);
if (fd_info && fd_info->listener->listen)
ret = fd_info->listener->listen(fd_info, n);
return ret;
}
int fd_fstat(int fd, struct stat *stat_buf)
{
struct fd_info *fd_info;
int ret = FD_NONE;
fd_info = fd_find(fd);
if (fd_info && fd_info->listener->fstat)
ret = fd_info->listener->fstat(fd_info, stat_buf);
return ret;
}
int fd_getsockopt(int fd, int level, int name, void *val, socklen_t *len)
{
struct fd_info *fd_info;
int ret = FD_NONE;
fd_info = fd_find(fd);
if (fd_info && fd_info->listener->getsockopt)
ret = fd_info->listener->getsockopt(fd_info, level, name, val, len);
return ret;
}
int fd_setsockopt(int fd, int level, int name, const void *val, socklen_t len)
{
struct fd_info *fd_info;
int ret = FD_NONE;
fd_info = fd_find(fd);
if (fd_info && fd_info->listener->setsockopt)
ret = fd_info->listener->setsockopt(fd_info, level, name, val, len);
return ret;
}
int fd_getsockname(int fd, struct sockaddr *addr, socklen_t *len)
{
struct fd_info *fd_info;
int ret = FD_NONE;
fd_info = fd_find(fd);
if (fd_info && fd_info->listener->getsockname)
ret = fd_info->listener->getsockname(fd_info, addr, len);
return ret;
}
ssize_t fd_recv(int fd, void *buf, size_t len, int flags)
{
struct fd_info *fd_info;
int ret = FD_NONE;
fd_info = fd_find(fd);
if (fd_info && fd_info->listener->recv)
ret = fd_info->listener->recv(fd_info, buf, len, flags);
return ret;
}
ssize_t fd_send(int fd, const void *buf, size_t len, int flags)
{
struct fd_info *fd_info;
int ret = FD_NONE;
fd_info = fd_find(fd);
if (fd_info && fd_info->listener->send)
ret = fd_info->listener->send(fd_info, buf, len, flags);
return ret;
}
ssize_t fd_recvfrom(int fd, void *buf, size_t len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen)
{
struct fd_info *fd_info;
int ret = FD_NONE;
fd_info = fd_find(fd);
if (fd_info && fd_info->listener->recvfrom)
ret = fd_info->listener->recvfrom(fd_info, buf, len, flags,
src_addr, addrlen);
return ret;
}
void fd_close(int fd)
{
struct fd_info *fd_info;
fd_info = fd_find(fd);
if (fd_info) {
if (fd_info->listener->close)
fd_info->listener->close(fd_info);
hash_del(&fd_info->node);
free(fd_info);
}
}