cidr.cpp revision ba76bdc1f958589049083de0d315acab0249c581
/* $Id$ */
/** @file
* IPRT - IPv4 address parsing.
*/
/*
* Copyright (C) 2006-2008 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
/**
* Scan a digit of an IPv4 address.
*
* @returns IPRT status code.
*
* @param iDigit The index of the IPv4 digit to scan.
* @param psz Pointer to the begin of the string.
* @param ppszNext Pointer to variable that should be set pointing to the first invalid character. (output)
* @param pu8 Pointer to the digit to write (output).
*/
{
if (( rc != VINF_SUCCESS
&& rc != VWRN_TRAILING_CHARS)
|| *pu8 > 254)
return VERR_INVALID_PARAMETER;
/* first digit cannot be 0 */
if ( iDigit == 1
&& *pu8 < 1)
return VERR_INVALID_PARAMETER;
if (**ppszNext == '/')
return VINF_SUCCESS;
if ( iDigit != 4
&& ( **ppszNext == '\0'
|| **ppszNext != '.'))
return VERR_INVALID_PARAMETER;
return VINF_SUCCESS;
}
{
uint8_t a;
uint8_t b = 0;
uint8_t c = 0;
uint8_t d = 0;
const char *psz = pszAddress;
char *pszNext;
int rc;
do
{
/* 1st digit */
if (RT_FAILURE(rc))
return rc;
if (*pszNext == '/')
break;
/* 2nd digit */
if (RT_FAILURE(rc))
return rc;
if (*pszNext == '/')
break;
/* 3rd digit */
if (RT_FAILURE(rc))
return rc;
if (*pszNext == '/')
break;
/* 4th digit */
if (RT_FAILURE(rc))
return rc;
} while (0);
if (*pszNext == '/')
{
return VERR_INVALID_PARAMETER;
}
else
cBits = 0;
/* nothing */;
if (*psz != '\0')
return VERR_INVALID_PARAMETER;
*pNetwork = RT_MAKE_U32_FROM_U8(d, c, b, a);
return VINF_SUCCESS;
}