Rework uiplib_ipaddr_snprint, and use it from logging module and shell

This commit is contained in:
Simon Duquennoy 2018-05-15 12:04:39 -07:00
parent 4581785134
commit 29061e9885
4 changed files with 51 additions and 136 deletions

View File

@ -3,17 +3,17 @@
* Computer Science. * Computer Science.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
* are met: * are met:
* 1. Redistributions of source code must retain the above copyright * 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote * 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior * products derived from this software without specific prior
* written permission. * written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
@ -25,7 +25,7 @@
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
* This file is part of the uIP TCP/IP stack and the Contiki operating system. * This file is part of the uIP TCP/IP stack and the Contiki operating system.
* *
@ -117,7 +117,7 @@ uiplib_ip6addrconv(const char *addrstr, uip_ip6addr_t *ipaddr)
} }
#endif /* NETSTACK_CONF_WITH_IPV6 */ #endif /* NETSTACK_CONF_WITH_IPV6 */
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/* Parse a IPv4-address from a string. Returns the number of characters read /* Parse a IPv4-address from a string. Returns the number of characters read
* for the address. */ * for the address. */
int int
uiplib_ip4addrconv(const char *addrstr, uip_ip4addr_t *ipaddr) uiplib_ip4addrconv(const char *addrstr, uip_ip4addr_t *ipaddr)
@ -156,48 +156,9 @@ uiplib_ip4addrconv(const char *addrstr, uip_ip4addr_t *ipaddr)
void void
uiplib_ipaddr_print(const uip_ipaddr_t *addr) uiplib_ipaddr_print(const uip_ipaddr_t *addr)
{ {
uint16_t a; char buf[40];
unsigned int i; uiplib_ipaddr_snprint(buf, sizeof(buf), addr);
int f; printf("%s", buf);
if(addr == NULL) {
printf("(NULL IP addr)");
return;
}
if(ip64_addr_is_ipv4_mapped_addr(addr)) {
/*
* Printing IPv4-mapped addresses is done according to RFC 4291 [1]
*
* "An alternative form that is sometimes more
* convenient when dealing with a mixed environment
* of IPv4 and IPv6 nodes is x:x:x:x:x:x:d.d.d.d,
* where the 'x's are the hexadecimal values of the
* six high-order 16-bit pieces of the address, and
* the 'd's are the decimal values of the four
* low-order 8-bit pieces of the address (standard
* IPv4 representation)."
*
* [1] https://tools.ietf.org/html/rfc4291#page-4
*/
printf("::FFFF:%u.%u.%u.%u", addr->u8[12], addr->u8[13], addr->u8[14], addr->u8[15]);
} else {
for(i = 0, f = 0; i < sizeof(uip_ipaddr_t); i += 2) {
a = (addr->u8[i] << 8) + addr->u8[i + 1];
if(a == 0 && f >= 0) {
if(f++ == 0) {
printf("::");
}
} else {
if(f > 0) {
f = -1;
} else if(i > 0) {
printf(":");
}
printf("%x", a);
}
}
}
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
int int
@ -205,15 +166,16 @@ uiplib_ipaddr_snprint(char *buf, size_t size, const uip_ipaddr_t *addr)
{ {
uint16_t a; uint16_t a;
unsigned int i; unsigned int i;
int f, n; int f;
int n = 0;
if(size == 0) { if(size == 0) {
return 0; return 0;
} }
if(addr == NULL) { if(addr == NULL) {
n = snprintf(buf, size - 1, "(NULL IP addr)"); n = snprintf(buf, size, "(NULL IP addr)");
return n;
} else if(ip64_addr_is_ipv4_mapped_addr(addr)) { } else if(ip64_addr_is_ipv4_mapped_addr(addr)) {
/* /*
* Printing IPv4-mapped addresses is done according to RFC 4291 [1] * Printing IPv4-mapped addresses is done according to RFC 4291 [1]
@ -229,32 +191,35 @@ uiplib_ipaddr_snprint(char *buf, size_t size, const uip_ipaddr_t *addr)
* *
* [1] https://tools.ietf.org/html/rfc4291#page-4 * [1] https://tools.ietf.org/html/rfc4291#page-4
*/ */
n = snprintf(buf, size - 1, "::FFFF:%u.%u.%u.%u", addr->u8[12], n = snprintf(buf, size, "::FFFF:%u.%u.%u.%u", addr->u8[12],
addr->u8[13], addr->u8[14], addr->u8[15]); addr->u8[13], addr->u8[14], addr->u8[15]);
return n;
} else { } else {
for(n = 0, i = 0, f = 0; i < sizeof(uip_ipaddr_t) && n < size - 1; i += 2) { for(i = 0, f = 0; i < sizeof(uip_ipaddr_t); i += 2) {
a = (addr->u8[i] << 8) + addr->u8[i + 1]; a = (addr->u8[i] << 8) + addr->u8[i + 1];
if(a == 0 && f >= 0) { if(a == 0 && f >= 0) {
if(f++ == 0) { if(f++ == 0) {
buf[n++] = ':'; n += snprintf(buf+n, size-n, "::");
buf[n++] = ':'; if(n >= size) {
return n;
}
} }
} else { } else {
if(f > 0) { if(f > 0) {
f = -1; f = -1;
} else if(i > 0) { } else if(i > 0) {
buf[n++] = ':'; n += snprintf(buf+n, size-n, ":");
if(n >= size) {
return n;
}
}
n += snprintf(buf+n, size-n, "%x", a);
if(n >= size) {
return n;
} }
n += snprintf(&buf[n], size - n - 1, "%x", a);
} }
} }
} }
/*
* Make sure the output string is always null-terminated.
*/
buf[MIN(n, size - 1)] = '\0';
return n; return n;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/

View File

@ -1,19 +1,19 @@
/* /*
* Copyright (c) 2002, Adam Dunkels. * Copyright (c) 2002, Adam Dunkels.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
* are met: * are met:
* 1. Redistributions of source code must retain the above copyright * 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above * 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following * copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided * disclaimer in the documentation and/or other materials provided
* with the distribution. * with the distribution.
* 3. The name of the author may not be used to endorse or promote * 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior * products derived from this software without specific prior
* written permission. * written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
@ -25,7 +25,7 @@
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
* This file is part of the Contiki desktop environment for the C64. * This file is part of the Contiki desktop environment for the C64.
* *
@ -65,7 +65,7 @@
* the numerical representation of the address. * the numerical representation of the address.
* *
* \retval 0 If the IP address could not be parsed. * \retval 0 If the IP address could not be parsed.
* \retval Non-zero If the IP address was parsed. * \retval Non-zero If the IP address was parsed.
*/ */
#if NETSTACK_CONF_WITH_IPV6 #if NETSTACK_CONF_WITH_IPV6
#define uiplib_ipaddrconv uiplib_ip6addrconv #define uiplib_ipaddrconv uiplib_ip6addrconv

View File

@ -50,40 +50,15 @@
#include "shell-commands.h" #include "shell-commands.h"
#include "net/ipv6/uip.h" #include "net/ipv6/uip.h"
#include "net/ipv6/ip64-addr.h" #include "net/ipv6/ip64-addr.h"
#include "net/ipv6/uiplib.h"
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void void
shell_output_6addr(shell_output_func output, const uip_ipaddr_t *ipaddr) shell_output_6addr(shell_output_func output, const uip_ipaddr_t *ipaddr)
{ {
uint16_t a; char buf[40];
unsigned int i; uiplib_ipaddr_snprint(buf, sizeof(buf), ipaddr);
int f; SHELL_OUTPUT(output, "%s", buf);
if(ipaddr == NULL) {
SHELL_OUTPUT(output, "(NULL IP addr)");
return;
}
if(ip64_addr_is_ipv4_mapped_addr(ipaddr)) {
/* Printing IPv4-mapped addresses is done according to RFC 4291 */
SHELL_OUTPUT(output, "::FFFF:%u.%u.%u.%u", ipaddr->u8[12], ipaddr->u8[13], ipaddr->u8[14], ipaddr->u8[15]);
} else {
for(i = 0, f = 0; i < sizeof(uip_ipaddr_t); i += 2) {
a = (ipaddr->u8[i] << 8) + ipaddr->u8[i + 1];
if(a == 0 && f >= 0) {
if(f++ == 0) {
SHELL_OUTPUT(output, "::");
}
} else {
if(f > 0) {
f = -1;
} else if(i > 0) {
SHELL_OUTPUT(output, ":");
}
SHELL_OUTPUT(output, "%x", a);
}
}
}
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void void

View File

@ -50,6 +50,7 @@
#include "sys/log.h" #include "sys/log.h"
#include "net/ipv6/ip64-addr.h" #include "net/ipv6/ip64-addr.h"
#include "net/ipv6/uiplib.h"
int curr_log_level_rpl = LOG_CONF_LEVEL_RPL; int curr_log_level_rpl = LOG_CONF_LEVEL_RPL;
int curr_log_level_tcpip = LOG_CONF_LEVEL_TCPIP; int curr_log_level_tcpip = LOG_CONF_LEVEL_TCPIP;
@ -84,35 +85,9 @@ struct log_module all_modules[] = {
void void
log_6addr(const uip_ipaddr_t *ipaddr) log_6addr(const uip_ipaddr_t *ipaddr)
{ {
uint16_t a; char buf[40];
unsigned int i; uiplib_ipaddr_snprint(buf, sizeof(buf), ipaddr);
int f; LOG_OUTPUT("%s", buf);
if(ipaddr == NULL) {
LOG_OUTPUT("(NULL IP addr)");
return;
}
if(ip64_addr_is_ipv4_mapped_addr(ipaddr)) {
/* Printing IPv4-mapped addresses is done according to RFC 4291 */
LOG_OUTPUT("::FFFF:%u.%u.%u.%u", ipaddr->u8[12], ipaddr->u8[13], ipaddr->u8[14], ipaddr->u8[15]);
} else {
for(i = 0, f = 0; i < sizeof(uip_ipaddr_t); i += 2) {
a = (ipaddr->u8[i] << 8) + ipaddr->u8[i + 1];
if(a == 0 && f >= 0) {
if(f++ == 0) {
LOG_OUTPUT("::");
}
} else {
if(f > 0) {
f = -1;
} else if(i > 0) {
LOG_OUTPUT(":");
}
LOG_OUTPUT("%x", a);
}
}
}
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void void