forked from lichao2014/name_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet.c
252 lines (199 loc) · 5.14 KB
/
net.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
241
242
243
244
245
246
247
248
249
250
251
252
#include "net.h"
#include "log.h"
#include <unistd.h>
#include <stdio.h>
int
io_context_init(struct io_context *ctx, size_t hint) {
int epollfd = epoll_create(hint);
if (epollfd < 0) {
perror("epoll_create");
return -1;
}
if (slice_init(&ctx->events, sizeof(struct epoll_event), 0, 2) < 0) {
close(epollfd);
return -1;
}
ctx->epollfd = epollfd;
list_init(&ctx->objects);
ctx->nobjects = 0;
return 0;
}
void
io_context_close(struct io_context *ctx) {
for (struct list_t *i = ctx->objects.next; i; i = i->next) {
struct io_object *o = list_of(i, struct io_object, q);
close(o->fd);
free(o);
}
ctx->nobjects = 0;
slice_free(&ctx->events);
if (ctx->epollfd >= 0) {
close(ctx->epollfd);
}
}
int
io_context_run(struct io_context *ctx, int timeout) {
if (ctx->nobjects > ctx->events.len) {
slice_append_n(&ctx->events, ctx->nobjects - ctx->events.len);
}
struct epoll_event *events = (struct epoll_event *)ctx->events.data;
int ret = epoll_wait(ctx->epollfd, events, ctx->nobjects, timeout);
if (ret <= 0) {
return ret;
}
DEBUG_LOG("epoll_wait ret:%d\n", ret);
for (int i = 0; i < ret; ++i) {
struct io_object *o = events[i].data.ptr;
if (events[i].events & EPOLLOUT) {
o->on_write(o);
}
if (events[i].events & EPOLLIN) {
o->on_read(o);
}
if (events[i].events & EPOLLERR) {
o->on_error(o);
}
if (o->deleted) {
free(o);
}
}
return ret;
}
int
io_context_add(struct io_context *ctx,
int fd,
read_callback_t on_read,
write_callback_t on_write,
error_callback_t on_error,
void *arg) {
struct io_object *o = malloc(sizeof(struct io_object));
if (!o) {
return -1;
}
list_init(&o->q);
o->ctx = ctx;
o->arg = arg;
o->fd = fd;
o->deleted = 0;
o->on_read = on_read;
o->on_write = on_write;
o->on_error = on_error;
struct epoll_event e;
e.data.ptr = o;
e.events = 0;
if (on_read) {
e.events |= EPOLLIN;
}
if (on_write) {
e.events |= EPOLLOUT;
}
if (on_error) {
e.events |= EPOLLERR;
}
if (epoll_ctl(ctx->epollfd, EPOLL_CTL_ADD, fd, &e) < 0) {
free(o);
return -1;
}
list_insert_after(&ctx->objects, &o->q);
ctx->nobjects++;
return 0;
}
int
io_context_del(struct io_context *ctx, int fd) {
struct io_object *o = NULL;
for (struct list_t *i = ctx->objects.next; i; i = i->next) {
o = list_of(i, struct io_object, q);
if (o->fd == fd) {
list_del(&o->q);
ctx->nobjects--;
break;
}
}
if (!o) {
return -1;
}
o->deleted = 1;
return epoll_ctl(ctx->epollfd, EPOLL_CTL_DEL, fd, NULL);
}
struct sockaddr_in
make_address4(const char *ip, int port) {
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ip);
return addr;
}
int
create_tcp_socket(const char *ip, int port, int is_server) {
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
perror("socket");
return fd;
}
int opt = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt) < 0) {
perror("setsockopt");
close(fd);
return -1;
}
struct sockaddr_in addr = make_address4(ip, port);
if (bind(fd, (struct sockaddr *)&addr, sizeof addr) < 0) {
perror("bind");
close(fd);
return -1;
}
if (is_server) {
if (listen(fd, 64) < 0) {
perror("listen");
close(fd);
return -1;
}
}
return fd;
}
int
send_with_fd(int s, void *buf, size_t len, int fd) {
char control[CMSG_SPACE(sizeof(int))];
struct msghdr msg;
msg.msg_name = NULL;
msg.msg_namelen = 0;
struct iovec iov;
iov.iov_base = buf;
iov.iov_len = len;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = control;
msg.msg_controllen = sizeof control;
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
*(int *)CMSG_DATA(cmsg) = fd;
return sendmsg(s, &msg, 0);
}
int
recv_with_fd(int s, void *buf, size_t len, int *fd) {
char control[CMSG_SPACE(sizeof(int))];
struct msghdr msg;
msg.msg_name = NULL;
msg.msg_namelen = 0;
struct iovec iov;
iov.iov_base = buf;
iov.iov_len = len;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = control;
msg.msg_controllen = sizeof control;
if (recvmsg(s, &msg, 0) <= 0) {
return -1;
}
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
if (!cmsg
|| (CMSG_LEN(sizeof(int)) != cmsg->cmsg_len)
|| (SOL_SOCKET != cmsg->cmsg_level)
|| (SCM_RIGHTS != cmsg->cmsg_type)) {
return -1;
}
*fd = *(int *)CMSG_DATA(cmsg);
return 0;
}