RPL border router Web server cleanup

This commit is contained in:
Simon Duquennoy 2017-12-09 07:27:08 -08:00
parent bc73bb3284
commit 52b10b6389
1 changed files with 55 additions and 205 deletions

View File

@ -32,46 +32,34 @@
#include "contiki.h" #include "contiki.h"
#include "net/routing/routing.h" #include "net/routing/routing.h"
#if RPL_WITH_NON_STORING
/* For RPL non-storing specific link printout.
* Note that traditional DS6 routes are printed in addition,
* whenever UIP_MAX_ROUTES != 0 */
#include "rpl-ns.h"
#endif
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static const char *TOP = "<html><head><title>Contiki-NG</title></head><body>\n"; static const char *TOP = "<html>\n <head>\n <title>Contiki-NG</title>\n </head>\n<body>\n";
static const char *BOTTOM = "</body></html>\n"; static const char *BOTTOM = "\n</body>\n</html>\n";
#if BUF_USES_STACK
static char *bufptr, *bufend;
#define ADD(...) do { \
bufptr += snprintf(bufptr, bufend - bufptr, __VA_ARGS__); \
} while(0)
#else
static char buf[256]; static char buf[256];
static int blen; static int blen;
#define ADD(...) do { \ #define ADD(...) do { \
blen += snprintf(&buf[blen], sizeof(buf) - blen, __VA_ARGS__); \ blen += snprintf(&buf[blen], sizeof(buf) - blen, __VA_ARGS__); \
} while(0) } while(0)
#endif #define SEND(s) do { \
SEND_STRING(s, buf); \
blen = 0; \
} while(0);
/* Use simple webserver with only one page for minimum footprint. /* Use simple webserver with only one page for minimum footprint.
* Multiple connections can result in interleaved tcp segments since * Multiple connections can result in interleaved tcp segments since
* a single static buffer is used for all segments. * a single static buffer is used for all segments.
*/ */
#include "httpd-simple.h" #include "httpd-simple.h"
/* The internal webserver can provide additional information if
* enough program flash is available.
*/
#define WEBSERVER_CONF_LOADTIME 0
#define WEBSERVER_CONF_FILESTATS 0
#define WEBSERVER_CONF_NEIGHBOR_STATUS 0
/* Adding links requires a larger RAM buffer. To avoid static allocation
* the stack can be used for formatting; however tcp retransmissions
* and multiple connections can result in garbled segments.
* TODO:use PSOCk_GENERATOR_SEND and tcp state storage to fix this.
*/
#define WEBSERVER_CONF_ROUTE_LINKS 0
#if WEBSERVER_CONF_ROUTE_LINKS
#define BUF_USES_STACK 1
#endif
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static void static void
@ -99,210 +87,72 @@ ipaddr_add(const uip_ipaddr_t *addr)
static static
PT_THREAD(generate_routes(struct httpd_state *s)) PT_THREAD(generate_routes(struct httpd_state *s))
{ {
static uip_ds6_route_t *r;
#if RPL_WITH_NON_STORING
static rpl_ns_node_t *link;
#endif /* RPL_WITH_NON_STORING */
static uip_ds6_nbr_t *nbr; static uip_ds6_nbr_t *nbr;
#if BUF_USES_STACK
char buf[256];
#endif
#if WEBSERVER_CONF_LOADTIME
static clock_time_t numticks;
numticks = clock_time();
#endif
PSOCK_BEGIN(&s->sout); PSOCK_BEGIN(&s->sout);
SEND_STRING(&s->sout, TOP); SEND_STRING(&s->sout, TOP);
#if BUF_USES_STACK
bufptr = buf;
bufend = bufptr + sizeof(buf);
#else
blen = 0;
#endif
ADD("Neighbors<pre>");
ADD(" Neighbors\n <ul>\n");
SEND(&s->sout);
for(nbr = nbr_table_head(ds6_neighbors); for(nbr = nbr_table_head(ds6_neighbors);
nbr != NULL; nbr != NULL;
nbr = nbr_table_next(ds6_neighbors, nbr)) { nbr = nbr_table_next(ds6_neighbors, nbr)) {
ADD(" <li>");
#if WEBSERVER_CONF_NEIGHBOR_STATUS
#if BUF_USES_STACK
{ char *j = bufptr + 25;
ipaddr_add(&nbr->ipaddr);
while(bufptr < j) ADD(" ");
switch(nbr->state) {
case NBR_INCOMPLETE: ADD(" INCOMPLETE");
break;
case NBR_REACHABLE: ADD(" REACHABLE");
break;
case NBR_STALE: ADD(" STALE");
break;
case NBR_DELAY: ADD(" DELAY");
break;
case NBR_PROBE: ADD(" NBR_PROBE");
break;
}
}
#else
{ uint8_t j = blen + 25;
ipaddr_add(&nbr->ipaddr);
while(blen < j) ADD(" ");
switch(nbr->state) {
case NBR_INCOMPLETE: ADD(" INCOMPLETE");
break;
case NBR_REACHABLE: ADD(" REACHABLE");
break;
case NBR_STALE: ADD(" STALE");
break;
case NBR_DELAY: ADD(" DELAY");
break;
case NBR_PROBE: ADD(" NBR_PROBE");
break;
}
}
#endif
#else
ipaddr_add(&nbr->ipaddr); ipaddr_add(&nbr->ipaddr);
#endif ADD("</li>\n");
SEND(&s->sout);
ADD("\n");
#if BUF_USES_STACK
if(bufptr > bufend - 45) {
SEND_STRING(&s->sout, buf);
bufptr = buf;
bufend = bufptr + sizeof(buf);
}
#else
if(blen > sizeof(buf) - 45) {
SEND_STRING(&s->sout, buf);
blen = 0;
}
#endif
} }
ADD("</pre>Routes<pre>\n"); ADD(" </ul>\n");
SEND_STRING(&s->sout, buf); SEND(&s->sout);
#if BUF_USES_STACK
bufptr = buf;
bufend = bufptr + sizeof(buf);
#else
blen = 0;
#endif
for(r = uip_ds6_route_head(); r != NULL; r = uip_ds6_route_next(r)) { #if (UIP_MAX_ROUTES != 0)
{
#if BUF_USES_STACK static uip_ds6_route_t *r;
#if WEBSERVER_CONF_ROUTE_LINKS ADD(" Routes\n <ul>\n");
ADD("<a href=http://["); SEND(&s->sout);
ipaddr_add(&r->ipaddr); for(r = uip_ds6_route_head(); r != NULL; r = uip_ds6_route_next(r)) {
ADD("]/status.shtml>"); ADD(" <li>");
ipaddr_add(&r->ipaddr); ipaddr_add(&r->ipaddr);
ADD("</a>"); ADD("/%u (via ", r->length);
#else ipaddr_add(uip_ds6_route_nexthop(r));
ipaddr_add(&r->ipaddr); ADD(") %lus", (unsigned long)r->state.lifetime);
#endif ADD("</li>\n");
#else SEND(&s->sout);
#if WEBSERVER_CONF_ROUTE_LINKS
ADD("<a href=http://[");
ipaddr_add(&r->ipaddr);
ADD("]/status.shtml>");
SEND_STRING(&s->sout, buf);
blen = 0;
ipaddr_add(&r->ipaddr);
ADD("</a>");
#else
ipaddr_add(&r->ipaddr);
#endif
#endif
ADD("/%u (via ", r->length);
ipaddr_add(uip_ds6_route_nexthop(r));
if(1 || (r->state.lifetime < 600)) {
ADD(") %lus\n", (unsigned long)r->state.lifetime);
} else {
ADD(")\n");
} }
SEND_STRING(&s->sout, buf); ADD(" </ul>\n");
#if BUF_USES_STACK SEND(&s->sout);
bufptr = buf;
bufend = bufptr + sizeof(buf);
#else
blen = 0;
#endif
} }
ADD("</pre>"); #endif /* UIP_MAX_ROUTES != 0 */
#if RPL_WITH_NON_STORING #if RPL_WITH_NON_STORING
ADD("Links<pre>\n"); {
SEND_STRING(&s->sout, buf); static rpl_ns_node_t *link;
#if BUF_USES_STACK ADD(" Routing links\n <ul>\n");
bufptr = buf; SEND(&s->sout);
bufend = bufptr + sizeof(buf); for(link = rpl_ns_node_head(); link != NULL; link = rpl_ns_node_next(link)) {
#else if(link->parent != NULL) {
blen = 0; uip_ipaddr_t child_ipaddr;
#endif uip_ipaddr_t parent_ipaddr;
for(link = rpl_ns_node_head(); link != NULL; link = rpl_ns_node_next(link)) {
if(link->parent != NULL) {
uip_ipaddr_t child_ipaddr;
uip_ipaddr_t parent_ipaddr;
rpl_ns_get_node_global_addr(&child_ipaddr, link); rpl_ns_get_node_global_addr(&child_ipaddr, link);
rpl_ns_get_node_global_addr(&parent_ipaddr, link->parent); rpl_ns_get_node_global_addr(&parent_ipaddr, link->parent);
#if BUF_USES_STACK ADD(" <li>");
#if WEBSERVER_CONF_ROUTE_LINKS ipaddr_add(&child_ipaddr);
ADD("<a href=http://[");
ipaddr_add(&child_ipaddr);
ADD("]/status.shtml>");
ipaddr_add(&child_ipaddr);
ADD("</a>");
#else
ipaddr_add(&child_ipaddr);
#endif
#else
#if WEBSERVER_CONF_ROUTE_LINKS
ADD("<a href=http://[");
ipaddr_add(&child_ipaddr);
ADD("]/status.shtml>");
SEND_STRING(&s->sout, buf);
blen = 0;
ipaddr_add(&child_ipaddr);
ADD("</a>");
#else
ipaddr_add(&child_ipaddr);
#endif
#endif
ADD(" (parent: "); ADD(" (parent: ");
ipaddr_add(&parent_ipaddr); ipaddr_add(&parent_ipaddr);
if(1 || (link->lifetime < 600)) { ADD(") %us", (unsigned int)link->lifetime);
ADD(") %us\n", (unsigned int)link->lifetime);
} else { ADD("</li>\n");
ADD(")\n"); SEND(&s->sout);
} }
SEND_STRING(&s->sout, buf);
#if BUF_USES_STACK
bufptr = buf;
bufend = bufptr + sizeof(buf);
#else
blen = 0;
#endif
} }
ADD(" </ul>");
SEND(&s->sout);
} }
ADD("</pre>");
#endif /* RPL_WITH_NON_STORING */ #endif /* RPL_WITH_NON_STORING */
#if WEBSERVER_CONF_FILESTATS
static uint16_t numtimes;
ADD("<br><i>This page sent %u times</i>", ++numtimes);
#endif
#if WEBSERVER_CONF_LOADTIME
numticks = clock_time() - numticks + 1;
ADD(" <i>(%u.%02u sec)</i>", numticks / CLOCK_SECOND, (100 * (numticks % CLOCK_SECOND)) / CLOCK_SECOND));
#endif
SEND_STRING(&s->sout, buf);
SEND_STRING(&s->sout, BOTTOM); SEND_STRING(&s->sout, BOTTOM);
PSOCK_END(&s->sout); PSOCK_END(&s->sout);