You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

in.h 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #ifndef _IPXE_IN_H
  2. #define _IPXE_IN_H
  3. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  4. #include <stdint.h>
  5. #include <byteswap.h>
  6. #include <ipxe/socket.h>
  7. /* Protocol numbers */
  8. #define IP_ICMP 1
  9. #define IP_TCP 6
  10. #define IP_UDP 17
  11. #define IP_ICMP6 58
  12. /* IP address constants */
  13. #define INADDR_NONE htonl ( 0xffffffff )
  14. #define INADDR_BROADCAST htonl ( 0xffffffff )
  15. #define INADDR_NET_CLASSA htonl ( 0xff000000 )
  16. #define INADDR_NET_CLASSB htonl ( 0xffff0000 )
  17. #define INADDR_NET_CLASSC htonl ( 0xffffff00 )
  18. #define IN_IS_CLASSA( addr ) \
  19. ( ( (addr) & htonl ( 0x80000000 ) ) == htonl ( 0x00000000 ) )
  20. #define IN_IS_CLASSB( addr ) \
  21. ( ( (addr) & htonl ( 0xc0000000 ) ) == htonl ( 0x80000000 ) )
  22. #define IN_IS_CLASSC( addr ) \
  23. ( ( (addr) & htonl ( 0xe0000000 ) ) == htonl ( 0xc0000000 ) )
  24. #define IN_IS_MULTICAST( addr ) \
  25. ( ( (addr) & htonl ( 0xf0000000 ) ) == htonl ( 0xe0000000 ) )
  26. /**
  27. * IP address structure
  28. */
  29. struct in_addr {
  30. uint32_t s_addr;
  31. };
  32. typedef struct in_addr in_addr;
  33. /**
  34. * IP6 address structure
  35. */
  36. struct in6_addr {
  37. union {
  38. uint8_t u6_addr8[16];
  39. uint16_t u6_addr16[8];
  40. uint32_t u6_addr32[4];
  41. } in6_u;
  42. #define s6_addr in6_u.u6_addr8
  43. #define s6_addr16 in6_u.u6_addr16
  44. #define s6_addr32 in6_u.u6_addr32
  45. };
  46. #define IN6_IS_ADDR_UNSPECIFIED( addr ) \
  47. ( ( ( ( ( const uint32_t * ) (addr) )[0] ) | \
  48. ( ( ( const uint32_t * ) (addr) )[1] ) | \
  49. ( ( ( const uint32_t * ) (addr) )[2] ) | \
  50. ( ( ( const uint32_t * ) (addr) )[3] ) ) == 0 )
  51. #define IN6_IS_ADDR_MULTICAST( addr ) \
  52. ( *( ( const uint8_t * ) (addr) ) == 0xff )
  53. #define IN6_IS_ADDR_LINKLOCAL( addr ) \
  54. ( ( *( ( const uint16_t * ) (addr) ) & htons ( 0xffc0 ) ) == \
  55. htons ( 0xfe80 ) )
  56. #define IN6_IS_ADDR_SITELOCAL( addr ) \
  57. ( ( *( ( const uint16_t * ) (addr) ) & htons ( 0xffc0 ) ) == \
  58. htons ( 0xfec0 ) )
  59. #define IN6_IS_ADDR_ULA( addr ) \
  60. ( ( *( ( const uint8_t * ) (addr) ) & 0xfe ) == 0xfc )
  61. /**
  62. * IPv4 socket address
  63. */
  64. struct sockaddr_in {
  65. /** Socket address family (part of struct @c sockaddr)
  66. *
  67. * Always set to @c AF_INET for IPv4 addresses
  68. */
  69. sa_family_t sin_family;
  70. /** Flags (part of struct @c sockaddr_tcpip) */
  71. uint16_t sin_flags;
  72. /** TCP/IP port (part of struct @c sockaddr_tcpip) */
  73. uint16_t sin_port;
  74. /** Scope ID (part of struct @c sockaddr_tcpip)
  75. *
  76. * For multicast addresses, this is the network device index.
  77. */
  78. uint16_t sin_scope_id;
  79. /** IPv4 address */
  80. struct in_addr sin_addr;
  81. /** Padding
  82. *
  83. * This ensures that a struct @c sockaddr_in is large enough
  84. * to hold a socket address for any TCP/IP address family.
  85. */
  86. char pad[ sizeof ( struct sockaddr ) -
  87. ( sizeof ( sa_family_t ) /* sin_family */ +
  88. sizeof ( uint16_t ) /* sin_flags */ +
  89. sizeof ( uint16_t ) /* sin_port */ +
  90. sizeof ( uint16_t ) /* sin_scope_id */ +
  91. sizeof ( struct in_addr ) /* sin_addr */ ) ];
  92. } __attribute__ (( packed, may_alias ));
  93. /**
  94. * IPv6 socket address
  95. */
  96. struct sockaddr_in6 {
  97. /** Socket address family (part of struct @c sockaddr)
  98. *
  99. * Always set to @c AF_INET6 for IPv6 addresses
  100. */
  101. sa_family_t sin6_family;
  102. /** Flags (part of struct @c sockaddr_tcpip) */
  103. uint16_t sin6_flags;
  104. /** TCP/IP port (part of struct @c sockaddr_tcpip) */
  105. uint16_t sin6_port;
  106. /** Scope ID (part of struct @c sockaddr_tcpip)
  107. *
  108. * For link-local or multicast addresses, this is the network
  109. * device index.
  110. */
  111. uint16_t sin6_scope_id;
  112. /** IPv6 address */
  113. struct in6_addr sin6_addr;
  114. /** Padding
  115. *
  116. * This ensures that a struct @c sockaddr_in6 is large
  117. * enough to hold a socket address for any TCP/IP address
  118. * family.
  119. */
  120. char pad[ sizeof ( struct sockaddr ) -
  121. ( sizeof ( sa_family_t ) /* sin6_family */ +
  122. sizeof ( uint16_t ) /* sin6_flags */ +
  123. sizeof ( uint16_t ) /* sin6_port */ +
  124. sizeof ( uint16_t ) /* sin6_scope_id */ +
  125. sizeof ( struct in6_addr ) /* sin6_addr */ ) ];
  126. } __attribute__ (( packed, may_alias ));
  127. extern int inet_aton ( const char *cp, struct in_addr *inp );
  128. extern char * inet_ntoa ( struct in_addr in );
  129. extern int inet6_aton ( const char *string, struct in6_addr *in );
  130. extern char * inet6_ntoa ( const struct in6_addr *in );
  131. #endif /* _IPXE_IN_H */