net: Add debug function to print IP address
Change-Id: I4464f1da6d602b1abba0f385e5afa92d2cb4afc8 Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
This commit is contained in:
parent
fa7ed053bf
commit
a6d9255647
1 changed files with 223 additions and 0 deletions
|
@ -86,9 +86,232 @@ static inline char *net_sprint_ll_addr(uint8_t *ll, uint8_t ll_len)
|
|||
|
||||
return net_sprint_ll_addr_buf(ll, ll_len, (char *)buf, sizeof(buf));
|
||||
}
|
||||
|
||||
static int net_value_to_udec(char *buf, uint32_t value, int precision)
|
||||
{
|
||||
uint32_t divisor;
|
||||
int i;
|
||||
int temp;
|
||||
char *start = buf;
|
||||
|
||||
divisor = 1000000000;
|
||||
if (precision < 0)
|
||||
precision = 1;
|
||||
for (i = 9; i >= 0; i--, divisor /= 10) {
|
||||
temp = value / divisor;
|
||||
value = value % divisor;
|
||||
if ((precision > i) || (temp != 0)) {
|
||||
precision = i;
|
||||
*buf++ = (char) (temp + '0');
|
||||
}
|
||||
}
|
||||
*buf = 0;
|
||||
|
||||
return buf - start;
|
||||
}
|
||||
|
||||
static inline char *net_sprint_ip_addr_buf(uint8_t *ip, int ip_len,
|
||||
char *buf, int buflen)
|
||||
{
|
||||
uint16_t *w = (uint16_t *)ip;
|
||||
uint8_t i, bl, bh, longest = 1;
|
||||
int8_t pos = -1;
|
||||
char delim = ':';
|
||||
unsigned char zeros[8] = { 0 };
|
||||
char *ptr = buf;
|
||||
int len = -1;
|
||||
uint16_t value;
|
||||
bool needcolon = false;
|
||||
|
||||
switch (ip_len) {
|
||||
case 16:
|
||||
len = 8;
|
||||
|
||||
for (i = 0; i < 8; i++) {
|
||||
uint8_t j;
|
||||
|
||||
for (j = i; j < 8; j++) {
|
||||
if (w[j] != 0) {
|
||||
break;
|
||||
}
|
||||
zeros[i]++;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < 8; i++) {
|
||||
if (zeros[i] > longest) {
|
||||
longest = zeros[i];
|
||||
pos = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (longest == 1) {
|
||||
pos = -1;
|
||||
}
|
||||
break;
|
||||
|
||||
case 4:
|
||||
len = 4;
|
||||
delim = '.';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
/* Invalid len, bail out */
|
||||
if (len < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
|
||||
/* IPv4 address a.b.c.d */
|
||||
if (len == 4) {
|
||||
uint8_t l;
|
||||
|
||||
value = (uint32_t)ip[i];
|
||||
|
||||
/* net_byte_to_udec() eats 0 */
|
||||
if (value == 0) {
|
||||
*ptr++ = '0';
|
||||
*ptr++ = delim;
|
||||
continue;
|
||||
}
|
||||
|
||||
l = net_value_to_udec(ptr, value, 0);
|
||||
|
||||
ptr += l;
|
||||
*ptr++ = delim;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* IPv6 address */
|
||||
if (i == pos) {
|
||||
if (needcolon || i == 0) {
|
||||
*ptr++ = ':';
|
||||
}
|
||||
*ptr++ = ':';
|
||||
needcolon = false;
|
||||
i += longest - 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (needcolon) {
|
||||
*ptr++ = ':';
|
||||
needcolon = false;
|
||||
}
|
||||
|
||||
value = (uint32_t)sys_be16_to_cpu(w[i]);
|
||||
bh = value >> 8;
|
||||
bl = value & 0xff;
|
||||
|
||||
if (bh) {
|
||||
if (bh > 0x0f) {
|
||||
ptr = net_byte_to_hex(ptr, bh, 'a', false);
|
||||
} else {
|
||||
if (bh < 10) {
|
||||
*ptr++ = (char)(bh + '0');
|
||||
} else {
|
||||
*ptr++ = (char) (bh - 10 + 'a');
|
||||
}
|
||||
}
|
||||
ptr = net_byte_to_hex(ptr, bl, 'a', true);
|
||||
} else if (bl > 0x0f) {
|
||||
ptr = net_byte_to_hex(ptr, bl, 'a', false);
|
||||
} else {
|
||||
if (bl < 10) {
|
||||
*ptr++ = (char)(bl + '0');
|
||||
} else {
|
||||
*ptr++ = (char) (bl - 10 + 'a');
|
||||
}
|
||||
}
|
||||
needcolon = true;
|
||||
}
|
||||
|
||||
if (!(ptr - buf)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (ip_len == 4) {
|
||||
*(ptr - 1) = '\0';
|
||||
} else {
|
||||
*ptr = '\0';
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
static inline char *net_sprint_ip_addr_ptr(uint8_t *ptr, uint8_t len)
|
||||
{
|
||||
static char buf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx")];
|
||||
|
||||
return net_sprint_ip_addr_buf(ptr, len, (char *)buf, sizeof(buf));
|
||||
}
|
||||
|
||||
static inline char *net_sprint_ipv6_addr(struct in6_addr *addr)
|
||||
{
|
||||
#if defined(CONFIG_NET_IPV6)
|
||||
return net_sprint_ip_addr_ptr(addr->s6_addr, 16);
|
||||
#else
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline char *net_sprint_ipv4_addr(struct in_addr *addr)
|
||||
{
|
||||
#if defined(CONFIG_NET_IPV4)
|
||||
return net_sprint_ip_addr_ptr(addr->s4_addr, 4);
|
||||
#else
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline char *net_sprint_ip_addr(struct net_addr *addr)
|
||||
{
|
||||
switch (addr->family) {
|
||||
case AF_INET6:
|
||||
#if defined(CONFIG_NET_IPV6)
|
||||
return net_sprint_ipv6_addr(&addr->in6_addr);
|
||||
#else
|
||||
break;
|
||||
#endif
|
||||
case AF_INET:
|
||||
#if defined(CONFIG_NET_IPV4)
|
||||
return net_sprint_ipv4_addr(&addr->in_addr);
|
||||
#else
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#else /* NET_DEBUG */
|
||||
|
||||
static inline char *net_sprint_ll_addr(uint8_t *ll, uint8_t ll_len)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static inline char *net_sprint_ip_addr_ptr(uint8_t *ptr, uint8_t len)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static inline char *net_sprint_ipv6_addr(struct in6_addr *addr)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static inline char *net_sprint_ipv4_addr(struct in_addr *addr)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static inline char *net_sprint_ip_addr(struct net_addr *addr)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
#endif /* NET_DEBUG */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue