forked from 51Degrees/common-cxx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip.h
145 lines (130 loc) · 4.79 KB
/
ip.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* *********************************************************************
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
* Copyright 2019 51 Degrees Mobile Experts Limited, 5 Charlotte Close,
* Caversham, Reading, Berkshire, United Kingdom RG4 7BY.
*
* This Original Work is licensed under the European Union Public Licence (EUPL)
* v.1.2 and is subject to its terms as set out below.
*
* If a copy of the EUPL was not distributed with this file, You can obtain
* one at https://opensource.org/licenses/EUPL-1.2.
*
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
* amended by the European Commission) shall be deemed incompatible for
* the purposes of the Work and the provisions of the compatibility
* clause in Article 5 of the EUPL shall not apply.
*
* If using the Work as, or as part of, a network application, by
* including the attribution notice(s) required under Article 5 of the EUPL
* in the end user terms of the application under an appropriate heading,
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */
#ifndef FIFTYONE_DEGREES_IP_H_INCLUDED
#define FIFTYONE_DEGREES_IP_H_INCLUDED
/**
* @ingroup FiftyOneDegreesCommon
* @defgroup fiftyoneDegreesIp IP
*
* Types and methods to parse IP address strings.
*
* ## Introduction
*
* IP v4 and v6 addresses can be parsed using the
* #fiftyoneDegreesIpParseAddress and #fiftyoneDegreesIpParseAddresses methods.
*
* @{
*/
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include "data.h"
#ifdef __cplusplus
#define EXTERNAL extern "C"
#else
#define EXTERNAL
#endif
/**
* The number of bytes in an Ipv4 Address
*/
#define FIFTYONE_DEGREES_IPV4_LENGTH 4
/**
* The number of bytes in an Ipv6 Address
*/
#define FIFTYONE_DEGREES_IPV6_LENGTH 16
/**
* Enum indicating the type of IP address.
*/
typedef enum e_fiftyone_degrees_evidence_ip_type {
FIFTYONE_DEGREES_EVIDENCE_IP_TYPE_IPV4 = 0, /**< An IPv4 address */
FIFTYONE_DEGREES_EVIDENCE_IP_TYPE_IPV6 = 1, /**< An IPv6 address */
FIFTYONE_DEGREES_EVIDENCE_IP_TYPE_INVALID = 2, /**< Invalid IP address */
} fiftyoneDegreesEvidenceIpType;
/** @cond FORWARD_DECLARATIONS */
typedef struct fiftyone_degrees_evidence_ip_address
fiftyoneDegreesEvidenceIpAddress;
/** @endcond */
/**
* IP address structure containing the bytes of a v4 or v6 IP address. This
* contains a pointer to a next IP address to enable a linked list to be
* created.
*/
typedef struct fiftyone_degrees_evidence_ip_address {
fiftyoneDegreesEvidenceIpType type; /**< The type of address (v4 or v6) */
byte *address; /**< The first byte of the address */
byte *current; /**< When building the address the next byte to update */
fiftyoneDegreesEvidenceIpAddress *next; /**< Next address in the list or
NULL */
byte bytesPresent; /**< Number of bytes in the original string which are
not abbreviated */
// const char *originalStart; // The first character for the IP address
// const char *originalEnd; // The last character for the IP addresses
} fiftyoneDegreesEvidenceIpAddress;
/**
* Free a linked list of IP addresses. This can also be a single IP address as
* this is equivalent to a linked list with a size of 1.
* @param free method to free the IP addresses
* @param addresses head of the linked list
*/
EXTERNAL void fiftyoneDegreesIpFreeAddresses(
void(*free)(void*),
fiftyoneDegreesEvidenceIpAddress *addresses);
/**
* Parse a single IP address string.
* @param malloc method to allocate the IP address
* @param start of the string containing the IP address to parse
* @param end of the string containing the IP address to parse
* @return pointer to the parsed IP address
*/
EXTERNAL fiftyoneDegreesEvidenceIpAddress* fiftyoneDegreesIpParseAddress(
void*(*malloc)(size_t),
const char *start,
const char *end);
/**
* Parse a list of IP addresses and return as a linked list.
* @param malloc method to allocate IP addresses
* @param start of the string containing the IP addresses to parse
* @return pointer to the head of the linked list
*/
EXTERNAL fiftyoneDegreesEvidenceIpAddress* fiftyoneDegreesIpParseAddresses(
void*(*malloc)(size_t),
const char *start);
/**
* Compare two IP addresses in its binary form
* @param ipAddress1 the first IP address
* @param ipAddress2 the second IP address
* @param type the type of IP address. This determine
* the number of bytes to compare. IPv4 require 4 bytes
* and IPv6 require 16 bytes
* @return a value indicate the result:
* 0 for equals
* > 0 for ipAddress1 comes after ipAddress2
* < 0 for ipAddress1 comes before ipAddress2
*/
EXTERNAL int fiftyoneDegreesCompareIpAddresses(
const unsigned char *ipAddress1,
const unsigned char *ipAddress2,
fiftyoneDegreesEvidenceIpType type);
/**
* @}
*/
#endif