VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/misc/cidr.cpp

Last change on this file was 106061, checked in by vboxsync, 8 weeks ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 4.1 KB
Line 
1/* $Id: cidr.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * IPRT - IPv4 address parsing.
4 */
5
6/*
7 * Copyright (C) 2006-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/cidr.h>
42#include "internal/iprt.h"
43
44#include <iprt/assert.h>
45#include <iprt/ctype.h>
46#include <iprt/errcore.h>
47#include <iprt/string.h>
48#include <iprt/stream.h>
49
50
51RTDECL(int) RTCidrStrToIPv4(const char *pszAddress, PRTNETADDRIPV4 pNetwork, PRTNETADDRIPV4 pNetmask)
52{
53 uint8_t cBits;
54 uint8_t addr[4];
55 uint32_t u32Netmask;
56 uint32_t u32Network;
57 const char *psz = pszAddress;
58 const char *pszNetmask;
59 char *pszNext;
60 int rc = VINF_SUCCESS;
61 int cDelimiter = 0;
62 int cDelimiterLimit = 0;
63
64 AssertPtrReturn(pszAddress, VERR_INVALID_PARAMETER);
65 AssertPtrReturn(pNetwork, VERR_INVALID_PARAMETER);
66 AssertPtrReturn(pNetmask, VERR_INVALID_PARAMETER);
67
68 pszNetmask = RTStrStr(psz, "/");
69 *(uint32_t *)addr = 0;
70 if (!pszNetmask)
71 cBits = 32;
72 else
73 {
74 rc = RTStrToUInt8Ex(pszNetmask + 1, &pszNext, 10, &cBits);
75 if ( RT_FAILURE(rc)
76 || cBits > 32
77 || rc != VINF_SUCCESS) /* No trailing symbols are acceptable after the digit */
78 return VERR_INVALID_PARAMETER;
79 }
80 u32Netmask = ~(uint32_t)((1<< (32 - cBits)) - 1);
81
82 if (cBits <= 8)
83 cDelimiterLimit = 0;
84 else if (cBits <= 16)
85 cDelimiterLimit = 1;
86 else if (cBits <= 24)
87 cDelimiterLimit = 2;
88 else if (cBits <= 32)
89 cDelimiterLimit = 3;
90
91 for (;;)
92 {
93 rc = RTStrToUInt8Ex(psz, &pszNext, 10, &addr[cDelimiter]);
94 if ( RT_FAILURE(rc)
95 || rc == VWRN_NUMBER_TOO_BIG)
96 return VERR_INVALID_PARAMETER;
97
98 if (*pszNext == '.')
99 cDelimiter++;
100 else if ( cDelimiter >= cDelimiterLimit
101 && ( *pszNext == '\0'
102 || *pszNext == '/'))
103 break;
104 else
105 return VERR_INVALID_PARAMETER;
106
107 if (cDelimiter > 3)
108 /* not more than four octets */
109 return VERR_INVALID_PARAMETER;
110
111 psz = pszNext + 1;
112 }
113 u32Network = RT_MAKE_U32_FROM_U8(addr[3], addr[2], addr[1], addr[0]);
114
115 /* Corner case: see RFC 790 page 2 and RFC 4632 page 6. */
116 if ( addr[0] == 0
117 && ( *(uint32_t *)addr != 0
118 || u32Netmask == (uint32_t)~0))
119 return VERR_INVALID_PARAMETER;
120
121 if ((u32Network & ~u32Netmask) != 0)
122 return VERR_INVALID_PARAMETER;
123
124 pNetmask->u = u32Netmask;
125 pNetwork->u = u32Network;
126 return VINF_SUCCESS;
127}
128RT_EXPORT_SYMBOL(RTCidrStrToIPv4);
129
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