diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2007-09-15 22:45:45 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2007-09-15 22:45:45 +0000 |
commit | 42027d080b72b8198072e7dc3933d8b70b6b40a5 (patch) | |
tree | d471d699ed674cbc615d89cd830e83c1b61d9eea /nuttx/netutils | |
parent | 8bd136d2202c6706ac2166a31b90f1ec18139dea (diff) | |
download | px4-nuttx-42027d080b72b8198072e7dc3933d8b70b6b40a5.tar.gz px4-nuttx-42027d080b72b8198072e7dc3933d8b70b6b40a5.tar.bz2 px4-nuttx-42027d080b72b8198072e7dc3933d8b70b6b40a5.zip |
Add basic structure to support multiple network interfaces
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@343 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/netutils')
-rw-r--r-- | nuttx/netutils/telnetd/telnetd.c | 429 | ||||
-rw-r--r-- | nuttx/netutils/webclient/webclient.c | 38 |
2 files changed, 242 insertions, 225 deletions
diff --git a/nuttx/netutils/telnetd/telnetd.c b/nuttx/netutils/telnetd/telnetd.c index 39a6f0f10..6d4ba2e7b 100644 --- a/nuttx/netutils/telnetd/telnetd.c +++ b/nuttx/netutils/telnetd/telnetd.c @@ -1,4 +1,5 @@ -/* +/* netutils/telnetd/telnetd.c + * * Copyright (c) 2003, Adam Dunkels. * All rights reserved. * @@ -32,6 +33,7 @@ #include <string.h> #include <net/uip/uip.h> +#include <net/uip/uip-arch.h> #include "telnetd.h" #include "shell.h" @@ -55,278 +57,291 @@ static struct telnetd_state s; #define TELNET_DO 253 #define TELNET_DONT 254 -/*---------------------------------------------------------------------------*/ -void -shell_quit(char *str) +void shell_quit(char *str) { s.state = STATE_CLOSE; } -/*---------------------------------------------------------------------------*/ -static void -sendline(char *line) +static void sendline(char *line) { - static unsigned int i; + unsigned int i; - for(i = 0; i < TELNETD_CONF_NUMLINES; ++i) { - if(s.lines[i] == NULL) { - s.lines[i] = line; - break; + for (i = 0; i < TELNETD_CONF_NUMLINES; ++i) + { + if (s.lines[i] == NULL) + { + s.lines[i] = line; + break; + } + } + if (i == TELNETD_CONF_NUMLINES) + { + free(line); } - } - if(i == TELNETD_CONF_NUMLINES) { - free(line); - } } -/*---------------------------------------------------------------------------*/ -void -shell_prompt(char *str) + +void shell_prompt(char *str) { char *line; line = (char*)malloc(TELNETD_CONF_LINELEN); - if(line != NULL) { - strncpy(line, str, TELNETD_CONF_LINELEN); - /* petsciiconv_toascii(line, TELNETD_CONF_LINELEN);*/ - sendline(line); - } + if (line != NULL) + { + strncpy(line, str, TELNETD_CONF_LINELEN); + sendline(line); + } } -/*---------------------------------------------------------------------------*/ -void -shell_output(char *str1, char *str2) + +void shell_output(char *str1, char *str2) { - static unsigned len; + unsigned len; char *line; line = (char*)malloc(TELNETD_CONF_LINELEN); - if(line != NULL) { - len = strlen(str1); - strncpy(line, str1, TELNETD_CONF_LINELEN); - if(len < TELNETD_CONF_LINELEN) { - strncpy(line + len, str2, TELNETD_CONF_LINELEN - len); - } - len = strlen(line); - if(len < TELNETD_CONF_LINELEN - 2) { - line[len] = ISO_cr; - line[len+1] = ISO_nl; - line[len+2] = 0; + if (line != NULL) + { + len = strlen(str1); + strncpy(line, str1, TELNETD_CONF_LINELEN); + if (len < TELNETD_CONF_LINELEN) + { + strncpy(line + len, str2, TELNETD_CONF_LINELEN - len); + } + len = strlen(line); + if (len < TELNETD_CONF_LINELEN - 2) + { + line[len] = ISO_cr; + line[len+1] = ISO_nl; + line[len+2] = 0; + } + sendline(line); } - /* petsciiconv_toascii(line, TELNETD_CONF_LINELEN);*/ - sendline(line); - } } -/*---------------------------------------------------------------------------*/ -void -telnetd_init(void) + +void telnetd_init(void) { uip_listen(HTONS(23)); shell_init(); } -/*---------------------------------------------------------------------------*/ -static void -acked(void) + +static void acked(void) { - static unsigned int i; - - while(s.numsent > 0) { - free(s.lines[0]); - for(i = 1; i < TELNETD_CONF_NUMLINES; ++i) { - s.lines[i - 1] = s.lines[i]; + unsigned int i; + + while(s.numsent > 0) + { + free(s.lines[0]); + for (i = 1; i < TELNETD_CONF_NUMLINES; ++i) + { + s.lines[i - 1] = s.lines[i]; + } + s.lines[TELNETD_CONF_NUMLINES - 1] = NULL; + --s.numsent; } - s.lines[TELNETD_CONF_NUMLINES - 1] = NULL; - --s.numsent; - } } -/*---------------------------------------------------------------------------*/ -static void -senddata(void) + +static void senddata(struct uip_driver_s *dev) { - static char *bufptr, *lineptr; - static int buflen, linelen; - - bufptr = uip_appdata; + char *bufptr, *lineptr; + int buflen, linelen; + + bufptr = (char*)dev->d_appdata; buflen = 0; - for(s.numsent = 0; s.numsent < TELNETD_CONF_NUMLINES && - s.lines[s.numsent] != NULL ; ++s.numsent) { - lineptr = s.lines[s.numsent]; - linelen = strlen(lineptr); - if(linelen > TELNETD_CONF_LINELEN) { - linelen = TELNETD_CONF_LINELEN; - } - if(buflen + linelen < uip_mss()) { - memcpy(bufptr, lineptr, linelen); - bufptr += linelen; - buflen += linelen; - } else { - break; + for (s.numsent = 0; + s.numsent < TELNETD_CONF_NUMLINES && s.lines[s.numsent] != NULL; + ++s.numsent) + { + lineptr = s.lines[s.numsent]; + linelen = strlen(lineptr); + if (linelen > TELNETD_CONF_LINELEN) + { + linelen = TELNETD_CONF_LINELEN; + } + if (buflen + linelen < uip_mss()) + { + memcpy(bufptr, lineptr, linelen); + bufptr += linelen; + buflen += linelen; + } + else + { + break; + } } - } - uip_send(uip_appdata, buflen); + uip_send(dev, dev->d_appdata, buflen); } -/*---------------------------------------------------------------------------*/ -static void -closed(void) + +static void closed(void) { - static unsigned int i; - - for(i = 0; i < TELNETD_CONF_NUMLINES; ++i) { - if(s.lines[i] != NULL) { - free(s.lines[i]); + unsigned int i; + + for (i = 0; i < TELNETD_CONF_NUMLINES; ++i) + { + if (s.lines[i] != NULL) + { + free(s.lines[i]); + } } - } } -/*---------------------------------------------------------------------------*/ -static void -get_char(uint8 c) + +static void get_char(uint8 c) { - if(c == ISO_cr) { + if (c == ISO_cr) + { return; } - + s.buf[(int)s.bufptr] = c; - if(s.buf[(int)s.bufptr] == ISO_nl || - s.bufptr == sizeof(s.buf) - 1) { - if(s.bufptr > 0) { - s.buf[(int)s.bufptr] = 0; - /* petsciiconv_topetscii(s.buf, TELNETD_CONF_LINELEN);*/ + if (s.buf[(int)s.bufptr] == ISO_nl || s.bufptr == sizeof(s.buf) - 1) + { + if (s.bufptr > 0) + { + s.buf[(int)s.bufptr] = 0; + } + shell_input(s.buf); + s.bufptr = 0; + } + else + { + ++s.bufptr; } - shell_input(s.buf); - s.bufptr = 0; - } else { - ++s.bufptr; - } } -/*---------------------------------------------------------------------------*/ -static void -sendopt(uint8 option, uint8 value) + +static void sendopt(uint8 option, uint8 value) { char *line; line = (char*)malloc(TELNETD_CONF_LINELEN); - if(line != NULL) { - line[0] = TELNET_IAC; - line[1] = option; - line[2] = value; - line[3] = 0; - sendline(line); - } + if (line != NULL) + { + line[0] = TELNET_IAC; + line[1] = option; + line[2] = value; + line[3] = 0; + sendline(line); + } } -/*---------------------------------------------------------------------------*/ -static void -newdata(void) + +static void newdata(struct uip_driver_s *dev) { uint16 len; uint8 c; char *dataptr; - - - len = uip_datalen(); - dataptr = (char *)uip_appdata; - - while(len > 0 && s.bufptr < sizeof(s.buf)) { - c = *dataptr; - ++dataptr; - --len; - switch(s.state) { - case STATE_IAC: - if(c == TELNET_IAC) { - get_char(c); - s.state = STATE_NORMAL; - } else { - switch(c) { - case TELNET_WILL: - s.state = STATE_WILL; - break; - case TELNET_WONT: - s.state = STATE_WONT; - break; - case TELNET_DO: - s.state = STATE_DO; - break; - case TELNET_DONT: - s.state = STATE_DONT; - break; - default: - s.state = STATE_NORMAL; - break; - } - } - break; - case STATE_WILL: - /* Reply with a DONT */ - sendopt(TELNET_DONT, c); - s.state = STATE_NORMAL; - break; - - case STATE_WONT: - /* Reply with a DONT */ - sendopt(TELNET_DONT, c); - s.state = STATE_NORMAL; - break; - case STATE_DO: - /* Reply with a WONT */ - sendopt(TELNET_WONT, c); - s.state = STATE_NORMAL; - break; - case STATE_DONT: - /* Reply with a WONT */ - sendopt(TELNET_WONT, c); - s.state = STATE_NORMAL; - break; - case STATE_NORMAL: - if(c == TELNET_IAC) { - s.state = STATE_IAC; - } else { - get_char(c); - } - break; + + len = uip_datalen(dev); + dataptr = (char *)dev->d_appdata; + + while(len > 0 && s.bufptr < sizeof(s.buf))\ + { + c = *dataptr; + ++dataptr; + --len; + switch(s.state) + { + case STATE_IAC: + if (c == TELNET_IAC) + { + get_char(c); + s.state = STATE_NORMAL; + } + else + { + switch (c) + { + case TELNET_WILL: + s.state = STATE_WILL; + break; + case TELNET_WONT: + s.state = STATE_WONT; + break; + case TELNET_DO: + s.state = STATE_DO; + break; + case TELNET_DONT: + s.state = STATE_DONT; + break; + default: + s.state = STATE_NORMAL; + break; + } + } + break; + case STATE_WILL: + /* Reply with a DONT */ + sendopt(TELNET_DONT, c); + s.state = STATE_NORMAL; + break; + + case STATE_WONT: + /* Reply with a DONT */ + sendopt(TELNET_DONT, c); + s.state = STATE_NORMAL; + break; + case STATE_DO: + /* Reply with a WONT */ + sendopt(TELNET_WONT, c); + s.state = STATE_NORMAL; + break; + case STATE_DONT: + /* Reply with a WONT */ + sendopt(TELNET_WONT, c); + s.state = STATE_NORMAL; + break; + case STATE_NORMAL: + if (c == TELNET_IAC) + { + s.state = STATE_IAC; + } + else + { + get_char(c); + } + break; + } } - } } /* This function is called by the UIP interrupt handling logic whenevent an * event of interest occurs. */ -void uip_interrupt_event(void) +void uip_interrupt_event(struct uip_driver_s *dev, void *private) { #warning OBSOLETE -- needs to be redesigned - static unsigned int i; - if(uip_connected()) { - /* tcp_markconn(uip_conn, &s);*/ - for(i = 0; i < TELNETD_CONF_NUMLINES; ++i) { - s.lines[i] = NULL; - } - s.bufptr = 0; - s.state = STATE_NORMAL; + unsigned int i; + if (uip_connected()) + { + for (i = 0; i < TELNETD_CONF_NUMLINES; ++i) + { + s.lines[i] = NULL; + } + s.bufptr = 0; + s.state = STATE_NORMAL; - shell_start(); + shell_start(); } - if(s.state == STATE_CLOSE) { + if (s.state == STATE_CLOSE) + { s.state = STATE_NORMAL; uip_close(); return; } - - if(uip_closed() || - uip_aborted() || - uip_timedout()) { + + if (uip_closed() || uip_aborted() || uip_timedout()) + { closed(); } - - if(uip_acked()) { + + if (uip_acked()) + { acked(); } - - if(uip_newdata()) { - newdata(); + + if (uip_newdata()) + { + newdata(dev); } - - if(uip_rexmit() || - uip_newdata() || - uip_acked() || - uip_connected() || - uip_poll()) { - senddata(); + + if (uip_rexmit() || uip_newdata() || uip_acked() || uip_connected() || uip_poll()) + { + senddata(dev); } } -/*---------------------------------------------------------------------------*/ diff --git a/nuttx/netutils/webclient/webclient.c b/nuttx/netutils/webclient/webclient.c index e26d5017f..9b900ef66 100644 --- a/nuttx/netutils/webclient/webclient.c +++ b/nuttx/netutils/webclient/webclient.c @@ -57,6 +57,8 @@ #include <net/uip/resolv.h> #include "uiplib/uiplib.h" +#include <net/uip/uip-arch.h> + #include "webclient.h" #define WEBCLIENT_TIMEOUT 100 @@ -178,14 +180,14 @@ static char *copy_string(char *dest, const char *src, int len) return dest + len; } -static void senddata(void) +static void senddata(struct uip_driver_s *dev) { uint16 len; char *getrequest; char *cptr; if (s.getrequestleft > 0) { - cptr = getrequest = (char *)uip_appdata; + cptr = getrequest = (char *)dev->d_appdata; cptr = copy_string(cptr, http_get, sizeof(http_get) - 1); cptr = copy_string(cptr, s.file, strlen(s.file)); @@ -204,7 +206,7 @@ static void senddata(void) len = s.getrequestleft > uip_mss()? uip_mss(): s.getrequestleft; - uip_send(&(getrequest[s.getrequestptr]), len); + uip_send(dev, &(getrequest[s.getrequestptr]), len); } } @@ -221,15 +223,15 @@ static void acked(void) } } -static uint16 parse_statusline(uint16 len) +static uint16 parse_statusline(struct uip_driver_s *dev, uint16 len) { char *cptr; while(len > 0 && s.httpheaderlineptr < sizeof(s.httpheaderline)) { - char *pappdata = (char*)uip_appdata; + char *pappdata = (char*)dev->d_appdata; s.httpheaderline[s.httpheaderlineptr] = *pappdata++; - uip_appdata = (void*)pappdata; + dev->d_appdata = (void*)pappdata; len--; if (s.httpheaderline[s.httpheaderlineptr] == ISO_nl) @@ -301,16 +303,16 @@ static char casecmp(char *str1, const char *str2, char len) return 0; } -static uint16 parse_headers(uint16 len) +static uint16 parse_headers(struct uip_driver_s *dev, uint16 len) { char *cptr; static unsigned char i; while(len > 0 && s.httpheaderlineptr < sizeof(s.httpheaderline)) { - char *pappdata = (char*)uip_appdata; + char *pappdata = (char*)dev->d_appdata; s.httpheaderline[s.httpheaderlineptr] = *pappdata++; - uip_appdata = (void*)pappdata; + dev->d_appdata = (void*)pappdata; len--; if (s.httpheaderline[s.httpheaderlineptr] == ISO_nl) @@ -378,23 +380,23 @@ static uint16 parse_headers(uint16 len) return len; } -static void newdata(void) +static void newdata(struct uip_driver_s *dev) { uint16 len; - len = uip_datalen(); + len = uip_datalen(dev); if (s.state == WEBCLIENT_STATE_STATUSLINE) { - len = parse_statusline(len); + len = parse_statusline(dev, len); } if (s.state == WEBCLIENT_STATE_HEADERS && len > 0) { - len = parse_headers(len); + len = parse_headers(dev, len); } if (len > 0 && s.state == WEBCLIENT_STATE_DATA && s.httpflag != HTTPFLAG_MOVED) { - webclient_datahandler((char *)uip_appdata, len); + webclient_datahandler((char *)dev->d_appdata, len); } } @@ -402,14 +404,14 @@ static void newdata(void) * event of interest occurs. */ -void uip_interrupt_event(void) +void uip_interrupt_event(struct uip_driver_s *dev) { #warning OBSOLETE -- needs to be redesigned if (uip_connected()) { s.timer = 0; s.state = WEBCLIENT_STATE_STATUSLINE; - senddata(); + senddata(dev); webclient_connected(); return; } @@ -440,12 +442,12 @@ void uip_interrupt_event(void) if (uip_newdata()) { s.timer = 0; - newdata(); + newdata(dev); } if (uip_rexmit() || uip_newdata() || uip_acked()) { - senddata(); + senddata(dev); } else if (uip_poll()) { |