Line data Source code
1 : /*
2 : * vt_gen_lib.c --
3 : *
4 : * Copyright(c) 2015, Juniper Networks, Inc.
5 : * All rights reserved
6 : */
7 : #include <stdio.h>
8 : #include <string.h>
9 : #include <inttypes.h>
10 : #include <stdlib.h>
11 : #include <stdbool.h>
12 : #include <errno.h>
13 : #include <ctype.h>
14 : #include <strings.h>
15 :
16 : #include <vr_types.h>
17 : #include <vt_gen_lib.h>
18 :
19 : unsigned char *
20 0 : vt_gen_skip_space(unsigned char *string)
21 : {
22 0 : unsigned int i = 0, len;
23 :
24 0 : if (!string)
25 0 : return string;
26 :
27 0 : len = strlen(string);
28 0 : if (!len)
29 0 : return string;
30 :
31 0 : while ((i < len) && isspace(string[i])) {
32 0 : i++;
33 : }
34 :
35 0 : if (i == len)
36 0 : return NULL;
37 :
38 0 : return &string[i];
39 : }
40 :
41 : unsigned char *
42 0 : vt_gen_skip_char(unsigned char *string, unsigned char c)
43 : {
44 0 : unsigned int i = 0, len;
45 :
46 0 : if (!string)
47 0 : return string;
48 :
49 0 : len = strlen(string);
50 0 : if (!len)
51 0 : return string;
52 :
53 0 : while ((i < len) && string[i] == c) {
54 0 : i++;
55 : }
56 :
57 0 : if (i == len)
58 0 : return NULL;
59 :
60 0 : return &string[i];
61 : }
62 :
63 : unsigned char *
64 0 : vt_gen_reach_char(unsigned char *string, unsigned char c)
65 : {
66 0 : unsigned int i = 0, len;
67 :
68 0 : if (!string)
69 0 : return string;
70 :
71 0 : len = strlen(string);
72 0 : if (!len)
73 0 : return string;
74 :
75 0 : while ((i < len) && (string[i] != c)) {
76 0 : i++;
77 : }
78 :
79 0 : return &string[i];
80 : }
81 :
82 : unsigned char *
83 0 : vt_gen_reach_space(unsigned char *string)
84 : {
85 0 : return vt_gen_reach_char(string, ' ');
86 : }
87 :
88 : bool
89 0 : vt_gen_byte_compare(uint8_t one, uint8_t two)
90 : {
91 0 : return one == two;
92 : }
93 :
94 : bool
95 0 : vt_gen_short_compare(uint16_t one, uint16_t two)
96 : {
97 0 : return one == two;
98 : }
99 :
100 : bool
101 0 : vt_gen_int_compare(unsigned int one, unsigned int two)
102 : {
103 0 : return one == two;
104 : }
105 :
106 : bool
107 0 : vt_gen_int64_compare(uint64_t one, uint64_t two)
108 : {
109 0 : return one == two;
110 : }
111 :
112 : bool
113 0 : vt_gen_flow_op_compare(int op, unsigned char *string)
114 : {
115 0 : int expected_op = 0;
116 :
117 0 : if (!string)
118 0 : return -1;
119 :
120 0 : if (!strncasecmp(string, "flow_set", strlen("flow_set")) ||
121 0 : !strncasecmp(string, "0", strlen("0"))) {
122 0 : expected_op = FLOW_OP_FLOW_SET;
123 0 : } else if (!strncasecmp(string, "flow_table_get",
124 0 : strlen("flow_table_get")) ||
125 0 : !strncasecmp(string, "2", strlen("2"))) {
126 0 : expected_op = FLOW_OP_FLOW_TABLE_GET;
127 : }
128 :
129 0 : return op == expected_op;
130 : }
131 :
132 : int
133 322 : vt_gen_flow_op(unsigned char *string)
134 : {
135 322 : if (!string)
136 0 : return -1;
137 :
138 322 : if (!strncasecmp(string, "flow_set", strlen("flow_set")) ||
139 322 : !strncasecmp(string, "0", strlen("0"))) {
140 322 : return FLOW_OP_FLOW_SET;
141 0 : } else if (!strncasecmp(string, "flow_table_get",
142 0 : strlen("flow_table_get")) ||
143 0 : !strncasecmp(string, "2", strlen("2"))) {
144 0 : return FLOW_OP_FLOW_TABLE_GET;
145 : }
146 :
147 0 : return -1;
148 : }
149 :
150 : bool
151 0 : vt_gen_op_compare(int op, unsigned char *string)
152 : {
153 0 : int expected_op = 0;
154 :
155 0 : if (!strncasecmp(string, "Add", strlen("Add"))) {
156 0 : expected_op = SANDESH_OP_ADD;
157 0 : } else if (!strncasecmp(string, "Get", strlen("Get"))) {
158 0 : expected_op = SANDESH_OP_GET;
159 0 : } else if (!strncasecmp(string, "Delete", strlen("Delete"))) {
160 0 : expected_op = SANDESH_OP_DEL;
161 0 : } else if (!strncasecmp(string, "Dump", strlen("Dump"))) {
162 0 : expected_op = SANDESH_OP_DUMP;
163 0 : } else if (!strncasecmp(string, "Reset", strlen("Reset"))) {
164 0 : expected_op = SANDESH_OP_RESET;
165 : }
166 :
167 0 : return op == expected_op;
168 : }
169 :
170 : int
171 2472 : vt_gen_op(unsigned char *string)
172 : {
173 2472 : if (!string)
174 0 : return -1;
175 :
176 2472 : if (!strncasecmp(string, "Add", strlen("Add")) ||
177 2472 : !strncasecmp(string, "0", strlen("0"))) {
178 863 : return SANDESH_OP_ADD;
179 1609 : } else if (!strncasecmp(string, "Get", strlen("Get")) ||
180 1609 : !strncasecmp(string, "1", strlen("1"))) {
181 483 : return SANDESH_OP_GET;
182 1126 : } else if (!strncasecmp(string, "Delete", strlen("Delete")) ||
183 1126 : !strncasecmp(string, "2", strlen("2"))) {
184 1122 : return SANDESH_OP_DEL;
185 4 : } else if (!strncasecmp(string, "Dump", strlen("Dump")) ||
186 4 : !strncasecmp(string, "3", strlen("3"))) {
187 0 : return SANDESH_OP_DUMP;
188 4 : } else if (!strncasecmp(string, "Reset", strlen("Reset")) ||
189 4 : !strncasecmp(string, "5", strlen("5"))) {
190 4 : return SANDESH_OP_RESET;
191 : }
192 :
193 0 : return -1;
194 : }
195 :
196 :
197 : /*TODO
198 : *
199 : * Create a generic function -> delimiter and base
200 : * */
201 :
202 : void *
203 0 : vt_gen_list(unsigned char *list, unsigned int type, unsigned int *list_size)
204 : {
205 0 : unsigned int i = 0, j = 0, type_size;
206 : unsigned int string_size;
207 0 : unsigned char *local_list = list;
208 : unsigned char *end;
209 : char *tmp;
210 0 : char *is_colon = NULL;
211 :
212 : unsigned char *store;
213 : uint8_t *store_b;
214 : uint16_t *store_i16;
215 : uint32_t *store_i32;
216 : uint64_t *store_i64;
217 :
218 0 : *list_size = 0;
219 0 : if (!list)
220 0 : return NULL;
221 :
222 0 : switch (type) {
223 0 : case GEN_TYPE_U8:
224 0 : type_size = 1;
225 0 : break;
226 :
227 0 : case GEN_TYPE_U16:
228 0 : type_size = 2;
229 0 : break;
230 :
231 0 : case GEN_TYPE_U32:
232 0 : type_size = 4;
233 0 : break;
234 :
235 0 : case GEN_TYPE_U64:
236 0 : type_size = 8;
237 0 : break;
238 :
239 0 : default:
240 0 : return NULL;
241 : }
242 :
243 0 : string_size = strlen(list);
244 0 : end = list + string_size;
245 :
246 :
247 :
248 : while (1) {
249 0 : local_list = vt_gen_skip_char(local_list, ':');
250 0 : if (!strlen(local_list))
251 0 : break;
252 :
253 0 : j++;
254 :
255 0 : local_list = vt_gen_reach_char(local_list, ':');
256 0 : if (!local_list)
257 0 : break;
258 : }
259 :
260 :
261 0 : store = calloc(j, type_size);
262 0 : if (!store)
263 0 : return NULL;
264 :
265 0 : *list_size = type_size * j;
266 :
267 0 : store_b = (uint8_t *)store;
268 0 : store_i16 = (uint16_t *)store;
269 0 : store_i32 = (uint32_t *)store;
270 0 : store_i64 = (uint64_t *)store;
271 :
272 0 : local_list = list;
273 0 : for (i = 0; i < j; i++) {
274 0 : switch (type) {
275 0 : case GEN_TYPE_U8:
276 : /*TODO For now only mac addresses are mapped to base 16*/
277 0 : store_b[i] = strtoul(local_list, &tmp, 16);
278 0 : break;
279 :
280 0 : case GEN_TYPE_U16:
281 0 : store_i16[i] = strtoul(local_list, &tmp, 0);
282 0 : break;
283 :
284 0 : case GEN_TYPE_U32:
285 0 : store_i32[i] = strtoul(local_list, &tmp, 0);
286 0 : break;
287 :
288 0 : case GEN_TYPE_U64:
289 0 : store_i64[i] = strtoul(local_list, &tmp, 0);
290 0 : break;
291 : }
292 :
293 0 : if (errno == ERANGE) {
294 0 : free(store);
295 0 : return NULL;
296 : }
297 :
298 0 : local_list = tmp;
299 :
300 0 : local_list = vt_gen_skip_char(local_list, ':');
301 :
302 0 : if (!local_list)
303 0 : break;
304 : }
305 :
306 0 : return store;
307 : }
308 :
309 : bool
310 0 : vt_gen_list_compare(void *list, unsigned int list_size,
311 : unsigned char *string, unsigned int type)
312 : {
313 : unsigned int buf_size;
314 : void *buf;
315 :
316 0 : buf = vt_gen_list(string, type, &buf_size);
317 0 : if (!buf)
318 0 : return false;
319 :
320 0 : if (buf_size != list_size)
321 0 : return false;
322 :
323 0 : if (memcmp(list, buf, list_size))
324 0 : return false;
325 :
326 0 : return true;
327 : }
328 :
329 : void *
330 826 : vt_gen_string(char *string)
331 : {
332 : unsigned int string_len;
333 : char *store;
334 :
335 826 : string_len = strlen(string);
336 826 : store = malloc(string_len + 1);
337 826 : if (!store)
338 0 : return NULL;
339 :
340 826 : strcpy(store, string);
341 :
342 826 : return store;
343 : }
344 :
|