Rework uiplib_ipaddr_snprint, and use it from logging module and shell
This commit is contained in:
parent
4581785134
commit
29061e9885
|
@ -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;
|
||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
|
|
|
@ -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
|
||||||
|
|
33
os/sys/log.c
33
os/sys/log.c
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue