2007-12-02 19:18:59 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* examples/nsh/nsh_telnetd.c
|
|
|
|
*
|
2010-08-05 23:44:36 +02:00
|
|
|
* Copyright (C) 2007-2010 Gregory Nutt. All rights reserved.
|
2007-12-02 19:18:59 +01:00
|
|
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
|
|
|
*
|
|
|
|
* This is a leverage of similar logic from uIP:
|
|
|
|
*
|
|
|
|
* Author: Adam Dunkels <adam@sics.se>
|
|
|
|
* Copyright (c) 2003, Adam Dunkels.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 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 with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``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 INSTITUTE OR CONTRIBUTORS 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 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2009-12-15 20:00:40 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2007-12-02 19:18:59 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <pthread.h>
|
2008-08-30 17:43:32 +02:00
|
|
|
#include <semaphore.h>
|
2008-08-16 22:52:06 +02:00
|
|
|
#include <assert.h>
|
2008-08-30 17:43:32 +02:00
|
|
|
#include <errno.h>
|
2007-12-02 19:18:59 +01:00
|
|
|
#include <debug.h>
|
|
|
|
|
2007-12-10 14:28:01 +01:00
|
|
|
#include <net/if.h>
|
2007-12-02 19:18:59 +01:00
|
|
|
#include <net/uip/uip-lib.h>
|
2010-08-05 23:44:36 +02:00
|
|
|
#if defined(CONFIG_EXAMPLES_NSH_DHCPC)
|
2010-08-06 18:14:02 +02:00
|
|
|
# include <net/uip/resolv.h>
|
2010-08-05 23:44:36 +02:00
|
|
|
# include <net/uip/dhcpc.h>
|
|
|
|
#endif
|
2007-12-02 19:18:59 +01:00
|
|
|
|
|
|
|
#include "nsh.h"
|
|
|
|
|
2008-08-30 17:43:32 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_NSH_TELNET
|
|
|
|
|
2007-12-02 19:18:59 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define ISO_nl 0x0a
|
|
|
|
#define ISO_cr 0x0d
|
|
|
|
|
|
|
|
#define STATE_NORMAL 0
|
|
|
|
#define STATE_IAC 1
|
|
|
|
#define STATE_WILL 2
|
|
|
|
#define STATE_WONT 3
|
|
|
|
#define STATE_DO 4
|
|
|
|
#define STATE_DONT 5
|
|
|
|
#define STATE_CLOSE 6
|
|
|
|
|
|
|
|
#define TELNET_IAC 255
|
|
|
|
#define TELNET_WILL 251
|
|
|
|
#define TELNET_WONT 252
|
|
|
|
#define TELNET_DO 253
|
|
|
|
#define TELNET_DONT 254
|
|
|
|
|
2008-09-07 15:42:55 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_NSH_TELNETD_DUMPBUFFER
|
|
|
|
# define nsh_telnetdump(vtbl,msg,buf,nb) nsh_dumpbuffer(vtbl,msg,buf,nb)
|
|
|
|
#else
|
|
|
|
# define nsh_telnetdump(vtbl,msg,buf,nb)
|
|
|
|
#endif
|
|
|
|
|
2007-12-02 19:18:59 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-08-16 22:52:06 +02:00
|
|
|
struct telnetio_s
|
|
|
|
{
|
2009-12-15 20:00:40 +01:00
|
|
|
sem_t tio_sem;
|
|
|
|
int tio_sockfd;
|
|
|
|
uint8_t tio_bufndx;
|
|
|
|
uint8_t tio_state;
|
|
|
|
char tio_inbuffer[CONFIG_EXAMPLES_NSH_IOBUFFER_SIZE];
|
2008-08-16 22:52:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct redirect_s
|
|
|
|
{
|
|
|
|
int rd_fd; /* Re-direct file descriptor */
|
|
|
|
FILE *rd_stream; /* Re-direct stream */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct telnetsave_s
|
|
|
|
{
|
2009-12-15 20:00:40 +01:00
|
|
|
bool ts_redirected;
|
2008-08-16 22:52:06 +02:00
|
|
|
union
|
|
|
|
{
|
|
|
|
struct telnetio_s *tn;
|
|
|
|
struct redirect_s rd;
|
|
|
|
} u;
|
|
|
|
};
|
|
|
|
|
2007-12-02 19:18:59 +01:00
|
|
|
struct telnetd_s
|
|
|
|
{
|
2008-08-16 22:52:06 +02:00
|
|
|
struct nsh_vtbl_s tn_vtbl;
|
2009-12-15 20:00:40 +01:00
|
|
|
uint16_t tn_sndlen;
|
|
|
|
bool tn_redirected;
|
2008-08-16 22:52:06 +02:00
|
|
|
union
|
|
|
|
{
|
|
|
|
struct telnetio_s *tn;
|
|
|
|
struct redirect_s rd;
|
|
|
|
} u;
|
2008-09-01 17:56:03 +02:00
|
|
|
char tn_outbuffer[CONFIG_EXAMPLES_NSH_IOBUFFER_SIZE];
|
2008-08-16 22:52:06 +02:00
|
|
|
char tn_cmd[CONFIG_EXAMPLES_NSH_LINELEN];
|
2007-12-02 19:18:59 +01:00
|
|
|
};
|
|
|
|
|
2008-08-16 20:39:46 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-09-02 14:35:30 +02:00
|
|
|
#ifndef CONFIG_EXAMPLES_NSH_DISABLEBG
|
2008-09-01 17:56:03 +02:00
|
|
|
static void tio_semtake(struct telnetio_s *tio);
|
2008-08-16 20:39:46 +02:00
|
|
|
static FAR struct nsh_vtbl_s *nsh_telnetclone(FAR struct nsh_vtbl_s *vtbl);
|
2008-09-02 14:35:30 +02:00
|
|
|
#endif
|
2008-08-16 20:39:46 +02:00
|
|
|
static void nsh_telnetrelease(FAR struct nsh_vtbl_s *vtbl);
|
|
|
|
static int nsh_telnetoutput(FAR struct nsh_vtbl_s *vtbl, const char *fmt, ...);
|
2008-08-16 22:52:06 +02:00
|
|
|
static int nsh_redirectoutput(FAR struct nsh_vtbl_s *vtbl, const char *fmt, ...);
|
2008-08-16 20:39:46 +02:00
|
|
|
static FAR char *nsh_telnetlinebuffer(FAR struct nsh_vtbl_s *vtbl);
|
2009-12-15 20:00:40 +01:00
|
|
|
static void nsh_telnetredirect(FAR struct nsh_vtbl_s *vtbl, int fd, FAR uint8_t *save);
|
|
|
|
static void nsh_telnetundirect(FAR struct nsh_vtbl_s *vtbl, FAR uint8_t *save);
|
2008-08-16 22:52:06 +02:00
|
|
|
static void nsh_telnetexit(FAR struct nsh_vtbl_s *vtbl);
|
2008-08-16 20:39:46 +02:00
|
|
|
|
2007-12-02 19:18:59 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-08-30 17:43:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: tio_semtake
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void tio_semtake(struct telnetio_s *tio)
|
|
|
|
{
|
2008-09-01 17:56:03 +02:00
|
|
|
/* Take the semaphore (perhaps waiting) */
|
2008-08-30 17:43:32 +02:00
|
|
|
|
2008-09-01 17:56:03 +02:00
|
|
|
while (sem_wait(&tio->tio_sem) != 0)
|
2008-08-30 17:43:32 +02:00
|
|
|
{
|
2008-09-01 17:56:03 +02:00
|
|
|
/* The only case that an error should occur here is if the wait was
|
|
|
|
* awakened by a signal.
|
|
|
|
*/
|
2008-08-30 17:43:32 +02:00
|
|
|
|
2008-09-01 17:56:03 +02:00
|
|
|
ASSERT(errno == EINTR);
|
2008-08-30 17:43:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tio_semgive
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-09-01 17:56:03 +02:00
|
|
|
#define tio_semgive(tio) ASSERT(sem_post(&tio->tio_sem) == 0)
|
2008-08-30 17:43:32 +02:00
|
|
|
|
2008-08-16 20:39:46 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_allocstruct
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-08-16 22:52:06 +02:00
|
|
|
static FAR struct telnetd_s *nsh_allocstruct(void)
|
2008-08-16 20:39:46 +02:00
|
|
|
{
|
2008-08-30 17:43:32 +02:00
|
|
|
struct telnetd_s *pstate = (struct telnetd_s *)zalloc(sizeof(struct telnetd_s));
|
2008-08-16 20:39:46 +02:00
|
|
|
if (pstate)
|
|
|
|
{
|
2008-09-02 14:35:30 +02:00
|
|
|
#ifndef CONFIG_EXAMPLES_NSH_DISABLEBG
|
2008-08-16 22:52:06 +02:00
|
|
|
pstate->tn_vtbl.clone = nsh_telnetclone;
|
|
|
|
pstate->tn_vtbl.release = nsh_telnetrelease;
|
2008-09-02 14:35:30 +02:00
|
|
|
#endif
|
2008-08-16 22:52:06 +02:00
|
|
|
pstate->tn_vtbl.output = nsh_telnetoutput;
|
|
|
|
pstate->tn_vtbl.linebuffer = nsh_telnetlinebuffer;
|
|
|
|
pstate->tn_vtbl.redirect = nsh_telnetredirect;
|
|
|
|
pstate->tn_vtbl.undirect = nsh_telnetundirect;
|
|
|
|
pstate->tn_vtbl.exit = nsh_telnetexit;
|
2008-08-16 20:39:46 +02:00
|
|
|
}
|
|
|
|
return pstate;
|
|
|
|
}
|
|
|
|
|
2008-08-16 22:52:06 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_openifnotopen
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int nsh_openifnotopen(struct telnetd_s *pstate)
|
|
|
|
{
|
|
|
|
struct redirect_s *rd = &pstate->u.rd;
|
|
|
|
|
|
|
|
/* The stream is open in a lazy fashion. This is done because the file
|
|
|
|
* descriptor may be opened on a different task than the stream.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!rd->rd_stream)
|
|
|
|
{
|
|
|
|
rd->rd_stream = fdopen(rd->rd_fd, "w");
|
|
|
|
if (!rd->rd_stream)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_closeifnotclosed
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void nsh_closeifnotclosed(struct telnetd_s *pstate)
|
|
|
|
{
|
|
|
|
struct redirect_s *rd = &pstate->u.rd;
|
|
|
|
|
|
|
|
if (rd->rd_stream == stdout)
|
|
|
|
{
|
|
|
|
fflush(stdout);
|
|
|
|
rd->rd_fd = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (rd->rd_stream)
|
|
|
|
{
|
|
|
|
fflush(rd->rd_stream);
|
|
|
|
fclose(rd->rd_stream);
|
|
|
|
}
|
|
|
|
else if (rd->rd_fd >= 0 && rd->rd_fd != 1)
|
|
|
|
{
|
|
|
|
close(rd->rd_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
rd->rd_fd = -1;
|
|
|
|
rd->rd_stream = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-02 19:18:59 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_putchar
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Add another parsed character to the TELNET command string
|
|
|
|
*
|
2008-08-30 17:43:32 +02:00
|
|
|
* Assumption:
|
|
|
|
* Caller holds TIO semaphore
|
|
|
|
*
|
2007-12-02 19:18:59 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2009-12-15 20:00:40 +01:00
|
|
|
static void nsh_putchar(struct telnetd_s *pstate, uint8_t ch)
|
2007-12-02 19:18:59 +01:00
|
|
|
{
|
2008-08-16 22:52:06 +02:00
|
|
|
struct telnetio_s *tio = pstate->u.tn;
|
|
|
|
|
2007-12-02 19:18:59 +01:00
|
|
|
/* Ignore carriage returns */
|
|
|
|
|
|
|
|
if (ch == ISO_cr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add all other characters to the cmd buffer */
|
|
|
|
|
2008-08-16 22:52:06 +02:00
|
|
|
pstate->tn_cmd[tio->tio_bufndx] = ch;
|
2007-12-02 19:18:59 +01:00
|
|
|
|
|
|
|
/* If a newline was added or if the buffer is full, then process it now */
|
|
|
|
|
2008-08-16 22:52:06 +02:00
|
|
|
if (ch == ISO_nl || tio->tio_bufndx == (CONFIG_EXAMPLES_NSH_LINELEN - 1))
|
2007-12-02 19:18:59 +01:00
|
|
|
{
|
2008-08-29 23:14:45 +02:00
|
|
|
pstate->tn_cmd[tio->tio_bufndx] = '\0';
|
2008-09-07 15:42:55 +02:00
|
|
|
nsh_telnetdump(&pstate->tn_vtbl, "TELNET CMD",
|
2009-12-15 20:00:40 +01:00
|
|
|
(uint8_t*)pstate->tn_cmd, strlen(pstate->tn_cmd));
|
2008-08-16 22:52:06 +02:00
|
|
|
nsh_parse(&pstate->tn_vtbl, pstate->tn_cmd);
|
|
|
|
tio->tio_bufndx = 0;
|
2007-12-02 19:18:59 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-08-16 22:52:06 +02:00
|
|
|
tio->tio_bufndx++;
|
|
|
|
vdbg("Add '%c', bufndx=%d\n", ch, tio->tio_bufndx);
|
2007-12-02 19:18:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_sendopt
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2009-12-15 20:00:40 +01:00
|
|
|
static void nsh_sendopt(struct telnetd_s *pstate, uint8_t option, uint8_t value)
|
2007-12-02 19:18:59 +01:00
|
|
|
{
|
2008-08-16 22:52:06 +02:00
|
|
|
struct telnetio_s *tio = pstate->u.tn;
|
2009-12-15 20:00:40 +01:00
|
|
|
uint8_t optbuf[4];
|
2007-12-02 19:18:59 +01:00
|
|
|
optbuf[0] = TELNET_IAC;
|
|
|
|
optbuf[1] = option;
|
|
|
|
optbuf[2] = value;
|
|
|
|
optbuf[3] = 0;
|
|
|
|
|
2008-09-07 15:42:55 +02:00
|
|
|
nsh_telnetdump(&pstate->tn_vtbl, "Send optbuf", optbuf, 4);
|
2008-09-01 17:56:03 +02:00
|
|
|
tio_semtake(tio); /* Only one call to send at a time */
|
2008-08-16 22:52:06 +02:00
|
|
|
if (send(tio->tio_sockfd, optbuf, 4, 0) < 0)
|
2007-12-02 19:18:59 +01:00
|
|
|
{
|
2008-08-30 17:43:32 +02:00
|
|
|
dbg("[%d] Failed to send TELNET_IAC: %d\n", tio->tio_sockfd, errno);
|
2007-12-02 19:18:59 +01:00
|
|
|
}
|
2008-09-01 17:56:03 +02:00
|
|
|
tio_semgive(tio);
|
2007-12-02 19:18:59 +01:00
|
|
|
}
|
|
|
|
|
2007-12-10 14:28:01 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_flush
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Dump the buffered output info.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void nsh_flush(FAR struct telnetd_s *pstate)
|
|
|
|
{
|
2008-08-16 22:52:06 +02:00
|
|
|
struct telnetio_s *tio = pstate->u.tn;
|
|
|
|
|
2008-09-01 17:56:03 +02:00
|
|
|
if (pstate->tn_sndlen > 0)
|
2007-12-10 14:28:01 +01:00
|
|
|
{
|
2008-09-07 15:42:55 +02:00
|
|
|
nsh_telnetdump(&pstate->tn_vtbl, "Shell output",
|
2009-12-15 20:00:40 +01:00
|
|
|
(uint8_t*)pstate->tn_outbuffer, pstate->tn_sndlen);
|
2008-09-01 17:56:03 +02:00
|
|
|
tio_semtake(tio); /* Only one call to send at a time */
|
|
|
|
if (send(tio->tio_sockfd, pstate->tn_outbuffer, pstate->tn_sndlen, 0) < 0)
|
2007-12-10 14:28:01 +01:00
|
|
|
{
|
2008-08-30 17:43:32 +02:00
|
|
|
dbg("[%d] Failed to send response: %d\n", tio->tio_sockfd, errno);
|
2007-12-10 14:28:01 +01:00
|
|
|
}
|
2008-09-01 17:56:03 +02:00
|
|
|
tio_semgive(tio);
|
2007-12-10 14:28:01 +01:00
|
|
|
}
|
2008-09-01 17:56:03 +02:00
|
|
|
pstate->tn_sndlen = 0;
|
2007-12-10 14:28:01 +01:00
|
|
|
}
|
|
|
|
|
2007-12-02 19:18:59 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_receive
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Process a received TELENET buffer
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int nsh_receive(struct telnetd_s *pstate, size_t len)
|
|
|
|
{
|
2008-08-16 22:52:06 +02:00
|
|
|
struct telnetio_s *tio = pstate->u.tn;
|
2008-09-01 17:56:03 +02:00
|
|
|
char *ptr = tio->tio_inbuffer;
|
2009-12-15 20:00:40 +01:00
|
|
|
uint8_t ch;
|
2007-12-02 19:18:59 +01:00
|
|
|
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
ch = *ptr++;
|
|
|
|
len--;
|
|
|
|
|
2008-08-16 22:52:06 +02:00
|
|
|
vdbg("ch=%02x state=%d\n", ch, tio->tio_state);
|
|
|
|
switch (tio->tio_state)
|
2007-12-02 19:18:59 +01:00
|
|
|
{
|
|
|
|
case STATE_IAC:
|
|
|
|
if (ch == TELNET_IAC)
|
|
|
|
{
|
|
|
|
nsh_putchar(pstate, ch);
|
2008-08-16 22:52:06 +02:00
|
|
|
tio->tio_state = STATE_NORMAL;
|
2007-12-02 19:18:59 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case TELNET_WILL:
|
2008-08-16 22:52:06 +02:00
|
|
|
tio->tio_state = STATE_WILL;
|
2007-12-02 19:18:59 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TELNET_WONT:
|
2008-08-16 22:52:06 +02:00
|
|
|
tio->tio_state = STATE_WONT;
|
2007-12-02 19:18:59 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TELNET_DO:
|
2008-08-16 22:52:06 +02:00
|
|
|
tio->tio_state = STATE_DO;
|
2007-12-02 19:18:59 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TELNET_DONT:
|
2008-08-16 22:52:06 +02:00
|
|
|
tio->tio_state = STATE_DONT;
|
2007-12-02 19:18:59 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2008-08-16 22:52:06 +02:00
|
|
|
tio->tio_state = STATE_NORMAL;
|
2007-12-02 19:18:59 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_WILL:
|
|
|
|
/* Reply with a DONT */
|
|
|
|
|
|
|
|
nsh_sendopt(pstate, TELNET_DONT, ch);
|
2008-08-16 22:52:06 +02:00
|
|
|
tio->tio_state = STATE_NORMAL;
|
2007-12-02 19:18:59 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_WONT:
|
|
|
|
/* Reply with a DONT */
|
|
|
|
|
|
|
|
nsh_sendopt(pstate, TELNET_DONT, ch);
|
2008-08-16 22:52:06 +02:00
|
|
|
tio->tio_state = STATE_NORMAL;
|
2007-12-02 19:18:59 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_DO:
|
|
|
|
/* Reply with a WONT */
|
|
|
|
|
|
|
|
nsh_sendopt(pstate, TELNET_WONT, ch);
|
2008-08-16 22:52:06 +02:00
|
|
|
tio->tio_state = STATE_NORMAL;
|
2007-12-02 19:18:59 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_DONT:
|
|
|
|
/* Reply with a WONT */
|
|
|
|
|
|
|
|
nsh_sendopt(pstate, TELNET_WONT, ch);
|
2008-08-16 22:52:06 +02:00
|
|
|
tio->tio_state = STATE_NORMAL;
|
2007-12-02 19:18:59 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_NORMAL:
|
|
|
|
if (ch == TELNET_IAC)
|
|
|
|
{
|
2008-08-16 22:52:06 +02:00
|
|
|
tio->tio_state = STATE_IAC;
|
2007-12-02 19:18:59 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nsh_putchar(pstate, ch);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_connection
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Each time a new connection to port 23 is made, a new thread is created
|
|
|
|
* that begins at this entry point. There should be exactly one argument
|
|
|
|
* and it should be the socket descriptor (+1).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void *nsh_connection(void *arg)
|
|
|
|
{
|
2008-08-16 22:52:06 +02:00
|
|
|
struct telnetd_s *pstate = nsh_allocstruct();
|
2008-08-30 17:43:32 +02:00
|
|
|
struct telnetio_s *tio = (struct telnetio_s *)zalloc(sizeof(struct telnetio_s));
|
2009-03-14 16:46:31 +01:00
|
|
|
struct nsh_vtbl_s *vtbl = &pstate->tn_vtbl;
|
2008-08-16 22:52:06 +02:00
|
|
|
int sockfd = (int)arg;
|
|
|
|
int ret = ERROR;
|
2007-12-02 19:18:59 +01:00
|
|
|
|
|
|
|
dbg("[%d] Started\n", sockfd);
|
|
|
|
|
|
|
|
/* Verify that the state structure was successfully allocated */
|
|
|
|
|
2008-08-16 22:52:06 +02:00
|
|
|
if (pstate && tio)
|
2007-12-02 19:18:59 +01:00
|
|
|
{
|
|
|
|
/* Initialize the thread state structure */
|
|
|
|
|
2008-08-30 17:43:32 +02:00
|
|
|
sem_init(&tio->tio_sem, 0, 1);
|
2008-08-16 22:52:06 +02:00
|
|
|
tio->tio_sockfd = sockfd;
|
|
|
|
tio->tio_state = STATE_NORMAL;
|
|
|
|
pstate->u.tn = tio;
|
2007-12-02 19:18:59 +01:00
|
|
|
|
2007-12-10 14:28:01 +01:00
|
|
|
/* Output a greeting */
|
|
|
|
|
2009-03-14 16:46:31 +01:00
|
|
|
nsh_output(vtbl, g_nshgreeting);
|
2007-12-10 14:28:01 +01:00
|
|
|
|
2008-09-11 23:54:23 +02:00
|
|
|
/* Execute the startup script */
|
|
|
|
|
|
|
|
#if defined(CONFIG_EXAMPLES_NSH_ROMFSETC) && !defined(CONFIG_EXAMPLES_NSH_CONSOLE)
|
2009-03-14 16:46:31 +01:00
|
|
|
(void)nsh_script(vtbl, "init", NSH_INITPATH);
|
2008-09-11 23:54:23 +02:00
|
|
|
#endif
|
|
|
|
|
2007-12-02 19:18:59 +01:00
|
|
|
/* Loop processing each TELNET command */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Display the prompt string */
|
|
|
|
|
2009-03-14 16:46:31 +01:00
|
|
|
nsh_output(vtbl, g_nshprompt);
|
2007-12-10 14:28:01 +01:00
|
|
|
nsh_flush(pstate);
|
2007-12-02 19:18:59 +01:00
|
|
|
|
|
|
|
/* Read a buffer of data from the TELNET client */
|
|
|
|
|
2008-09-07 15:42:55 +02:00
|
|
|
ret = recv(tio->tio_sockfd, tio->tio_inbuffer,
|
|
|
|
CONFIG_EXAMPLES_NSH_IOBUFFER_SIZE, 0);
|
2007-12-02 19:18:59 +01:00
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Process the received TELNET data */
|
|
|
|
|
2009-03-14 16:46:31 +01:00
|
|
|
nsh_telnetdump(vtbl, "Received buffer",
|
2009-12-15 20:00:40 +01:00
|
|
|
(uint8_t*)tio->tio_inbuffer, ret);
|
2007-12-02 19:18:59 +01:00
|
|
|
ret = nsh_receive(pstate, ret);
|
|
|
|
}
|
|
|
|
}
|
2008-08-16 22:52:06 +02:00
|
|
|
while (ret >= 0 && tio->tio_state != STATE_CLOSE);
|
|
|
|
dbg("[%d] ret=%d tn.tio_state=%d\n", sockfd, ret, tio->tio_state);
|
2007-12-02 19:18:59 +01:00
|
|
|
|
|
|
|
/* End of command processing -- Clean up and exit */
|
2008-08-16 22:52:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Exit the task */
|
2007-12-02 19:18:59 +01:00
|
|
|
|
2008-08-16 22:52:06 +02:00
|
|
|
if (pstate)
|
|
|
|
{
|
2007-12-02 19:18:59 +01:00
|
|
|
free(pstate);
|
|
|
|
}
|
|
|
|
|
2008-08-16 22:52:06 +02:00
|
|
|
if (tio)
|
|
|
|
{
|
2008-08-30 17:43:32 +02:00
|
|
|
sem_destroy(&tio->tio_sem);
|
2008-08-16 22:52:06 +02:00
|
|
|
free(tio);
|
|
|
|
}
|
2007-12-02 19:18:59 +01:00
|
|
|
|
|
|
|
dbg("[%d] Exitting\n", sockfd);
|
|
|
|
close(sockfd);
|
2009-03-14 16:46:31 +01:00
|
|
|
return NULL;
|
2007-12-02 19:18:59 +01:00
|
|
|
}
|
|
|
|
|
2008-08-16 20:39:46 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_telnetoutput
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Print a string to the remote shell window.
|
|
|
|
*
|
|
|
|
* This function is implemented by the shell GUI / telnet server and
|
|
|
|
* can be called by the shell back-end to output a string in the
|
|
|
|
* shell window. The string is automatically appended with a linebreak.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int nsh_telnetoutput(FAR struct nsh_vtbl_s *vtbl, const char *fmt, ...)
|
|
|
|
{
|
2008-08-16 22:52:06 +02:00
|
|
|
struct telnetd_s *pstate = (struct telnetd_s *)vtbl;
|
2008-09-01 17:56:03 +02:00
|
|
|
int nbytes = pstate->tn_sndlen;
|
2008-08-16 22:52:06 +02:00
|
|
|
int len;
|
|
|
|
va_list ap;
|
2008-08-16 20:39:46 +02:00
|
|
|
|
|
|
|
/* Put the new info into the buffer. Here we are counting on the fact that
|
|
|
|
* no output strings will exceed CONFIG_EXAMPLES_NSH_LINELEN!
|
|
|
|
*/
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2008-09-01 17:56:03 +02:00
|
|
|
vsnprintf(&pstate->tn_outbuffer[nbytes],
|
2008-08-16 20:39:46 +02:00
|
|
|
(CONFIG_EXAMPLES_NSH_IOBUFFER_SIZE - 1) - nbytes, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
/* Get the size of the new string just added and the total size of
|
|
|
|
* buffered data
|
|
|
|
*/
|
|
|
|
|
2008-09-01 17:56:03 +02:00
|
|
|
len = strlen(&pstate->tn_outbuffer[nbytes]);
|
2008-08-16 20:39:46 +02:00
|
|
|
nbytes += len;
|
|
|
|
|
|
|
|
/* Expand any terminating \n to \r\n */
|
|
|
|
|
|
|
|
if (nbytes < (CONFIG_EXAMPLES_NSH_IOBUFFER_SIZE - 2) &&
|
2008-09-01 17:56:03 +02:00
|
|
|
pstate->tn_outbuffer[nbytes-1] == '\n')
|
2008-08-16 20:39:46 +02:00
|
|
|
{
|
2008-09-01 17:56:03 +02:00
|
|
|
pstate->tn_outbuffer[nbytes-1] = ISO_cr;
|
|
|
|
pstate->tn_outbuffer[nbytes] = ISO_nl;
|
|
|
|
pstate->tn_outbuffer[nbytes+1] = '\0';
|
2008-08-16 20:39:46 +02:00
|
|
|
nbytes++;
|
|
|
|
}
|
2008-09-01 17:56:03 +02:00
|
|
|
pstate->tn_sndlen = nbytes;
|
2008-08-16 20:39:46 +02:00
|
|
|
|
|
|
|
/* Flush to the network if the buffer does not have room for one more
|
|
|
|
* maximum length string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (nbytes > CONFIG_EXAMPLES_NSH_IOBUFFER_SIZE - CONFIG_EXAMPLES_NSH_LINELEN)
|
|
|
|
{
|
|
|
|
nsh_flush(pstate);
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2008-08-16 22:52:06 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_redirectoutput
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Print a string to the currently selected stream.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int nsh_redirectoutput(FAR struct nsh_vtbl_s *vtbl, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
FAR struct telnetd_s *pstate = (FAR struct telnetd_s *)vtbl;
|
|
|
|
va_list ap;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* The stream is open in a lazy fashion. This is done because the file
|
|
|
|
* descriptor may be opened on a different task than the stream. The
|
|
|
|
* actual open will then occur with the first output from the new task.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (nsh_openifnotopen(pstate) != 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
2008-08-30 17:43:32 +02:00
|
|
|
|
2008-08-16 22:52:06 +02:00
|
|
|
va_start(ap, fmt);
|
|
|
|
ret = vfprintf(pstate->u.rd.rd_stream, fmt, ap);
|
|
|
|
va_end(ap);
|
2008-08-30 17:43:32 +02:00
|
|
|
|
2008-08-16 22:52:06 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-08-16 20:39:46 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_telnetlinebuffer
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return a reference to the current line buffer
|
|
|
|
*
|
2008-08-16 22:52:06 +02:00
|
|
|
* ****************************************************************************/
|
2008-08-16 20:39:46 +02:00
|
|
|
|
|
|
|
static FAR char *nsh_telnetlinebuffer(FAR struct nsh_vtbl_s *vtbl)
|
|
|
|
{
|
|
|
|
struct telnetd_s *pstate = (struct telnetd_s *)vtbl;
|
|
|
|
return pstate->tn_cmd;
|
|
|
|
}
|
|
|
|
|
2008-08-16 22:52:06 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_telnetclone
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Make an independent copy of the vtbl
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-09-02 14:35:30 +02:00
|
|
|
#ifndef CONFIG_EXAMPLES_NSH_DISABLEBG
|
2008-08-16 22:52:06 +02:00
|
|
|
static FAR struct nsh_vtbl_s *nsh_telnetclone(FAR struct nsh_vtbl_s *vtbl)
|
|
|
|
{
|
2008-08-30 17:43:32 +02:00
|
|
|
FAR struct telnetd_s *pstate = (FAR struct telnetd_s *)vtbl;
|
|
|
|
FAR struct telnetd_s *pclone = nsh_allocstruct();
|
|
|
|
FAR struct nsh_vtbl_s *ret = NULL;
|
2008-08-16 22:52:06 +02:00
|
|
|
|
2008-08-30 17:43:32 +02:00
|
|
|
if (pclone)
|
|
|
|
{
|
|
|
|
if (pstate->tn_redirected)
|
|
|
|
{
|
2009-12-15 20:00:40 +01:00
|
|
|
pclone->tn_redirected = true;
|
2008-08-30 17:43:32 +02:00
|
|
|
pclone->tn_vtbl.output = nsh_redirectoutput;
|
|
|
|
pclone->u.rd.rd_fd = pstate->u.rd.rd_fd;
|
|
|
|
pclone->u.rd.rd_stream = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pclone->u.tn = pstate->u.tn;
|
|
|
|
}
|
|
|
|
ret = &pclone->tn_vtbl;
|
|
|
|
}
|
|
|
|
return ret;
|
2008-08-16 22:52:06 +02:00
|
|
|
}
|
2008-09-02 14:35:30 +02:00
|
|
|
#endif
|
2008-08-16 22:52:06 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_telnetrelease
|
|
|
|
*
|
|
|
|
* Description:
|
2008-08-30 17:43:32 +02:00
|
|
|
* Release the cloned instance
|
2008-08-16 22:52:06 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-09-02 14:35:30 +02:00
|
|
|
#ifndef CONFIG_EXAMPLES_NSH_DISABLEBG
|
2008-08-16 22:52:06 +02:00
|
|
|
static void nsh_telnetrelease(FAR struct nsh_vtbl_s *vtbl)
|
|
|
|
{
|
|
|
|
FAR struct telnetd_s *pstate = (FAR struct telnetd_s *)vtbl;
|
2008-08-30 17:43:32 +02:00
|
|
|
|
|
|
|
if (pstate->tn_redirected)
|
2008-08-16 22:52:06 +02:00
|
|
|
{
|
2008-08-30 17:43:32 +02:00
|
|
|
nsh_closeifnotclosed(pstate);
|
2008-08-16 22:52:06 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-08-30 17:43:32 +02:00
|
|
|
nsh_flush(pstate);
|
2008-08-16 22:52:06 +02:00
|
|
|
}
|
2008-08-30 17:43:32 +02:00
|
|
|
free(pstate);
|
2008-08-16 22:52:06 +02:00
|
|
|
}
|
2008-09-02 14:35:30 +02:00
|
|
|
#endif
|
2008-08-16 22:52:06 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_telnetredirect
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set up for redirected output
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2009-12-15 20:00:40 +01:00
|
|
|
static void nsh_telnetredirect(FAR struct nsh_vtbl_s *vtbl, int fd, FAR uint8_t *save)
|
2008-08-16 22:52:06 +02:00
|
|
|
{
|
|
|
|
FAR struct telnetd_s *pstate = (FAR struct telnetd_s *)vtbl;
|
|
|
|
FAR struct telnetsave_s *ssave = (FAR struct telnetsave_s *)save;
|
|
|
|
|
|
|
|
if (pstate->tn_redirected)
|
|
|
|
{
|
|
|
|
(void)nsh_openifnotopen(pstate);
|
|
|
|
fflush(pstate->u.rd.rd_stream);
|
|
|
|
if (!ssave)
|
|
|
|
{
|
|
|
|
fclose(pstate->u.rd.rd_stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ssave)
|
|
|
|
{
|
|
|
|
ssave->ts_redirected = pstate->tn_redirected;
|
|
|
|
memcpy(&ssave->u.rd, &pstate->u.rd, sizeof(struct redirect_s));
|
|
|
|
}
|
|
|
|
|
2009-12-15 20:00:40 +01:00
|
|
|
pstate->tn_redirected = true;
|
2008-08-16 22:52:06 +02:00
|
|
|
pstate->u.rd.rd_fd = fd;
|
|
|
|
pstate->u.rd.rd_stream = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2008-09-02 14:35:30 +02:00
|
|
|
* Name: nsh_telnetundirect
|
2008-08-16 22:52:06 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set up for redirected output
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2009-12-15 20:00:40 +01:00
|
|
|
static void nsh_telnetundirect(FAR struct nsh_vtbl_s *vtbl, FAR uint8_t *save)
|
2008-08-16 22:52:06 +02:00
|
|
|
{
|
|
|
|
FAR struct telnetd_s *pstate = (FAR struct telnetd_s *)vtbl;
|
|
|
|
FAR struct telnetsave_s *ssave = (FAR struct telnetsave_s *)save;
|
|
|
|
|
|
|
|
if (pstate->tn_redirected)
|
|
|
|
{
|
|
|
|
nsh_closeifnotclosed(pstate);
|
|
|
|
}
|
|
|
|
|
|
|
|
pstate->tn_redirected = ssave->ts_redirected;
|
|
|
|
memcpy(&pstate->u.rd, &ssave->u.rd, sizeof(struct redirect_s));
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_telnetexit
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Quit the shell instance
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void nsh_telnetexit(FAR struct nsh_vtbl_s *vtbl)
|
|
|
|
{
|
|
|
|
struct telnetd_s *pstate = (struct telnetd_s *)vtbl;
|
|
|
|
pstate->u.tn->tio_state = STATE_CLOSE;
|
|
|
|
}
|
|
|
|
|
2007-12-02 19:18:59 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2008-08-30 17:43:32 +02:00
|
|
|
* Name: nsh_telnetmain
|
2007-12-02 19:18:59 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is the main processing thread for telnetd. It never returns
|
|
|
|
* unless an error occurs
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-08-16 20:39:46 +02:00
|
|
|
int nsh_telnetmain(int argc, char *argv[])
|
2007-12-02 19:18:59 +01:00
|
|
|
{
|
|
|
|
struct in_addr addr;
|
2010-08-05 23:44:36 +02:00
|
|
|
#if defined(CONFIG_EXAMPLES_NSH_DHCPC)
|
|
|
|
FAR void *handle;
|
|
|
|
#endif
|
2007-12-02 19:18:59 +01:00
|
|
|
#if defined(CONFIG_EXAMPLES_NSH_DHCPC) || defined(CONFIG_EXAMPLES_NSH_NOMAC)
|
2009-12-15 20:00:40 +01:00
|
|
|
uint8_t mac[IFHWADDRLEN];
|
2007-12-02 19:18:59 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Many embedded network interfaces must have a software assigned MAC */
|
|
|
|
|
|
|
|
#ifdef CONFIG_EXAMPLES_NSH_NOMAC
|
|
|
|
mac[0] = 0x00;
|
|
|
|
mac[1] = 0xe0;
|
|
|
|
mac[2] = 0xb0;
|
|
|
|
mac[3] = 0x0b;
|
|
|
|
mac[4] = 0xba;
|
|
|
|
mac[5] = 0xbe;
|
|
|
|
uip_setmacaddr("eth0", mac);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Set up our host address */
|
|
|
|
|
|
|
|
#if !defined(CONFIG_EXAMPLES_NSH_DHCPC)
|
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLES_NSH_IPADDR);
|
|
|
|
#else
|
|
|
|
addr.s_addr = 0;
|
|
|
|
#endif
|
|
|
|
uip_sethostaddr("eth0", &addr);
|
|
|
|
|
|
|
|
/* Set up the default router address */
|
|
|
|
|
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLES_NSH_DRIPADDR);
|
|
|
|
uip_setdraddr("eth0", &addr);
|
|
|
|
|
|
|
|
/* Setup the subnet mask */
|
|
|
|
|
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLES_NSH_NETMASK);
|
|
|
|
uip_setnetmask("eth0", &addr);
|
|
|
|
|
|
|
|
#if defined(CONFIG_EXAMPLES_NSH_DHCPC)
|
|
|
|
/* Set up the resolver */
|
|
|
|
|
|
|
|
resolv_init();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_EXAMPLES_NSH_DHCPC)
|
|
|
|
/* Get the MAC address of the NIC */
|
|
|
|
|
|
|
|
uip_getmacaddr("eth0", mac);
|
|
|
|
|
|
|
|
/* Set up the DHCPC modules */
|
|
|
|
|
|
|
|
handle = dhcpc_open(&mac, IFHWADDRLEN);
|
|
|
|
|
2010-08-10 18:39:34 +02:00
|
|
|
/* Get an IP address. Note that there is no logic for renewing the IP address in this
|
|
|
|
* example. The address should be renewed in ds.lease_time/2 seconds.
|
|
|
|
*/
|
2007-12-02 19:18:59 +01:00
|
|
|
|
|
|
|
if (handle)
|
|
|
|
{
|
|
|
|
struct dhcpc_state ds;
|
|
|
|
(void)dhcpc_request(handle, &ds);
|
|
|
|
uip_sethostaddr("eth1", &ds.ipaddr);
|
|
|
|
if (ds.netmask.s_addr != 0)
|
|
|
|
{
|
|
|
|
uip_setnetmask("eth0", &ds.netmask);
|
|
|
|
}
|
|
|
|
if (ds.default_router.s_addr != 0)
|
|
|
|
{
|
|
|
|
uip_setdraddr("eth0", &ds.default_router);
|
|
|
|
}
|
|
|
|
if (ds.dnsaddr.s_addr != 0)
|
|
|
|
{
|
|
|
|
resolv_conf(&ds.dnsaddr);
|
|
|
|
}
|
|
|
|
dhcpc_close(handle);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Execute nsh_connection on each connection to port 23 */
|
|
|
|
|
|
|
|
uip_server(HTONS(23), nsh_connection, CONFIG_EXAMPLES_NSH_STACKSIZE);
|
|
|
|
return OK;
|
|
|
|
}
|
2008-08-30 17:43:32 +02:00
|
|
|
|
|
|
|
#endif /* CONFIG_EXAMPLES_NSH_TELNET */
|