source: rtems-libbsd/testsuite/arphole/test_main.c @ 9979989

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 9979989 was 9979989, checked in by Sebastian Huber <sebastian.huber@…>, on Jan 17, 2014 at 11:41:35 AM

arphole: New test

  • Property mode set to 100644
File size: 3.3 KB
Line 
1/**
2 * @file
3 *
4 * This test program processes all ARP requests and claims every IP address it
5 * notices.  Use with care in production networks.
6 */
7
8/*
9 * Copyright (c) 2014 embedded brains GmbH.  All rights reserved.
10 *
11 *  embedded brains GmbH
12 *  Dornierstr. 4
13 *  82178 Puchheim
14 *  Germany
15 *  <rtems@embedded-brains.de>
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 *    notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 *    notice, this list of conditions and the following disclaimer in the
24 *    documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39#include <sys/types.h>
40
41#include <net/if.h>
42#include <net/if_arp.h>
43#include <net/ethernet.h>
44#include <arpa/inet.h>
45#include <netinet/in.h>
46
47#include <assert.h>
48#include <ifaddrs.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52
53#include <rtems/bsd/util.h>
54
55#define TEST_NAME "LIBBSD ARP HOLE"
56
57typedef struct {
58        struct ether_header eh;
59        struct arphdr arh;
60        uint8_t sha[ETHER_ADDR_LEN];
61        uint32_t spa;
62        uint8_t tha[ETHER_ADDR_LEN];
63        uint32_t tpa;
64} __packed arp_request;
65
66static void
67arp_processor(void *arg, int fd, const uint8_t eaddr[ETHER_ADDR_LEN],
68    const struct arphdr *ar, uint32_t spa, uint32_t tpa,
69    const uint8_t *sha, const uint8_t *tha)
70{
71        if (ar->ar_op == htons(ARPOP_REQUEST) && spa == htonl(INADDR_ANY) &&
72            tpa != htonl(INADDR_ANY)) {
73                arp_request arr;
74
75                memcpy(&arr.eh.ether_shost[0], &eaddr[0], ETHER_HDR_LEN);
76                memcpy(&arr.eh.ether_dhost[0], sha, ETHER_HDR_LEN);
77                arr.eh.ether_type = htons(ETHERTYPE_ARP);
78
79                arr.arh.ar_hrd = htons(ARPHRD_ETHER);
80                arr.arh.ar_pro = htons(ETHERTYPE_IP);
81                arr.arh.ar_hln = ETHER_ADDR_LEN;
82                arr.arh.ar_pln = sizeof(spa);
83                arr.arh.ar_op = htons(ARPOP_REPLY);
84
85                memcpy(&arr.sha[0], &eaddr[0], ETHER_ADDR_LEN);
86                arr.spa = tpa;
87                memcpy(&arr.tha[0], sha, ETHER_ADDR_LEN);
88                arr.tpa = htonl(INADDR_ANY);
89
90                write(fd, &arr, sizeof(arr));
91        }
92}
93
94static void
95test_main(void)
96{
97        char ifnamebuf[IF_NAMESIZE];
98        char *ifname;
99        rtems_bsd_arp_processor_context *ctx;
100
101        ifname = if_indextoname(1, &ifnamebuf[0]);
102        assert(ifname != NULL);
103
104        ctx = rtems_bsd_arp_processor_create(ifname);
105        assert(ctx != NULL);
106
107        while (1) {
108                rtems_bsd_arp_processor_process(ctx, arp_processor, NULL);
109        }
110}
111
112#define DEFAULT_NETWORK_NO_STATIC_IFCONFIG
113
114#include <rtems/bsd/test/default-network-init.h>
Note: See TracBrowser for help on using the repository browser.