forked from InfiniteRegen/Network-Manage-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckIPAddress.c
More file actions
80 lines (66 loc) · 1.82 KB
/
checkIPAddress.c
File metadata and controls
80 lines (66 loc) · 1.82 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
#include "main.h"
#include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/* function that writes a detection log */
void LogIpDetection(char *fileName, char *logData, int isExit)
{
static FILE* fdWrite = NULL;
if(isExit == 1)
{
fclose(fdWrite);
fdWrite = NULL;
return;
}
if(fdWrite == NULL)
{
fdWrite = fopen(fileName, "a");
if(fdWrite == NULL)
{
perror("wrtie error:");
return;
}
}
fprintf(fdWrite, "%s\n", logData);
return;
}
/* function that detects specified ip address */
void checkIpAddress(unsigned char *pktData, char *targetAddr)
{
ip_header *iph = (ip_header *)(&pktData[14]);
struct sockaddr_in src, dst;
src.sin_addr.s_addr = iph->srcAddr;
dst.sin_addr.s_addr = iph->dstAddr;
char *srcAddr = inet_ntoa(src.sin_addr);
char *dstAddr = inet_ntoa(dst.sin_addr);
char logData[MAX_LOG_LENGTH];
// test
printf("\nsrcAddr:%s dstAddr:%s targetAddr:%s"
, srcAddr, dstAddr, targetAddr);
if( !strcmp(srcAddr, targetAddr) )
{
setcolor(RESET_BG);
printf("\n###=====>>>> Target address founed in srcAddr [%s]\n", srcAddr);
memset(logData, 0x00, sizeof(logData));
sprintf(logData, "source address:%s target address:%s", srcAddr, targetAddr);
LogIpDetection("testLog.log", logData, 0);
// test
//printf("\nsrcAddr:%s dstAddr:%s targetAddr:%s"
//, srcAddr, dstAddr, targetAddr);
}else if( !strcmp(dstAddr, targetAddr) )
{
setcolor(RESET_BG);
// test
//printf("\nsrcAddr:%s dstAddr:%s targetAddr:%s"
//, srcAddr, dstAddr, targetAddr);
memset(logData, 0x00, sizeof(logData));
sprintf(logData, "destination address:%s target address:%s", srcAddr, targetAddr);
printf("\n###=====>>>> Target address founed in dstAddr [%s]\n", dstAddr);
LogIpDetection("testLog.log", logData, 0);
}
return;
}