For the IPV4 protocol, IP is a 32-bit integer, and for IPv6, IP is a 128-bit integer. In memory, IP is stored in binary form, but not easy to observe, so
OKconversion translates it into a dotted-decimal representation.
In Linux, there are functions for converting IP binary and dotted decimal to each other:
Inet_ntop the binary into dotted decimal, AF represents the protocol used, and af_inet indicates that the IPV6,SRC used is a IPV4,AF_INET6 that represents the IP
struct in_addr, DST is the string used to store the IP dotted decimal form, and the size is the length of the DST.
Inet_pton is used to convert the dot decimal of an IP into a binary representation, AF indicates whether it is IPV4 or IPV6,SRC is a string that stores IP dotted decimal, DST is a storage IP
struct IN_ADDR type structure body.
#include<stdio.h>
#include<arpa/inet.h>
#include<stdlib.h>
void pton(const char* src)
{
struct in_addr addr;
int temp = inet_pton(AF_INET,src,&addr);
if(temp<=0)
{
perror("fail to convert");
exit(-1);
}
printf("convert success,ip:%x\n",addr.s_addr);
}
void ntop(struct in_addr addr)
{
Span class= "KWD" >char DST [ 16 Span class= "pun" >]={ 0 }; //255.255.255.255 3*4+3 ('. ') +1 (' =16 ')
const char* temp = inet_ntop(AF_INET,&addr,&dst,sizeof(dst));
if(temp==NULL)
{
perror("fail to convert");
exit(-1);
}
printf("convert success,ip:%s\n",dst);
}
int main()
{
pton("192.168.1.24");
struct in_addr addr;
addr.s_addr = 0x1801a8c0;
ntop(addr);
return 0;
}
From for notes (Wiz)
Conversion of address forms in Linux network communication