Functions

Functions

int TelnetPrintf (const char *format,...)
 Custom Telnet printf function. More...
 
int32_t TelnetWriteChar (char c)
 append a char to telnet write buffer More...
 
void Telnet_send_option (SOCKET *tcp_server, unsigned char option, unsigned char c)
 send a Telnet option More...
 
int Telnet_check_message (telnet_server_type *telnet)
 Parse user commands. More...
 
uint32_t Telnet_initialize_server (user_type *user, char *hello_msg, int16_t(*InputParser)(T_ComDevice *, char), T_Handle CmdTab)
 initialize Telnet Server More...
 
telnet_return_typeTelnet_server (void)
 handle Telnet Server More...
 

Detailed Description

Function Documentation

int TelnetPrintf ( const char *  format,
  ... 
)
Parameters
format- format string
Returns
amount of printed characters
164 {
165  /***************************************************************************
166  locals
167  ***************************************************************************/
168  va_list argptr;
169  int cnt = 0;
170 
171  if ( tcp_get_state (telnet.server) != TCP_ESTABLISHED ) return (0);
172 
173  /***************************************************************************
174  initializes variable argument list
175  ***************************************************************************/
176 #pragma diag_suppress 238
177  va_start (argptr, format);
178 #pragma diag_default 238
179 
180  /***************************************************************************
181  print to buffer
182  ***************************************************************************/
183  cnt = vsprintf (&telnet.outbuffer[telnet.out_wptr], format, argptr);
184 
185  /***************************************************************************
186  vsprintf returns the amount of printed characters. The user buffer size is
187  limited to TELNET_OUTPUT_BUFFER_SIZE. Check for buffer overflow and exit if
188  necessary.
189  ***************************************************************************/
190  telnet.out_wptr+=cnt;
192  {
194  prg_exit ("buffer overflow");
195  }
196 
197  /***************************************************************************
198  end variable argument list
199  ***************************************************************************/
200  va_end(argptr);
201 
202  return (cnt);
203 }
#define prg_exit(s)
Definition: Common.h:267
uint32_t exit_code
Definition: Common.c:104
int16_t tcp_get_state(SOCKET *so)
Determine tcp state.
#define BUFFEROVERFLOW_ERROR
Definition: Common.h:383
static telnet_server_type telnet
Definition: telnetio.c:147
#define TELNET_OUTPUT_BUFFER_SIZE
Definition: telnetio.h:72
#define TCP_ESTABLISHED
Definition: net.h:693
SOCKET * server
Definition: telnetio.h:134
uint32_t out_wptr
Definition: telnetio.h:137
char outbuffer[TELNET_OUTPUT_BUFFER_SIZE]
Definition: telnetio.h:136
int32_t TelnetWriteChar ( char  c)
Parameters
c- character to send
Returns
amount of printed characters
252 {
253  int32_t cnt = 0;
254 
255  /***************************************************************************
256  print only if connection established
257  ***************************************************************************/
258  if ( tcp_get_state (telnet.server) != TCP_ESTABLISHED ) return (0);
259 
260  /***************************************************************************
261  print to buffer
262  ***************************************************************************/
263  cnt = 1;
265 
266  /***************************************************************************
267  The user buffer size is limited to TELNET_OUTPUT_BUFFER_SIZE. Check for
268  buffer overflow and exit if necessary.
269  ***************************************************************************/
270  telnet.out_wptr+=cnt;
273  {
275  prg_exit ("buffer overflow");
276  }
277 
278  return (cnt);
279 }
#define prg_exit(s)
Definition: Common.h:267
uint32_t exit_code
Definition: Common.c:104
int16_t tcp_get_state(SOCKET *so)
Determine tcp state.
#define BUFFEROVERFLOW_ERROR
Definition: Common.h:383
static telnet_server_type telnet
Definition: telnetio.c:147
#define TELNET_OUTPUT_BUFFER_SIZE
Definition: telnetio.h:72
#define TCP_ESTABLISHED
Definition: net.h:693
SOCKET * server
Definition: telnetio.h:134
uint32_t out_wptr
Definition: telnetio.h:137
int int32_t
Definition: stdint.h:46
char outbuffer[TELNET_OUTPUT_BUFFER_SIZE]
Definition: telnetio.h:136
void Telnet_send_option ( SOCKET *  tcp_server,
unsigned char  option,
unsigned char  c 
)
Parameters
tcp_server- current connection
option- option to send
c- option value
Returns
nothing
See also
net_send_ready() - netlib
293 {
294  /***************************************************************************
295  locals
296  ***************************************************************************/
297 
298  telnet_option_msg[1] = option;
299  telnet_option_msg[2] = c;
301 }
unsigned char telnet_option_msg[]
Definition: telnetio.c:127
SOCKET * tcp_server
Definition: NetTest.c:273
int32_t net_send_ready(SOCKET *so, void *data, uint32_t len, uint32_t timeout)
Send a message via the specified socket, wait until message is successfully transmitted or a timeout ...
int Telnet_check_message ( telnet_server_type telnet)
Parameters
telnet- connected TCP server socket
Returns
  • ( 0) Normal return
  • (>0) After successful up/download or error
