~ubuntu-branches/ubuntu/natty/iptraf/natty-proposed

« back to all changes in this revision

Viewing changes to src/parseproto.c

  • Committer: Bazaar Package Importer
  • Author(s): Frederic Peters
  • Date: 2006-10-15 13:34:14 UTC
  • mfrom: (1.1.2 upstream) (3.1.1 dapper)
  • Revision ID: james.westby@ubuntu.com-20061015133414-77itbhydih1z3amr
* Sync with Ubuntu fixes (by Oliver Grawert and Michael Vogt)
  * added fix for /var/run detection (since it is a tmpfs by default on
    Ubuntu) [and fixed ubuntu fix]
  * added support for ath devices
  * fixed FTBFS by changing linux/if_tr.h to netinet/if_tr.h

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * parseports.c - code to extract the protocol codes or ranges thereof from
 
3
 *                the user-defined string.
 
4
 *
 
5
 * Copyright (c) Gerard Paul Java 2002
 
6
 *
 
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.
 
11
 *
 
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
 
15
 * details.
 
16
 */
 
17
 
 
18
#include <string.h>
 
19
#include <ctype.h>
 
20
#include <stdlib.h>
 
21
#include "parseproto.h"
 
22
 
 
23
 
 
24
/*
 
25
 * Extracts next token from the buffer.
 
26
 */
 
27
char *get_next_token(char *buf, char **cptr)
 
28
{
 
29
    static char rtoken[32];
 
30
    int i;
 
31
 
 
32
    i = 0;
 
33
 
 
34
 
 
35
    /*
 
36
     * Skip over leading whitespace
 
37
     */
 
38
 
 
39
    while (isspace(**cptr))
 
40
        (*cptr)++;
 
41
 
 
42
    if (**cptr == ',' || **cptr == '-') {
 
43
        rtoken[0] = **cptr;
 
44
        rtoken[1] = '\0';
 
45
        (*cptr)++;
 
46
    } else {
 
47
        while (!isspace(**cptr) && **cptr != '-' && **cptr != ','
 
48
               && **cptr != '\0') {
 
49
            rtoken[i] = **cptr;
 
50
            (*cptr)++;
 
51
            i++;
 
52
        }
 
53
        rtoken[i] = '\0';
 
54
    }
 
55
 
 
56
    return rtoken;
 
57
}
 
58
 
 
59
void get_next_protorange(char *src, char **cptr,
 
60
                         unsigned int *proto1, unsigned int *proto2,
 
61
                         int *parse_result, char **badtokenptr)
 
62
{
 
63
    char toktmp[5];
 
64
    char prototmp1[5];
 
65
    char prototmp2[5];
 
66
    char *cerr_ptr;
 
67
    static char bad_token[5];
 
68
    unsigned int tmp;
 
69
 
 
70
    memset(toktmp, 0, 5);
 
71
    memset(prototmp1, 0, 5);
 
72
    memset(prototmp2, 0, 5);
 
73
    memset(bad_token, 0, 5);
 
74
 
 
75
    strncpy(prototmp1, get_next_token(src, cptr), 5);
 
76
    if (prototmp1[0] == '\0') {
 
77
        *parse_result = NO_MORE_TOKENS;
 
78
        return;
 
79
    }
 
80
 
 
81
    strncpy(toktmp, get_next_token(src, cptr), 5);
 
82
 
 
83
    *parse_result = RANGE_OK;
 
84
 
 
85
    switch (toktmp[0]) {
 
86
    case '-':
 
87
        strncpy(prototmp2, get_next_token(src, cptr), 5);
 
88
 
 
89
        /*
 
90
         * Check for missing right-hand token for -
 
91
         */
 
92
        if (prototmp2[0] == '\0') {
 
93
            *parse_result = INVALID_RANGE;
 
94
            strcpy(bad_token, "-");
 
95
            *badtokenptr = bad_token;
 
96
            break;
 
97
        }
 
98
        *proto2 = (unsigned int) strtoul(prototmp2, &cerr_ptr, 10);
 
99
        /*
 
100
         * First check for an invalid character
 
101
         */
 
102
        if (*cerr_ptr != '\0') {
 
103
            *parse_result = INVALID_RANGE;
 
104
            strncpy(bad_token, prototmp2, 5);
 
105
            *badtokenptr = bad_token;
 
106
        } else {
 
107
            /*
 
108
             * Then check for the validity of the token
 
109
             */
 
110
 
 
111
            if (*proto2 > 255) {
 
112
                strncpy(bad_token, prototmp2, 5);
 
113
                *badtokenptr = bad_token;
 
114
                *parse_result = OUT_OF_RANGE;
 
115
            }
 
116
 
 
117
            /*
 
118
             * Then check if the next token is a comma
 
119
             */
 
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;
 
125
            }
 
126
        }
 
127
 
 
128
        break;
 
129
    case ',':
 
130
    case '\0':
 
131
        *proto2 = 0;
 
132
        break;
 
133
    default:
 
134
        *parse_result = COMMA_EXPECTED;
 
135
        strncpy(bad_token, toktmp, 5);
 
136
        *badtokenptr = bad_token;
 
137
        break;
 
138
    }
 
139
 
 
140
    if (*parse_result != RANGE_OK)
 
141
        return;
 
142
 
 
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;
 
152
    } else
 
153
        *badtokenptr = NULL;
 
154
 
 
155
    if (*proto2 != 0 && *proto1 > *proto2) {
 
156
        tmp = *proto1;
 
157
        *proto1 = *proto2;
 
158
        *proto2 = tmp;
 
159
    }
 
160
}
 
161
 
 
162
int validate_ranges(char *samplestring, int *parse_result,
 
163
                    char **badtokenptr)
 
164
{
 
165
    int proto1, proto2;
 
166
    char *cptr = samplestring;
 
167
 
 
168
    do {
 
169
        get_next_protorange(samplestring, &cptr, &proto1, &proto2,
 
170
                            parse_result, badtokenptr);
 
171
    } while (*parse_result == RANGE_OK);
 
172
 
 
173
    if (*parse_result != NO_MORE_TOKENS)
 
174
        return 0;
 
175
 
 
176
    return 1;
 
177
}