summaryrefslogtreecommitdiffstats
path: root/src/utl_yaml.cpp
blob: 444031c6bc774b66437243bc1698d66c041fad6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "utl_yaml.h"
#include <common/Network/Packet/CPktCmn.h>
/*
 Hanoh Haim
 Cisco Systems, Inc.
*/

/*
Copyright (c) 2015-2015 Cisco Systems, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include <istream>

#define INADDRSZ 4

static int my_inet_pton4(const char *src, unsigned char *dst)
{
	static const char digits[] = "0123456789";
	int saw_digit, octets, ch;
	unsigned char tmp[INADDRSZ], *tp;

	saw_digit = 0;
	octets = 0;
	*(tp = tmp) = 0;
	while ((ch = *src++) != '\0') {
		const char *pch;

		if ((pch = strchr(digits, ch)) != NULL) {
			unsigned int _new = *tp * 10 + (pch - digits);

			if (_new > 255)
				return (0);
			if (! saw_digit) {
				if (++octets > 4)
					return (0);
				saw_digit = 1;
			}
			*tp = (unsigned char)_new;
		} else if (ch == '.' && saw_digit) {
			if (octets == 4)
				return (0);
			*++tp = 0;
			saw_digit = 0;
		} else
			return (0);
	}
	if (octets < 4)
		return (0);

	memcpy(dst, tmp, INADDRSZ);
	return (1);
}


bool utl_yaml_read_ip_addr(const YAML::Node& node, 
                           const std::string &name,
                           uint32_t & val){
    std::string tmp;
    uint32_t ip;
    bool res=false;
    if ( node.FindValue(name) ) {
        node[name] >> tmp ;
        if ( my_inet_pton4((char *)tmp.c_str(), (unsigned char *)&ip) ){
            val=PKT_NTOHL(ip);
            res=true;
        }else{
            printf(" ERROR  not a valid ip %s \n",(char *)tmp.c_str());
            exit(-1);
        }
    }
    return (res);
}



bool utl_yaml_read_uint32(const YAML::Node& node, 
                          const std::string &name,
                          uint32_t & val){
    bool res=false;
    if ( node.FindValue(name) ) {
        node[name] >> val ;
        res=true;
    }
    return (res);
}

bool utl_yaml_read_uint16(const YAML::Node& node, 
                          const std::string &name,
                          uint16_t & val){
    uint32_t val_tmp;
    bool res=false;
    if ( node.FindValue(name) ) {
        node[name] >> val_tmp ;
        val = (uint16_t)val_tmp;
        res=true;
    }

    return (res);
}

static void
split_str_by_delimiter(std::string str, char delim, std::vector<std::string> &tokens) {
    size_t pos = 0;
    std::string token;

    while ((pos = str.find(delim)) != std::string::npos) {
        token = str.substr(0, pos);
        tokens.push_back(token);
        str.erase(0, pos + 1);
    }

    if (str.size() > 0) {
        tokens.push_back(str);
    }
}

static bool mac2uint64(const std::string &mac_str, uint64_t &mac_num) {
    std::vector<std::string> tokens;
    uint64_t val;

    split_str_by_delimiter(mac_str, ':', tokens);
    if (tokens.size() != 6) {
        return false;
    }

    val = 0;

    for (int i = 0; i < 6 ; i++) {
        char *endptr = NULL;
        unsigned long octet = strtoul(tokens[i].c_str(), &endptr, 16);

        if ( (*endptr != 0) || (octet > 0xff) ) {
            return false;
        }

        val = (val << 8) + octet;
    }

    mac_num = val;

    return true;
}

/************************
 * YAML Parser Wrapper
 *
 ***********************/
bool
YAMLParserWrapper::parse_bool(const YAML::Node &node, const std::string &name, bool def) {
    if (!node.FindValue(name)) {
        return def;
    }

    return parse_bool(node, name);
}

bool
YAMLParserWrapper::parse_bool(const YAML::Node &node, const std::string &name) {
    try {
        bool val;
        node[name] >> val;
        return (val);

    } catch (const YAML::InvalidScalar &ex) {
        parse_err("expecting true/false for field '" + name + "'", node[name]);

    } catch (const YAML::KeyNotFound &ex) {
        parse_err("cannot locate mandatory field '" + name + "'", node);
    }

    assert(0);
}

