-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.c
More file actions
437 lines (374 loc) · 16.1 KB
/
server.c
File metadata and controls
437 lines (374 loc) · 16.1 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
//#include <iostream>
//using namespace std;
// List of includes
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#define MAXBUF 1024
#define MAXHOSTNAME 200
#define MAXACTION 10
#define MAXPORT 6
struct eventQ{
char Cname[MAXHOSTNAME];
int Cport;
char Caction[MAXACTION];
time_t joined;
struct eventQ *next;
};
struct logQ{
char message[100];
struct logQ *next;
};
void logger(char lbuffer[100], struct logQ *Head);
int main(int argc,char* argv[]){
struct timeval tv;
struct eventQ *Head;
Head = (struct eventQ *) malloc (sizeof(struct eventQ));
Head -> next = NULL;
struct logQ *lHead;
lHead = (struct logQ *) malloc (sizeof(struct logQ));
lHead -> next = NULL;
char timeArr[30];
time_t millitime = 0;
int cont = 1, numBits;
int counter = 0, fd = 0, numConnected = 0;
socklen_t client;
char suppliedPort[MAXPORT], currClient[MAXHOSTNAME], buffer[MAXBUF], Action[MAXACTION], lbuffer[100], message[MAXBUF], milliray[MAXBUF];
sprintf(suppliedPort,"%s",argv[1]);
int port = atoi(argv[1]);
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0){ printf("ERROR establishing socket \n"); return 0;}
struct sockaddr_in my_addr;
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(port);
my_addr.sin_addr.s_addr = INADDR_ANY;
struct sockaddr_in clientInfo;
client = sizeof(clientInfo);
if (bind(fd, (struct sockaddr *) &my_addr, sizeof(my_addr)) < 0) {printf("Binding error (45)\n"); return 0;}
if (listen(fd, MAXACTION) < 0) {printf("ERROR lsitening \n"); return 0;}
while (cont == 1) {//infinite loop so we can take in requests
int currConnfd = accept(fd, (struct sockaddr*) &clientInfo, &client);
if (currConnfd < 0) {printf("accepting error\n"); return 0;}
numBits = read(currConnfd, buffer, MAXBUF);
if (numBits < 0) {printf("reading error\n"); return 0;}
//get client ip and store in currClient
inet_ntop(AF_INET, &clientInfo.sin_addr.s_addr, currClient, sizeof(currClient));
strcpy(Action, buffer);
//creating log string
memset(lbuffer, 0, sizeof(lbuffer));
//getting cur time
gettimeofday(&tv, NULL);
millitime = tv.tv_sec;
strftime(timeArr, 30, "%m-%d-%Y %T.", (const void *)localtime(&millitime));
memset(milliray, 0, sizeof(milliray));
sprintf(milliray, "%d", tv.tv_usec);
strcpy(lbuffer, "\"");
strcat(lbuffer, timeArr);
strcat(lbuffer, ".");
strcat(lbuffer, milliray);
strcat(lbuffer,"\": Recieved a \"#");
strcat(lbuffer, Action);
strcat(lbuffer, "\" action from agent \"");
strcat(lbuffer, currClient);
strcat(lbuffer, "\"\n");
logger(lbuffer, lHead);
if (strcmp(Action, "JOIN") == 0) {
int isHere = 0;
struct eventQ *search;
search = (struct eventQ *) malloc (sizeof(struct eventQ));
search = Head;
while(search -> next != NULL && isHere == 0) {
search = search -> next;
if (strcmp(search -> Cname, currClient) == 0) {
strcpy(message, "$");
strcat(message, "ERROR: ALREADY MEMBER");
printf("%s\n", message);
write(currConnfd, message, strlen(message));//works
memset(message, 0, sizeof(message));
memset(lbuffer, 0, sizeof(lbuffer));
//getting cur time
gettimeofday(&tv, NULL);
millitime = tv.tv_sec;
strftime(timeArr, 30, "%m-%d-%Y %T.", localtime(&millitime));
memset(milliray, 0, sizeof(milliray));
sprintf(milliray, "%d", tv.tv_usec);
strcpy(lbuffer, "\"");
strcat(lbuffer, timeArr);
strcat(lbuffer, ".");
strcat(lbuffer, milliray);
strcat(lbuffer, "\": Responded to agent \"");
strcat(lbuffer, currClient);
strcat(lbuffer, "\" with \"$ERROR: ALREADY MEMBER\"\n");
logger(lbuffer, lHead);
isHere = 1;
}
}
if (isHere == 0) {
struct eventQ *new;
new = (struct eventQ *) malloc (sizeof(struct eventQ));
struct eventQ *search;
search =(struct eventQ *) malloc (sizeof(struct eventQ));
search = Head;
while (search -> next != NULL) {
search = search -> next;
}
search -> next = new;
new -> next = NULL;
int i = 0;
for(i = 0; i < sizeof(currClient); i++){
new -> Cname[i] = currClient[i];
}
for (i = 0; i < sizeof(Action); i++) {
new -> Caction[i] = Action[i];
}
new -> joined = time(0);
printf("%cOK\n",36);
strcpy(message, "$");
strcat(message, "OK");
write(currConnfd, message, strlen(message)); // works
memset(lbuffer, 0, sizeof(lbuffer));
//getting cur time
gettimeofday(&tv, NULL);
millitime = tv.tv_sec;
strftime(timeArr, 30, "%m-%d-%Y %T.", localtime(&millitime));
memset(milliray, 0, sizeof(milliray));
sprintf(milliray, "%d", tv.tv_usec);
strcpy(lbuffer, "\"");
strcat(lbuffer, timeArr);
strcat(lbuffer, ".");
strcat(lbuffer, milliray);
strcat(lbuffer, "\": Responded to agent \"");
strcat(lbuffer, currClient);
strcat(lbuffer, "\" with \"$OK\"\n");
logger(lbuffer, lHead);
}
}
if (strcmp(Action, "LEAVE") == 0) {
int isHere = 0;
struct eventQ *search;
search = (struct eventQ *) malloc (sizeof(struct eventQ));
struct eventQ *trailer;
trailer = (struct eventQ *) malloc (sizeof(struct eventQ));
search = Head;
trailer = Head;
while(search -> next != NULL && isHere == 0) {
search = search -> next;
if (strcmp(search -> Cname, currClient) == 0) {
isHere = 1;
trailer -> next = search -> next;
if(search != Head) free(search);
printf("%cOK\n",36);
memset(message, 0, sizeof(message));
strcpy(message, "$");
strcat(message, "OK");
write(currConnfd, message, 3);
memset(lbuffer, 0, sizeof(lbuffer));
//getting cur time
gettimeofday(&tv, NULL);
millitime = tv.tv_sec;
strftime(timeArr, 30, "%m-%d-%Y %T.", localtime(&millitime));
memset(milliray, 0, sizeof(milliray));
sprintf(milliray, "%d", tv.tv_usec);
strcpy(lbuffer, "\"");
strcat(lbuffer, timeArr);
strcat(lbuffer, ".");
strcat(lbuffer, milliray);
strcat(lbuffer, "\": Responded to agent \"");
strcat(lbuffer, currClient);
strcat(lbuffer, "\" with \"$OK\"\n");
logger(lbuffer, lHead);
}
search = trailer;
trailer = trailer -> next;
}
if (isHere == 0){
printf("$ERROR: NOT MEMBER\n");
memset(message, 0, sizeof(message));
strcpy(message, "$");
strcat(message, "ERROR: NOT MEMBER");
write(currConnfd, message, strlen(message));
memset(lbuffer, 0, sizeof(lbuffer));
memset(timeArr, 0, sizeof(timeArr));
//getting cur time
gettimeofday(&tv, NULL);
millitime = tv.tv_sec;
strftime(timeArr, 30, "%m-%d-%Y %T.", localtime(&millitime));
memset(milliray, 0, sizeof(milliray));
sprintf(milliray, "%d", tv.tv_usec);
strcpy(lbuffer, "\"");
strcat(lbuffer, timeArr);
strcat(lbuffer, ".");
strcat(lbuffer, milliray);
strcat(lbuffer, "\": Responded to agent \"");
strcat(lbuffer, currClient);
strcat(lbuffer, "\" with \"$ERROR: NOT MEMBER\"\n");
logger(lbuffer, lHead);
}
}
if (strcmp(Action, "LIST") == 0) {
struct eventQ *search;
search = (struct eventQ *) malloc (sizeof(struct eventQ));
search = Head;
int found = 0;
while (search -> next != NULL) {
search = search -> next;
if (strcmp(search -> Cname, currClient) == 0) found = 1;
}
if (found == 0) {
memset(lbuffer, 0, sizeof(lbuffer));
//getting cur time
gettimeofday(&tv, NULL);
millitime = tv.tv_sec;
strftime(timeArr, 30, "%m-%d-%Y %T.", localtime(&millitime));
memset(milliray, 0, sizeof(milliray));
sprintf(milliray, "%d", tv.tv_usec);
strcpy(lbuffer, "\"");
strcat(lbuffer, timeArr);
strcat(lbuffer, ".");
strcat(lbuffer, milliray);
strcat(lbuffer, "\": no response is supplied to agent \"");
strcat(lbuffer, currClient);
strcat(lbuffer, "\" (agent not active)\n");
logger(lbuffer, lHead);
}
if (found == 1){
printf("$List of active agents\n");
search = Head;
while (search -> next != NULL){
search = search -> next;
printf("$ <%s, %ld>\n", search -> Cname, (time(0) - search -> joined));
memset(message, 0, sizeof(message));
strcpy(message, "$ <");
strcat(message, search -> Cname);
strcat(message, ", ");
char numarry[MAXBUF];
sprintf(numarry, "%ld", time(0) - search -> joined);
strcat(message, numarry);
strcat(message, ">\n");
write(currConnfd, message, strlen(message));
}
memset(lbuffer, 0, sizeof(lbuffer));
//getting cur time
gettimeofday(&tv, NULL);
millitime = tv.tv_sec;
strftime(timeArr, 30, "%m-%d-%Y %T.", localtime(&millitime));
memset(milliray, 0, sizeof(milliray));
sprintf(milliray, "%d", tv.tv_usec);
strcpy(lbuffer, "\"");
strcat(lbuffer, timeArr);
strcat(lbuffer, ".");
strcat(lbuffer, milliray);
strcat(lbuffer, "\": Responded to agent \"");
strcat(lbuffer, currClient);
strcat(lbuffer, "\" with \"$List of active agents\n\"\n");
logger(lbuffer, lHead);
}
}
if (strcmp(Action, "LOG") == 0) {
int isHere = 0;
struct logQ * search;
search = (struct logQ *) malloc (sizeof(struct logQ));
struct eventQ *find;
find = (struct eventQ *) malloc (sizeof(struct eventQ));
find = Head;
while(find -> next != NULL && isHere == 0) {
find = find -> next;
if (strcmp(find -> Cname, currClient) == 0) {
isHere = 1; // isfound
}
}
if(isHere == 0){
memset(lbuffer, 0, sizeof(lbuffer));
//getting cur time
gettimeofday(&tv, NULL);
millitime = tv.tv_sec;
strftime(timeArr, 30, "%m-%d-%Y %T.", localtime(&millitime));
memset(milliray, 0, sizeof(milliray));
sprintf(milliray, "%d", tv.tv_usec);
strcpy(lbuffer, "\"");
strcat(lbuffer, timeArr);
strcat(lbuffer, ".");
strcat(lbuffer, milliray);
strcat(lbuffer, "\": no response is supplied to agent \"");
strcat(lbuffer, currClient);
strcat(lbuffer, "\" (agent not active)\n");
logger(lbuffer, lHead);
}
if (isHere == 1){
FILE *myfile = fopen("log.txt", "w");
printf("Printing Log File\n");
//getting cur time
gettimeofday(&tv, NULL);
millitime = tv.tv_sec;
strftime(timeArr, 30, "%m-%d-%Y %T.", localtime(&millitime));
memset(milliray, 0, sizeof(milliray));
sprintf(milliray, "%d", tv.tv_usec);
strcpy(lbuffer, "\"");
strcat(lbuffer, timeArr);
strcat(lbuffer, ".");
strcat(lbuffer, milliray);
strcat(lbuffer, "\": Responded to agent \"");
strcat(lbuffer, currClient);
strcat(lbuffer, "\" with \"$Log File\"\n");
logger(lbuffer, lHead);
search = lHead;
while(search -> next != NULL){
search = search -> next;
printf(" %s\n", search -> message);
fprintf(myfile, "%s\n", search -> message);
memset(message, 0, sizeof(message));
strcpy(message, "$");
strcat(message, search -> message);
write(currConnfd, message, strlen(message));
}
fclose(myfile);
}
}
memset(currClient, 0, sizeof(currClient)); // clearing these three for next iteration
memset(Action, 0, sizeof(Action));
memset(buffer, 0, sizeof(buffer));
close(currConnfd);
}
}
void logger(char lbuffer[100], struct logQ *lHead){
//printf("The contents of the buffer in logger: %s", lbuffer);
struct logQ *newEntry;
newEntry = (struct logQ *) malloc (sizeof(struct logQ));
struct logQ *search;
search = (struct logQ *) malloc (sizeof(struct logQ));
search = lHead;
while (search -> next != NULL) {
search = search -> next;
}
search -> next = newEntry;
newEntry -> next = NULL;
strcpy(newEntry -> message, lbuffer);
}
//CONNECT
// establishes a connection with a server
// int connect(int sockfd, struct sockaddr * name, socklen_t namelen);
//BIND
// binds a socket (sockfd) to an address and a port
// int bind(int sockfd, struct sockaddr * my_addr, socklen_t addrlen);
//SOCKET
// returns a file descriptor to be used for subsequent calls & reutrns a -1 on error
// int socket (int domain, int type, int protocol);
//ACCEPT
// blocks server until client connects and returns new file descriptor to read a write with client
// int accept (int sockfd, struct sockaddr * addr, socklen_t *addrlen)
//READ
// try to read count into fifer from fd, retunrs bytes written or -1 on error (0 dentoes end of file)
// int read(int fd, void * buf, int count);
//WRITE
// writes up to count into fd from buffer and returns num bites written
// int write( int fd, const void * buffer, int count);
//LISTEN
// creates a queue and allows the process to listen
// int listen(int fd, int queue size);