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 connection information to the client.
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 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 char PROGMEM TELNETMenu
[] = "\r\n"
47 " Available Commands:\r\n"
48 " c) List Active TCP Connections\r\n"
51 /** Initialization function for the simple HTTP webserver. */
52 void TELNETServerApp_Init(void)
54 /* Listen on port 23 for TELNET connections from hosts */
55 uip_listen(HTONS(TELNET_SERVER_PORT
));
58 /** uIP stack application callback for the TELNET server. This function must be called each time the
59 * TCP/IP stack needs a TCP packet to be processed.
61 void TELNETServerApp_Callback(void)
63 uip_tcp_appstate_t
* const AppState
= &uip_conn
->appstate
;
64 char* const AppData
= (char*)uip_appdata
;
68 AppState
->TELNETServer
.CurrentState
= TELNET_STATE_SendHeader
;
73 AppState
->TELNETServer
.CurrentState
= AppState
->TELNETServer
.NextState
;
76 if (uip_rexmit() || uip_acked() || uip_newdata() || uip_connected() || uip_poll())
78 switch (AppState
->TELNETServer
.CurrentState
)
80 case TELNET_STATE_SendHeader
:
81 /* Copy over and send the TELNET welcome message upon first connection */
82 strncpy_P(AppData
, WelcomeHeader
, strlen_P(WelcomeHeader
));
83 uip_send(AppData
, strlen_P(WelcomeHeader
));
85 AppState
->TELNETServer
.NextState
= TELNET_STATE_SendMenu
;
87 case TELNET_STATE_SendMenu
:
88 /* Copy over and send the TELNET menu to the client */
89 strncpy_P(AppData
, TELNETMenu
, strlen_P(TELNETMenu
));
90 uip_send(AppData
, strlen_P(TELNETMenu
));
92 AppState
->TELNETServer
.NextState
= TELNET_STATE_GetCommand
;
94 case TELNET_STATE_GetCommand
:
98 /* Save the issued command for later processing */
99 AppState
->TELNETServer
.IssuedCommand
= AppData
[0];
101 AppState
->TELNETServer
.CurrentState
= TELNET_STATE_SendResponse
;
103 case TELNET_STATE_SendResponse
:
104 /* Determine which command was issued, perform command processing */
105 switch (AppState
->TELNETServer
.IssuedCommand
)
108 TELNETServerApp_DisplayTCPConnections();
112 AppState
->TELNETServer
.NextState
= TELNET_STATE_SendMenu
;
118 /** Sends a list of active TCP connections to the TELNET client. */
119 static void TELNETServerApp_DisplayTCPConnections(void)
121 char* const AppData
= (char*)uip_appdata
;
123 strcpy(AppData
, "\r\n* Current TCP Connections: *\r\n");
125 uint16_t ResponseLen
= strlen(AppData
);
126 uint8_t ActiveConnCount
= 0;
128 /* Loop through the complete uIP TCP connections list, looking for active connections */
129 for (uint8_t i
= 0; i
< UIP_CONNS
; i
++)
131 struct uip_conn
* CurrConnection
= &uip_conns
[i
];
133 /* If the connection is not closed, it is active and must be added to the out buffer */
134 if (CurrConnection
->tcpstateflags
!= UIP_CLOSED
)
136 /* Add the current connection's details to the out buffer */
137 ResponseLen
+= sprintf(&AppData
[ResponseLen
], "%u) %02d.%02d.%02d.%02d (Local %u, Remote %u)\r\n",
138 ++ActiveConnCount
, CurrConnection
->ripaddr
.u8
[0],
139 CurrConnection
->ripaddr
.u8
[1],
140 CurrConnection
->ripaddr
.u8
[2],
141 CurrConnection
->ripaddr
.u8
[3],
142 HTONS(CurrConnection
->lport
), HTONS(CurrConnection
->rport
));
146 uip_send(AppData
, ResponseLen
);