1
0

ARPHeader.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #ifndef ARPHeader_H
  16. #define ARPHeader_H
  17. #include "util/Assert.h"
  18. #include "util/Endian.h"
  19. #include "wire/Ethernet.h"
  20. #include <stdint.h>
  21. #include <stddef.h>
  22. #include <stdbool.h>
  23. struct ARPHeader_Pre {
  24. uint16_t htype;
  25. uint16_t ptype;
  26. uint8_t hlen;
  27. uint8_t plen;
  28. uint16_t operation;
  29. };
  30. #define ARPHeader_Pre_SIZE 8
  31. Assert_compileTime(sizeof(struct ARPHeader_Pre) == ARPHeader_Pre_SIZE);
  32. struct ARPHeader_6_4 {
  33. struct ARPHeader_Pre prefix;
  34. uint8_t sha[6]; // Sender hardware address
  35. uint8_t spa[4]; // Sender protocol address
  36. uint8_t tha[6]; // Target hardware address
  37. uint8_t tpa[4]; // Target protocol address
  38. };
  39. #define ARPHeader_6_4_SIZE 28
  40. Assert_compileTime(sizeof(struct ARPHeader_6_4) == ARPHeader_6_4_SIZE);
  41. #define ARPHeader_OP_Q Endian_hostToBigEndian16( 1 )
  42. #define ARPHeader_OP_A Endian_hostToBigEndian16( 2 )
  43. Assert_compileTime(Ethernet_ADDRLEN == 6);
  44. static inline bool ARPHeader_isEthIP4(struct ARPHeader_Pre* prefix)
  45. {
  46. return prefix->htype == Endian_hostToBigEndian16( 1 ) // Hardware is MAC,
  47. && prefix->ptype == Ethernet_TYPE_IP4 // protocol is IPv4,
  48. && prefix->hlen == Ethernet_ADDRLEN // and check lengths.
  49. && prefix->plen == 4;
  50. }
  51. #endif