描述
int inet_pton(int af, const char *src, void *dst);
This function converts the character string src into a network address structure in the af address family, then copies the network address structure to dst. The af argument must be either AF_INET or AF_INET6.
inet_pton用于将点分格式的IP地址字符串转换为网络地址结构体。
af:地址族,值为AF_INET (ipv4)或AF_INET6 (ipv6)
src:对于AF_INET地址族,指向"xxx.xxx.xxx.xxx"格式的字符串
dst:对于AF_INET地址族,指向struct in_addr结构体的指针
const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
This function converts the network address structure src in the af address family into a character string. The resulting string is copied to the buffer pointed to by dst, which must be a non-NULL pointer. The caller specifies the number of bytes available in this buffer in the argument size.
inet_ntop用于将网络地址结构体转换为字符串形式的IP地址。
af:地址族,值为AF_INET (ipv4)或AF_INET6 (ipv6)
src:对于AF_INET地址族,指向被转换的struct in_addr结构体
dst:字符串缓存区
size:字符串缓存区大小
源码解析
这里只提供了inet_pton函数的源码示例:
my_inet_pton.h:
#ifndef INET_PTON_H
#define INET_PTON_H
#include <arpa/inet.h>
int my_inet_pton (int af, const char *src, void *dst);
#endif // INET_PTON_H
my_inet_pton.c:
#include "my_inet_pton.h"
#include <string.h>
#define NULL 0
#define NS_INADDRSZ 4
#define NS_IN6ADDRSZ 16
#define NS_INT16SZ 2
static int inet_pton4 (const char *src, const char *src_end, u_char *dst);
static int hex_digit_value (char ch);
static int inet_pton6 (const char *src, const char *src_endp, unsigned char *dst);
static int __inet_pton_length (int af, const char *src, size_t srclen, void *dst)
{
switch (af)
{
case AF_INET:
return inet_pton4 (src, src + srclen, dst);
case AF_INET6:
return inet_pton6 (src, src + srclen, dst);
default:
return -1;
}
}
int my_inet_pton (int af, const char *src, void *dst)
{
return __inet_pton_length (af, src, strlen (src), dst);
}
static int inet_pton4 (const char *src, const char *end, unsigned char *dst)
{
int saw_digit, octets, ch;
unsigned char tmp[NS_INADDRSZ], *tp;
saw_digit = 0;
octets = 0;
*(tp = tmp) = 0;
while (src < end)
{
ch = *src++;
if (ch >= '0' && ch <= '9')
{
unsigned int new = *tp * 10 + (ch - '0');
if (saw_digit && *tp == 0)
return 0;
if (new > 255)
return 0;
*tp = new;
if (! saw_digit)
{
if (++octets > 4)
return 0;
saw_digit = 1;
}
}
else if (ch == '.' && saw_digit)
{
if (octets == 4)
return 0;
*++tp = 0;
saw_digit = 0;
}
else
return 0;
}
if (octets < 4)
return 0;
memcpy (dst, tmp, NS_INADDRSZ);
return 1;
}
static int hex_digit_value (char ch)
{
if ('0' <= ch && ch <= '9')
return ch - '0';
if ('a' <= ch && ch <= 'f')
return ch - 'a' + 10;
if ('A' <= ch && ch <= 'F')
return ch - 'A' + 10;
return -1;
}
static int inet_pton6 (const char *src, const char *src_endp, unsigned char *dst)
{
unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
const char *curtok;
int ch;
size_t xdigits_seen; /* Number of hex digits since colon. */
unsigned int val;
tp = memset (tmp, '\0', NS_IN6ADDRSZ);
endp = tp + NS_IN6ADDRSZ;
colonp = NULL;
/* Leading :: requires some special handling. */
if (src == src_endp)
return 0;
if (*src == ':')
{
++src;
if (src == src_endp || *src != ':')
return 0;
}
curtok = src;
xdigits_seen = 0;
val = 0;
while (src < src_endp)
{
ch = *src++;
int digit = hex_digit_value (ch);
if (digit >= 0)
{
if (xdigits_seen == 4)
return 0;
val <<= 4;
val |= digit;
if (val > 0xffff)
return 0;
++xdigits_seen;
continue;
}
if (ch == ':')
{
curtok = src;
if (xdigits_seen == 0)
{
if (colonp)
return 0;
colonp = tp;
continue;
}
else if (src == src_endp)
return 0;
if (tp + NS_INT16SZ > endp)
return 0;
*tp++ = (unsigned char) (val >> 8) & 0xff;
*tp++ = (unsigned char) val & 0xff;
xdigits_seen = 0;
val = 0;
continue;
}
if (ch == '.' && ((tp + NS_INADDRSZ) <= endp)
&& inet_pton4 (curtok, src_endp, tp) > 0)
{
tp += NS_INADDRSZ;
xdigits_seen = 0;
break; /* '\0' was seen by inet_pton4. */
}
return 0;
}
if (xdigits_seen > 0)
{
if (tp + NS_INT16SZ > endp)
return 0;
*tp++ = (unsigned char) (val >> 8) & 0xff;
*tp++ = (unsigned char) val & 0xff;
}
if (colonp != NULL)
{
/* Replace :: with zeros. */
if (tp == endp)
/* :: would expand to a zero-width field. */
return 0;
size_t n = tp - colonp;
memmove (endp - n, colonp, n);
memset (colonp, 0, endp - n - colonp);
tp = endp;
}
if (tp != endp)
return 0;
memcpy (dst, tmp, NS_IN6ADDRSZ);
return 1;
}
main.c:
#include <stdio.h>
#include "my_inet_pton.h"
int main(int argc, char *argv[])
{
char IPDotDec[20]="192.168.1.111";
struct in_addr s;
my_inet_pton(AF_INET,IPDotDec,&s);//调用自己实现的inet_pton函数,将Ip地址的点分格式转为网络地址格式
printf("binary format: %02X %02X %02X %02X\n",
*((unsigned char *)&s.s_addr),
*((unsigned char *)&s.s_addr+1),
*((unsigned char *)&s.s_addr+2),
*((unsigned char *)&s.s_addr+3)
);
char IPDotBuffer[20]={0};
inet_ntop(AF_INET,&s,IPDotBuffer,sizeof(IPDotBuffer));
printf("text format: %s\n",IPDotBuffer);
return 0;
}
输出:
binary format: C0 A8 01 6F
text format: 192.168.1.111