inet_ntoa 连续使用 注意事项

今天在向同学show的时候,发现了了我打印源ip和目的ip都是一样的,晕,幸亏同学都没有发现。

回来当然要改程序了。

inet_ntoa()

简述:

    将网络地址转换成“.”点隔的字符串格式。

    char * inet_ntoa( struct in_addr in);

    in:一个表示Internet主机地址的结构。

      The inet_ntoa() function converts the Internet host address in given in network byte order to a string in standard

       numbers-and-dots  notation.   The string is returned in a statically allocated buffer, which subsequent calls will

       overwrite.

返回值:

    若无错误发生,inet_ntoa()返回一个字符指针。否则的话,返回NVLL。其中的数据应在下一个WINDOWS套接口调用前复制出来。

参见:

    inet_addr().

 

测试代码如下

include <stdio.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <string.h>

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

{

         struct in_addr addr1,addr2;

         ulong   l1,l2;

         l1= inet_addr("192.168.0.74");

         l2 = inet_addr("211.100.21.179");

         memcpy(&addr1, &l1, 4);

         memcpy(&addr2, &l2, 4);

         printf("%s : %s/n", inet_ntoa(addr1), inet_ntoa(addr2));    //注意这一句的运行结果

         printf("%s/n", inet_ntoa(addr1));

         printf("%s/n", inet_ntoa(addr2));

         return 0;

}

实际运行结果如下:

192.168.0.74 : 192.168.0.74       //从这里可以看出,printf里的inet_ntoa只运行了一次。

192.168.0.74

211.100.21.179

inet_ntoa返回一个char *,而这个char *的空间是在inet_ntoa里面静态分配的,所以inet_ntoa后面的调用会覆盖上一次的调用。第一句printf的结果只能说明在printf里面的可变参数的求值是从右到左的,仅此而已。gdb调试情况:

(gdb) r

Starting program: /home/wangyao/test/a.out

Breakpoint 1, process_tcp (argument=0x0, packet_header=0xbfd38718, packet_content=0x804c682 "") at t1.c:192

192         printf("/n#### Get A Pattern Packet ####/n");

(gdb) s

#### Get A Pattern Packet ####

194             printf("%s/n",inet_ntoa(ip_protocol->ip_src_address));

(gdb) p inet_ntoa(ip_protocol->ip_src_address)

$1 = -1209189324

(gdb) x -1209189324

0xb7ed3c34 <_res+564>:  0x2e313132

(gdb) x/s -1209189324

0xb7ed3c34 <_res+564>:   "211.94.144.100"

(gdb) s

211.94.144.100

195             printf("%s/n",inet_ntoa(ip_protocol->ip_dst_address));

(gdb) p inet_ntoa(ip_protocol->ip_dst_address)

$2 = -1209189324

(gdb) x/s -1209189324

0xb7ed3c34 <_res+564>:   "173.26.100.34"

(gdb) p ip_protocol->ip_dst_address

$3 = {s_addr = 576985773}

(gdb) p ip_protocol->ip_src_address

$4 = {s_addr = 1687183059}

(gdb) s

173.26.100.34

196         printf("%s:%d--->%s:%d/n",inet_ntoa(ip_protocol->ip_src_address),ntohs(tcp_protocol->tcp_src_port),inet_ntoa(ip_protocol->ip_dst_address),ntohs(tcp_protocol->tcp_dst_port));

(gdb) x/s inet_ntoa(ip_protocol->ip_src_address)

0xb7ed3c34 <_res+564>:   "211.94.144.100"

(gdb) x/s inet_ntoa(ip_protocol->ip_dst_address)

0xb7ed3c34 <_res+564>:   "173.26.100.34"

(gdb) s

211.94.144.100:80--->211.94.144.100:49983

(gdb) q

从上面可以看出,inet_ntoa返回的char*地址是一样的,这样的话,在最后的printf部分就会产生数据的覆盖问题

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值