inet_pton 和inet_ntop对IPv4和IPv6都适用
<arpa/inet.h>
头文件中的声明如下:
int inet_pton (int __af, const char *__restrict __cp, void *__restrict __buf)
const char *inet_ntop (int __af, const void *__restrict __cp, char *__restrict __buf, size_t socklen_t__len)
man
SYNOPSIS
#include <arpa/inet.h>
int inet_pton(int af, const char *src, void *dst);
DESCRIPTION
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_ntop
NAME
inet_ntop - convert IPv4 and IPv6 addresses from binary to text form
SYNOPSIS
#include <arpa/inet.h>
const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
DESCRIPTION
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.
/*size 为目标储存单元的大小,即点分十进制的长度或冒号16进制的长度,为了有助于指定这个大小,在`<netinet/in.h>`中有如下定义:*/
#define INET_ADDRSTRLEN 16
#define INET6_ADDRSTRLEN 46
与书中有些出入,不过不影响。
字节操纵函数
为什么要这些函数?
因为有些字段(IP地址)可能包含值为0的字节,但却不是C字符串。不能用str开头的函数处理。
这些函数分为两类:
以b开头的第一组函数起源于4.2BSD:
#include <strings.h>
//bzero - write zero-valued bytes
void bzero(void *s, size_t n);
//bcopy - copy byte sequence
void bcopy(const void *src, void *dest, size_t n);
// bcmp - compare byte sequences
int bcmp(const void *s1, const void *s2, size_t n);
//const限定被修饰的指针所指向的内容不能被函数改变
man
The bzero() function sets the first n bytes of area starting at s to zero (bytes containing '\0').
The bcopy() function copies n bytes from src to dest. The result is correct, even when both areas overlap.
DESCRIPTION
The bcmp() function compares the two byte sequences s1 and s2 of length n each. If they are equal, and in particular if n is zero, bcmp() returns 0. Otherwise, it returns a nonzero result.
RETURN VALUE
The bcmp() function returns 0 if the byte sequences are equal, other-wise a nonzero result is returned.
以mem(memory)开头的第二组函数起源于ANSI标准:
#include <string.h>
//memset - fill memory with a constant byte
void *memset(void *s, int c, size_t n);
//memcpy - copy memory area
void *memcpy(void *dest, const void *src, size_t n);
//memcmp - compare memory areas
int memcmp(const void *s1, const void *s2, size_t n);
man
//memset
The memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c.
//memcpy
The memcpy() function copies n bytes from memory area src to memory area dest. The memory areas must not overlap. Use memmove(3) if the memory areas do overlap.
//memcmp
DESCRIPTION
The memcmp() function compares the first n bytes (each interpreted as unsigned char) of the memory areas s1 and s2.
RETURN VALUE
The memcmp() function returns an integer less than, equal to, or greater than zero if the first n bytes of s1 is found, respectively, to be less than, to match, or be greater than the first n bytes of s2.
For a nonzero return value, the sign is determined by the sign of the difference between the first pair of bytes (interpreted as unsigned char) that differ in s1 and s2.
If n is zero, the return value is zero.