Merge remote-tracking branch 'origin/master' into composite

This commit is contained in:
Gregory Nutt 2017-06-29 10:19:55 -06:00
commit 94f82d473d
35 changed files with 5275 additions and 80 deletions

View File

@ -1,7 +1,7 @@
/****************************************************************************
* apps/examples/cc3000/shell.c
*
* Copyright (C) 2013 Gregory Nutt. All rights reserved.
* Copyright (C) 2013, 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* This is a leverage of similar logic from uIP:
@ -217,6 +217,7 @@ int shell_main(int argc, char *argv[])
/* Configure the telnet daemon */
config.d_port = HTONS(23);
config.d_family = AF_INET;
config.d_priority = CONFIG_EXAMPLES_TELNETD_DAEMONPRIO;
config.d_stacksize = CONFIG_EXAMPLES_TELNETD_DAEMONSTACKSIZE;
config.t_priority = CONFIG_EXAMPLES_TELNETD_CLIENTPRIO;

View File

@ -73,6 +73,16 @@
# undef CONFIG_NSH_TELNET
#endif
/* If Telnet is used and both IPv6 and IPv4 are enabled, then we need to
* pick one.
*/
#ifdef CONFIG_NET_IPv6
# define ADDR_FAMILY AF_INET6
#else
# define ADDR_FAMILY AF_INET
#endif
/****************************************************************************
* Private Types
****************************************************************************/
@ -153,11 +163,14 @@ int nsh_main(int argc, char *argv[])
nsh_initialize();
/* If the Telnet console is selected as a front-end, then start the
* Telnet daemon.
* Telnet daemon UNLESS network initialization is deferred via
* CONFIG_NSH_NETLOCAL. In that case, the telnet daemon must be
* started manually with the telnetd command after the network has
* been initialized
*/
#ifdef CONFIG_NSH_TELNET
ret = nsh_telnetstart();
#if defined(CONFIG_NSH_TELNET) && !defined(CONFIG_NSH_NETLOCAL)
ret = nsh_telnetstart(ADDR_FAMILY);
if (ret < 0)
{
/* The daemon is NOT running. Report the error then fail...

View File

@ -71,6 +71,26 @@
#include "nxterm_internal.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* The NSH telnet console requires networking support (and TCP/IP) */
#ifndef CONFIG_NET
# undef CONFIG_NSH_TELNET
#endif
/* If Telnet is used and both IPv6 and IPv4 are enabled, then we need to
* pick one.
*/
#ifdef CONFIG_NET_IPv6
# define ADDR_FAMILY AF_INET6
#else
# define ADDR_FAMILY AF_INET
#endif
/****************************************************************************
* Public Data
****************************************************************************/
@ -234,7 +254,7 @@ int nxterm_main(int argc, char **argv)
*/
#ifdef CONFIG_NSH_TELNET
ret = nsh_telnetstart();
ret = nsh_telnetstart(ADDR_FAMILY);
if (ret < 0)
{
/* The daemon is NOT running. Report the error then fail...

View File

@ -1,7 +1,7 @@
/****************************************************************************
* examples/telnetd/telnetd.c
*
* Copyright (C) 2012 Gregory Nutt. All rights reserved.
* Copyright (C) 2012, 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* This is a leverage of similar logic from uIP:
@ -53,10 +53,6 @@
#include "telnetd.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Types
****************************************************************************/
@ -247,6 +243,7 @@ int telnetd_main(int argc, char *argv[])
/* Configure the telnet daemon */
config.d_port = HTONS(23);
config.d_family = AF_INET;
config.d_priority = CONFIG_EXAMPLES_TELNETD_DAEMONPRIO;
config.d_stacksize = CONFIG_EXAMPLES_TELNETD_DAEMONSTACKSIZE;
config.t_priority = CONFIG_EXAMPLES_TELNETD_CLIENTPRIO;

850
include/netutils/telnetc.h Normal file
View File

@ -0,0 +1,850 @@
/****************************************************************************
* apps/netutils/telnetc/telnetc.c
*
* Leveraged from libtelnet, https://github.com/seanmiddleditch/libtelnet.
* Modified and re-released under the BSD license:
*
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* The original authors of libtelnet are listed below. Per their licesne,
* "The author or authors of this code dedicate any and all copyright
* interest in this code to the public domain. We make this dedication for
* the benefit of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* code under copyright law."
*
* Author: Sean Middleditch <sean@sourcemud.org>
* (Also listed in the AUTHORS file are Jack Kelly <endgame.dos@gmail.com>
* and Katherine Flavel <kate@elide.org>)
*
* 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 NuttX 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 COPYRIGHT HOLDERS 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
* COPYRIGHT OWNER 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.
*
****************************************************************************/
/* libtelnet - TELNET protocol handling library
*
* SUMMARY:
*
* libtelnet is a library for handling the TELNET protocol. It includes
* routines for parsing incoming data from a remote peer as well as formatting
* data to send to the remote peer.
*
* libtelnet uses a callback-oriented API, allowing application-specific
* handling of various events. The callback system is also used for buffering
* outgoing protocol data, allowing the application to maintain control over
* the actual socket connection.
*
* Features supported include the full TELNET protocol, Q-method option
* negotiation, ZMP, MCCP2, MSSP, and NEW-ENVIRON.
*
* CONFORMS TO:
*
* RFC854 - http://www.faqs.org/rfcs/rfc854.html
* RFC855 - http://www.faqs.org/rfcs/rfc855.html
* RFC1091 - http://www.faqs.org/rfcs/rfc1091.html
* RFC1143 - http://www.faqs.org/rfcs/rfc1143.html
* RFC1408 - http://www.faqs.org/rfcs/rfc1408.html
* RFC1572 - http://www.faqs.org/rfcs/rfc1572.html
*
* LICENSE:
*
* The author or authors of this code dedicate any and all copyright interest
* in this code to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and successors. We
* intend this dedication to be an overt act of relinquishment in perpetuity of
* all present and future rights to this code under copyright law.
*
* Author: Sean Middleditch <sean@sourcemud.org>
*/
#ifndef __APPS_INCLUDE_NETUTILS_TELNETC_H
#define __APPS_INCLUDE_NETUTILS_TELNETC_H 1
/****************************************************************************
* Included Files
****************************************************************************/
#include <stdarg.h>
/* C++ support */
#if defined(__cplusplus)
extern "C"
{
#endif
/****************************************************************************
* Pre-proecessor Definitions
****************************************************************************/
/* Telnet commands and special values. */
#define TELNET_IAC 255
#define TELNET_DONT 254
#define TELNET_DO 253
#define TELNET_WONT 252
#define TELNET_WILL 251
#define TELNET_SB 250
#define TELNET_GA 249
#define TELNET_EL 248
#define TELNET_EC 247
#define TELNET_AYT 246
#define TELNET_AO 245
#define TELNET_IP 244
#define TELNET_BREAK 243
#define TELNET_DM 242
#define TELNET_NOP 241
#define TELNET_SE 240
#define TELNET_EOR 239
#define TELNET_ABORT 238
#define TELNET_SUSP 237
#define TELNET_EOF 236
/* Telnet options. */
#define TELNET_TELOPT_BINARY 0
#define TELNET_TELOPT_ECHO 1
#define TELNET_TELOPT_RCP 2
#define TELNET_TELOPT_SGA 3
#define TELNET_TELOPT_NAMS 4
#define TELNET_TELOPT_STATUS 5
#define TELNET_TELOPT_TM 6
#define TELNET_TELOPT_RCTE 7
#define TELNET_TELOPT_NAOL 8
#define TELNET_TELOPT_NAOP 9
#define TELNET_TELOPT_NAOCRD 10
#define TELNET_TELOPT_NAOHTS 11
#define TELNET_TELOPT_NAOHTD 12
#define TELNET_TELOPT_NAOFFD 13
#define TELNET_TELOPT_NAOVTS 14
#define TELNET_TELOPT_NAOVTD 15
#define TELNET_TELOPT_NAOLFD 16
#define TELNET_TELOPT_XASCII 17
#define TELNET_TELOPT_LOGOUT 18
#define TELNET_TELOPT_BM 19
#define TELNET_TELOPT_DET 20
#define TELNET_TELOPT_SUPDUP 21
#define TELNET_TELOPT_SUPDUPOUTPUT 22
#define TELNET_TELOPT_SNDLOC 23
#define TELNET_TELOPT_TTYPE 24
#define TELNET_TELOPT_EOR 25
#define TELNET_TELOPT_TUID 26
#define TELNET_TELOPT_OUTMRK 27
#define TELNET_TELOPT_TTYLOC 28
#define TELNET_TELOPT_3270REGIME 29
#define TELNET_TELOPT_X3PAD 30
#define TELNET_TELOPT_NAWS 31
#define TELNET_TELOPT_TSPEED 32
#define TELNET_TELOPT_LFLOW 33
#define TELNET_TELOPT_LINEMODE 34
#define TELNET_TELOPT_XDISPLOC 35
#define TELNET_TELOPT_ENVIRON 36
#define TELNET_TELOPT_AUTHENTICATION 37
#define TELNET_TELOPT_ENCRYPT 38
#define TELNET_TELOPT_NEW_ENVIRON 39
#define TELNET_TELOPT_MSSP 70
#define TELNET_TELOPT_COMPRESS 85
#define TELNET_TELOPT_COMPRESS2 86
#define TELNET_TELOPT_ZMP 93
#define TELNET_TELOPT_EXOPL 255
#define TELNET_TELOPT_MCCP2 86
/* TERMINAL-TYPE codes. */
#define TELNET_TTYPE_IS 0
#define TELNET_TTYPE_SEND 1
/* NEW-ENVIRON/ENVIRON codes. */
#define TELNET_ENVIRON_IS 0
#define TELNET_ENVIRON_SEND 1
#define TELNET_ENVIRON_INFO 2
#define TELNET_ENVIRON_VAR 0
#define TELNET_ENVIRON_VALUE 1
#define TELNET_ENVIRON_ESC 2
#define TELNET_ENVIRON_USERVAR 3
/* MSSP codes. */
#define TELNET_MSSP_VAR 1
#define TELNET_MSSP_VAL 2
/* Telnet state tracker flags. */
#define TELNET_FLAG_PROXY (1 << 0)
#define TELNET_PFLAG_DEFLATE (1 << 7)
/****************************************************************************
* Public Types
****************************************************************************/
/* Error codes */
enum telnet_error_u
{
TELNET_EOK = 0, /* No error */
TELNET_EBADVAL, /* Invalid parameter, or API misuse */
TELNET_ENOMEM, /* Memory allocation failure */
TELNET_EOVERFLOW, /* Data exceeds buffer size */
TELNET_EPROTOCOL, /* Invalid sequence of special bytes */
TELNET_ECOMPRESS /* Error handling compressed streams */
};
/* Event codes */
enum telnet_event_type_e
{
TELNET_EV_DATA = 0, /* Raw text data has been received */
TELNET_EV_SEND, /* Data needs to be sent to the peer */
TELNET_EV_IAC, /* Generic IAC code received */
TELNET_EV_WILL, /* WILL option negotiation received */
TELNET_EV_WONT, /* WONT option neogitation received */
TELNET_EV_DO, /* DO option negotiation received */
TELNET_EV_DONT, /* DONT option negotiation received */
TELNET_EV_SUBNEGOTIATION, /* Sub-negotiation data received */
TELNET_EV_COMPRESS, /* Compression has been enabled */
TELNET_EV_ZMP, /* ZMP command has been received */
TELNET_EV_TTYPE, /* TTYPE command has been received */
TELNET_EV_ENVIRON, /* ENVIRON command has been received */
TELNET_EV_MSSP, /* MSSP command has been received */
TELNET_EV_WARNING, /* Recoverable error has occured */
TELNET_EV_ERROR /* Non-recoverable error has occured */
};
/* Environ/MSSP command information */
struct telnet_environ_s
{
unsigned char type; /* either TELNET_ENVIRON_VAR or
* TELNET_ENVIRON_USERVAR */
char *var; /* Name of the variable being set */
char *value; /* value of variable being set; empty string
* if no value */
};
/* State tracker -- private data structure */
struct telnet_s; /* Forward reference */
/* Event information */
union telnet_event_u
{
/* Event type The type field will determine which of the other
* event structure fields have been filled in. For instance, if the
* event type is TELNET_EV_ZMP, then the zmp event field (and ONLY the
* zmp event field) will be filled in.
*/
enum telnet_event_type_e type;
/* Data event: for DATA and SEND events */
struct
{
enum telnet_event_type_e _type; /* Alias for type */
const char *buffer; /* Byte buffer */
size_t size; /* Number of bytes in buffer */
} data;
/* WARNING and ERROR events */
struct
{
enum telnet_event_type_e _type; /* Alias for type */
const char *file; /* File the error occured in */
const char *func; /* Function the error occured in */
const char *msg; /* Error message string */
int line; /* Line of file error occured on */
enum telnet_error_u errcode; /* Error code */
} error;
/* Command event: for IAC */
struct
{
enum telnet_event_type_e _type; /* Alias for type */
unsigned char cmd; /* Telnet command received */
} iac;
/* Negotiation event: WILL, WONT, DO, DONT */
struct
{
enum telnet_event_type_e _type; /* Alias for type */
unsigned char telopt; /* Option being negotiated */
} neg;
/* Subnegotiation event */
struct
{
enum telnet_event_type_e _type; /* Alias for type */
const char *buffer; /* Data of sub-negotiation */
size_t size; /* Number of bytes in buffer */
unsigned char telopt; /* Option code for negotiation */
} sub;
/* ZMP event */
struct
{
enum telnet_event_type_e _type; /* Alias for type */
const char **argv; /* Array of argument string */
size_t argc; /* Number of elements in argv */
} zmp;
/* TTYPE event */
struct
{
enum telnet_event_type_e _type; /* Alias for type */
unsigned char cmd; /* TELNET_TTYPE_IS or TELNET_TTYPE_SEND */
const char *name; /* Terminal type name (IS only) */
} ttype;
/* COMPRESS event */
struct
{
enum telnet_event_type_e _type; /* Alias for type */
unsigned char state; /* 1 if compression is enabled, 0 if
* disabled */
} compress;
/* ENVIRON/NEW-ENVIRON event */
struct
{
enum telnet_event_type_e _type; /* Alias for type */
const struct telnet_environ_s *values; /* Array of variable values */
size_t size; /* Number of elements in values */
unsigned char cmd; /* SEND, IS, or INFO */
} envevent;
/* MSSP event */
struct
{
enum telnet_event_type_e _type; /* Alias for type */
const struct telnet_environ_s *values; /* Array of variable values */
size_t size; /* Number of elements in values */
} mssp;
};
/* Name: telnet_event_handler_t
*
* Description:
* This is the type of function that must be passed to
* telnet_init() when creating a new telnet object. The
* function will be invoked once for every event generated
* by the libtelnet protocol parser.
*
* Input Parameters:
* telnet The telnet object that generated the event
* event Event structure with details about the event
* user_data User-supplied pointer
*/
typedef void (*telnet_event_handler_t)(struct telnet_s *telnet,
union telnet_event_u *event,
void *user_data);
/* telopt support table element; use telopt of -1 for end marker */
struct telnet_telopt_s
{
short telopt; /* One of the TELOPT codes or -1 */
unsigned char us; /* TELNET_WILL or TELNET_WONT */
unsigned char him; /* TELNET_DO or TELNET_DONT */
};
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: telnet_init
*
* Description:
* Initialize a telnet state tracker.
*
* This function initializes a new state tracker, which is used for all
* other libtelnet functions. Each connection must have its own
* telnet state tracker object.
*
* Input Parameters:
* telopts Table of TELNET options the application supports.
* eh Event handler function called for every event.
* flags 0 or TELNET_FLAG_PROXY.
* user_data Optional data pointer that will be passsed to eh.
*
* Returned Value:
* Telent state tracker object.
*
****************************************************************************/
struct telnet_s *telnet_init(const struct telnet_telopt_s *telopts,
telnet_event_handler_t eh, unsigned char flags,
void *user_data);
/****************************************************************************
* Name: telnet_free
*
* Description:
* Free up any memory allocated by a state tracker.
*
* This function must be called when a telnet state tracker is no
* longer needed (such as after the connection has been closed) to
* release any memory resources used by the state tracker.
*
* Input Parameters:
* telnet Telnet state tracker object.
*
****************************************************************************/
void telnet_free(struct telnet_s *telnet);
/****************************************************************************
* Name: telnet_recv
*
* Description:
* Push a byte buffer into the state tracker.
*
* Passes one or more bytes to the telnet state tracker for
* protocol parsing. The byte buffer is most often going to be
* the buffer that recv() was called for while handling the
* connection.
*
* Input Parameters:
* telnet Telnet state tracker object.
* buffer Pointer to byte buffer.
* size Number of bytes pointed to by buffer.
*
****************************************************************************/
void telnet_recv(struct telnet_s *telnet, const char *buffer, size_t size);
/****************************************************************************
* Name: telnet_iac
*
* Description:
* Send a telnet command.
*
* Input Parameters:
* telnet Telnet state tracker object.
* cmd Command to send.
*
****************************************************************************/
void telnet_iac(struct telnet_s *telnet, unsigned char cmd);
/****************************************************************************
* Name: telnet_negotiate
*
* Description:
* Send negotiation command.
*
* Internally, libtelnet uses RFC1143 option negotiation rules.
* The negotiation commands sent with this function may be ignored
* if they are determined to be redundant.
*
* Input Parameters:
* telnet Telnet state tracker object.
* cmd TELNET_WILL, TELNET_WONT, TELNET_DO, or TELNET_DONT.
* opt One of the TELNET_TELOPT_* values.
*
****************************************************************************/
void telnet_negotiate(struct telnet_s *telnet, unsigned char cmd,
unsigned char opt);
/****************************************************************************
* Name: telnet_send
*
* Description:
* Send non-command data (escapes IAC bytes).
*
* Input Parameters:
* telnet Telnet state tracker object.
* buffer Buffer of bytes to send.
* size Number of bytes to send.
*
****************************************************************************/
void telnet_send(struct telnet_s *telnet, const char *buffer, size_t size);
/****************************************************************************
* Name: telnet_begin_sb
*
* Description:
* Begin a sub-negotiation command.
*
* Sends IAC SB followed by the telopt code. All following data sent
* will be part of the sub-negotiation, until telnet_finish_sb() is
* called.
*
* Input Parameters:
* telnet Telnet state tracker object.
* telopt One of the TELNET_TELOPT_* values.
*
****************************************************************************/
void telnet_begin_sb(struct telnet_s *telnet, unsigned char telopt);
/****************************************************************************
* Name: telnet_finish_sb
*
* Description:
* Finish a sub-negotiation command.
*
* This must be called after a call to telnet_begin_sb() to finish a
* sub-negotiation command.
*
* Input Parameters:
* telnet Telnet state tracker object.
*
****************************************************************************/
#define telnet_finish_sb(telnet) telnet_iac((telnet), TELNET_SE)
/****************************************************************************
* Name: telnet_subnegotiation
*
* Description:
* Send a complete subnegotiation buffer.
*
* Equivalent to:
* telnet_begin_sb(telnet, telopt);
* telnet_send(telnet, buffer, size);
* telnet_finish_sb(telnet);
*
* Input Parameters:
* telnet Telnet state tracker format.
* telopt One of the TELNET_TELOPT_* values.
* buffer Byte buffer for sub-negotiation data.
* size Number of bytes to use for sub-negotiation data.
*
****************************************************************************/
void telnet_subnegotiation(struct telnet_s *telnet, unsigned char telopt,
const char *buffer, size_t size);
/****************************************************************************
* Name: telnet_begin_compress2
*
* Description:
* Begin sending compressed data.
*
* This function will begein sending data using the COMPRESS2 option,
* which enables the use of zlib to compress data sent to the client.
* The client must offer support for COMPRESS2 with option negotiation,
* and zlib support must be compiled into libtelnet.
*
* Only the server may call this command.
*
* Input Parameters:
* telnet Telnet state tracker object.
*
****************************************************************************/
void telnet_begin_compress2(struct telnet_s *telnet);
/****************************************************************************
* Name: telnet_printf
*
* Description:
* Send formatted data.
*
* This function is a wrapper around telnet_send(). It allows using
* printf-style formatting.
*
* Additionally, this function will translate \\r to the CR NUL construct
* and \\n with CR LF, as well as automatically escaping IAC bytes like
* telnet_send().
*
* Input Parameters:
* telnet Telnet state tracker object.
* fmt Format string.
*
* Returned Value:
* Number of bytes sent.
*
****************************************************************************/
int telnet_printf(struct telnet_s *telnet, const char *fmt, ...);
/****************************************************************************
* Name: telnet_vprintf
*
* Description:
* Send formatted data with \r and \n translation in addition to IAC IAC
*
* See telnet_printf().
*
****************************************************************************/
int telnet_vprintf(struct telnet_s *telnet, const char *fmt, va_list va);
/****************************************************************************
* Name: telnet_raw_printf
*
* Description:
* Send formatted data (no newline escaping).
*
* This behaves identically to telnet_printf(), except that the \\r and \\n
* characters are not translated. The IAC byte is still escaped as normal
* with telnet_send().
*
* Input Parameters:
* telnet Telnet state tracker object.
* fmt Format string.
*
* Returned Value:
* Number of bytes sent.
*
****************************************************************************/
int telnet_raw_printf(struct telnet_s *telnet, const char *fmt, ...);
/****************************************************************************
* Name: telnet_raw_vprintf
*
* Description:
* Send formatted data (no newline escaping).
*
* See telnet_raw_printf().
*
****************************************************************************/
int telnet_raw_vprintf(struct telnet_s *telnet, const char *fmt, va_list va);
/****************************************************************************
* Name: telnet_begin_newenviron
*
* Description:
* Begin a new set of NEW-ENVIRON values to request or send.
*
* This function will begin the sub-negotiation block for sending or
* requesting NEW-ENVIRON values.
*
* The telnet_finish_newenviron() macro must be called after this
* function to terminate the NEW-ENVIRON command.
*
* Input Parameters:
* telnet Telnet state tracker object.
* type One of TELNET_ENVIRON_SEND, TELNET_ENVIRON_IS, or
* TELNET_ENVIRON_INFO.
*
****************************************************************************/
void telnet_begin_newenviron(struct telnet_s *telnet, unsigned char type);
/****************************************************************************
* Name: telnet_newenviron_value
*
* Description:
* Send a NEW-ENVIRON variable name or value.
*
* This can only be called between calls to telnet_begin_newenviron() and
* telnet_finish_newenviron().
*
* Input Parameters:
* telnet Telnet state tracker object.
* type One of TELNET_ENVIRON_VAR, TELNET_ENVIRON_USERVAR, or
* TELNET_ENVIRON_VALUE.
* string Variable name or value.
*
****************************************************************************/
void telnet_newenviron_value(struct telnet_s *telnet, unsigned char type,
const char *string);
/****************************************************************************
* Name: telnet_finish_newenviron
*
* Description:
* Finish a NEW-ENVIRON command.
*
* This must be called after a call to telnet_begin_newenviron() to finish a
* NEW-ENVIRON variable list.
*
* telnet Telnet state tracker object.
*
****************************************************************************/
#define telnet_finish_newenviron(telnet) telnet_finish_sb((telnet))
/****************************************************************************
* Name: telnet_ttype_send
*
* Description:
* Send the TERMINAL-TYPE SEND command.
*
* Sends the sequence IAC TERMINAL-TYPE SEND.
*
* telnet Telnet state tracker object.
*
****************************************************************************/
void telnet_ttype_send(struct telnet_s * telnet);
/****************************************************************************
* Name: telnet_ttype_is
*
* Description:
* Send the TERMINAL-TYPE IS command.
*
* Sends the sequence IAC TERMINAL-TYPE IS "string".
*
* According to the RFC, the recipient of a TERMINAL-TYPE SEND shall
* send the next possible terminal-type the client supports. Upon sending
* the type, the client should switch modes to begin acting as the terminal
* type is just sent.
*
* The server may continue sending TERMINAL-TYPE IS until it receives a
* terminal type is understands. To indicate to the server that it has
* reached the end of the available optoins, the client must send the last
* terminal type a second time. When the server receives the same terminal
* type twice in a row, it knows it has seen all available terminal types.
*
* After the last terminal type is sent, if the client receives another
* TERMINAL-TYPE SEND command, it must begin enumerating the available
* terminal types from the very beginning. This allows the server to
* scan the available types for a preferred terminal type and, if none
* is found, to then ask the client to switch to an acceptable
* alternative.
*
* Note that if the client only supports a single terminal type, then
* simply sending that one type in response to every SEND will satisfy
* the behavior requirements.
*
* Input Parameters:
* telnet Telnet state tracker object.
* ttype Name of the terminal-type being sent.
*
****************************************************************************/
void telnet_ttype_is(struct telnet_s * telnet, const char *ttype);
/****************************************************************************
* Name: telnet_send_zmp
*
* Description:
* Send a ZMP command.
*
* Input Parameters:
* telnet Telnet state tracker object.
* argc Number of ZMP commands being sent.
* argv Array of argument strings.
*
****************************************************************************/
void telnet_send_zmp(struct telnet_s * telnet, size_t argc,
const char **argv);
/****************************************************************************
* Name: telnet_send_zmpv
*
* Description:
* Send a ZMP command.
*
* Arguments are listed out in var-args style. After the last argument, a
* NULL pointer must be passed in as a sentinel value.
*
* Input Parameters:
* telnet Telnet state tracker object.
*
****************************************************************************/
void telnet_send_zmpv(struct telnet_s * telnet, ...);
/****************************************************************************
* Name: telnet_send_vzmpv
*
* Description:
* Send a ZMP command.
*
* See telnet_send_zmpv().
*
****************************************************************************/
void telnet_send_vzmpv(struct telnet_s * telnet, va_list va);
/****************************************************************************
* Name: telnet_begin_zmp
*
* Description:
* Begin sending a ZMP command
*
* Input Parameters:
* telnet Telnet state tracker object.
* cmd The first argument (command name) for the ZMP command.
*
****************************************************************************/
void telnet_begin_zmp(struct telnet_s * telnet, const char *cmd);
/****************************************************************************
* Name: telnet_zmp_arg
*
* Description:
* Send a ZMP command argument.
*
* Input Parameters:
* telnet Telnet state tracker object.
* arg Telnet argument string.
*
****************************************************************************/
void telnet_zmp_arg(struct telnet_s * telnet, const char *arg);
/****************************************************************************
* Name: telnet_finish_zmp
*
* Description:
* Finish a ZMP command.
*
* This must be called after a call to telnet_begin_zmp() to finish a
* ZMP argument list.
*
* Input Parameters:
* telnet Telnet state tracker object.
*
****************************************************************************/
#define telnet_finish_zmp(telnet) telnet_finish_sb((telnet))
#if defined(__cplusplus)
}
#endif
#endif /* __APPS_INCLUDE_NETUTILS_TELNETC_H */

View File

@ -1,7 +1,7 @@
/****************************************************************************
* apps/include/netutils/telnetd.h
*
* Copyright (C) 2007, 2011-2012, 2015 Gregory Nutt. All rights reserved.
* Copyright (C) 2007, 2011-2012, 2015, 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -61,18 +61,22 @@ struct telnetd_config_s
{
/* These fields describe the telnet daemon */
int d_port; /* The port to listen on (in network byte order) */
int d_priority; /* The execution priority of the Telnet daemon task */
int d_stacksize; /* The stack size needed by the Telnet daemon task */
uint8_t d_priority; /* The execution priority of the Telnet daemon task */
size_t d_stacksize; /* The stack size needed by the Telnet daemon task */
/* These fields describe the network connection */
uint16_t d_port; /* The port to listen on (in network byte order) */
sa_family_t d_family; /* Address family */
/* These fields describe the priority of each thread created by the Telnet
* daemon.
*/
int t_priority; /* The execution priority of the spawned task, */
int t_stacksize; /* The stack size needed by the spawned task */
main_t t_entry; /* The entrypoint of the task to spawn when a new
* connection is accepted. */
uint8_t t_priority; /* The execution priority of the spawned task, */
size_t t_stacksize; /* The stack size needed by the spawned task */
main_t t_entry; /* The entrypoint of the task to spawn when a new
* connection is accepted. */
};
/****************************************************************************

View File

@ -1,7 +1,7 @@
/****************************************************************************
* apps/include/nshlib/nshlib.h
*
* Copyright (C) 2011, 2013, 2016 Gregory Nutt. All rights reserved.
* Copyright (C) 2011, 2013, 2016-2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -147,8 +147,12 @@ int nsh_consolemain(int argc, char *argv[]);
* the daemon has been started.
*
* Input Parameters:
* None. All of the properties of the Telnet daemon are controlled by
* NuttX configuration setting.
* family - Provides the IP family to use by the server. May be either
* AF_INET or AF_INET6. This is needed because both both may be
* enabled in the configuration.
*
* All of the other properties of the Telnet daemon are controlled by
* NuttX configuration settings.
*
* Returned Values:
* The task ID of the Telnet daemon was successfully started. A negated
@ -156,7 +160,7 @@ int nsh_consolemain(int argc, char *argv[]);
*
****************************************************************************/
int nsh_telnetstart(void);
int nsh_telnetstart(sa_family_t family);
/****************************************************************************
* Name: platform_motd

View File

@ -216,11 +216,7 @@ ssize_t readline(FAR char *buf, int buflen, FILE *instream, FILE *outstream);
*
****************************************************************************/
#if CONFIG_NFILE_STREAMS > 0
# define std_readline(b,s) readline(b,s,stdin,stdout)
#else
ssize_t std_readline(FAR char *buf, int buflen);
#endif
#define std_readline(b,s) readline(b,s,stdin,stdout)
#undef EXTERN
#ifdef __cplusplus

View File

@ -98,6 +98,10 @@ int ieee802154_getsaddr(int fd, FAR uint8_t *saddr);
int ieee802154_seteaddr(int fd, FAR const uint8_t *eaddr);
int ieee802154_geteaddr(int fd, FAR uint8_t *eaddr);
int ieee802154_getcoordsaddr(int fd, FAR uint8_t *saddr);
int ieee802154_getcoordeaddr(int fd, FAR uint8_t *eaddr);
int ieee802154_setpromisc(int fd, bool promisc);
int ieee802154_getpromisc(int fd, FAR bool *promisc);

View File

@ -55,6 +55,12 @@ highly influenced by uIP) include:
and adapted for NuttX by Darcy Gong.
tftpc - TFTP client. See apps/include/netutils/tftp.h
for interface information.
telnetc - This is a port of libtelnet to NuttX. This is a public domain
Telnet client library available from
https://github.com/seanmiddleditch/libtelnet modified for use
with NuttX. Original Authors: Sean Middleditch <sean@sourcemud.org>,
Jack Kelly <endgame.dos@gmail.com>, and Katherine Flavel
<kate@elide.org>
telnetd - TELNET server. This is the Telnet logic adapted from
uIP and generalized for use as the front end to any
shell. The telnet daemon creates sessions that are

