3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
33 * TELNET Webserver Application. When connected to the uIP stack,
34 * this will serve out raw TELNET to the client on port 23.
37 #define INCLUDE_FROM_TELNETSERVERAPP_C
38 #include "TELNETServerApp.h"
40 /** Welcome message to send to a TELNET client when a connection is first made. */
41 const char PROGMEM WelcomeHeader
[] = "********************************************\r\n"
42 "* LUFA uIP Webserver (TELNET) *\r\n"
43 "********************************************\r\n";
45 /** Main TELNET menu, giving the user the list of available commands they may issue */
46 const char PROGMEM TELNETMenu
[] = "\r\n"
47 " == Available Commands: ==\r\n"
48 " c) List Active TCP Connections\r\n"
49 " =========================\r\n"
52 /** Header to print before the current connections are printed to the client */
53 const char PROGMEM CurrentConnectionsHeader
= "\r\n* Current TCP Connections: *\r\n";
55 /** Initialization function for the simple HTTP webserver. */
56 void TELNETServerApp_Init(void)
58 /* Listen on port 23 for TELNET connections from hosts */
59 uip_listen(HTONS(TELNET_SERVER_PORT
));
62 /** uIP stack application callback for the TELNET server. This function must be called each time the
63 * TCP/IP stack needs a TCP packet to be processed.
65 void TELNETServerApp_Callback(void)
67 uip_tcp_appstate_t
* const AppState
= &uip_conn
->appstate
;
68 char* const AppData
= (char*)uip_appdata
;
72 /* New connection - initialize connection state values */
73 AppState
->TELNETServer
.CurrentState
= TELNET_STATE_SendHeader
;
78 /* Progress to the next state once the current state's data has been ACKed */
79 AppState
->TELNETServer
.CurrentState
= AppState
->TELNETServer
.NextState
;
82 if (uip_rexmit() || uip_acked() || uip_newdata() || uip_connected() || uip_poll())
84 switch (AppState
->TELNETServer
.CurrentState
)
86 case TELNET_STATE_SendHeader
:
87 /* Copy over and send the TELNET welcome message upon first connection */
88 strcpy_P(AppData
, WelcomeHeader
);
89 uip_send(AppData
, strlen(AppData
));
91 AppState
->TELNETServer
.NextState
= TELNET_STATE_SendMenu
;
93 case TELNET_STATE_SendMenu
:
94 /* Copy over and send the TELNET menu to the client */
95 strcpy_P(AppData
, TELNETMenu
);
96 uip_send(AppData
, strlen(AppData
));
98 AppState
->TELNETServer
.NextState
= TELNET_STATE_GetCommand
;
100 case TELNET_STATE_GetCommand
:
101 if (!(uip_datalen()))
104 /* Save the issued command for later processing */
105 AppState
->TELNETServer
.IssuedCommand
= AppData
[0];
107 AppState
->TELNETServer
.CurrentState
= TELNET_STATE_SendResponse
;
109 case TELNET_STATE_SendResponse
:
110 /* Determine which command was issued, perform command processing */
111 switch (AppState
->TELNETServer
.IssuedCommand
)
114 TELNETServerApp_DisplayTCPConnections();
117 strcpy(AppData
, "Invalid Command.\r\n");
118 uip_send(AppData
, strlen(AppData
));
122 AppState
->TELNETServer
.NextState
= TELNET_STATE_SendMenu
;
128 /** Sends a list of active TCP connections to the TELNET client. */
129 static void TELNETServerApp_DisplayTCPConnections(void)
131 char* const AppData
= (char*)uip_appdata
;
133 strcpy_P(AppData
, CurrentConnectionsHeader
);
135 uint16_t ResponseLen
= strlen(AppData
);
136 uint8_t ActiveConnCount
= 0;
138 /* Loop through the complete uIP TCP connections list, looking for active connections */
139 for (uint8_t i
= 0; i
< UIP_CONNS
; i
++)
141 struct uip_conn
* CurrConnection
= &uip_conns
[i
];
143 /* If the connection is not closed, it is active and must be added to the out buffer */
144 if (CurrConnection
->tcpstateflags
!= UIP_CLOSED
)
146 /* Add the current connection's details to the out buffer */
147 ResponseLen
+= sprintf(&AppData
[ResponseLen
], "%u) %02d.%02d.%02d.%02d (Local %u, Remote %u)\r\n",
148 ++ActiveConnCount
, CurrConnection
->ripaddr
.u8
[0],
149 CurrConnection
->ripaddr
.u8
[1],
150 CurrConnection
->ripaddr
.u8
[2],
151 CurrConnection
->ripaddr
.u8
[3],
152 HTONS(CurrConnection
->lport
), HTONS(CurrConnection
->rport
));
156 uip_send(AppData
, ResponseLen
);