sendpacket.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * ssize_t sendpacket (struct channel const * channel, void * memory, ssize_t extent);
  11. *
  12. * channel.h
  13. *
  14. * send one packet over a raw packet channel; return the number of
  15. * bytes sent or -1 on error; dump outgoing packets on stdout when
  16. * the VERBOSE flag is set;
  17. *
  18. * Contributor(s):
  19. * Charles Maier <cmaier@qca.qualcomm.com>
  20. * Nathaniel Houghton <nhoughto@qca.qualcomm.com>
  21. *
  22. *--------------------------------------------------------------------*/
  23. #ifndef SENDPACKET_SOURCE
  24. #define SENDPACKET_SOURCE
  25. #include <unistd.h>
  26. #include "../ether/channel.h"
  27. #include "../tools/memory.h"
  28. #include "../tools/flags.h"
  29. ssize_t sendpacket (struct channel const * channel, void * memory, ssize_t extent)
  30. {
  31. if (_anyset (channel->flags, CHANNEL_VERBOSE))
  32. {
  33. hexdump (memory, 0, extent, stdout);
  34. }
  35. #if defined (__linux__)
  36. extent = sendto (channel->fd, memory, extent, 0, (struct sockaddr *) (0), (socklen_t) (0));
  37. #elif defined (__APPLE__) || defined (__OpenBSD__)
  38. extent = write (channel->fd, memory, extent);
  39. #elif defined (WINPCAP) || defined (LIBPCAP)
  40. if (pcap_sendpacket (channel->socket, (byte *) (memory), extent))
  41. {
  42. extent = - 1;
  43. }
  44. #else
  45. #error "Unknown Environment"
  46. #endif
  47. return (extent);
  48. }
  49. #endif