Updated API for uiplib and resolv to use uip_ipaddr_t instead of uint16_t for compability with IPv6

This commit is contained in:
nifi 2010-05-31 15:22:08 +00:00
parent c13bb8666b
commit b54c6e673b
22 changed files with 194 additions and 106 deletions

View file

@ -57,7 +57,7 @@
*
* This file is part of the uIP TCP/IP stack.
*
* $Id: resolv.c,v 1.8 2010/02/15 20:49:38 dak664 Exp $
* $Id: resolv.c,v 1.9 2010/05/31 15:22:08 nifi Exp $
*
*/
@ -71,6 +71,31 @@
#define NULL (void *)0
#endif /* NULL */
#if UIP_CONF_IPV6
/* Currently this implementation only supports IPv4 DNS lookups.
Until support for IPv6 is added, dummy functions are used to
enable compilation with IPv6.
*/
process_event_t resolv_event_found;
PROCESS(resolv_process, "DNS resolver");
void resolv_conf(const uip_ipaddr_t *dnsserver) { }
uip_ipaddr_t *resolv_getserver(void) { return NULL; }
uip_ipaddr_t *resolv_lookup(const char *name) { return NULL; }
void resolv_query(const char *name) { }
PROCESS_THREAD(resolv_process, ev, data)
{
PROCESS_BEGIN();
resolv_event_found = process_alloc_event();
PROCESS_END();
}
#else /* UIP_CONF_IPV6 */
/** \internal The maximum number of retries when asking for a name. */
#define MAX_RETRIES 8
@ -103,7 +128,7 @@ struct dns_answer {
u16_t class;
u16_t ttl[2];
u16_t len;
u16_t ipaddr[2];
u8_t ipaddr[4];
};
struct namemap {
@ -118,7 +143,7 @@ struct namemap {
u8_t seqno;
u8_t err;
char name[32];
u16_t ipaddr[2];
uip_ipaddr_t ipaddr;
};
#ifndef UIP_CONF_RESOLV_ENTRIES
@ -140,6 +165,8 @@ process_event_t resolv_event_found;
PROCESS(resolv_process, "DNS resolver");
static void resolv_found(char *name, uip_ipaddr_t *ipaddr);
enum {
EVENT_NEW_SERVER=0
};
@ -180,8 +207,8 @@ check_entries(void)
{
register struct dns_hdr *hdr;
char *query, *nptr, *nameptr;
static u8_t i;
static u8_t n;
uint8_t i;
uint8_t n;
register struct namemap *namemapptr;
for(i = 0; i < RESOLV_ENTRIES; ++i) {
@ -315,16 +342,17 @@ newdata(void)
ans->class == HTONS(1) &&
ans->len == HTONS(4)) {
/* printf("IP address %d.%d.%d.%d\n",
htons(ans->ipaddr[0]) >> 8,
htons(ans->ipaddr[0]) & 0xff,
htons(ans->ipaddr[1]) >> 8,
htons(ans->ipaddr[1]) & 0xff);*/
ans->ipaddr[0],
ans->ipaddr[1],
ans->ipaddr[2],
ans->ipaddr[3]);*/
/* XXX: we should really check that this IP address is the one
we want. */
namemapptr->ipaddr[0] = ans->ipaddr[0];
namemapptr->ipaddr[1] = ans->ipaddr[1];
for(i = 0; i < 4; i++) {
namemapptr->ipaddr.u8[i] = ans->ipaddr[i];
}
resolv_found(namemapptr->name, namemapptr->ipaddr);
resolv_found(namemapptr->name, &namemapptr->ipaddr);
return;
} else {
nameptr = nameptr + 10 + htons(ans->len);
@ -387,7 +415,7 @@ PROCESS_THREAD(resolv_process, ev, data)
*/
/*-----------------------------------------------------------------------------------*/
void
resolv_query(char *name)
resolv_query(const char *name)
{
static u8_t i;
static u8_t lseq, lseqi;
@ -435,8 +463,8 @@ resolv_query(char *name)
* hostnames.
*/
/*-----------------------------------------------------------------------------------*/
u16_t *
resolv_lookup(char *name)
uip_ipaddr_t *
resolv_lookup(const char *name)
{
static u8_t i;
struct namemap *nameptr;
@ -447,7 +475,7 @@ resolv_lookup(char *name)
nameptr = &names[i];
if(nameptr->state == STATE_DONE &&
strcmp(name, nameptr->name) == 0) {
return nameptr->ipaddr;
return &nameptr->ipaddr;
}
}
return NULL;
@ -496,12 +524,13 @@ resolv_conf(const uip_ipaddr_t *dnsserver)
*
*/
/*-----------------------------------------------------------------------------------*/
void
resolv_found(char *name, u16_t *ipaddr)
static void
resolv_found(char *name, uip_ipaddr_t *ipaddr)
{
process_post(PROCESS_BROADCAST, resolv_event_found, name);
}
/*-----------------------------------------------------------------------------------*/
#endif /* UIP_CONF_IPV6 */
#endif /* UIP_UDP */
/** @} */

View file

@ -34,7 +34,7 @@
*
* This file is part of the uIP TCP/IP stack.
*
* $Id: resolv.h,v 1.3 2006/09/18 23:30:40 oliverschmidt Exp $
* $Id: resolv.h,v 1.4 2010/05/31 15:22:08 nifi Exp $
*
*/
#ifndef __RESOLV_H__
@ -47,15 +47,11 @@
*/
CCIF extern process_event_t resolv_event_found;
/* Callbacks. */
void resolv_found(char *name, u16_t *ipaddr);
/* Functions. */
CCIF void resolv_conf(const uip_ipaddr_t *dnsserver);
CCIF uip_ipaddr_t *resolv_getserver(void);
void resolv_init(char *arg);
CCIF u16_t *resolv_lookup(char *name);
CCIF void resolv_query(char *name);
CCIF uip_ipaddr_t *resolv_lookup(const char *name);
CCIF void resolv_query(const char *name);
PROCESS_NAME(resolv_process);

View file

@ -29,19 +29,78 @@
*
* This file is part of the uIP TCP/IP stack and the Contiki operating system.
*
* $Id: uiplib.c,v 1.1 2006/06/17 22:41:19 adamdunkels Exp $
* $Id: uiplib.c,v 1.2 2010/05/31 15:22:08 nifi Exp $
*
*/
#include "net/uip.h"
#include "net/uiplib.h"
#include <string.h>
#define DEBUG DEBUG_NONE
#include "net/uip-debug.h"
/*-----------------------------------------------------------------------------------*/
unsigned char
uiplib_ipaddrconv(char *addrstr, unsigned char *ipaddr)
int
uiplib_ipaddrconv(const char *addrstr, uip_ipaddr_t *ipaddr)
{
#if UIP_CONF_IPV6
uint16_t value;
int tmp, len, zero;
char c;
value = 0;
zero = -1;
for(len = 0; len < sizeof(uip_ipaddr_t) - 1; addrstr++) {
c = *addrstr;
if(c == ':' || c == '\0') {
ipaddr->u8[len] = (value >> 8) & 0xff;
ipaddr->u8[len + 1] = value & 0xff;
len += 2;
value = 0;
if(c == '\0') {
break;
}
if(*(addrstr + 1) == ':') {
/* Zero compression */
if(zero < 0) {
zero = len;
}
addrstr++;
}
} else {
if(c >= '0' && c <= '9') {
tmp = c - '0';
} else if(c >= 'a' && c <= 'f') {
tmp = c - 'a' + 10;
} else if(c >= 'A' && c <= 'F') {
tmp = c - 'A' + 10;
} else {
PRINTF("uiplib: illegal char: '%c'\n", c);
return 0;
}
value = (value << 4) + (tmp & 0xf);
}
}
if(c != '\0') {
PRINTF("uiplib: too large address\n");
return 0;
}
if(len < sizeof(uip_ipaddr_t)) {
if(zero < 0) {
PRINTF("uiplib: too short address\n");
return 0;
}
memmove(&ipaddr->u8[zero + sizeof(uip_ipaddr_t) - len],
&ipaddr->u8[zero], len - zero);
memset(&ipaddr->u8[zero], 0, sizeof(uip_ipaddr_t) - len);
}
#else /* UIP_CONF_IPV6 */
unsigned char tmp;
char c;
unsigned char i, j;
@ -57,8 +116,7 @@ uiplib_ipaddrconv(char *addrstr, unsigned char *ipaddr)
return 0;
}
if(c == '.' || c == 0) {
*ipaddr = tmp;
++ipaddr;
ipaddr->u8[i] = tmp;
tmp = 0;
} else if(c >= '0' && c <= '9') {
tmp = (tmp * 10) + (c - '0');
@ -68,6 +126,7 @@ uiplib_ipaddrconv(char *addrstr, unsigned char *ipaddr)
++addrstr;
} while(c != '.' && c != 0);
}
#endif /* UIP_CONF_IPV6 */
return 1;
}

View file

@ -37,12 +37,14 @@
*
* This file is part of the Contiki desktop environment for the C64.
*
* $Id: uiplib.h,v 1.2 2006/08/26 23:58:45 oliverschmidt Exp $
* $Id: uiplib.h,v 1.3 2010/05/31 15:22:08 nifi Exp $
*
*/
#ifndef __UIPLIB_H__
#define __UIPLIB_H__
#include "net/uip.h"
/**
* \addtogroup uipconvfunc
* @{
@ -52,19 +54,20 @@
* Convert a textual representation of an IP address to a numerical representation.
*
* This function takes a textual representation of an IP address in
* the form a.b.c.d and converts it into a 4-byte array that can be
* used by other uIP functions.
* the form a.b.c.d for IPv4 or a:b:c:d:e:f:g:h for IPv6 and converts
* it into a numeric IP address representation that can be used by
* other uIP functions.
*
* \param addrstr A pointer to a string containing the IP address in
* textual form.
*
* \param addr A pointer to a 4-byte array that will be filled in with
* \param addr A pointer to a uip_ipaddr_t that will be filled in with
* the numerical representation of the address.
*
* \retval 0 If the IP address could not be parsed.
* \retval Non-zero If the IP address was parsed.
*/
CCIF unsigned char uiplib_ipaddrconv(char *addrstr, unsigned char *addr);
CCIF int uiplib_ipaddrconv(const char *addrstr, uip_ipaddr_t *addr);
/** @} */