314 {
315  /***************************************************************************
316  locals
317  ***************************************************************************/
318  int len=0; /* receive length */
319  char c;
320  char *cp;
321  int16_t ret = 0;
322  tpInputParser InputParser =
323 #ifdef INC_CMD
324  (tpInputParser)(int32_t)CMD_process_user_input;
325 #else
327 #endif
328 
329  /***************************************************************************
330  new message received ?
331  ***************************************************************************/
332  if (net_recv (telnet-> server, telnet-> inbuffer, TCP_MAX_PACKET_SIZE))
333  {
334  len = net_recv_event_handler (telnet-> server, NULL);
335  }
336  if (len)
337  {
338  /***********************************************************************
339  parse buffer
340  ***********************************************************************/
341  cp = telnet-> inbuffer;
342  while ( len )
343  {
344  /*******************************************************************
345  get one character
346  *******************************************************************/
347  c = *cp++; len--;
348 
349  /*******************************************************************
350  parse telnet protocol
351  *******************************************************************/
352  switch ( telnet-> state )
353  {
354  case TSTATE_NORMAL:
355  if ((unsigned char)c == TELNET_IAC)
356  {
357  telnet-> state = TSTATE_IAC;
358  }
359  else
360  {
361  /*******************************************************
362  process user command
363  *******************************************************/
364  switch ((unsigned char)c)
365  {
366  case TELNET_IAC :
367  case TELNET_WILL:
368  case TELNET_WONT:
369  case TELNET_DO :
370  case TELNET_DONT:
371  /***********************************************
372  ignore telnet control characters
373  ***********************************************/
374  break;
375 
376  default:
377  /***********************************************
378  parse character
379  ***********************************************/
380  ret = InputParser (&telnet->CmdDevice, c);
381  if (ret)
382  {
383  len = 0;
384  }
385  break;
386  }
387  }
388  break;
389 
390  case TSTATE_IAC:
391  if ((unsigned char)c == TELNET_IAC)
392  {
393  /*******************************************************
394  not valid, back to normal state
395  *******************************************************/
396  telnet-> state = TSTATE_NORMAL;
397  }
398  else
399  {
400  /*******************************************************
401  telnet option
402  *******************************************************/
403  switch ((unsigned char)c)
404  {
405  case TELNET_WILL:
406  telnet-> state = TSTATE_WILL;
407  break;
408 
409  case TELNET_WONT:
410  telnet-> state = TSTATE_WONT;
411  break;
412 
413  case TELNET_DO:
414  telnet-> state = TSTATE_DO;
415  break;
416 
417  case TELNET_DONT:
418  telnet-> state = TSTATE_DONT;
419  break;
420 
421  default:
422  telnet-> state = TSTATE_NORMAL;
423  break;
424  }
425  }
426  break;
427 
428  case TSTATE_WILL:
429  /***********************************************************
430  Reply with a DONT
431  ***********************************************************/
432  Telnet_send_option (telnet-> server, TELNET_DONT, c);
433  telnet-> state = TSTATE_NORMAL;
434  break;
435 
436  case TSTATE_WONT:
437  /***********************************************************
438  Reply with a DONT
439  ***********************************************************/
440  Telnet_send_option (telnet-> server, TELNET_DONT, c);
441  telnet-> state = TSTATE_NORMAL;
442  break;
443 
444  case TSTATE_DO:
445  /***********************************************************
446  Reply with a WONT
447  ***********************************************************/
448  Telnet_send_option (telnet-> server, TELNET_WONT, c);
449  telnet-> state = TSTATE_NORMAL;
450  break;
451 
452  case TSTATE_DONT:
453  /***********************************************************
454  Reply with a WONT
455  ***********************************************************/
456  Telnet_send_option (telnet-> server, TELNET_WONT, c);
457  telnet-> state = TSTATE_NORMAL;
458  break;
459 
460  default :
461  telnet-> state = TSTATE_NORMAL;
462  break;
463  } // switch
464  } // while
465  }
466 
467  return ((int)ret);
468 }
#define TELNET_DONT
Definition: telnetio.h:69
#define TELNET_WONT
Definition: telnetio.h:67
short int16_t
Definition: stdint.h:44
Definition: telnetio.h:119
Definition: telnetio.h:120
#define NULL
Definition: net.h:126
int32_t net_recv(SOCKET *so, void *data, uint16_t maxdatasize)
Receive data via the specified socket.
Definition: telnetio.h:122
#define TCP_MAX_PACKET_SIZE
Definition: net.h:681
Definition: telnetio.h:117
Definition: telnetio.h:118
#define TELNET_DO
Definition: telnetio.h:68
T_Handle InputParser
Definition: Common.h:340
Definition: telnetio.h:121
#define TELNET_WILL
Definition: telnetio.h:66
#define TELNET_IAC
Definition: telnetio.h:65
void Telnet_send_option(SOCKET *tcp_server, unsigned char option, unsigned char c)
send a Telnet option
Definition: telnetio.c:292
int16_t(* tpInputParser)(T_ComDevice *, char)
Definition: Common.h:355
T_ComDevice CmdDevice
Definition: telnetio.h:146
int int32_t
Definition: stdint.h:46
uint32_t Telnet_initialize_server ( user_type user,
char *  hello_msg,
int16_t(*)(T_ComDevice *, char)  InputParser,
T_Handle  CmdTab 
)
Parameters
*user- pointer to user list
*hello_msg- user defined welcome message
*InputParser- input parser
CmdTab- command table
Returns
  • TRUE - success
  • FALSE - error
