IP地址转换函数

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int inet_aton (const char *name, struct in_addr *addr)
uint32_t inet_addr (const char *name)
uint32_t inet_network (const char *name)
char * inet_ntoa (struct in_addr addr)
struct in_addr inet_makeaddr (uint32_t net, uint32_t local)
uint32_t inet_lnaof (struct in_addr addr)
uint32_t inet_netof (struct in_addr addr)

/*线程安全、可重入*/
int inet_pton (int af, const char *cp, void *buf)
const char * inet_ntop (int af, const void *cp, char *buf, size_t len)

IP地址结构:

#include <netinet/in.h>
/*
 * Internet address (a structure for historical reasons)
 */
struct in_addr {
	in_addr_t s_addr;
};
typedef __uint32_t      in_addr_t;      /* base type for internet address */

1、字符串IP转二进制IP

int inet_aton (const char *name, struct in_addr *addr)

Description:
This function converts the IPv4 Internet host address name from the standard numbers-and-dots notation into binary 
 data and stores it in the struct in_addr that addr points to. inet_aton returns nonzero if the address is valid, zero if not. 
 

2、字符串IP转二进制IP

uint32_t inet_addr (const char *name)

Description:
This function converts the IPv4 Internet host address name from the standard numbers-and-dots notation into binary data. 
 If the input is not valid, inet_addr returns INADDR_NONE. This is an obsolete interface to inet_aton, described immediately above. It is obsolete because INADDR_NONE is a valid address (255.255.255.255), and inet_aton provides a cleaner way to indicate error return. 

3、字符串IP的网络部分转为二进制

uint32_t inet_network (const char *name)

Description:
This function extracts the network number from the address name, given in the standard numbers-and-dots notation. 
 The returned address is in host order. If the input is not valid, inet_network returns -1. 

The function works only with traditional IPv4 class A, B and C network types. It doesn't work with classless addresses 
 and shouldn't be used anymore. 

4、二进制IP转为字符串IP

char * inet_ntoa (struct in_addr addr)

Description:
This function converts the IPv4 Internet host address addr to a string in the standard numbers-and-dots notation. The 
 return value is a pointer into a statically-allocated buffer. Subsequent calls will overwrite the same buffer, so you should copy the string if you need to save it. 

5、将网络地址和主机地址合并为IP地址

struct in_addr inet_makeaddr (uint32_t net, uint32_t local)

Description:
This function makes an IPv4 Internet host address by combining the network number net with the local-address-
 within-network number local. 

6、获取IP地址的主机地址

uint32_t inet_lnaof (struct in_addr addr)

Description:
This function returns the local-address-within-network part of the Internet host address addr. 

The function works only with traditional IPv4 class A, B and C network types. It doesn't work with classless addresses 
 and shouldn't be used anymore. 

7、获取IP地址的网络地址

uint32_t inet_netof (struct in_addr addr)

Description:
This function returns the network number part of the Internet host address addr. 

The function works only with traditional IPv4 class A, B and C network types. It doesn't work with classless addresses 
 and shouldn't be used anymore. 

8、inet_pton & inet_ntop

安全性: inet_ntoa的返回值是一个静态内存,每次调用该函数,返回值都会重新覆盖这块内存,也就是不可重入性;inet_ntop和inet_pton都具有可重入性;
协议无关性:inet_ntop和inet_pton支持IPv4和IPv6;

int inet_pton (int af, const char *cp, void *buf)

Description:
This function converts an Internet address (either IPv4 or IPv6) from presentation (textual) to network (binary) format. 

af should be either AF_INET or AF_INET6, as appropriate for the type of address being converted. 

cp is a pointer to the input string, and buf is a pointer to a buffer for the result. 

It is the caller's responsibility to make sure the buffer is large enough. 
const char * inet_ntop (int af, const void *cp, char *buf, size_t len)

Description:
This function converts an Internet address (either IPv4 or IPv6) from network (binary) to presentation (textual) form. 

af should be either AF_INET or AF_INET6, as appropriate. cp is a pointer to the address to be converted. 

buf should be a pointer to a buffer to hold the result, and len is the length of this buffer. 

The return value from the function will be this buffer address. 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值