diff options
author | Denis Ovsienko <denis@ovsienko.info> | 2023-03-12 16:44:28 +0000 |
---|---|---|
committer | Denis Ovsienko <denis@ovsienko.info> | 2023-03-12 16:44:28 +0000 |
commit | 989e1947f418e501bcea4d2cee1a986b455fe173 (patch) | |
tree | d83b9ea1cfc7c0376da304682e15cba5c368e8aa /nametoaddr.c | |
parent | 023844895059279ad51b0b24cc1de5c121d1833e (diff) |
Clean up DECnet address handling.
In gen_scode() make any id invalid DECnet address. In gen_ncode() make
{N} invalid DECnet address. In __pcap_atodn() replace sscanf() with an
FSM to parse the entire input string and to require a valid nominal
DECnet address syntax. Remove __pcap_nametodnaddr(), which worked on
Ultrix only. Add various comments to explain the context better. In
pcap-filter(3PCAP) clarify the nominal DECnet address syntax and remove
mentions of the host name translation.
Before:
filtertest: decnet name support not included, 'abc' cannot be translated
1 -- no error
100.2000 -- no error
1.2.3 -- no error
1.2.3.4 -- no error
After:
filtertest: invalid DECnet address 'abc'
filtertest: invalid DECnet address '1'
filtertest: invalid DECnet address '100.2000'
filtertest: invalid DECnet address '1.2.3'
filtertest: invalid DECnet address '1.2.3.4'
Diffstat (limited to 'nametoaddr.c')
-rw-r--r-- | nametoaddr.c | 109 |
1 files changed, 75 insertions, 34 deletions
diff --git a/nametoaddr.c b/nametoaddr.c index 7a04a61d..a746cb6d 100644 --- a/nametoaddr.c +++ b/nametoaddr.c @@ -26,11 +26,6 @@ #include <config.h> #endif -#ifdef DECNETLIB -#include <sys/types.h> -#include <netdnet/dnetdb.h> -#endif - #ifdef _WIN32 #include <winsock2.h> #include <ws2tcpip.h> @@ -685,6 +680,13 @@ __pcap_atoin(const char *s, bpf_u_int32 *addr) /* NOTREACHED */ } +/* + * If 's' is not a string that is a well-formed DECnet address (aa.nnnn), + * return zero. Otherwise parse the address into the low 16 bits of 'addr' + * and return a non-zero. The binary DECnet address consists of a 6-bit area + * number and a 10-bit node number; neither area 0 nor node 0 are valid for + * normal addressing purposes, but either can appear on the wire. + */ int __pcap_atodn(const char *s, bpf_u_int32 *addr) { @@ -692,14 +694,76 @@ __pcap_atodn(const char *s, bpf_u_int32 *addr) #define AREAMASK 0176000 #define NODEMASK 01777 - u_int node, area; - - if (sscanf(s, "%d.%d", &area, &node) != 2) - return(0); + /* Initialize to squelch a compiler warning only. */ + u_int node = 0, area = 0; + /* + * +--+ +--+ + * | | | | + * v | v | + * --> START --> AREA --> DOT --> NODE --> + * | | | | + * | v v | + * +--------> INVALID <------+ + */ + enum { + START, + AREA, + DOT, + NODE, + INVALID + } fsm_state = START; - *addr = (area << AREASHIFT) & AREAMASK; - *addr |= (node & NODEMASK); + while (*s) { + switch (fsm_state) { + case START: + if (PCAP_ISDIGIT(*s)) { + area = *s - '0'; + fsm_state = AREA; + break; + } + fsm_state = INVALID; + break; + case AREA: + if (*s == '.') { + fsm_state = DOT; + break; + } + if (PCAP_ISDIGIT(*s)) { + area = area * 10 + *s - '0'; + if (area <= AREAMASK >> AREASHIFT) + break; + } + fsm_state = INVALID; + break; + case DOT: + if (PCAP_ISDIGIT(*s)) { + node = *s - '0'; + fsm_state = NODE; + break; + } + fsm_state = INVALID; + break; + case NODE: + if (PCAP_ISDIGIT(*s)) { + node = node * 10 + *s - '0'; + if (node <= NODEMASK) + break; + } + fsm_state = INVALID; + break; + case INVALID: + return 0; + } /* switch */ + s++; + } /* while */ + /* + * This condition is false if the string comes from the lexer, but + * let's not depend on that. + */ + if (fsm_state != NODE) + return 0; + *addr = area << AREASHIFT | node; return(32); } @@ -800,26 +864,3 @@ pcap_ether_hostton(const char *name) return (ap); } #endif - -/* - * XXX - not guaranteed to be thread-safe! - */ -int -#ifdef DECNETLIB -__pcap_nametodnaddr(const char *name, u_short *res) -{ - struct nodeent *getnodebyname(); - struct nodeent *nep; - - nep = getnodebyname(name); - if (nep == ((struct nodeent *)0)) - return(0); - - memcpy((char *)res, (char *)nep->n_addr, sizeof(unsigned short)); - return(1); -#else -__pcap_nametodnaddr(const char *name _U_, u_short *res _U_) -{ - return(0); -#endif -} |