const YAML::Node &
YAMLParserWrapper::parse_list(const YAML::Node &node, const std::string &name) {

    try {
        const YAML::Node &val = node[name];
        if (val.Type() != YAML::NodeType::Sequence) {
            throw YAML::InvalidScalar(node[name].GetMark());
        }
        return val;

    } catch (const YAML::InvalidScalar &ex) {
        parse_err("expecting sequence/list for field '" + name + "'", node[name]);

    } catch (const YAML::KeyNotFound &ex) {
        parse_err("cannot locate mandatory field '" + name + "'", node);
    }

    assert(0);
}

const YAML::Node &
YAMLParserWrapper::parse_map(const YAML::Node &node, const std::string &name) {

    try {
        const YAML::Node &val = node[name];
        if (val.Type() != YAML::NodeType::Map) {
            throw YAML::InvalidScalar(node[name].GetMark());
        }
        return val;

    } catch (const YAML::InvalidScalar &ex) {
        parse_err("expecting map for field '" + name + "'", node[name]);

    } catch (const YAML::KeyNotFound &ex) {
        parse_err("cannot locate mandatory field '" + name + "'", node);
    }

    assert(0);
}

uint32_t
YAMLParserWrapper::parse_ip(const YAML::Node &node, const std::string &name) {
    

    try {
        std::string ip_str;
        uint32_t    ip_num;

        node[name] >> ip_str;
        int rc = my_inet_pton4((char *)ip_str.c_str(), (unsigned char *)&ip_num);
        if (!rc) {
            parse_err("invalid IP address: " + ip_str, node[name]);
        }

        return PKT_NTOHL(ip_num);

    } catch (const YAML::InvalidScalar &ex) {
        parse_err("expecting valid IP address for field '" + name + "'", node[name]);

    } catch (const YAML::KeyNotFound &ex) {
        parse_err("cannot locate mandatory field '" + name + "'", node);
    }

    assert(0);
}

uint64_t
YAMLParserWrapper::parse_mac_addr(const YAML::Node &node, const std::string &name, uint64_t def) {
    if (!node.FindValue(name)) {
        return def;
    }

    return parse_mac_addr(node, name);
}

uint64_t
YAMLParserWrapper::parse_mac_addr(const YAML::Node &node, const std::string &name) {

    std::string mac_str;
    uint64_t    mac_num;

    try {

        node[name] >> mac_str;
        bool rc = mac2uint64(mac_str, mac_num);
        if (!rc) {
            parse_err("invalid MAC address: " + mac_str, node[name]);
        }
        return mac_num;

    } catch (const YAML::InvalidScalar &ex) {
        parse_err("expecting true/false for field '" + name + "'", node[name]);

    } catch (const YAML::KeyNotFound &ex) {
        parse_err("cannot locate mandatory field '" + name + "'", node);
    }

    assert(0);
}

uint64_t 
YAMLParserWrapper::parse_uint(const YAML::Node &node, const std::string &name, uint64_t low, uint64_t high, uint64_t def) {
    if (!node.FindValue(name)) {
        return def;
    }

    return parse_uint(node, name, low, high);
}

uint64_t 
YAMLParserWrapper::parse_uint(const YAML::Node &node, const std::string &name, uint64_t low, uint64_t high) {

    try {

        uint64_t val;
        node[name] >> val;

        if ( (val < low) || (val > high) ) {
            std::stringstream ss;
            ss << "valid range for field '" << name << "' is: [" << low << " - " << high << "]";
            parse_err(ss.str(), node[name]);
        }

        return (val);

    } catch (const YAML::InvalidScalar &ex) {
        parse_err("expecting true/false for field '" + name + "'", node[name]);

    } catch (const YAML::KeyNotFound &ex) {
        parse_err("cannot locate mandatory field '" + name + "'", node);
    }

    assert(0);
}

void
YAMLParserWrapper::parse_err(const std::string &err, const YAML::Node &node) const {
    std::stringstream ss;

    ss << "\n*** '" << m_header << "' - YAML parsing error at line " << node.GetMark().line << ": ";
    ss << err;

    throw std::runtime_error(ss.str());
}

void
YAMLParserWrapper::parse_err(const std::string &err) const {
    std::stringstream ss;

    ss << "\n*** '" << m_header << "' - YAML parsing error: " << err;

    throw std::runtime_error(ss.str());
}