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