VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/net/netaddrstr2.cpp@ 50456

Last change on this file since 50456 was 50456, checked in by vboxsync, 11 years ago

RTNetStrToIPv6Addr: first cut at IPv6 address parsing. I'm still not
sure how to deal with zone ids properly given that RFC 4007 leaves
their syntax as "implementation dependent". Suggestions are welcome.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.4 KB
Line 
1/* $Id: netaddrstr2.cpp 50456 2014-02-14 02:10:23Z vboxsync $ */
2/** @file
3 * IPRT - Network Address String Handling.
4 */
5
6/*
7 * Copyright (C) 2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27/*******************************************************************************
28* Header Files *
29*******************************************************************************/
30#include "internal/iprt.h"
31#include <iprt/net.h>
32
33#include <iprt/asm.h>
34#include <iprt/mem.h>
35#include <iprt/string.h>
36#include <iprt/stream.h>
37#include "internal/string.h"
38
39
40DECLHIDDEN(int) rtNetStrToIPv4AddrEx(const char *pcszAddr, PRTNETADDRIPV4 pAddr,
41 char **ppszNext)
42{
43 char *pszNext;
44 int rc;
45
46 AssertPtrReturn(pcszAddr, VERR_INVALID_PARAMETER);
47 AssertPtrReturn(pAddr, VERR_INVALID_PARAMETER);
48
49 rc = RTStrToUInt8Ex(pcszAddr, &pszNext, 10, &pAddr->au8[0]);
50 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
51 return VERR_INVALID_PARAMETER;
52 if (*pszNext++ != '.')
53 return VERR_INVALID_PARAMETER;
54
55 rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &pAddr->au8[1]);
56 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
57 return VERR_INVALID_PARAMETER;
58 if (*pszNext++ != '.')
59 return VERR_INVALID_PARAMETER;
60
61 rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &pAddr->au8[2]);
62 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
63 return VERR_INVALID_PARAMETER;
64 if (*pszNext++ != '.')
65 return VERR_INVALID_PARAMETER;
66
67 rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &pAddr->au8[3]);
68 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_SPACES && rc != VWRN_TRAILING_CHARS)
69 return VERR_INVALID_PARAMETER;
70
71 if (ppszNext != NULL)
72 *ppszNext = pszNext;
73 return VINF_SUCCESS;
74}
75
76
77RTDECL(int) RTNetStrToIPv4AddrEx(const char *pcszAddr, PRTNETADDRIPV4 pAddr,
78 char **ppszNext)
79{
80 return rtNetStrToIPv4AddrEx(pcszAddr, pAddr, ppszNext);
81}
82RT_EXPORT_SYMBOL(RTNetStrToIPv4AddrEx);
83
84
85RTDECL(int) RTNetStrToIPv4Addr(const char *pcszAddr, PRTNETADDRIPV4 pAddr)
86{
87 char *pszNext;
88 int rc;
89
90 AssertPtrReturn(pcszAddr, VERR_INVALID_PARAMETER);
91 AssertPtrReturn(pAddr, VERR_INVALID_PARAMETER);
92
93 pcszAddr = RTStrStripL(pcszAddr);
94 rc = rtNetStrToIPv4AddrEx(pcszAddr, pAddr, &pszNext);
95 if (rc != VINF_SUCCESS)
96 return VERR_INVALID_PARAMETER;
97
98 pszNext = RTStrStripL(pszNext);
99 if (*pszNext != '\0')
100 return VERR_INVALID_PARAMETER;
101
102 return VINF_SUCCESS;
103}
104RT_EXPORT_SYMBOL(RTNetStrToIPv4Addr);
105
106
107RTDECL(bool) RTNetIsIPv4AddrStr(const char *pcszAddr)
108{
109 RTNETADDRIPV4 addrIPv4;
110 char *pszNext;
111 int rc;
112
113 if (pcszAddr == NULL)
114 return false;
115
116 rc = rtNetStrToIPv4AddrEx(pcszAddr, &addrIPv4, &pszNext);
117 if (rc != VINF_SUCCESS)
118 return false;
119
120 if (*pszNext != '\0')
121 return false;
122
123 return true;
124}
125RT_EXPORT_SYMBOL(RTNetIsIPv4AddrStr);
126
127
128static int rtNetStrToHexGroup(const char *pcszValue, char **ppszNext,
129 uint16_t *pu16)
130{
131 char *pszNext;
132 int rc;
133
134 rc = RTStrToUInt16Ex(pcszValue, &pszNext, 16, pu16);
135 if (RT_FAILURE(rc))
136 return rc;
137
138 if ( rc != VINF_SUCCESS
139 && rc != VWRN_TRAILING_CHARS
140 && rc != VWRN_TRAILING_SPACES)
141 {
142 return -rc; /* convert warning to error */
143 }
144
145 /* parser always accepts 0x prefix */
146 if (pcszValue[0] == '0' && (pcszValue[1] == 'x' || pcszValue[1] == 'X'))
147 {
148 if (pu16)
149 *pu16 = 0;
150 if (ppszNext)
151 *ppszNext = (/* UNCONST */ char *)pcszValue + 1; /* to 'x' */
152 return VWRN_TRAILING_CHARS;
153 }
154
155 /* parser accepts leading zeroes "000000f" */
156 if (pszNext - pcszValue > 4)
157 return VERR_PARSE_ERROR;
158
159 if (ppszNext)
160 *ppszNext = pszNext;
161 return rc;
162}
163
164
165/*
166 * This function deals only with the hex-group IPv6 address syntax
167 * proper (with possible embedded IPv4).
168 */
169DECLHIDDEN(int) rtNetStrToIPv6AddrBase(const char *pcszAddr, PRTNETADDRIPV6 pAddrResult,
170 char **ppszNext)
171{
172 RTNETADDRIPV6 ipv6;
173 RTNETADDRIPV4 ipv4;
174 const char *pcszPos;
175 char *pszNext;
176 int iGroup;
177 uint16_t u16;
178 int rc;
179
180 memset(&ipv6, 0, sizeof(ipv6));
181
182 pcszPos = pcszAddr;
183
184 if (pcszPos[0] == ':') /* compressed zero run at the beginning? */
185 {
186 if (pcszPos[1] != ':')
187 return VERR_PARSE_ERROR;
188
189 pcszPos += 2; /* skip over "::" */
190 pszNext = (/* UNCONST */ char *)pcszPos;
191 iGroup = 1;
192 }
193 else
194 {
195 /*
196 * Scan forward until we either get complete address or find
197 * "::" compressed zero run.
198 */
199 for (iGroup = 0; iGroup < 8; ++iGroup)
200 {
201 /* check for embedded IPv4 at the end */
202 if (iGroup == 6)
203 {
204 rc = rtNetStrToIPv4AddrEx(pcszPos, &ipv4, &pszNext);
205 if (rc == VINF_SUCCESS)
206 {
207 ipv6.au32[3] = ipv4.au32[0];
208 iGroup = 8; /* filled 6 and 7 */
209 break; /* we are done */
210 }
211 }
212
213 rc = rtNetStrToHexGroup(pcszPos, &pszNext, &u16);
214 if (RT_FAILURE(rc))
215 return VERR_PARSE_ERROR;
216
217 ipv6.au16[iGroup] = RT_H2N_U16(u16);
218
219 if (iGroup == 7)
220 pcszPos = pszNext;
221 else
222 {
223 /* skip the colon that delimits this group */
224 if (*pszNext != ':')
225 return VERR_PARSE_ERROR;
226 pcszPos = pszNext + 1;
227
228 /* compressed zero run? */
229 if (*pcszPos == ':')
230 {
231 ++pcszPos; /* skip over :: */
232 pszNext += 2; /* skip over :: (in case we are done) */
233 iGroup += 2; /* current field and the zero in the next */
234 break;
235 }
236 }
237 }
238 }
239
240 if (iGroup != 8)
241 {
242 /*
243 * iGroup is the first group that can be filled by the part of
244 * the address after "::".
245 */
246 RTNETADDRIPV6 ipv6Tail;
247 const int iMaybeStart = iGroup;
248 int j;
249
250 memset(&ipv6Tail, 0, sizeof(ipv6Tail));
251
252 /*
253 * We try to accept longest match; we'll shift if necessary.
254 * Unlike the first loop, a failure to parse a group doesn't
255 * mean invalid address.
256 */
257 for (; iGroup < 8; ++iGroup)
258 {
259 /* check for embedded IPv4 at the end */
260 if (iGroup <= 6)
261 {
262 rc = rtNetStrToIPv4AddrEx(pcszPos, &ipv4, &pszNext);
263 if (rc == VINF_SUCCESS)
264 {
265 ipv6Tail.au16[iGroup] = ipv4.au16[0];
266 ipv6Tail.au16[iGroup + 1] = ipv4.au16[1];
267 iGroup = iGroup + 2; /* these two are done */
268 break; /* the rest is trailer */
269 }
270 }
271
272 rc = rtNetStrToHexGroup(pcszPos, &pszNext, &u16);
273 if (RT_FAILURE(rc))
274 break;
275
276 ipv6Tail.au16[iGroup] = RT_H2N_U16(u16);
277
278 if (iGroup == 7)
279 pcszPos = pszNext;
280 else
281 {
282 if (*pszNext != ':')
283 {
284 ++iGroup; /* this one is done */
285 break; /* the rest is trailer */
286 }
287
288 pcszPos = pszNext + 1;
289 }
290 }
291
292 for (j = 7, --iGroup; iGroup >= iMaybeStart; --j, --iGroup)
293 ipv6.au16[j] = ipv6Tail.au16[iGroup];
294 }
295
296 if (pAddrResult != NULL)
297 memcpy(pAddrResult, &ipv6, sizeof(ipv6));
298 if (ppszNext != NULL)
299 *ppszNext = pszNext;
300 return VINF_SUCCESS;
301}
302
303
304DECLHIDDEN(int) rtNetStrToIPv6AddrEx(const char *pcszAddr, PRTNETADDRIPV6 pAddr,
305 char **ppszZone, char **ppszNext)
306{
307 char *pszNext, *pszZone;
308 int rc;
309
310 rc = rtNetStrToIPv6AddrBase(pcszAddr, pAddr, &pszNext);
311 if (RT_FAILURE(rc))
312 return rc;
313
314 if (*pszNext != '%') /* is there a zone id? */
315 {
316 pszZone = NULL;
317 }
318 else
319 {
320 pszZone = pszNext + 1; /* skip '%' zone id delimiter */
321 if (*pszZone == '\0')
322 return VERR_PARSE_ERROR; /* empty zone id */
323
324 /*
325 * XXX: this is speculative as zone id syntax is
326 * implementation dependent, so we kinda guess here (accepting
327 * unreserved characters from URI syntax).
328 */
329 for (pszNext = pszZone; *pszNext != '\0'; ++pszNext)
330 {
331 const char c = *pszNext;
332 if ( !('0' <= c && c <= '9')
333 && !('a' <= c && c <= 'z')
334 && !('A' <= c && c <= 'Z')
335 && c != '_'
336 && c != '.'
337 && c != '-'
338 && c != '~')
339 {
340 break;
341 }
342 }
343 }
344
345 if (ppszZone != NULL)
346 *ppszZone = pszZone;
347 if (ppszNext != NULL)
348 *ppszNext = pszNext;
349
350 if (*pszNext == '\0') /* all input string consumed */
351 return VINF_SUCCESS;
352 else
353 {
354 while (*pszNext == ' ' || *pszNext == '\t')
355 ++pszNext;
356 if (*pszNext == '\0')
357 return VWRN_TRAILING_SPACES;
358 else
359 return VWRN_TRAILING_CHARS;
360 }
361}
362
363
364RTDECL(int) RTNetStrToIPv6AddrEx(const char *pcszAddr, PRTNETADDRIPV6 pAddr,
365 char **ppszNext)
366{
367 AssertPtrReturn(pcszAddr, VERR_INVALID_PARAMETER);
368 AssertPtrReturn(pAddr, VERR_INVALID_PARAMETER);
369
370 return rtNetStrToIPv6AddrBase(pcszAddr, pAddr, ppszNext);
371}
372RT_EXPORT_SYMBOL(RTNetStrToIPv6AddrEx);
373
374
375RTDECL(int) RTNetStrToIPv6Addr(const char *pcszAddr, PRTNETADDRIPV6 pAddr,
376 char **ppszZone)
377{
378 int rc;
379
380 AssertPtrReturn(pcszAddr, VERR_INVALID_PARAMETER);
381 AssertPtrReturn(pAddr, VERR_INVALID_PARAMETER);
382 AssertPtrReturn(ppszZone, VERR_INVALID_PARAMETER);
383
384 pcszAddr = RTStrStripL(pcszAddr);
385 rc = rtNetStrToIPv6AddrEx(pcszAddr, pAddr, ppszZone, NULL);
386 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_SPACES)
387 return VERR_INVALID_PARAMETER;
388
389 return VINF_SUCCESS;
390}
391RT_EXPORT_SYMBOL(RTNetStrToIPv6Addr);
392
393
394RTDECL(bool) RTNetIsIPv6AddrStr(const char *pcszAddr)
395{
396 RTNETADDRIPV6 addrIPv6;
397 int rc;
398
399 if (pcszAddr == NULL)
400 return false;
401
402 rc = rtNetStrToIPv6AddrEx(pcszAddr, &addrIPv6, NULL, NULL);
403 if (rc != VINF_SUCCESS)
404 return false;
405
406 return true;
407}
408RT_EXPORT_SYMBOL(RTNetIsIPv6AddrStr);
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette