parse_ip.h File Reference

Generalized IP address parser APIs. More...


Defines

#define IP_DELIMITER_PREFIX   '/'
#define IP_DESIGNATION_ALL   "all"
#define IP_DESIGNATION_DEFAULT   "default"
#define IP_DESIGNATION_ANY_UNICAST   "any-unicast"
#define IP_DESIGNATION_ANY_MULTICAST   "any-multicast"
#define IPV4_MASK_ANY_UNICAST   0x00000001
#define IPV4_MASK_ANY_MULTICAST   0x00000002
#define IP_ADDR_BUFLEN   (INET_ADDRSTRLEN * 2)
 IP_ADDR_BUFLEN includes NULL terminator and accomodates "255.255.255.255/32" or "255.255.255.255/255.255.255.255"
#define IP_ADDR_BYTE_MAX   255
#define IP_ADDR_BYTE_NUM   (sizeof(struct in_addr))
#define IPV6_ADDR_BUFLEN   (INET6_ADDRSTRLEN * 2)
 IPv6 address buffer length.
#define PIF_LEN   (1<<0)
 Flags for parse_ipaddr(). Allow prefix length (see above).
#define PIF_LENREQ   (1<<1)
 Require prefix length.
#define PIF_FULL   (1<<2)
 Require full specification (implied w/IPv6).
#define PIF_MASK   (1<<3)
 /mask can substitute for /prefix-length
#define PIF_DEFAULT   (1<<4)
 "all" or "default" -> 0/0
#define PIF_AREAID   (1<<5)
 OSPF area id syntax; grim.
#define PIF_LENOPT   (1<<6)
 Do not assume prefix length (see above).
#define PIF_PREFIXONLY   (1<<7)
 Prefix only: require host bits to be zero.
#define PIF_MULTICAST_ONLY   (1<<8)
 Allow Multicast address only.
#define PIF_ANYCAST   (1<<9)
 'any-unicast'
#define PIF_MARTIAN   (1<<10)
 check for martian addresses
#define PIF_UNICASTONLY   (1<<11)
 Allow Unicast address only.
#define IN_ADDR_BYTE(_a, _b)   (((const unsigned char *)&((_a)->s_addr))[_b])
#define IN6_ADDR_HSHORT(_a, _s)   ntohs(_a->__u6_addr.__u6_addr16[_s])

Functions

parse_retcode_t parse_ipaddr (int *af, const char *str, unsigned long ipflags, void *address, size_t address_size, int *pfxseen, size_t *pfxlen, int *maskseen, void *mask, size_t mask_size, char *msg, size_t msgsize)
 Parses the various forms of IP addresses directed by ipflags (PIF_*).
size_t format_ipaddr (int af, const void *address, boolean canonicalize, int pfxseen, size_t pfxlen, int maskseen, const void *mask, char *out, size_t outsize)
 Produces textual form of address, followed by prefix or netmask.
size_t format_ipv6addr_partial (const void *address, size_t pfxlen, boolean showpfxlen, char *out, size_t outsize)
 Produces textual form of address with bits masked, followed by prefix length, if required.
char * format_in6_addr (const void *vaddr, int plen, char *buf, int buflen, int null_terminate)
 Formats the IPv6 address using the ::-notation for compressing away a sequence of low-order zeroes.
char * format_in6_prefix (const void *vaddr, int plen, char *buf, int buflen, int null_terminate)
 Formats the IPv6 prefix into the buffer, using the ::-notation for compressing away a sequence of low-order zeroes.
boolean valid_ipv4_hostaddr (const char *address)
 Verifies that the IPv4 address is a valid host address.
boolean parse_ipaddr_is_unicast (int af, void *address)
 Verifies that the IP address is one of the well known unicast addresses.


Detailed Description

Generalized IP address parser APIs.


Define Documentation

#define IPV6_ADDR_BUFLEN   (INET6_ADDRSTRLEN * 2)
 

IPv6 address buffer length.

IPV6_ADDR_BUFLEN includes NULL terminator and accomodates any of: "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF/128" or "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF/FFFF:FFFF:FFFF:FFFF:FFFF:FFF... or "0000:0000:0000:0000:0000:0000:255.255.255.255/128" or "0000:0000:0000:0000:0000:0000:255.255.255.255/0000:0000:0000:0000:00...

#define PIF_LEN   (1<<0)
 

Flags for parse_ipaddr(). Allow prefix length (see above).

PIF_LENOPT indicates that if the prefix length is not specified, then this address is not meant have a prefix associated with it: so DO NOT provide a value for prefix length. The PIF_LENOPT option has no effect on the operation of parse_ipaddr. PIF_LENOPT is used by the type conversion methods (convert.c).

The type conversion methods use PIF_LEN && !PIF_LENOPT to indicate that the prefix length should always part of the result string (whether the prefix length was specified or not). The type conversion methods use PIF_LEN && PIF_LENOPT to indicate that the prefix length will be part of the result string only if it was specified to begin with.


Function Documentation

char* format_in6_addr const void *  vaddr,
int  plen,
char *  buf,
int  buflen,
int  null_terminate
 

Formats the IPv6 address using the ::-notation for compressing away a sequence of low-order zeroes.

