net_fuzz.cc 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2022 Nathaniel Brough
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. */
  25. #include "tusb_config.h"
  26. #if defined(CFG_TUD_ECM_RNDIS) || defined(CFG_TUD_NCM)
  27. #include "class/net/net_device.h"
  28. #include "fuzz_private.h"
  29. #include <cassert>
  30. #include <cstdint>
  31. #include <vector>
  32. #include "lwip/sys.h"
  33. extern "C" {
  34. bool tud_network_recv_cb(const uint8_t *src, uint16_t size) {
  35. assert(_fuzz_data_provider.has_value());
  36. (void)src;
  37. (void)size;
  38. return _fuzz_data_provider->ConsumeBool();
  39. }
  40. // client must provide this: copy from network stack packet pointer to dst
  41. uint16_t tud_network_xmit_cb(uint8_t *dst, void *ref, uint16_t arg) {
  42. (void)ref;
  43. (void)arg;
  44. assert(_fuzz_data_provider.has_value());
  45. uint16_t size = _fuzz_data_provider->ConsumeIntegral<uint16_t>();
  46. std::vector<uint8_t> temp = _fuzz_data_provider->ConsumeBytes<uint8_t>(size);
  47. memcpy(dst, temp.data(), temp.size());
  48. return size;
  49. }
  50. /* lwip has provision for using a mutex, when applicable */
  51. sys_prot_t sys_arch_protect(void) { return 0; }
  52. void sys_arch_unprotect(sys_prot_t pval) { (void)pval; }
  53. //------------- ECM/RNDIS -------------//
  54. // client must provide this: initialize any network state back to the beginning
  55. void tud_network_init_cb(void) {
  56. // NoOp.
  57. }
  58. // client must provide this: 48-bit MAC address
  59. // TODO removed later since it is not part of tinyusb stack
  60. const uint8_t tud_network_mac_address[6] = {0};
  61. //------------- NCM -------------//
  62. // callback to client providing optional indication of internal state of network
  63. // driver
  64. void tud_network_link_state_cb(bool state) {
  65. (void)state;
  66. // NoOp.
  67. }
  68. }
  69. #endif