-
Notifications
You must be signed in to change notification settings - Fork 0
/
cclient.c
457 lines (368 loc) · 9.87 KB
/
cclient.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
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <strings.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include "cclient.h"
#include "server.h"
#define MAXBUF 4000
#define INSTBUF 3
/**
* By Ryan McKinney
* TCP Chat Client
*/
// This clients handle
char* handle;
void getInput(int socket_num) {
char inst[MAXBUF];
fd_set fds;
do {
FD_ZERO(&fds);
FD_SET(socket_num, &fds);
FD_SET(STDIN_FILENO, &fds);
printf("$: ");
fflush(stdout);
if (select(socket_num+1, &fds, NULL, NULL, NULL) < 0) {
perror("select call");
}
// Check for user instruction
if (FD_ISSET(STDIN_FILENO, &fds)) {
fgets(inst, MAXBUF, stdin);
if (inst[0] != '%') {
printf("Invalid command\n");
}
else {
switch(inst[1]) {
case 'H':
case 'h':
// Help
printHelp();
break;
case 'M':
case 'm':
// Send message
sendMsg(socket_num, inst);
break;
case 'B':
case 'b':
// Broadcast message
sendBCast(socket_num, inst);
break;
case 'L':
case 'l':
// List Handles
sendListReq(socket_num);
break;
case 'E':
case 'e':
// Exit
sendExit(socket_num);
break;
default:
printf("Invalid command\n");
break;
}
}
}
// Check for incoming packet
if (FD_ISSET(socket_num, &fds)) {
receivePacket(socket_num);
}
} while (1);
}
void sendInitial(int socket_num) {
struct c_header header;
header.flag = F_INIT;
u_short handleLen = getLength(handle);
header.length = sizeof(struct c_header) + handleLen;
// Send F_INIT to server
char *packet = malloc(header.length);
memcpy(packet, &header, sizeof(struct c_header));
memcpy(packet + sizeof(struct c_header), handle, handleLen);
send(socket_num, packet, MAXBUF, 0);
// Wait for F_INIT_GOOD response from server
char buffer[MAXBUF];
c_header *rcvHeader;
do {
recv(socket_num, buffer, MAXBUF, MSG_WAITALL);
rcvHeader = (c_header*)buffer;
if (rcvHeader->flag == F_INIT_BAD) {
printf("Handle already in use: %s\n", handle);
exit(-1);
}
} while (rcvHeader->flag != F_INIT_GOOD);
return;
}
void sendMsg(int socket_num, char inst[]) {
short packetLength = sizeof(c_header);
char* destHandles[MAXHNDL];
u_char destHandleLens[MAXHNDL];
// Construct packet header
struct c_header header;
header.flag = F_MSG;
// Get client handle length
u_char cHandleLen = (u_char)getLength(handle);
packetLength += cHandleLen + sizeof(u_char);
// Parse instruction
strtok(inst, " ");
char* nextInst = strtok(NULL, " ");
if (nextInst == NULL) {
printf("Invalid command\n");
return;
}
u_char numHandles = atoi(nextInst);
packetLength += sizeof(u_char);
if (numHandles == 0) {
// No handle num option given
numHandles = 1;
destHandleLens[0] = getLength(nextInst);
packetLength += destHandleLens[0] + sizeof(u_char);
destHandles[0] = nextInst;
}
else {
int i;
for (i=0; i < numHandles; i++) {
nextInst = strtok(NULL, " ");
if (nextInst == NULL) {
printf("Invalid command\n");
return;
}
destHandleLens[i] = getLength(nextInst);
destHandles[i] = nextInst;
packetLength += destHandleLens[i] + sizeof(u_char);
}
}
// Get text of the message
char* text = strtok(NULL, "\0");
if (text == NULL) {
printf("Invalid command\n");
return;
}
packetLength += getLength(text);
header.length = packetLength;
// Construct message packet
char* packet = createMsgPacket(packetLength, destHandles, destHandleLens,
text, numHandles);
send(socket_num, packet, MAXBUF, 0);
}
void sendBCast(int socket_num, char inst[]) {
strtok(inst, " ");
char *text = strtok(NULL, "\0");
char *packet = malloc(MAXBUF);
char *current = packet + sizeof(c_header);
// Construct header
c_header *header = (c_header*)packet;
header->flag = F_BCAST;
// Construct sender handle
u_char cLength = getLength(handle);
memcpy(current, &cLength, sizeof(u_char));
current += sizeof(u_char);
memcpy(current, handle, cLength);
current += cLength;
header->length = sizeof(c_header) + sizeof(u_char) + cLength + getLength(text);
memcpy(current, text, getLength(text));
send(socket_num, packet, MAXBUF, 0);
}
void sendExit(int socket_num) {
char *packet = malloc(MAXBUF);
// Send exit packet to server
c_header *header = (c_header*)packet;
header->flag = F_EXIT;
header->length = sizeof(c_header);
send(socket_num, packet, MAXBUF, 0);
// Wait for ACK from server
recv(socket_num, packet, MAXBUF, MSG_WAITALL);
while (header->flag != F_ACK) {
recv(socket_num, packet, MAXBUF, MSG_WAITALL);
}
// Exit client
exit(0);
}
void sendListReq(int socket_num) {
// Send list request flag to server
c_header *header = malloc(sizeof(c_header));
header->flag = F_LIST;
header->length = sizeof(c_header);
send(socket_num, header, MAXBUF, 0);
receiveList(socket_num);
}
char* createMsgPacket(short length, char* destHandles[], u_char handleLens[],
char* text, u_char numHandles) {
char* packet = malloc(MAXBUF);
char* current = packet;
// Construct packet header
c_header header;
header.flag = F_MSG;
header.length = length;
memcpy(current, &header, sizeof(c_header));
current += sizeof(c_header);
// Construct sender handle section
u_char cLength = getLength(handle);
memcpy(current, &cLength, sizeof(u_char));
current += sizeof(u_char);
memcpy(current, handle, cLength);
current += cLength;
memcpy(current, &numHandles, sizeof(u_char));
current += sizeof(u_char);
// Construct destination handle sections
int i;
for (i=0; i < numHandles; i++) {
u_char dLength = handleLens[i];
memcpy(current, &dLength, sizeof(u_char));
current += sizeof(u_char);
memcpy(current, destHandles[i], dLength);
current += dLength;
}
// Construct text section
memcpy(current, text, getLength(text));
return packet;
}
void receiveList(int socket_num) {
char packet[MAXBUF];
// Receive inc packet rom server
recv(socket_num, packet, MAXBUF, MSG_WAITALL);
c_header *header = (c_header*) packet;
while (header->flag != F_INC) {
recv(socket_num, packet, MAXBUF, MSG_WAITALL);
}
int numIncHandles = *(packet + sizeof(c_header));
printf("Number of clients: %d\n", numIncHandles);
// Receive rest of packets
while (numIncHandles--) {
recv(socket_num, packet, MAXBUF, MSG_WAITALL);
u_char curLength = *(packet + sizeof(c_header));
printf(" %.*s\n", curLength, packet+sizeof(c_header)+sizeof(u_char));
}
}
void receiveBCast(char* packet) {
c_header *header = (c_header*) packet;
char *current = packet + sizeof(c_header);
u_char cLength = *(current);
current += sizeof(u_char);
printf("\n%.*s: ", cLength, current);
current += cLength;
printf("%.*s", (int)(header->length - sizeof(c_header) - sizeof(u_char) -
cLength), current);
free(packet);
}
void receiveMsg(char* packet) {
char *inc_handle;
u_char inc_handle_len, cur_handle_len;
c_header *header = (c_header*)packet;
int ofs = sizeof(c_header);
// Get incoming handle
inc_handle_len = *(packet + ofs);
ofs += sizeof(u_char);
inc_handle = packet + ofs;
ofs += inc_handle_len;
// Skip destination handles
u_char numHandles = *(packet + ofs);
ofs += sizeof(u_char);
int i;
for (i=0; i < numHandles; i++) {
cur_handle_len = *(packet + ofs);
ofs += sizeof(u_char) + cur_handle_len;
}
int textLength = header->length - ofs;
printf("\n%.*s: ", inc_handle_len, inc_handle);
printf("%.*s", textLength, packet + ofs);
}
void receiveError(char* packet) {
u_char handleLength;
c_header *header = (c_header*)packet;
handleLength = header->length - sizeof(c_header) - sizeof(u_char);
printf("\nClient with handle %.*s does not exist\n", handleLength,
packet+sizeof(c_header)+sizeof(u_char));
}
void receivePacket(int inc_socket) {
char *packet = malloc(MAXBUF);
recv(inc_socket, packet, MAXBUF, MSG_WAITALL);
c_header *header = (c_header*)packet;
switch(header->flag) {
case F_MSG:
receiveMsg(packet);
break;
case F_BCAST:
receiveBCast(packet);
break;
case F_ERROR:
receiveError(packet);
break;
default:
printf("\nServer Terminated\n");
exit(-1);
}
}
int getLengthInput(char* data) {
int length = 0;
while (*data != '\n') {
data++;
length++;
}
return length;
}
u_char getLength(char* data) {
if (data == NULL) {
return 0;
}
u_char length = 0;
while (*data != '\0') {
data++;
length++;
}
return length;
}
int setupClient(char *host_name, char *port) {
int socket_num;
struct sockaddr_in remote; // socket address for remote side
struct hostent *hp;
// create the socket
if ((socket_num = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket call");
exit(-1);
}
remote.sin_family= AF_INET;
// get the address of the remote host and store
if ((hp = gethostbyname(host_name)) == NULL) {
printf("Error getting hostname: %s\n", host_name);
exit(-1);
}
memcpy((char*)&remote.sin_addr, (char*)hp->h_addr, hp->h_length);
// get the port used on the remote side and store
remote.sin_port= htons(atoi(port));
if(connect(socket_num, (struct sockaddr*)&remote,
sizeof(struct sockaddr_in)) < 0) {
perror("connect call");
exit(-1);
}
return socket_num;
}
void printHelp() {
printf("\n Message one client:\n %%M destination-handle {message}\n");
printf(" Message multiple clients:\n %%M num-handles dest-handle-1 dest-handle-2 ... {message}\n");
printf(" Broadcast a message:\n %%B {message}\n");
printf(" List clients in chat\n %%L\n");
printf(" Exit:\n %%E\n");
}
int main(int argc, char * argv[]) {
handle = argv[1];
if (argc != 4) {
printf("usage: %s handle host-name port-number \n", argv[0]);
exit(1);
}
// Set up the server
int socket_num = setupClient(argv[2], argv[3]);
sendInitial(socket_num);
printf("Type %%H for help\n");
// Get user input
getInput(socket_num);
return 0;
}