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
|
#include <stdio.h>
#define IPSTRLEN 20
int
ip_str_to_ip_tuple(char ip_str[], int ip_tuple[])
{
int retval;
if (4!=(retval=sscanf(ip_str, "%d.%d.%d.%d",
&ip_tuple[0], &ip_tuple[1], &ip_tuple[2], &ip_tuple[3]))) {
fprintf(stderr, "Error: sscanf failed, retval was: %d\n", retval);
return retval;
}
return 0;
}
int
ip_tuple_to_ip_str(int ip_tuple[], char ip_str[], int ip_str_size)
{
int retval;
if (IPSTRLEN<(retval=snprintf(ip_str, ip_str_size, "%d.%d.%d.%d",
ip_tuple[3], ip_tuple[2], ip_tuple[1], ip_tuple[0]))) {
return retval;
}
return 0;
}
unsigned long
ip_tuple_to_addr(int ip_tuple[])
{
unsigned long result=0;
for (int i = 0; i < 4; i += 1) {
result = result<<8;
result = ip_tuple[i]|result;
}
return result;
}
int
addr_to_ip_tuple(unsigned long addr, int ip_tuple[])
{
for (int i = 0; i < 4; i += 1) {
ip_tuple[i] = (addr>>i*8)&255;
}
return 0;
}
unsigned long
compute_netmask_for_size(int net_size)
{
unsigned long netmask = 0xffffffff;
netmask = netmask<<net_size;
return netmask;
}
unsigned long
compute_network_address(unsigned long host_addr, unsigned long netmask)
{
unsigned long net_addr;
net_addr = host_addr&netmask;
return net_addr;
}
unsigned long
computer_router_address(unsigned long host_addr, unsigned long netmask)
{
unsigned long router_addr;
router_addr = compute_network_address(host_addr, netmask)+1;
return router_addr;
}
int
addr_to_ip_str(unsigned long addr, char ip_str[], int ip_str_size)
{
int ip_tuple[4];
addr_to_ip_tuple(addr, ip_tuple);
if (ip_tuple_to_ip_str(ip_tuple, ip_str, ip_str_size)) {
fprintf(stderr, "Error: IP Address malformed.\n");
}
return 0;
}
int
main(void)
{
char the_ip[]="147.142.207.19", new_ip[IPSTRLEN];
int net_size=8;
int ip_tuple[4];
unsigned long ip_addr;
if (ip_str_to_ip_tuple(the_ip, ip_tuple)) {
fprintf(stderr, "Invalid IP address: %s\n", the_ip);
return 1;
}
ip_addr = ip_tuple_to_addr(ip_tuple);
printf("IP Address: %lx\n", ip_addr);
if (addr_to_ip_str(compute_network_address(ip_addr,
compute_netmask_for_size(net_size)), new_ip, IPSTRLEN)) {
fprintf(stderr, "Couldn't format network address.\n");
return 1;
}
printf("Network for this host (netmask %lx): %s\n",
compute_netmask_for_size(net_size), new_ip);
if (addr_to_ip_str(computer_router_address(ip_addr,
compute_netmask_for_size(net_size)), new_ip, IPSTRLEN)) {
fprintf(stderr, "Couldn't format router address.\n");
return 1;
}
printf("Probable router for this host (netmask %lx): %s\n",
compute_netmask_for_size(net_size), new_ip);
return 0;
}
|