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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "alloc-util.h"
#include "escape.h"
#include "ether-addr-util.h"
#include "hexdecoct.h"
#include "in-addr-util.h"
#include "json-util.h"
#include "lldp-neighbor.h"
#include "memory-util.h"
#include "missing_network.h"
#include "unaligned.h"
static void lldp_neighbor_id_hash_func(const LLDPNeighborID *id, struct siphash *state) {
assert(id);
assert(state);
siphash24_compress_safe(id->chassis_id, id->chassis_id_size, state);
siphash24_compress_typesafe(id->chassis_id_size, state);
siphash24_compress_safe(id->port_id, id->port_id_size, state);
siphash24_compress_typesafe(id->port_id_size, state);
}
int lldp_neighbor_id_compare_func(const LLDPNeighborID *x, const LLDPNeighborID *y) {
assert(x);
assert(y);
return memcmp_nn(x->chassis_id, x->chassis_id_size, y->chassis_id, y->chassis_id_size)
?: memcmp_nn(x->port_id, x->port_id_size, y->port_id, y->port_id_size);
}
DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
lldp_neighbor_hash_ops,
LLDPNeighborID,
lldp_neighbor_id_hash_func,
lldp_neighbor_id_compare_func,
sd_lldp_neighbor,
lldp_neighbor_unlink);
int lldp_neighbor_prioq_compare_func(const void *a, const void *b) {
const sd_lldp_neighbor *x = a, *y = b;
assert(x);
assert(y);
return CMP(x->until, y->until);
}
sd_lldp_neighbor *sd_lldp_neighbor_ref(sd_lldp_neighbor *n) {
if (!n)
return NULL;
assert(n->n_ref > 0 || n->lldp_rx);
n->n_ref++;
return n;
}
static sd_lldp_neighbor *lldp_neighbor_free(sd_lldp_neighbor *n) {
if (!n)
return NULL;
free(n->id.port_id);
free(n->id.chassis_id);
free(n->port_description);
free(n->system_name);
free(n->system_description);
free(n->mud_url);
free(n->chassis_id_as_string);
free(n->port_id_as_string);
return mfree(n);
}
sd_lldp_neighbor *sd_lldp_neighbor_unref(sd_lldp_neighbor *n) {
/* Drops one reference from the neighbor. Note that the object is not freed unless it is already unlinked from
* the sd_lldp object. */
if (!n)
return NULL;
assert(n->n_ref > 0);
n->n_ref--;
if (n->n_ref <= 0 && !n->lldp_rx)
lldp_neighbor_free(n);
return NULL;
}
sd_lldp_neighbor *lldp_neighbor_unlink(sd_lldp_neighbor *n) {
/* Removes the neighbor object from the LLDP object, and frees it if it also has no other reference. */
if (!n)
return NULL;
if (!n->lldp_rx)
return NULL;
/* Only remove the neighbor object from the hash table if it's in there, don't complain if it isn't. This is
* because we are used as destructor call for hashmap_clear() and thus sometimes are called to de-register
* ourselves from the hashtable and sometimes are called after we already are de-registered. */
(void) hashmap_remove_value(n->lldp_rx->neighbor_by_id, &n->id, n);
assert_se(prioq_remove(n->lldp_rx->neighbor_by_expiry, n, &n->prioq_idx) >= 0);
n->lldp_rx = NULL;
if (n->n_ref <= 0)
lldp_neighbor_free(n);
return NULL;
}
sd_lldp_neighbor *lldp_neighbor_new(size_t raw_size) {
sd_lldp_neighbor *n;
if (raw_size > SIZE_MAX - ALIGN(sizeof(sd_lldp_neighbor)))
return NULL;
n = malloc0(ALIGN(sizeof(sd_lldp_neighbor)) + raw_size);
if (!n)
return NULL;
n->raw_size = raw_size;
n->n_ref = 1;
return n;
}
static int parse_string(sd_lldp_rx *lldp_rx, char **s, const void *q, size_t n) {
const char *p = q;
char *k;
assert(s);
assert(p || n == 0);
if (*s) {
log_lldp_rx(lldp_rx, "Found duplicate string, ignoring field.");
return 0;
}
/* Strip trailing NULs, just to be nice */
while (n > 0 && p[n-1] == 0)
n--;
if (n <= 0) /* Ignore empty strings */
return 0;
/* Look for inner NULs */
if (memchr(p, 0, n)) {
log_lldp_rx(lldp_rx, "Found inner NUL in string, ignoring field.");
return 0;
}
/* Let's escape weird chars, for security reasons */
k = cescape_length(p, n);
if (!k)
return log_oom_debug();
free_and_replace(*s, k);
return 1;
}
int lldp_neighbor_parse(sd_lldp_neighbor *n) {
struct ether_header h;
const uint8_t *p;
size_t left;
int r;
assert(n);
if (n->raw_size < sizeof(struct ether_header))
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"Received truncated packet, ignoring.");
memcpy(&h, LLDP_NEIGHBOR_RAW(n), sizeof(h));
if (h.ether_type != htobe16(ETHERTYPE_LLDP))
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"Received packet with wrong type, ignoring.");
if (h.ether_dhost[0] != 0x01 ||
h.ether_dhost[1] != 0x80 ||
h.ether_dhost[2] != 0xc2 ||
h.ether_dhost[3] != 0x00 ||
h.ether_dhost[4] != 0x00 ||
!IN_SET(h.ether_dhost[5], 0x00, 0x03, 0x0e))
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"Received packet with wrong destination address, ignoring.");
memcpy(&n->source_address, h.ether_shost, sizeof(struct ether_addr));
memcpy(&n->destination_address, h.ether_dhost, sizeof(struct ether_addr));
p = (const uint8_t*) LLDP_NEIGHBOR_RAW(n) + sizeof(struct ether_header);
left = n->raw_size - sizeof(struct ether_header);
for (;;) {
uint8_t type;
uint16_t length;
if (left < 2)
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"TLV lacks header, ignoring.");
type = p[0] >> 1;
length = p[1] + (((uint16_t) (p[0] & 1)) << 8);
p += 2, left -= 2;
if (left < length)
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"TLV truncated, ignoring datagram.");
switch (type) {
case SD_LLDP_TYPE_END:
if (length != 0)
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"End marker TLV not zero-sized, ignoring datagram.");
/* Note that after processing the SD_LLDP_TYPE_END left could still be > 0
* as the message may contain padding (see IEEE 802.1AB-2016, sec. 8.5.12) */
goto end_marker;
case SD_LLDP_TYPE_CHASSIS_ID:
if (length < 2 || length > 256)
/* includes the chassis subtype, hence one extra byte */
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"Chassis ID field size out of range, ignoring datagram.");
if (n->id.chassis_id)
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"Duplicate chassis ID field, ignoring datagram.");
n->id.chassis_id = memdup(p, length);
if (!n->id.chassis_id)
return log_oom_debug();
n->id.chassis_id_size = length;
break;
case SD_LLDP_TYPE_PORT_ID:
if (length < 2 || length > 256)
/* includes the port subtype, hence one extra byte */
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"Port ID field size out of range, ignoring datagram.");
if (n->id.port_id)
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"Duplicate port ID field, ignoring datagram.");
n->id.port_id = memdup(p, length);
if (!n->id.port_id)
return log_oom_debug();
n->id.port_id_size = length;
break;
case SD_LLDP_TYPE_TTL:
if (length != 2)
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"TTL field has wrong size, ignoring datagram.");
if (n->has_ttl)
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"Duplicate TTL field, ignoring datagram.");
n->ttl = unaligned_read_be16(p);
n->has_ttl = true;
break;
case SD_LLDP_TYPE_PORT_DESCRIPTION:
r = parse_string(n->lldp_rx, &n->port_description, p, length);
if (r < 0)
return r;
break;
case SD_LLDP_TYPE_SYSTEM_NAME:
r = parse_string(n->lldp_rx, &n->system_name, p, length);
if (r < 0)
return r;
break;
case SD_LLDP_TYPE_SYSTEM_DESCRIPTION:
r = parse_string(n->lldp_rx, &n->system_description, p, length);
if (r < 0)
return r;
break;
case SD_LLDP_TYPE_SYSTEM_CAPABILITIES:
if (length != 4)
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"System capabilities field has wrong size.");
n->system_capabilities = unaligned_read_be16(p);
n->enabled_capabilities = unaligned_read_be16(p + 2);
n->has_capabilities = true;
break;
case SD_LLDP_TYPE_PRIVATE:
if (length < 4)
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"Found private TLV that is too short, ignoring.");
/* RFC 8520: MUD URL */
if (memcmp(p, SD_LLDP_OUI_IANA_MUD, sizeof(SD_LLDP_OUI_IANA_MUD)) == 0) {
r = parse_string(n->lldp_rx, &n->mud_url, p + sizeof(SD_LLDP_OUI_IANA_MUD),
length - sizeof(SD_LLDP_OUI_IANA_MUD));
if (r < 0)
return r;
}
break;
}
p += length, left -= length;
}
end_marker:
if (!n->id.chassis_id || !n->id.port_id || !n->has_ttl)
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"One or more mandatory TLV missing in datagram. Ignoring.");
n->rindex = sizeof(struct ether_header);
return 0;
}
void lldp_neighbor_start_ttl(sd_lldp_neighbor *n) {
assert(n);
if (n->ttl > 0) {
usec_t base;
/* Use the packet's timestamp if there is one known */
base = triple_timestamp_by_clock(&n->timestamp, CLOCK_BOOTTIME);
if (!timestamp_is_set(base))
base = now(CLOCK_BOOTTIME); /* Otherwise, take the current time */
n->until = usec_add(base, n->ttl * USEC_PER_SEC);
} else
n->until = 0;
if (n->lldp_rx)
prioq_reshuffle(n->lldp_rx->neighbor_by_expiry, n, &n->prioq_idx);
}
bool lldp_neighbor_equal(const sd_lldp_neighbor *a, const sd_lldp_neighbor *b) {
if (a == b)
return true;
if (!a || !b)
return false;
if (a->raw_size != b->raw_size)
return false;
return memcmp(LLDP_NEIGHBOR_RAW(a), LLDP_NEIGHBOR_RAW(b), a->raw_size) == 0;
}
int sd_lldp_neighbor_get_source_address(sd_lldp_neighbor *n, struct ether_addr* address) {
assert_return(n, -EINVAL);
assert_return(address, -EINVAL);
*address = n->source_address;
return 0;
}
int sd_lldp_neighbor_get_destination_address(sd_lldp_neighbor *n, struct ether_addr* address) {
assert_return(n, -EINVAL);
assert_return(address, -EINVAL);
*address = n->destination_address;
return 0;
}
int sd_lldp_neighbor_get_chassis_id(sd_lldp_neighbor *n, uint8_t *type, const void **ret, size_t *size) {
assert_return(n, -EINVAL);
assert_return(type, -EINVAL);
assert_return(ret, -EINVAL);
assert_return(size, -EINVAL);
assert(n->id.chassis_id_size > 0);
*type = *(uint8_t*) n->id.chassis_id;
*ret = (uint8_t*) n->id.chassis_id + 1;
*size = n->id.chassis_id_size - 1;
return 0;
}
static int format_mac_address(const void *data, size_t sz, char **ret) {
struct ether_addr a;
char *k;
assert(data || sz <= 0);
if (sz != 7)
return 0;
memcpy(&a, (uint8_t*) data + 1, sizeof(a));
k = new(char, ETHER_ADDR_TO_STRING_MAX);
if (!k)
return -ENOMEM;
*ret = ether_addr_to_string(&a, k);
return 1;
}
static int format_network_address(const void *data, size_t sz, char **ret) {
union in_addr_union a;
int family, r;
if (sz == 6 && ((uint8_t*) data)[1] == 1) {
memcpy(&a.in, (uint8_t*) data + 2, sizeof(a.in));
family = AF_INET;
} else if (sz == 18 && ((uint8_t*) data)[1] == 2) {
memcpy(&a.in6, (uint8_t*) data + 2, sizeof(a.in6));
family = AF_INET6;
} else
return 0;
r = in_addr_to_string(family, &a, ret);
if (r < 0)
return r;
return 1;
}
int sd_lldp_neighbor_get_chassis_id_as_string(sd_lldp_neighbor *n, const char **ret) {
char *k;
int r;
assert_return(n, -EINVAL);
assert_return(ret, -EINVAL);
if (n->chassis_id_as_string) {
*ret = n->chassis_id_as_string;
return 0;
}
assert(n->id.chassis_id_size > 0);
switch (*(uint8_t*) n->id.chassis_id) {
case SD_LLDP_CHASSIS_SUBTYPE_CHASSIS_COMPONENT:
case SD_LLDP_CHASSIS_SUBTYPE_INTERFACE_ALIAS:
case SD_LLDP_CHASSIS_SUBTYPE_PORT_COMPONENT:
case SD_LLDP_CHASSIS_SUBTYPE_INTERFACE_NAME:
case SD_LLDP_CHASSIS_SUBTYPE_LOCALLY_ASSIGNED:
k = cescape_length((char*) n->id.chassis_id + 1, n->id.chassis_id_size - 1);
if (!k)
return -ENOMEM;
goto done;
case SD_LLDP_CHASSIS_SUBTYPE_MAC_ADDRESS:
r = format_mac_address(n->id.chassis_id, n->id.chassis_id_size, &k);
if (r < 0)
return r;
if (r > 0)
goto done;
break;
case SD_LLDP_CHASSIS_SUBTYPE_NETWORK_ADDRESS:
r = format_network_address(n->id.chassis_id, n->id.chassis_id_size, &k);
if (r < 0)
return r;
if (r > 0)
goto done;
break;
}
/* Generic fallback */
k = hexmem(n->id.chassis_id, n->id.chassis_id_size);
if (!k)
return -ENOMEM;
done:
*ret = n->chassis_id_as_string = k;
return 0;
}
int sd_lldp_neighbor_get_port_id(sd_lldp_neighbor *n, uint8_t *type, const void **ret, size_t *size) {
assert_return(n, -EINVAL);
assert_return(type, -EINVAL);
assert_return(ret, -EINVAL);
assert_return(size, -EINVAL);
assert(n->id.port_id_size > 0);
*type = *(uint8_t*) n->id.port_id;
*ret = (uint8_t*) n->id.port_id + 1;
*size = n->id.port_id_size - 1;
return 0;
}
int sd_lldp_neighbor_get_port_id_as_string(sd_lldp_neighbor *n, const char **ret) {
char *k;
int r;
assert_return(n, -EINVAL);
assert_return(ret, -EINVAL);
if (n->port_id_as_string) {
*ret = n->port_id_as_string;
return 0;
}
assert(n->id.port_id_size > 0);
switch (*(uint8_t*) n->id.port_id) {
case SD_LLDP_PORT_SUBTYPE_INTERFACE_ALIAS:
case SD_LLDP_PORT_SUBTYPE_PORT_COMPONENT:
case SD_LLDP_PORT_SUBTYPE_INTERFACE_NAME:
case SD_LLDP_PORT_SUBTYPE_LOCALLY_ASSIGNED:
k = cescape_length((char*) n->id.port_id + 1, n->id.port_id_size - 1);
if (!k)
return -ENOMEM;
goto done;
case SD_LLDP_PORT_SUBTYPE_MAC_ADDRESS:
r = format_mac_address(n->id.port_id, n->id.port_id_size, &k);
if (r < 0)
return r;
if (r > 0)
goto done;
break;
case SD_LLDP_PORT_SUBTYPE_NETWORK_ADDRESS:
r = format_network_address(n->id.port_id, n->id.port_id_size, &k);
if (r < 0)
return r;
if (r > 0)
goto done;
break;
}
/* Generic fallback */
k = hexmem(n->id.port_id, n->id.port_id_size);
if (!k)
return -ENOMEM;
done:
*ret = n->port_id_as_string = k;
return 0;
}
int sd_lldp_neighbor_get_ttl(sd_lldp_neighbor *n, uint16_t *ret_sec) {
assert_return(n, -EINVAL);
assert_return(ret_sec, -EINVAL);
*ret_sec = n->ttl;
return 0;
}
int sd_lldp_neighbor_get_system_name(sd_lldp_neighbor *n, const char **ret) {
assert_return(n, -EINVAL);
assert_return(ret, -EINVAL);
if (!n->system_name)
return -ENODATA;
*ret = n->system_name;
return 0;
}
int sd_lldp_neighbor_get_system_description(sd_lldp_neighbor *n, const char **ret) {
assert_return(n, -EINVAL);
assert_return(ret, -EINVAL);
if (!n->system_description)
return -ENODATA;
*ret = n->system_description;
return 0;
}
int sd_lldp_neighbor_get_port_description(sd_lldp_neighbor *n, const char **ret) {
assert_return(n, -EINVAL);
assert_return(ret, -EINVAL);
if (!n->port_description)
return -ENODATA;
*ret = n->port_description;
return 0;
}
int sd_lldp_neighbor_get_mud_url(sd_lldp_neighbor *n, const char **ret) {
assert_return(n, -EINVAL);
assert_return(ret, -EINVAL);
if (!n->mud_url)
return -ENODATA;
*ret = n->mud_url;
return 0;
}
int sd_lldp_neighbor_get_system_capabilities(sd_lldp_neighbor *n, uint16_t *ret) {
assert_return(n, -EINVAL);
assert_return(ret, -EINVAL);
if (!n->has_capabilities)
return -ENODATA;
*ret = n->system_capabilities;
return 0;
}
int sd_lldp_neighbor_get_enabled_capabilities(sd_lldp_neighbor *n, uint16_t *ret) {
assert_return(n, -EINVAL);
assert_return(ret, -EINVAL);
if (!n->has_capabilities)
return -ENODATA;
*ret = n->enabled_capabilities;
return 0;
}
int sd_lldp_neighbor_tlv_rewind(sd_lldp_neighbor *n) {
assert_return(n, -EINVAL);
assert(n->raw_size >= sizeof(struct ether_header));
n->rindex = sizeof(struct ether_header);
return n->rindex < n->raw_size;
}
int sd_lldp_neighbor_tlv_next(sd_lldp_neighbor *n) {
size_t length;
assert_return(n, -EINVAL);
if (n->rindex == n->raw_size) /* EOF */
return -ESPIPE;
if (n->rindex + 2 > n->raw_size) /* Truncated message */
return -EBADMSG;
length = LLDP_NEIGHBOR_TLV_LENGTH(n);
if (n->rindex + 2 + length > n->raw_size)
return -EBADMSG;
n->rindex += 2 + length;
return n->rindex < n->raw_size;
}
int sd_lldp_neighbor_tlv_get_type(sd_lldp_neighbor *n, uint8_t *type) {
assert_return(n, -EINVAL);
assert_return(type, -EINVAL);
if (n->rindex == n->raw_size) /* EOF */
return -ESPIPE;
if (n->rindex + 2 > n->raw_size)
return -EBADMSG;
*type = LLDP_NEIGHBOR_TLV_TYPE(n);
return 0;
}
int sd_lldp_neighbor_tlv_is_type(sd_lldp_neighbor *n, uint8_t type) {
uint8_t k;
int r;
assert_return(n, -EINVAL);
r = sd_lldp_neighbor_tlv_get_type(n, &k);
if (r < 0)
return r;
return type == k;
}
int sd_lldp_neighbor_tlv_get_oui(sd_lldp_neighbor *n, uint8_t oui[_SD_ARRAY_STATIC 3], uint8_t *subtype) {
const uint8_t *d;
size_t length;
int r;
assert_return(n, -EINVAL);
assert_return(oui, -EINVAL);
assert_return(subtype, -EINVAL);
r = sd_lldp_neighbor_tlv_is_type(n, SD_LLDP_TYPE_PRIVATE);
if (r < 0)
return r;
if (r == 0)
return -ENXIO;
length = LLDP_NEIGHBOR_TLV_LENGTH(n);
if (length < 4)
return -EBADMSG;
if (n->rindex + 2 + length > n->raw_size)
return -EBADMSG;
d = LLDP_NEIGHBOR_TLV_DATA(n);
memcpy(oui, d, 3);
*subtype = d[3];
return 0;
}
int sd_lldp_neighbor_tlv_is_oui(sd_lldp_neighbor *n, const uint8_t oui[_SD_ARRAY_STATIC 3], uint8_t subtype) {
uint8_t k[3], st;
int r;
r = sd_lldp_neighbor_tlv_get_oui(n, k, &st);
if (r == -ENXIO)
return 0;
if (r < 0)
return r;
return memcmp(k, oui, 3) == 0 && st == subtype;
}
int sd_lldp_neighbor_tlv_get_raw(sd_lldp_neighbor *n, const void **ret, size_t *size) {
size_t length;
assert_return(n, -EINVAL);
assert_return(ret, -EINVAL);
assert_return(size, -EINVAL);
/* Note that this returns the full TLV, including the TLV header */
if (n->rindex + 2 > n->raw_size)
return -EBADMSG;
length = LLDP_NEIGHBOR_TLV_LENGTH(n);
if (n->rindex + 2 + length > n->raw_size)
return -EBADMSG;
*ret = (uint8_t*) LLDP_NEIGHBOR_RAW(n) + n->rindex;
*size = length + 2;
return 0;
}
int sd_lldp_neighbor_get_timestamp(sd_lldp_neighbor *n, clockid_t clock, uint64_t *ret) {
assert_return(n, -EINVAL);
assert_return(TRIPLE_TIMESTAMP_HAS_CLOCK(clock), -EOPNOTSUPP);
assert_return(clock_supported(clock), -EOPNOTSUPP);
assert_return(ret, -EINVAL);
if (!triple_timestamp_is_set(&n->timestamp))
return -ENODATA;
*ret = triple_timestamp_by_clock(&n->timestamp, clock);
return 0;
}
int lldp_neighbor_build_json(sd_lldp_neighbor *n, sd_json_variant **ret) {
const char *chassis_id = NULL, *port_id = NULL, *port_description = NULL,
*system_name = NULL, *system_description = NULL;
uint16_t cc = 0;
bool valid_cc;
assert(n);
assert(ret);
(void) sd_lldp_neighbor_get_chassis_id_as_string(n, &chassis_id);
(void) sd_lldp_neighbor_get_port_id_as_string(n, &port_id);
(void) sd_lldp_neighbor_get_port_description(n, &port_description);
(void) sd_lldp_neighbor_get_system_name(n, &system_name);
(void) sd_lldp_neighbor_get_system_description(n, &system_description);
valid_cc = sd_lldp_neighbor_get_enabled_capabilities(n, &cc) >= 0;
return sd_json_buildo(
ret,
JSON_BUILD_PAIR_STRING_NON_EMPTY("ChassisID", chassis_id),
SD_JSON_BUILD_PAIR_BYTE_ARRAY("RawChassisID", n->id.chassis_id, n->id.chassis_id_size),
JSON_BUILD_PAIR_STRING_NON_EMPTY("PortID", port_id),
SD_JSON_BUILD_PAIR_BYTE_ARRAY("RawPortID", n->id.port_id, n->id.port_id_size),
JSON_BUILD_PAIR_STRING_NON_EMPTY("PortDescription", port_description),
JSON_BUILD_PAIR_STRING_NON_EMPTY("SystemName", system_name),
JSON_BUILD_PAIR_STRING_NON_EMPTY("SystemDescription", system_description),
SD_JSON_BUILD_PAIR_CONDITION(valid_cc, "EnabledCapabilities", SD_JSON_BUILD_UNSIGNED(cc)));
}
|