Changeset 50418 in vbox for trunk/src/VBox/Runtime
- Timestamp:
- Feb 11, 2014 6:50:58 PM (11 years ago)
- Location:
- trunk/src/VBox/Runtime
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/net/netaddrstr.cpp
r46178 r50418 1229 1229 } 1230 1230 RT_EXPORT_SYMBOL(RTNetIsIPv6AddrStr); 1231 1232 1233 RTDECL(bool) RTNetIsIPv4AddrStr(const char *pszAddress)1234 {1235 static char const s_szIpV4Digits[] = "0123456789.";1236 1237 size_t cchAddress = strlen(pszAddress);1238 if (cchAddress < 7 || cchAddress > 15)1239 return false;1240 1241 const char *pStart, *pFrom, *pTo, *pNow;1242 pStart = pNow = pFrom = pTo = pszAddress;1243 1244 unsigned cOctets = 0;1245 while (*pNow != '\0')1246 {1247 const char *pChar = (const char *)memchr(s_szIpV4Digits, *pNow, sizeof(s_szIpV4Digits) - 1);1248 const char *pDigit = (const char *)memchr(s_szIpV4Digits, *pNow, sizeof(s_szIpV4Digits) - 2);1249 const char *pNext = pNow + 1;1250 1251 if (!pChar)1252 return false;1253 1254 if (pDigit && *pNext != '\0')1255 {1256 pTo = pNow;1257 pNow++;1258 continue;1259 }1260 1261 if (*pNow == '.' || *pNext == '\0')1262 {1263 if (*pNext == '\0')1264 pTo = pNow;1265 1266 size_t cchSub = pTo - pFrom;1267 if (cchSub > 2)1268 return false;1269 1270 char szDummy[4] = { 0, 0, 0, 0 };1271 memcpy(szDummy, pFrom, cchSub + 1);1272 1273 int rc = RTStrToUInt8Ex(szDummy, NULL, 10, NULL);1274 if (rc != VINF_SUCCESS)1275 return false;1276 1277 cOctets++;1278 if (cOctets > 4)1279 return false;1280 pFrom = pNext;1281 }1282 pNow++;1283 }1284 1285 if (cOctets != 4)1286 return false;1287 1288 return true;1289 }1290 RT_EXPORT_SYMBOL(RTNetIsIPv4AddrStr); -
trunk/src/VBox/Runtime/common/net/netaddrstr2.cpp
r48935 r50418 36 36 #include "internal/string.h" 37 37 38 RTDECL(int) RTNetStrToIPv4Addr(const char *pszAddr, PRTNETADDRIPV4 pAddr) 38 39 DECLHIDDEN(int) rtNetStrToIPv4AddrEx(const char *pcszAddr, PRTNETADDRIPV4 pAddr, 40 char **ppszNext) 39 41 { 40 42 char *pszNext; 41 AssertPtrReturn(pszAddr, VERR_INVALID_PARAMETER); 43 int rc; 44 45 AssertPtrReturn(pcszAddr, VERR_INVALID_PARAMETER); 42 46 AssertPtrReturn(pAddr, VERR_INVALID_PARAMETER); 43 47 44 int rc = RTStrToUInt8Ex(RTStrStripL(pszAddr), &pszNext, 10, &pAddr->au8[0]);48 rc = RTStrToUInt8Ex(pcszAddr, &pszNext, 10, &pAddr->au8[0]); 45 49 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS) 46 50 return VERR_INVALID_PARAMETER; … … 61 65 62 66 rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &pAddr->au8[3]); 63 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_SPACES )67 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_SPACES && rc != VWRN_TRAILING_CHARS) 64 68 return VERR_INVALID_PARAMETER; 69 70 if (ppszNext != NULL) 71 *ppszNext = pszNext; 72 return VINF_SUCCESS; 73 } 74 75 76 RTDECL(int) RTNetStrToIPv4AddrEx(const char *pcszAddr, PRTNETADDRIPV4 pAddr, 77 char **ppszNext) 78 { 79 return rtNetStrToIPv4AddrEx(pcszAddr, pAddr, ppszNext); 80 } 81 RT_EXPORT_SYMBOL(RTNetStrToIPv4AddrEx); 82 83 84 RTDECL(int) RTNetStrToIPv4Addr(const char *pcszAddr, PRTNETADDRIPV4 pAddr) 85 { 86 char *pszNext; 87 int rc; 88 89 AssertPtrReturn(pcszAddr, VERR_INVALID_PARAMETER); 90 AssertPtrReturn(pAddr, VERR_INVALID_PARAMETER); 91 92 pcszAddr = RTStrStripL(pcszAddr); 93 rc = rtNetStrToIPv4AddrEx(pcszAddr, pAddr, &pszNext); 94 if (rc != VINF_SUCCESS) 95 return VERR_INVALID_PARAMETER; 96 65 97 pszNext = RTStrStripL(pszNext); 66 if (*pszNext )98 if (*pszNext != '\0') 67 99 return VERR_INVALID_PARAMETER; 68 100 … … 71 103 RT_EXPORT_SYMBOL(RTNetStrToIPv4Addr); 72 104 105 106 RTDECL(bool) RTNetIsIPv4AddrStr(const char *pcszAddr) 107 { 108 RTNETADDRIPV4 addrIPv4; 109 char *pszNext; 110 int rc; 111 112 if (pcszAddr == NULL) 113 return false; 114 115 rc = rtNetStrToIPv4AddrEx(pcszAddr, &addrIPv4, &pszNext); 116 if (rc != VINF_SUCCESS) 117 return false; 118 119 if (*pszNext != '\0') 120 return false; 121 122 return true; 123 } 124 RT_EXPORT_SYMBOL(RTNetIsIPv4AddrStr); -
trunk/src/VBox/Runtime/testcase/tstRTNetIPv4.cpp
r50415 r50418 66 66 67 67 68 #define CHECKADDREX(String, Trailer, rcExpected, ExpectedAddr) \ 69 do { \ 70 RTNETADDRIPV4 Addr; \ 71 const char *strAll = String /* concat */ Trailer; \ 72 const char *pTrailer = strAll + sizeof(String) - 1; \ 73 char *pNext = NULL; \ 74 int rc2 = RTNetStrToIPv4AddrEx(strAll, &Addr, &pNext); \ 75 if ((rcExpected) && !rc2) \ 76 { \ 77 RTTestIFailed("at line %d: '%s': expected %Rrc got %Rrc\n", \ 78 __LINE__, String, (rcExpected), rc2); \ 79 } \ 80 else if ((rcExpected) != rc2 \ 81 || (rc2 == VINF_SUCCESS \ 82 && (RT_H2N_U32_C(ExpectedAddr) != Addr.u \ 83 || pTrailer != pNext))) \ 84 { \ 85 RTTestIFailed("at line %d: '%s': expected %Rrc got %Rrc," \ 86 " expected address %RTnaipv4 got %RTnaipv4" \ 87 " expected trailer \"%s\" got %s%s%s" \ 88 "\n", \ 89 __LINE__, String, rcExpected, rc2, \ 90 RT_H2N_U32_C(ExpectedAddr), Addr.u, \ 91 pTrailer, \ 92 pNext ? "\"" : "", \ 93 pNext ? pNext : "(null)", \ 94 pNext ? "\"" : ""); \ 95 } \ 96 } while (0) 97 98 68 99 int main() 69 100 { … … 91 122 BADADDR("1.2.3.666"); 92 123 124 /* 125 * Parsing itself is covered by the tests above, here we only 126 * check trailers 127 */ 128 CHECKADDREX("1.2.3.4", "", VINF_SUCCESS, 0x01020304); 129 CHECKADDREX("1.2.3.4", " ", VINF_SUCCESS, 0x01020304); 130 CHECKADDREX("1.2.3.4", "x", VINF_SUCCESS, 0x01020304); 131 CHECKADDREX("1.2.3.444", "", VERR_INVALID_PARAMETER, 0); 132 93 133 return RTTestSummaryAndDestroy(hTest); 94 134 }
Note:
See TracChangeset
for help on using the changeset viewer.