-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
354 lines (276 loc) · 7.85 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
/*Marwan Mostafa mam024 11305332*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <network.h>
/*Command line argument vars*/
char* hostname;
char* port;
char myname[13];
char* msgtype;
int date;
int starttime;
int endtime;
char Eventtitle[10];
/*packet to send*/
uint16_t packet[64];
/*packet recieved*/
uint16_t output[64];
/*This function formats the CMD args
* into a packet following the protocol*/
void format_packet(int msgtype, int arc)
{
int i;
uint16_t year;
uint16_t month;
uint16_t day;
uint16_t hour;
uint16_t min;
/*formating header*/
/*adding name to header in int*/
for (i = 0;i<13;i++){
packet[i] = (uint16_t) myname[i];
}
/*adding msgtype to header*/
packet[13] = msgtype;
/*formating data*/
switch (msgtype){
case 2:
/*add msgtype packet formatting*/
day = date % 100;
month = (date - day) % 10000;
year = (date - month - day) / 10000;
packet[14] = month;
packet[15] = day;
packet[16] = year;
min = starttime % 100;
hour = (starttime - min ) / 100;
packet[24] = hour;
packet[25] = min;
min = endtime % 100;
hour = (endtime - min ) / 100;
packet[34] = hour;
packet[35] = min;
for (i = 44;i<54;i++){
packet[i] = (uint16_t) Eventtitle[i-44];
}
break;
case 3:
/*remove msgtype packet formatting*/
day = date % 100;
month = (date - day) % 10000;
year = (date - month - day) / 10000;
packet[14] = month;
packet[15] = day;
packet[16] = year;
min = starttime % 100;
hour = (starttime - min ) / 100;
packet[24] = hour;
packet[25] = min;
break;
case 4:
/*update msgtype packet formatting*/
day = date % 100;
month = (date - day) % 10000;
year = (date - month - day) / 10000;
packet[14] = month;
packet[15] = day;
packet[16] = year;
min = starttime % 100;
hour = (starttime - min ) / 100;
packet[24] = hour;
packet[25] = min;
min = endtime % 100;
hour = (endtime - min ) / 100;
packet[34] = hour;
packet[35] = min;
for (i = 44;i<54;i++){
packet[i] = (uint16_t) Eventtitle[i-44];
}
break;
case 5:
/*get msgtype packet formatting*/
day = date % 100;
month = (date - day) % 10000;
year = (date - month - day) / 10000;
packet[14] = month;
packet[15] = day;
packet[16] = year;
if (arc == 7){
min = starttime % 100;
hour = (starttime - min ) / 100;
packet[24] = hour;
packet[25] = min;
packet[63] = 7;
}
break;
case 6:
packet[14] = 0;
break;
default:
fprintf(stderr,"non reconized msgtype");
}
}
/*this function prints the servers response to stdout*/
void display_output()
{
int i;
char msg[50];
network_to_host(output,64);
if (output[15] != 0){
for (i = 0;i<50;i++){
msg[i] = (char) output[i+14];
}
fprintf(stdout,"\n%s\n",msg);
}else{
fprintf(stdout,"\n%d\n",output[14]);
}
}
int main(int argc, char * argv[])
{
int s; /*Main socket*/
int c; /*connect return value*/
int bs; /*number of bytes sent*/
int br; /*bytes recieved*/
/*Vars for socket setup*/
int errno, rc;
struct hostent hbuf, *hp;
char tmp[8192];
struct in_addr **addr_list;
char *had; /*Host address*/
/*Vars for getaddrinfo*/
int status;
struct addrinfo hints;
struct addrinfo *res;
/*User must enter at least 4 args, up to msgtype*/
if (argc < 5) {
fprintf(stderr,"need minimum 4 args");
exit(1);
}
/*check for myname length*/
if (strlen(argv[3]) > 12){
fprintf(stderr,"max 12 chars for 3rd arg");
exit(1);
}
memset(myname,0,13);
memset(Eventtitle,0,10);
/*Saving cmd args into vars*/
hostname = argv[1];
port = argv[2];
strcpy(myname,argv[3]); /*risk of buffer overflow if len not checked*/
msgtype = argv[4];
/*getting server ip from hostname*/
rc = gethostbyname_r(hostname,
&hbuf,
tmp,
8192,
&hp,
&errno);
/*checking gethostbyname_r return value*/
if (rc != 0) {
printf("gethostbyname_r error: %s\n", hstrerror(errno));
exit(0);
}
/*Extracting ip*/
addr_list = (struct in_addr **)hp->h_addr_list;
had = inet_ntoa(*addr_list[0]);
/*setting up structs for getaddrinfo*/
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
/*calling getaddrinfo*/
status = getaddrinfo(had,port,&hints,&res);
/*checking return of getaddrinfo*/
if (status != 0){
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
exit(1);
}
/*setting up socket*/
s = socket(res->ai_family,res->ai_socktype,res->ai_protocol);
/*checking return of socket*/
if (s == -1){
fprintf(stderr,"socket error:");
exit(1);
}
/*connecting to host*/
c = connect(s,res->ai_addr,res->ai_addrlen);
/*checking connect return value*/
if (c == -1){
fprintf(stderr,"connection failed");
exit(1);
}
/*checking for msg type add*/
if (strcmp(msgtype,"add") == 0){
date = atoi(argv[5]);
starttime = atoi(argv[6]);
endtime = atoi(argv[7]);
/*check for eventname length*/
if (strlen(argv[8]) > 9){
fprintf(stderr,"max 9 chars for 8th arg");
exit(1);
}
strcpy(Eventtitle,argv[8]); /*buffer overflow risk*/
format_packet(2,argc); /*formating data into packet for sending*/
host_to_network(packet,64); /*converting packet to network order*/
}
/*checking for msg type remove*/
if (strcmp(msgtype,"remove") == 0){
date = atoi(argv[5]);
starttime = atoi(argv[6]);
format_packet(3,argc); /*formating data into packet for sending*/
host_to_network(packet,64); /*converting packet to network order*/
}
/*checking for msg type update*/
if (strcmp(msgtype,"update") == 0){
date = atoi(argv[5]);
starttime = atoi(argv[6]);
endtime = atoi(argv[7]);
/*check for eventname length*/
if (strlen(argv[8]) > 9){
fprintf(stderr,"max 9 chars for 8th arg");
exit(1);
}
strcpy(Eventtitle,argv[8]); /*buffer overflow risk*/
format_packet(4,argc); /*formating data into packet for sending*/
host_to_network(packet,64); /*converting packet to network order*/
}
/*checking for msg type get*/
if (strcmp(msgtype,"get") == 0){
date = atoi(argv[5]);
if (argc == 7){
starttime = atoi(argv[6]);
}
format_packet(5,argc); /*formating data into packet for sending*/
host_to_network(packet,64); /*converting packet to network order*/
}
/*checking for msg type getall*/
if (strcmp(msgtype,"getall") == 0){
format_packet(6,argc); /*formating data into packet for sending*/
host_to_network(packet,64); /*converting packet to network order*/
}
bs = send(s,packet,sizeof(uint16_t)*64,0); /*sending packet to server*/
/*checking if all bytes were sent or if an error occured*/
if (bs < 64 || bs == -1){
fprintf(stderr,"send failed");
}
/*waiting for response from server*/
br = recv(s,output,sizeof(uint16_t)*64,0);
/*checking if all bytes were recieved or if an error occured*/
if (br < 64 || br == -1){
fprintf(stderr,"recv failed");
}
/*checking if connection got closed*/
if (br == 0){
fprintf(stderr,"connection closed");
exit(1);
}
/*display server response*/
display_output();
freeaddrinfo(res);
return 0;
}