获取协议名、协议号——getprotoent(),getprotobyname(),getprotobynumber()


/* 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.  */
};
struct protoent *getprotoent(void);
struct protoent *getprotobyname(const char *name);
struct protoent *getprotobynumber(int proto);
void setprotoent(int stayopen);
void endprotoent(void);
说明:

(1) 这几个函数的原理是读取文件 /etc/protocols (ubuntu下)

/**
 * getprotoent()
 * OS: Ubuntu 11.04 Server
 */
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>

static void printproto(struct protoent *proto);

int main()
{
	struct protoent *proto = NULL;
	setprotoent(1);
	while( (proto = getprotoent()) != NULL )
	{
		printproto(proto);
		printf("\n");
	}
	endprotoent();
	return 0;
}
static void printproto(struct protoent *proto)
{
	char **p = NULL;
	
	printf("protocol: %s\n", proto->p_name);
	
	for(p = proto->p_aliases; *p; p++)
	{
		printf("alias: %s\n", *p);
	}
	
	printf("protocol: %d\n", proto->p_proto);
}
/*
output:
protocol: ip
alias: IP
protocol: 0

protocol: icmp
alias: ICMP
protocol: 1

protocol: igmp
alias: IGMP
protocol: 2

...

读取文件:/etc/protocols
*/





你可能感兴趣的:(server,struct,ubuntu,null,output)