VirtualBox

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

Last change on this file since 45390 was 45047, checked in by vboxsync, 12 years ago

iprt/cidr.h: RTIPV4ADDR -> RTNETADDRIPV4.
the dependencies are modified as well.

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