Update copyrights for 2014.
[pub/USBasp.git] / Projects / Webserver / Lib / TELNETServerApp.c
index 2855f8d..a05cd28 100644 (file)
-/*\r
-             LUFA Library\r
-     Copyright (C) Dean Camera, 2010.\r
-              \r
-  dean [at] fourwalledcubicle [dot] com\r
-      www.fourwalledcubicle.com\r
-*/\r
-\r
-/*\r
-  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
-\r
-  Permission to use, copy, modify, distribute, and sell this \r
-  software and its documentation for any purpose is hereby granted\r
-  without fee, provided that the above copyright notice appear in \r
-  all copies and that both that the copyright notice and this\r
-  permission notice and warranty disclaimer appear in supporting \r
-  documentation, and that the name of the author not be used in \r
-  advertising or publicity pertaining to distribution of the \r
-  software without specific, written prior permission.\r
-\r
-  The author disclaim all warranties with regard to this\r
-  software, including all implied warranties of merchantability\r
-  and fitness.  In no event shall the author be liable for any\r
-  special, indirect or consequential damages or any damages\r
-  whatsoever resulting from loss of use, data or profits, whether\r
-  in an action of contract, negligence or other tortious action,\r
-  arising out of or in connection with the use or performance of\r
-  this software.\r
-*/\r
-\r
-#if defined(ENABLE_TELNET_SERVER) || defined(__DOXYGEN__)\r
-\r
-/** \file\r
- *\r
- *  TELNET Webserver Application. When connected to the uIP stack,\r
- *  this will serve out raw TELNET to the client on port 23.\r
- */\r
\r
-#define  INCLUDE_FROM_TELNETSERVERAPP_C\r
-#include "TELNETServerApp.h"\r
-\r
-/** Welcome message to send to a TELNET client when a connection is first made. */\r
-const char PROGMEM WelcomeHeader[] = "********************************************\r\n"\r
-                                     "*       LUFA uIP Webserver (TELNET)        *\r\n"\r
-                                     "********************************************\r\n";\r
-\r
-/** Main TELNET menu, giving the user the list of available commands they may issue */\r
-const char PROGMEM TELNETMenu[] = "\r\n"\r
-                                  "  == Available Commands: ==\r\n"\r
-                                  "     c) List Active TCP Connections\r\n"\r
-                                  "  =========================\r\n"\r
-                                  "\r\n>";\r
-                                                                 \r
-/** Header to print before the current connections are printed to the client */\r
-const char PROGMEM CurrentConnectionsHeader[] = "\r\n* Current TCP Connections: *\r\n";\r
-\r
-/** Initialization function for the simple HTTP webserver. */\r
-void TELNETServerApp_Init(void)\r
-{\r
-       /* Listen on port 23 for TELNET connections from hosts */\r
-       uip_listen(HTONS(TELNET_SERVER_PORT));\r
-}\r
-\r
-/** uIP stack application callback for the TELNET server. This function must be called each time the\r
- *  TCP/IP stack needs a TCP packet to be processed.\r
- */\r
-void TELNETServerApp_Callback(void)\r
-{\r
-       uip_tcp_appstate_t* const AppState   = &uip_conn->appstate;\r
-       char*               const AppData    = (char*)uip_appdata;\r
-\r
-       if (uip_connected())\r
-       {\r
-               /* New connection - initialize connection state values */\r
-               AppState->TELNETServer.CurrentState = TELNET_STATE_SendHeader;\r
-       }\r
-\r
-       if (uip_acked())\r
-       {\r
-               /* Progress to the next state once the current state's data has been ACKed */\r
-               AppState->TELNETServer.CurrentState = AppState->TELNETServer.NextState; \r
-       }\r
-\r
-       if (uip_rexmit() || uip_acked() || uip_newdata() || uip_connected() || uip_poll())\r
-       {\r
-               switch (AppState->TELNETServer.CurrentState)\r
-               {\r
-                       case TELNET_STATE_SendHeader:\r
-                               /* Copy over and send the TELNET welcome message upon first connection */\r
-                               strcpy_P(AppData, WelcomeHeader);\r
-                               uip_send(AppData, strlen(AppData));\r
-                               \r
-                               AppState->TELNETServer.NextState = TELNET_STATE_SendMenu;\r
-                               break;\r
-                       case TELNET_STATE_SendMenu:\r
-                               /* Copy over and send the TELNET menu to the client */\r
-                               strcpy_P(AppData, TELNETMenu);\r
-                               uip_send(AppData, strlen(AppData));\r
-                               \r
-                               AppState->TELNETServer.NextState = TELNET_STATE_GetCommand;\r
-                               break;\r
-                       case TELNET_STATE_GetCommand:\r
-                               if (!(uip_datalen()))\r
-                                 break;\r
-\r
-                               /* Save the issued command for later processing */\r
-                               AppState->TELNETServer.IssuedCommand = AppData[0];\r
\r
-                               AppState->TELNETServer.CurrentState  = TELNET_STATE_SendResponse;\r
-                               break;\r
-                       case TELNET_STATE_SendResponse:\r
-                               /* Determine which command was issued, perform command processing */\r
-                               switch (AppState->TELNETServer.IssuedCommand)\r
-                               {\r
-                                       case 'c':\r
-                                               TELNETServerApp_DisplayTCPConnections();\r
-                                               break;\r
-                                       default:\r
-                                               strcpy_P(AppData, PSTR("Invalid Command.\r\n"));\r
-                                               uip_send(AppData, strlen(AppData));\r
-                                               break;\r
-                               }\r
-\r
-                               AppState->TELNETServer.NextState = TELNET_STATE_SendMenu;\r
-                               break;\r
-               }\r
-       }               \r
-}\r
-\r
-/** Sends a list of active TCP connections to the TELNET client. */\r
-static void TELNETServerApp_DisplayTCPConnections(void)\r
-{\r
-       char* const AppData    = (char*)uip_appdata;\r
-\r
-       strcpy_P(AppData, CurrentConnectionsHeader);\r
-                                                       \r
-       uint16_t ResponseLen     = strlen(AppData);\r
-       uint8_t  ActiveConnCount = 0;\r
-\r
-       /* Loop through the complete uIP TCP connections list, looking for active connections */\r
-       for (uint8_t i = 0; i < UIP_CONNS; i++)\r
-       {\r
-               struct uip_conn* CurrConnection = &uip_conns[i];\r
-               \r
-               /* If the connection is not closed, it is active and must be added to the out buffer */\r
-               if (CurrConnection->tcpstateflags != UIP_CLOSED)\r
-               {\r
-                       /* Add the current connection's details to the out buffer */\r
-                       ResponseLen += sprintf_P(&AppData[ResponseLen], PSTR("%u) %02d.%02d.%02d.%02d (Local %u, Remote %u)\r\n"),\r
-                                                ++ActiveConnCount,\r
-                                                CurrConnection->ripaddr.u8[0],\r
-                                                CurrConnection->ripaddr.u8[1],\r
-                                                CurrConnection->ripaddr.u8[2],\r
-                                                CurrConnection->ripaddr.u8[3],\r
-                                                HTONS(CurrConnection->lport), HTONS(CurrConnection->rport));\r
-               }\r
-       }\r
-\r
-       uip_send(AppData, ResponseLen);\r
-}\r
-\r
-#endif\r
+/*
+             LUFA Library
+     Copyright (C) Dean Camera, 2014.
+
+  dean [at] fourwalledcubicle [dot] com
+           www.lufa-lib.org
+*/
+
+/*
+  Copyright 2014  Dean Camera (dean [at] fourwalledcubicle [dot] com)
+
+  Permission to use, copy, modify, distribute, and sell this
+  software and its documentation for any purpose is hereby granted
+  without fee, provided that the above copyright notice appear in
+  all copies and that both that the copyright notice and this
+  permission notice and warranty disclaimer appear in supporting
+  documentation, and that the name of the author not be used in
+  advertising or publicity pertaining to distribution of the
+  software without specific, written prior permission.
+
+  The author disclaims all warranties with regard to this
+  software, including all implied warranties of merchantability
+  and fitness.  In no event shall the author be liable for any
+  special, indirect or consequential damages or any damages
+  whatsoever resulting from loss of use, data or profits, whether
+  in an action of contract, negligence or other tortious action,
+  arising out of or in connection with the use or performance of
+  this software.
+*/
+
+/** \file
+ *
+ *  TELNET Webserver Application. When connected to the uIP stack,
+ *  this will serve out raw TELNET to the client on port 23.
+ */
+
+#define  INCLUDE_FROM_TELNETSERVERAPP_C
+#include "TELNETServerApp.h"
+
+#if defined(ENABLE_TELNET_SERVER) || defined(__DOXYGEN__)
+
+/** Welcome message to send to a TELNET client when a connection is first made. */
+const char PROGMEM WelcomeHeader[] = "********************************************\r\n"
+                                     "*       LUFA uIP Webserver (TELNET)        *\r\n"
+                                     "********************************************\r\n";
+
+/** Main TELNET menu, giving the user the list of available commands they may issue */
+const char PROGMEM TELNETMenu[] = "\r\n"
+                                  "  == Available Commands: ==\r\n"
+                                  "     c) List Active TCP Connections\r\n"
+                                  "  =========================\r\n"
+                                  "\r\n>";
+
+/** Header to print before the current connections are printed to the client */
+const char PROGMEM CurrentConnectionsHeader[] = "\r\n* Current TCP Connections: *\r\n";
+
+/** Initialization function for the simple TELNET webserver. */
+void TELNETServerApp_Init(void)
+{
+       /* Listen on port 23 for TELNET connections from hosts */
+       uip_listen(HTONS(TELNET_SERVER_PORT));
+}
+
+/** uIP stack application callback for the TELNET server. This function must be called each time the
+ *  TCP/IP stack needs a TCP packet to be processed.
+ */
+void TELNETServerApp_Callback(void)
+{
+       uip_tcp_appstate_t* const AppState   = &uip_conn->appstate;
+       char*               const AppData    = (char*)uip_appdata;
+
+       if (uip_connected())
+       {
+               /* New connection - initialize connection state values */
+               AppState->TELNETServer.CurrentState = TELNET_STATE_SendHeader;
+       }
+
+       if (uip_acked())
+       {
+               /* Progress to the next state once the current state's data has been ACKed */
+               AppState->TELNETServer.CurrentState = AppState->TELNETServer.NextState;
+       }
+
+       if (uip_rexmit() || uip_acked() || uip_newdata() || uip_connected() || uip_poll())
+       {
+               switch (AppState->TELNETServer.CurrentState)
+               {
+                       case TELNET_STATE_SendHeader:
+                               /* Copy over and send the TELNET welcome message upon first connection */
+                               strcpy_P(AppData, WelcomeHeader);
+                               uip_send(AppData, strlen(AppData));
+
+                               AppState->TELNETServer.NextState = TELNET_STATE_SendMenu;
+                               break;
+                       case TELNET_STATE_SendMenu:
+                               /* Copy over and send the TELNET menu to the client */
+                               strcpy_P(AppData, TELNETMenu);
+                               uip_send(AppData, strlen(AppData));
+
+                               AppState->TELNETServer.NextState = TELNET_STATE_GetCommand;
+                               break;
+                       case TELNET_STATE_GetCommand:
+                               if (!(uip_datalen()))
+                                 break;
+
+                               /* Save the issued command for later processing */
+                               AppState->TELNETServer.IssuedCommand = AppData[0];
+
+                               AppState->TELNETServer.CurrentState  = TELNET_STATE_SendResponse;
+                               break;
+                       case TELNET_STATE_SendResponse:
+                               /* Determine which command was issued, perform command processing */
+                               switch (AppState->TELNETServer.IssuedCommand)
+                               {
+                                       case 'c':
+                                               TELNETServerApp_DisplayTCPConnections();
+                                               break;
+                                       default:
+                                               strcpy_P(AppData, PSTR("Invalid Command.\r\n"));
+                                               uip_send(AppData, strlen(AppData));
+                                               break;
+                               }
+
+                               AppState->TELNETServer.NextState = TELNET_STATE_SendMenu;
+                               break;
+               }
+       }
+}
+
+/** Sends a list of active TCP connections to the TELNET client. */
+static void TELNETServerApp_DisplayTCPConnections(void)
+{
+       char* const AppData    = (char*)uip_appdata;
+
+       strcpy_P(AppData, CurrentConnectionsHeader);
+
+       uint16_t ResponseLen     = strlen(AppData);
+       uint8_t  ActiveConnCount = 0;
+
+       /* Loop through the complete uIP TCP connections list, looking for active connections */
+       for (uint8_t i = 0; i < UIP_CONNS; i++)
+       {
+               struct uip_conn* CurrConnection = &uip_conns[i];
+
+               /* If the connection is not closed, it is active and must be added to the out buffer */
+               if (CurrConnection->tcpstateflags != UIP_CLOSED)
+               {
+                       /* Add the current connection's details to the out buffer */
+                       ResponseLen += sprintf_P(&AppData[ResponseLen], PSTR("%u) %d.%d.%d.%d (Local Port %u <=> Remote Port %u)\r\n"),
+                                                ++ActiveConnCount,
+                                                CurrConnection->ripaddr.u8[0],
+                                                CurrConnection->ripaddr.u8[1],
+                                                CurrConnection->ripaddr.u8[2],
+                                                CurrConnection->ripaddr.u8[3],
+                                                HTONS(CurrConnection->lport), HTONS(CurrConnection->rport));
+               }
+       }
+
+       uip_send(AppData, ResponseLen);
+}
+
+#endif
+