iPXE
Data Structures | Macros | Typedefs | Functions
in.h File Reference
#include <stdint.h>
#include <byteswap.h>
#include <ipxe/socket.h>

Go to the source code of this file.

Data Structures

struct  in_addr
 IP address structure. More...
 
struct  in6_addr
 IP6 address structure. More...
 
struct  sockaddr_in
 IPv4 socket address. More...
 
struct  sockaddr_in6
 IPv6 socket address. More...
 

Macros

#define IP_ICMP   1
 
#define IP_TCP   6
 
#define IP_UDP   17
 
#define IP_ICMP6   58
 
#define INADDR_NONE   htonl ( 0xffffffff )
 
#define INADDR_BROADCAST   htonl ( 0xffffffff )
 
#define INADDR_NET_CLASSA   htonl ( 0xff000000 )
 
#define INADDR_NET_CLASSB   htonl ( 0xffff0000 )
 
#define INADDR_NET_CLASSC   htonl ( 0xffffff00 )
 
#define IN_IS_CLASSA(addr)   ( ( (addr) & htonl ( 0x80000000 ) ) == htonl ( 0x00000000 ) )
 
#define IN_IS_CLASSB(addr)   ( ( (addr) & htonl ( 0xc0000000 ) ) == htonl ( 0x80000000 ) )
 
#define IN_IS_CLASSC(addr)   ( ( (addr) & htonl ( 0xe0000000 ) ) == htonl ( 0xc0000000 ) )
 
#define IN_IS_MULTICAST(addr)   ( ( (addr) & htonl ( 0xf0000000 ) ) == htonl ( 0xe0000000 ) )
 
#define s6_addr   in6_u.u6_addr8
 
#define s6_addr16   in6_u.u6_addr16
 
#define s6_addr32   in6_u.u6_addr32
 
#define IN6_IS_ADDR_UNSPECIFIED(addr)
 
#define IN6_IS_ADDR_MULTICAST(addr)   ( *( ( const uint8_t * ) (addr) ) == 0xff )
 
#define IN6_IS_ADDR_LINKLOCAL(addr)
 
#define IN6_IS_ADDR_SITELOCAL(addr)
 
#define IN6_IS_ADDR_ULA(addr)   ( ( *( ( const uint8_t * ) (addr) ) & 0xfe ) == 0xfc )
 

Typedefs

typedef struct in_addr in_addr
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
int inet_aton (const char *cp, struct in_addr *inp)
 Parse IPv4 address. More...
 
char * inet_ntoa (struct in_addr in)
 Convert IPv4 address to dotted-quad notation. More...
 
int inet6_aton (const char *string, struct in6_addr *in)
 Parse IPv6 address. More...
 
char * inet6_ntoa (const struct in6_addr *in)
 Convert IPv6 address to standard notation. More...
 

Macro Definition Documentation

◆ IP_ICMP

#define IP_ICMP   1

Definition at line 12 of file in.h.

◆ IP_TCP

#define IP_TCP   6

Definition at line 13 of file in.h.

◆ IP_UDP

#define IP_UDP   17

Definition at line 14 of file in.h.

◆ IP_ICMP6

#define IP_ICMP6   58

Definition at line 15 of file in.h.

◆ INADDR_NONE

#define INADDR_NONE   htonl ( 0xffffffff )

Definition at line 19 of file in.h.

◆ INADDR_BROADCAST

#define INADDR_BROADCAST   htonl ( 0xffffffff )

Definition at line 21 of file in.h.

◆ INADDR_NET_CLASSA

#define INADDR_NET_CLASSA   htonl ( 0xff000000 )

Definition at line 23 of file in.h.

◆ INADDR_NET_CLASSB

#define INADDR_NET_CLASSB   htonl ( 0xffff0000 )

Definition at line 24 of file in.h.

◆ INADDR_NET_CLASSC

#define INADDR_NET_CLASSC   htonl ( 0xffffff00 )

Definition at line 25 of file in.h.

◆ IN_IS_CLASSA

#define IN_IS_CLASSA (   addr)    ( ( (addr) & htonl ( 0x80000000 ) ) == htonl ( 0x00000000 ) )

Definition at line 27 of file in.h.

