1 /* For terms of usage/redistribution/modification see the LICENSE file */
2 /* For authors and contributors see the AUTHORS file */
5 * parseports.c - code to extract the protocol codes or ranges thereof from
6 * the user-defined string.
10 #include "iptraf-ng-compat.h"
11 #include "parseproto.h"
15 * Extracts next token from the buffer.
17 static char *get_next_token(char **cptr
)
19 static char rtoken
[32];
24 skip_whitespace(*cptr
);
26 if (**cptr
== ',' || **cptr
== '-') {
31 while (!isspace(**cptr
) && **cptr
!= '-' && **cptr
!= ','
43 void get_next_protorange(char **cptr
, unsigned int *proto1
,
44 unsigned int *proto2
, int *parse_result
,
51 static char bad_token
[6];
54 memset(toktmp
, 0, sizeof(toktmp
));
55 memset(prototmp1
, 0, sizeof(prototmp1
));
56 memset(prototmp2
, 0, sizeof(prototmp2
));
57 memset(bad_token
, 0, sizeof(bad_token
));
59 strncpy(prototmp1
, get_next_token(cptr
), sizeof(prototmp1
) - 1);
60 if (prototmp1
[0] == '\0') {
61 *parse_result
= NO_MORE_TOKENS
;
65 strncpy(toktmp
, get_next_token(cptr
), sizeof(toktmp
) - 1);
67 *parse_result
= RANGE_OK
;
71 strncpy(prototmp2
, get_next_token(cptr
), sizeof(prototmp2
) - 1);
74 * Check for missing right-hand token for -
76 if (prototmp2
[0] == '\0') {
77 *parse_result
= INVALID_RANGE
;
78 strcpy(bad_token
, "-");
79 *badtokenptr
= bad_token
;
82 *proto2
= (unsigned int) strtoul(prototmp2
, &cerr_ptr
, 10);
84 * First check for an invalid character
86 if (*cerr_ptr
!= '\0') {
87 *parse_result
= INVALID_RANGE
;
88 strncpy(bad_token
, prototmp2
, sizeof(bad_token
));
89 *badtokenptr
= bad_token
;
92 * Then check for the validity of the token
96 strncpy(bad_token
, prototmp2
, sizeof(bad_token
));
97 *badtokenptr
= bad_token
;
98 *parse_result
= OUT_OF_RANGE
;
102 * Then check if the next token is a comma
104 strncpy(toktmp
, get_next_token(cptr
), sizeof(toktmp
) - 1);
105 if (toktmp
[0] != '\0' && toktmp
[0] != ',') {
106 *parse_result
= COMMA_EXPECTED
;
107 strncpy(bad_token
, toktmp
, sizeof(bad_token
));
108 *badtokenptr
= bad_token
;
118 *parse_result
= COMMA_EXPECTED
;
119 strncpy(bad_token
, toktmp
, sizeof(bad_token
));
120 *badtokenptr
= bad_token
;
124 if (*parse_result
!= RANGE_OK
)
127 *proto1
= (unsigned int) strtoul(prototmp1
, &cerr_ptr
, 10);
128 if (*cerr_ptr
!= '\0') {
129 *parse_result
= INVALID_RANGE
;
130 strncpy(bad_token
, prototmp1
, sizeof(bad_token
));
131 *badtokenptr
= bad_token
;
132 } else if (*proto1
> 255) {
133 *parse_result
= OUT_OF_RANGE
;
134 strncpy(bad_token
, prototmp1
, sizeof(bad_token
));
135 *badtokenptr
= bad_token
;
139 if (*proto2
!= 0 && *proto1
> *proto2
) {
146 int validate_ranges(char *samplestring
, int *parse_result
, char **badtokenptr
)
148 unsigned int proto1
, proto2
;
149 char *cptr
= samplestring
;
152 get_next_protorange(&cptr
, &proto1
, &proto2
,
153 parse_result
, badtokenptr
);
154 } while (*parse_result
== RANGE_OK
);
156 if (*parse_result
!= NO_MORE_TOKENS
)