2006-06-18 00:41:10 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2002, Adam Dunkels.
|
2008-11-09 13:39:31 +01:00
|
|
|
* All rights reserved.
|
2006-06-18 00:41:10 +02:00
|
|
|
*
|
2008-11-09 13:39:31 +01:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2006-06-18 00:41:10 +02:00
|
|
|
* 2. Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials provided
|
2008-11-09 13:39:31 +01:00
|
|
|
* with the distribution.
|
2006-06-18 00:41:10 +02:00
|
|
|
* 3. The name of the author may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior
|
2008-11-09 13:39:31 +01:00
|
|
|
* written permission.
|
2006-06-18 00:41:10 +02:00
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
|
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
|
|
|
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
2008-11-09 13:39:31 +01:00
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2006-06-18 00:41:10 +02:00
|
|
|
*
|
|
|
|
* This file is part of the "contiki" web browser.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-11-30 22:53:50 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
#include "contiki-net.h"
|
2007-11-30 22:53:50 +01:00
|
|
|
#include "www.h"
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
#include "webclient.h"
|
|
|
|
|
|
|
|
#define WEBCLIENT_TIMEOUT 100
|
|
|
|
|
|
|
|
#define WEBCLIENT_STATE_STATUSLINE 0
|
|
|
|
#define WEBCLIENT_STATE_HEADERS 1
|
|
|
|
#define WEBCLIENT_STATE_DATA 2
|
|
|
|
#define WEBCLIENT_STATE_CLOSE 3
|
|
|
|
|
|
|
|
#define HTTPFLAG_NONE 0
|
|
|
|
#define HTTPFLAG_OK 1
|
|
|
|
#define HTTPFLAG_MOVED 2
|
2015-06-05 15:44:09 +02:00
|
|
|
#define HTTPFLAG_HTTPS 3
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
#define ISO_nl 0x0a
|
|
|
|
#define ISO_cr 0x0d
|
|
|
|
#define ISO_space 0x20
|
|
|
|
|
|
|
|
struct webclient_state {
|
2012-02-20 20:13:50 +01:00
|
|
|
uint8_t timer;
|
|
|
|
uint8_t state;
|
|
|
|
uint8_t httpflag;
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2012-02-20 20:13:50 +01:00
|
|
|
uint16_t port;
|
2006-06-18 00:41:10 +02:00
|
|
|
char host[40];
|
2015-05-02 00:11:59 +02:00
|
|
|
char file[WWW_CONF_MAX_URLLEN - 10]; // URL - "http://<host>/"
|
2012-02-20 20:13:50 +01:00
|
|
|
uint16_t getrequestptr;
|
|
|
|
uint16_t getrequestleft;
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2015-05-02 00:11:59 +02:00
|
|
|
char httpheaderline[WWW_CONF_MAX_URLLEN + 10]; // URL + "Location: "
|
2012-02-20 20:13:50 +01:00
|
|
|
uint16_t httpheaderlineptr;
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
char mimetype[32];
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct webclient_state s;
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
char *
|
|
|
|
webclient_mimetype(void)
|
|
|
|
{
|
|
|
|
return s.mimetype;
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
char *
|
|
|
|
webclient_filename(void)
|
|
|
|
{
|
|
|
|
return s.file;
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
char *
|
|
|
|
webclient_hostname(void)
|
|
|
|
{
|
|
|
|
return s.host;
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
unsigned short
|
|
|
|
webclient_port(void)
|
|
|
|
{
|
|
|
|
return s.port;
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
webclient_init(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
init_connection(void)
|
|
|
|
{
|
|
|
|
s.state = WEBCLIENT_STATE_STATUSLINE;
|
|
|
|
|
|
|
|
s.getrequestleft = sizeof(http_get) - 1 + 1 +
|
|
|
|
sizeof(http_10) - 1 +
|
|
|
|
sizeof(http_crnl) - 1 +
|
|
|
|
sizeof(http_host) - 1 +
|
|
|
|
sizeof(http_crnl) - 1 +
|
2012-02-20 20:13:50 +01:00
|
|
|
(uint16_t)strlen(http_user_agent_fields) +
|
|
|
|
(uint16_t)strlen(s.file) + (uint16_t)strlen(s.host);
|
2006-06-18 00:41:10 +02:00
|
|
|
s.getrequestptr = 0;
|
|
|
|
|
|
|
|
s.httpheaderlineptr = 0;
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
webclient_close(void)
|
|
|
|
{
|
|
|
|
s.state = WEBCLIENT_STATE_CLOSE;
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
unsigned char
|
2012-02-20 20:13:50 +01:00
|
|
|
webclient_get(const char *host, uint16_t port, const char *file)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
2010-05-31 17:22:08 +02:00
|
|
|
uip_ipaddr_t addr;
|
2006-06-18 00:41:10 +02:00
|
|
|
struct uip_conn *conn;
|
2010-05-31 17:22:08 +02:00
|
|
|
uip_ipaddr_t *ipaddr;
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
/* First check if the host is an IP address. */
|
2010-05-31 17:22:08 +02:00
|
|
|
ipaddr = &addr;
|
|
|
|
if(uiplib_ipaddrconv(host, &addr) == 0) {
|
2010-02-01 20:44:30 +01:00
|
|
|
#if UIP_UDP
|
core/net/resolv: IPv6 and mDNS ("Bonjour") support. Major refactor.
This patch updates the DNS resolver to support IPv6 and introduces an
improved API for looking up DNS entries. This patch also adds optional
support for mDNS lookups and responses to the DNS resolver.
Here is a quick summary of the changes:
* Added support for IPv6 lookups.
* DNS queries now honor record expiration.
* Added support for mDNS, compatible with "Bonjour".
* Implemented a new lookup api, `resolv_lookup2()`, which provides
more information about the state of the record(error, expired,
looking-up, etc.).
About mDNS/Bonjour Support
--------------------------
This patch adds basic support for mDNS/Bonjour, which allows you to
refer to the name of a device instead of its IP address. This is
incredibly convenient for IPv6 addresses because they tend to be very
long and difficult to remember. It is especially important for
link-local IPv6 addresses, since not all programs support the '%'
notation for indicating a network interface (required on systems with
more than one network interface to disambiguate).
In other words, instead of typing in this:
* `http://[fe80::58dc:d7ed:a644:628f%en1]/`
You can type this instead:
* `http://contiki.local/`
Huge improvement, no?
The convenience extends beyond that: this mechanism can be used for
nodes to talk to each other based on their human-readable names instead
of their IPv6 addresses. So instead of a switch on
`aaaa::58dc:d7ed:a644:628f` triggering an actuator on
`aaaa::ed26:19c1:4bd2:f95b`, `light-switch.local` can trigger the
actuator on `living-room-lights.local`.
What you need to do to be able to look up `.local` names on your
workstation depends on a few factors:
* Your machine needs to be able to send and receive multicast packets
to and from the LoWPAN. You can do this easily with the Jackdaw
firmware on an RZUSBStick. If you have a border router, you will need
it to bridge the mDNS multicast packets across the border.
* If you are using a Mac, you win. All Apple devices support mDNS
lookups.
* If you are using Windows, you can install Apple's Bonjour for Windows
package. (This may be already installed on your machine if you have
installed iTunes) After you install this you can easily do `.local`
lookups.
* If you are using a Unix machine, you can install Avahi.
The default hostname is set to `contiki.local.`. You can change the
hostname programmatically by calling `resolv_set_hostname()`. You can
change the default hostname by changing `CONTIKI_CONF_DEFAULT_HOSTNAME`.
You may disable mDNS support by setting `RESOLV_CONF_SUPPORTS_MDNS` to
`0`.
---------------------------------
core/net/resolv: `resolv_lookup2()` -> `resolv_lookup()`
Note that this patch should fix several `resolv_lookup()` bugs
that already existed. There were many cases where `resolv_lookup()`
was being called and the IP address ignored, but later code
assumed that the IP address had been fetched... ANYWAY, those
should be fixed now.
---------------------------------
examples/udp-ipv6: Updated client to use MDNS to lookup the server.
Also updated the Cooja regression test simulation.
2012-05-17 17:24:08 +02:00
|
|
|
if(resolv_lookup(host,&ipaddr) != RESOLV_STATUS_CACHED) {
|
2006-06-18 00:41:10 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2010-02-01 20:44:30 +01:00
|
|
|
#else /* UIP_UDP */
|
2010-05-31 17:22:08 +02:00
|
|
|
return 0;
|
2010-02-01 20:44:30 +01:00
|
|
|
#endif /* UIP_UDP */
|
2010-05-31 17:22:08 +02:00
|
|
|
}
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2010-10-19 20:29:03 +02:00
|
|
|
conn = tcp_connect(ipaddr, uip_htons(port), NULL);
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
if(conn == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
s.port = port;
|
|
|
|
strncpy(s.file, file, sizeof(s.file));
|
|
|
|
strncpy(s.host, host, sizeof(s.host));
|
|
|
|
|
|
|
|
init_connection();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
2008-11-09 13:39:31 +01:00
|
|
|
/* Copy data into a "window", specified by the windowstart and
|
|
|
|
windowend variables. Only data that fits within the window is
|
|
|
|
copied. This function is used to copy data into the uIP buffer, which
|
|
|
|
typically is smaller than the data that is to be copied.
|
|
|
|
*/
|
|
|
|
static unsigned char *windowptr;
|
|
|
|
static int windowstart, windowend;
|
|
|
|
static int
|
|
|
|
window_copy(int curptr, const char *data, unsigned char datalen)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
2008-11-09 13:39:31 +01:00
|
|
|
int len;
|
|
|
|
|
|
|
|
if(windowstart == windowend) {
|
|
|
|
return curptr + datalen;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(curptr + datalen < windowstart) {
|
|
|
|
/* If all the data is before the window, we do not copy the
|
|
|
|
data. */
|
|
|
|
return curptr + datalen;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(curptr > windowend) {
|
|
|
|
/* If all the data is after the window, we do not copy the data. */
|
|
|
|
return curptr + datalen;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = datalen;
|
|
|
|
|
|
|
|
/* Trim off data before the window. */
|
2008-11-10 22:57:26 +01:00
|
|
|
data += windowstart - curptr;
|
|
|
|
len -= windowstart - curptr;
|
2008-11-09 13:39:31 +01:00
|
|
|
|
|
|
|
/* Trim off data after the window. */
|
|
|
|
if(len > windowend - windowstart) {
|
|
|
|
len = windowend - windowstart;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy(windowptr + windowstart, data, len);
|
|
|
|
windowstart += len;
|
|
|
|
|
|
|
|
return curptr + datalen;
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
senddata(void)
|
|
|
|
{
|
2012-02-20 20:13:50 +01:00
|
|
|
uint16_t len;
|
2008-11-09 13:39:31 +01:00
|
|
|
int curptr;
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
if(s.getrequestleft > 0) {
|
|
|
|
|
2008-11-09 13:39:31 +01:00
|
|
|
windowstart = s.getrequestptr;
|
|
|
|
curptr = 0;
|
|
|
|
windowend = windowstart + uip_mss();
|
|
|
|
windowptr = (char *)uip_appdata - windowstart;
|
|
|
|
|
|
|
|
curptr = window_copy(curptr, http_get, sizeof(http_get) - 1);
|
|
|
|
curptr = window_copy(curptr, s.file, (unsigned char)strlen(s.file));
|
|
|
|
curptr = window_copy(curptr, " ", 1);
|
|
|
|
curptr = window_copy(curptr, http_10, sizeof(http_10) - 1);
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2008-11-09 13:39:31 +01:00
|
|
|
curptr = window_copy(curptr, http_crnl, sizeof(http_crnl) - 1);
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2008-11-09 13:39:31 +01:00
|
|
|
curptr = window_copy(curptr, http_host, sizeof(http_host) - 1);
|
|
|
|
curptr = window_copy(curptr, s.host, (unsigned char)strlen(s.host));
|
|
|
|
curptr = window_copy(curptr, http_crnl, sizeof(http_crnl) - 1);
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2008-11-09 13:39:31 +01:00
|
|
|
curptr = window_copy(curptr, http_user_agent_fields,
|
2006-08-15 01:31:40 +02:00
|
|
|
(unsigned char)strlen(http_user_agent_fields));
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
len = s.getrequestleft > uip_mss()?
|
|
|
|
uip_mss():
|
|
|
|
s.getrequestleft;
|
2008-11-09 13:39:31 +01:00
|
|
|
uip_send(uip_appdata, len);
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2008-11-09 13:39:31 +01:00
|
|
|
}
|
2006-06-18 00:41:10 +02:00
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
acked(void)
|
|
|
|
{
|
2012-02-20 20:13:50 +01:00
|
|
|
uint16_t len;
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
if(s.getrequestleft > 0) {
|
|
|
|
len = s.getrequestleft > uip_mss()?
|
|
|
|
uip_mss():
|
|
|
|
s.getrequestleft;
|
|
|
|
s.getrequestleft -= len;
|
|
|
|
s.getrequestptr += len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
2012-02-20 20:13:50 +01:00
|
|
|
static uint16_t
|
|
|
|
parse_statusline(uint16_t len)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
|
|
|
char *cptr;
|
|
|
|
|
|
|
|
while(len > 0 && s.httpheaderlineptr < sizeof(s.httpheaderline)) {
|
|
|
|
s.httpheaderline[s.httpheaderlineptr] = *(char *)uip_appdata;
|
2007-11-18 02:36:59 +01:00
|
|
|
uip_appdata = (char *)uip_appdata + 1;
|
2006-06-18 00:41:10 +02:00
|
|
|
--len;
|
|
|
|
if(s.httpheaderline[s.httpheaderlineptr] == ISO_nl) {
|
|
|
|
|
|
|
|
if((strncmp(s.httpheaderline, http_10,
|
|
|
|
sizeof(http_10) - 1) == 0) ||
|
|
|
|
(strncmp(s.httpheaderline, http_11,
|
|
|
|
sizeof(http_11) - 1) == 0)) {
|
|
|
|
cptr = &(s.httpheaderline[9]);
|
|
|
|
s.httpflag = HTTPFLAG_NONE;
|
|
|
|
if(strncmp(cptr, http_200, sizeof(http_200) - 1) == 0) {
|
|
|
|
/* 200 OK */
|
|
|
|
s.httpflag = HTTPFLAG_OK;
|
|
|
|
} else if(strncmp(cptr, http_301, sizeof(http_301) - 1) == 0 ||
|
|
|
|
strncmp(cptr, http_302, sizeof(http_302) - 1) == 0) {
|
|
|
|
/* 301 Moved permanently or 302 Found. Location: header line
|
|
|
|
will contain thw new location. */
|
|
|
|
s.httpflag = HTTPFLAG_MOVED;
|
|
|
|
} else {
|
|
|
|
s.httpheaderline[s.httpheaderlineptr - 1] = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uip_abort();
|
|
|
|
webclient_aborted();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We're done parsing the status line, so we reset the pointer
|
|
|
|
and start parsing the HTTP headers.*/
|
|
|
|
s.httpheaderlineptr = 0;
|
|
|
|
s.state = WEBCLIENT_STATE_HEADERS;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
++s.httpheaderlineptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
static char
|
|
|
|
casecmp(char *str1, const char *str2, char len)
|
|
|
|
{
|
|
|
|
static char c;
|
|
|
|
|
|
|
|
while(len > 0) {
|
|
|
|
c = *str1;
|
|
|
|
/* Force lower-case characters. */
|
|
|
|
if(c & 0x40) {
|
|
|
|
c |= 0x20;
|
|
|
|
}
|
|
|
|
if(*str2 != c) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
++str1;
|
|
|
|
++str2;
|
|
|
|
--len;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
2012-02-20 20:13:50 +01:00
|
|
|
static uint16_t
|
|
|
|
parse_headers(uint16_t len)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
|
|
|
char *cptr;
|
|
|
|
static unsigned char i;
|
|
|
|
|
2015-05-02 00:11:59 +02:00
|
|
|
while(len > 0) {
|
2006-06-18 00:41:10 +02:00
|
|
|
s.httpheaderline[s.httpheaderlineptr] = *(char *)uip_appdata;
|
2007-11-18 02:36:59 +01:00
|
|
|
uip_appdata = (char *)uip_appdata + 1;
|
2006-06-18 00:41:10 +02:00
|
|
|
--len;
|
|
|
|
if(s.httpheaderline[s.httpheaderlineptr] == ISO_nl) {
|
2015-05-02 00:11:59 +02:00
|
|
|
/* We reached the end of an HTTP header line. */
|
2006-06-18 00:41:10 +02:00
|
|
|
if(s.httpheaderline[0] == ISO_cr) {
|
|
|
|
/* This was the last header line (i.e., and empty "\r\n"), so
|
|
|
|
we are done with the headers and proceed with the actual
|
|
|
|
data. */
|
|
|
|
s.state = WEBCLIENT_STATE_DATA;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2015-05-02 00:11:59 +02:00
|
|
|
if(s.httpheaderlineptr < sizeof(s.httpheaderline) - 1) {
|
|
|
|
/* We have an entire HTTP header line in s.httpheaderline, so
|
|
|
|
we parse it. */
|
|
|
|
s.httpheaderline[s.httpheaderlineptr - 1] = 0;
|
|
|
|
/* Check for specific HTTP header fields. */
|
|
|
|
if(casecmp(s.httpheaderline, http_content_type,
|
|
|
|
sizeof(http_content_type) - 1) == 0) {
|
|
|
|
/* Found Content-type field. */
|
|
|
|
cptr = strchr(s.httpheaderline, ';');
|
|
|
|
if(cptr != NULL) {
|
|
|
|
*cptr = 0;
|
|
|
|
}
|
|
|
|
strncpy(s.mimetype, s.httpheaderline +
|
|
|
|
sizeof(http_content_type) - 1, sizeof(s.mimetype));
|
|
|
|
} else if(casecmp(s.httpheaderline, http_location,
|
|
|
|
sizeof(http_location) - 1) == 0) {
|
|
|
|
cptr = s.httpheaderline +
|
|
|
|
sizeof(http_location) - 1;
|
2015-06-05 15:44:09 +02:00
|
|
|
|
|
|
|
if(strncmp(cptr, http_https, sizeof(http_https) - 1) == 0) {
|
|
|
|
s.httpflag = HTTPFLAG_HTTPS;
|
|
|
|
} else if(strncmp(cptr, http_http, 7) == 0) {
|
2015-05-02 00:11:59 +02:00
|
|
|
cptr += 7;
|
|
|
|
for(i = 0; i < s.httpheaderlineptr - 7; ++i) {
|
|
|
|
if(*cptr == 0 ||
|
|
|
|
*cptr == '/' ||
|
|
|
|
*cptr == ' ' ||
|
|
|
|
*cptr == ':') {
|
|
|
|
s.host[i] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s.host[i] = *cptr;
|
|
|
|
++cptr;
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
|
|
|
}
|
2015-05-02 00:11:59 +02:00
|
|
|
strncpy(s.file, cptr, sizeof(s.file));
|
|
|
|
/* s.file[s.httpheaderlineptr - i] = 0;*/
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We're done parsing, so we reset the pointer and start the
|
|
|
|
next line. */
|
2008-11-09 13:39:31 +01:00
|
|
|
s.httpheaderlineptr = 0;
|
2006-06-18 00:41:10 +02:00
|
|
|
} else {
|
2015-05-02 00:11:59 +02:00
|
|
|
if(s.httpheaderlineptr < sizeof(s.httpheaderline) - 1) {
|
|
|
|
++s.httpheaderlineptr;
|
|
|
|
}
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
newdata(void)
|
|
|
|
{
|
2012-02-20 20:13:50 +01:00
|
|
|
uint16_t len;
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
len = uip_datalen();
|
|
|
|
|
|
|
|
if(s.state == WEBCLIENT_STATE_STATUSLINE) {
|
|
|
|
len = parse_statusline(len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(s.state == WEBCLIENT_STATE_HEADERS && len > 0) {
|
|
|
|
len = parse_headers(len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(len > 0 && s.state == WEBCLIENT_STATE_DATA &&
|
2015-06-05 15:44:09 +02:00
|
|
|
s.httpflag == HTTPFLAG_OK) {
|
2006-06-18 00:41:10 +02:00
|
|
|
webclient_datahandler((char *)uip_appdata, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
webclient_appcall(void *state)
|
|
|
|
{
|
2008-11-09 13:39:31 +01:00
|
|
|
char *dataptr;
|
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
if(uip_connected()) {
|
|
|
|
s.timer = 0;
|
|
|
|
s.state = WEBCLIENT_STATE_STATUSLINE;
|
|
|
|
senddata();
|
|
|
|
webclient_connected();
|
|
|
|
tcp_markconn(uip_conn, &s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(uip_timedout()) {
|
|
|
|
webclient_timedout();
|
|
|
|
}
|
2008-11-09 13:39:31 +01:00
|
|
|
|
|
|
|
if(uip_aborted()) {
|
|
|
|
webclient_aborted();
|
|
|
|
}
|
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
if(state == NULL) {
|
|
|
|
uip_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(s.state == WEBCLIENT_STATE_CLOSE) {
|
|
|
|
webclient_closed();
|
|
|
|
uip_abort();
|
|
|
|
return;
|
|
|
|
}
|
2008-11-09 13:39:31 +01:00
|
|
|
|
|
|
|
/* The acked() and newdata() functions may alter the uip_appdata
|
|
|
|
ptr, so we need to store it in the "dataptr" variable so that we
|
|
|
|
can restore it before the senddata() function is called. */
|
|
|
|
dataptr = uip_appdata;
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
if(uip_acked()) {
|
|
|
|
s.timer = 0;
|
|
|
|
acked();
|
|
|
|
}
|
|
|
|
if(uip_newdata()) {
|
|
|
|
s.timer = 0;
|
|
|
|
newdata();
|
|
|
|
}
|
2008-11-09 13:39:31 +01:00
|
|
|
|
|
|
|
uip_appdata = dataptr;
|
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
if(uip_rexmit() ||
|
|
|
|
uip_newdata() ||
|
|
|
|
uip_acked()) {
|
|
|
|
senddata();
|
|
|
|
} else if(uip_poll()) {
|
|
|
|
++s.timer;
|
|
|
|
if(s.timer == WEBCLIENT_TIMEOUT) {
|
|
|
|
webclient_timedout();
|
|
|
|
uip_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* senddata();*/
|
|
|
|
}
|
|
|
|
|
|
|
|
if(uip_closed()) {
|
|
|
|
tcp_markconn(uip_conn, NULL);
|
2015-11-01 15:34:09 +01:00
|
|
|
/* Client requested close takes precedence over server initiated close. */
|
|
|
|
if(s.state == WEBCLIENT_STATE_CLOSE) {
|
|
|
|
webclient_closed();
|
|
|
|
return;
|
|
|
|
}
|
2015-06-05 15:44:09 +02:00
|
|
|
switch(s.httpflag) {
|
|
|
|
case HTTPFLAG_HTTPS:
|
|
|
|
/* Send some info to the user. */
|
|
|
|
webclient_datahandler((char *)http_redirect, sizeof(http_redirect) - 1);
|
|
|
|
webclient_datahandler(s.file, strlen(s.file));
|
|
|
|
webclient_datahandler((char *)http_crnl, sizeof(http_crnl) - 1);
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case HTTPFLAG_OK:
|
2006-06-18 00:41:10 +02:00
|
|
|
/* Send NULL data to signal EOF. */
|
|
|
|
webclient_datahandler(NULL, 0);
|
2015-06-05 15:44:09 +02:00
|
|
|
break;
|
|
|
|
case HTTPFLAG_MOVED:
|
2006-06-18 00:41:10 +02:00
|
|
|
/* conn = uip_connect(uip_conn->ripaddr, s.port);
|
|
|
|
if(conn != NULL) {
|
|
|
|
dispatcher_markconn(conn, NULL);
|
|
|
|
init_connection();
|
|
|
|
}*/
|
2010-02-01 20:44:30 +01:00
|
|
|
#if UIP_UDP
|
core/net/resolv: IPv6 and mDNS ("Bonjour") support. Major refactor.
This patch updates the DNS resolver to support IPv6 and introduces an
improved API for looking up DNS entries. This patch also adds optional
support for mDNS lookups and responses to the DNS resolver.
Here is a quick summary of the changes:
* Added support for IPv6 lookups.
* DNS queries now honor record expiration.
* Added support for mDNS, compatible with "Bonjour".
* Implemented a new lookup api, `resolv_lookup2()`, which provides
more information about the state of the record(error, expired,
looking-up, etc.).
About mDNS/Bonjour Support
--------------------------
This patch adds basic support for mDNS/Bonjour, which allows you to
refer to the name of a device instead of its IP address. This is
incredibly convenient for IPv6 addresses because they tend to be very
long and difficult to remember. It is especially important for
link-local IPv6 addresses, since not all programs support the '%'
notation for indicating a network interface (required on systems with
more than one network interface to disambiguate).
In other words, instead of typing in this:
* `http://[fe80::58dc:d7ed:a644:628f%en1]/`
You can type this instead:
* `http://contiki.local/`
Huge improvement, no?
The convenience extends beyond that: this mechanism can be used for
nodes to talk to each other based on their human-readable names instead
of their IPv6 addresses. So instead of a switch on
`aaaa::58dc:d7ed:a644:628f` triggering an actuator on
`aaaa::ed26:19c1:4bd2:f95b`, `light-switch.local` can trigger the
actuator on `living-room-lights.local`.
What you need to do to be able to look up `.local` names on your
workstation depends on a few factors:
* Your machine needs to be able to send and receive multicast packets
to and from the LoWPAN. You can do this easily with the Jackdaw
firmware on an RZUSBStick. If you have a border router, you will need
it to bridge the mDNS multicast packets across the border.
* If you are using a Mac, you win. All Apple devices support mDNS
lookups.
* If you are using Windows, you can install Apple's Bonjour for Windows
package. (This may be already installed on your machine if you have
installed iTunes) After you install this you can easily do `.local`
lookups.
* If you are using a Unix machine, you can install Avahi.
The default hostname is set to `contiki.local.`. You can change the
hostname programmatically by calling `resolv_set_hostname()`. You can
change the default hostname by changing `CONTIKI_CONF_DEFAULT_HOSTNAME`.
You may disable mDNS support by setting `RESOLV_CONF_SUPPORTS_MDNS` to
`0`.
---------------------------------
core/net/resolv: `resolv_lookup2()` -> `resolv_lookup()`
Note that this patch should fix several `resolv_lookup()` bugs
that already existed. There were many cases where `resolv_lookup()`
was being called and the IP address ignored, but later code
assumed that the IP address had been fetched... ANYWAY, those
should be fixed now.
---------------------------------
examples/udp-ipv6: Updated client to use MDNS to lookup the server.
Also updated the Cooja regression test simulation.
2012-05-17 17:24:08 +02:00
|
|
|
if(resolv_lookup(s.host, NULL) != RESOLV_STATUS_CACHED) {
|
2006-06-18 00:41:10 +02:00
|
|
|
resolv_query(s.host);
|
|
|
|
}
|
2010-02-01 20:44:30 +01:00
|
|
|
#endif /* UIP_UDP */
|
2006-06-18 00:41:10 +02:00
|
|
|
webclient_get(s.host, s.port, s.file);
|
2015-06-05 15:44:09 +02:00
|
|
|
break;
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|