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
|
/* -*- mode: c; c-file-style: "openbsd" -*- */
/*
* Copyright (c) 2015 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 <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <fcntl.h>
#include <check.h>
#include "common.h"
int dump = -1;
char *filename = NULL;
struct pkts_t pkts;
char macaddress[ETHER_ADDR_LEN] = { 0x5e, 0x10, 0x8e, 0xe7, 0x84, 0xad };
struct lldpd_hardware hardware;
struct lldpd_chassis chassis;
int
pcap_send(struct lldpd *cfg, struct lldpd_hardware *hardware,
char *buffer, size_t size)
{
struct pcaprec_hdr hdr;
struct packet *pkt;
int n;
/* Write pcap record header */
hdr.ts_sec = time(NULL);
hdr.ts_usec = 0;
hdr.incl_len = hdr.orig_len = size;
n = write(dump, &hdr, sizeof(hdr));
if (n == 1) {
fail("unable to write pcap record header to %s", filename);
return -1;
}
/* Write data */
n = write(dump, buffer, size);
if (n == -1) {
fail("unable to write pcap data to %s", filename);
return -1;
}
/* Append to list of packets */
pkt = (struct packet *)malloc(size + sizeof(TAILQ_HEAD(,packet)) + sizeof(int));
if (!pkt) {
fail("unable to allocate packet");
return -1;
}
memcpy(pkt->data, buffer, size);
pkt->size = size;
TAILQ_INSERT_TAIL(&pkts, pkt, next);
return 0;
}
struct lldpd_ops pcap_ops = {
.send = pcap_send,
.recv = NULL, /* Won't be used */
.cleanup = NULL, /* Won't be used */
};
void
pcap_setup()
{
static int serial = 0;
struct pcap_hdr hdr;
int n;
/* Prepare packet buffer */
TAILQ_INIT(&pkts);
/* Open a new dump file */
n = asprintf(&filename, "%s_%04d.pcap", filenameprefix, serial++);
if (n == -1) {
fail("unable to compute filename");
return;
}
dump = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (dump == -1) {
fail("unable to open %s", filename);
return;
}
/* Write a PCAP header */
hdr.magic_number = 0xa1b2c3d4;
hdr.version_major = 2;
hdr.version_minor = 4;
hdr.thiszone = 0;
hdr.sigfigs = 0;
hdr.snaplen = 65535;
hdr.network = 1;
n = write(dump, &hdr, sizeof(hdr));
if (n == -1) {
fail("unable to write pcap header to %s", filename);
return;
}
/* Prepare hardware */
memset(&hardware, 0, sizeof(struct lldpd_hardware));
TAILQ_INIT(&hardware.h_rports);
#ifdef ENABLE_DOT1
TAILQ_INIT(&hardware.h_lport.p_vlans);
TAILQ_INIT(&hardware.h_lport.p_ppvids);
TAILQ_INIT(&hardware.h_lport.p_pids);
#endif
hardware.h_mtu = 1500;
hardware.h_ifindex = 4;
strlcpy(hardware.h_ifname, "test", sizeof(hardware.h_ifname));
memcpy(hardware.h_lladdr, macaddress, ETHER_ADDR_LEN);
hardware.h_ops = &pcap_ops;
/* Prepare chassis */
memset(&chassis, 0, sizeof(struct lldpd_chassis));
hardware.h_lport.p_chassis = &chassis;
}
void
pcap_teardown()
{
struct packet *npkt, *pkt;
for (pkt = TAILQ_FIRST(&pkts);
pkt != NULL;
pkt = npkt) {
npkt = TAILQ_NEXT(pkt, next);
TAILQ_REMOVE(&pkts, pkt, next);
free(pkt);
}
if (dump != -1) {
close(dump);
dump = -1;
}
if (filename) {
free(filename);
filename = NULL;
}
}
/* Disable leak detection sanitizer */
int __lsan_is_turned_off() {
return 1;
}
|