linux ip to int,谨慎使用LINUX平台的gethostbynamer函数

谨慎使用LINUX平台的gethostbynamer函数

(2013-04-16 22:49:15)

标签:

平台

linux

杂谈

谨慎使用LINUX平台的gethostbyname_r函数编译运行下面的代码:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

unsigned int get_ip4(char *host)

{

//临时变量

char dns_buff;

struct hostent hostinfo,鸿和扬钧的博客 *phost;

struct sockaddr_in addr4;

int i, flag, rc;

time_t now;

unsigned int ip4;

//尝试直接转换

bzero(&addr4, sizeof(struct sockaddr_in));

if (1 == inet_pton(AF_INET, host, &(addr4.sin_addr)))

return(addr4.sin_addr.s_addr);

//查询DNS

if (0 == gethostbyname_r(host, &hostinfo, dns_buff, 8192,

&phost, &rc)) {

ip4 = *(unsigned int *)(hostinfo.h_addr);//新记录

} else {

ip4 = 0;//查询失败

}

return(ip4);

}

int main(int argc, char *argv[])

{

unsigned int ip4 = 0;

ip4 = get_ip4("192.1468.80.38");

return(0);

}

在freeBSD运行不会段错误, 在CentOS 4上就会段错误

现在我只好重新实现类似函数了

其它LINUX/UNIX平台的帮我实验下, 我想确定是LINUX还是仅仅是RHEL的问题

谢谢了

[ 本帖最后由 safedead 于 2007-8-31 17:25 编辑 ]关于gethostbyname_r的返回值

man手册是这么说的

struct hostent *gethostbyname2(const char *name, int af);

int gethostbyname_r (const char *name,

struct hostent *ret, char *buf, size_t buflen,

struct hostent **result, int *h_errnop);

int gethostbyname2_r (const char *name, int af,

struct hostent *ret, char *buf, size_t buflen,

struct hostent **result, int *h_errnop);

......

The functions gethostbyname() and gethostbyaddr() may

returnpointers

tostaticdata, which may be overwritten by later calls. Copying

the

struct hostent does not suffice, since it contains pointers

-adeep

copy is required.

Glibc2alsohasa gethostbyname2() that works like

gethostbyname(),

but permits to specify the address family to

whichtheaddressmust

belong.

Glibc2alsohasreentrant versions gethostbyname_r() and

gethostby-

name2_r().These return 0 on success and nonzero on error. The

result

ofthecall is now stored in the struct with address ret.After

the

call, *result will be NULL on error or point to the result on

success.

Auxiliarydata is stored in the buffer buf of length buflen.(If

the

buffer is too small,纪苏艳影的博客 these functions will

return ERANGE.) Noglobal

variableh_errnois modified, but the address of a variable in

which

to store error numbers is passed in h_errnop.

按照man的说法

gethostbyname_r()调用成功时返回0

*result指向解析成功的数据结构, *result如果为NULL则表示解析出错

我根据实验发现

如果被解析的字符串是类似"xxx.xxx.xxx.xxx"的 数字+点 字符串

那么gethostbyname_r并没有发出DNS请求

而是直接去计算了

192.1468.80.38显然不是合法的IP地址, 但gethostbyname_r()返回0

此时*result是NULL

所以我程序中的那句ip4 = *(unsigned int *)(hostinfo.h_addr);就会段错误

如果被解析的字串确实无法解析, gethostbyname_r()返回非零值

但前提是: 这个字串不能是类似"xxx.xxx.xxx.xxx"的 数字+点 字符串

否则gethostbyname_r根本不发出DNS请求

刚才在FreeBSD/MAC OS X上测试了一下

对于192.1468.80.38这样的非法IP地址, gethostbyname_r()返回非零值,

不会导致执行ip4 = *(unsigned int *)(hostinfo.h_addr);造成段错误楼主很细心:mrgreen:

分享:

a4c26d1e5885305701be709a3d33442f.png喜欢

0

a4c26d1e5885305701be709a3d33442f.png赠金笔

加载中,请稍候......

评论加载中,请稍候...

发评论

登录名: 密码: 找回密码 注册记住登录状态

昵   称:

评论并转载此博文

a4c26d1e5885305701be709a3d33442f.png

发评论

以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值