2
* parseports.c - code to extract the protocol codes or ranges thereof from
3
* the user-defined string.
5
* Copyright (c) Gerard Paul Java 2002
7
* This software is open-source; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed WITHOUT ANY WARRANTY; without even the
13
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
* See the GNU General Public License in the included COPYING file for
21
#include "parseproto.h"
25
* Extracts next token from the buffer.
27
char *get_next_token(char *buf, char **cptr)
29
static char rtoken[32];
36
* Skip over leading whitespace
39
while (isspace(**cptr))
42
if (**cptr == ',' || **cptr == '-') {
47
while (!isspace(**cptr) && **cptr != '-' && **cptr != ','
59
void get_next_protorange(char *src, char **cptr,
60
unsigned int *proto1, unsigned int *proto2,
61
int *parse_result, char **badtokenptr)
67
static char bad_token[5];
71
memset(prototmp1, 0, 5);
72
memset(prototmp2, 0, 5);
73
memset(bad_token, 0, 5);
75
strncpy(prototmp1, get_next_token(src, cptr), 5);
76
if (prototmp1[0] == '\0') {
77
*parse_result = NO_MORE_TOKENS;
81
strncpy(toktmp, get_next_token(src, cptr), 5);
83
*parse_result = RANGE_OK;
87
strncpy(prototmp2, get_next_token(src, cptr), 5);
90
* Check for missing right-hand token for -
92
if (prototmp2[0] == '\0') {
93
*parse_result = INVALID_RANGE;
94
strcpy(bad_token, "-");
95
*badtokenptr = bad_token;
98
*proto2 = (unsigned int) strtoul(prototmp2, &cerr_ptr, 10);
100
* First check for an invalid character
102
if (*cerr_ptr != '\0') {
103
*parse_result = INVALID_RANGE;
104
strncpy(bad_token, prototmp2, 5);
105
*badtokenptr = bad_token;
108
* Then check for the validity of the token
112
strncpy(bad_token, prototmp2, 5);
113
*badtokenptr = bad_token;
114
*parse_result = OUT_OF_RANGE;
118
* Then check if the next token is a comma
120
strncpy(toktmp, get_next_token(src, cptr), 5);
121
if (toktmp[0] != '\0' && toktmp[0] != ',') {
122
*parse_result = COMMA_EXPECTED;
123
strncpy(bad_token, toktmp, 5);
124
*badtokenptr = bad_token;
134
*parse_result = COMMA_EXPECTED;
135
strncpy(bad_token, toktmp, 5);
136
*badtokenptr = bad_token;
140
if (*parse_result != RANGE_OK)
143
*proto1 = (unsigned int) strtoul(prototmp1, &cerr_ptr, 10);
144
if (*cerr_ptr != '\0') {
145
*parse_result = INVALID_RANGE;
146
strncpy(bad_token, prototmp1, 5);
147
*badtokenptr = bad_token;
148
} else if (*proto1 > 255) {
149
*parse_result = OUT_OF_RANGE;
150
strncpy(bad_token, prototmp1, 5);
151
*badtokenptr = bad_token;
155
if (*proto2 != 0 && *proto1 > *proto2) {
162
int validate_ranges(char *samplestring, int *parse_result,
166
char *cptr = samplestring;
169
get_next_protorange(samplestring, &cptr, &proto1, &proto2,
170
parse_result, badtokenptr);
171
} while (*parse_result == RANGE_OK);
173
if (*parse_result != NO_MORE_TOKENS)