struct protoent结构,getprotobyname 及相关函数

在我的嵌入式设备中写了一个抓包程序,获取协议号的信息,结果在调用getprotobynumber时老是出错
返回的protoent结构指针始终为空,打印了调试信息
src_addr:192.168.5.155,dst_addr:180.153.201.235
ip_p=6-------
protoent is NULL!!!!
src_addr:180.153.201.235,dst_addr:192.168.5.155
ip_p=6-------
protoent is NULL!!!!
src_addr:192.168.5.155,dst_addr:180.153.201.235
ip_p=6-------
protoent is NULL!!!!
src_addr:180.153.201.235,dst_addr:192.168.5.155
ip_p=6-------
protoent is NULL!!!!



里面有一句话”此函数会从 /etc/protocols中查找符合条件的数据并由结构protoent返回“,才想到我的设备/etc下没这个文件
复制linux下的进去试试,再运行,ok了!!
src_addr:192.168.5.152,dst_addr:238.8.8.8
ip_p=17-------
pro_name :udp
udp src_port:60597,dst_port:11111
src_addr:192.168.5.30,dst_addr:255.255.255.255
ip_p=17-------
pro_name :udp
udp src_port:3601,dst_port:3600
src_addr:192.168.5.152,dst_addr:238.8.8.8
ip_p=17-------
pro_name :udp
udp src_port:60597,dst_port:11111
src_addr:192.168.5.152,dst_addr:238.8.8.8
ip_p=17-------
pro_name :udp
udp src_port:60597,dst_port:11111


相关函数:getprotobyname, getprotoent, setprotoent, endprotoent
表头文件:#include <netdb.h>
函数定义:struct protoent *getprotobynumber(int proto)
函 数说明:getprotobynumber()会返回一个protoent结构,参数proto为欲查询的网络协议编号。此函数会从 /etc/protocols中查找符合条件的数据并由结构protoent返回。 结构protoent定义请参getprotoent()
返回值   :成功则返回protoent结构指印,若有错误或找不到各个符合的数据则返回NULL指针

struct protoent结构
      
/* Description of data base entry for a single service.  */
struct protoent
{
  char *p_name;         /* Official protocol name.  */
  char **p_aliases;     /* Alias list.  */
  int p_proto;          /* Protocol number.  */
};


getprotobyname():依照通讯协定 (protocol) 的名称来获取该通讯协定的其他资料。 
  格 式: struct protoent * getprotobyname( const char *name ); 
  参 数: name   通讯协定名称 
  传回值: 成功 - 一指向 struct protoent 的指针 
       失败 - NULL  
        说明: 利用通讯协定的名称来得知该通讯协定的别名、编号等资料。 

getprotobynumber():依照通讯协定的编号来获取该通讯协定的其他资料。 
  格 式: struct protoent * getprotobynumber( int number ); 
  参 数: number  以 host 排列方式的通讯协定编号 
  传回值: 成功 - 一指向 struct protoent 的指针 
       失败 - NULL  
  说明: 利用通讯协定的编号来得知该通讯协定的名称、别名等资料。 
=============2.6.31内核================
/* Open protocol data base files and mark them as staying open even
   after a later search if STAY_OPEN is non-zero.
   This function is a possible cancellation point and therefore not
   marked with __THROW.  */
extern void setprotoent (int __stay_open);

/* Close protocol data base files and clear `stay open' flag.
   This function is a possible cancellation point and therefore not
   marked with __THROW.  */
extern void endprotoent (void);

/* Get next entry from protocol data base file.  Open data base if
   necessary.
   This function is a possible cancellation point and therefore not
   marked with __THROW.  */
extern struct protoent *getprotoent (void);

/* Return entry from protocol data base for network with NAME.
   This function is a possible cancellation point and therefore not
   marked with __THROW.  */
extern struct protoent *getprotobyname (__const char *__name);

/* Return entry from protocol data base which number is PROTO.
   This function is a possible cancellation point and therefore not
   marked with __THROW.  */
extern struct protoent *getprotobynumber (int __proto);


=================end========================



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值