Repository navigation
Expand file tree
/
Copy pathclient.cpp
More file actions
502 lines (484 loc) · 15.4 KB
/
Copy pathclient.cpp
File metadata and controls
502 lines (484 loc) · 15.4 KB
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <cstring>
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#include <iostream>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <signal.h>
#include <time.h>
#include <netdb.h>
#define SBCP_VERSION 3
// Message types
#define SBCP_MSG_JOIN 2
#define SBCP_MSG_FWD 3
#define SBCP_MSG_SEND 4
// Bonus
#define SBCP_MSG_NAK 5
#define SBCP_MSG_OFFLINE 6
#define SBCP_MSG_ACK 7
#define SBCP_MSG_ONLINE 8
#define SBCP_MSG_IDLE 9
// Attribute types
#define ATTR_REASON 1
#define ATTR_USERNAME 2
#define ATTR_CLIENT_COUNT 3
#define ATTR_MESSAGE 4
// Max allowed
#define MAX_USERNAME_LEN 16
#define MAX_MESSAGE_LEN 512
#define MAX_REASON_LEN 128
#define SBCP_HDR_LEN 4
#define ATTR_HDR_LEN 4
#define MAX_BUF 1024
#define IDLE_TIMEOUT_SEC 10 // Bonus Feature 2
#define MAX_USER_COUNT 256
using namespace std;
struct __attribute__((__packed__)) sbcp_header {
uint16_t vrsn_type; // Version(9b) | Type(7b)
uint16_t length; // length all the whole message(including the header)
};
struct __attribute__((__packed__)) sbcp_attr_header {
uint16_t type;
uint16_t length; // attribute length, including header
};
//Header Encoding
void encode_header(struct sbcp_header *hdr, int type, int total_len) {
uint16_t vt = (SBCP_VERSION << 7) | (type & 0x7F);
hdr->vrsn_type = htons(vt);
hdr->length = htons(total_len);
return ;
}
void decode_header(const struct sbcp_header *hdr, int *version, int *type, int *length) {
uint16_t vt = ntohs(hdr->vrsn_type);
*version = (vt & 0xFF80)>>7 ;
*type = (vt & 0x7F) ;
*length = ntohs(hdr->length);
}
//Attribute Encoding
int append_attr(char *buf, int offset, int buf_size,
uint16_t attr_type, const char *payload, int payload_len) {
int total = payload_len + ATTR_HDR_LEN;// attr header + payload
if (payload_len < 0 || offset + total > buf_size) {
fprintf(stderr, "invalid attribute length\n");
return -1;
}
struct sbcp_attr_header *ah = (struct sbcp_attr_header *)(buf + offset);
ah->type = htons(attr_type);
ah->length = htons(total);
memcpy(buf + offset + ATTR_HDR_LEN, payload, payload_len);
return total;
}
//Build Message based on type
// Join
int build_join(char *buf, int buf_size, const char *username) {
int name_len = strlen(username) ;
if(name_len>MAX_USERNAME_LEN||name_len==0){
fprintf(stderr,"Valid User name length is between 1 to 16\n");
return -1;
}
int offset = SBCP_HDR_LEN;
int n = append_attr(buf,offset,buf_size,ATTR_USERNAME,username,name_len);
if (n<0) {
fprintf(stderr, "fail to build attribute header\n");
return -1;
}
int total = SBCP_HDR_LEN+ATTR_HDR_LEN+name_len;
encode_header((sbcp_header*)(buf), SBCP_MSG_JOIN, total);
return total;
}
// Send Message
int build_send(char *buf, int buf_size, const char *msg, int msg_len) {
//valide length check
if(msg_len>MAX_MESSAGE_LEN){
fprintf(stderr,"Valid message length is capped by 512\n");
return -1;
}
int offset = SBCP_HDR_LEN;
int n = append_attr(buf,offset,buf_size,ATTR_MESSAGE,msg,msg_len);
if (n<0) {
fprintf(stderr, "fail to build message header\n");
return -1;
}
int total = SBCP_HDR_LEN+ATTR_HDR_LEN+msg_len;
encode_header((sbcp_header*)(buf), SBCP_MSG_SEND, total);
return total;
}
// Bonus: Idle
int build_idle(char *buf, int buf_size) {
if(buf_size < SBCP_HDR_LEN){
fprintf(stderr, "Invalid Buffersize\n");
return -1;
}
encode_header((sbcp_header*)(buf),SBCP_MSG_IDLE,SBCP_HDR_LEN) ;
return SBCP_HDR_LEN;
}
//send message
int send_all(int sockfd, const char *buf, int len) {
int sent = 0;
while (sent<len) {
int n = send(sockfd, buf+sent, len-sent, MSG_NOSIGNAL);
if (n < 0) {
if (errno==EINTR) {
continue;
}
perror("send");
return -1;
}
sent += n ;
}
return len;
}
// Receive message from server
// return 0: handle successfully
// return -1: disconnect
int handle_server_message(const char *buf, int n) {
// check length(screen for short message)
if (n<SBCP_HDR_LEN) {
fprintf(stderr, "message too short, discarded\n");
return 0;
}
int version, type, length;
decode_header((const sbcp_header*)buf, &version, &type, &length);
// version confirm
if (version!=SBCP_VERSION) {
fprintf(stderr, "unknown version %d, discarded\n", version);
return 0;
}
// length confirm
if (length!=n) {
fprintf(stderr, "length mismatch (header %d, read %d), discarded\n", length, n);
return 0;
}
// attribute check
int pos = SBCP_HDR_LEN;
char username[MAX_USERNAME_LEN + 1] = "";
char message[MAX_MESSAGE_LEN + 1] = "";
char reason[MAX_REASON_LEN + 1] = ""; // NAK
int client_count = -1;
char user_list[MAX_USER_COUNT][MAX_USERNAME_LEN + 1]; // ACK list
int user_count = 0; // counter for user_list
while (pos<length) {
// trivial length check
if (length-pos<ATTR_HDR_LEN) {
fprintf(stderr, "truncated attribute, discarded\n");
return 0 ;
}
const struct sbcp_attr_header *ah = (const struct sbcp_attr_header *)(buf + pos);
int attr_type = ntohs(ah->type);
int attr_len = ntohs(ah->length);
// check for valid attribute len, avoid infinite loop
if (attr_len<ATTR_HDR_LEN||pos+attr_len>n) {
fprintf(stderr, "invalid attribute length, discarded\n");
return 0 ;
}
const char *payload = buf+pos+ATTR_HDR_LEN;
int payload_len = attr_len-ATTR_HDR_LEN;
if (attr_type == ATTR_USERNAME) {
if (payload_len > MAX_USERNAME_LEN) {
fprintf(stderr, "username too long, discarded\n");
return 0 ;
}
memcpy(username, payload, payload_len);
username[payload_len] = '\0';
// bonus: user name
if (user_count<MAX_USER_COUNT) {
strcpy(user_list[user_count], username);
user_count+=1 ;
}
}
else if (attr_type == ATTR_MESSAGE) {
if (payload_len > MAX_MESSAGE_LEN) {
fprintf(stderr, "Message too long, discarded\n");
return 0 ;
}
memcpy(message, payload, payload_len);
message[payload_len] = '\0';
}
//bnous, NAK
else if (attr_type == ATTR_REASON) {
if (payload_len > MAX_REASON_LEN) {
fprintf(stderr, "reason too long, discarded\n");
return 0 ;
}
memcpy(reason,payload,payload_len);
reason[payload_len]= '\0' ;
}
else if (attr_type == ATTR_CLIENT_COUNT) {
// client count 是 2 bytes 的數字,不是字串
if (payload_len!=2) {
fprintf(stderr, "invalid client count length, discarded\n");
return 0 ;
}
uint16_t raw;
memcpy(&raw,payload,2);
client_count = ntohs(raw);
}
//bonus nak
pos += attr_len;
}
switch (type) {
case SBCP_MSG_FWD:
if(username[0] == '\0'||message[0]=='\0'){
fprintf(stderr, "Invalid format, either empty message or username\n");
break;
}
printf("%s: %s\n", username, message);
break;
case SBCP_MSG_ONLINE:
if(username[0] == '\0'){
fprintf(stderr, "empty username\n");
break;
}
printf("%s is online\n", username);
break;
case SBCP_MSG_OFFLINE:
if(username[0] == '\0'){
fprintf(stderr, "empty username\n");
break;
}
printf("%s is offline\n", username);
break;
case SBCP_MSG_IDLE:
if(username[0] == '\0'){
fprintf(stderr, "empty username\n");
break;
}
printf("%s is idle\n", username);
break;
case SBCP_MSG_ACK:
if (message[0]!='\0') {
printf("%s\n", message);
break;
}
printf("Joined! %d client(s) online: \n", client_count);
if(client_count==1){
printf("You are the only user.\n");
break;
}
printf("===========Server users list===========\n");
for(int i=0; i < user_count ; i++){
printf("user %d: %s\n",i+1,user_list[i]) ;
}
printf("===========Users list ends===========\n");
break;
case SBCP_MSG_NAK:
if (reason[0]=='\0') {
printf("Join rejected by server\n");
} else {
printf("Join rejected: %s\n", reason);
}
return -1;
break;
default:
fprintf(stderr, "unknown message type %d, ignored\n", type);
break;
}
return 0;
}
//bonus ipv6
int connect_to_server_ipv6(const char *host, const char *port) {
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC; //support both ipv4 and ipv6
hints.ai_socktype = SOCK_STREAM; //tcp
struct addrinfo *res;
int err = getaddrinfo(host,port,&hints,&res);
if (err != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(err));
return -1;
}
int sockfd = -1;
struct addrinfo *p;
for (p = res; p != NULL; p = p->ai_next) {
sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (sockfd < 0) {
continue;
}
if (connect(sockfd,p->ai_addr,p->ai_addrlen) == 0) {
break;
}
close(sockfd);
sockfd = -1;
}
freeaddrinfo(res);
if (sockfd < 0) {
fprintf(stderr, "could not connect to %s:%s\n", host, port);
return -1;
}
return sockfd;
}
//bonus ipv6
int connect_to_server(const char *ip, int port) {
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd==-1){
perror("socket");
return -1;
}
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(port);
if (inet_pton(AF_INET, ip, &servaddr.sin_addr) <= 0) {
fprintf(stderr, "Invalid IP: %s\n", ip);
close(sockfd);
return -1;
}
//establish connection
int connectStatus = connect(
sockfd,
reinterpret_cast<sockaddr*>(&servaddr),
sizeof(servaddr)
);
if (connectStatus < 0 ){
perror("connect");
close(sockfd);
return -1 ;
} //connection fail
return sockfd;
}
int main(int argc, char **argv) {
//error handle
signal(SIGPIPE, SIG_IGN);
// check valid usage
if(argc!=4){
fprintf(stderr, "Usage: %s <username> <server_ip> <server_port>\n", argv[0]);
return 1;
}
// build connection
const char* username = argv[1];
const char* server_ip = argv[2];
const char* server_port = argv[3];
int portNumber = atoi(server_port) ;
if(portNumber>65535||portNumber<1){
fprintf(stderr, "Invalid port number\n");
return 1;
}
//int sockfd = connect_to_server(server_ip,portNumber);
int sockfd = connect_to_server_ipv6(server_ip,server_port);
if(sockfd<0){
fprintf(stderr, "Connection Fail\n");
return 1 ;
}
// join the server
char buf[MAX_BUF];
int len = build_join(buf,sizeof(buf),username);
if(len<0){
close(sockfd);
return 1;
}
int sent = send_all(sockfd,buf,len);
if(sent<0){
close(sockfd);
return 1 ;
}
//bonus: time out
time_t last_active = time(NULL); // JOIN 完成的時間,當作第一次活動
bool idle_sent = false;
//select between listen and send
while (1) {
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);
FD_SET(sockfd, &readfds);
//bonus: timeout
struct timeval tv;
struct timeval *tvp = NULL;
if (!idle_sent) {
int remaining = IDLE_TIMEOUT_SEC-(time(NULL)-last_active);
if (remaining < 0) {
remaining = 0;
}
tv.tv_sec = remaining;
tv.tv_usec = 0;
tvp = &tv;
}
//bonus: timeout end
int maxfd = sockfd;
int ready = select(maxfd + 1, &readfds, NULL, NULL, tvp);
if(ready==-1){
if(errno==EINTR){
continue;
}
perror("select");
close(sockfd);
return 1;
}
//bonus timeout
//send idle
if (ready == 0) {
int idle_len = build_idle(buf,sizeof(buf));
if (idle_len < 0) {
close(sockfd);
return 1;
}
if (send_all(sockfd,buf,idle_len) < 0) {
close(sockfd);
return 1;
}
idle_sent = true;
continue; // 沒有 fd 就緒,直接進入下一輪
}
//bonus timeout end
// message ready to send
if (FD_ISSET(STDIN_FILENO, &readfds)) {
char line[MAX_MESSAGE_LEN + 2] ;
if (fgets(line, sizeof(line), stdin) == NULL) {
fprintf(stderr,"EOF detect, disconnected from the server\n");
close(sockfd);
return 0 ;
}
int l = strlen(line);
if (l > 0 && line[l - 1] == '\n') {
line[l - 1] = '\0';
l--;
}
//empty input, enter next loop
if(l==0){
continue;
}
int build_len = build_send(buf,sizeof(buf),line,l);
if(build_len<0){
continue; //message build fail, wait for the next input
}
int sent = send_all(sockfd,buf,build_len);
if(sent<0){
close(sockfd);//message send fail, disconnect.
return 1;
}
last_active = time(NULL);
idle_sent = false;
}
// message arrive
if (FD_ISSET(sockfd, &readfds)) {
char rbuf[MAX_BUF];
int r = read(sockfd, rbuf, sizeof(rbuf));
if(r==0){
fprintf(stderr, "Server closed\n");
close(sockfd);
return 1 ;
}
if(r<0){
if(errno==EINTR){
continue;
}
perror("read");
close(sockfd);
return 1 ;
}
if(handle_server_message(rbuf,r)<0){
close(sockfd);
return 1 ;
}
}
}
close(sockfd);
return 0;
}