-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatter.c
111 lines (98 loc) · 2.06 KB
/
chatter.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
#include "knockModule.h"
#include "ipScanModule.h"
#include "listener.h"
#include "sender.h"
#include "userlist.h"
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <pthread.h>
char * getFullLine(void) {
char * line = malloc(100), * linep = line;
size_t lenmax = 100, len = lenmax;
int c;
if(line == NULL)
return NULL;
for(;;) {
c = fgetc(stdin);
if(c == EOF)
break;
if(--len == 0) {
len = lenmax;
char * linen = realloc(linep, lenmax *= 2);
if(linen == NULL) {
free(linep);
return NULL;
}
line = linen + (line - linep);
linep = linen;
}
if((*line++ = c) == '\n')
break;
}
*line = '\0';
return linep;
}
void printhelp()
{
printf(":s\n\tscan all network to get online users.\n");
printf(":l\n\tprint all recorded users on the LAN.\n");
printf(":q\n\tterminate the program.\n");
printf(":h\n\tenter the help menu.\n");
printf("<username> <message>: send one line message to recorded username.\n");
}
int main( int argc, const char* argv[] )
{
pthread_t tID;
//process the knocking listener
pthread_create(&tID,NULL,(void *)knock,NULL);
pthread_create(&tID,NULL,(void *)listener,NULL);
//process scaning
ipScan();
char username[50];
char temp[50];
printf("Welcome\n>");
int c;
while(1)
{
c = fgetc(stdin);
if(c == ':')
{
c = fgetc(stdin);
if(c == 'q')
break;//quit
else if(c == 's')
ipScan();//Scan
else if(c == 'h')
printhelp();
else if(c == 'l')
printAll();
else
printf("There is no such that command\n");
c= fgetc(stdin);
}
else if(c == '\n')
{
//do nothing
}
else
{
scanf("%s",temp);
username[0] = c;
username[1] = 0;
strcat(username, temp);
char * IP = findUser(username);
//find username
if( IP != NULL)
{
char * msg = getFullLine();
if(msg != NULL)
sender(IP, msg);
}
else
printf("404 NOT FOUND\n");
}
printf(">");
};
return 0;
}