-
Notifications
You must be signed in to change notification settings - Fork 0
/
mask.c
67 lines (49 loc) · 1.19 KB
/
mask.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
#include<stdio.h>
#include<string.h>
#include<netdb.h>
#include"cgiaudit.h"
/*
* bit twiddling operations yanked from nmap and adopted to cgiaudit
*
* - super
*/
/* just a driver for testing */
/* int main(void)
{
struct hostent*he;
options.v=1;
he=gethostbyname("www.lame.org");
setmask(16,he->h_addr);
if(mask("www.lame.org"))
puts("success");
else
puts("failure");
return 0;
} */
static unsigned int netbits=24;
static struct in_addr orig_addr,start_addr,end_addr;
int setmask(int m,struct in_addr*new_addr)
{
if(m>32||m<0)
return 0;
netbits=m;
orig_addr.s_addr=ntohl(new_addr->s_addr);
start_addr.s_addr = orig_addr.s_addr & (unsigned long) (0 - (1<<(32 - netbits)));
end_addr.s_addr = orig_addr.s_addr | (unsigned long) ((1<<(32 - netbits)) - 1);
return 1;
}
/* ipv4 netmask test */
int mask(char*host)
{
struct in_addr test_addr;
struct hostent*he;
he=gethostbyname(host);
if(!he)
{
if(options.v)
fprintf(stderr,"[%d.%d] Unable to resolve host (%s) referenced by A tag.\n",1,1,host);
return 0;
}
test_addr.s_addr=ntohl((*((struct in_addr*)he->h_addr)).s_addr);
return test_addr.s_addr>=start_addr.s_addr&&test_addr.s_addr<=end_addr.s_addr;
}