1 | /* $Id: cidr.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - IPv4 address parsing.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2008 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.alldomusa.eu.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 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #include <iprt/cidr.h>
|
---|
32 | #include "internal/iprt.h"
|
---|
33 |
|
---|
34 | #include <iprt/ctype.h>
|
---|
35 | #include <iprt/string.h>
|
---|
36 | #include <iprt/stream.h>
|
---|
37 |
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Scan a digit of an IPv4 address.
|
---|
41 | *
|
---|
42 | * @returns IPRT status code.
|
---|
43 | *
|
---|
44 | * @param iDigit The index of the IPv4 digit to scan.
|
---|
45 | * @param psz Pointer to the begin of the string.
|
---|
46 | * @param ppszNext Pointer to variable that should be set pointing to the first invalid character. (output)
|
---|
47 | * @param pu8 Pointer to the digit to write (output).
|
---|
48 | */
|
---|
49 | static int scanIPv4Digit(int iDigit, const char *psz, char **ppszNext, uint8_t *pu8)
|
---|
50 | {
|
---|
51 | int rc = RTStrToUInt8Ex(psz, ppszNext, 10, pu8);
|
---|
52 | if ( ( rc != VINF_SUCCESS
|
---|
53 | && rc != VWRN_TRAILING_CHARS)
|
---|
54 | || *pu8 > 254)
|
---|
55 | return VERR_INVALID_PARAMETER;
|
---|
56 |
|
---|
57 | /* first digit cannot be 0 */
|
---|
58 | if ( iDigit == 1
|
---|
59 | && *pu8 < 1)
|
---|
60 | return VERR_INVALID_PARAMETER;
|
---|
61 |
|
---|
62 | if (**ppszNext == '/')
|
---|
63 | return VINF_SUCCESS;
|
---|
64 |
|
---|
65 | if ( iDigit != 4
|
---|
66 | && ( **ppszNext == '\0'
|
---|
67 | || **ppszNext != '.'))
|
---|
68 | return VERR_INVALID_PARAMETER;
|
---|
69 |
|
---|
70 | return VINF_SUCCESS;
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | RTDECL(int) RTCidrStrToIPv4(const char *pszAddress, PRTIPV4ADDR pNetwork, PRTIPV4ADDR pNetmask)
|
---|
75 | {
|
---|
76 | uint8_t cBits;
|
---|
77 | uint8_t a;
|
---|
78 | uint8_t b = 0;
|
---|
79 | uint8_t c = 0;
|
---|
80 | uint8_t d = 0;
|
---|
81 | const char *psz = pszAddress;
|
---|
82 | char *pszNext;
|
---|
83 | int rc;
|
---|
84 |
|
---|
85 | do
|
---|
86 | {
|
---|
87 | /* 1st digit */
|
---|
88 | rc = scanIPv4Digit(1, psz, &pszNext, &a);
|
---|
89 | if (RT_FAILURE(rc))
|
---|
90 | return rc;
|
---|
91 | if (*pszNext == '/')
|
---|
92 | break;
|
---|
93 | psz = pszNext + 1;
|
---|
94 |
|
---|
95 | /* 2nd digit */
|
---|
96 | rc = scanIPv4Digit(2, psz, &pszNext, &b);
|
---|
97 | if (RT_FAILURE(rc))
|
---|
98 | return rc;
|
---|
99 | if (*pszNext == '/')
|
---|
100 | break;
|
---|
101 | psz = pszNext + 1;
|
---|
102 |
|
---|
103 | /* 3rd digit */
|
---|
104 | rc = scanIPv4Digit(3, psz, &pszNext, &c);
|
---|
105 | if (RT_FAILURE(rc))
|
---|
106 | return rc;
|
---|
107 | if (*pszNext == '/')
|
---|
108 | break;
|
---|
109 | psz = pszNext + 1;
|
---|
110 |
|
---|
111 | /* 4th digit */
|
---|
112 | rc = scanIPv4Digit(4, psz, &pszNext, &d);
|
---|
113 | if (RT_FAILURE(rc))
|
---|
114 | return rc;
|
---|
115 | } while (0);
|
---|
116 |
|
---|
117 | if (*pszNext == '/')
|
---|
118 | {
|
---|
119 | psz = pszNext + 1;
|
---|
120 | rc = RTStrToUInt8Ex(psz, &pszNext, 10, &cBits);
|
---|
121 | if (rc != VINF_SUCCESS || cBits < 8 || cBits > 28)
|
---|
122 | return VERR_INVALID_PARAMETER;
|
---|
123 | }
|
---|
124 | else
|
---|
125 | cBits = 0;
|
---|
126 |
|
---|
127 | for (psz = pszNext; RT_C_IS_SPACE(*psz); psz++)
|
---|
128 | /* nothing */;
|
---|
129 | if (*psz != '\0')
|
---|
130 | return VERR_INVALID_PARAMETER;
|
---|
131 |
|
---|
132 | *pNetwork = RT_MAKE_U32_FROM_U8(d, c, b, a);
|
---|
133 | *pNetmask = ~(((uint32_t)1 << (32 - cBits)) - 1);
|
---|
134 | return VINF_SUCCESS;
|
---|
135 | }
|
---|
136 | RT_EXPORT_SYMBOL(RTCidrStrToIPv4);
|
---|
137 |
|
---|