-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathtest.c
executable file
·133 lines (132 loc) · 3.6 KB
/
test.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
#include "function.h"
#include <pcap.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int shmid = 0;
char* eth_Name = 0;
void getPacket(u_char *arg, const struct pcap_pkthdr *pkthdr, const u_char *packet)
{
sem_wait(&(Share.Q_state));
Q_Push(&(Share.Msg), (void *)packet);
sem_post(&(Share.Contains));
sem_post(&(Share.Q_state));
}
void Main_Process()
{
int status;
u_char i = 0;
pthread_t tid = 0;
unsigned char *packet;
char errbuff[PCAP_ERRBUF_SIZE];
extern void *pShm;
pcap_t *device = pcap_open_live(eth_Name, 8000, 1, 0, errbuff);
if (!device)
{
printf("error: pcap_open_live(): %s\n", errbuff);
exit(1);
}
if (pcap_set_rfmon(device, 1) == 0)
{
printf("can't enter rfmode\n");
exit(1);
}
get_mac();
PT_init(&Share);
Init_thread_Share();
if ((pShm = shmat(shmid, 0, 0)) <= 0)
{
printf("Monitoring Process Failed!\r\n");
exit(1);
}
pthread_create(&tid, NULL, Analysis_Data, NULL);
pthread_create(&tid, NULL, Analysis_Data, NULL);
pthread_create(&tid, NULL, Timer, NULL);
pthread_create(&tid, NULL, Sending_To_Server, NULL);
//printf("Scaning starts\n");
pcap_loop(device, -1, getPacket, &i); //Scaning starts
pcap_close(device);
}
void Create_Monitor_Process()
{
#define MemBase Share_Mem
#define _MemFlag *((char *)MemBase)
#define _Belonging *((char *)MemBase + 1)
#define _Program_Exit *((char *)MemBase + 2)
pid_t sub_Process = 0;
void *Share_Mem = NULL;
sub_Process = fork();
if (sub_Process == 0) //child process
{
if ((Share_Mem = shmat(shmid, 0, 0)) <= 0)
{
printf("Monitoring Process Failed!\r\n");
exit(1);
}
while (1)
{
sleep_m(5, 0);
if (_Program_Exit == Program_Exit)
{
printf("Monition Thread Exit.Stop Scanning.\r\n");
exit(1);
}
if( _MemFlag <= -1)
{
printf("Main Process is Dead.\r\n");
sub_Process = fork();
if(_Belonging == Montion_Create_Main)
{
wait(NULL);
}
else
{
_Belonging = Montion_Create_Main; //Monitor Create Main
}
if (sub_Process == 0) //child process(main)
{
break;
}
}
else
{
_MemFlag = _MemFlag - 1;
}
}
Main_Process(eth_Name);
}
else if (sub_Process < 0)
{
exit(1);
}
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
printf("usage:./name <wlan name>\n");
exit(1);
}
if (fork() == 0)
{
void *Share_Mem = NULL;
if ((shmid = shmget(0, 4, 0666 | IPC_CREAT)) == -1)
{
printf("Share_Mem_Create_Failed!\r\n");
exit(1);
}
if ((Share_Mem = shmat(shmid, 0, 0)) <= 0)
{
printf("Monitoring Process Failed!\r\n");
exit(1);
}
*((char *)Share_Mem + 1) = Main_Create_Montion;//Main Create Monitor
*((char *)Share_Mem + 2) = Program_Running;//Running
eth_Name = (char *)malloc(strlen(argv[1]) + 1);
memcpy(eth_Name, argv[1], strlen(argv[1]) + 1);
Create_Monitor_Process();
Main_Process();
}
return 0;
}