forked from gozfree/gear-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_libskt.c
313 lines (290 loc) · 8.38 KB
/
test_libskt.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
306
307
308
309
310
311
312
313
/******************************************************************************
* Copyright (C) 2014-2018 Zhifeng Gong <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with libraries; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#include "libskt.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <libgevent.h>
#include <libthread.h>
#include <libmacro.h>
struct skt_connection *g_sc = NULL;
static struct gevent_base *g_evbase;
static uint8_t socket_id = -1;
void on_read(int fd, void *arg)
{
char buf[14];
memset(buf, 0, sizeof(buf));
int ret = skt_recv(fd, buf, sizeof(buf));
if (ret > 0) {
DUMP_BUFFER(buf, sizeof(buf));
socket_id = buf[6];
printf("socket_id = %d\n", socket_id);
} else if (ret == 0) {
printf("delete connection fd:%d\n", fd);
} else if (ret < 0) {
printf("recv failed!\n");
}
}
void on_recv(int fd, void *arg)
{
char buf[2048];
memset(buf, 0, sizeof(buf));
int ret = skt_recv(fd, buf, 2048);
if (ret > 0) {
printf("recv buf = %s\n", buf);
} else if (ret == 0) {
printf("delete connection fd:%d\n", fd);
} else if (ret < 0) {
printf("recv failed!\n");
}
}
void on_error(int fd, void *arg)
{
printf("error: %d\n", errno);
}
static void *tcp_client_thread(struct thread *thread, void *arg)
{
struct skt_connection *sc = (struct skt_connection *)arg;
g_evbase = gevent_base_create();
if (!g_evbase) {
return NULL;
}
if (0 > skt_set_nonblock(sc->fd)) {
printf("skt_set_nonblock failed!\n");
return NULL;
}
struct gevent *e = gevent_create(sc->fd, on_recv, NULL, on_error, (void *)&sc->fd);
if (-1 == gevent_add(g_evbase, e)) {
printf("event_add failed!\n");
}
gevent_base_loop(g_evbase);
return NULL;
}
int tcp_client(const char *host, uint16_t port)
{
char str_ip[INET_ADDRSTRLEN];
int n, ret;
char buf[64];
struct tcp_info tcpi;
g_sc = skt_tcp_connect(host, port);
if (g_sc == NULL) {
printf("connect failed!\n");
return -1;
}
on_read(g_sc->fd, NULL);
skt_addr_ntop(str_ip, g_sc->local.ip);
printf("local ip = %s, port = %d\n", str_ip, g_sc->local.port);
skt_addr_ntop(str_ip, g_sc->remote.ip);
printf("remote ip = %s, port = %d\n", str_ip, g_sc->remote.port);
skt_set_tcp_keepalive(g_sc->fd, 1);
memset(&tcpi, 0, sizeof(tcpi));
ret = skt_get_tcp_info(g_sc->fd, &tcpi);
if (ret == 0) {
printf("unrecovered=%u "
"rto=%u ato=%u snd_mss=%u rcv_mss=%u "
"lost=%u retrans=%u rtt=%u rttvar=%u "
"sshthresh=%u cwnd=%u total_retrans=%u\n",
tcpi.tcpi_retransmits, // Number of unrecovered [RTO] timeouts
tcpi.tcpi_rto, // Retransmit timeout in usec
tcpi.tcpi_ato, // Predicted tick of soft clock in usec
tcpi.tcpi_snd_mss,
tcpi.tcpi_rcv_mss,
tcpi.tcpi_lost, // Lost packets
tcpi.tcpi_retrans, // Retransmitted packets out
tcpi.tcpi_rtt, // Smoothed round trip time in usec
tcpi.tcpi_rttvar, // Medium deviation
tcpi.tcpi_snd_ssthresh,
tcpi.tcpi_snd_cwnd,
tcpi.tcpi_total_retrans); // Total retransmits for entire connection
}
struct thread *thread = thread_create(tcp_client_thread, g_sc);
if (!thread) {
printf("thread_create failed!\n");
}
while (1) {
memset(buf, 0, sizeof(buf));
printf("input> ");
scanf("%s", buf);
n = skt_send(g_sc->fd, buf, strlen(buf));
if (n == -1) {
printf("skt_send failed!\n");
return -1;
}
// sleep(1);
}
}
int udp_client(const char *host, uint16_t port)
{
struct skt_connection *sc = skt_udp_connect(host, port);
int ret = skt_send(sc->fd, "aaa", 4);
printf("fd = %d, ret = %d\n", sc->fd, ret);
return 0;
}
void on_connect(int fd, void *arg)
{
char str_ip[INET_ADDRSTRLEN];
int afd;
uint32_t ip;
uint16_t port;
afd = skt_accept(fd, &ip, &port);
if (afd == -1) {
printf("errno=%d %s\n", errno, strerror(errno));
return;
};
skt_addr_ntop(str_ip, ip);
printf("new connect comming: ip = %s, port = %d\n", str_ip, port);
if (0 > skt_set_nonblock(afd)) {
printf("skt_set_nonblock failed!\n");
return;
}
struct gevent *e = gevent_create(afd, on_recv, NULL, on_error, (void *)&afd);
if (-1 == gevent_add(g_evbase, e)) {
printf("event_add failed!\n");
}
}
int udp_server(uint16_t port)
{
int fd;
fd = skt_udp_bind(NULL, port);
if (fd == -1) {
return -1;
}
g_evbase = gevent_base_create();
if (!g_evbase) {
return -1;
}
skt_set_noblk(fd, true);
struct gevent *e = gevent_create(fd, on_recv, NULL, on_error, NULL);
if (-1 == gevent_add(g_evbase, e)) {
printf("event_add failed!\n");
}
gevent_base_loop(g_evbase);
return 0;
}
int tcp_server(uint16_t port)
{
int fd;
fd = skt_tcp_bind_listen(NULL, port);
if (fd == -1) {
return -1;
}
struct skt_addr addr;
skt_getaddr_by_fd(fd, &addr);
printf("addr = %s\n", addr.ip_str);
g_evbase = gevent_base_create();
if (!g_evbase) {
return -1;
}
struct gevent *e = gevent_create(fd, on_connect, NULL, on_error, NULL);
if (-1 == gevent_add(g_evbase, e)) {
printf("event_add failed!\n");
}
gevent_base_loop(g_evbase);
return 0;
}
void usage()
{
fprintf(stderr, "./test_libskt -s port\n"
"./test_libskt -c ip port\n");
}
void addr_test()
{
char str_ip[INET_ADDRSTRLEN];
uint32_t net_ip;
net_ip = skt_addr_pton("192.168.1.123");
printf("ip = %x\n", net_ip);
skt_addr_ntop(str_ip, net_ip);
printf("ip = %s\n", str_ip);
}
void domain_test()
{
void *p;
char str[MAX_ADDR_STRING];
skt_addr_list_t *tmp;
if (0 == skt_get_local_list(&tmp, 0)) {
for (; tmp; tmp = tmp->next) {
skt_addr_ntop(str, tmp->addr.ip);
printf("ip = %s port = %d\n", str, tmp->addr.port);
}
}
if (0 == skt_getaddrinfo(&tmp, "www.sina.com", "3478")) {
for (; tmp; tmp = tmp->next) {
skt_addr_ntop(str, tmp->addr.ip);
printf("ip = %s port = %d\n", str, tmp->addr.port);
}
}
if (0 == skt_gethostbyname(&tmp, "www.baidu.com")) {
for (; tmp; tmp = tmp->next) {
skt_addr_ntop(str, tmp->addr.ip);
printf("ip = %s port = %d\n", str, tmp->addr.port);
}
}
do {
p = tmp;
if (tmp) tmp = tmp->next;
if (p) free(p);
} while (tmp);
}
void ctrl_c_op(int signo)
{
if (g_sc) {
skt_close(g_sc->fd);
}
exit(0);
}
int main(int argc, char **argv)
{
uint16_t port;
const char *ip;
skt_get_local_info();
if (argc < 2) {
usage();
exit(0);
}
signal(SIGINT, ctrl_c_op);
if (!strcmp(argv[1], "-s")) {
if (argc == 3)
port = atoi(argv[2]);
else
port = 0;
//tcp_server(port);
udp_server(port);
} else if (!strcmp(argv[1], "-c")) {
if (argc == 3) {
ip = "127.0.0.1";
port = atoi(argv[2]);
} else if (argc == 4) {
ip = argv[2];
port = atoi(argv[3]);
}
//tcp_client(ip, port);
udp_client(ip, port);
}
if (!strcmp(argv[1], "-t")) {
addr_test();
}
if (!strcmp(argv[1], "-d")) {
domain_test();
}
return 0;
}