487 {
488  /***************************************************************************
489  locals
490  ***************************************************************************/
491  /* - */
492 
493  /***************************************************************************
494  initialize Telnet server
495  ***************************************************************************/
498  telnet.out_wptr = 0;
499  telnet.hello_msg = hello_msg;
500  // telnet_user_list = user;
501  (void)user;
502 
503  /***************************************************************************
504  create TCP data server socket on telnet port
505  ***************************************************************************/
507  ANY_PORT,
508  TELNET_PORT,
510  TCP_INIT_FUNC);
511  if ( telnet.server == INVALID_SOCKET )
512  {
513  return (FALSE);
514  }
515 
516  /***************************************************************************
517  set receive-buffer size
518  ***************************************************************************/
520 
521  /***************************************************************************
522  configure command processor
523  ***************************************************************************/
530 #ifdef INC_CMD
531  aTelnetCmdLine;
532 #else
533  NULL;
534 #endif
535  telnet.CmdDevice.InputParser = (T_Handle)InputParser;
536  telnet.CmdDevice.CmdTab = CmdTab;
537 
538  return (TRUE);
539 }
int32_t set_recv_buffer(SOCKET *so, void *data, uint16_t maxdatasize)
Define a socket buffer.
TelnetServerState_t state
Definition: telnetio.h:141
void TelnetWriteStr(const char *buffer)
append a zero terminated string to telnet write buffer
Definition: telnetio.c:213
#define ANY_PORT
Definition: net.h:519
T_Handle gp
Definition: Common.h:338
tpWriteChar out
Definition: Common.h:334
#define NULL
Definition: net.h:126
int32_t TelnetWriteChar(char c)
append a char to telnet write buffer
Definition: telnetio.c:251
T_Handle CmdTab
Definition: Common.h:339
#define TCP_MAX_PACKET_SIZE
Definition: net.h:681
Definition: telnetio.h:117
char * aCmdLine
Definition: Common.h:341
#define TCP_INIT_FUNC
Definition: net.h:499
static telnet_server_type telnet
Definition: telnetio.c:147
TelnetSocketState_t so_state
Definition: telnetio.h:140
SOCKET * socket_open(char *dest_addr, uint16_t dest_port, uint16_t src_port, uint8_t data_type, int32_t(*init_func)(SOCKET *))
Create a new socket.
T_Handle InputParser
Definition: Common.h:340
#define COM_DEV_TELNET
Definition: Common.h:347
char * hello_msg
Definition: telnetio.h:138
#define ANY_ADDRESS
Definition: net.h:511
SOCKET * server
Definition: telnetio.h:134
char inbuffer[TCP_MAX_PACKET_SIZE]
Definition: telnetio.h:135
#define DATATYPE_CHAR
Definition: net.h:489
uint32_t out_wptr
Definition: telnetio.h:137
T_ComDevice CmdDevice
Definition: telnetio.h:146
int TelnetPrintf(const char *format,...)
Custom Telnet printf function.
Definition: telnetio.c:163
uint16_t dev
Definition: Common.h:342
#define INVALID_SOCKET
Definition: net.h:540
Definition: telnetio.h:105
#define T_Handle
Definition: Common.h:324
tpOutputFunc outf
Definition: Common.h:335
tpWriteString outs
Definition: Common.h:336
#define TELNET_PORT
Definition: net.h:524
telnet_return_type* Telnet_server ( void  )
Returns
  • ( 0) Normal return
  • (>0) After successful up/download or error
