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 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_NONGLOBAL( addr ) \
  57. ( IN6_IS_ADDR_LINKLOCAL (addr) || IN6_IS_ADDR_MULTICAST (addr) )
  58. /**
  59. * IPv4 socket address
  60. */
  61. struct sockaddr_in {
  62. /** Socket address family (part of struct @c sockaddr)
  63. *
  64. * Always set to @c AF_INET for IPv4 addresses
  65. */
  66. sa_family_t sin_family;
  67. /** Flags (part of struct @c sockaddr_tcpip) */
  68. uint16_t sin_flags;
  69. /** TCP/IP port (part of struct @c sockaddr_tcpip) */
  70. uint16_t sin_port;
  71. /** IPv4 address */
  72. struct in_addr sin_addr;
  73. /** Padding
  74. *
  75. * This ensures that a struct @c sockaddr_in is large enough
  76. * to hold a socket address for any TCP/IP address family.
  77. */
  78. char pad[ sizeof ( struct sockaddr ) -
  79. ( sizeof ( sa_family_t ) /* sin_family */ +
  80. sizeof ( uint16_t ) /* sin_flags */ +
  81. sizeof ( uint16_t ) /* sin_port */ +
  82. sizeof ( struct in_addr ) /* sin_addr */ ) ];
  83. } __attribute__ (( packed, may_alias ));
  84. /**
  85. * IPv6 socket address
  86. */
  87. struct sockaddr_in6 {
  88. /** Socket address family (part of struct @c sockaddr)
  89. *
  90. * Always set to @c AF_INET6 for IPv6 addresses
  91. */
  92. sa_family_t sin6_family;
  93. /** Flags (part of struct @c sockaddr_tcpip) */
  94. uint16_t sin6_flags;
  95. /** TCP/IP port (part of struct @c sockaddr_tcpip) */
  96. uint16_t sin6_port;
  97. /** Scope ID
  98. *
  99. * For link-local addresses, this is the network device index.
  100. */
  101. uint16_t sin6_scope_id;
  102. /** IPv6 address */
  103. struct in6_addr sin6_addr;
  104. /** Padding
  105. *
  106. * This ensures that a struct @c sockaddr_in6 is large
  107. * enough to hold a socket address for any TCP/IP address
  108. * family.
  109. */
  110. char pad[ sizeof ( struct sockaddr ) -
  111. ( sizeof ( sa_family_t ) /* sin6_family */ +
  112. sizeof ( uint16_t ) /* sin6_flags */ +
  113. sizeof ( uint16_t ) /* sin6_port */ +
  114. sizeof ( uint16_t ) /* sin6_scope_id */ +
  115. sizeof ( struct in6_addr ) /* sin6_addr */ ) ];
  116. } __attribute__ (( packed, may_alias ));
  117. extern int inet_aton ( const char *cp, struct in_addr *inp );
  118. extern char * inet_ntoa ( struct in_addr in );
  119. extern int inet6_aton ( const char *string, struct in6_addr *in );
  120. extern char * inet6_ntoa ( const struct in6_addr *in );
  121. #endif /* _IPXE_IN_H */