◆ IN_IS_CLASSB

#define IN_IS_CLASSB (   addr)    ( ( (addr) & htonl ( 0xc0000000 ) ) == htonl ( 0x80000000 ) )

Definition at line 29 of file in.h.

◆ IN_IS_CLASSC

#define IN_IS_CLASSC (   addr)    ( ( (addr) & htonl ( 0xe0000000 ) ) == htonl ( 0xc0000000 ) )

Definition at line 31 of file in.h.

◆ IN_IS_MULTICAST

#define IN_IS_MULTICAST (   addr)    ( ( (addr) & htonl ( 0xf0000000 ) ) == htonl ( 0xe0000000 ) )

Definition at line 33 of file in.h.

◆ s6_addr

#define s6_addr   in6_u.u6_addr8

Definition at line 54 of file in.h.

◆ s6_addr16

#define s6_addr16   in6_u.u6_addr16

Definition at line 55 of file in.h.

◆ s6_addr32

#define s6_addr32   in6_u.u6_addr32

Definition at line 56 of file in.h.

◆ IN6_IS_ADDR_UNSPECIFIED

#define IN6_IS_ADDR_UNSPECIFIED (   addr)
Value:
( ( ( ( ( const uint32_t * ) (addr) )[0] ) | \
( ( ( const uint32_t * ) (addr) )[1] ) | \
( ( ( const uint32_t * ) (addr) )[2] ) | \
( ( ( const uint32_t * ) (addr) )[3] ) ) == 0 )
u32 addr
Definition: sky2.h:8
unsigned int uint32_t
Definition: stdint.h:12

Definition at line 59 of file in.h.

◆ IN6_IS_ADDR_MULTICAST

#define IN6_IS_ADDR_MULTICAST (   addr)    ( *( ( const uint8_t * ) (addr) ) == 0xff )

Definition at line 65 of file in.h.

◆ IN6_IS_ADDR_LINKLOCAL

#define IN6_IS_ADDR_LINKLOCAL (   addr)
Value:
( ( *( ( const uint16_t * ) (addr) ) & htons ( 0xffc0 ) ) == \
htons ( 0xfe80 ) )
unsigned short uint16_t
Definition: stdint.h:11
u32 addr
Definition: sky2.h:8
#define htons(value)
Definition: byteswap.h:135

Definition at line 68 of file in.h.

◆ IN6_IS_ADDR_SITELOCAL

#define IN6_IS_ADDR_SITELOCAL (   addr)
Value:
( ( *( ( const uint16_t * ) (addr) ) & htons ( 0xffc0 ) ) == \
htons ( 0xfec0 ) )
unsigned short uint16_t
Definition: stdint.h:11
u32 addr
Definition: sky2.h:8
#define htons(value)
Definition: byteswap.h:135

Definition at line 72 of file in.h.

◆ IN6_IS_ADDR_ULA

#define IN6_IS_ADDR_ULA (   addr)    ( ( *( ( const uint8_t * ) (addr) ) & 0xfe ) == 0xfc )

Definition at line 76 of file in.h.

Typedef Documentation

◆ in_addr

typedef struct in_addr in_addr

Definition at line 43 of file in.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ inet_aton()

int inet_aton ( const char *  string,
struct in_addr in 
)

Parse IPv4 address.

Parameters
stringIPv4 address string
Return values
inIPv4 address to fill in
okIPv4 address is valid

Note that this function returns nonzero iff the address is valid, to match the standard BSD API function of the same name. Unlike most other iPXE functions, a zero therefore indicates failure.

Definition at line 631 of file ipv4.c.

631  {
632  const char *separator = "...";
633  uint8_t *byte = ( ( uint8_t * ) in );
634  char *endp;
635  unsigned long value;
636 
637  while ( 1 ) {
638  value = strtoul ( string, &endp, 0 );
639  if ( string == endp )
640  return 0;
641  if ( value > 0xff )
642  return 0;
643  *(byte++) = value;
644  if ( *endp != *separator )
645  return 0;
646  if ( ! *(separator++) )
647  return 1;
648  string = ( endp + 1 );
649  }
650 }
unsigned long strtoul(const char *string, char **endp, int base)
Convert string to numeric value.
Definition: string.c:456
__be32 in[4]
Definition: CIB_PRM.h:35
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
unsigned char uint8_t
Definition: stdint.h:10

