1 | /* $Id: macstr.cpp 79747 2019-07-12 23:00:54Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Command Line Parsing
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2019 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 <iprt/net.h> /* must come before getopt.h */
|
---|
33 | #include "internal/iprt.h"
|
---|
34 |
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/ctype.h>
|
---|
37 | #include <iprt/errcore.h>
|
---|
38 | #include <iprt/message.h>
|
---|
39 | #include <iprt/string.h>
|
---|
40 |
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Converts an stringified Ethernet MAC address into the RTMAC representation.
|
---|
44 | *
|
---|
45 | * @returns VINF_SUCCESS on success.
|
---|
46 | *
|
---|
47 | * @param pszValue The value to convert.
|
---|
48 | * @param pAddr Where to store the result.
|
---|
49 | */
|
---|
50 | RTDECL(int) RTNetStrToMacAddr(const char *pszValue, PRTMAC pAddr)
|
---|
51 | {
|
---|
52 | /*
|
---|
53 | * First check if it might be a 12 xdigit string without any separators.
|
---|
54 | */
|
---|
55 | size_t cchValue = strlen(pszValue);
|
---|
56 | if (cchValue >= 12 && memchr(pszValue, ':', 12) == NULL)
|
---|
57 | {
|
---|
58 | bool fOkay = true;
|
---|
59 | for (size_t off = 0; off < 12 && fOkay; off++)
|
---|
60 | fOkay = RT_C_IS_XDIGIT(pszValue[off]);
|
---|
61 | if (fOkay && cchValue > 12)
|
---|
62 | for (size_t off = 12; off < cchValue && fOkay; off++)
|
---|
63 | fOkay = RT_C_IS_SPACE(pszValue[off]);
|
---|
64 | if (fOkay)
|
---|
65 | {
|
---|
66 | int rc = RTStrConvertHexBytes(pszValue, pAddr, sizeof(*pAddr), 0);
|
---|
67 | if (RT_SUCCESS(rc))
|
---|
68 | rc = VINF_SUCCESS;
|
---|
69 | return rc;
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | /*
|
---|
74 | * Not quite sure if I should accept stuff like "08::27:::1" here...
|
---|
75 | * The code is accepting "::" patterns now, except for for the first
|
---|
76 | * and last parts.
|
---|
77 | */
|
---|
78 |
|
---|
79 | /* first */
|
---|
80 | char *pszNext;
|
---|
81 | int rc = RTStrToUInt8Ex(RTStrStripL(pszValue), &pszNext, 16, &pAddr->au8[0]);
|
---|
82 | if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
|
---|
83 | return VERR_INVALID_PARAMETER;
|
---|
84 | if (*pszNext++ != ':')
|
---|
85 | return VERR_INVALID_PARAMETER;
|
---|
86 |
|
---|
87 | /* middle */
|
---|
88 | for (unsigned i = 1; i < 5; i++)
|
---|
89 | {
|
---|
90 | if (*pszNext == ':')
|
---|
91 | pAddr->au8[i] = 0;
|
---|
92 | else
|
---|
93 | {
|
---|
94 | rc = RTStrToUInt8Ex(pszNext, &pszNext, 16, &pAddr->au8[i]);
|
---|
95 | if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
|
---|
96 | return rc;
|
---|
97 | if (*pszNext != ':')
|
---|
98 | return VERR_INVALID_PARAMETER;
|
---|
99 | }
|
---|
100 | pszNext++;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /* last */
|
---|
104 | rc = RTStrToUInt8Ex(pszNext, &pszNext, 16, &pAddr->au8[5]);
|
---|
105 | if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_SPACES)
|
---|
106 | return rc;
|
---|
107 | pszNext = RTStrStripL(pszNext);
|
---|
108 | if (*pszNext)
|
---|
109 | return VERR_INVALID_PARAMETER;
|
---|
110 |
|
---|
111 | return VINF_SUCCESS;
|
---|
112 | }
|
---|
113 | RT_EXPORT_SYMBOL(RTNetStrToMacAddr);
|
---|