xref: /minix/minix/lib/liblwip/dist/test/fuzz/fuzz.c (revision fb9c64b2)
1 /*
2  * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25  * OF SUCH DAMAGE.
26  *
27  * This file is part of the lwIP TCP/IP stack.
28  *
29  * Author: Erik Ekman <erik@kryo.se>
30  *
31  */
32 
33 #include "lwip/init.h"
34 #include "lwip/netif.h"
35 #include "netif/etharp.h"
36 #if LWIP_IPV6
37 #include "lwip/ethip6.h"
38 #include "lwip/nd6.h"
39 #endif
40 #include <string.h>
41 #include <stdio.h>
42 
43 /* no-op send function */
44 static err_t lwip_tx_func(struct netif *netif, struct pbuf *p)
45 {
46   LWIP_UNUSED_ARG(netif);
47   LWIP_UNUSED_ARG(p);
48   return ERR_OK;
49 }
50 
51 static err_t testif_init(struct netif *netif)
52 {
53   netif->name[0] = 'f';
54   netif->name[1] = 'z';
55   netif->output = etharp_output;
56   netif->linkoutput = lwip_tx_func;
57   netif->mtu = 1500;
58   netif->hwaddr_len = 6;
59   netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
60 
61   netif->hwaddr[0] = 0x00;
62   netif->hwaddr[1] = 0x23;
63   netif->hwaddr[2] = 0xC1;
64   netif->hwaddr[3] = 0xDE;
65   netif->hwaddr[4] = 0xD0;
66   netif->hwaddr[5] = 0x0D;
67 
68 #if LWIP_IPV6
69   netif->output_ip6 = ethip6_output;
70   netif->ip6_autoconfig_enabled = 1;
71   netif_create_ip6_linklocal_address(netif, 1);
72   netif->flags |= NETIF_FLAG_MLD6;
73 #endif
74 
75   return ERR_OK;
76 }
77 
78 static void input_pkt(struct netif *netif, const u8_t *data, size_t len)
79 {
80   struct pbuf *p, *q;
81   err_t err;
82 
83   LWIP_ASSERT("pkt too big", len <= 0xFFFF);
84   p = pbuf_alloc(PBUF_RAW, (u16_t)len, PBUF_POOL);
85   LWIP_ASSERT("alloc failed", p);
86   for(q = p; q != NULL; q = q->next) {
87     MEMCPY(q->payload, data, q->len);
88     data += q->len;
89   }
90   err = netif->input(p, netif);
91   if (err != ERR_OK) {
92     pbuf_free(p);
93   }
94 }
95 
96 int main(int argc, char** argv)
97 {
98   struct netif net_test;
99   ip4_addr_t addr;
100   ip4_addr_t netmask;
101   ip4_addr_t gw;
102   u8_t pktbuf[2000];
103   size_t len;
104 
105   lwip_init();
106 
107   IP4_ADDR(&addr, 172, 30, 115, 84);
108   IP4_ADDR(&netmask, 255, 255, 255, 0);
109   IP4_ADDR(&gw, 172, 30, 115, 1);
110 
111   netif_add(&net_test, &addr, &netmask, &gw, &net_test, testif_init, ethernet_input);
112   netif_set_up(&net_test);
113 
114 #if LWIP_IPV6
115   nd6_tmr(); /* tick nd to join multicast groups */
116 #endif
117 
118   if(argc > 1) {
119     FILE* f;
120     const char* filename;
121     printf("reading input from file... ");
122     fflush(stdout);
123     filename = argv[1];
124     LWIP_ASSERT("invalid filename", filename != NULL);
125     f = fopen(filename, "rb");
126     LWIP_ASSERT("open failed", f != NULL);
127     len = fread(pktbuf, 1, sizeof(pktbuf), f);
128     fclose(f);
129     printf("testing file: \"%s\"...\r\n", filename);
130   } else {
131     len = fread(pktbuf, 1, sizeof(pktbuf), stdin);
132   }
133   input_pkt(&net_test, pktbuf, len);
134 
135   return 0;
136 }
137