References in, strtoul(), and value.

Referenced by inet_aton_fail_okx(), inet_aton_okx(), ipv4_sock_aton(), parse_ipv4_setting(), and tftp_process_multicast().

◆ inet_ntoa()

char* inet_ntoa ( struct in_addr  in)

Convert IPv4 address to dotted-quad notation.

Parameters
inIPv4 address
Return values
stringIPv4 address in dotted-quad notation

Definition at line 658 of file ipv4.c.

658  {
659  static char buf[16]; /* "xxx.xxx.xxx.xxx" */
660  uint8_t *bytes = ( uint8_t * ) &in;
661 
662  sprintf ( buf, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3] );
663  return buf;
664 }
__be32 in[4]
Definition: CIB_PRM.h:35
#define sprintf(buf, fmt,...)
Write a formatted string to a buffer.
Definition: stdio.h:36
unsigned char uint8_t
Definition: stdint.h:10
uint8_t bytes[64]
Definition: ib_mad.h:16

References bytes, in, and sprintf.

Referenced by add_ipv4_miniroute(), del_ipv4_miniroute(), dhcp_deliver(), dhcp_discovery_rx(), dhcp_proxy_rx(), dhcp_proxy_tx(), dhcp_pxebs_accept(), dhcp_pxebs_rx(), dhcp_pxebs_tx(), dhcp_request_rx(), dhcp_request_tx(), efi_snp_mcast_ip_to_mac(), fetch_next_server_and_filename(), format_ipv4_setting(), ibft_ipaddr(), inet_aton_okx(), inet_ntoa_okx(), ipoib_transmit(), ipv4_gratuitous_arp(), ipv4_ntoa(), ipv4_rx(), ipv4_sock_ntoa(), ipv4_tx(), pxenv_udp_open(), pxenv_udp_read(), pxenv_udp_write(), pxenv_undi_get_mcast_address(), route_ipv4_print(), start_pxebs(), tftp_apply_settings(), and tftp_process_multicast().

◆ inet6_aton()

int inet6_aton ( const char *  string,
struct in6_addr in 
)

Parse IPv6 address.

Parameters
stringIPv6 address string
Return values
inIPv6 address to fill in
rcReturn status code

Definition at line 824 of file ipv6.c.

