利用IP地址和子网掩码计算网络号和主机号

概述

在学习TCP/IP的时候一开始看到主机号不知道怎么来的,仔细一想是可以算出来的。

利用IP地址和子网掩码快速得出网络号和主机号实现代码

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
// a file named get-netid-subnetid-hostid.c

#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>

#define MAXBUF 8192 /* Max I/O buffer size */

int main(int argc, char *argv[])
{
int IPAddress, subnetMask,temp, hostID, calculHostID;
struct in_addr inaddrOne, inaddrTwo, inaddrThree; /* Address in network byte order */
struct in_addr inaddr;
int checkOne, checkTwo, checkThree;
char buf[MAXBUF];
if(argc != 3){
fprintf(stderr,"Usage: %s IP-address subnet-mask\n",argv[0]);
exit(1);
}

checkOne = inet_pton(AF_INET, argv[1], &inaddrOne);
checkTwo = inet_pton(AF_INET, argv[2], &inaddrTwo);

if (checkOne == 0){
printf("inet_pton error: %s is invalid dotted-decimal address\n",argv[1]);
fprintf(stderr,"The input max dotted-decimal should be 255.255.255.255\n");
exit(1);
}
else if (checkOne < 0){
printf("inet_pton error\n");
}

if (checkTwo == 0){
printf("inet_pton error: %s is invalid dotted-decimal address\n",argv[2]);
fprintf(stderr,"The input max dotted-decimal should be 255.255.255.255\n");
exit(1);
}
else if (checkTwo < 0){
printf("inet_pton error\n");
}

IPAddress = ntohl(inaddrOne.s_addr);
subnetMask = ntohl(inaddrTwo.s_addr);

temp = IPAddress & subnetMask;

inaddr.s_addr = htonl(temp);
if(!inet_ntop(AF_INET, &inaddr, buf, MAXBUF)){
printf("inet_ntop\n");
}
else{
printf("netID/subnetID: %s\n",buf);
}

checkThree = inet_pton(AF_INET, buf, &inaddrThree);
if (checkThree == 0){
printf("inet_pton error: invalid dotted-decimal address\n");
fprintf(stderr,"The input max dotted-decimal should be 255.255.255.255\n");
}
else if (checkThree < 0){
printf("inet_pton error\n");
}
calculHostID = ntohl(inaddrThree.s_addr);
hostID = IPAddress - calculHostID;
printf("hostID: %d\n",hostID);

return 0;
}

编译并运行:

cc -g -o get-netid-subnetid-hostid get-netid-subnetid-hostid.c
./get-netid-subnetid-hostid

使用样例:

haha@hello:~/.../lj$  ./get-netid-subnetid-hostid
Usage: ./get-netid-subnetid-hostid IP-address subnet-mask
haha@hello:~/.../lj$ ./get-netid-subnetid-hostid 140.252.1.29 255.255.255.0
netID/subnetID: 140.252.1.0
hostID: 29
haha@hello:~/.../lj$ ./get-netid-subnetid-hostid 140.252.13.33 255.255.255.224
netID/subnetID: 140.252.13.32
hostID: 1
haha@hello:~/.../lj$ ./get-netid-subnetid-hostid 140.252.13.34 255.255.255.224
netID/subnetID: 140.252.13.32
hostID: 2
haha@hello:~/.../lj$ ./get-netid-subnetid-hostid 140.252.13.35 255.255.255.224
netID/subnetID: 140.252.13.32
hostID: 3
haha@hello:~/.../lj$ ./get-netid-subnetid-hostid 140.252.13.66 255.255.255.224
netID/subnetID: 140.252.13.64
hostID: 2
haha@hello:~/.../lj$ ./get-netid-subnetid-hostid 140.252.13.65 255.255.255.224
netID/subnetID: 140.252.13.64
hostID: 1
haha@hello:~/.../lj$ ./get-netid-subnetid-hostid 140.252.13.63 255.255.255.224
netID/subnetID: 140.252.13.32
hostID: 31
haha@hello:~/.../lj$
haha@hello:~/.../lj$
haha@hello:~/.../lj$
haha@hello:~/.../lj$
haha@hello:~/.../lj$
haha@hello:~/.../lj$  ./get-netid-subnetid-hostid 255.255.255.255.1 255.255.255.255
inet_pton error: 255.255.255.255.1 is invalid dotted-decimal address
The input max dotted-decimal should be 255.255.255.255
haha@hello:~/.../lj$  ./get-netid-subnetid-hostid 255.255.255.255.1 255.255.255.255.2
inet_pton error: 255.255.255.255.1 is invalid dotted-decimal address
The input max dotted-decimal should be 255.255.255.255
haha@hello:~/.../lj$   ./get-netid-subnetid-hostid 255.255.255.255 255.255.255.255.2
inet_pton error: 255.255.255.255.2 is invalid dotted-decimal address
The input max dotted-decimal should be 255.255.255.255
haha@hello:~/.../lj$   ./get-netid-subnetid-hostid 255.255.255.255 255.255.255.255
netID/subnetID: 255.255.255.255
hostID: 0
haha@hello:~/.../lj$   ./get-netid-subnetid-hostid 140.252.13.34 255.255.255.221.2
inet_pton error: 255.255.255.221.2 is invalid dotted-decimal address
The input max dotted-decimal should be 255.255.255.255
haha@hello:~/.../lj$