netaddrstr2.cpp revision 64eea8161bef2aa3c6516481383c830bca27abfe
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync/* $Id$ */
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync/** @file
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync * IPRT - Network Address String Handling.
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync */
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync/*
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync * Copyright (C) 2013 Oracle Corporation
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync *
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync * available from http://www.virtualbox.org. This file is free software;
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync * you can redistribute it and/or modify it under the terms of the GNU
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync * General Public License (GPL) as published by the Free Software
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync *
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync * The contents of this file may alternatively be used under the terms
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync * of the Common Development and Distribution License Version 1.0
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync * VirtualBox OSE distribution, in which case the provisions of the
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync * CDDL are applicable instead of those of the GPL.
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync *
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync * You may elect to license modified versions of this file under the
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync * terms and conditions of either the GPL or the CDDL or both.
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync */
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync/*******************************************************************************
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync* Header Files *
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync*******************************************************************************/
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync#include "internal/iprt.h"
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync#include <iprt/net.h>
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync#include <iprt/mem.h>
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync#include <iprt/string.h>
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync#include <iprt/stream.h>
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync#include "internal/string.h"
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync
deb4998ba50060c48cce222fd18a8eed053918d7vboxsyncRTDECL(int) RTNetStrToIPv4Addr(const char *pszAddr, PRTNETADDRIPV4 pAddr)
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync{
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync char *pszNext;
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync AssertPtrReturn(pszAddr, VERR_INVALID_PARAMETER);
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync AssertPtrReturn(pAddr, VERR_INVALID_PARAMETER);
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync int rc = RTStrToUInt8Ex(RTStrStripL(pszAddr), &pszNext, 10, &pAddr->au8[0]);
if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
return VERR_INVALID_PARAMETER;
if (*pszNext++ != '.')
return VERR_INVALID_PARAMETER;
rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &pAddr->au8[1]);
if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
return VERR_INVALID_PARAMETER;
if (*pszNext++ != '.')
return VERR_INVALID_PARAMETER;
rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &pAddr->au8[2]);
if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
return VERR_INVALID_PARAMETER;
if (*pszNext++ != '.')
return VERR_INVALID_PARAMETER;
rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &pAddr->au8[3]);
if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_SPACES)
return VERR_INVALID_PARAMETER;
pszNext = RTStrStripL(pszNext);
if (*pszNext)
return VERR_INVALID_PARAMETER;
return VINF_SUCCESS;
}
RT_EXPORT_SYMBOL(RTNetStrToIPv4Addr);