6
netutils/telnetc/.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
/.built
/.depend
/Make.dep
/*.src
/*.obj
/*.lst

20
netutils/telnetc/Kconfig Normal file
View File

@ -0,0 +1,20 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#
config NETUTILS_TELNETC
bool "Telnet client library"
default n
depends on NET && NET_TCP
---help---
Enable support for the libtelnet. This is a public domain
Telnet client library available from https://github.com/seanmiddleditch/libtelnet
modified for use with NuttX. Original Authors:
Sean Middleditch <sean@sourcemud.org>
Jack Kelly <endgame.dos@gmail.com>
Katherine Flavel <kate@elide.org>
if NETUTILS_TELNETC
endif

View File

@ -0,0 +1,39 @@
# apps/netutils/telnetc/Make.defs
# Adds selected applications to apps/ build
#
# Copyright (C) 2017 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# 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 NuttX 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 COPYRIGHT HOLDERS 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
# COPYRIGHT OWNER 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.
#
############################################################################
ifeq ($(CONFIG_NETUTILS_TELNETC),y)
CONFIGURED_APPS += netutils/telnetc
endif

104
netutils/telnetc/Makefile Normal file
View File

@ -0,0 +1,104 @@
############################################################################
# apps/netutils/telnetc/Makefile
#
# Copyright (C) 2017 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# 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 NuttX 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 COPYRIGHT HOLDERS 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
# COPYRIGHT OWNER 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.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
# Telnet daemon
ASRCS =
CSRCS =
ifeq ($(CONFIG_NET_TCP),y)
CSRCS += telnetc.c
endif
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)
OBJS = $(AOBJS) $(COBJS)
ifeq ($(CONFIG_WINDOWS_NATIVE),y)
BIN = ..\..\libapps$(LIBEXT)
else
ifeq ($(WINTOOL),y)
BIN = ..\\..\\libapps$(LIBEXT)
else
BIN = ../../libapps$(LIBEXT)
endif
endif
ROOTDEPPATH = --dep-path .
# Common build
VPATH =
all: .built
.PHONY: context depend clean distclean preconfig
$(AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
.built: $(OBJS)
$(call ARCHIVE, $(BIN), $(OBJS))
$(Q) touch .built
install:
context:
.depend: Makefile $(SRCS)
$(Q) $(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(SRCS) >Make.dep
$(Q) touch $@
depend: .depend
clean:
$(call DELFILE, .built)
$(call CLEAN)
distclean: clean
$(call DELFILE, Make.dep)
$(call DELFILE, .depend)
preconfig:
-include Make.dep

2410
netutils/telnetc/telnetc.c Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
/****************************************************************************
* netutils/telnetd/telnetd_daemon.c
*
* Copyright (C) 2012 Gregory Nutt. All rights reserved.
* Copyright (C) 2012, 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -48,6 +48,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include <semaphore.h>
#include <sched.h>
@ -72,9 +73,10 @@
struct telnetd_s
{
int port; /* The port to listen on (in network byte order) */
int priority; /* The execution priority of the spawned task, */
int stacksize; /* The stack size needed by the spawned task */
uint16_t port; /* The port to listen on (in network byte order) */
sa_family_t family; /* Address family */
uint8_t priority; /* The execution priority of the spawned task, */
size_t stacksize; /* The stack size needed by the spawned task */
main_t entry; /* The entrypoint of the task to spawn when a new
* connection is accepted. */
};
@ -126,7 +128,16 @@ static struct telnetd_common_s g_telnetdcommon;
static int telnetd_daemon(int argc, char *argv[])
{
FAR struct telnetd_s *daemon;
struct sockaddr_in myaddr;
union
{
struct sockaddr generic;
#ifdef CONFIG_NET_IPv4
struct sockaddr_in ipv4;
#endif
#ifdef CONFIG_NET_IPv6
struct sockaddr_in6 ipv6;
#endif
} addr;
struct telnet_session_s session;
#ifdef CONFIG_NET_SOLINGER
struct linger ling;
@ -187,11 +198,12 @@ static int telnetd_daemon(int argc, char *argv[])
/* Create a new TCP socket to use to listen for connections */
listensd = socket(PF_INET, SOCK_STREAM, 0);
listensd = socket(daemon->family, SOCK_STREAM, 0);
if (listensd < 0)
{
int errval = errno;
nerr("ERROR: socket failure: %d\n", errval);
nerr("ERROR: socket() failed for family %u: %d\n",
daemon->family, errval);
return -errval;
}
@ -208,11 +220,33 @@ static int telnetd_daemon(int argc, char *argv[])
/* Bind the socket to a local address */
myaddr.sin_family = AF_INET;
myaddr.sin_port = daemon->port;
myaddr.sin_addr.s_addr = INADDR_ANY;
#ifdef CONFIG_NET_IPv4
if (daemon->family == AF_INET)
{
addr.ipv4.sin_family = AF_INET;
addr.ipv4.sin_port = daemon->port;
addr.ipv4.sin_addr.s_addr = INADDR_ANY;
addrlen = sizeof(struct sockaddr_in);
}
else
#endif
#ifdef CONFIG_NET_IPv6
if (daemon->family == AF_INET6)
{
addr.ipv6.sin6_family = AF_INET6;
addr.ipv6.sin6_port = daemon->port;
addrlen = sizeof(struct sockaddr_in6);
if (bind(listensd, (struct sockaddr*)&myaddr, sizeof(struct sockaddr_in)) < 0)
memset(addr.ipv6.sin6_addr.s6_addr, 0, addrlen);
}
else
#endif
{
nerr("ERROR: Unsupported address family: %u", daemon->family);
goto errout_with_socket;
}
if (bind(listensd, &addr.generic, addrlen) < 0)
{
nerr("ERROR: bind failure: %d\n", errno);
goto errout_with_socket;
@ -240,8 +274,8 @@ static int telnetd_daemon(int argc, char *argv[])
{
ninfo("Accepting connections on port %d\n", ntohs(daemon->port));
addrlen = sizeof(struct sockaddr_in);
acceptsd = accept(listensd, (struct sockaddr*)&myaddr, &addrlen);
socklen_t accptlen = sizeof(addr);
acceptsd = accept(listensd, &addr.generic, &accptlen);
if (acceptsd < 0)
{
/* Accept failed */
@ -390,6 +424,7 @@ int telnetd_start(FAR struct telnetd_config_s *config)
/* Initialize the daemon structure */
daemon->port = config->d_port;
daemon->family = config->d_family;
daemon->priority = config->t_priority;
daemon->stacksize = config->t_stacksize;
daemon->entry = config->t_entry;

View File

@ -483,6 +483,11 @@ config NSH_DISABLE_TEST
bool "Disable test"
default n
config NSH_DISABLE_TELNETD
bool "Disable telnetd"
default n if !NSH_NETLOCAL
default y if NSH_NETLOCAL
config NSH_DISABLE_UMOUNT
bool "Disable umount"
default n

View File

@ -1080,6 +1080,21 @@ o sleep <sec>
Pause execution (sleep) of <sec> seconds.
o telnetd
The Telnet daemon may be started either programmatically by calling
nsh_telnetstart() or it may be started from the NSH command line using
this telnetd command.
Normally this command would be suppressed with CONFIG_NSH_DISABLE_TELNETD
because the Telnet daemon is automatically started in nsh_main.c. The
exception is when CONFIG_NSH_NETLOCAL is selected. IN that case, the
network is not enabled at initialization but rather must be enabled from
the NSH command line or via other applications.
In that case, calling nsh_telnetstart() before the the network is
initialized will fail.
o time "<command>"
Perform command timing. This command will execute the following <command>
@ -1274,6 +1289,7 @@ Command Dependencies on Configuration Settings
shutdown CONFIG_BOARDCTL_POWEROFF || CONFIG_BOARDCTL_RESET
sleep !CONFIG_DISABLE_SIGNALS
test !CONFIG_NSH_DISABLESCRIPT
telnetd CONFIG_NSH_TELNET && !CONFIG_NSH_DISABLE_TELNETD
time ---
umount !CONFIG_DISABLE_MOUNTPOINT && CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_FS_READABLE
uname !CONFIG_NSH_DISABLE_UNAME

View File

@ -1187,6 +1187,11 @@ int cmd_lsmod(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
int cmd_wget(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
# endif
# endif
# if defined(CONFIG_NSH_TELNET)
# ifndef CONFIG_NSH_DISABLE_TELNETD
int cmd_telnetd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
# endif
# endif
#endif /* CONFIG_NET */
#if defined(CONFIG_LIBC_NETDB) && defined(CONFIG_NETDB_DNSCLIENT) && \

View File

@ -472,6 +472,14 @@ static const struct cmdmap_s g_cmdmap[] =
{ "test", cmd_test, 3, CONFIG_NSH_MAXARGUMENTS, "<expression>" },
#endif
#if defined(CONFIG_NSH_TELNET) && !defined(CONFIG_NSH_DISABLE_TELNETD)
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
{"telnetd", cmd_telnetd, 2, 2, [ipv4|ipv6] },
#else
{"telnetd", cmd_telnetd, 1, 1, NULL },
#endif
#endif
#ifndef CONFIG_NSH_DISABLE_TIME
{ "time", cmd_time, 2, 2, "\"<command>\"" },
#endif

View File

@ -1,7 +1,7 @@
/****************************************************************************
* apps/nshlib/nsh_telnetd.c
*
* Copyright (C) 2007-2013, 2016 Gregory Nutt. All rights reserved.
* Copyright (C) 2007-2013, 2016-2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -40,6 +40,8 @@
#include <nuttx/config.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <debug.h>
#include <arpa/inet.h>
@ -51,6 +53,17 @@
#ifdef CONFIG_NSH_TELNET
/****************************************************************************
* Private Types
****************************************************************************/
enum telnetd_state_e
{
TELNETD_NOTRUNNING = 0,
TELNETD_STARTED,
TELNETD_RUNNING
};
/****************************************************************************
* Private Functions
****************************************************************************/
@ -167,8 +180,12 @@ static int nsh_telnetmain(int argc, char *argv[])
* the daemon has been started.
*
* Input Parameters:
* None. All of the properties of the Telnet daemon are controlled by
* NuttX configuration setting.
* family - Provides the IP family to use by the server. May be either
* AF_INET or AF_INET6. This is needed because both both may be
* enabled in the configuration.
*
* All of the other properties of the Telnet daemon are controlled by
* NuttX configuration settings.
*
* Returned Values:
* The task ID of the Telnet daemon was successfully started. A negated
@ -176,39 +193,104 @@ static int nsh_telnetmain(int argc, char *argv[])
*
****************************************************************************/
int nsh_telnetstart(void)
int nsh_telnetstart(sa_family_t family)
{
struct telnetd_config_s config;
int ret;
static enum telnetd_state_e state = TELNETD_NOTRUNNING;
int ret = OK;
/* Initialize any USB tracing options that were requested. If standard
* console is also defined, then we will defer this step to the standard
* console.
*/
if (state == TELNETD_NOTRUNNING)
{
struct telnetd_config_s config;
/* There is a tiny race condition here if two tasks were to try to
* start the Telnet daemon concurrently.
*/
state = TELNETD_STARTED;
/* Initialize any USB tracing options that were requested. If
* standard console is also defined, then we will defer this step to
* the standard console.
*/
#if defined(CONFIG_NSH_USBDEV_TRACE) && !defined(CONFIG_NSH_CONSOLE)
usbtrace_enable(TRACE_BITSET);
usbtrace_enable(TRACE_BITSET);
#endif
/* Configure the telnet daemon */
/* Configure the telnet daemon */
config.d_port = HTONS(CONFIG_NSH_TELNETD_PORT);
config.d_priority = CONFIG_NSH_TELNETD_DAEMONPRIO;
config.d_stacksize = CONFIG_NSH_TELNETD_DAEMONSTACKSIZE;
config.t_priority = CONFIG_NSH_TELNETD_CLIENTPRIO;
config.t_stacksize = CONFIG_NSH_TELNETD_CLIENTSTACKSIZE;
config.t_entry = nsh_telnetmain;
config.d_port = HTONS(CONFIG_NSH_TELNETD_PORT);
config.d_family = family;
config.d_priority = CONFIG_NSH_TELNETD_DAEMONPRIO;
config.d_stacksize = CONFIG_NSH_TELNETD_DAEMONSTACKSIZE;
config.t_priority = CONFIG_NSH_TELNETD_CLIENTPRIO;
config.t_stacksize = CONFIG_NSH_TELNETD_CLIENTSTACKSIZE;
config.t_entry = nsh_telnetmain;
/* Start the telnet daemon */
/* Start the telnet daemon */
_info("Starting the Telnet daemon\n");
ret = telnetd_start(&config);
if (ret < 0)
{
_err("ERROR: Failed to tart the Telnet daemon: %d\n", ret);
_info("Starting the Telnet daemon\n");
ret = telnetd_start(&config);
if (ret < 0)
{
_err("ERROR: Failed to tart the Telnet daemon: %d\n", ret);
state = TELNETD_NOTRUNNING;
}
else
{
state = TELNETD_RUNNING;
}
}
return ret;
}
/****************************************************************************
* Name: cmd_telnetd
*
* Description:
* The Telnet daemon may be started either programmatically by calling
* nsh_telnetstart() or it may be started from the NSH command line using
* this telnetd command.
*
* Normally this command would be suppressed with CONFIG_NSH_DISABLE_TELNETD
* because the Telnet daemon is automatically started in nsh_main.c. The
* exception is when CONFIG_NSH_NETLOCAL is selected. IN that case, the
* network is not enabled at initialization but rather must be enabled from
* the NSH command line or via other applications.
*
* In that case, calling nsh_telnetstart() before the the network is
* initialized will fail.
*
* Input Parameters:
* None. All of the properties of the Telnet daemon are controlled by
* NuttX configuration setting.
*
* Returned Values:
* OK is returned on success; ERROR is return on failure.
*
****************************************************************************/
#ifndef CONFIG_NSH_DISABLE_TELNETD
int cmd_telnetd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
sa_family_t family;
/* If both IPv6 nd IPv4 are enabled, then the address family must
* be specified on the command line.
*/
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
family = (strcmp(argv[1], "ipv6") == 0) ? AF_INET6 : AF_INET;
#elif defined(CONFIG_NET_IPv6)
family = AF_INET6;
#else /* if defined(CONFIG_NET_IPv4) */
family = AF_INET;
#endif
return nsh_telnetstart(family) < 0 ? ERROR : OK;
}
#endif
#endif /* CONFIG_NSH_TELNET */

11
system/telnet/.gitignore vendored Normal file
View File

@ -0,0 +1,11 @@
/Make.dep
/.depend
/.built
/*.asm
/*.obj
/*.rel
/*.lst
/*.sym
/*.adb
/*.lib
/*.src

61
system/telnet/Kconfig Normal file
View File

@ -0,0 +1,61 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#
menuconfig SYSTEM_TELNET_CHATD
bool "Telnet chat deamon"
default n
depends on NET && NET_TCP && !DISABLE_POLL
select NETUTILS_TELNETC
---help---
Enable the Telnet Chat daemon.
if SYSTEM_TELNET_CHATD
config SYSTEM_TELNET_CHATD_PROGNAME
string "Chat daemon program name"
default "chatd"
depends on BUILD_KERNEL
if NSH_BUILTIN_APPS
config SYSTEM_TELNET_CHATD_STACKSIZE
int "Chat daemon stacksize"
default 2048
config SYSTEM_TELNET_CHATD_PRIORITY
int "Chat daemon priority"
default 100
endif # NSH_BUILTIN_APPS
endif # SYSTEM_TELNET_CHATD
menuconfig SYSTEM_TELNET_CLIENT
bool "Telnet client"
default n
depends on NET && NET_TCP && !DISABLE_POLL
select NETUTILS_TELNETC
select SYSTEM_READLINE
---help---
Enable the Telnet client program.
if SYSTEM_TELNET_CLIENT
config SYSTEM_TELNET_CLIENT_PROGNAME
string "Telnet client program name"
default "chatd"
depends on BUILD_KERNEL
if NSH_BUILTIN_APPS
config SYSTEM_TELNET_CLIENT_STACKSIZE
int "Telnet client stacksize"
default 2048
config SYSTEM_TELNET_CLIENT_PRIORITY
int "Telnet client priority"
default 100
endif # NSH_BUILTIN_APPS
endif # SYSTEM_TELNET_CLIENT

41
system/telnet/Make.defs Normal file
View File

@ -0,0 +1,41 @@
############################################################################
# apps/system/usbmsc/Make.defs
# Adds selected applications to apps/ build
#
# Copyright (C) 2016 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# 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 NuttX 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 COPYRIGHT HOLDERS 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
# COPYRIGHT OWNER 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.
#
############################################################################
ifeq ($(CONFIG_SYSTEM_TELNET_CHATD),y)
CONFIGURED_APPS += system/telnet
else ifeq ($(CONFIG_SYSTEM_TELNET_CLIENT),y)
CONFIGURED_APPS += system/telnet
endif

205
system/telnet/Makefile Normal file
View File

@ -0,0 +1,205 @@
############################################################################
# apps/system/telnet/Makefile
#
# Copyright (C) 2017 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# 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 NuttX 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 COPYRIGHT HOLDERS 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
# COPYRIGHT OWNER 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.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
# Telnet Programs
ifeq ($(CONFIG_SYSTEM_TELNET_CHATD),y)
# Chatd files
CHATD_CSRCS =
CHATD_MAINSRC = telnet_chatd.c
CHATD_COBJS = $(CHATD_CSRCS:.c=$(OBJEXT))
CHATD_MAINOBJ = $(CHATD_MAINSRC:.c=$(OBJEXT))
# Chatd Application Info
CONFIG_SYSTEM_TELNET_CHATD_PROGNAME ?= chatd
CONFIG_SYSTEM_TELNET_CHATD_PRIORITY ?= 100
CONFIG_SYSTEM_TELNET_CHATD_STACKSIZE ?= 2048
CHATD_MAINNAME = chatd_main
CHATD_APPNAME = chatd
CHATD_PROGNAME = $(CONFIG_SYSTEM_TELNET_CHATD_PROGNAME)
CHATD_PRIORITY = $(CONFIG_SYSTEM_TELNET_CHATD_PRIORITY)
CHATD_STACKSIZE = $(CONFIG_SYSTEM_TELNET_CHATD_STACKSIZE)
endif
ifeq ($(CONFIG_SYSTEM_TELNET_CLIENT),y)
# Telnet client files
CLIENT_CSRCS =
CLIENT_MAINSRC = telnet_client.c
CLIENT_COBJS = $(CLIENT_CSRCS:.c=$(OBJEXT))
CLIENT_MAINOBJ = $(CLIENT_MAINSRC:.c=$(OBJEXT))
# Telnet client Application Info
CONFIG_SYSTEM_TELNET_CLIENT_PROGNAME ?= tcpclient
CONFIG_SYSTEM_TELNET_CLIENT_PRIORITY ?= 100
CONFIG_SYSTEM_TELNET_CLIENT_STACKSIZE ?= 2048
CLIENT_MAINNAME = telnet_main
CLIENT_APPNAME = telnet
CLIENT_PROGNAME = $(CONFIG_SYSTEM_TELNET_CLIENT_PROGNAME)
CLIENT_PRIORITY = $(CONFIG_SYSTEM_TELNET_CLIENT_PRIORITY)
CLIENT_STACKSIZE = $(CONFIG_SYSTEM_TELNET_CLIENT_STACKSIZE)
endif
# All programns
TARG_SRCS = $(CHATD_CRCS) $(CHATD_MAINSRC) $(CLIENT_CSRCS) $(CLIENT_MAINSRC)
TARG_OBJS = $(CHATD_COBJS) $(CLIENT_COBJS)
ifneq ($(CONFIG_BUILD_KERNEL),y)
TARG_OBJS += $(CHATD_MAINOBJ) $(CLIENT_MAINOBJ)
endif
ifeq ($(CONFIG_WINDOWS_NATIVE),y)
TARG_BIN = ..\..\libapps$(LIBEXT)
else
ifeq ($(WINTOOL),y)
TARG_BIN = ..\\..\\libapps$(LIBEXT)
else
TARG_BIN = ../../libapps$(LIBEXT)
endif
endif
ifeq ($(WINTOOL),y)
INSTALL_DIR = "${shell cygpath -w $(BIN_DIR)}"
else
INSTALL_DIR = $(BIN_DIR)
endif
ROOTDEPPATH = --dep-path .
# Common build
VPATH =
all: .built
.PHONY: clean depend distclean preconfig
$(CHATD_COBJS) $(CHATD_MAINOBJ) $(CLIENT_COBJS) $(CLIENT_MAINOBJ) : %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
.built: $(TARG_OBJS)
$(call ARCHIVE, $(TARG_BIN), $(TARG_OBJS))
$(Q) touch .built
ifeq ($(CONFIG_BUILD_KERNEL),y)
ifeq ($(CONFIG_SYSTEM_TELNET_CHATD),y)
$(BIN_DIR)$(DELIM)$(CHATD_PROGNAME): $(OBJS) $(CHATD_MAINOBJ)
@echo "LD: $(CHATD_PROGNAME)"
$(Q) $(LD) $(LDELFFLAGS) $(LDLIBPATH) -o $(INSTALL_DIR)$(DELIM)$(CHATD_PROGNAME) $(ARCHCRT0OBJ) $(CHATD_MAINOBJ) $(LDLIBS)
$(Q) $(NM) -u $(INSTALL_DIR)$(DELIM)$(CHATD_PROGNAME)
chatd_install: $(BIN_DIR)$(DELIM)$(CHATD_PROGNAME)
else
chatd_install:
endif
ifeq ($(CONFIG_SYSTEM_TELNET_CLIENT),y)
$(BIN_DIR)$(DELIM)$(CLIENT_PROGNAME): $(OBJS) $(CLIENT_MAINOBJ)
@echo "LD: $(CLIENT_PROGNAME)"
$(Q) $(LD) $(LDELFFLAGS) $(LDLIBPATH) -o $(INSTALL_DIR)$(DELIM)$(CLIENT_PROGNAME) $(ARCHCRT0OBJ) $(CLIENT_MAINOBJ) $(LDLIBS)
$(Q) $(NM) -u $(INSTALL_DIR)$(DELIM)$(CLIENT_PROGNAME)
client_install: $(BIN_DIR)$(DELIM)$(CLIENT_PROGNAME)
else
client_install:
endif
install: chatd_install client_install
else
install:
endif
ifeq ($(CONFIG_NSH_BUILTIN_APPS),y)
ifeq ($(CONFIG_SYSTEM_TELNET_CHATD),y)
$(BUILTIN_REGISTRY)$(DELIM)$(CHATD_APPNAME)_main.bdat: $(DEPCONFIG) Makefile
$(call REGISTER,$(CHATD_APPNAME),$(CHATD_PRIORITY),$(CHATD_STACKSIZE),$(CHATD_MAINNAME))
chatd_register: $(BUILTIN_REGISTRY)$(DELIM)$(CHATD_APPNAME)_main.bdat
else
chatd_register:
endif
ifeq ($(CONFIG_SYSTEM_TELNET_CLIENT),y)
$(BUILTIN_REGISTRY)$(DELIM)$(CLIENT_APPNAME)_main.bdat: $(DEPCONFIG) Makefile
$(call REGISTER,$(CLIENT_APPNAME),$(CLIENT_PRIORITY),$(CLIENT_STACKSIZE),$(CLIENT_MAINNAME))
client_register: $(BUILTIN_REGISTRY)$(DELIM)$(CLIENT_APPNAME)_main.bdat
else
client_register:
endif
context: chatd_register client_register
else
context:
endif
.depend: Makefile $(TARG_CSRCS)
@$(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(TARG_CSRCS) >Make.dep
@touch $@
depend: .depend
clean:
$(call DELFILE, .built)
$(call DELFILE, *.dSYM)
$(call CLEAN)
distclean: clean
$(call DELFILE, Make.dep)
$(call DELFILE, .depend)
preconfig:
-include Make.dep

View File

@ -0,0 +1,518 @@
/****************************************************************************
* apps/system/telnet/telnet_chatd.c
*
* Leveraged from libtelnet, https://github.com/seanmiddleditch/libtelnet.
* Modified and re-released under the BSD license:
*
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* The original authors of libtelnet are listed below. Per their licesne,
* "The author or authors of this code dedicate any and all copyright
* interest in this code to the public domain. We make this dedication for
* the benefit of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* code under copyright law."
*
* Author: Sean Middleditch <sean@sourcemud.org>
* (Also listed in the AUTHORS file are Jack Kelly <endgame.dos@gmail.com>
* and Katherine Flavel <kate@elide.org>)
*
* 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 NuttX 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 COPYRIGHT HOLDERS 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
* COPYRIGHT OWNER 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 <sys/socket.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <poll.h>
#include <netdb.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "netutils/telnetc.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define MAX_USERS 64
#define LINEBUFFER_SIZE 256
/****************************************************************************
* Private Types
****************************************************************************/
struct user_s
{
char *name;
int sock;
struct telnet_s *telnet;
char linebuf[256];
int linepos;
};
/****************************************************************************
* Private Data
****************************************************************************/
static const struct telnet_telopt_s g_telopts[] =
{
{
TELNET_TELOPT_COMPRESS2, TELNET_WILL, TELNET_DONT
},
{
-1, 0, 0
}
};
static struct user_s g_users[MAX_USERS];
/****************************************************************************
* Private Functions
****************************************************************************/
static void linebuffer_push(char *buffer, size_t size, int *linepos,
char ch, void (*cb) (const char *line, int overflow,
void *ud), void *ud)
{
/* CRLF -- line terminator */
if (ch == '\n' && *linepos > 0 && buffer[*linepos - 1] == '\r')
{
/* NUL terminate (replaces \r in buffer), notify app, clear */
buffer[*linepos - 1] = 0;
cb(buffer, 0, ud);
*linepos = 0;
/* CRNUL -- just a CR */
}
else if (ch == 0 && *linepos > 0 && buffer[*linepos - 1] == '\r')
{
/* So nothing, the CR is already in the buffer */
/* Anything else (including technically invalid CR followed by anything
* besides LF or NUL -- just buffer if we have room \r
*/
}
else if (*linepos != size)
{
buffer[(*linepos)++] = ch;
/* Buffer overflow */
}
else
{
/* Terminate (NOTE: eats a byte), notify app, clear buffer */
buffer[size - 1] = 0;
cb(buffer, size - 1, ud);
*linepos = 0;
}
}
static void _message(const char *from, const char *msg)
{
int i;
for (i = 0; i != MAX_USERS; ++i)
{
if (g_users[i].sock != -1)
{
telnet_printf(g_users[i].telnet, "%s: %s\n", from, msg);
}
}
}
static void _send(int sock, const char *buffer, unsigned int size)
{
int ret;
/* Ignore on invalid socket */
if (sock == -1)
{
return;
}
/* Send data */
while (size > 0)
{
if ((ret = send(sock, buffer, size, 0)) == -1)
{
if (errno != EINTR && errno != ECONNRESET)
{
fprintf(stderr, "send() failed: %d\n", errno);
exit(1);
}
else
{
return;
}
}
else if (ret == 0)
{
fprintf(stderr, "send() unexpectedly returned 0\n");
exit(1);
}
/* Update pointer and size to see if we've got more to send */
buffer += ret;
size -= ret;
}
}
/* Process input line */
static void _online(const char *line, int overflow, void *ud)
{
struct user_s *user = (struct user_s *)ud;
int i;
/* If the user has no name, this is his "login" */
if (user->name == 0)
{
/* Must not be empty, must be at least 32 chars */
if (strlen(line) == 0 || strlen(line) > 32)
{
telnet_printf(user->telnet, "Invalid name.\nEnter name: ");
return;
}
/* Must not already be in use */
for (i = 0; i != MAX_USERS; ++i)
{
if (g_users[i].name != 0 && strcmp(g_users[i].name, line) == 0)
{
telnet_printf(user->telnet, "Name in use.\nEnter name: ");
return;
}
}
/* Keep name */
user->name = strdup(line);
telnet_printf(user->telnet, "Welcome, %s!\n", line);
return;
}
/* If line is "quit" then, well, quit */
if (strcmp(line, "quit") == 0)
{
close(user->sock);
user->sock = -1;
_message(user->name, "** HAS QUIT **");
free(user->name);
user->name = 0;
return;
}
/* Just a message -- send to all users */
_message(user->name, line);
}
static void _input(struct user_s *user, const char *buffer, unsigned int size)
{
unsigned int i;
for (i = 0; i != size; ++i)
{
linebuffer_push(user->linebuf, sizeof(user->linebuf), &user->linepos,
(char)buffer[i], _online, user);
}
}
static void _event_handler(struct telnet_s *telnet,
union telnet_event_u *ev, void *user_data)
{
struct user_s *user = (struct user_s *)user_data;
switch (ev->type)
{
/* Data received */
case TELNET_EV_DATA:
_input(user, ev->data.buffer, ev->data.size);
telnet_negotiate(telnet, TELNET_WONT, TELNET_TELOPT_ECHO);
telnet_negotiate(telnet, TELNET_WILL, TELNET_TELOPT_ECHO);
break;
/* Data must be sent */
case TELNET_EV_SEND:
_send(user->sock, ev->data.buffer, ev->data.size);
break;
/* Enable compress2 if accepted by client */
case TELNET_EV_DO:
if (ev->neg.telopt == TELNET_TELOPT_COMPRESS2)
{
telnet_begin_compress2(telnet);
}
break;
/* Error */
case TELNET_EV_ERROR:
close(user->sock);
user->sock = -1;
if (user->name != 0)
{
_message(user->name, "** HAS HAD AN ERROR **");
free(user->name);
user->name = 0;
}
telnet_free(user->telnet);
break;
default:
/* Ignore */
break;
}
}
/****************************************************************************
* Public Functions
****************************************************************************/
#ifdef CONFIG_BUILD_KERNEL
int main(int argc, FAR char *argv[])
#else
int chatd_main(int argc, char *argv[])
#endif
{
char buffer[512];
short listen_port;
int listen_sock;
int ret;
int i;
struct sockaddr_in addr;
socklen_t addrlen;
struct pollfd pfd[MAX_USERS + 1];
/* Check usage */
if (argc != 2)
{
fprintf(stderr, "Usage:\n %s <port>\n", argv[0]);
return 1;
}
/* Initialize data structures */
memset(&pfd, 0, sizeof(pfd));
memset(g_users, 0, sizeof(g_users));
for (i = 0; i != MAX_USERS; ++i)
{
g_users[i].sock = -1;
}
/* Parse listening port */
listen_port = (short)strtol(argv[1], 0, 10);
/* Create listening socket */
if ((listen_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
fprintf(stderr, "socket() failed: %d\n", errno);
return 1;
}
/* Reuse address option */
ret = 1;
setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, (void *)&ret, sizeof(ret));
/* Bind to listening addr/port */
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(listen_port);
if (bind(listen_sock, (struct sockaddr *)&addr, sizeof(addr)) == -1)
{
fprintf(stderr, "bind() failed: %d\n", errno);
return 1;
}
/* Listen for clients */
if (listen(listen_sock, 5) == -1)
{
fprintf(stderr, "listen() failed: %d\n", errno);
return 1;
}
printf("LISTENING ON PORT %d\n", listen_port);
/* Initialize listening descriptors */
pfd[MAX_USERS].fd = listen_sock;
pfd[MAX_USERS].events = POLLIN;
/* Loop for ever */
for (;;)
{
/* Prepare for poll */
for (i = 0; i != MAX_USERS; ++i)
{
if (g_users[i].sock != -1)
{
pfd[i].fd = g_users[i].sock;
pfd[i].events = POLLIN;
}
else
{
pfd[i].fd = -1;
pfd[i].events = 0;
}
}
/* Poll */
ret = poll(pfd, MAX_USERS + 1, -1);
if (ret == -1 && errno != EINTR)
{
fprintf(stderr, "poll() failed: %d\n", errno);
return 1;
}
/* New connection */
if (pfd[MAX_USERS].revents & POLLIN)
{
/* Accept the sock */
addrlen = sizeof(addr);
if ((ret = accept(listen_sock, (struct sockaddr *)&addr,
&addrlen)) == -1)
{
fprintf(stderr, "accept() failed: %d\n", errno);
return 1;
}
printf("Connection received.\n");
/* Find a free user */
for (i = 0; i != MAX_USERS; ++i)
{
if (g_users[i].sock == -1)
{
break;
}
}
if (i == MAX_USERS)
{
printf(" rejected (too many users)\n");
_send(ret, "Too many users.\r\n", 14);
close(ret);
}
/* Init, welcome */
g_users[i].sock = ret;
g_users[i].telnet = telnet_init(g_telopts, _event_handler, 0, &g_users[i]);
telnet_negotiate(g_users[i].telnet, TELNET_WILL,
TELNET_TELOPT_COMPRESS2);
telnet_printf(g_users[i].telnet, "Enter name: ");
telnet_negotiate(g_users[i].telnet, TELNET_WILL, TELNET_TELOPT_ECHO);
}
/* Read from client */
for (i = 0; i != MAX_USERS; ++i)
{
/* Skip users that aren't actually connected */
if (g_users[i].sock == -1)
{
continue;
}
if (pfd[i].revents & POLLIN)
{
if ((ret = recv(g_users[i].sock, buffer, sizeof(buffer), 0)) > 0)
{
telnet_recv(g_users[i].telnet, buffer, ret);
}
else if (ret == 0)
{
printf("Connection closed.\n");
close(g_users[i].sock);
if (g_users[i].name != 0)
{
_message(g_users[i].name, "** HAS DISCONNECTED **");
free(g_users[i].name);
g_users[i].name = 0;
}
telnet_free(g_users[i].telnet);
g_users[i].sock = -1;
break;
}
else if (errno != EINTR)
{
fprintf(stderr, "recv(client) failed: %d\n", errno);
exit(1);
}
}
}
}
/* Not that we can reach this, but GCC will cry if it's not here */
return 0;
}

View File

@ -0,0 +1,457 @@
/****************************************************************************
* apps/system/telnet/telnet_client.c
*
* Leveraged from libtelnet, https://github.com/seanmiddleditch/libtelnet.
* Modified and re-released under the BSD license:
*
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* The original authors of libtelnet are listed below. Per their licesne,
* "The author or authors of this code dedicate any and all copyright
* interest in this code to the public domain. We make this dedication for
* the benefit of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* code under copyright law."
*
* Author: Sean Middleditch <sean@sourcemud.org>
* (Also listed in the AUTHORS file are Jack Kelly <endgame.dos@gmail.com>
* and Katherine Flavel <kate@elide.org>)
*
* 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 NuttX 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 COPYRIGHT HOLDERS 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
* COPYRIGHT OWNER 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 <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <poll.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#ifdef HAVE_ZLIB
# include "zlib.h"
#endif
#include "system/readline.h"
#include "netutils/telnetc.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifdef CONFIG_NSH_TELNETD_PORT
# define DEFAULT_PORT CONFIG_NSH_TELNETD_PORT
#else
# define DEFAULT_PORT 23
#endif
/****************************************************************************
* Private Data
****************************************************************************/
static struct telnet_s *g_telnet;
static int g_echo;
static const struct telnet_telopt_s g_telopts[] =
{
{
TELNET_TELOPT_ECHO, TELNET_WONT, TELNET_DO
},
{
TELNET_TELOPT_TTYPE, TELNET_WILL, TELNET_DONT
},
{
TELNET_TELOPT_COMPRESS2, TELNET_WONT, TELNET_DO
},
{
TELNET_TELOPT_MSSP, TELNET_WONT, TELNET_DO
},
{
-1, 0, 0
}
};
/****************************************************************************
* Private Functions
****************************************************************************/
static void send_local_input(char *buffer, int size)
{
static char crlf[] = { '\r', '\n' };
int i;
for (i = 0; i != size; ++i)
{
/* If we got a CR or LF, replace with CRLF NOTE that usually you'd get a
* CR in UNIX, but in raw mode we get LF instead (not sure why).
*/
if (buffer[i] == '\r' || buffer[i] == '\n')
{
if (g_echo)
{
printf("\r\n");
}
telnet_send(g_telnet, crlf, 2);
}
else
{
if (g_echo)
{
putchar(buffer[i]);
}
telnet_send(g_telnet, buffer + i, 1);
}
}
fflush(stdout);
}
static void telnet_ev_send(int sock, const char *buffer, size_t size)
{
int ret;
/* Send data */
while (size > 0)
{
if ((ret = send(sock, buffer, size, 0)) == -1)
{
fprintf(stderr, "send() failed: %d\n", errno);
exit(1);
}
else if (ret == 0)
{
fprintf(stderr, "send() unexpectedly returned 0\n");
exit(1);
}
/* Update pointer and size to see if we've got more to send */
buffer += ret;
size -= ret;
}
}
static void _event_handler(struct telnet_s *telnet,
union telnet_event_u *ev, void *user_data)
{
int sock = *(int *)user_data;
switch (ev->type)
{
/* Data received */
case TELNET_EV_DATA:
printf("%.*s", (int)ev->data.size, ev->data.buffer);
fflush(stdout);
break;
/* Data must be sent */
case TELNET_EV_SEND:
telnet_ev_send(sock, ev->data.buffer, ev->data.size);
break;
/* Request to enable remote feature (or receipt) */
case TELNET_EV_WILL:
/* We'll agree to turn off our echo if server wants us to stop */
if (ev->neg.telopt == TELNET_TELOPT_ECHO)
{
g_echo = 0;
}
break;
/* Notification of disabling remote feature (or receipt) */
case TELNET_EV_WONT:
if (ev->neg.telopt == TELNET_TELOPT_ECHO)
{
g_echo = 1;
}
break;
/* Request to enable local feature (or receipt) */
case TELNET_EV_DO:
break;
/* Demand to disable local feature (or receipt) */
case TELNET_EV_DONT:
break;
/* Respond to TTYPE commands */
case TELNET_EV_TTYPE:
/* Respond with our terminal type, if requested */
if (ev->ttype.cmd == TELNET_TTYPE_SEND)
{
telnet_ttype_is(telnet, getenv("TERM"));
}
break;
/* Respond to particular subnegotiations */
case TELNET_EV_SUBNEGOTIATION:
break;
/* Error */
case TELNET_EV_ERROR:
fprintf(stderr, "ERROR: %s\n", ev->error.msg);
exit(1);
default:
/* Ignore */
break;
}
}
static void show_usage(const char *progname, int exitcode)
{
fprintf(stderr, "Usage:\n");
fprintf(stderr, "\t%s <server-IP-addr> [<port>]\n", progname);
fprintf(stderr, "Where:\n");
fprintf(stderr, "\t<server-IP-addr> is the address of the Telnet server. Either\n");
fprintf(stderr, "\t\tIPv4 form: ddd.ddd.ddd.ddd\n");
fprintf(stderr, "\t\tIPv6 form: xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx\n");
fprintf(stderr, "\t<port> is the (optional) listening port of the Telnet server.\n");
fprintf(stderr, "\t\tDefault: %u\n", DEFAULT_PORT);
exit(exitcode) ;
}
/****************************************************************************
* Public Functions
****************************************************************************/
#ifdef CONFIG_BUILD_KERNEL
int main(int argc, FAR char *argv[])
#else
int telnet_main(int argc, char *argv[])
#endif
{
char buffer[512];
union
{
struct sockaddr generic;
#ifdef CONFIG_NET_IPv6
struct sockaddr_in6 ipv6;
#endif
#ifdef CONFIG_NET_IPv4
struct sockaddr_in ipv4;
#endif
} server;
union
{
#ifdef CONFIG_NET_IPv6
struct sockaddr_in6 ipv6;
#endif
#ifdef CONFIG_NET_IPv4
struct sockaddr_in ipv4;
#endif
} local;
struct pollfd pfd[2];
sa_family_t family;
uint16_t addrlen;
int portno;
int sock;
int ret;
/* Check usage */
if (argc < 2 || argc > 3)
{
fprintf(stderr, "Invalid number of arguments\n");
show_usage(argv[0], 1);
}
/* Convert the port number to binary if provided */
if (argc == 3)
{
portno = atoi(argv[2]);
if (portno < 0 || portno > UINT16_MAX)
{
fprintf(stderr, "Invalid port number\n");
show_usage(argv[0], 1);
}
}
else
{
portno = DEFAULT_PORT;
}
/* Convert the <server-IP-addr> argument into a binary address */
memset(&local, 0, sizeof(local));
#ifdef CONFIG_EXAMPLES_NETTEST_IPv6
addrlen = sizeof(struct sockaddr_in6);
family = AF_INET6;
local.ipv6.sin6_family = AF_INET6;
server.ipv6.sin6_family = AF_INET6;
server.ipv6.sin6_port = htons(portno);
ret = inet_pton(AF_INET6, argv[1], server.ipv6.sin6_addr.s6_addr);
if (ret < 0)
#endif
#ifdef CONFIG_EXAMPLES_NETTEST_IPv
{
addrlen = sizeof(struct sockaddr_in);
family = AF_INET;
local.ipv4.sin_family = AF_INET;
server.ipv4.sin_family = AF_INET;
server.ipv4.sin_port = htons(portno);
ret = inet_pton(AF_INET, argv[1], &server.ipv4.sin_addr);
}
if (ret < 0)
#endif
{
fprintf(stderr, "ERROR: <server-IP-addr> is invalid\n");
show_usage(argv[0], 1);
}
/* Create server socket */
sock = socket(family, SOCK_STREAM, 0);
if (sock < 0)
{
fprintf(stderr, "socket() failed: %d\n", errno);
return 1;
}
/* Bind server socket */
ret = bind(sock, (struct sockaddr *)&local, addrlen);
if (ret < 0)
{
fprintf(stderr, "bind() failed: %d\n", errno);
return 1;
}
/* Connect */
ret = connect(sock, &server.generic, addrlen);
if (ret < 0)
{
fprintf(stderr, "connect() failed: %d\n", errno);
return 1;
}
/* Set input echoing on by default */
g_echo = 1;
/* Initialize telnet box */
g_telnet = telnet_init(g_telopts, _event_handler, 0, &sock);
/* Initialize poll descriptors */
memset(pfd, 0, sizeof(pfd));
pfd[0].fd = 1;
pfd[0].events = POLLIN;
pfd[1].fd = sock;
pfd[1].events = POLLIN;
/* Loop while both connections are open */
while (poll(pfd, 2, -1) != -1)
{
/* Read from stdin */
if (pfd[0].revents & POLLIN)
{
ret = std_readline(buffer, sizeof(buffer));
if (ret > 0)
{
send_local_input(buffer, ret);
}
else if (ret == 0)
{
break;
}
else
{
fprintf(stderr, "recv(server) failed: %d\n", errno);
exit(1);
}
}
/* Read from client */
if (pfd[1].revents & POLLIN)
{
if ((ret = recv(sock, buffer, sizeof(buffer), 0)) > 0)
{
telnet_recv(g_telnet, buffer, ret);
}
else if (ret == 0)
{
break;
}
else
{
fprintf(stderr, "recv(client) failed: %d\n", errno);
exit(1);
}
}
}
/* Clean up */
telnet_free(g_telnet);
close(sock);
return 0;
}

View File

@ -48,7 +48,7 @@ STACKSIZE = 4096
ASRCS =
CSRCS = i8sak_acceptassoc.c i8sak_assoc.c i8sak_scan.c i8sak_blaster.c i8sak_poll.c
CSRCS += i8sak_sniffer.c i8sak_startpan.c i8sak_tx.c i8sak_chan.c
CSRCS += i8sak_sniffer.c i8sak_startpan.c i8sak_tx.c i8sak_chan.c i8sak_coordinfo.c
CSRCS += wpanlistener.c
MAINSRC = i8sak_main.c

View File

@ -99,7 +99,14 @@
/* /dev/ is 5 characters */
#define I8SAK_DAEMONNAME_FMTLEN (6 + (I8SAK_MAX_DEVNAME-5) + 1)
/* Helper Macros *************************************************************/
#define PRINT_COORDEADDR(eaddr) \
printf(" Coordinator EADDR: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n", \
eaddr[0], eaddr[1], eaddr[2], eaddr[3], eaddr[4], eaddr[5], eaddr[6], eaddr[7]);
#define PRINT_COORDSADDR(saddr) \
printf(" Coordinator SADDR: %02X:%02X\n", saddr[0], saddr[1]);
/****************************************************************************
* Public Types
@ -183,7 +190,8 @@ void i8sak_tx_cmd (FAR struct i8sak_s *i8sak, int argc, FAR char *argv[
void i8sak_poll_cmd (FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]);
void i8sak_sniffer_cmd (FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]);
void i8sak_blaster_cmd (FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]);
void i8sak_chan_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]);
void i8sak_chan_cmd (FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]);
void i8sak_coordinfo_cmd (FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]);
/****************************************************************************
* Inline Functions

View File

@ -0,0 +1,141 @@
/****************************************************************************
* apps/wireless/ieee802154/i8sak/i8sak_coordinfo.c
* IEEE 802.15.4 Swiss Army Knife
*
* Copyright (C) 2017 Verge Inc. All rights reserved.
* Author: Anthony Merlino <anthony@vergeaero.com>
*
* 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 NuttX 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 COPYRIGHT HOLDERS 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
* COPYRIGHT OWNER 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <nuttx/fs/ioctl.h>
#include "i8sak.h"
#include <nuttx/wireless/ieee802154/ieee802154_ioctl.h>
#include <nuttx/wireless/ieee802154/ieee802154_mac.h>
#include "wireless/ieee802154.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name : i8sak_coordinfo_cmd
*
* Description :
* Print various coordinator attributes
****************************************************************************/
void i8sak_coordinfo_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[])
{
int option;
int fd;
int argind;
bool geteaddr = false;
bool getsaddr = false;
uint8_t eaddr[IEEE802154_EADDRSIZE];
uint8_t saddr[IEEE802154_SADDRSIZE];
argind = 1;
while ((option = getopt(argc, argv, ":haes")) != ERROR)
{
switch (option)
{
argind++;
case 'h':
fprintf(stderr, "Prints coordinator attributes\n"
"Usage: %s [-h]\n"
" -h = this help menu\n"
, argv[0]);
/* Must manually reset optind if we are going to exit early */
optind = -1;
return;
case 'a':
geteaddr = true;
getsaddr = true;
break;
case 'e':
geteaddr = true;
break;
case 's':
getsaddr = true;
break;
case ':':
fprintf(stderr, "ERROR: missing argument\n");
/* Must manually reset optind if we are going to exit early */
optind = -1;
i8sak_cmd_error(i8sak); /* This exits for us */
case '?':
fprintf(stderr, "ERROR: unknown argument\n");
/* Must manually reset optind if we are going to exit early */
optind = -1;
i8sak_cmd_error(i8sak); /* This exits for us */
}
}
fd = open(i8sak->devname, O_RDWR);
if (fd < 0)
{
printf("cannot open %s, errno=%d\n", i8sak->devname, errno);
i8sak_cmd_error(i8sak);
}
if (geteaddr)
{
ieee802154_getcoordeaddr(fd, eaddr);
PRINT_COORDEADDR(eaddr);
}
if (getsaddr)
{
ieee802154_getcoordsaddr(fd, saddr);
PRINT_COORDSADDR(saddr);
}
close(fd);
}

View File

@ -110,6 +110,7 @@ static const struct i8sak_command_s g_i8sak_commands[] =
{"sniffer", (CODE void *)i8sak_sniffer_cmd},
{"blaster", (CODE void *)i8sak_blaster_cmd},
{"chan", (CODE void *)i8sak_chan_cmd},
{"coordinfo", (CODE void *)i8sak_coordinfo_cmd},
};
#define NCOMMANDS (sizeof(g_i8sak_commands) / sizeof(struct i8sak_command_s))
@ -738,7 +739,8 @@ static int i8sak_showusage(FAR const char *progname, int exitcode)
" poll [-h]\n"
" blaster [-h|q|f <hex payload>|p <period_ms>]\n"
" sniffer [-h|q]\n"
" chan [-h|g] [<chan>]"
" chan [-h|g] [<chan>]\n"
" coordinfo [-h|a|e|s]\n"
, progname);
exit(exitcode);
}

View File

@ -233,21 +233,11 @@ static void scan_eventcb(FAR struct ieee802154_notif_s *notif, FAR void *arg)
if (scan->pandescs[i].coordaddr.mode == IEEE802154_ADDRMODE_SHORT)
{
printf(" Coordinator saddr: %02X:%02X\n",
scan->pandescs[i].coordaddr.saddr[0],
scan->pandescs[i].coordaddr.saddr[1]);
PRINT_COORDSADDR(scan->pandescs[i].coordaddr.saddr);
}
else
{
printf(" Coordinator eaddr: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n",
scan->pandescs[i].coordaddr.eaddr[0],
scan->pandescs[i].coordaddr.eaddr[1],
scan->pandescs[i].coordaddr.eaddr[2],
scan->pandescs[i].coordaddr.eaddr[3],
scan->pandescs[i].coordaddr.eaddr[4],
scan->pandescs[i].coordaddr.eaddr[5],
scan->pandescs[i].coordaddr.eaddr[6],
scan->pandescs[i].coordaddr.eaddr[7]);
PRINT_COORDEADDR(scan->pandescs[i].coordaddr.eaddr);
}
}
}

View File

@ -54,7 +54,10 @@ CSRCS += ieee802154_seteaddr.c ieee802154_geteaddr.c
CSRCS += ieee802154_setpromisc.c ieee802154_getpromisc.c
CSRCS += ieee802154_setrxonidle.c ieee802154_getrxonidle.c
CSRCS += ieee802154_settxpwr.c ieee802154_gettxpwr.c
CSRCS += ieee802154_getdevmode.c ieee802154_setassocpermit.c
CSRCS += ieee802154_getdevmode.c
CSRCS += ieee802154_setassocpermit.c
CSRCS += ieee802154_getcoordsaddr.c
CSRCS += ieee802154_getcoordeaddr.c
ifeq ($(CONFIG_NET_6LOWPAN),y)
# Add Get/Set Attribute helpers

View File

@ -0,0 +1,67 @@
/****************************************************************************
* apps/wireless/ieee802154/libmac/ieee802154_getcoordeaddr.c
*
* Copyright (C) 2017 Verge Inc. All rights reserved.
* Author: Anthony Merlino <anthony@vergeaero.com>
*
* 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 NuttX 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 COPYRIGHT HOLDERS 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
* COPYRIGHT OWNER 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/ioctl.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <nuttx/wireless/ieee802154/ieee802154_mac.h>
#include "wireless/ieee802154.h"
/****************************************************************************
* Public Functions
****************************************************************************/
int ieee802154_getcoordeaddr(int fd, FAR uint8_t *eaddr)
{
struct ieee802154_get_req_s req;
int ret;
req.attr = IEEE802154_ATTR_MAC_COORD_EADDR;
ret = ieee802154_get_req(fd, &req);
IEEE802154_EADDRCOPY(eaddr, req.attrval.mac.eaddr);
return ret;
}

View File

@ -0,0 +1,66 @@
/****************************************************************************
* apps/wireless/ieee802154/libmac/ieee802154_getcoordsaddr.c
*
* Copyright (C) 2017 Verge Inc. All rights reserved.
* Author: Anthony Merlino <anthony@vergeaero.com>
*
* 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 NuttX 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 COPYRIGHT HOLDERS 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
* COPYRIGHT OWNER 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/ioctl.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <nuttx/wireless/ieee802154/ieee802154_mac.h>
#include "wireless/ieee802154.h"
/****************************************************************************
* Public Functions
****************************************************************************/
int ieee802154_getcoordsaddr(int fd, FAR uint8_t *saddr)
{
struct ieee802154_get_req_s req;
int ret;
req.attr = IEEE802154_ATTR_MAC_COORD_SADDR;
ret = ieee802154_get_req(fd, &req);
IEEE802154_SADDRCOPY(saddr, req.attrval.mac.saddr);
return ret;
}