123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- #define INCLUDE_FROM_TELNETSERVERAPP_C
- #include "TELNETServerApp.h"
- #if defined(ENABLE_TELNET_SERVER) || defined(__DOXYGEN__)
- const char PROGMEM WelcomeHeader[] = "********************************************\r\n"
- "* LUFA uIP Webserver (TELNET) *\r\n"
- "********************************************\r\n";
- const char PROGMEM TELNETMenu[] = "\r\n"
- " == Available Commands: ==\r\n"
- " c) List Active TCP Connections\r\n"
- " =========================\r\n"
- "\r\n>";
- const char PROGMEM CurrentConnectionsHeader[] = "\r\n* Current TCP Connections: *\r\n";
- void TELNETServerApp_Init(void)
- {
-
- uip_listen(HTONS(TELNET_SERVER_PORT));
- }
- void TELNETServerApp_Callback(void)
- {
- uip_tcp_appstate_t* const AppState = &uip_conn->appstate;
- char* const AppData = (char*)uip_appdata;
- if (uip_connected())
- {
-
- AppState->TELNETServer.CurrentState = TELNET_STATE_SendHeader;
- }
- if (uip_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:
-
- strcpy_P(AppData, WelcomeHeader);
- uip_send(AppData, strlen(AppData));
- AppState->TELNETServer.NextState = TELNET_STATE_SendMenu;
- break;
- case TELNET_STATE_SendMenu:
-
- strcpy_P(AppData, TELNETMenu);
- uip_send(AppData, strlen(AppData));
- AppState->TELNETServer.NextState = TELNET_STATE_GetCommand;
- break;
- case TELNET_STATE_GetCommand:
- if (!(uip_datalen()))
- break;
-
- AppState->TELNETServer.IssuedCommand = AppData[0];
- AppState->TELNETServer.CurrentState = TELNET_STATE_SendResponse;
- break;
- case TELNET_STATE_SendResponse:
-
- 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;
- }
- }
- }
- static void TELNETServerApp_DisplayTCPConnections(void)
- {
- char* const AppData = (char*)uip_appdata;
- strcpy_P(AppData, CurrentConnectionsHeader);
- uint16_t ResponseLen = strlen(AppData);
- uint8_t ActiveConnCount = 0;
-
- for (uint8_t i = 0; i < UIP_CONNS; i++)
- {
- struct uip_conn* CurrConnection = &uip_conns[i];
-
- if (CurrConnection->tcpstateflags != UIP_CLOSED)
- {
-
- 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
|