See also
tcp_get_state() - netlib
net_send_string() - netlib
telnet_check_message() - this module
554 {
555  /***************************************************************************
556  locals
557  ***************************************************************************/
558  telnet_return_type *telnet_ret = NULL;
559  static timeval OutScanTime;
560 
561  /***************************************************************************
562  check for connection established
563  ***************************************************************************/
565  {
566  /***********************************************************************
567  is it a new connection ?
568  ***********************************************************************/
570  {
571  /*******************************************************************
572  clear the SO_CONNECTION_ESTABLISHED event
573  *******************************************************************/
574  telnet.server-> error_code = SO_NO_ERROR;
575 
576  /*******************************************************************
577  print a hello message
578  *******************************************************************/
579  if (telnet.hello_msg)
580  {
582  TelnetWriteStr ("\r\n");
583  }
584 
586 
587  /*******************************************************************
588  set detailed return parameter
589  *******************************************************************/
590  telnet.telnet_ret.offset = telnet.server-> dest_addr;
592  telnet_ret = &(telnet.telnet_ret);
593  }
594 
595  /***********************************************************************
596  check for new message
597  ***********************************************************************/
599  {
600  /*******************************************************************
601  set detailed return parameter
602  *******************************************************************/
603  telnet_ret = &(telnet.telnet_ret);
604  }
605 
606  /***********************************************************************
607  check for data to send
608  ***********************************************************************/
609  if (telnet.out_wptr)
610  {
611  if ( tv_elapsed (&OutScanTime, UINT32_C(0), TELNET_OUTPUT_SCAN_INTERVAL, TRUE) )
612  {
613  net_send_safe (telnet.server,
616  TCP_NOWAIT);
617  telnet.out_wptr=0;
618  }
619  }
620  }
621  else
622  {
623  /***********************************************************************
624  if server is listening
625  ***********************************************************************/
627  {
628  /*******************************************************************
629  but status connected
630  *******************************************************************/
632  {
633  /***************************************************************
634  clear status
635  ***************************************************************/
637 
638  /***************************************************************
639  set detailed return parameter
640  ***************************************************************/
642  telnet_ret = &(telnet.telnet_ret);
643  }
644  }
645  }
646  return (telnet_ret);
647 }
void TelnetWriteStr(const char *buffer)
append a zero terminated string to telnet write buffer
Definition: telnetio.c:213
int16_t tcp_get_state(SOCKET *so)
Determine tcp state.
#define NULL
Definition: net.h:126
#define UINT32_C(value)
Definition: stdint.h:210
#define TELNET_NEW_CON
Definition: telnetio.h:88
int tv_elapsed(timeval *t, time_t s, time_t u, uint16_t retrigger)
check if given time has elapsed
Definition: timer.c:286
uint32_t reason
Definition: telnetio.h:87
#define TELNET_OUTPUT_SCAN_INTERVAL
Definition: telnetio.h:71
uint32_t offset
Definition: telnetio.h:93
#define TCP_LISTEN
Definition: net.h:690
static telnet_server_type telnet
Definition: telnetio.c:147
TelnetSocketState_t so_state
Definition: telnetio.h:140
char * hello_msg
Definition: telnetio.h:138
Definition: timer.h:75
#define TELNET_CLOSE_CON
Definition: telnetio.h:90
telnet server return structure
Definition: telnetio.h:85
#define TCP_ESTABLISHED
Definition: net.h:693
SOCKET * server
Definition: telnetio.h:134
uint32_t out_wptr
Definition: telnetio.h:137
telnet_return_type telnet_ret
Definition: telnetio.h:143
Definition: telnetio.h:106
Definition: telnetio.h:105
int Telnet_check_message(telnet_server_type *telnet)
Parse user commands.
Definition: telnetio.c:313
char outbuffer[TELNET_OUTPUT_BUFFER_SIZE]
Definition: telnetio.h:136
#define SO_NO_ERROR
Definition: net.h:461
uint32_t telnet_active
Definition: telnetio.c:129