capt_get_packet(): check for key press only every 20ms
[iptraf-ng.git] / src / parseproto.c
blob73eb52f96b037fdef2963727fea7b813973ea0a4
1 /* For terms of usage/redistribution/modification see the LICENSE file */
2 /* For authors and contributors see the AUTHORS file */
4 /*
5 * parseports.c - code to extract the protocol codes or ranges thereof from
6 * the user-defined string.
8 */
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];
20 int i;
22 i = 0;
24 skip_whitespace(*cptr);
26 if (**cptr == ',' || **cptr == '-') {
27 rtoken[0] = **cptr;
28 rtoken[1] = '\0';
29 (*cptr)++;
30 } else {
31 while (!isspace(**cptr) && **cptr != '-' && **cptr != ','
32 && **cptr != '\0') {
33 rtoken[i] = **cptr;
34 (*cptr)++;
35 i++;
37 rtoken[i] = '\0';
40 return rtoken;
43 void get_next_protorange(char **cptr, unsigned int *proto1,
44 unsigned int *proto2, int *parse_result,
45 char **badtokenptr)
47 char toktmp[6];
48 char prototmp1[6];
49 char prototmp2[6];
50 char *cerr_ptr;
51 static char bad_token[6];
52 unsigned int tmp;
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;
62 return;
65 strncpy(toktmp, get_next_token(cptr), sizeof(toktmp) - 1);
67 *parse_result = RANGE_OK;
69 switch (toktmp[0]) {
70 case '-':
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;
80 break;
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;
90 } else {
92 * Then check for the validity of the token
95 if (*proto2 > 255) {
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;
112 break;
113 case ',':
114 case '\0':
115 *proto2 = 0;
116 break;
117 default:
118 *parse_result = COMMA_EXPECTED;
119 strncpy(bad_token, toktmp, sizeof(bad_token));
120 *badtokenptr = bad_token;
121 break;
124 if (*parse_result != RANGE_OK)
125 return;
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;
136 } else
137 *badtokenptr = NULL;
139 if (*proto2 != 0 && *proto1 > *proto2) {
140 tmp = *proto1;
141 *proto1 = *proto2;
142 *proto2 = tmp;
146 int validate_ranges(char *samplestring, int *parse_result, char **badtokenptr)
148 unsigned int proto1, proto2;
149 char *cptr = samplestring;
151 do {
152 get_next_protorange(&cptr, &proto1, &proto2,
153 parse_result, badtokenptr);
154 } while (*parse_result == RANGE_OK);
156 if (*parse_result != NO_MORE_TOKENS)
157 return 0;
159 return 1;