1 | /* $Id: netaddrstr2.cpp 50418 2014-02-11 18:50:58Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Network Address String Handling.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013 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 | * Header Files *
|
---|
29 | *******************************************************************************/
|
---|
30 | #include "internal/iprt.h"
|
---|
31 | #include <iprt/net.h>
|
---|
32 |
|
---|
33 | #include <iprt/mem.h>
|
---|
34 | #include <iprt/string.h>
|
---|
35 | #include <iprt/stream.h>
|
---|
36 | #include "internal/string.h"
|
---|
37 |
|
---|
38 |
|
---|
39 | DECLHIDDEN(int) rtNetStrToIPv4AddrEx(const char *pcszAddr, PRTNETADDRIPV4 pAddr,
|
---|
40 | char **ppszNext)
|
---|
41 | {
|
---|
42 | char *pszNext;
|
---|
43 | int rc;
|
---|
44 |
|
---|
45 | AssertPtrReturn(pcszAddr, VERR_INVALID_PARAMETER);
|
---|
46 | AssertPtrReturn(pAddr, VERR_INVALID_PARAMETER);
|
---|
47 |
|
---|
48 | rc = RTStrToUInt8Ex(pcszAddr, &pszNext, 10, &pAddr->au8[0]);
|
---|
49 | if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
|
---|
50 | return VERR_INVALID_PARAMETER;
|
---|
51 | if (*pszNext++ != '.')
|
---|
52 | return VERR_INVALID_PARAMETER;
|
---|
53 |
|
---|
54 | rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &pAddr->au8[1]);
|
---|
55 | if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
|
---|
56 | return VERR_INVALID_PARAMETER;
|
---|
57 | if (*pszNext++ != '.')
|
---|
58 | return VERR_INVALID_PARAMETER;
|
---|
59 |
|
---|
60 | rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &pAddr->au8[2]);
|
---|
61 | if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
|
---|
62 | return VERR_INVALID_PARAMETER;
|
---|
63 | if (*pszNext++ != '.')
|
---|
64 | return VERR_INVALID_PARAMETER;
|
---|
65 |
|
---|
66 | rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &pAddr->au8[3]);
|
---|
67 | if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_SPACES && rc != VWRN_TRAILING_CHARS)
|
---|
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 |
|
---|
97 | pszNext = RTStrStripL(pszNext);
|
---|
98 | if (*pszNext != '\0')
|
---|
99 | return VERR_INVALID_PARAMETER;
|
---|
100 |
|
---|
101 | return VINF_SUCCESS;
|
---|
102 | }
|
---|
103 | RT_EXPORT_SYMBOL(RTNetStrToIPv4Addr);
|
---|
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);
|
---|