inet_addr的功能是将一个ip地址字符串转换成一个整数值。一般的IP地址串格式为:'a.b.c.d'分成四段。
但是也会有分为1、2或3段的格式。下面我们来看看WIN2K下inet_addr函数的源码:
- unsigned long PASCAL
- inet_addr(
- IN const char *cp
- )
- {
- register unsigned long val, base, n;
- register char c;
- unsigned long parts[4], *pp = parts;
-
- WS_ENTER( "inet_addr", (PVOID)cp, NULL, NULL, NULL );
- again:
-
-
-
-
-
- val = 0; base = 10;
- if (*cp == '0') {
- base = 8, cp++;
- if (*cp == 'x' || *cp == 'X')
- base = 16, cp++;
- }
-
-
- while (c = *cp) {
- if (isdigit(c)) {
- val = (val * base) + (c - '0');
- cp++;
- continue;
- }
- if (base == 16 && isxdigit(c)) {
- val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
- cp++;
- continue;
- }
- break;
- }
-
- if (*cp == '.') {
-
-
-
-
-
-
-
- if (pp >= parts + 3) {
- WS_EXIT( "inet_addr", -1, TRUE );
- return ((unsigned long) -1);
- }
- *pp++ = val, cp++;
- goto again;
- }
-
-
-
-
- if (*cp && !isspace(*cp)) {
- WS_EXIT( "inet_addr", -1, TRUE );
- return (INADDR_NONE);
- }
- *pp++ = val;
-
-
-
-
- n = (unsigned long)(pp - parts);
- switch ((int) n) {
-
- case 1:
- val = parts[0];
- break;
-
- case 2:
- if ((parts[0] > 0xff) || (parts[1] > 0xffffff)) {
- WS_EXIT( "inet_addr", -1, TRUE );
- return(INADDR_NONE);
- }
- val = (parts[0] << 24) | (parts[1] & 0xffffff);
- break;
-
- case 3: //当为3个段a.b.c时,整数值构成XX XX XX XX
- if ((parts[0] > 0xff) || (parts[1] > 0xff) ||
- (parts[2] > 0xffff)) {
- WS_EXIT( "inet_addr", -1, TRUE );
- return(INADDR_NONE);
- }
- val = (parts[0] << 24) | ((parts[1] & 0xff) << 16) |
- (parts[2] & 0xffff);
- break;
-
- case 4: //当为4个段a.b.c.d时,整数值构成XX XX XX XX
- if ((parts[0] > 0xff) || (parts[1] > 0xff) ||
- (parts[2] > 0xff) || (parts[3] > 0xff)) {
- WS_EXIT( "inet_addr", -1, TRUE );
- return(INADDR_NONE);
- }
- val = (parts[0] << 24) | ((parts[1] & 0xff) << 16) |
- ((parts[2] & 0xff) << 8) | (parts[3] & 0xff);
- break;
-
- default:
- WS_EXIT( "inet_addr", -1, TRUE );
- return (INADDR_NONE);
- }
- val = htonl(val);
- WS_EXIT( "inet_addr", val, FALSE );
- return (val);
- }
原文地址: http://blog.csdn.net/misterliwei/article/details/2318725
inet_addr 将"数字+句点"的格式的IP地址转换到unsigned long中,返回值已经是按照网络字节顺序的
相反inet_ntoa把类型为struct in_addr的数据转化为"数字+句点"的形式的字符串
typedef u_int32_t in_addr_t;
struct in_addr
{
in_addr_t s_addr;
};
本机字节顺序与网络字节顺序的转换
#include <arpa/inet.h>
htons ------"host to network short"
htonl -------"host to network long"
ntohs -------"network to host short"
ntohl -------"network to host long"
*注意:在你的数据放到网络上的时候,确信它是网络字节顺序
网络字节顺序(大端字节)和x86机器字节顺序(小端字节)
eg:0X3132 在x86上显示21 在网络传输中为12
inet_addr返回的整数形式是网络字节序,而inet_network返回的整数形式是主机字节序。他俩都有一个小缺陷,
那就是当IP是255.255.255.255时,这两个函数会认为这是个无效的IP地址,这是历史遗留问题,其实在目前大部
分的路由器上,这个255.255.255.255的IP都是有效的。
inet_aton函数和上面这俩个函数的区别就是在于他认为255.255.255.255是有效的,他不会冤枉这个看似特殊的IP地址。对了,inet_aton函数返回的是网络字节序的IP地址。
综上所述,应该使用inet_aton和inet_ntoa这一对函数。
资料:
#include <sys/socket.h>
#include <netinet/
in.h>
#include <arpa/inet.h>
typedef uint32_t in_addr_t;
int inet_aton(
const
char *cp,
struct in_addr *inp);
in_addr_t inet_addr(
const
char *cp);
in_addr_t inet_network(
const
char *cp);
char *inet_ntoa(
struct in_addr
in);
struct in_addr inet_makeaddr(
int net,
int host);
in_addr_t inet_lnaof(
struct in_addr
in);
in_addr_t inet_netof(
struct in_addr
in);
//
Internet address.
struct in_addr {
union {
struct { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b;
struct { u_short s_w1,s_w2; } S_un_w;
u_long S_addr;
/*
port in network byte order
*/
} S_un;
#define s_addr S_un.S_addr
};
//
Socket address, internet style.
struct sockaddr_in {
//
struct sockaddr的一种特殊形式
short sin_family;
/*
address family: AF_INET
*/
u_short sin_port;
/*
port in network byte order
*/
struct in_addr sin_addr;
/*
port in network byte order
*/
char sin_zero[8];
/*
8 byte pad
*/
};
//
Structure used by kernel to store most addresses.
struct sockaddr {
u_short sa_family;
/*
address family
*/
char sa_data[14];
/*
up to 14 bytes of direct address
*/
};
struct in_addr {
unsigned
long
int s_addr;
}
原文地址: http://www.cppblog.com/toMyself/archive/2010/08/13/123353.aspx