]>
Commit | Line | Data |
---|---|---|
eab6aa62 VB |
1 | /* -*- mode: c; c-file-style: "openbsd" -*- */ |
2 | /* | |
3 | * Copyright (c) 2015 Vincent Bernat <bernat@luffy.cx> | |
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 | ||
4dc61015 VB |
18 | #include <stdlib.h> |
19 | #include <stdio.h> | |
20 | #include <unistd.h> | |
21 | #include <sys/types.h> | |
22 | #include <sys/stat.h> | |
23 | #include <time.h> | |
24 | #include <fcntl.h> | |
25 | #include <check.h> | |
4dc61015 VB |
26 | #include "common.h" |
27 | ||
28 | int dump = -1; | |
29 | char *filename = NULL; | |
30 | struct pkts_t pkts; | |
4e5f34c5 | 31 | char macaddress[ETHER_ADDR_LEN] = { 0x5e, 0x10, 0x8e, 0xe7, 0x84, 0xad }; |
4dc61015 VB |
32 | struct lldpd_hardware hardware; |
33 | struct lldpd_chassis chassis; | |
34 | ||
35 | int | |
36 | pcap_send(struct lldpd *cfg, struct lldpd_hardware *hardware, | |
37 | char *buffer, size_t size) | |
38 | { | |
39 | struct pcaprec_hdr hdr; | |
40 | struct packet *pkt; | |
41 | int n; | |
42 | ||
43 | /* Write pcap record header */ | |
44 | hdr.ts_sec = time(NULL); | |
45 | hdr.ts_usec = 0; | |
46 | hdr.incl_len = hdr.orig_len = size; | |
47 | n = write(dump, &hdr, sizeof(hdr)); | |
48 | if (n == 1) { | |
49 | fail("unable to write pcap record header to %s", filename); | |
50 | return -1; | |
51 | } | |
52 | ||
53 | /* Write data */ | |
54 | n = write(dump, buffer, size); | |
55 | if (n == -1) { | |
56 | fail("unable to write pcap data to %s", filename); | |
57 | return -1; | |
58 | } | |
59 | ||
60 | /* Append to list of packets */ | |
61 | pkt = (struct packet *)malloc(size + sizeof(TAILQ_HEAD(,packet)) + sizeof(int)); | |
62 | if (!pkt) { | |
63 | fail("unable to allocate packet"); | |
64 | return -1; | |
65 | } | |
66 | memcpy(pkt->data, buffer, size); | |
67 | pkt->size = size; | |
68 | TAILQ_INSERT_TAIL(&pkts, pkt, next); | |
69 | return 0; | |
70 | } | |
71 | ||
72 | struct lldpd_ops pcap_ops = { | |
73 | .send = pcap_send, | |
74 | .recv = NULL, /* Won't be used */ | |
75 | .cleanup = NULL, /* Won't be used */ | |
76 | }; | |
77 | ||
78 | ||
79 | void | |
80 | pcap_setup() | |
81 | { | |
82 | static int serial = 0; | |
83 | struct pcap_hdr hdr; | |
84 | int n; | |
85 | /* Prepare packet buffer */ | |
86 | TAILQ_INIT(&pkts); | |
87 | /* Open a new dump file */ | |
88 | n = asprintf(&filename, "%s_%04d.pcap", filenameprefix, serial++); | |
89 | if (n == -1) { | |
90 | fail("unable to compute filename"); | |
91 | return; | |
92 | } | |
93 | dump = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644); | |
94 | if (dump == -1) { | |
95 | fail("unable to open %s", filename); | |
96 | return; | |
97 | } | |
98 | /* Write a PCAP header */ | |
99 | hdr.magic_number = 0xa1b2c3d4; | |
100 | hdr.version_major = 2; | |
101 | hdr.version_minor = 4; | |
102 | hdr.thiszone = 0; | |
103 | hdr.sigfigs = 0; | |
104 | hdr.snaplen = 65535; | |
105 | hdr.network = 1; | |
106 | n = write(dump, &hdr, sizeof(hdr)); | |
107 | if (n == -1) { | |
108 | fail("unable to write pcap header to %s", filename); | |
109 | return; | |
110 | } | |
111 | /* Prepare hardware */ | |
112 | memset(&hardware, 0, sizeof(struct lldpd_hardware)); | |
113 | TAILQ_INIT(&hardware.h_rports); | |
114 | #ifdef ENABLE_DOT1 | |
115 | TAILQ_INIT(&hardware.h_lport.p_vlans); | |
bc99d796 SK |
116 | TAILQ_INIT(&hardware.h_lport.p_ppvids); |
117 | TAILQ_INIT(&hardware.h_lport.p_pids); | |
4dc61015 VB |
118 | #endif |
119 | hardware.h_mtu = 1500; | |
5aa86a41 | 120 | hardware.h_ifindex = 4; |
608b5669 | 121 | strlcpy(hardware.h_ifname, "test", sizeof(hardware.h_ifname)); |
4e5f34c5 | 122 | memcpy(hardware.h_lladdr, macaddress, ETHER_ADDR_LEN); |
4dc61015 VB |
123 | hardware.h_ops = &pcap_ops; |
124 | /* Prepare chassis */ | |
125 | memset(&chassis, 0, sizeof(struct lldpd_chassis)); | |
126 | hardware.h_lport.p_chassis = &chassis; | |
4dc61015 VB |
127 | } |
128 | ||
129 | void | |
130 | pcap_teardown() | |
131 | { | |
132 | struct packet *npkt, *pkt; | |
133 | for (pkt = TAILQ_FIRST(&pkts); | |
134 | pkt != NULL; | |
135 | pkt = npkt) { | |
136 | npkt = TAILQ_NEXT(pkt, next); | |
137 | TAILQ_REMOVE(&pkts, pkt, next); | |
138 | free(pkt); | |
139 | } | |
140 | if (dump != -1) { | |
141 | close(dump); | |
142 | dump = -1; | |
143 | } | |
144 | if (filename) { | |
145 | free(filename); | |
146 | filename = NULL; | |
147 | } | |
148 | } | |
b55bba2f VB |
149 | |
150 | /* Disable leak detection sanitizer */ | |
151 | int __lsan_is_turned_off() { | |
152 | return 1; | |
153 | } |