net.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* Minimal network stack: ARP + ICMP echo (ping) over a netdev NIC.
  2. Parsing, decoding, reply generation, and the card's IPv4 address. */
  3. #ifndef NET_H
  4. #define NET_H
  5. /* Ethernet ethertypes. */
  6. #define ET_IPV4 0x0800
  7. #define ET_ARP 0x0806
  8. #define ET_IPV6 0x86DD
  9. /* IPv4 protocol numbers. */
  10. #define IPPROTO_ICMP 1
  11. #define IPPROTO_UDP 17
  12. #define IPPROTO_TCP 6
  13. /* ICMP message types. */
  14. #define ICMP_ECHO_REPLY 0
  15. #define ICMP_ECHO_REQUEST 8
  16. void net_set_ip(const unsigned char ip[4]);
  17. void net_get_ip(unsigned char ip[4]);
  18. int net_parse_ip(const char *s, unsigned char ip[4]); /* 0 on success, -1 on error */
  19. void net_print_ip(void);
  20. /* Full configuration (mask/gateway stay zero when unknown). net_set_ip()
  21. * keeps the manual override semantics and clears mask/gateway; DHCP uses
  22. * net_configure(). */
  23. void net_configure(const unsigned char ip[4], const unsigned char mask[4],
  24. const unsigned char gw[4]);
  25. void net_get_mask(unsigned char mask[4]);
  26. void net_get_gateway(unsigned char gw[4]);
  27. int net_is_configured(void); /* ip != 0.0.0.0 */
  28. const char *net_ethertype_name(unsigned short et);
  29. /* Internet checksum (16-bit one's complement, network byte order). */
  30. unsigned short net_checksum(const void *data, unsigned int len);
  31. /* Largest UDP payload net_udp_send() accepts. */
  32. #define NET_UDP_MAX 1400
  33. /* Send one UDP datagram (IPv4, checksum disabled) in a single Ethernet frame
  34. * to dst_mac/dst_ip, with the configured source address. Returns 0 on
  35. * success, -1 on error. */
  36. int net_udp_send(const unsigned char *dst_mac, const unsigned char *dst_ip,
  37. unsigned int src_port, unsigned int dst_port,
  38. const void *payload, unsigned int len);
  39. /* Same, but with an explicit source address (DHCP uses 0.0.0.0). */
  40. int net_udp_send_from(const unsigned char *src_ip,
  41. const unsigned char *dst_mac, const unsigned char *dst_ip,
  42. unsigned int src_port, unsigned int dst_port,
  43. const void *payload, unsigned int len);
  44. /* Parse one Ethernet frame as a UDP datagram addressed to us or to the
  45. * broadcast address. Fills the addresses/ports and the payload location.
  46. * Returns 1 on match. */
  47. int net_udp_parse(const unsigned char *frame, unsigned int len,
  48. unsigned char src_ip[4], unsigned char dst_ip[4],
  49. unsigned int *src_port, unsigned int *dst_port,
  50. const unsigned char **payload, unsigned int *plen);
  51. /* Print one decoded frame summary line (dst/src/ethertype + ARP/IP/ICMP). */
  52. void net_decode_frame(const unsigned char *frame, unsigned int len);
  53. /* Build a reply to an ARP request or ICMP echo request addressed to us.
  54. Returns reply length (> 0) or 0 if the frame needs no reply. */
  55. int net_build_reply(const unsigned char *frame, unsigned int len,
  56. unsigned char *out, unsigned int maxout);
  57. #endif