824  {
825  uint16_t *word = in->s6_addr16;
826  uint16_t *end = ( word + ( sizeof ( in->s6_addr16 ) /
827  sizeof ( in->s6_addr16[0] ) ) );
828  uint16_t *pad = NULL;
829  const char *nptr = string;
830  char *endptr;
831  unsigned long value;
832  size_t pad_len;
833  size_t move_len;
834 
835  /* Parse string */
836  while ( 1 ) {
837 
838  /* Parse current word */
839  value = strtoul ( nptr, &endptr, 16 );
840  if ( value > 0xffff ) {
841  DBG ( "IPv6 invalid word value %#lx in \"%s\"\n",
842  value, string );
843  return -EINVAL;
844  }
845  *(word++) = htons ( value );
846 
847  /* Parse separator */
848  if ( ! *endptr )
849  break;
850  if ( *endptr != ':' ) {
851  DBG ( "IPv6 invalid separator '%c' in \"%s\"\n",
852  *endptr, string );
853  return -EINVAL;
854  }
855  if ( ( endptr == nptr ) && ( nptr != string ) ) {
856  if ( pad ) {
857  DBG ( "IPv6 invalid multiple \"::\" in "
858  "\"%s\"\n", string );
859  return -EINVAL;
860  }
861  pad = word;
862  }
863  nptr = ( endptr + 1 );
864 
865  /* Check for overrun */
866  if ( word == end ) {
867  DBG ( "IPv6 too many words in \"%s\"\n", string );
868  return -EINVAL;
869  }
870  }
871 
872  /* Insert padding if specified */
873  if ( pad ) {
874  move_len = ( ( ( void * ) word ) - ( ( void * ) pad ) );
875  pad_len = ( ( ( void * ) end ) - ( ( void * ) word ) );
876  memmove ( ( ( ( void * ) pad ) + pad_len ), pad, move_len );
877  memset ( pad, 0, pad_len );
878  } else if ( word != end ) {
879  DBG ( "IPv6 underlength address \"%s\"\n", string );
880  return -EINVAL;
881  }
882 
883  return 0;
884 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
unsigned short uint16_t
Definition: stdint.h:11
unsigned long strtoul(const char *string, char **endp, int base)
Convert string to numeric value.
Definition: string.c:456
__be32 in[4]
Definition: CIB_PRM.h:35
uint32_t string
Definition: multiboot.h:14
u32 pad[9]
Padding.
Definition: ar9003_mac.h:90
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
void * memmove(void *dest, const void *src, size_t len) __nonnull
unsigned short word
Definition: smc9000.h:39
uint32_t end
Ending offset.
Definition: netvsc.h:18
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
#define htons(value)
Definition: byteswap.h:135
void * memset(void *dest, int character, size_t len) __nonnull

References DBG, EINVAL, end, htons, in, memmove(), memset(), NULL, pad, string, strtoul(), and value.

Referenced by inet6_aton_fail_okx(), inet6_aton_okx(), ipv6_route_okx(), ipv6_sock_aton(), ipv6_table_okx(), and parse_ipv6_setting().

◆ inet6_ntoa()

char* inet6_ntoa ( const struct in6_addr in)

Convert IPv6 address to standard notation.

Parameters
inIPv6 address
Return values
stringIPv6 address string in canonical format

RFC5952 defines the canonical format for IPv6 textual representation.

Definition at line 894 of file ipv6.c.

894  {
895  static char buf[41]; /* ":xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx" */
896  char *out = buf;
897  char *longest_start = NULL;
898  char *start = NULL;
899  int longest_len = 1;
900  int len = 0;
901  char *dest;
902  unsigned int i;
903  uint16_t value;
904 
905  /* Format address, keeping track of longest run of zeros */
906  for ( i = 0 ; i < ( sizeof ( in->s6_addr16 ) /
907  sizeof ( in->s6_addr16[0] ) ) ; i++ ) {
908  value = ntohs ( in->s6_addr16[i] );
909  if ( value == 0 ) {
910  if ( len++ == 0 )
911  start = out;
912  if ( len > longest_len ) {
913  longest_start = start;
914  longest_len = len;
915  }
916  } else {
917  len = 0;
918  }
919  out += sprintf ( out, ":%x", value );
920  }
921 
922  /* Abbreviate longest run of zeros, if applicable */
923  if ( longest_start ) {
924  dest = strcpy ( ( longest_start + 1 ),
925  ( longest_start + ( 2 * longest_len ) ) );
926  if ( dest[0] == '\0' )
927  dest[1] = '\0';
928  dest[0] = ':';
929  }
930  return ( ( longest_start == buf ) ? buf : ( buf + 1 ) );
931 }
unsigned short uint16_t
Definition: stdint.h:11
__be32 in[4]
Definition: CIB_PRM.h:35
#define sprintf(buf, fmt,...)
Write a formatted string to a buffer.
Definition: stdio.h:36
#define ntohs(value)
Definition: byteswap.h:136
uint32_t start
Starting offset.
Definition: netvsc.h:12
__be32 out[4]
Definition: CIB_PRM.h:36
char * strcpy(char *dest, const char *src)
Copy string.
Definition: string.c:311
static void * dest
Definition: strings.h:176
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
uint32_t len
Length.
Definition: ena.h:14
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

References dest, in, len, ntohs, NULL, out, sprintf, start, strcpy(), and value.

Referenced by dhcpv6_rx(), format_ipv6_setting(), inet6_aton_okx(), inet6_ntoa_okx(), ipv6_dump_miniroute(), ipv6_ntoa(), ipv6_route_okx(), ipv6_rx(), ipv6_sock_ntoa(), ipv6_tx(), ndp_rx_neighbour_advertisement_ll_target(), ndp_rx_neighbour_solicitation_ll_source(), ndp_rx_router_advertisement_ll_source(), ndp_rx_router_advertisement_prefix(), and route_ipv6_print().