VirtualBox

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

Last change on this file since 76553 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 3.8 KB
Line 
1/* $Id: cidr.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * IPRT - IPv4 address parsing.
4 */
5
6/*
7 * Copyright (C) 2006-2019 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/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/cidr.h>
32#include "internal/iprt.h"
33
34#include <iprt/assert.h>
35#include <iprt/ctype.h>
36#include <iprt/errcore.h>
37#include <iprt/string.h>
38#include <iprt/stream.h>
39
40
41RTDECL(int) RTCidrStrToIPv4(const char *pszAddress, PRTNETADDRIPV4 pNetwork, PRTNETADDRIPV4 pNetmask)
42{
43 uint8_t cBits;
44 uint8_t addr[4];
45 uint32_t u32Netmask;
46 uint32_t u32Network;
47 const char *psz = pszAddress;
48 const char *pszNetmask;
49 char *pszNext;
50 int rc = VINF_SUCCESS;
51 int cDelimiter = 0;
52 int cDelimiterLimit = 0;
53
54 AssertPtrReturn(pszAddress, VERR_INVALID_PARAMETER);
55 AssertPtrReturn(pNetwork, VERR_INVALID_PARAMETER);
56 AssertPtrReturn(pNetmask, VERR_INVALID_PARAMETER);
57
58 pszNetmask = RTStrStr(psz, "/");
59 *(uint32_t *)addr = 0;
60 if (!pszNetmask)
61 cBits = 32;
62 else
63 {
64 rc = RTStrToUInt8Ex(pszNetmask + 1, &pszNext, 10, &cBits);
65 if ( RT_FAILURE(rc)
66 || cBits > 32
67 || rc != VINF_SUCCESS) /* No trailing symbols are acceptable after the digit */
68 return VERR_INVALID_PARAMETER;
69 }
70 u32Netmask = ~(uint32_t)((1<< (32 - cBits)) - 1);
71
72 if (cBits <= 8)
73 cDelimiterLimit = 0;
74 else if (cBits <= 16)
75 cDelimiterLimit = 1;
76 else if (cBits <= 24)
77 cDelimiterLimit = 2;
78 else if (cBits <= 32)
79 cDelimiterLimit = 3;
80
81 for (;;)
82 {
83 rc = RTStrToUInt8Ex(psz, &pszNext, 10, &addr[cDelimiter]);
84 if ( RT_FAILURE(rc)
85 || rc == VWRN_NUMBER_TOO_BIG)
86 return VERR_INVALID_PARAMETER;
87
88 if (*pszNext == '.')
89 cDelimiter++;
90 else if ( cDelimiter >= cDelimiterLimit
91 && ( *pszNext == '\0'
92 || *pszNext == '/'))
93 break;
94 else
95 return VERR_INVALID_PARAMETER;
96
97 if (cDelimiter > 3)
98 /* not more than four octets */
99 return VERR_INVALID_PARAMETER;
100
101 psz = pszNext + 1;
102 }
103 u32Network = RT_MAKE_U32_FROM_U8(addr[3], addr[2], addr[1], addr[0]);
104
105 /* Corner case: see RFC 790 page 2 and RFC 4632 page 6. */
106 if ( addr[0] == 0
107 && ( *(uint32_t *)addr != 0
108 || u32Netmask == (uint32_t)~0))
109 return VERR_INVALID_PARAMETER;
110
111 if ((u32Network & ~u32Netmask) != 0)
112 return VERR_INVALID_PARAMETER;
113
114 pNetmask->u = u32Netmask;
115 pNetwork->u = u32Network;
116 return VINF_SUCCESS;
117}
118RT_EXPORT_SYMBOL(RTCidrStrToIPv4);
119
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