VirtualBox

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

Last change on this file since 54479 was 49326, checked in by vboxsync, 11 years ago

IPRT: exposes RTNetStrToMAC (resolves todo in misc/getopt.cpp).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.0 KB
Line 
1/* $Id: macstr.cpp 49326 2013-10-30 04:33:40Z vboxsync $ */
2/** @file
3 * IPRT - Command Line Parsing
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.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* Header Files *
29*******************************************************************************/
30#include <iprt/cidr.h>
31#include <iprt/net.h> /* must come before getopt.h */
32#include "internal/iprt.h"
33
34#include <iprt/assert.h>
35#include <iprt/ctype.h>
36#include <iprt/err.h>
37#include <iprt/message.h>
38#include <iprt/string.h>
39
40
41/**
42 * Converts an stringified Ethernet MAC address into the RTMAC representation.
43 *
44 * @returns VINF_SUCCESS on success.
45 *
46 * @param pszValue The value to convert.
47 * @param pAddr Where to store the result.
48 */
49RTDECL(int) RTNetStrToMacAddr(const char *pszValue, PRTMAC pAddr)
50{
51 /*
52 * Not quite sure if I should accept stuff like "08::27:::1" here...
53 * The code is accepting "::" patterns now, except for for the first
54 * and last parts.
55 */
56
57 /* first */
58 char *pszNext;
59 int rc = RTStrToUInt8Ex(RTStrStripL(pszValue), &pszNext, 16, &pAddr->au8[0]);
60 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
61 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
62 if (*pszNext++ != ':')
63 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
64
65 /* middle */
66 for (unsigned i = 1; i < 5; i++)
67 {
68 if (*pszNext == ':')
69 pAddr->au8[i] = 0;
70 else
71 {
72 rc = RTStrToUInt8Ex(pszNext, &pszNext, 16, &pAddr->au8[i]);
73 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
74 return rc;
75 if (*pszNext != ':')
76 return VERR_INVALID_PARAMETER;
77 }
78 pszNext++;
79 }
80
81 /* last */
82 rc = RTStrToUInt8Ex(pszNext, &pszNext, 16, &pAddr->au8[5]);
83 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_SPACES)
84 return rc;
85 pszNext = RTStrStripL(pszNext);
86 if (*pszNext)
87 return VERR_INVALID_PARAMETER;
88
89 return VINF_SUCCESS;
90}
91RT_EXPORT_SYMBOL(RTNetStrToMacAddr);
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette