fuzz.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include "lwip/init.h"
  33. #include "lwip/netif.h"
  34. #include "netif/etharp.h"
  35. #if LWIP_IPV6
  36. #include "lwip/ethip6.h"
  37. #include "lwip/nd6.h"
  38. #endif
  39. #include <string.h>
  40. #include <stdio.h>
  41. /* no-op send function */
  42. static err_t lwip_tx_func(struct netif *netif, struct pbuf *p)
  43. {
  44. LWIP_UNUSED_ARG(netif);
  45. LWIP_UNUSED_ARG(p);
  46. return ERR_OK;
  47. }
  48. static err_t testif_init(struct netif *netif)
  49. {
  50. netif->name[0] = 'f';
  51. netif->name[1] = 'z';
  52. netif->output = etharp_output;
  53. netif->linkoutput = lwip_tx_func;
  54. netif->mtu = 1500;
  55. netif->hwaddr_len = 6;
  56. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
  57. netif->hwaddr[0] = 0x00;
  58. netif->hwaddr[1] = 0x23;
  59. netif->hwaddr[2] = 0xC1;
  60. netif->hwaddr[3] = 0xDE;
  61. netif->hwaddr[4] = 0xD0;
  62. netif->hwaddr[5] = 0x0D;
  63. #if LWIP_IPV6
  64. netif->output_ip6 = ethip6_output;
  65. netif->ip6_autoconfig_enabled = 1;
  66. netif_create_ip6_linklocal_address(netif, 1);
  67. netif->flags |= NETIF_FLAG_MLD6;
  68. #endif
  69. return ERR_OK;
  70. }
  71. static void input_pkt(struct netif *netif, const u8_t *data, size_t len)
  72. {
  73. struct pbuf *p, *q;
  74. err_t err;
  75. LWIP_ASSERT("pkt too big", len <= 0xFFFF);
  76. p = pbuf_alloc(PBUF_RAW, (u16_t)len, PBUF_POOL);
  77. LWIP_ASSERT("alloc failed", p);
  78. for(q = p; q != NULL; q = q->next) {
  79. MEMCPY(q->payload, data, q->len);
  80. data += q->len;
  81. }
  82. err = netif->input(p, netif);
  83. if (err != ERR_OK) {
  84. pbuf_free(p);
  85. }
  86. }
  87. int main(int argc, char** argv)
  88. {
  89. struct netif net_test;
  90. ip4_addr_t addr;
  91. ip4_addr_t netmask;
  92. ip4_addr_t gw;
  93. u8_t pktbuf[2000];
  94. size_t len;
  95. lwip_init();
  96. IP4_ADDR(&addr, 172, 30, 115, 84);
  97. IP4_ADDR(&netmask, 255, 255, 255, 0);
  98. IP4_ADDR(&gw, 172, 30, 115, 1);
  99. netif_add(&net_test, &addr, &netmask, &gw, &net_test, testif_init, ethernet_input);
  100. netif_set_up(&net_test);
  101. #if LWIP_IPV6
  102. nd6_tmr(); /* tick nd to join multicast groups */
  103. #endif
  104. if(argc > 1) {
  105. FILE* f;
  106. const char* filename;
  107. printf("reading input from file... ");
  108. fflush(stdout);
  109. filename = argv[1];
  110. LWIP_ASSERT("invalid filename", filename != NULL);
  111. f = fopen(filename, "rb");
  112. LWIP_ASSERT("open failed", f != NULL);
  113. len = fread(pktbuf, 1, sizeof(pktbuf), f);
  114. fclose(f);
  115. printf("testing file: \"%s\"...\r\n", filename);
  116. } else {
  117. len = fread(pktbuf, 1, sizeof(pktbuf), stdin);
  118. }
  119. input_pkt(&net_test, pktbuf, len);
  120. return 0;
  121. }