59045c06a3323f15a16af83b7dfa2ffb040a902b
[pub/lufa.git] / Projects / Webserver / Lib / uIPManagement.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 *
33 * uIP Management functions. This file contains the functions and globals needed to maintain the uIP
34 * stack once an RNDIS device has been attached to the system.
35 */
36
37 #define INCLUDE_FROM_UIPMANAGEMENT_C
38 #include "uIPManagement.h"
39
40 /** Connection timer, to retain the time elapsed since the last time the uIP connections were managed. */
41 static struct timer ConnectionTimer;
42
43 /** ARP timer, to retain the time elapsed since the ARP cache was last updated. */
44 static struct timer ARPTimer;
45
46 /** MAC address of the RNDIS device, when enumerated. */
47 struct uip_eth_addr MACAddress;
48
49 /** Indicates if an IP configuration has been set in the device. */
50 bool HaveIPConfiguration;
51
52
53 /** Configures the uIP stack ready for network traffic processing. */
54 void uIPManagement_Init(void)
55 {
56 /* uIP Timing Initialization */
57 clock_init();
58 timer_set(&ConnectionTimer, CLOCK_SECOND / 2);
59 timer_set(&ARPTimer, CLOCK_SECOND * 10);
60
61 /* uIP Stack Initialization */
62 uip_init();
63 uip_arp_init();
64
65 /* DHCP/Server IP Settings Initialization */
66 if (USB_CurrentMode == USB_MODE_Device)
67 {
68 MACAddress.addr[0] = SERVER_MAC_ADDRESS[0];
69 MACAddress.addr[1] = SERVER_MAC_ADDRESS[1];
70 MACAddress.addr[2] = SERVER_MAC_ADDRESS[2];
71 MACAddress.addr[3] = SERVER_MAC_ADDRESS[3];
72 MACAddress.addr[4] = SERVER_MAC_ADDRESS[4];
73 MACAddress.addr[5] = SERVER_MAC_ADDRESS[5];
74
75 #if defined(ENABLE_DHCP_SERVER)
76 DHCPServerApp_Init();
77 #endif
78
79 HaveIPConfiguration = true;
80 uip_ipaddr_t IPAddress, Netmask, GatewayIPAddress;
81 uip_ipaddr(&IPAddress, DEVICE_IP_ADDRESS[0], DEVICE_IP_ADDRESS[1], DEVICE_IP_ADDRESS[2], DEVICE_IP_ADDRESS[3]);
82 uip_ipaddr(&Netmask, DEVICE_NETMASK[0], DEVICE_NETMASK[1], DEVICE_NETMASK[2], DEVICE_NETMASK[3]);
83 uip_ipaddr(&GatewayIPAddress, DEVICE_GATEWAY[0], DEVICE_GATEWAY[1], DEVICE_GATEWAY[2], DEVICE_GATEWAY[3]);
84 uip_sethostaddr(&IPAddress);
85 uip_setnetmask(&Netmask);
86 uip_setdraddr(&GatewayIPAddress);
87 }
88 else
89 {
90 #if defined(ENABLE_DHCP_CLIENT)
91 HaveIPConfiguration = false;
92 DHCPClientApp_Init();
93 #else
94 HaveIPConfiguration = true;
95 uip_ipaddr_t IPAddress, Netmask, GatewayIPAddress;
96 uip_ipaddr(&IPAddress, DEVICE_IP_ADDRESS[0], DEVICE_IP_ADDRESS[1], DEVICE_IP_ADDRESS[2], DEVICE_IP_ADDRESS[3]);
97 uip_ipaddr(&Netmask, DEVICE_NETMASK[0], DEVICE_NETMASK[1], DEVICE_NETMASK[2], DEVICE_NETMASK[3]);
98 uip_ipaddr(&GatewayIPAddress, DEVICE_GATEWAY[0], DEVICE_GATEWAY[1], DEVICE_GATEWAY[2], DEVICE_GATEWAY[3]);
99 uip_sethostaddr(&IPAddress);
100 uip_setnetmask(&Netmask);
101 uip_setdraddr(&GatewayIPAddress);
102 #endif
103 }
104
105 /* Virtual Webserver Ethernet Address Configuration */
106 uip_setethaddr(MACAddress);
107
108 /* HTTP Webserver Initialization */
109 HTTPServerApp_Init();
110
111 /* TELNET Server Initialization */
112 #if defined(ENABLE_TELNET_SERVER)
113 TELNETServerApp_Init();
114 #endif
115 }
116
117 /** uIP Management function. This function manages the uIP stack when called while an RNDIS device has been
118 * attached to the system.
119 */
120 void uIPManagement_ManageNetwork(void)
121 {
122 if (((USB_CurrentMode == USB_MODE_Host) && (USB_HostState == HOST_STATE_Configured)) ||
123 ((USB_CurrentMode == USB_MODE_Device) && (USB_DeviceState == DEVICE_STATE_Configured)))
124 {
125 uIPManagement_ProcessIncomingPacket();
126 uIPManagement_ManageConnections();
127 }
128 }
129
130 /** uIP TCP/IP network stack callback function for the processing of a given TCP connection. This routine dispatches
131 * to the appropriate TCP protocol application based on the connection's listen port number.
132 */
133 void uIPManagement_TCPCallback(void)
134 {
135 /* Call the correct TCP application based on the port number the connection is listening on */
136 switch (uip_conn->lport)
137 {
138 case HTONS(HTTP_SERVER_PORT):
139 HTTPServerApp_Callback();
140 break;
141 #if defined(ENABLE_TELNET_SERVER)
142 case HTONS(TELNET_SERVER_PORT):
143 TELNETServerApp_Callback();
144 break;
145 #endif
146 }
147 }
148
149 /** uIP TCP/IP network stack callback function for the processing of a given UDP connection. This routine dispatches
150 * to the appropriate UDP protocol application based on the connection's listen port number.
151 */
152 void uIPManagement_UDPCallback(void)
153 {
154 /* Call the correct UDP application based on the port number the connection is listening on */
155 switch (uip_udp_conn->lport)
156 {
157 #if defined(ENABLE_DHCP_CLIENT)
158 case HTONS(DHCP_CLIENT_PORT):
159 DHCPClientApp_Callback();
160 break;
161 #endif
162 #if defined(ENABLE_DHCP_SERVER)
163 case HTONS(DHCP_SERVER_PORT):
164 DHCPServerApp_Callback();
165 break;
166 #endif
167 }
168 }
169
170 /** Processes Incoming packets to the server from the connected RNDIS device, creating responses as needed. */
171 static void uIPManagement_ProcessIncomingPacket(void)
172 {
173 /* Determine which USB mode the system is currently initialised in */
174 if (USB_CurrentMode == USB_MODE_Device)
175 {
176 /* If no packet received, exit processing routine */
177 if (!(RNDIS_Device_IsPacketReceived(&Ethernet_RNDIS_Interface_Device)))
178 return;
179
180 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
181
182 /* Read the Incoming packet straight into the UIP packet buffer */
183 RNDIS_Device_ReadPacket(&Ethernet_RNDIS_Interface_Device, uip_buf, &uip_len);
184 }
185 else
186 {
187 /* If no packet received, exit processing routine */
188 if (!(RNDIS_Host_IsPacketReceived(&Ethernet_RNDIS_Interface_Host)))
189 return;
190
191 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
192
193 /* Read the Incoming packet straight into the UIP packet buffer */
194 RNDIS_Host_ReadPacket(&Ethernet_RNDIS_Interface_Host, uip_buf, &uip_len);
195 }
196
197 /* If the packet contains an Ethernet frame, process it */
198 if (uip_len > 0)
199 {
200 switch (((struct uip_eth_hdr*)uip_buf)->type)
201 {
202 case HTONS(UIP_ETHTYPE_IP):
203 /* Filter packet by MAC destination */
204 uip_arp_ipin();
205
206 /* Process Incoming packet */
207 uip_input();
208
209 /* If a response was generated, send it */
210 if (uip_len > 0)
211 {
212 /* Add destination MAC to outgoing packet */
213 uip_arp_out();
214
215 uip_split_output();
216 }
217
218 break;
219 case HTONS(UIP_ETHTYPE_ARP):
220 /* Process ARP packet */
221 uip_arp_arpin();
222
223 /* If a response was generated, send it */
224 if (uip_len > 0)
225 uip_split_output();
226
227 break;
228 }
229 }
230
231 LEDs_SetAllLEDs(LEDMASK_USB_READY | ((HaveIPConfiguration) ? LEDMASK_UIP_READY_CONFIG : LEDMASK_UIP_READY_NOCONFIG));
232 }
233
234 /** Manages the currently open network connections, including TCP and (if enabled) UDP. */
235 static void uIPManagement_ManageConnections(void)
236 {
237 /* Poll TCP connections for more data to send back to the host */
238 for (uint8_t i = 0; i < UIP_CONNS; i++)
239 {
240 uip_poll_conn(&uip_conns[i]);
241
242 /* If a response was generated, send it */
243 if (uip_len > 0)
244 {
245 /* Add destination MAC to outgoing packet */
246 uip_arp_out();
247
248 /* Split and send the outgoing packet */
249 uip_split_output();
250 }
251 }
252
253 /* Manage open connections for timeouts */
254 if (timer_expired(&ConnectionTimer))
255 {
256 timer_reset(&ConnectionTimer);
257
258 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
259
260 for (uint8_t i = 0; i < UIP_CONNS; i++)
261 {
262 /* Run periodic connection management for each TCP connection */
263 uip_periodic(i);
264
265 /* If a response was generated, send it */
266 if (uip_len > 0)
267 {
268 /* Add destination MAC to outgoing packet */
269 uip_arp_out();
270
271 /* Split and send the outgoing packet */
272 uip_split_output();
273 }
274 }
275
276 #if defined(ENABLE_DHCP_CLIENT)
277 for (uint8_t i = 0; i < UIP_UDP_CONNS; i++)
278 {
279 /* Run periodic connection management for each UDP connection */
280 uip_udp_periodic(i);
281
282 /* If a response was generated, send it */
283 if (uip_len > 0)
284 {
285 /* Add destination MAC to outgoing packet */
286 uip_arp_out();
287
288 /* Split and send the outgoing packet */
289 uip_split_output();
290 }
291 }
292 #endif
293
294 LEDs_SetAllLEDs(LEDMASK_USB_READY);
295 }
296
297 /* Manage ARP cache refreshing */
298 if (timer_expired(&ARPTimer))
299 {
300 timer_reset(&ARPTimer);
301 uip_arp_timer();
302 }
303 }
304