<netinet/in.h>:
/*
* Protocols (RFC 1700)
*/
#define IPPROTO_IP 0 /* dummy for IP */
#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
#define IPPROTO_HOPOPTS 0 /* IP6 hop-by-hop options */
#endif /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
#define IPPROTO_ICMP 1 /* control message protocol */
#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
#define IPPROTO_IGMP 2 /* group mgmt protocol */
#define IPPROTO_GGP 3 /* gateway^2 (deprecated) */
#define IPPROTO_IPV4 4 /* IPv4 encapsulation */
#define IPPROTO_IPIP IPPROTO_IPV4 /* for compatibility */
#endif /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
#define IPPROTO_TCP 6 /* tcp */
#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
#define IPPROTO_ST 7 /* Stream protocol II */
/* ... */
是否存在用于翻译<netinet/in.h>名字?理想的返回字符[]:
ipproto2str(IPPROTO_ICMP): "Control Message Protocol (ICMP)"
您正在寻找getprotobynumber()
。
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
int main(void) {
struct protoent *p = getprotobynumber(IPPROTO_ICMP);
if (p) {
printf("IPPROTO_ICMP (%d) = %sn", p->p_proto, p->p_name);
} else {
puts("Couldn't find IPPROTO_ICMP");
}
}