Page MenuHomeFreeBSD

bpf BIOCSHDRCMPLT check
ActivePublic

Authored by melifaro on Nov 7 2015, 7:29 PM.
Tags
None
Referenced Files
F282618: bpf BIOCSHDRCMPLT check
Feb 24 2016, 5:34 AM
Subscribers
None
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <err.h>
#include <fcntl.h>
#include <sysexits.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/bpf.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <net/if_arp.h>
#define BPF_PATH "/dev/bpf"
static void
send_arp(int fd)
{
char buf[128];
struct ether_header *eh;
struct arphdr *ah;
struct in_addr sip, tip;
int len, ret;
memset(buf, 0, sizeof(buf));
eh = (struct ether_header *)buf;
ah = (struct arphdr *)(eh + 1);
eh->ether_type = htons(ETHERTYPE_ARP);
memcpy(eh->ether_shost, ether_aton("00:01:00:02:00:03"), sizeof(eh->ether_shost));
memcpy(eh->ether_dhost, ether_aton("00:02:00:04:00:04"), sizeof(eh->ether_dhost));
inet_pton(AF_INET, "1.2.3.4", &sip);
inet_pton(AF_INET, "2.3.4.5", &tip);
ah->ar_hrd = ARPHRD_ETHER;
ah->ar_pro = htons(ETHERTYPE_IP);
ah->ar_hln = 6;
ah->ar_pln = sizeof(struct in_addr);
ah->ar_op = htons(ARPOP_REQUEST);
bcopy(eh->ether_shost, ar_sha(ah), ah->ar_hln);
bcopy(&sip, ar_spa(ah), ah->ar_pln);
bcopy(&tip, ar_tpa(ah), ah->ar_pln);
len = sizeof(*eh) + sizeof(*ah) + 2 * sizeof(struct in_addr) + 2 * 6;
if ((ret = write(fd, buf, len)) != len)
warn("write() error");
}
int
main(int ac, char **av)
{
int fd;
struct ifreq ifr;
u_int val;
if (ac < 2)
errx(EX_USAGE, "usage: %s iface [hdrcmplt]", av[0]);
if ((fd = open(BPF_PATH, O_RDWR)) == -1)
err(EX_OSFILE, "failed to open %s\n", BPF_PATH);
memset(&ifr, 0, sizeof(ifr));
strlcpy(ifr.ifr_name, av[1], sizeof(ifr.ifr_name));
if (ioctl(fd, BIOCSETIF, &ifr) != 0)
err(EX_DATAERR, "failed to bind to interface %s", av[1]);
if (ac > 2) {
val = 1;
if (ioctl(fd, BIOCSHDRCMPLT, &val) != 0)
err(EX_OSERR, "failed to set BIOCSHDRCMPLT");
}
send_arp(fd);
return (0);
}

Event Timeline

melifaro changed the title of this paste from untitled to bpf BIOCSHDRCMPLT check.Nov 7 2015, 7:29 PM
melifaro updated the paste's language from autodetect to c.
melifaro edited the content of this paste. (Show Details)