📄 rvaddress.c
字号:
}
RvChar *RvAddressIpv6ToString(RvChar *buf, RvSize_t bufsize, const RvUint8 *ip)
{
#if defined(RV_NULLCHECK)
if((buf == NULL) || (ip == NULL))
return NULL;
#endif
return Rv_inet_ntop6(ip, buf, bufsize);
}
RvBool RvAddressStringToIpv6(RvUint8 *ip, const RvChar *buf)
{
#if defined(RV_NULLCHECK)
if((ip == NULL) || (buf == NULL))
return RV_FALSE;
#endif
return Rv_inet_pton6(buf, ip);
}
const RvAddressIpv6 *RvAddressGetIpv6(const RvAddress *addr)
{
#if defined(RV_NULLCHECK)
if(addr == NULL)
return NULL;
#endif
#if defined(RV_OTHERCHECK)
if(addr->addrtype != RV_ADDRESS_TYPE_IPV6)
return NULL;
#endif
return &addr->data.ipv6;
}
RvAddressIpv6 *RvAddressIpv6Construct(RvAddressIpv6 *addr, const RvUint8 *ip, RvUint16 port)
{
#if defined(RV_NULLCHECK)
if((addr == NULL) || (ip == NULL))
return NULL;
#endif
memcpy(addr->ip, ip, RV_ADDRESS_IPV6_ADDRSIZE);
addr->port = port;
return addr;
}
RvBool RvAddressIpv6Compare(const RvAddressIpv6 *addr1, const RvAddressIpv6 *addr2, RvInt comparetype)
{
#if defined(RV_NULLCHECK)
if((addr1 == NULL) || (addr2 == NULL))
return RV_FALSE;
#endif
#if defined(RV_RANGECHECK)
if((comparetype != RV_ADDRESS_FULLADDRESS) && (comparetype != RV_ADDRESS_BASEADDRESS))
return RV_FALSE;
#endif
/* Check base IP address. */
if(memcmp(addr1->ip,addr2->ip, RV_ADDRESS_IPV6_ADDRSIZE) != 0)
return RV_FALSE;
/* Check full address, which means the port too. */
if((comparetype == RV_ADDRESS_FULLADDRESS) && (addr1->port != addr2->port))
return RV_FALSE;
return RV_TRUE;
}
RvChar *RvAddressIpv6GetString(const RvAddressIpv6 *addr, RvChar *buf, RvSize_t bufsize)
{
#if defined(RV_NULLCHECK)
if((addr == NULL) || (buf == NULL))
return RV_FALSE;
#endif
return Rv_inet_ntop6(addr->ip, buf, bufsize);
}
RvAddressIpv6 *RvAddressIpv6SetString(RvAddressIpv6 *addr, const RvChar *buf)
{
#if defined(RV_NULLCHECK)
if((addr == NULL) || (buf == NULL))
return NULL;
#endif
if(Rv_inet_pton6(buf, addr->ip) == RV_FALSE)
return NULL;
return addr;
}
const RvUint8 *RvAddressIpv6GetIp(const RvAddressIpv6 *addr)
{
#if defined(RV_NULLCHECK)
if(addr == NULL)
return NULL;
#endif
return addr->ip;
}
RvAddressIpv6 *RvAddressIpv6SetIp(RvAddressIpv6 *addr, const RvUint8 *ip)
{
#if defined(RV_NULLCHECK)
if(addr == NULL)
return NULL;
#endif
memcpy(addr->ip, ip, RV_ADDRESS_IPV6_ADDRSIZE);
return addr;
}
RvUint16 RvAddressIpv6GetPort(const RvAddressIpv6 *addr)
{
#if defined(RV_NULLCHECK)
if(addr == NULL)
return RvUint16Const(0);
#endif
return addr->port;
}
RvAddressIpv6 *RvAddressIpv6SetPort(RvAddressIpv6 *addr, RvUint16 port)
{
#if defined(RV_NULLCHECK)
if(addr == NULL)
return NULL;
#endif
addr->port = port;
return addr;
}
RvBool RvAddressIpv6IsMulticast(const RvAddressIpv6 *addr)
{
#if defined(RV_NULLCHECK)
if(addr == NULL)
return RV_FALSE;
#endif
if(addr->ip[0] == RvUint8Const(0xFF))
return RV_TRUE;
return RV_FALSE;
}
/*********************************************************************************/
/* The software in this section is from the Internet Software Consortium */
/* with only minor changes: */
/*
* Copyright (c) 1996-1999 by Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
/* const char *
* inet_ntop4(src, dst, size)
* format an IPv4 address
* return:
* `dst' (as a const)
* notes:
* (1) uses no statics
* (2) takes a u_char* not an in_addr as input
* author:
* Paul Vixie, 1996.
*/
static RvChar *Rv_inet_ntop4(const RvUint8 *src, RvChar *dst, RvSize_t size)
{
RvChar tmp[sizeof "255.255.255.255"];
RvSprintf(tmp, "%u.%u.%u.%u", src[0], src[1], src[2], src[3]);
if (strlen(tmp) >= size)
return (NULL);
strcpy(dst, tmp);
return (dst);
}
/* const char *
* inet_ntop6(src, dst, size)
* convert IPv6 binary address into presentation (printable) format
* author:
* Paul Vixie, 1996.
*/
static RvChar *Rv_inet_ntop6(const RvUint8 *src, RvChar *dst, RvSize_t size)
{
/*
* Note that int32_t and int16_t need only be "at least" large enough
* to contain a value of the specified size. On some systems, like
* Crays, there is no such thing as an integer variable with 16 bits.
* Keep this in mind if you think this function should have been coded
* to use pointer overlays. All the world's not a VAX.
*/
RvChar tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
struct { RvInt base, len; } best, cur;
RvUint words[RV_ADDRESS_IPV6_ADDRSIZE / 2];
RvInt i;
/*
* Preprocess:
* Copy the input (bytewise) array into a wordwise array.
* Find the longest run of 0x00's in src[] for :: shorthanding.
*/
memset(words, '\0', sizeof words);
for (i = 0; i < RV_ADDRESS_IPV6_ADDRSIZE; i++)
words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
best.base = -1;
best.len = 0;
cur.base = -1;
cur.len = 0;
for (i = 0; i < (RV_ADDRESS_IPV6_ADDRSIZE / 2); i++) {
if (words[i] == 0) {
if (cur.base == -1)
cur.base = i, cur.len = 1;
else
cur.len++;
} else {
if (cur.base != -1) {
if (best.base == -1 || cur.len > best.len)
best = cur;
cur.base = -1;
}
}
}
if (cur.base != -1) {
if (best.base == -1 || cur.len > best.len)
best = cur;
}
if (best.base != -1 && best.len < 2)
best.base = -1;
/*
* Format the result.
*/
tp = tmp;
for (i = 0; i < (RV_ADDRESS_IPV6_ADDRSIZE / 2); i++) {
/* Are we inside the best run of 0x00's? */
if (best.base != -1 && i >= best.base &&
i < (best.base + best.len)) {
if (i == best.base)
*tp++ = ':';
continue;
}
/* Are we following an initial run of 0x00s or any real hex? */
if (i != 0)
*tp++ = ':';
/* Is this address an encapsulated IPv4? */
if (i == 6 && best.base == 0 &&
(best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
if (Rv_inet_ntop4(src+12, tp, (RvSize_t)(sizeof tmp - (tp - tmp))) == RV_FALSE)
return (NULL);
tp += strlen(tp);
break;
}
RvSprintf(tp, "%x", words[i]);
tp += strlen(tp);
}
/* Was it a trailing run of 0x00's? */
if (best.base != -1 && (best.base + best.len) ==
(RV_ADDRESS_IPV6_ADDRSIZE / 2))
*tp++ = ':';
*tp++ = '\0';
/*
* Check for overflow, copy, and we're done.
*/
if ((size_t)(tp - tmp) > size)
return (NULL);
strcpy(dst, tmp);
return (dst);
}
/* int
* inet_pton4(src, dst)
* like inet_aton() but without all the hexadecimal and shorthand.
* return:
* 1 if `src' is a valid dotted quad, else 0.
* notice:
* does not touch `dst' unless it's returning 1.
* author:
* Paul Vixie, 1996.
*/
static RvBool Rv_inet_pton4(const RvChar *src, RvUint8 *dst)
{
static RvChar digits[] = "0123456789";
RvInt saw_digit, octets, ch;
RvUint8 tmp[RV_ADDRESS_IPV4_ADDRSIZE], *tp;
saw_digit = 0;
octets = 0;
*(tp = tmp) = 0;
while ((ch = *src++) != '\0') {
RvChar *pch;
if ((pch = strchr(digits, ch)) != NULL) {
RvUint new = *tp * 10 + (pch - digits);
if (new > 255)
return RV_FALSE;
*tp = (RvUint8)new;
if (! saw_digit) {
if (++octets > 4)
return RV_FALSE;
saw_digit = 1;
}
} else if (ch == '.' && saw_digit) {
if (octets == 4)
return RV_FALSE;
*++tp = 0;
saw_digit = 0;
} else
return RV_FALSE;
}
if (octets < 4)
return RV_FALSE;
memcpy(dst, tmp, RV_ADDRESS_IPV4_ADDRSIZE);
return RV_TRUE;
}
/* int
* inet_pton6(src, dst)
* convert presentation level address to network order binary form.
* return:
* 1 if `src' is a valid [RFC1884 2.2] address, else 0.
* notice:
* (1) does not touch `dst' unless it's returning 1.
* (2) :: in a full address is silently ignored.
* credit:
* inspired by Mark Andrews.
* author:
* Paul Vixie, 1996.
*/
static RvBool Rv_inet_pton6(const RvChar *src, RvUint8 *dst)
{
static RvChar xdigits_l[] = "0123456789abcdef",
xdigits_u[] = "0123456789ABCDEF";
RvUint8 tmp[RV_ADDRESS_IPV6_ADDRSIZE], *tp, *endp, *colonp;
RvChar *xdigits;
const RvChar *curtok;
RvInt ch, saw_xdigit;
RvUint val;
memset((tp = tmp), '\0', RV_ADDRESS_IPV6_ADDRSIZE);
endp = tp + RV_ADDRESS_IPV6_ADDRSIZE;
colonp = NULL;
/* Leading :: requires some special handling. */
if (*src == ':')
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -