- Timestamp:
- Dec 7, 2018 3:13:22 PM (6 years ago)
- Location:
- trunk/src/VBox/Runtime
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/net/netaddrstr2.cpp
r71435 r76061 196 196 197 197 198 RTDECL(int) RTNetStrToIPv4Cidr(const char *pcszAddr, PRTNETADDRIPV4 pAddr, int *piPrefix) 199 { 200 RTNETADDRIPV4 Addr; 201 uint8_t u8Prefix; 202 char *pszNext; 203 int rc; 204 205 AssertPtrReturn(pcszAddr, VERR_INVALID_PARAMETER); 206 AssertPtrReturn(pAddr, VERR_INVALID_PARAMETER); 207 AssertPtrReturn(piPrefix, VERR_INVALID_PARAMETER); 208 209 pcszAddr = RTStrStripL(pcszAddr); 210 rc = rtNetStrToIPv4AddrEx(pcszAddr, &Addr, &pszNext); 211 if (RT_FAILURE(rc)) 212 return rc; 213 214 /* if prefix is missing, treat is as exact (/32) address specification */ 215 if (*pszNext == '\0' || rc == VWRN_TRAILING_SPACES) 216 { 217 *pAddr = Addr; 218 *piPrefix = 32; 219 return VINF_SUCCESS; 220 } 221 222 if (*pszNext != '/') 223 return VERR_INVALID_PARAMETER; 224 225 ++pszNext; 226 rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &u8Prefix); 227 if (RT_FAILURE(rc) || rc == VWRN_TRAILING_CHARS) 228 return VERR_INVALID_PARAMETER; 229 230 if (u8Prefix == 0 || u8Prefix > 32) 231 return VERR_INVALID_PARAMETER; 232 233 *pAddr = Addr; 234 *piPrefix = u8Prefix; 235 return VINF_SUCCESS; 236 } 237 RT_EXPORT_SYMBOL(RTNetStrToIPv4Cidr); 238 239 198 240 static int rtNetStrToHexGroup(const char *pcszValue, char **ppszNext, 199 241 uint16_t *pu16) -
trunk/src/VBox/Runtime/testcase/tstRTNetIPv4.cpp
r71435 r76061 97 97 98 98 99 #define CHECKCIDR(String, rcExpected, ExpectedAddr, iExpectedPrefix) \ 100 do { \ 101 RTNETADDRIPV4 Addr; \ 102 int iPrefix; \ 103 \ 104 int rc2 = RTNetStrToIPv4Cidr(String, &Addr, &iPrefix); \ 105 if ((rcExpected) && !rc2) \ 106 { \ 107 RTTestIFailed("at line %d: '%s': expected %Rrc got %Rrc\n", \ 108 __LINE__, String, (rcExpected), rc2); \ 109 } \ 110 else if ( (rcExpected) != rc2 \ 111 || ( rc2 == VINF_SUCCESS \ 112 && ( RT_H2N_U32_C(ExpectedAddr) != Addr.u \ 113 || iExpectedPrefix != iPrefix))) \ 114 { \ 115 RTTestIFailed("at line %d: '%s': expected %Rrc got %Rrc," \ 116 " expected address %RTnaipv4/%d got %RTnaipv4/%d\n", \ 117 __LINE__, String, rcExpected, rc2, \ 118 RT_H2N_U32_C(ExpectedAddr), (iExpectedPrefix), \ 119 Addr.u, iPrefix); \ 120 } \ 121 } while (0) 122 123 #define GOODCIDR(String, ExpectedAddr, iExpectedPrefix) \ 124 CHECKCIDR(String, VINF_SUCCESS, ExpectedAddr, iExpectedPrefix) 125 126 #define BADCIDR(String) \ 127 CHECKCIDR(String, VERR_INVALID_PARAMETER, 0, 0) 128 129 99 130 #define CHECKISADDR(String, fExpected) \ 100 131 do { \ … … 274 305 CHECKADDREX("1.2.3.4", "x", VWRN_TRAILING_CHARS, 0x01020304); 275 306 CHECKADDREX("1.2.3.444", "", VERR_INVALID_PARAMETER, 0); 307 308 309 GOODCIDR("1.2.3.4", 0x01020304, 32); 310 GOODCIDR("1.2.3.4/32", 0x01020304, 32); 311 GOODCIDR("1.2.3.4/24", 0x01020304, 24); /* address is not truncated to prefix */ 312 313 GOODCIDR("\t " "1.2.3.4/24", 0x01020304, 24); /* leading spaces ok */ 314 GOODCIDR( "1.2.3.4/24" " \t", 0x01020304, 24); /* trailing spaces ok */ 315 GOODCIDR("\t " "1.2.3.4/24" " \t", 0x01020304, 24); /* both are ok */ 316 317 BADCIDR("1.2.3.4/0"); /* prefix can't be zero */ 318 BADCIDR("1.2.3.4/33"); /* prefix is too big */ 319 BADCIDR("1.2.3.4/-1"); /* prefix is negative */ 320 BADCIDR("1.2.3.4/"); /* prefix is missing */ 321 BADCIDR("1.2.3.4/"); /* prefix is missing */ 322 BADCIDR("1.2.3.4/a"); /* prefix is not a number */ 323 BADCIDR("1.2.3.4/0xa"); /* prefix is not decimal */ 324 // BADCIDR("1.2.3.0/024"); /* XXX: prefix is not decimal */ 325 326 BADCIDR("1.2.3.0 /24"); /* no spaces after address */ 327 BADCIDR("1.2.3.0/ 24"); /* no spaces after slash */ 328 329 BADCIDR("1.2.3.0/24" "x"); /* trailing chars */ 330 BADCIDR("1.2.3.0/24" " x"); /* trailing chars */ 331 332 276 333 277 334 /* NB: RTNetIsIPv4AddrStr does NOT allow leading/trailing whitespace */
Note:
See TracChangeset
for help on using the changeset viewer.