-
Notifications
You must be signed in to change notification settings - Fork 19
/
net_linux.c
188 lines (159 loc) · 5.3 KB
/
net_linux.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
// required for accept4 to be defined...
#define _GNU_SOURCE
#include <errno.h> // errno
#include <string.h> // strcpy, strerror
#include <stddef.h> // size_t
#include <stdio.h> // printf
#include <sys/epoll.h> // epoll_create1, epoll_wait, epoll_event, ...
#include <sys/socket.h> // socket, bind, listen, send
#include <sys/un.h> // sockaddr_un
#include <netinet/in.h> // sockaddr_in, INADDR_ANY, htons
#include <unistd.h> // unlink, close
#include "net.h"
static void print_err(const char *context)
{
printf("net error (%s): %s\n", context, strerror(errno));
}
int net_port(unsigned short port)
{
int server = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (server == -1)
goto abort;
int reuse = 1;
if (setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1)
goto abort;
struct sockaddr_in address = {0};
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(port);
if (bind(server, (struct sockaddr *) &address, sizeof(address)) == -1)
goto abort;
if (listen(server, SOMAXCONN) == -1)
goto abort;
return server;
abort:
print_err("net_port");
close(server);
return -1;
}
int net_sock(char *path)
{
if (!path) {
printf("error: no path provided for socket.\n");
return -1;
}
int server = socket(AF_UNIX, SOCK_STREAM, 0);
if (server == -1)
goto abort;
int reuse = 1;
if (setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1)
goto abort;
struct sockaddr_un address = {0};
address.sun_family = AF_UNIX;
strcpy(address.sun_path, path);
if (unlink(path) == -1)
goto abort;
if (bind(server, (struct sockaddr *) &address, sizeof(address)) == -1)
goto abort;
if (listen(server, SOMAXCONN) == -1)
goto abort;
return server;
abort:
print_err("net_sock");
close(server);
return -1;
}
void net_listen(int server, net_handler *handler)
{
static struct epoll_event events[32] = {0};
static unsigned char read_buf[8192] = {0};
if (!handler) {
printf("net error: no socket request handler provided.\n");
return;
}
int epoll_fd = 0;
struct epoll_event event = {0};
int ev_count = 0;
epoll_fd = epoll_create1(0);
if (epoll_fd == -1)
goto abort;
event.data.fd = server;
event.events = EPOLLIN | EPOLLET;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, server, &event) == -1)
goto abort;
while (1) {
ev_count = epoll_wait(epoll_fd, events, sizeof(events) / sizeof(*events), -1);
if (ev_count == -1)
goto abort;
for (int i = 0; i < ev_count; i += 1) {
// only read and write, ignore the rest.
if (!(events[i].events & EPOLLIN) &&
!(events[i].events & EPOLLOUT))
continue;
// server
if (events[i].data.fd == server) {
while (1) {
int client = accept4(server, 0, 0, SOCK_NONBLOCK);
if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK))
break;
if (client == -1) {
print_err("failed to accept new client in loop");
break;
}
event.data.fd = client;
event.events = EPOLLIN | EPOLLET;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client, &event) == -1) {
print_err("failed to add new client to epoll");
close(client);
} else {
handler(client, net_conn, 0, 0);
}
}
continue;
}
// clients.
if (events[i].events & EPOLLIN) {
while (1) {
ssize_t received = recv(events[i].data.fd, read_buf, sizeof(read_buf), 0);
if (received == -1 && (errno == EAGAIN || errno == EWOULDBLOCK))
break;
if (received == -1) {
print_err("failed while reading");
break;
}
if (received == 0) {
handler(events[i].data.fd, net_closed, 0, 0);
epoll_ctl(epoll_fd, EPOLL_CTL_DEL, events[i].data.fd, &event);
break;
}
handler(events[i].data.fd, net_read, read_buf, (size_t) received);
}
}
}
}
abort:
print_err("net_listen");
close(epoll_fd);
close(server);
}
unsigned long long net_send(int socket, void *buf, unsigned long long n)
{
unsigned long long sent = 0;
if (!buf)
return 0;
while (sent < n) {
long long tmp = send(socket, buf + sent, n - sent, 0);
if (tmp == -1 && (errno == EAGAIN || errno == EWOULDBLOCK))
break;
if (tmp == -1) {
print_err("net_write");
break;
}
sent += tmp;
}
return sent;
}
void net_close(int socket)
{
close(socket);
}