Changeset 29845 in vbox for trunk/src/VBox
- Timestamp:
- May 27, 2010 11:29:36 AM (15 years ago)
- Location:
- trunk/src/VBox/Runtime
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/misc/cidr.cpp
r29840 r29845 32 32 #include "internal/iprt.h" 33 33 34 #include <iprt/assert.h> 34 35 #include <iprt/ctype.h> 35 36 #include <iprt/string.h> … … 44 45 uint32_t u32Network; 45 46 const char *psz = pszAddress; 47 const char *pszNetmask; 46 48 char *pszNext; 47 49 int rc = VINF_SUCCESS; 48 50 int cDelimiter = 0; 49 51 int cDelimiterLimit = 0; 50 if ( pszAddress == NULL 51 || pNetwork == NULL 52 || pNetmask == NULL) 53 return VERR_INVALID_PARAMETER; 54 char *pszNetmask = RTStrStr(psz, "/"); 52 53 AssertPtrReturn(pszAddress, VERR_INVALID_PARAMETER); 54 AssertPtrReturn(pNetwork, VERR_INVALID_PARAMETER); 55 AssertPtrReturn(pNetmask, VERR_INVALID_PARAMETER); 56 57 pszNetmask = RTStrStr(psz, "/"); 55 58 *(uint32_t *)addr = 0; 56 if ( pszNetmask == NULL)57 cBits = 32; 58 else 59 { 59 if (!pszNetmask) 60 cBits = 32; 61 else 62 { 60 63 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 if ( RT_FAILURE(rc) 65 || cBits > 32 66 || rc != VINF_SUCCESS) /* No trailing symbols are accptable after the digit */ 64 67 return VERR_INVALID_PARAMETER; 65 68 } 66 69 u32Netmask = ~(uint32_t)((1<< (32 - cBits)) - 1); 67 70 68 rc = RTStrToUInt8Ex(psz, &pszNext, 10, &addr[0]); 69 if (RT_FAILURE(rc)) 70 return rc; 71 72 if (cBits < 9) 71 if (cBits <= 8) 73 72 cDelimiterLimit = 0; 74 73 else if (cBits <= 16) … … 79 78 cDelimiterLimit = 3; 80 79 81 rc = RTStrToUInt8Ex(psz, &pszNext, 10, &addr[cDelimiter]); 82 while (RT_SUCCESS(rc)) 80 for (;;) 83 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 84 87 if (*pszNext == '.') 85 88 cDelimiter++; 86 else if ( cDelimiter >= cDelimiterLimit87 && ( *pszNext == '\0'88 || *pszNext == '/'))89 else if ( cDelimiter >= cDelimiterLimit 90 && ( *pszNext == '\0' 91 || *pszNext == '/')) 89 92 break; 90 else 93 else 91 94 return VERR_INVALID_PARAMETER; 92 95 93 if (cDelimiter > 3)94 /* no more than four octets */96 if (cDelimiter > 3) 97 /* not more than four octets */ 95 98 return VERR_INVALID_PARAMETER; 96 99 97 rc = RTStrToUInt8Ex(pszNext + 1, &pszNext, 10, &addr[cDelimiter]); 98 if (rc == VWRN_NUMBER_TOO_BIG) 99 break; 100 psz = pszNext + 1; 100 101 } 101 if ( RT_FAILURE(rc)102 || rc == VWRN_NUMBER_TOO_BIG)103 return VERR_INVALID_PARAMETER;104 102 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 103 104 /* Corner case: see RFC 790 page 2 and RFC 4632 page 6. */ 105 if ( addr[0] == 0 107 106 && ( *(uint32_t *)addr != 0 108 107 || u32Netmask == (uint32_t)~0)) … … 111 110 if ((u32Network & ~u32Netmask) != 0) 112 111 return VERR_INVALID_PARAMETER; 113 112 114 113 *pNetmask = u32Netmask; 115 *pNetwork = u32Network; 114 *pNetwork = u32Network; 116 115 return VINF_SUCCESS; 117 116 } -
trunk/src/VBox/Runtime/testcase/tstRTCidr.cpp
r29841 r29845 70 70 CHECKNETWORK("10.0.0/-45", VERR_INVALID_PARAMETER, 0, 0); 71 71 CHECKNETWORK("10.0.0/24", VINF_SUCCESS, 0x0A000000, 0xFFFFFF00); 72 CHECKNETWORK("10..0.0/24", VERR_INVALID_PARAMETER, 0, 0); 73 CHECKNETWORK(".10.0.0/24", VERR_INVALID_PARAMETER, 0, 0); 74 CHECKNETWORK("10.0.0//24", VERR_INVALID_PARAMETER, 0, 0); 72 75 CHECKNETWORK("10.0.0/8", VINF_SUCCESS, 0x0A000000, 0xFF000000); 73 76 CHECKNETWORK("10.0.0./24", VERR_INVALID_PARAMETER, 0, 0);
Note:
See TracChangeset
for help on using the changeset viewer.