]> git.ipfire.org Git - thirdparty/lldpd.git/blame - src/lib/atoms/mgmt.c
lib: expose management interface index
[thirdparty/lldpd.git] / src / lib / atoms / mgmt.c
CommitLineData
94c98157
AA
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"
94c98157 24#include "../log.h"
a5f98717 25#include "atom.h"
94c98157
AA
26#include "helpers.h"
27
28static int
29_lldpctl_atom_new_mgmts_list(lldpctl_atom_t *atom, va_list ap)
30{
31 struct _lldpctl_atom_mgmts_list_t *plist =
32 (struct _lldpctl_atom_mgmts_list_t *)atom;
99ef55d3 33 plist->parent = va_arg(ap, lldpctl_atom_t *);
94c98157 34 plist->chassis = va_arg(ap, struct lldpd_chassis *);
99ef55d3 35 lldpctl_atom_inc_ref(plist->parent);
94c98157
AA
36 return 1;
37}
38
39static void
40_lldpctl_atom_free_mgmts_list(lldpctl_atom_t *atom)
41{
42 struct _lldpctl_atom_mgmts_list_t *plist =
43 (struct _lldpctl_atom_mgmts_list_t *)atom;
99ef55d3 44 lldpctl_atom_dec_ref(plist->parent);
94c98157
AA
45}
46
47static lldpctl_atom_iter_t*
48_lldpctl_atom_iter_mgmts_list(lldpctl_atom_t *atom)
49{
50 struct _lldpctl_atom_mgmts_list_t *plist =
51 (struct _lldpctl_atom_mgmts_list_t *)atom;
52 return (lldpctl_atom_iter_t*)TAILQ_FIRST(&plist->chassis->c_mgmt);
53}
54
55static lldpctl_atom_iter_t*
56_lldpctl_atom_next_mgmts_list(lldpctl_atom_t *atom, lldpctl_atom_iter_t *iter)
57{
58 struct lldpd_mgmt *mgmt = (struct lldpd_mgmt *)iter;
59 return (lldpctl_atom_iter_t*)TAILQ_NEXT(mgmt, m_entries);
60}
61
62static lldpctl_atom_t*
63_lldpctl_atom_value_mgmts_list(lldpctl_atom_t *atom, lldpctl_atom_iter_t *iter)
64{
65 struct _lldpctl_atom_mgmts_list_t *plist =
66 (struct _lldpctl_atom_mgmts_list_t *)atom;
67 struct lldpd_mgmt *mgmt = (struct lldpd_mgmt *)iter;
68 return _lldpctl_new_atom(atom->conn, atom_mgmt, plist->parent, mgmt);
69}
70
71static int
72_lldpctl_atom_new_mgmt(lldpctl_atom_t *atom, va_list ap)
73{
74 struct _lldpctl_atom_mgmt_t *mgmt =
75 (struct _lldpctl_atom_mgmt_t *)atom;
99ef55d3 76 mgmt->parent = va_arg(ap, lldpctl_atom_t *);
94c98157 77 mgmt->mgmt = va_arg(ap, struct lldpd_mgmt *);
99ef55d3 78 lldpctl_atom_inc_ref(mgmt->parent);
94c98157
AA
79 return 1;
80}
81
82static void
83_lldpctl_atom_free_mgmt(lldpctl_atom_t *atom)
84{
85 struct _lldpctl_atom_mgmt_t *mgmt =
86 (struct _lldpctl_atom_mgmt_t *)atom;
99ef55d3 87 lldpctl_atom_dec_ref(mgmt->parent);
94c98157
AA
88}
89
d319b0de
VB
90static long int
91_lldpctl_atom_get_int_mgmt(lldpctl_atom_t *atom, lldpctl_key_t key)
92{
93 struct _lldpctl_atom_mgmt_t *m =
94 (struct _lldpctl_atom_mgmt_t *)atom;
95
96 /* Local and remote port */
97 switch (key) {
98 case lldpctl_k_mgmt_iface_index:
99 return m->mgmt->m_iface;
100 default:
101 return SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST);
102 }
103}
104
94c98157
AA
105static const char*
106_lldpctl_atom_get_str_mgmt(lldpctl_atom_t *atom, lldpctl_key_t key)
107{
108 char *ipaddress = NULL;
109 size_t len; int af;
110 struct _lldpctl_atom_mgmt_t *m =
111 (struct _lldpctl_atom_mgmt_t *)atom;
112
113 /* Local and remote port */
114 switch (key) {
115 case lldpctl_k_mgmt_ip:
116 switch (m->mgmt->m_family) {
117 case LLDPD_AF_IPV4:
118 len = INET_ADDRSTRLEN + 1;
119 af = AF_INET;
120 break;
121 case LLDPD_AF_IPV6:
122 len = INET6_ADDRSTRLEN + 1;
123 af = AF_INET6;
124 break;
125 default:
126 len = 0;
127 }
128 if (len == 0) break;
129 ipaddress = _lldpctl_alloc_in_atom(atom, len);
130 if (!ipaddress) return NULL;
131 if (inet_ntop(af, &m->mgmt->m_addr, ipaddress, len) == NULL)
132 break;
133 return ipaddress;
134 default:
135 SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST);
136 return NULL;
137 }
138 SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST);
139 return NULL;
140}
141
142static struct atom_builder mgmts_list =
143 { atom_mgmts_list, sizeof(struct _lldpctl_atom_mgmts_list_t),
144 .init = _lldpctl_atom_new_mgmts_list,
145 .free = _lldpctl_atom_free_mgmts_list,
146 .iter = _lldpctl_atom_iter_mgmts_list,
147 .next = _lldpctl_atom_next_mgmts_list,
148 .value = _lldpctl_atom_value_mgmts_list };
149
150static struct atom_builder mgmt =
151 { atom_mgmt, sizeof(struct _lldpctl_atom_mgmt_t),
152 .init = _lldpctl_atom_new_mgmt,
153 .free = _lldpctl_atom_free_mgmt,
d319b0de 154 .get_int = _lldpctl_atom_get_int_mgmt,
94c98157
AA
155 .get_str = _lldpctl_atom_get_str_mgmt };
156
157ATOM_BUILDER_REGISTER(mgmts_list, 6);
158ATOM_BUILDER_REGISTER(mgmt, 7);
159