netaddrstr.cpp revision 9fd89a2b9c6b3e970bd97f550c10960bdbb52933
/* $Id$ */
/** @file
* IPRT - Network Address String Handling.
*/
/*
* Contributed by Oliver Loch.
*
* Copyright (C) 2012 Oracle Corporation
*
* 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
/** @page pg_rtnetipv6_addr IPv6 Address Format
*
* IPv6 Addresses, their representation in text and other problems.
*
* The following is based on:
*
*
*
* Before you start using those functions, you should have an idea of
* what you're dealing with, before you come and blame the functions...
*
* First of all, the address itself:
*
* An address is written like this: (READ THIS FINE MANUAL!)
*
* - 2001:db8:abc:def::1
*
* The characters between two colons are called a "hextet".
* Each hextet consists of four characters and each IPv6 address
* consists of a maximum of eight hextets. So a full blown address
* would look like this:
*
* - 1111:2222:3333:4444:5555:6666:7777:8888
*
* The allowed characters are "0123456789abcdef". They have to be
* lower case. Upper case is not allowed.
*
* *** Gaps and adress shortening
*
* If an address contains hextets that contain only "0"s, they
* can be shortened, like this:
*
* - 1111:2222:0000:0000:0000:0000:7777:8888 -> 1111:2222::7777:8888
*
* The double colon represents the hextets that have been shortened "::".
* The "::" will be called "gap" from now on.
*
* When shortening an address, there are some special rules that need to be applied:
*
* - Shorten always the longest group of hextets.
*
* Let's say, you have this address: 2001:db8:0:0:0:1:0:0 then it has to be
* shortened to "2001:db8::1:0:0". Shortening to "2001:db8:0:0:0:1::" would
* return an error.
*
* - Two or more gaps the same size.
*
* Let's say you have this address: 2001:db8:0:0:1:0:0:1. As you can see, there
* are two gaps, both the size of two hextets. If you shorten the last two hextets,
* you end up in pain, as the RFC forbids this, so the correct address is:
* "2001:db8::1:0:0:1"
*
* It's important to note that an address can only be shortened ONE TIME!
* This is invalid: "2001:db8::1::1"
*
* *** The scope.
*
* Each address has a so called "scope" it is added to the end of the address,
* separated by a percent sign "%". If there is no scope at the end, it defaults
* to "0".
*
* So "2001:db8::1" is the same as "2001:db8::1%0".
*
* gives you the ability to choose on which interface the system should listen.
*
* AFAIK, the scope can be used with unicast as well as link local addresses, but
* it is mandatory with link local addresses (starting with fe80::).
*
* On Linux the default scope is the interface's name. On Windows it's just the index
* of the interface. Run "route print -6" in the shell, to see the interface's index
* on Winodows.
*
* All functions can deal with the scope, and DO NOT warn if you put garbage there.
*
* *** Port added to the IPv6 address
*
* There is only one way to add a port to an IPv6 address is to embed it in brackets:
*
* [2001:db8::1]:12345
*
* This gives you the address "2001:db8::1" and the port "12345".
*
* What also works, but is not recommended by rfc is to separate the port
* by a dot:
*
* 2001:db8::1.12345
*
* It even works with embedded IPv4 addresses.
*
* *** Special addresses and how they are written
*
* The following are notations to represent "special addresses".
*
* "::" IN6ADDR_ANY
* ":::123" IN6ADDR_ANY with port "123"
* "[::]:123" IN6ADDR_ANY with port "123"
* "[:::123]" -> NO. Not allowed and makes no sense
* "::1" -> address of the loopback device (127.0.0.1 in v4)
*
* On systems with dual sockets, one can use so called embedded IPv4 addresses:
*
* "::ffff:192.168.1.1" results in the IPv6 address "::ffff:c0a8:0101" as two octets
* of the IPv4 address will be converted to one hextet in the IPv6 address.
*
* The prefix of such addresses MUST BE "::ffff:", 10 bytes as zero and two bytes as 255.
*
* The so called IPv4-compatible IPv6 addresses are deprecated and no longer in use.
*
* *** Valid addresses and string
*
* If you use any of the IPv6 address functions, keep in mind, that those addresses
* are all returning "valid" even if the underlying system (e.g. VNC) doesn't like
* such strings.
*
* [2001:db8::1]
* [2001:db8::1]:12345
*
* and so on. So to make sure you only pass the underlying software a pure IPv6 address
* without any garbage, you should use the "outAddress" parameters to get a RFC compliant
* address returned.
*
* So after reading the above, you'll start using the functions and see a bool called
* "followRfc" which is true by default. This is what this bool does:
*
* The following addresses all represent the exact same address:
*
* 1 - 2001:db8::1
* 2 - 2001:db8:0::1
* 3 - 2001:0db8:0000:0000:0000:0000:0000:0001
* 4 - 2001:DB8::1
* 5 - [2001:db8::1]
* 6 - [2001:db8:0::1]
*
* According to RFC 5952, number two, three, four and six are invalid.
*
* #2 - because there is a single hextet that hasn't been shortened
*
* #3 - because there has nothing been shortened (hextets 3 to 7) and
* there are leading zeros in at least one hextet ("0db8")
*
* #4 - all characters in an IPv6 address have to be lower case
*
* #6 - same as two but included in brackets
*
* If you follow RFC, the above addresses are not converted and an
* error is returned. If you turn RFC off, you will get the expected
* representation of the address.
*
* It's a nice way to convert "weird" addresses to rfc compliant addresses
*
*/
/**
* Parses any string and tests if it is an IPv6 Address
*
* This function should NOT be used directly. If you do, note
* that no security checks are done at the moment. This can change.
*
* @returns iprt sstatus code.
* @param pszAddress The strin that holds the IPv6 address
* @param addressLength The length of pszAddress
* @param pszAddressOut Returns a plain, full blown IPv6 address
* as a char array
* @param addressOutSize The size of pszAddressOut (length)
* @param pPortOut 32 bit unsigned integer, holding the port
* If pszAddress doesn't contain a port, it's 0
* @param pszScopeOut Returns the scope of the address, if none it's 0
* @param scopeOutSize sizeof(pszScopeOut)
* @param pBrackets returns true if the address was enclosed in brackets
* @param pEmbeddedV4 returns true if the address is an embedded IPv4 address
* @param followRfc if set to true, the function follows RFC (default)
*/
static int rtStrParseAddrStr6(const char *pszAddress, size_t addressLength, char *pszAddressOut, size_t addressOutSize, uint32_t *pPortOut, char *pszIfIdOut, size_t ifIdOutSize, bool *pBrackets, bool *pEmbeddedV4, bool followRfc)
{
/************************\
* Pointer Hell Ahead *
\************************/
char *pStart = NULL, *pNow = NULL, *pNext = NULL, *pNowChar = NULL, *pIfId = NULL, *pIfIdEnd = NULL;
bool isLinkLocal = false;
char szDummy[4];
uint16_t returnValue = 0;
uint32_t colonsOverAll = 0;
uint32_t fieldLength = 0;
uint32_t intPortOut = 0;
if (!followRfc)
if (addressLength<2)
returnValue = 711;
{
if (pszInternalPortStart)
return -701;
}
{
if (!pFrom)
if (pszResultPort)
{
{
if (*pLast == '\0')
returnValue = 721;
if (!pszResultPortStart)
returnValue = 702;
continue;
}
returnValue = 11;
{
continue;
}
if (pNowDigit)
{
*pszResultPort = *pNowDigit;
continue;
}
else
returnValue = 12;
}
if (pszResultAddress4)
{
{
dots = 0;
if (!pszResultAddress4Start)
{
returnValue = 401;
break;
}
continue;
}
returnValue = 412;
{
returnValue = 402;
break;
}
{
continue;
}
returnValue = 411;
returnValue = 403;
pTo--;
dots++;
{
pMisc--;
pTo--;
}
{
if (!pBracketOpen)
{
}
else
{
returnValue = 409;
}
}
dots = 0;
{
}
if (*pNow == '%')
{
continue;
}
if (*pNow != ']')
continue;
}
{
{
continue;
}
{
returnValue = 442;
break;
}
{
continue;
}
if (*pNow == ']')
{
continue;
}
else
{
continue;
}
}
if (!pNowChar)
{
returnValue = 254;
if (followRfc)
{
if (pMisc)
returnValue = 253;
}
}
returnValue = 255;
{
continue;
}
{
colons++;
continue;
}
if (*pNow == ':' )
{
colons++;
}
if (*pNow == '.')
{
{
if (*pMisc == '.')
dots++;
pMisc++;
}
}
if (*pNow == ']')
{
if (pBracketClose)
returnValue = 77;
if (!pBracketOpen)
returnValue = 22;
{
}
}
if (*pNow == '[')
{
if (pBracketOpen)
returnValue = 23;
returnValue = 24;
pBracketOpen = pNow;
pStart++;
pFrom++;
continue;
}
if (*pNow == '%')
{
if (pIfId)
returnValue = 441;
}
if (colons > 0)
{
if (colons == 1)
{
returnValue = 31;
returnValue = 32;
}
if (colons == 2)
{
if (pGap)
returnValue = 33;
{
}
}
if (colons == 3)
{
returnValue = 34;
if (pBracketOpen)
returnValue = 35;
returnValue = 36;
if (!pGap)
{
}
if (pNowDigit)
{
}
}
}
{
if (pNowDigit)
}
if (dots > 0)
{
if (dots == 1)
{
}
returnValue = 601;
{
}
if (dots > 4)
returnValue = 603;
dots = 0;
}
{
{
returnValue = 51;
break;
}
if (followRfc)
{
returnValue = 101;
returnValue = 102;
returnValue = 103;
{
if (!pFieldStart)
{
}
else
{
}
}
else
{
{
}
pFieldStart = NULL;
}
}
{
{
isLinkLocal = true;
{
isLinkLocal = false;
pTo--;
pMisc--;
}
}
else
{
{
pMisc--;
pTo--;
}
}
}
}
colons = 0;
} // end of loop
returnValue = 252;
returnValue = 25;
if (!returnValue && pGap)
{
if (followRfc)
{
if (gapSize < 5)
returnValue = 104;
if (fieldLength > gapSize)
returnValue = 105;
returnValue = 106;
}
if (!returnValue && pszResultAddress4Start)
{
returnValue = 405;
if (pGap != pszResultAddressStart)
returnValue = 407;
for (int i = 0; i < 4; i++)
{
{
returnValue = 406;
break;
}
}
for (int i = 0; i<4; i++)
{
{
}
else
{
returnValue = 499;
}
}
}
else
{
{
}
{
}
}
}
else
{
if (!returnValue)
{
returnValue = 111;
if (followRfc)
{
if (fieldLength > 4)
returnValue = 112;
}
}
}
if (pszResultPortStart)
{
{
intPortOut = 0;
if (rc == 0)
{
intPortOut = 0;
}
else
{
returnValue = 888;
}
}
else
{
returnValue = 889;
}
}
/*
full blown address 32 bytes, no colons -> pszInternalAddressStart
port as string -> pszResultPortStart
port as binary integer -> intPortOut
interface id in pIfId and pIfIdEnd
Now fill the out parameters.
*/
if (!returnValue && pszAddressOut)
{
{
if (!pszRc)
returnValue = 910;
if (!pszRc)
returnValue = 911;
}
else
{
returnValue = 912;
}
}
if (!returnValue && pPortOut)
{
*pPortOut = intPortOut;
}
if (!returnValue && pszIfIdOut)
{
{
{
if (!pszRc)
returnValue = 913;
if (!pszRc)
returnValue = 914;
}
else
{
returnValue = 915;
}
}
else
{
if (!pszRc)
returnValue = 916;
}
// temporary hack
{
*pszIfIdOut = '%';
pszIfIdOut++;
*pszIfIdOut = '0';
pszIfIdOut++;
}
}
*pBrackets = true;
if (pEmbeddedV4 && pszResultAddress4Start)
*pEmbeddedV4 = true;
if (pszResultPortStart)
if (pszInternalPortStart)
}
/**
* Takes a string and returns a RFC compliant string of the address
* This function SHOULD NOT be used directly. It expects a 33 byte
* char array with a full blown IPv6 address without separators.
*
* @returns iprt status code.
* @param psz The string to convert
* @param pszAddrOut The char[] that will hold the result
* @param addOutSize The size of the char[] from above.
* @param pszPortOut char[] for the text representation of the port
* @param portOutSize sizeof(pszPortOut);
*/
DECLHIDDEN(int) rtStrToIpAddr6Str(const char *psz, char *pszAddrOut, size_t addrOutSize, char *pszPortOut, size_t portOutSize, bool followRfc)
{
char *pGapTStart = NULL;
if (!psz || !pszAddrOut)
return VERR_NOT_SUPPORTED;
if (addrOutSize < 40)
return VERR_NOT_SUPPORTED;
while (*pCurrent != '\0')
{
if (*pCurrent != '0')
pGapTStart = NULL;
{
}
{
if (pGapTStart && pGapTEnd)
{
{
}
}
}
pCurrent++;
}
pOut = (char *)pszAddrOut;
while (*pCurrent != '\0')
{
if (*pCurrent != '0')
pGapTStart = NULL;
if (!pGapTStart)
{
pOut++;
}
{
{
pOut++;
}
{
*pOut = ':';
pOut++;
}
}
{
*pOut = ':';
pOut++;
}
pCurrent++;
}
return VINF_SUCCESS;
}
/**
* Tests if the given string is a valid IPv6 address.
*
* @returns 0 if valid, some random number if not. THIS IS NOT AN IPRT STATUS!
* @param psz The string to test
* @param pszResultAddress plain address, optional read "valid addresses
* and strings" above.
* @param resultAddressSize size of pszResultAddress
* @param addressOnly return only the plain address (no scope)
* Ignored, and will always return the if id
*/
static int rtNetIpv6CheckAddrStr(const char *psz, char *pszResultAddress, size_t resultAddressSize, bool addressOnly, bool followRfc)
{
int rc;
int rc2;
int returnValue;
return VERR_NO_TMP_MEMORY;
rc = rtStrParseAddrStr6(psz, strlen(psz), pszAddressOutLocal, memAllocMaxSize, NULL, pszIfIdOutLocal, memAllocMaxSize, NULL, NULL, followRfc);
if (rc == 0)
if (rc == 0 && pszResultAddress)
{
// convert the 32 characters to a valid, shortened ipv6 address
rc2 = rtStrToIpAddr6Str((const char *)pszAddressOutLocal, pszAddressRfcOutLocal, memAllocMaxSize, NULL, 0, followRfc);
if (rc2 != 0)
returnValue = 951;
// this is a temporary solution
if (!returnValue && strlen(pszIfIdOutLocal) > 0) // the if identifier is copied over _ALWAYS_ && !addressOnly)
{
*p = '%';
p++;
if (!pl)
}
if (!pl)
}
if (rc != 0)
if (pszAddressOutLocal)
if (pszIfIdOutLocal)
return returnValue;
}
{
}
{
static char const s_szIpV4Digits[] = "0123456789.";
return false;
unsigned cOctets = 0;
while (*pNow != '\0')
{
if (!pChar)
return false;
{
pNow++;
continue;
}
{
if (*pNext == '\0')
if (cchSub > 2)
return false;
char szDummy[4] = { 0, 0, 0, 0 };
if (rc != VINF_SUCCESS)
return false;
cOctets++;
if (cOctets > 4)
return false;
}
pNow++;
}
if (cOctets != 4)
return false;
return true;
}