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
339
340
341
342
343
344
|
/* -*- mode: c; c-file-style: "openbsd" -*- */
/*
* Copyright (c) 2012 Vincent Bernat <bernat@luffy.cx>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/queue.h>
#include "../lldpd-structs.h"
#include "../compat/compat.h"
#include "../marshal.h"
#include "../ctl.h"
/* connection.c */
struct lldpctl_conn_t {
/* the Unix-domain socket to connect to lldpd */
char *ctlname;
/* Callback handling */
lldpctl_recv_callback recv; /* Receive callback */
lldpctl_send_callback send; /* Send callback */
void *user_data; /* Callback user data */
/* IO state handling. */
uint8_t *input_buffer; /* Current input/output buffer */
uint8_t *output_buffer; /* Current input/output buffer */
size_t input_buffer_len;
size_t output_buffer_len;
#define CONN_STATE_IDLE 0
#define CONN_STATE_GET_INTERFACES_SEND 1
#define CONN_STATE_GET_INTERFACES_RECV 2
#define CONN_STATE_GET_PORT_SEND 3
#define CONN_STATE_GET_PORT_RECV 4
#define CONN_STATE_SET_PORT_SEND 5
#define CONN_STATE_SET_PORT_RECV 6
#define CONN_STATE_SET_WATCH_SEND 7
#define CONN_STATE_SET_WATCH_RECV 8
#define CONN_STATE_GET_CONFIG_SEND 9
#define CONN_STATE_GET_CONFIG_RECV 10
#define CONN_STATE_SET_CONFIG_SEND 11
#define CONN_STATE_SET_CONFIG_RECV 12
#define CONN_STATE_GET_CHASSIS_SEND 13
#define CONN_STATE_GET_CHASSIS_RECV 14
#define CONN_STATE_GET_DEFAULT_PORT_SEND 15
#define CONN_STATE_GET_DEFAULT_PORT_RECV 16
#define CONN_STATE_WATCHING 17
#define CONN_STATE_SET_CHASSIS_SEND 18
#define CONN_STATE_SET_CHASSIS_RECV 19
int state; /* Current state */
/* Data attached to the state. It is used to check that we are using the
* same data as a previous call until the state machine goes to
* CONN_STATE_IDLE. */
char state_data[IFNAMSIZ + 64];
/* Error handling */
lldpctl_error_t error; /* Last error */
/* Handling notifications */
lldpctl_change_callback watch_cb;
lldpctl_change_callback2 watch_cb2;
void *watch_data;
int watch_triggered;
};
/* User data for synchronous callbacks. */
struct lldpctl_conn_sync_t {
int fd; /* File descriptor to the socket. */
};
ssize_t _lldpctl_needs(lldpctl_conn_t *lldpctl, size_t length);
int _lldpctl_do_something(lldpctl_conn_t *conn, int state_send, int state_recv,
const char *state_data, enum hmsg_type type, void *to_send,
struct marshal_info *mi_send, void **to_recv, struct marshal_info *mi_recv);
/* errors.c */
#define SET_ERROR(conn, x) ((conn)->error = x)
#define RESET_ERROR(conn) SET_ERROR((conn), LLDPCTL_NO_ERROR)
/* atom.c and atom-private.c */
typedef enum {
atom_config,
atom_interfaces_list,
atom_interface,
atom_ports_list,
atom_port,
atom_mgmts_list,
atom_mgmt,
#ifdef ENABLE_DOT3
atom_dot3_power,
#endif
#ifdef ENABLE_DOT1
atom_vlans_list,
atom_vlan,
atom_ppvids_list,
atom_ppvid,
atom_pis_list,
atom_pi,
#endif
#ifdef ENABLE_LLDPMED
atom_med_policies_list,
atom_med_policy,
atom_med_locations_list,
atom_med_location,
atom_med_caelements_list,
atom_med_caelement,
atom_med_power,
#endif
#ifdef ENABLE_CUSTOM
atom_custom_list,
atom_custom,
#endif
atom_chassis,
} atom_t;
void *_lldpctl_alloc_in_atom(lldpctl_atom_t *, size_t);
const char *_lldpctl_dump_in_atom(lldpctl_atom_t *, const uint8_t *, size_t, char,
size_t);
struct atom_buffer {
TAILQ_ENTRY(atom_buffer) next;
u_int8_t data[0];
};
struct lldpctl_atom_t {
int count;
atom_t type;
lldpctl_conn_t *conn;
TAILQ_HEAD(, atom_buffer) buffers; /* List of buffers */
/* Destructor */
void (*free)(lldpctl_atom_t *);
/* Iterators */
lldpctl_atom_iter_t *(*iter)(lldpctl_atom_t *);
lldpctl_atom_iter_t *(*next)(lldpctl_atom_t *, lldpctl_atom_iter_t *);
lldpctl_atom_t *(*value)(lldpctl_atom_t *, lldpctl_atom_iter_t *);
/* Getters */
lldpctl_atom_t *(*get)(lldpctl_atom_t *, lldpctl_key_t);
const char *(*get_str)(lldpctl_atom_t *, lldpctl_key_t);
const u_int8_t *(*get_buffer)(lldpctl_atom_t *, lldpctl_key_t, size_t *);
long int (*get_int)(lldpctl_atom_t *, lldpctl_key_t);
/* Setters */
lldpctl_atom_t *(*set)(lldpctl_atom_t *, lldpctl_key_t, lldpctl_atom_t *);
lldpctl_atom_t *(*set_str)(lldpctl_atom_t *, lldpctl_key_t, const char *);
lldpctl_atom_t *(
*set_buffer)(lldpctl_atom_t *, lldpctl_key_t, const u_int8_t *, size_t);
lldpctl_atom_t *(*set_int)(lldpctl_atom_t *, lldpctl_key_t, long int);
lldpctl_atom_t *(*create)(lldpctl_atom_t *);
};
struct _lldpctl_atom_config_t {
lldpctl_atom_t base;
struct lldpd_config *config;
};
struct _lldpctl_atom_interfaces_list_t {
lldpctl_atom_t base;
struct lldpd_interface_list *ifs;
};
struct _lldpctl_atom_interface_t {
lldpctl_atom_t base;
char *name;
};
struct _lldpctl_atom_chassis_t {
lldpctl_atom_t base;
struct lldpd_chassis *chassis;
struct _lldpctl_atom_port_t
*parent; /* Optional: parent of this atom (owning our reference) */
int embedded; /* This atom is "embedded" (not refcounted) */
};
struct _lldpctl_atom_port_t {
lldpctl_atom_t base;
int local; /* Local or remote port? */
struct lldpd_hardware *hardware; /* Local port only (but optional) */
struct lldpd_port *port; /* Local and remote */
struct _lldpctl_atom_port_t *parent; /* Local port if we are a remote port */
lldpctl_atom_t *chassis; /* Internal atom for chassis */
};
/* Can represent any simple list holding just a reference to a port. */
struct _lldpctl_atom_any_list_t {
lldpctl_atom_t base;
struct _lldpctl_atom_port_t *parent;
};
struct _lldpctl_atom_mgmts_list_t {
lldpctl_atom_t base;
lldpctl_atom_t *parent;
struct lldpd_chassis *chassis; /* Chassis containing the list of IP addresses */
};
struct _lldpctl_atom_mgmt_t {
lldpctl_atom_t base;
lldpctl_atom_t *parent;
struct lldpd_mgmt *mgmt;
};
#ifdef ENABLE_DOT3
struct _lldpctl_atom_dot3_power_t {
lldpctl_atom_t base;
struct _lldpctl_atom_port_t *parent;
};
#endif
#ifdef ENABLE_DOT1
struct _lldpctl_atom_vlan_t {
lldpctl_atom_t base;
struct _lldpctl_atom_port_t *parent;
struct lldpd_vlan *vlan;
};
struct _lldpctl_atom_ppvid_t {
lldpctl_atom_t base;
struct _lldpctl_atom_port_t *parent;
struct lldpd_ppvid *ppvid;
};
struct _lldpctl_atom_pi_t {
lldpctl_atom_t base;
struct _lldpctl_atom_port_t *parent;
struct lldpd_pi *pi;
};
#endif
#ifdef ENABLE_LLDPMED
struct _lldpctl_atom_med_policy_t {
lldpctl_atom_t base;
struct _lldpctl_atom_port_t *parent;
struct lldpd_med_policy *policy;
};
struct _lldpctl_atom_med_location_t {
lldpctl_atom_t base;
struct _lldpctl_atom_port_t *parent;
struct lldpd_med_loc *location;
};
/* This list should have the same structure than _llpdctl_atom_any_list_t */
struct _lldpctl_atom_med_caelements_list_t {
lldpctl_atom_t base;
struct _lldpctl_atom_med_location_t *parent;
};
struct _lldpctl_atom_med_caelement_t {
lldpctl_atom_t base;
struct _lldpctl_atom_med_location_t *parent;
int type;
uint8_t *value;
size_t len;
};
struct _lldpctl_atom_med_power_t {
lldpctl_atom_t base;
struct _lldpctl_atom_port_t *parent;
};
#endif
#ifdef ENABLE_CUSTOM
struct _lldpctl_atom_custom_list_t {
lldpctl_atom_t base;
struct _lldpctl_atom_port_t *parent;
struct lldpd_custom_list *list;
};
struct _lldpctl_atom_custom_t {
lldpctl_atom_t base;
struct _lldpctl_atom_port_t *parent;
int op;
struct lldpd_custom *tlv;
};
#endif
struct lldpctl_atom_t *_lldpctl_new_atom(lldpctl_conn_t *conn, atom_t type, ...);
struct atom_map {
int key;
struct atom_map *next;
lldpctl_map_t map[];
};
void atom_map_register(struct atom_map *map, int);
void init_atom_map(void);
#define ATOM_MAP_REGISTER(NAME, PRIO) \
void init_atom_map_##NAME(void); \
void init_atom_map_##NAME(void) \
{ \
atom_map_register(&NAME, PRIO); \
}
struct atom_builder {
atom_t type; /* Atom type */
size_t size; /* Size of structure to allocate */
int (*init)(lldpctl_atom_t *, va_list); /* Optional additional init steps */
void (*free)(lldpctl_atom_t *); /* Optional deallocation steps */
lldpctl_atom_iter_t *(*iter)(
lldpctl_atom_t *); /* Optional, return an iterator for this object */
lldpctl_atom_iter_t *(*next)(lldpctl_atom_t *,
lldpctl_atom_iter_t
*); /* Return the next object for the provided iterator */
lldpctl_atom_t *(*value)(lldpctl_atom_t *,
lldpctl_atom_iter_t
*); /* Return the current object for the provided iterator */
lldpctl_atom_t *(*get)(lldpctl_atom_t *, lldpctl_key_t);
const char *(*get_str)(lldpctl_atom_t *, lldpctl_key_t);
const u_int8_t *(*get_buffer)(lldpctl_atom_t *, lldpctl_key_t, size_t *);
long int (*get_int)(lldpctl_atom_t *, lldpctl_key_t);
lldpctl_atom_t *(*set)(lldpctl_atom_t *, lldpctl_key_t, lldpctl_atom_t *);
lldpctl_atom_t *(*set_str)(lldpctl_atom_t *, lldpctl_key_t, const char *);
lldpctl_atom_t *(
*set_buffer)(lldpctl_atom_t *, lldpctl_key_t, const u_int8_t *, size_t);
lldpctl_atom_t *(*set_int)(lldpctl_atom_t *, lldpctl_key_t, long int);
lldpctl_atom_t *(*create)(lldpctl_atom_t *);
struct atom_builder *nextb;
};
void atom_builder_register(struct atom_builder *builder, int);
void init_atom_builder(void);
#define ATOM_BUILDER_REGISTER(NAME, PRIO) \
void init_atom_builder_##NAME(void); \
void init_atom_builder_##NAME(void) \
{ \
atom_builder_register(&NAME, PRIO); \
}
|