Skip to content

Commit 77c1c1a

Browse files
committed
Fix parsing of numeric protocols in WKS RRs
1 parent ab6924c commit 77c1c1a

File tree

2 files changed

+9
-22
lines changed

2 files changed

+9
-22
lines changed

src/generic/types.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -386,11 +386,11 @@ static int32_t parse_wks_rdata(
386386
if ((code = take_contiguous(parser, type, &fields[1], token)) < 0)
387387
return code;
388388

389-
int32_t protocol = scan_protocol(token->data, token->length);
390-
if (protocol == -1)
389+
uint8_t protocol;
390+
if (!scan_protocol(token->data, token->length, &protocol))
391391
SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(&fields[1]), NAME(type));
392392

393-
*rdata->octets++ = (uint8_t)protocol;
393+
*rdata->octets++ = protocol;
394394
uint8_t *bitmap = rdata->octets;
395395
int32_t highest_port = -1;
396396

src/generic/wks.h

+6-19
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,11 @@
7575
# define TCP (0x7463700000000000llu)
7676
# define UDP (0x7564700000000000llu)
7777
#else
78-
#error
7978
# error "byte order unknown"
8079
#endif
8180

8281
static really_inline int32_t scan_protocol(
83-
const char *name, size_t length)
82+
const char *name, size_t length, uint8_t *protocol)
8483
{
8584
static const int8_t zero_masks[48] = {
8685
-1, -1, -1, -1, -1, -1, -1, -1,
@@ -101,22 +100,11 @@ static really_inline int32_t scan_protocol(
101100
key &= mask;
102101

103102
if (key == TCP)
104-
return 6;
105-
if (key == UDP)
106-
return 17;
107-
108-
if (length > 3) // protocol numbers must be between 0 and 255
109-
return -1;
110-
111-
uint8_t digit;
112-
int32_t number = 0;
113-
size_t index = 0;
114-
while ((digit = (uint8_t)name[index++] - '0') <= 9)
115-
number = number * 10 + digit;
116-
117-
if (index != length || number > 255)
118-
return -1;
119-
return number;
103+
return (void)(*protocol = 6), 1;
104+
else if (key == UDP)
105+
return (void)(*protocol = 17), 1;
106+
else
107+
return scan_int8(name, length, protocol);
120108
}
121109

122110
typedef struct service service_t;
@@ -131,7 +119,6 @@ struct service {
131119
#define UNKNOWN_SERVICE() { { "", 0 }, 0 }
132120
#define SERVICE(name, port) { { name, sizeof(name) - 1 }, port }
133121

134-
// FIXME: state we're not interested in reverse lookup for wks!
135122
static const service_t services[64] = {
136123
UNKNOWN_SERVICE(),
137124
SERVICE("snmptrap", 162),

0 commit comments

Comments
 (0)