]> git.ipfire.org Git - thirdparty/lldpd.git/blob - tests/common.c
4ca7cb2a0456015f45b017f2f729b971190a40fe
[thirdparty/lldpd.git] / tests / common.c
1 #define _GNU_SOURCE 1
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <time.h>
8 #include <fcntl.h>
9 #include <check.h>
10 #include "../src/lldpd.h"
11 #include "common.h"
12
13 int dump = -1;
14 char *filename = NULL;
15 struct pkts_t pkts;
16 char macaddress[ETH_ALEN] = { 0x5e, 0x10, 0x8e, 0xe7, 0x84, 0xad };
17 struct lldpd_hardware hardware;
18 struct lldpd_chassis chassis;
19
20 int
21 pcap_send(struct lldpd *cfg, struct lldpd_hardware *hardware,
22 char *buffer, size_t size)
23 {
24 struct pcaprec_hdr hdr;
25 struct packet *pkt;
26 int n;
27
28 /* Write pcap record header */
29 hdr.ts_sec = time(NULL);
30 hdr.ts_usec = 0;
31 hdr.incl_len = hdr.orig_len = size;
32 n = write(dump, &hdr, sizeof(hdr));
33 if (n == 1) {
34 fail("unable to write pcap record header to %s", filename);
35 return -1;
36 }
37
38 /* Write data */
39 n = write(dump, buffer, size);
40 if (n == -1) {
41 fail("unable to write pcap data to %s", filename);
42 return -1;
43 }
44
45 /* Append to list of packets */
46 pkt = (struct packet *)malloc(size + sizeof(TAILQ_HEAD(,packet)) + sizeof(int));
47 if (!pkt) {
48 fail("unable to allocate packet");
49 return -1;
50 }
51 memcpy(pkt->data, buffer, size);
52 pkt->size = size;
53 TAILQ_INSERT_TAIL(&pkts, pkt, next);
54 return 0;
55 }
56
57 struct lldpd_ops pcap_ops = {
58 .send = pcap_send,
59 .recv = NULL, /* Won't be used */
60 .cleanup = NULL, /* Won't be used */
61 };
62
63
64 void
65 pcap_setup()
66 {
67 static int serial = 0;
68 struct pcap_hdr hdr;
69 int n;
70 /* Prepare packet buffer */
71 TAILQ_INIT(&pkts);
72 /* Open a new dump file */
73 n = asprintf(&filename, "%s_%04d.pcap", filenameprefix, serial++);
74 if (n == -1) {
75 fail("unable to compute filename");
76 return;
77 }
78 dump = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
79 if (dump == -1) {
80 fail("unable to open %s", filename);
81 return;
82 }
83 /* Write a PCAP header */
84 hdr.magic_number = 0xa1b2c3d4;
85 hdr.version_major = 2;
86 hdr.version_minor = 4;
87 hdr.thiszone = 0;
88 hdr.sigfigs = 0;
89 hdr.snaplen = 65535;
90 hdr.network = 1;
91 n = write(dump, &hdr, sizeof(hdr));
92 if (n == -1) {
93 fail("unable to write pcap header to %s", filename);
94 return;
95 }
96 /* Prepare hardware */
97 memset(&hardware, 0, sizeof(struct lldpd_hardware));
98 TAILQ_INIT(&hardware.h_rports);
99 #ifdef ENABLE_DOT1
100 TAILQ_INIT(&hardware.h_lport.p_vlans);
101 TAILQ_INIT(&hardware.h_lport.p_ppvids);
102 TAILQ_INIT(&hardware.h_lport.p_pids);
103 #endif
104 hardware.h_mtu = 1500;
105 hardware.h_ifindex = 4;
106 strcpy(hardware.h_ifname, "test");
107 memcpy(hardware.h_lladdr, macaddress, ETH_ALEN);
108 hardware.h_ops = &pcap_ops;
109 /* Prepare chassis */
110 memset(&chassis, 0, sizeof(struct lldpd_chassis));
111 hardware.h_lport.p_chassis = &chassis;
112 chassis.c_ttl = 180;
113 }
114
115 void
116 pcap_teardown()
117 {
118 struct packet *npkt, *pkt;
119 for (pkt = TAILQ_FIRST(&pkts);
120 pkt != NULL;
121 pkt = npkt) {
122 npkt = TAILQ_NEXT(pkt, next);
123 TAILQ_REMOVE(&pkts, pkt, next);
124 free(pkt);
125 }
126 if (dump != -1) {
127 close(dump);
128 dump = -1;
129 }
130 if (filename) {
131 free(filename);
132 filename = NULL;
133 }
134 }