当前位置:编程学习 > C/C++ >>

struct protoent结构,getprotobyname 及相关函数

在我的嵌入式设备中写了一个抓包程序,获取协议号的信息,结果在调用getprotobynumber时老是出错
返回的protoent结构指针始终为空,打印了调试信息
[cpp]  
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!!!!  
 
 
 
在网上查到关于这个函数的博客,http://yadang418.blog.163.com/blog/static/2684365620096101121612/  
里面有一句话”此函数会从 /etc/protocols中查找符合条件的数据并由结构protoent返回“,才想到我的设备/etc下没这个文件
复制linux下的进去试试,再运行,ok了!!
[cpp]  
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结构
      
[cpp]  
/* 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内核================
[cpp]  
/* 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);  
 
补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,