-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.c
executable file
·378 lines (347 loc) · 12.1 KB
/
client.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* @Author: HandsomeChen
* @Date: 2019-12-15 19:58:50
* @LastEditTime : 2019-12-18 10:25:06
* @LastEditors : HandsomeChen
* @Description: 客户端服务
*/
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <assert.h>
#include "client.h"
#include "endpoint.h"
#include "message.h"
#define INTERVAL_PING 10
static int quited = 0; /* 退出标志 */
static int g_clientfd; /* 客户端本地sock */
static ENDPOINT *g_server; /* 服务器节点信息 */
static ENDPOINT_GROUP *g_peers; /* 分组成员列表 */
void *keep_alive_loop();
void *receive_loop();
void *console_loop();
static void quit()
{
quited = 1;
}
/**
* @description: 子线程, 保持与已打通的主机及服务器的连接活性
* @return: NULL
*/
void *keepalive_loop()
{
Message ping;
ping.head.magic = MSG_MAGIC;
ping.head.type = MSG_TYPE_PING;
ping.head.length = 0;
ping.body = NULL;
unsigned int i = 0;
struct list_head *get = NULL;
struct list_head *n = NULL;
while(!quited) {
/* 每10秒发送一次PING */
if (i++ < INTERVAL_PING) {
sleep(1);
continue;
}
i = 0;
udp_send_msg(g_clientfd, g_server, ping);
if (NULL != g_peers) /* 如果存在已打洞主机, 则分别发送PING消息, 保持活性 */
{
list_iterate_safe(get, n, &g_peers->list_endpoint)
{
ENDPOINT *tmp = list_get(get, ENDPOINT, list);
udp_send_msg(g_clientfd, tmp, ping);
}
}
}
printf("quiting keepalive_loop\n");
return NULL;
}
/**
* @description: 消息解析函数
* @param addr 源节点地址
* @param msg 接收到的消息
* @return: void
*/
void on_message(struct sockaddr_in addr, Message msg)
{
struct list_head *get = NULL;
struct list_head *n = NULL;
/* 来自服务器的消息 */
if (addr_equal(g_server->client_addr, addr))
{
printf("RECV %d bytes FROM %s: %s %s\n", msg.head.length,
ep_tostring(g_server), msg_type_str(msg.head.type), msg.body);
switch (msg.head.type)
{
case MSG_TYPE_PUNCH: /* 如果是打洞请求, 则对方主机回复打洞请求 */
{
ENDPOINT *peer = ep_fromstring(msg.body);
printf("%s on call, replying...\n", ep_tostring(peer));
udp_send_text(g_clientfd, peer, MSG_TYPE_REPLY, NULL);
}
break;
case MSG_TYPE_TEXT:
printf("SERVER: %s\n", msg.body);
break;
case MSG_TYPE_QUIT_REPLY: /* 如果是退出分组的回复, 则释放资源, 置g_peers为NULL */
{
printf("SERVER: %s\n", msg.body);
free(g_peers);
g_peers = NULL;
}
break;
case MSG_TYPE_PEERS_REPLY: /* 如果是请求分组成员的回复, 则解析消息将成员都加入g_peers中 */
{
printf("SERVER: %s\n", msg.body);
char *user = NULL;
user = strtok(msg.body, ";");
while (NULL != user)
{
ENDPOINT *tmp = ep_fromstring(user);
LIST_ADD(&tmp->list, &g_peers->list_endpoint);
user = strtok(NULL, ";");
}
}
break;
default:
break;
}
return;
}
/* 来自客户端的消息 */
if (NULL != g_peers)
{
/* 搜索消息来自哪一个已打通客户端 */
list_iterate_safe(get, n, &g_peers->list_endpoint)
{
ENDPOINT *tmp = list_get(get, ENDPOINT, list);
if (addr_equal(tmp->client_addr, addr))
{
printf("RECV %d bytes FROM %s: %s %s\n", msg.head.length,
ep_tostring(tmp), msg_type_str(msg.head.type), msg.body);
switch (msg.head.type)
{
case MSG_TYPE_TEXT:
printf("Peer(%s): %s\n", ep_tostring(tmp), msg.body);
break;
case MSG_TYPE_REPLY: /* 请求打洞回复, 说明打洞成功 */
printf("Peer(%s) replied, you can talk now\n", ep_tostring(tmp));
break;
case MSG_TYPE_PUNCH: /* 可能出现的情况(已经打通, 但对方又请求打洞), 这里简单处理 */
udp_send_text(g_clientfd, tmp, MSG_TYPE_TEXT, "I SEE YOU");
break;
case MSG_TYPE_PING: /* 对方的PING消息, 回复PONG */
udp_send_text(g_clientfd, tmp, MSG_TYPE_PONG, NULL);
break;
default:
break;
}
break;
}
}
}
}
/**
* @description: 子线程, 接收消息函数
* @return: NULL
*/
void *receive_loop()
{
struct sockaddr_in peer;
socklen_t addrlen;
char buf[SIZE_RECV_BUF];
int nfds;
fd_set readfds;
struct timeval timeout;
nfds = g_clientfd + 1;
while(!quited) {
FD_ZERO(&readfds);
FD_SET(g_clientfd, &readfds);
timeout.tv_sec = 1;
timeout.tv_usec = 0;
int ret = select(nfds, &readfds, NULL, NULL, &timeout);
if (ret == 0) {
/* timeout */
continue;
}
else if (ret == -1) {
perror("select");
continue;
}
assert(FD_ISSET(g_clientfd, &readfds));
addrlen = sizeof(peer);
memset(&peer, 0, addrlen);
memset(buf, 0, SIZE_RECV_BUF);
int rd_size = recvfrom(g_clientfd, buf, SIZE_RECV_BUF, 0,
(struct sockaddr*)&peer, &addrlen);
if (rd_size == -1) {
perror("recvfrom");
continue;
} else if (rd_size == 0) {
printf("EOF from %s:%d\n", inet_ntoa(peer.sin_addr), ntohs(peer.sin_port));
continue;
}
Message msg = msg_stream_pack(buf, rd_size);
if (msg.head.magic != MSG_MAGIC || msg.body == NULL) {
printf("Invalid message(%d bytes): {0x%x,%d,%d} %p\n", rd_size,
msg.head.magic, msg.head.type, msg.head.length, msg.body);
continue;
}
on_message(peer, msg);
}
printf("quiting receive_loop\n");
return NULL;
}
/**
* @description: 操作提示
* @return: void
*/
static void print_help()
{
const static char *help_message = ""
"Usage:"
"\n\n login username password phone_number"
"\n login to server so that other peer(s) can see you"
"\n\n list"
"\n list all groups"
"\n\n join group"
"\n join in a group"
"\n\n peers"
"\n list peers in group"
"\n\n punch"
"\n punch a hole through UDP to [host:port]s of group"
"\n\n send data"
"\n send [data] to peer [host:port]s in the group through UDP protocol "
"\n the other peers could receive your message if UDP hole punching succeed"
"\n\n quit"
"\n quit the group"
"\n\n logout"
"\n logout from server"
"\n\n help"
"\n print this help message";
printf("%s\n", help_message);
}
/**
* @description: 命令读取
* @return: NULL
*/
void *console_loop()
{
char *line = NULL;
size_t len;
ssize_t read;
while(fprintf(stdout, ">>> ") && (read = getline(&line, &len, stdin)) != -1) {
/* 空行忽略 */
if (read == 1) continue;
/* 解析命令 */
char *cmd = strtok(line, " ");
if (strncmp(cmd, "login", 5) == 0) {
char text[SIZE_SEND_BUF];
/* 解析参数, 即 username password phone_number */
char *username = strtok(NULL, " ");
char *password = strtok(NULL, " ");
char *phone_number = strtok(NULL, "\n");
/* 以 username:password:phone_number 格式发送 */
sprintf(text, "%s:%s:%s", username, password, phone_number);
udp_send_text(g_clientfd, g_server, MSG_TYPE_LOGIN, text);
} else if (strncmp(cmd, "list", 4) == 0) {
udp_send_text(g_clientfd, g_server, MSG_TYPE_LIST, NULL);
} else if (strncmp(cmd, "join", 4) == 0) {
int dsc;
/* 解析参数 分组描述符(group) */
char *group = strtok(NULL, "\n");
/* 现在以加入分组, g_peers初始化 */
g_peers = ep_create_endpoint_group();
sscanf(group, "%d", &dsc); /* 字符串(group)转整数(dsc) */
g_peers->dsc = dsc;
udp_send_text(g_clientfd, g_server, MSG_TYPE_JOIN, group);
} else if (strncmp(cmd, "peers", 5) == 0) {
udp_send_text(g_clientfd, g_server, MSG_TYPE_PEERS, NULL);
} else if (strncmp(cmd, "punch", 5) == 0) {
if (NULL == g_peers) /* g_peers为NULL, 则还未加入分组, 不可 punch */
{
printf("You haven't joined a group\n");
continue;
}
struct list_head *get = NULL;
struct list_head *n = NULL;
/* 对所有分组内的发打洞消息 */
list_iterate_safe(get, n, &g_peers->list_endpoint)
{
ENDPOINT *tmp = list_get(get, ENDPOINT, list);
printf("punching %s\n", ep_tostring(tmp));
udp_send_text(g_clientfd, tmp, MSG_TYPE_PUNCH, NULL);
}
/* 请求服务器协助打洞, 即让对方主机对本机回复 */
udp_send_text(g_clientfd, g_server, MSG_TYPE_PUNCH, NULL);
} else if (strncmp(cmd, "send", 4) == 0) {
if (NULL == g_peers)
{
printf("You haven't joined a group\n");
continue;
}
char *data = strtok(NULL, "\n");
struct list_head *get = NULL;
struct list_head *n = NULL;
/* 对分组内所有主机发送消息 */
list_iterate_safe(get, n, &g_peers->list_endpoint)
{
ENDPOINT *tmp = list_get(get, ENDPOINT, list);
udp_send_text(g_clientfd, tmp, MSG_TYPE_TEXT, data);
}
} else if (strncmp(cmd, "quit", 4) == 0) {
udp_send_text(g_clientfd, g_server, MSG_TYPE_QUIT, NULL);
} else if (strncmp(cmd, "logout", 6) == 0) {
udp_send_text(g_clientfd, g_server, MSG_TYPE_LOGOUT, NULL);
quit();
break;
} else if (strncmp(cmd, "help", 4) == 0) {
print_help();
} else {
printf("Unknown command %s\n", cmd);
print_help();
}
}
free(line);
printf("quiting console_loop\n");
return NULL;
}
int main(int argc, char **argv)
{
if (argc != 2) {
fprintf(stderr, "Usage: %s server:port\n", argv[0]);
return 1;
}
int ret;
pthread_t keepalive_pid, receive_pid, console_pid;
char text[SIZE_ENDPOINT_STR];
sprintf(text, "%s:%s:%d:%d", "Server", "127.0.0.1", 3478, 0);
/* 初始化服务器地址 */
g_server = ep_fromstring(text);
/* 打通主机列表初始化为NULL */
g_peers = NULL;
g_clientfd = socket(AF_INET, SOCK_DGRAM, 0);
printf("setting server to %s\n", ep_tostring(g_server));
/* 创建线程 */
if (g_clientfd == -1) { perror("socket"); goto clean; }
ret = pthread_create(&keepalive_pid, NULL, &keepalive_loop, NULL);
if (ret != 0) { perror("keepalive"); goto clean; }
ret = pthread_create(&receive_pid, NULL, &receive_loop, NULL);
if (ret != 0) { perror("receive"); goto clean; }
ret = pthread_create(&console_pid, NULL, &console_loop, NULL);
if (ret != 0) { perror("console"); goto clean; }
pthread_join(console_pid, NULL);
pthread_join(receive_pid, NULL);
pthread_join(keepalive_pid, NULL);
clean: /* 程序结束清理 */
close(g_clientfd);
return 0;
}