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 strcpy_P(AppData
, WelcomeHeader
);
83 uip_send(AppData
, strlen(AppData
));
85 AppState
->TELNETServer
.NextState
= TELNET_STATE_SendMenu
;
87 case TELNET_STATE_SendMenu
:
88 /* Copy over and send the TELNET menu to the client */
89 strcpy_P(AppData
, TELNETMenu
);
90 uip_send(AppData
, strlen(AppData
));
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();
111 strcpy(AppData
, "Invalid Command.\r\n");
112 uip_send(AppData
, strlen(AppData
));
116 AppState
->TELNETServer
.NextState
= TELNET_STATE_SendMenu
;
122 /** Sends a list of active TCP connections to the TELNET client. */
123 static void TELNETServerApp_DisplayTCPConnections(void)
125 char* const AppData
= (char*)uip_appdata
;
127 strcpy(AppData
, "\r\n* Current TCP Connections: *\r\n");
129 uint16_t ResponseLen
= strlen(AppData
);
130 uint8_t ActiveConnCount
= 0;
132 /* Loop through the complete uIP TCP connections list, looking for active connections */
133 for (uint8_t i
= 0; i
< UIP_CONNS
; i
++)
135 struct uip_conn
* CurrConnection
= &uip_conns
[i
];
137 /* If the connection is not closed, it is active and must be added to the out buffer */
138 if (CurrConnection
->tcpstateflags
!= UIP_CLOSED
)
140 /* Add the current connection's details to the out buffer */
141 ResponseLen
+= sprintf(&AppData
[ResponseLen
], "%u) %02d.%02d.%02d.%02d (Local %u, Remote %u)\r\n",
142 ++ActiveConnCount
, CurrConnection
->ripaddr
.u8
[0],
143 CurrConnection
->ripaddr
.u8
[1],
144 CurrConnection
->ripaddr
.u8
[2],
145 CurrConnection
->ripaddr
.u8
[3],
146 HTONS(CurrConnection
->lport
), HTONS(CurrConnection
->rport
));
150 uip_send(AppData
, ResponseLen
);