Note:
This function formats the address backwards, from the end of the supplied buffer.
Parameters:
[in] vaddr Address to format
[in] plen Prefix length
[out] buf Output buffer
[in] buflen Length of the output buffer
[in] null_terminate Flag to indicate if the output buffer should be null-terminated
Returns:
The start of the formatted address string in buf.

char* format_in6_prefix const void *  vaddr,
int  plen,
char *  buf,
int  buflen,
int  null_terminate
 

Formats the IPv6 prefix into the buffer, using the ::-notation for compressing away a sequence of low-order zeroes.

Note:
This function formats the address backwards, from the end of the supplied buffer.
Parameters:
[in] vaddr Address to format
[in] plen Prefix length
[out] buf Output buffer
[in] buflen Length of the output buffer
[in] null_terminate Flag to indicate if the output buffer should be null-terminated
Returns:
The start of the formatted address string in buf.

size_t format_ipaddr int  af,
const void *  address,
boolean  canonicalize,
int  pfxseen,
size_t  pfxlen,
int  maskseen,
const void *  mask,
char *  out,
size_t  outsize
 

Produces textual form of address, followed by prefix or netmask.

Parameters:
[in] af Address family
[in] address Address to be formatted (in network byte order)
[in] canonicalize Indicates padding with zeros (produce sortable key)
[in] pfxseen Attach prefix length /'pfxlen' to the end of the output buffer
[in] pfxlen Prefix length.
[in] maskseen Attach mask 'mask' to the end of the output buffer
[in] mask Pointer to the mask (in network byte order)
[out] out Output buffer
[in] outsize Size of the output buffer, in bytes.
Usage
  • To return address alone in ASCII format, both pfxseen and maskseen should be set to 0.
  • To return address with its prefix in ASCII format, pfxseen should be set to 1. When pfxseen is set to 1, the string in out will be in the form 'address/pfxlen'.
  • To return an address with its mask in ASCII format, maskseen should be set to 1. When maskseen is set to 1, the string in out will be in the form 'address%mask'.
Returns:
0 when not able to format string, error value will be stored in the global variable errno; otherwise the size of the string in out, not including the null-terminator character.

size_t format_ipv6addr_partial const void *  address,
size_t  pfxlen,
boolean  showpfxlen,
char *  out,
size_t  outsize
 

Produces textual form of address with bits masked, followed by prefix length, if required.

Parameters:
[in] address Partially- or fully-specified address
[in] pfxlen Prefix length, printed at end of result, if showpfx is TRUE. Also indicates the number of bits of data that addr points to (rounded up to the nearest byte.)
[in] showpfxlen Print prefix length (/pfxlen) at the end of result
[out] out Output buffer
[in] outsize Size of the output buffer, in bytes
Returns:
0 when not able to format string, error value will be stored in the global variable errno; otherwise the size of the string in out, not including the null-terminator character.

parse_retcode_t parse_ipaddr int *  af,
const char *  str,
unsigned long  ipflags,
void *  address,
size_t  address_size,
int *  pfxseen,
size_t *  pfxlen,
int *  maskseen,
void *  mask,
size_t  mask_size,
char *  msg,
size_t  msgsize
 

Parses the various forms of IP addresses directed by ipflags (PIF_*).

Parameters:
[in,out] af Address family, set if not specified
[in] str Address to be parsed
[in] ipflags Directs parsing of str (see PIF_ flags)
[out] address Result address (in network byte order)
[out] address_size Size of result address buffer, in bytes
[out] pfxseen Prefix length (/'pfxlen') was seen in string
[out] pfxlen Result prefix length. Default is the maximum value.
[out] maskseen Mask ('mask') was seen in string
[out] mask Result mask (in network byte order)
[in] mask_size Size of of the result mask, in bytes
[out] msg When an error occurs, the error message is written here
[in] msgsize Size of the message buffer
Returns:
The status of the parsing.

boolean parse_ipaddr_is_unicast int  af,
void *  address
 

Verifies that the IP address is one of the well known unicast addresses.

The address cannot be a multicast, broadcast address or invalid.

Note:
Loopback address is considered a valid unicast address.
Parameters:
[in] af Address family. Only IPv4 and IPv6 are supported.
[in] address IP address.
Returns:
TRUE if address is a valid unicast address; FALSE otherwise.

boolean valid_ipv4_hostaddr const char *  address  ) 
 

Verifies that the IPv4 address is a valid host address.

The address cannot be a multicast or broadcast address.

Note:
Loopback address is not a valid host address, but it is not checked for. Historically, we allowed it in earlier releases of JUNOS and enforcing the check may break the customer config during an upgrade.
Parameters:
[in] address IPv4 address as a string
Returns:
TRUE if address is a valid host address; FALSE otherwise.


2007-2009 Juniper Networks, Inc. All rights reserved. The information contained herein is confidential information of Juniper Networks, Inc., and may not be used, disclosed, distributed, modified, or copied without the prior written consent of Juniper Networks, Inc. in an express license. This information is subject to change by Juniper Networks, Inc. Juniper Networks, the Juniper Networks logo, and JUNOS are registered trademarks of Juniper Networks, Inc. in the United States and other countries. All other trademarks, service marks, registered trademarks, or registered service marks are the property of their respective owners.
Generated on Sun May 30 20:24:32 2010 for libjuniper by Doxygen 1.4.5