# The code in the C language that determines whether two IPv4 addresses belong to the same subnet

• 2020-05-30 20:34:45
• OfStack

Problem description:

Now given two IPv4 addresses and a subnet mask, judge whether they belong to the same subnet. If they belong to the same subnet, output 1; otherwise, output 0.

For example, input:

172.16.1.3

172.16.1.35

255.255.255.224

Output:

0

Solutions:

First, convert the IP address in string format to the IP address in 4 bytes, and then use the & ) operation, respectively match the two IP addresses with the mask. If the last value is the same, it is the same subnet, otherwise it is not.

The following function converts IP in string format to IP in 4 bytes (because it is 4 bytes, int is used, but int on different platforms takes up different bytes.

``````
int _to_int(char * str, int start_idx, int end_idx)
{
int a = 0, i;
for (i = start_idx; i <= end_idx; ++i)
{
a = a * 10 + (str[i] - '0');
}

return a;
}

/*
*  will ip String to 4 Byte shaping
*/
int ip_to_int(char * ip)
{
int start = 0, i = 0, ret = 0;
int shift_factor = 3; // 1 We're going to start moving to the right 3 * 8 position
char c;
while (c = ip[i])
{
if (c == '.')
{
int a = _to_int(ip, start, i - 1);
int temp = shift_factor * 8;
ret = ret | (a << temp);

shift_factor--;
start = i + 1;
}
i++;
}

return ret;
}``````

The _to_int() function converts a string to a number, essentially a dotted string to a number, and the ip_to_int() function converts ip in string form to an integer.

The following is the section of ip address and subnet mask operation:

``````
#include <stdio.h>

int main()
{
char a1, a2, a3;

gets(a1);
gets(a2);
gets(a3);

int ip1 = ip_to_int(a1);
int ip2 = ip_to_int(a2);
int ip3 = ip_to_int(a3);

int result = 0;
if ((ip1 & ip3) == (ip2 & ip3))
{
result = 1;
}

printf("%d", result);

return 0;
}``````

Here's what others have to add

Topic describes

The subnet mask is used to determine whether the IP addresses of any two computers belong to the same subnetwork.
The subnet mask is the same as the IP address structure, which is a 32-bit 2-base number, in which the network number part is all "1" and the host number part is all "0". The subnet mask can be used to determine whether two hosts are in the same subnet or not. If the IP addresses of two hosts are the same as their subnet masks, then the two hosts are in the same subnet.
Example:
Convert to base 2 for operation:
AND operation
11000000.10101000.00000000.00000000
After conversion to 10, it is:
192.168.0.0

Convert to base 2 for operation:
AND operation
11000000.10101000.00000000.00000000
After conversion to 10, it is:
192.168.0.0
After the above AND operation on the IP address and subnet mask of the two computers, we can see that the result of its operation is like 1. Both are 192.168.0.0, so the two computers can be considered as the same subnetwork.
/*
* function: determine if the IP address of two computers is the same subnetwork.
* String ip1: IP address of computer 1, format: "192.168.0.254";
* String ip2: IP address of computer 2, format: "192.168.0.1";
*
* return value: 0: IP1 and IP2 belong to the same subnetwork; 1: illegal IP address or subnet mask format; 2: IP1 and IP2 do not belong to the same subnetwork
*/
public int checkNetSegment(String mask, String ip1, String ip2)
{
/* implement the function here */
return 0;
}
Input description:

Output description:

Get the calculated result
Input example:

255.255.255.0
192.168.224.256
192.168.10.4

Output examples:

1
Solution code:

``````
#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<cstdlib>
using namespace std;

typedef struct ip
{
int first;
int second;
int three;
} IP;

int judgeIp(string ipSubNet,IP &ip)
{
int index=0;
ip.first=atoi(&ipSubNet[index]);
if(ip.first>255)
return 0;

index++;
index=ipSubNet.find_first_of('.',index);
ip.second=atoi(&ipSubNet[++index]);
if(ip.second>255)
return 0;

index++;
index=ipSubNet.find_first_of('.',index);
ip.three=atoi(&ipSubNet[++index]);
if(ip.three>255)
return 0;

index++;
index=ipSubNet.find_first_of('.',index);
ip.fouth=atoi(&ipSubNet[++index]);
if(ip.fouth>255)
return 0;

return 1;
}

int main()
{
IP subNet,ip1,ip2;
{
{
ip1.first=ip1.first & subNet.first;
ip1.second=ip1.first & subNet.second;
ip1.second=ip1.first & subNet.second;
ip1.fouth=ip1.first & subNet.fouth;

ip2.first=ip2.first & subNet.first;
ip2.second=ip2.first & subNet.second;
ip2.second=ip2.first & subNet.second;
ip2.fouth=ip2.first & subNet.fouth;

if(ip1.first==ip2.first&&ip1.second==ip2.second&&ip1.three==ip2.three&&ip1.fouth==ip2.fouth)
cout<<'0'<<endl;
else
cout<<'2'<<endl;
}
else
cout<<'1'<<endl;
}
return 0;
}``````

C language - how to determine if two IP are in the same segment

``````