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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2012 Microsoft Corporation. All Rights Reserved.
//
// Module Name:
// HelperFunctions_IPAddress.cpp
//
// Abstract:
// This module contains functions which assist in actions pertaining to IP Addresses.
//
// Naming Convention:
//
// <Scope><Module><Object><Action><Modifier>
//
// i.e.
//
// <Scope>
// {
// - Function is likely visible to other modules
// }
// <Module>
// {
// Hlpr - Function is from HelperFunctions_* Modules.
// }
// <Object>
// {
// IPAddressV4String - Function pertains to null terminated wide character string
// representaions of IPv4 Addresses
// IPAddressV6String - Function pertains to null terminated wide character string
// representaions of IPv6 Addresses
// }
// <Action>
// {
// To - Function converts from one type to another
// Is - Function compares values.
// }
// <Modifier>
// {
// ValidFormat - Function validates condition.
// Value - Function acts on a value.
// }
//
// Private Functions:
//
// Public Functions:
// HlprIPAddressV4StringIsValidFormat(),
// HlprIPAddressV4StringToValue(),
// HlprIPAddressV6StringIsValidFormat(),
// HlprIPAddressV6StringToValue(),
//
// Author:
// Dusty Harper (DHarper)
//
// Revision History:
//
// [ Month ][Day] [Year] - [Revision]-[ Comments ]
// May 01, 2010 - 1.0 - Creation
//
////////////////////////////////////////////////////////////////////////////////////////////////////
#include "HelperFunctions_Include.h" /// .
/**
@helper_function="HlprIPAddressV4StringIsValidFormat"
Purpose: Determine if a string may be an IPv4 address by verifying the string: <br>
is at least 7 characters <br>
has at least 3 decimals('.') <br>
is not more than 16 characters <br>
<br>
Notes: <br>
<br>
MSDN_Ref: <br>
*/
BOOLEAN HlprIPAddressV4StringIsValidFormat(_In_ PCWSTR pIPAddress)
{
ASSERT(pIPAddress);
UINT32 status = NO_ERROR;
BOOLEAN isIPv4Address = FALSE;
size_t addressSize = 0;
status = StringCchLength(pIPAddress,
STRSAFE_MAX_CCH,
&addressSize);
if(FAILED(status))
{
HlprLogError(L"HlprIPAddressV4StringIsValidFormat : StringCchLength() [status: %#x]",
status);
HLPR_BAIL;
}
if(addressSize > 6 && /// minimum address size: 0.0.0.0
addressSize < 16) /// maximum address size: 255.255.255.255
{
UINT32 numPeriods = 0;
for(UINT32 index = 0;
index < addressSize;
index++)
{
if(pIPAddress[index] == '.')
numPeriods++;
}
if(numPeriods == 3)
isIPv4Address = TRUE;
}
HLPR_BAIL_LABEL:
return isIPv4Address;
}
/**
@helper_function="HlprIPAddressV4StringToValue"
Purpose: Convert a string representing an IPv4 Address to it's 4 BYTE value. <br>
<br>
Notes: <br>
<br>
MSDN_Ref: HTTP://MSDN.Microsoft.com/En-US/Library/Windows/Desktop/AA814459.aspx <br>
*/
_Success_(return == NO_ERROR)
UINT32 HlprIPAddressV4StringToValue(_In_ PCWSTR pIPv4AddressString,
_Inout_ UINT32* pIPv4Address)
{
ASSERT(pIPv4AddressString);
ASSERT(pIPv4Address);
UINT32 status = NO_ERROR;
#pragma warning(push)
#pragma warning(disable: 24002) /// function is IPv4 specific, however there is a v6 counter-part
#pragma warning(disable: 24007) /// function is IPv4 specific, however there is a v6 counter-part
if(HlprIPAddressV4StringIsValidFormat(pIPv4AddressString))
{
UINT16 port = 0;
IN_ADDR v4Addr = {0};
status = RtlIpv4StringToAddressEx(pIPv4AddressString,
FALSE,
&v4Addr,
&port);
if(status != NO_ERROR)
{
HlprLogError(L"HlprIPAddressV4StringToValue : RtlIpv4StringToAddressEx() [status: %#x][pIPv4AddressString: %s]",
status,
pIPv4AddressString);
HLPR_BAIL;
}
CopyMemory(pIPv4Address,
&v4Addr,
IPV4_ADDRESS_LENGTH);
*pIPv4Address = ntohl(*pIPv4Address);
}
else
{
status = ERROR_INVALID_PARAMETER;
HlprLogError(L"HlprIPAddressV4StringToValue() [status: %#x][pIPv4AddressString: %#s]",
status,
pIPv4AddressString);
}
#pragma warning(pop)
HLPR_BAIL_LABEL:
return status;
}
/**
@helper_function="HlprIPAddressV6StringIsValidFormat"
Purpose: Determine if a string may be an IPv6 address by verifying the string: <br>
is at least 3 characters <br>
has at least 2 colons (':') <br>
is not more than 40 characters <br>
<br>
Notes: <br>
<br>
MSDN_Ref: <br>
*/
BOOLEAN HlprIPAddressV6StringIsValidFormat(_In_ PCWSTR pIPAddress)
{
ASSERT(pIPAddress);
UINT32 status = NO_ERROR;
BOOLEAN isIPv6Address = FALSE;
size_t addressSize = 0;
status = StringCchLength(pIPAddress,
STRSAFE_MAX_CCH,
&addressSize);
if(FAILED(status))
{
HlprLogError(L"HlprIPAddressV6StringIsValidFormat : StringCchLength() [status: %#x]",
status);
HLPR_BAIL;
}
if(addressSize > 2 && /// minimum address size: ::1
addressSize < 40) /// maximum address size: FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
{
UINT32 numColons = 0;
for(UINT32 index = 0;
index < addressSize;
index++)
{
if(pIPAddress[index] == ':')
numColons++;
}
if(numColons > 1 &&
numColons < 8)
isIPv6Address = TRUE;
}
HLPR_BAIL_LABEL:
return isIPv6Address;
}
/**
@helper_function="HlprIPAddressV4StringToValue"
Purpose: Convert a string representing an IPv6 Address to it's 16 BYTE value. <br>
<br>
Notes: <br>
<br>
MSDN_Ref: HTTP://MSDN.Microsoft.com/En-US/Library/Windows/Desktop/AA814463.aspx <br>
*/
_Success_(return == NO_ERROR)
UINT32 HlprIPAddressV6StringToValue(_In_ PCWSTR pIPv6AddressString,
_Inout_updates_all_(IPV6_ADDRESS_LENGTH) BYTE* pIPv6Address)
{
ASSERT(pIPv6AddressString);
ASSERT(pIPv6Address);
UINT32 status = NO_ERROR;
if(HlprIPAddressV6StringIsValidFormat(pIPv6AddressString))
{
UINT32 scopeId = 0;
UINT16 port = 0;
IN6_ADDR v6Addr = {0};
status = RtlIpv6StringToAddressEx(pIPv6AddressString,
&v6Addr,
(PULONG)&scopeId,
&port);
if(status != NO_ERROR)
{
HlprLogError(L"HlprIPAddressV6StringToValue : RtlIpv6StringToAddressEx() [status: %#x][pIPv6AddressString: %s]",
status,
pIPv6AddressString);
HLPR_BAIL;
}
CopyMemory(pIPv6Address,
&v6Addr,
IPV6_ADDRESS_LENGTH);
}
else
{
status = ERROR_INVALID_PARAMETER;
HlprLogError(L"HlprIPAddressV6StringToValue() [status: %#x][pIPv6AddressString: %#p][pIPv6Address: %#p]",
status,
pIPv6AddressString,
pIPv6Address);
}
HLPR_BAIL_LABEL:
return status;
}
|