]>
git.ipfire.org Git - thirdparty/lldpd.git/blob - tests/common.c
4ca7cb2a0456015f45b017f2f729b971190a40fe
10 #include "../src/lldpd.h"
14 char *filename
= NULL
;
16 char macaddress
[ETH_ALEN
] = { 0x5e, 0x10, 0x8e, 0xe7, 0x84, 0xad };
17 struct lldpd_hardware hardware
;
18 struct lldpd_chassis chassis
;
21 pcap_send(struct lldpd
*cfg
, struct lldpd_hardware
*hardware
,
22 char *buffer
, size_t size
)
24 struct pcaprec_hdr hdr
;
28 /* Write pcap record header */
29 hdr
.ts_sec
= time(NULL
);
31 hdr
.incl_len
= hdr
.orig_len
= size
;
32 n
= write(dump
, &hdr
, sizeof(hdr
));
34 fail("unable to write pcap record header to %s", filename
);
39 n
= write(dump
, buffer
, size
);
41 fail("unable to write pcap data to %s", filename
);
45 /* Append to list of packets */
46 pkt
= (struct packet
*)malloc(size
+ sizeof(TAILQ_HEAD(,packet
)) + sizeof(int));
48 fail("unable to allocate packet");
51 memcpy(pkt
->data
, buffer
, size
);
53 TAILQ_INSERT_TAIL(&pkts
, pkt
, next
);
57 struct lldpd_ops pcap_ops
= {
59 .recv
= NULL
, /* Won't be used */
60 .cleanup
= NULL
, /* Won't be used */
67 static int serial
= 0;
70 /* Prepare packet buffer */
72 /* Open a new dump file */
73 n
= asprintf(&filename
, "%s_%04d.pcap", filenameprefix
, serial
++);
75 fail("unable to compute filename");
78 dump
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
80 fail("unable to open %s", filename
);
83 /* Write a PCAP header */
84 hdr
.magic_number
= 0xa1b2c3d4;
85 hdr
.version_major
= 2;
86 hdr
.version_minor
= 4;
91 n
= write(dump
, &hdr
, sizeof(hdr
));
93 fail("unable to write pcap header to %s", filename
);
96 /* Prepare hardware */
97 memset(&hardware
, 0, sizeof(struct lldpd_hardware
));
98 TAILQ_INIT(&hardware
.h_rports
);
100 TAILQ_INIT(&hardware
.h_lport
.p_vlans
);
101 TAILQ_INIT(&hardware
.h_lport
.p_ppvids
);
102 TAILQ_INIT(&hardware
.h_lport
.p_pids
);
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
;
118 struct packet
*npkt
, *pkt
;
119 for (pkt
= TAILQ_FIRST(&pkts
);
122 npkt
= TAILQ_NEXT(pkt
, next
);
123 TAILQ_REMOVE(&pkts
, pkt
, next
);