1 /* -*- mode: c; c-file-style: "openbsd" -*- */
2 /*
3 * Copyright (c) 2015 Vincent Bernat <vincent@bernat.im>
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18 #include <stdio.h>
19 #include <stdarg.h>
20 #include <string.h>
21 #include <arpa/inet.h>
22
23 #include "lldpctl.h"
24 #include "../log.h"
25 #include "atom.h"
26 #include "helpers.h"
27
28 const char*
map_lookup(lldpctl_map_t * list,int n)29 map_lookup(lldpctl_map_t *list, int n)
30 {
31
32 unsigned int i;
33
34 for (i = 0; list[i].string != NULL; i++) {
35 if (list[i].value == n) {
36 return list[i].string;
37 }
38 }
39
40 return "unknown";
41 }
42
43 int
map_reverse_lookup(lldpctl_map_t * list,const char * string)44 map_reverse_lookup(lldpctl_map_t *list, const char *string)
45 {
46 if (!string) return -1;
47
48 for (unsigned int i = 0; list[i].string != NULL; i++) {
49 if (!strcasecmp(list[i].string, string))
50 return list[i].value;
51 }
52
53 return -1;
54 }
55
56 int
_lldpctl_atom_new_any_list(lldpctl_atom_t * atom,va_list ap)57 _lldpctl_atom_new_any_list(lldpctl_atom_t *atom, va_list ap)
58 {
59 struct _lldpctl_atom_any_list_t *plist =
60 (struct _lldpctl_atom_any_list_t *)atom;
61 plist->parent = va_arg(ap, struct _lldpctl_atom_port_t *);
62 lldpctl_atom_inc_ref((lldpctl_atom_t *)plist->parent);
63 return 1;
64 }
65
66 void
_lldpctl_atom_free_any_list(lldpctl_atom_t * atom)67 _lldpctl_atom_free_any_list(lldpctl_atom_t *atom)
68 {
69 struct _lldpctl_atom_any_list_t *plist =
70 (struct _lldpctl_atom_any_list_t *)atom;
71 lldpctl_atom_dec_ref((lldpctl_atom_t *)plist->parent);
72 }
73
74