A device detection library that is used natively or by 51Degrees products

IP

Detailed Description

Types and methods to parse IP address strings.

Introduction

IP v4 and v6 addresses can be parsed using the fiftyoneDegreesIpAddressParse method.

Collaboration diagram for IP:

Structs

struct  fiftyoneDegreesIpAddress
 The structure to hold a IP Address in byte array format. More...

Macros

#define FIFTYONE_DEGREES_IPV4_LENGTH   4
 The number of bytes in an Ipv4 Address.
#define FIFTYONE_DEGREES_IPV6_LENGTH   16
 The number of bytes in an Ipv6 Address.

Enumerations

enum  fiftyoneDegreesIpType { FIFTYONE_DEGREES_IP_TYPE_INVALID = 0 , FIFTYONE_DEGREES_IP_TYPE_IPV4 = 4 , FIFTYONE_DEGREES_IP_TYPE_IPV6 = 6 }
 Enum indicating the type of IP address. More...

Functions

bool fiftyoneDegreesIpAddressParse (const char *start, const char *end, fiftyoneDegreesIpAddress *address)
 Parse a single IP address string.
int fiftyoneDegreesIpAddressesCompare (const unsigned char *ipAddress1, const unsigned char *ipAddress2, fiftyoneDegreesIpType type)
 Compare two IP addresses in its binary form.

Enumeration Type Documentation

◆ fiftyoneDegreesIpType

Enum indicating the type of IP address.

Enumerator

FIFTYONE_DEGREES_IP_TYPE_INVALID 

Invalid IP address.

FIFTYONE_DEGREES_IP_TYPE_IPV4 

An IPv4 address.

FIFTYONE_DEGREES_IP_TYPE_IPV6 

An IPv6 address.

Function Documentation

◆ fiftyoneDegreesIpAddressesCompare()

int fiftyoneDegreesIpAddressesCompare ( const unsigned char * ipAddress1,
const unsigned char * ipAddress2,
fiftyoneDegreesIpType type )

Compare two IP addresses in its binary form.

Parameters
ipAddress1the first IP address
ipAddress2the second IP address
typethe type of IP address. This determine the number of bytes to compare. IPv4 require 4 bytes and IPv6 require 16 bytes
Returns
a value indicate the result: 0 for equals

0 for ipAddress1 comes after ipAddress2 < 0 for ipAddress1 comes before ipAddress2

◆ fiftyoneDegreesIpAddressParse()

bool fiftyoneDegreesIpAddressParse ( const char * start,
const char * end,
fiftyoneDegreesIpAddress * address )

Parse a single IP address string.

The string is read only when all of it is one of these forms:

  • an IPv4 address in dotted-decimal form, such as 203.0.113.1, with no leading zeros
  • an IPv6 address in any text form of RFC 4291 section 2.2, such as 2001:db8::1 or ::ffff:203.0.113.1
  • an IPv4 address, a colon and a port, such as 203.0.113.1:8080
  • an IPv6 address in square brackets, with or without a colon and a port after the closing bracket, such as [2001:db8::1]:8080

The port is checked and then dropped. Anything else is not read, including a prefix length, text or a space before or after the address, a list of addresses and an IPv6 zone index. The address is not changed in that case. An IPv4-mapped IPv6 address stays an IPv6 address here. See https://github.com/51Degrees/specifications/blob/main/ip-intelligence-specification/pipeline-elements/ip-intelligence-on-premise.md#reading-the-client-ip-value

Does not modify the last 12 (out of 16) bytes when parsing IPv4.

Parameters
startof the string containing the IP address to parse
endthe character after the last character of the string
addressmemory to write parsed IP address into.
Returns
true if address was parsed correctly, false otherwise