VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/net/macstr.cpp

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
1/* $Id: macstr.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Command Line Parsing
4 */
5
6/*
7 * Copyright (C) 2013-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/cidr.h>
42#include <iprt/net.h> /* must come before getopt.h */
43#include "internal/iprt.h"
44
45#include <iprt/assert.h>
46#include <iprt/ctype.h>
47#include <iprt/errcore.h>
48#include <iprt/message.h>
49#include <iprt/string.h>
50
51
52/**
53 * Converts an stringified Ethernet MAC address into the RTMAC representation.
54 *
55 * @returns VINF_SUCCESS on success.
56 *
57 * @param pszValue The value to convert.
58 * @param pAddr Where to store the result.
59 */
60RTDECL(int) RTNetStrToMacAddr(const char *pszValue, PRTMAC pAddr)
61{
62 /*
63 * First check if it might be a 12 xdigit string without any separators.
64 */
65 size_t cchValue = strlen(pszValue);
66 if (cchValue >= 12 && memchr(pszValue, ':', 12) == NULL)
67 {
68 bool fOkay = true;
69 for (size_t off = 0; off < 12 && fOkay; off++)
70 fOkay = RT_C_IS_XDIGIT(pszValue[off]);
71 if (fOkay && cchValue > 12)
72 for (size_t off = 12; off < cchValue && fOkay; off++)
73 fOkay = RT_C_IS_SPACE(pszValue[off]);
74 if (fOkay)
75 {
76 int rc = RTStrConvertHexBytes(pszValue, pAddr, sizeof(*pAddr), 0);
77 if (RT_SUCCESS(rc))
78 rc = VINF_SUCCESS;
79 return rc;
80 }
81 }
82
83 /*
84 * Not quite sure if I should accept stuff like "08::27:::1" here...
85 * The code is accepting "::" patterns now, except for for the first
86 * and last parts.
87 */
88
89 /* first */
90 char *pszNext;
91 int rc = RTStrToUInt8Ex(RTStrStripL(pszValue), &pszNext, 16, &pAddr->au8[0]);
92 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
93 return VERR_INVALID_PARAMETER;
94 if (*pszNext++ != ':')
95 return VERR_INVALID_PARAMETER;
96
97 /* middle */
98 for (unsigned i = 1; i < 5; i++)
99 {
100 if (*pszNext == ':')
101 pAddr->au8[i] = 0;
102 else
103 {
104 rc = RTStrToUInt8Ex(pszNext, &pszNext, 16, &pAddr->au8[i]);
105 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
106 return rc;
107 if (*pszNext != ':')
108 return VERR_INVALID_PARAMETER;
109 }
110 pszNext++;
111 }
112
113 /* last */
114 rc = RTStrToUInt8Ex(pszNext, &pszNext, 16, &pAddr->au8[5]);
115 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_SPACES)
116 return rc;
117 pszNext = RTStrStripL(pszNext);
118 if (*pszNext)
119 return VERR_INVALID_PARAMETER;
120
121 return VINF_SUCCESS;
122}
123RT_EXPORT_SYMBOL(RTNetStrToMacAddr);
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use