macstr.cpp revision 10019aae08864f54606add8cef1a241932a0c8fc
5896bf5b1db43d4c2d0974e137cb1d2ad174643fvboxsync/* $Id$ */
5896bf5b1db43d4c2d0974e137cb1d2ad174643fvboxsync/** @file
5896bf5b1db43d4c2d0974e137cb1d2ad174643fvboxsync * IPRT - Command Line Parsing
5896bf5b1db43d4c2d0974e137cb1d2ad174643fvboxsync */
5896bf5b1db43d4c2d0974e137cb1d2ad174643fvboxsync
5896bf5b1db43d4c2d0974e137cb1d2ad174643fvboxsync/*
8cde32eca33c0b47371ed3ba58e4d2cceed0b548vboxsync * Copyright (C) 2013 Oracle Corporation
8cde32eca33c0b47371ed3ba58e4d2cceed0b548vboxsync *
8cde32eca33c0b47371ed3ba58e4d2cceed0b548vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
8cde32eca33c0b47371ed3ba58e4d2cceed0b548vboxsync * available from http://www.virtualbox.org. This file is free software;
8cde32eca33c0b47371ed3ba58e4d2cceed0b548vboxsync * you can redistribute it and/or modify it under the terms of the GNU
8cde32eca33c0b47371ed3ba58e4d2cceed0b548vboxsync * General Public License (GPL) as published by the Free Software
f5840fca26963de38533fd43c951758c386ed60avboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
f5840fca26963de38533fd43c951758c386ed60avboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
f5840fca26963de38533fd43c951758c386ed60avboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
f5840fca26963de38533fd43c951758c386ed60avboxsync *
f5840fca26963de38533fd43c951758c386ed60avboxsync * The contents of this file may alternatively be used under the terms
f5840fca26963de38533fd43c951758c386ed60avboxsync * of the Common Development and Distribution License Version 1.0
1400154bf298b7208c611e81321e0c82b721afd7vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
1400154bf298b7208c611e81321e0c82b721afd7vboxsync * VirtualBox OSE distribution, in which case the provisions of the
1400154bf298b7208c611e81321e0c82b721afd7vboxsync * CDDL are applicable instead of those of the GPL.
1400154bf298b7208c611e81321e0c82b721afd7vboxsync *
1400154bf298b7208c611e81321e0c82b721afd7vboxsync * You may elect to license modified versions of this file under the
1400154bf298b7208c611e81321e0c82b721afd7vboxsync * terms and conditions of either the GPL or the CDDL or both.
af66a95ff0d75da2d50719eb14a686a9de7bff76vboxsync */
af66a95ff0d75da2d50719eb14a686a9de7bff76vboxsync
af66a95ff0d75da2d50719eb14a686a9de7bff76vboxsync/*******************************************************************************
af66a95ff0d75da2d50719eb14a686a9de7bff76vboxsync* Header Files *
af66a95ff0d75da2d50719eb14a686a9de7bff76vboxsync*******************************************************************************/
af66a95ff0d75da2d50719eb14a686a9de7bff76vboxsync#include <iprt/cidr.h>
8d56e4d4be56ac2cbe2c03ce678432c6d8d1d7d3vboxsync#include <iprt/net.h> /* must come before getopt.h */
8d56e4d4be56ac2cbe2c03ce678432c6d8d1d7d3vboxsync#include "internal/iprt.h"
8d56e4d4be56ac2cbe2c03ce678432c6d8d1d7d3vboxsync
8d56e4d4be56ac2cbe2c03ce678432c6d8d1d7d3vboxsync#include <iprt/assert.h>
8d56e4d4be56ac2cbe2c03ce678432c6d8d1d7d3vboxsync#include <iprt/ctype.h>
8d56e4d4be56ac2cbe2c03ce678432c6d8d1d7d3vboxsync#include <iprt/err.h>
5a1e64cee1eec57da3361ccda90351bd9c2e97a6vboxsync#include <iprt/message.h>
5a1e64cee1eec57da3361ccda90351bd9c2e97a6vboxsync#include <iprt/string.h>
5a1e64cee1eec57da3361ccda90351bd9c2e97a6vboxsync
5a1e64cee1eec57da3361ccda90351bd9c2e97a6vboxsync
5a1e64cee1eec57da3361ccda90351bd9c2e97a6vboxsync/**
5a1e64cee1eec57da3361ccda90351bd9c2e97a6vboxsync * Converts an stringified Ethernet MAC address into the RTMAC representation.
fdb7d72fbf00de23e58088243b932f48684e83davboxsync *
fdb7d72fbf00de23e58088243b932f48684e83davboxsync * @returns VINF_SUCCESS on success.
fdb7d72fbf00de23e58088243b932f48684e83davboxsync *
fdb7d72fbf00de23e58088243b932f48684e83davboxsync * @param pszValue The value to convert.
fdb7d72fbf00de23e58088243b932f48684e83davboxsync * @param pAddr Where to store the result.
fdb7d72fbf00de23e58088243b932f48684e83davboxsync */
0a45dc6fc6692b57703e7e251f63b36476cc3435vboxsyncRTDECL(int) RTNetStrToMacAddr(const char *pszValue, PRTMAC pAddr)
0a45dc6fc6692b57703e7e251f63b36476cc3435vboxsync{
0a45dc6fc6692b57703e7e251f63b36476cc3435vboxsync /*
0a45dc6fc6692b57703e7e251f63b36476cc3435vboxsync * Not quite sure if I should accept stuff like "08::27:::1" here...
0a45dc6fc6692b57703e7e251f63b36476cc3435vboxsync * The code is accepting "::" patterns now, except for for the first
0a45dc6fc6692b57703e7e251f63b36476cc3435vboxsync * and last parts.
0a45dc6fc6692b57703e7e251f63b36476cc3435vboxsync */
0a45dc6fc6692b57703e7e251f63b36476cc3435vboxsync
0a45dc6fc6692b57703e7e251f63b36476cc3435vboxsync /* first */
0a45dc6fc6692b57703e7e251f63b36476cc3435vboxsync char *pszNext;
0a45dc6fc6692b57703e7e251f63b36476cc3435vboxsync int rc = RTStrToUInt8Ex(RTStrStripL(pszValue), &pszNext, 16, &pAddr->au8[0]);
0a45dc6fc6692b57703e7e251f63b36476cc3435vboxsync if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
e5ece1e2eda83c3ac47fc02dc4d343e430009381vboxsync return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
e5ece1e2eda83c3ac47fc02dc4d343e430009381vboxsync if (*pszNext++ != ':')
e5ece1e2eda83c3ac47fc02dc4d343e430009381vboxsync return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
e5ece1e2eda83c3ac47fc02dc4d343e430009381vboxsync
4148135d7797d294ed20b17713f3792aedb4426dvboxsync /* middle */
e5ece1e2eda83c3ac47fc02dc4d343e430009381vboxsync for (unsigned i = 1; i < 5; i++)
b9966c0aebbaadea7fafdb13bd9cc7dec1dde6e2vboxsync {
b9966c0aebbaadea7fafdb13bd9cc7dec1dde6e2vboxsync if (*pszNext == ':')
b9966c0aebbaadea7fafdb13bd9cc7dec1dde6e2vboxsync pAddr->au8[i] = 0;
b9966c0aebbaadea7fafdb13bd9cc7dec1dde6e2vboxsync else
b9966c0aebbaadea7fafdb13bd9cc7dec1dde6e2vboxsync {
b9966c0aebbaadea7fafdb13bd9cc7dec1dde6e2vboxsync rc = RTStrToUInt8Ex(pszNext, &pszNext, 16, &pAddr->au8[i]);
05b0c045e695a3e2dfe0e3f3ad0909f061f28272vboxsync if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
5db64e99b90067bcff9b0c080746edf83cf9dae6vboxsync return rc;
5db64e99b90067bcff9b0c080746edf83cf9dae6vboxsync if (*pszNext != ':')
5db64e99b90067bcff9b0c080746edf83cf9dae6vboxsync return VERR_INVALID_PARAMETER;
5db64e99b90067bcff9b0c080746edf83cf9dae6vboxsync }
5db64e99b90067bcff9b0c080746edf83cf9dae6vboxsync pszNext++;
fae3aa27fee8a48462bbdb535cc8674f66249f6bvboxsync }
fae3aa27fee8a48462bbdb535cc8674f66249f6bvboxsync
fae3aa27fee8a48462bbdb535cc8674f66249f6bvboxsync /* last */
fae3aa27fee8a48462bbdb535cc8674f66249f6bvboxsync rc = RTStrToUInt8Ex(pszNext, &pszNext, 16, &pAddr->au8[5]);
fae3aa27fee8a48462bbdb535cc8674f66249f6bvboxsync if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_SPACES)
fae3aa27fee8a48462bbdb535cc8674f66249f6bvboxsync return rc;
02ab40eecbb0eb6b6cf6101439e1b2c40ff169d2vboxsync pszNext = RTStrStripL(pszNext);
02ab40eecbb0eb6b6cf6101439e1b2c40ff169d2vboxsync if (*pszNext)
02ab40eecbb0eb6b6cf6101439e1b2c40ff169d2vboxsync return VERR_INVALID_PARAMETER;
02ab40eecbb0eb6b6cf6101439e1b2c40ff169d2vboxsync
02ab40eecbb0eb6b6cf6101439e1b2c40ff169d2vboxsync return VINF_SUCCESS;
02ab40eecbb0eb6b6cf6101439e1b2c40ff169d2vboxsync}
a4b0396828d3dfafb72e49a456c926b62bd19fb4vboxsyncRT_EXPORT_SYMBOL(RTNetStrToMacAddr);
a4b0396828d3dfafb72e49a456c926b62bd19fb4vboxsync