diff options
author | Denis Ovsienko <denis@ovsienko.info> | 2017-08-27 14:15:17 +0100 |
---|---|---|
committer | Denis Ovsienko <denis@ovsienko.info> | 2017-08-27 14:19:25 +0100 |
commit | b3fb6a6c61e25ef8be56a2f1da7790a4e5ccd20b (patch) | |
tree | 0ce06ef12a7abd269a88240027d1e22e56b0a673 /print-cnfp.c | |
parent | 1b17a8cc5e2f73c17a11b07ada38e2cb173975a7 (diff) | |
download | tcpdump-b3fb6a6c61e25ef8be56a2f1da7790a4e5ccd20b.tar.gz |
Use a table instead of getprotobynumber().
On Linux getprotobynumber() returns different results for the same
argument depending on the contents of /etc/protocols at runtime
(expectedly but gets in the way of reproducible test cases). On FreeBSD
it returns results that are irrelevant of the contents of /etc/protocols
at runtime (unexpectedly). Other implementations exist and may expose
interesting properties too. And if the host uses LDAP instead of
/etc/protocols for name services, a call to that function may cause LDAP
handle the request. All of the above is not right for the specific task
of network protocols decoding, which needs to be fast and deterministic.
As the protocol number space is just 8-bit, add a 256-element array of
strings/NULLs for the translation and a wrapper function around it for
index range enforcement. Change the code to use the new function instead
of getprotobynumber().
Fix a typo while at it.
Diffstat (limited to 'print-cnfp.c')
-rw-r--r-- | print-cnfp.c | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/print-cnfp.c b/print-cnfp.c index bd9b8a65..7e7d835c 100644 --- a/print-cnfp.c +++ b/print-cnfp.c @@ -159,7 +159,7 @@ cnfp_v1_print(netdissect_options *ndo, const u_char *cp) { register const struct nfhdr_v1 *nh; register const struct nfrec_v1 *nr; - struct protoent *pent; + const char *p_name; int nrecs, ver; #if 0 time_t t; @@ -211,13 +211,13 @@ cnfp_v1_print(netdissect_options *ndo, const u_char *cp) ND_PRINT((ndo, ">> %s\n ", intoa(nr->nhop_ina.s_addr))); - if (!ndo->ndo_nflag && (pent = getprotobynumber(nr->proto)) != NULL) - ND_PRINT((ndo, "%s ", pent->p_name)); + if (!ndo->ndo_nflag && (p_name = netdb_protoname(nr->proto)) != NULL) + ND_PRINT((ndo, "%s ", p_name)); else ND_PRINT((ndo, "%u ", nr->proto)); /* tcp flags for tcp only */ - if (pent && pent->p_proto == IPPROTO_TCP) { + if (nr->proto == IPPROTO_TCP) { int flags; flags = nr->tcp_flags; ND_PRINT((ndo, "%s%s%s%s%s%s%s", @@ -248,7 +248,7 @@ cnfp_v5_print(netdissect_options *ndo, const u_char *cp) { register const struct nfhdr_v5 *nh; register const struct nfrec_v5 *nr; - struct protoent *pent; + const char *p_name; int nrecs, ver; #if 0 time_t t; @@ -307,13 +307,13 @@ cnfp_v5_print(netdissect_options *ndo, const u_char *cp) ND_PRINT((ndo, ">> %s\n ", intoa(nr->nhop_ina.s_addr))); - if (!ndo->ndo_nflag && (pent = getprotobynumber(nr->proto)) != NULL) - ND_PRINT((ndo, "%s ", pent->p_name)); + if (!ndo->ndo_nflag && (p_name = netdb_protoname(nr->proto)) != NULL) + ND_PRINT((ndo, "%s ", p_name)); else ND_PRINT((ndo, "%u ", nr->proto)); /* tcp flags for tcp only */ - if (pent && pent->p_proto == IPPROTO_TCP) { + if (nr->proto == IPPROTO_TCP) { int flags; flags = nr->tcp_flags; ND_PRINT((ndo, "%s%s%s%s%s%s%s", @@ -344,7 +344,7 @@ cnfp_v6_print(netdissect_options *ndo, const u_char *cp) { register const struct nfhdr_v6 *nh; register const struct nfrec_v6 *nr; - struct protoent *pent; + const char *p_name; int nrecs, ver; #if 0 time_t t; @@ -403,13 +403,13 @@ cnfp_v6_print(netdissect_options *ndo, const u_char *cp) ND_PRINT((ndo, ">> %s\n ", intoa(nr->nhop_ina.s_addr))); - if (!ndo->ndo_nflag && (pent = getprotobynumber(nr->proto)) != NULL) - ND_PRINT((ndo, "%s ", pent->p_name)); + if (!ndo->ndo_nflag && (p_name = netdb_protoname(nr->proto)) != NULL) + ND_PRINT((ndo, "%s ", p_name)); else ND_PRINT((ndo, "%u ", nr->proto)); /* tcp flags for tcp only */ - if (pent && pent->p_proto == IPPROTO_TCP) { + if (nr->proto == IPPROTO_TCP) { int flags; flags = nr->tcp_flags; ND_PRINT((ndo, "%s%s%s%s%s%s%s", |