VirtualBox

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

Last change on this file since 29840 was 29840, checked in by vboxsync, 15 years ago

Runtime: no more than four octets are allowed.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.7 KB
Line 
1/* $Id: cidr.cpp 29840 2010-05-27 10:19:59Z 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.215389.xyz. 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
39RTDECL(int) RTCidrStrToIPv4(const char *pszAddress, PRTIPV4ADDR pNetwork, PRTIPV4ADDR pNetmask)
40{
41 uint8_t cBits;
42 uint8_t addr[4];
43 uint32_t u32Netmask;
44 uint32_t u32Network;
45 const char *psz = pszAddress;
46 char *pszNext;
47 int rc = VINF_SUCCESS;
48 int cDelimiter = 0;
49 int cDelimiterLimit = 0;
50 if ( pszAddress == NULL
51 || pNetwork == NULL
52 || pNetmask == NULL)
53 return VERR_INVALID_PARAMETER;
54 char *pszNetmask = RTStrStr(psz, "/");
55 *(uint32_t *)addr = 0;
56 if (pszNetmask == NULL)
57 cBits = 32;
58 else
59 {
60 rc = RTStrToUInt8Ex(pszNetmask + 1, &pszNext, 10, &cBits);
61 if ( RT_FAILURE(rc)
62 || cBits > 32
63 || rc != 0) /* No trailing symbols are accptable after the digit */
64 return VERR_INVALID_PARAMETER;
65 }
66 u32Netmask = ~(uint32_t)((1<< (32 - cBits)) - 1);
67
68 rc = RTStrToUInt8Ex(psz, &pszNext, 10, &addr[0]);
69 if (RT_FAILURE(rc))
70 return rc;
71
72 if (cBits < 9)
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 rc = RTStrToUInt8Ex(psz, &pszNext, 10, &addr[cDelimiter]);
82 while (RT_SUCCESS(rc))
83 {
84 if (*pszNext == '.')
85 cDelimiter++;
86 else if( cDelimiter >= cDelimiterLimit
87 && ( *pszNext == '\0'
88 || *pszNext == '/'))
89 break;
90 else
91 return VERR_INVALID_PARAMETER;
92
93 if(cDelimiter > 3)
94 /* no more than four octets */
95 return VERR_INVALID_PARAMETER;
96
97 rc = RTStrToUInt8Ex(pszNext + 1, &pszNext, 10, &addr[cDelimiter]);
98 if (rc == VWRN_NUMBER_TOO_BIG)
99 break;
100 }
101 if ( RT_FAILURE(rc)
102 || rc == VWRN_NUMBER_TOO_BIG)
103 return VERR_INVALID_PARAMETER;
104 u32Network = RT_MAKE_U32_FROM_U8(addr[3], addr[2], addr[1], addr[0]);
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 = u32Netmask;
115 *pNetwork = u32Network;
116 return VINF_SUCCESS;
117}
118RT_EXPORT_SYMBOL(RTCidrStrToIPv4);
119
Note: See TracBrowser for help on using the repository browser.

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