forked from jibi/nm-picotcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnm-picotcp.c
306 lines (242 loc) · 6.56 KB
/
nm-picotcp.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
* Copyright (C) 2014 jibi <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#define _GNU_SOURCE
#define NETMAP_WITH_LIBS
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <net/netmap_user.h>
#include <netinet/ether.h>
#include <sys/ioctl.h>
#include <pico_ipv4.h>
#include <pico_stack.h>
#include <pico_socket.h>
#define BSIZE 2048
struct {
char *if_mac;
char *if_name;
char *if_addr;
char *port;
} config;
void setup_tcp_app();
void pico_netmap_destroy(struct pico_device *dev);
struct pico_device *pico_netmap_create(char *interface, char *name, uint8_t *mac);
static char recvbuf[BSIZE];
static int pos = 0;
static int len = 0;
static int flag = 0;
void
deferred_exit(pico_time __attribute__((unused)) now, void *arg) {
if (arg) {
free(arg);
arg = NULL;
}
exit(0);
}
int
send_tcpecho(struct pico_socket *s) {
int w, ww = 0;
if (len > pos) {
do {
w = pico_socket_write(s, recvbuf + pos, len - pos);
if (w > 0) {
pos += w;
ww += w;
if (pos >= len) {
pos = 0;
len = 0;
}
}
} while((w > 0) && (pos < len));
}
return ww;
}
void
cb_tcpecho(uint16_t ev, struct pico_socket *s) {
int r = 0;
printf("tcpecho> wakeup ev=%u\n", ev);
if (ev & PICO_SOCK_EV_RD) {
if (flag & PICO_SOCK_EV_CLOSE) {
printf("SOCKET> EV_RD, FIN RECEIVED\n");
}
while (len < BSIZE) {
r = pico_socket_read(s, recvbuf + len, BSIZE - len);
if (r > 0) {
len += r;
flag &= ~(PICO_SOCK_EV_RD);
} else {
flag |= PICO_SOCK_EV_RD;
break;
}
}
if (flag & PICO_SOCK_EV_WR) {
flag &= ~PICO_SOCK_EV_WR;
send_tcpecho(s);
}
}
if (ev & PICO_SOCK_EV_CONN) {
struct pico_socket *sock_a = { 0 };
struct pico_ip4 orig = { 0 };
uint16_t port = 0;
char peer[30] = { 0 };
int yes = 1;
sock_a = pico_socket_accept(s, &orig, &port);
pico_ipv4_to_string(peer, orig.addr);
printf("Connection established with %s:%d.\n", peer, short_be(port));
pico_socket_setoption(sock_a, PICO_TCP_NODELAY, &yes);
}
if (ev & PICO_SOCK_EV_FIN) {
printf("Socket closed. Exit normally. \n");
pico_timer_add(2000, deferred_exit, NULL);
}
if (ev & PICO_SOCK_EV_ERR) {
printf("Socket error received: %s. Bailing out.\n", strerror(pico_err));
exit(1);
}
if (ev & PICO_SOCK_EV_CLOSE) {
printf("Socket received close from peer.\n");
if (flag & PICO_SOCK_EV_RD) {
pico_socket_shutdown(s, PICO_SHUT_WR);
printf("SOCKET> Called shutdown write, ev = %d\n", ev);
}
}
if (ev & PICO_SOCK_EV_WR) {
r = send_tcpecho(s);
if (r == 0) {
flag |= PICO_SOCK_EV_WR;
} else {
flag &= (~PICO_SOCK_EV_WR);
}
}
}
void
setup_tcp_app() {
struct pico_socket *listen_socket;
struct pico_ip4 address;
uint16_t port;
int ret, yes;
yes = 1;
port = short_be(atoi(config.port));
bzero(&address, sizeof(address));
listen_socket = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_TCP, &cb_tcpecho);
if (!listen_socket) {
printf("cannot open socket: %s", strerror(pico_err));
exit(1);
}
pico_socket_setoption(listen_socket, PICO_TCP_NODELAY, &yes);
ret = pico_socket_bind(listen_socket, &address, &port);
if (ret < 0) {
printf("cannot bind socket to port %u: %s", short_be(port), strerror(pico_err));
exit(1);
}
ret = pico_socket_listen(listen_socket, 40);
if (ret != 0) {
printf("cannot listen on port %u", short_be(port));
exit(1);
}
return;
}
struct pico_device_netmap {
struct pico_device dev;
struct nm_desc *conn;
};
int
pico_netmap_send(struct pico_device *dev, void *buf, int len) {
struct pico_device_netmap *netmap = (struct pico_device_netmap *) dev;
return nm_inject(netmap->conn, buf, len);
}
void
pico_dev_netmap_cb(u_char *u, const struct nm_pkthdr *h, const uint8_t *buf) {
struct pico_device *dev = (struct pico_device *) u;
pico_stack_recv(dev, (uint8_t *) buf, (uint32_t) h->len);
}
int
pico_netmap_poll(struct pico_device *dev, int loop_score) {
struct pico_device_netmap *netmap;
struct pollfd fds;
netmap = (struct pico_device_netmap *) dev;
fds.fd = NETMAP_FD(netmap->conn);
fds.events = POLLIN;
do {
if (poll(&fds, 1, 0) <= 0) {
return loop_score;
}
loop_score -= nm_dispatch(netmap->conn, loop_score, pico_dev_netmap_cb, (u_char *) netmap);
} while(loop_score > 0);
return 0;
}
void
pico_netmap_destroy(struct pico_device *dev) {
struct pico_device_netmap *netmap = (struct pico_device_netmap *) dev;
nm_close(netmap->conn);
}
struct pico_device *
pico_netmap_create(char *interface, char *name, uint8_t *mac) {
struct pico_device_netmap *netmap;
char ifname[IFNAMSIZ + 7];
netmap = PICO_ZALLOC(sizeof(struct pico_device_netmap));
if (!netmap) {
return NULL;
}
if (pico_device_init((struct pico_device *)netmap, name, mac)) {
pico_netmap_destroy((struct pico_device *)netmap);
return NULL;
}
sprintf(ifname, "netmap:%s", interface);
netmap->dev.overhead = 0;
netmap->conn = nm_open(ifname, NULL, 0, 0);
if (! netmap->conn) {
pico_netmap_destroy((struct pico_device *)netmap);
return NULL;
}
netmap->dev.send = pico_netmap_send;
netmap->dev.poll = pico_netmap_poll;
netmap->dev.destroy = pico_netmap_destroy;
return (struct pico_device *) netmap;
}
void
init_picotcp() {
struct ether_addr mac;
struct pico_device *dev = NULL;
struct pico_ip4 addr;
struct pico_ip4 netm;
ether_aton_r(config.if_mac, &mac);
pico_stack_init();
dev = pico_netmap_create(config.if_name, "eth_if", (uint8_t *) &mac);
pico_string_to_ipv4(config.if_addr, &addr.addr);
pico_string_to_ipv4("255.255.255.0", &netm.addr);
pico_ipv4_link_add(dev, addr, netm);
}
int
main(int argc, char *argv[]) {
if (argc < 4) {
printf("usage: %s if_name if_mac if_addr port\n", argv[0]);
exit(1);
}
config.if_name = argv[1];
config.if_mac = argv[2];
config.if_addr = argv[3];
config.port = argv[4];
init_picotcp();
setup_tcp_app();
pico_stack_loop();
return 0;
}