forked from pandax381/microps
-
Notifications
You must be signed in to change notification settings - Fork 1
/
echo_server.c
83 lines (80 loc) · 1.89 KB
/
echo_server.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
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include "microps.h"
/*
struct microps_param param = {
.ethernet_device = "en2",
.ethernet_addr = "00:1f:5b:fe:ef:cd",
.ip_addr = "10.10.2.228",
.ip_netmask = "255.255.0.0",
.ip_gateway = "10.10.0.1"
};
*/
struct microps_param param = {
.ethernet_device = "eth7",
.ethernet_addr = "a0:36:9f:3e:a0:8a",
.ip_addr = "10.0.0.1",
.ip_netmask = "255.255.255.0",
.ip_gateway = NULL,
.use_dhcp = 0
};
int
main (int argc, char *argv[]) {
int soc = -1, acc;
uint8_t buf[65536];
ssize_t len;
if (microps_init(¶m) == -1) {
goto ERROR;
}
soc = tcp_api_open();
if (soc == -1) {
goto ERROR;
}
if (tcp_api_bind(soc, hton16(7)) == -1) {
goto ERROR;
}
tcp_api_listen(soc);
acc = tcp_api_accept(soc);
if (acc == -1) {
goto ERROR;
}
fprintf(stderr, "accept success, soc=%d, acc=%d\n", soc, acc);
while (1) {
len = tcp_api_recv(acc, buf, sizeof(buf));
if (len <= 0) {
break;
}
hexdump(stderr, buf, len);
tcp_api_send(acc, buf, len);
}
tcp_api_close(acc);
/*
ip_addr_t peer_addr;
uint16_t peer_port;
ip_addr_pton("72.21.215.232", &peer_addr);
peer_port = hton16(80);
tcp_api_connect(soc, &peer_addr, peer_port);
strcpy(buf, "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: Close\r\n\r\n");
tcp_api_send(soc, (uint8_t *)buf, strlen(buf));
while (1) {
len = tcp_api_recv(soc, (uint8_t *)buf, sizeof(buf));
//fprintf(stderr, "len: %ld\n", len);
if (len <= 0) {
break;
}
//hexdump(stderr, buf, len);
}
*/
tcp_api_close(soc);
microps_cleanup();
return 0;
ERROR:
if (soc != -1) {
tcp_api_close(soc);
}
microps_cleanup();
return -1;
}