1 | /** @file
|
---|
2 | *
|
---|
3 | * chargen server for lwip
|
---|
4 | */
|
---|
5 | /*
|
---|
6 | * Copyright (c) 2003 NBS Card Technology, Paramus, NJ.
|
---|
7 | * All rights reserved.
|
---|
8 | *
|
---|
9 | * Redistribution and use in source and binary forms, with or without modification,
|
---|
10 | * are permitted provided that the following conditions are met:
|
---|
11 | *
|
---|
12 | * 1. Redistributions of source code must retain the above copyright notice,
|
---|
13 | * this list of conditions and the following disclaimer.
|
---|
14 | * 2. Redistributions in binary form must reproduce the above copyright notice,
|
---|
15 | * this list of conditions and the following disclaimer in the documentation
|
---|
16 | * and/or other materials provided with the distribution.
|
---|
17 | * 3. The name of the author may not be used to endorse or promote products
|
---|
18 | * derived from this software without specific prior written permission.
|
---|
19 | *
|
---|
20 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
---|
21 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
---|
22 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
---|
23 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
---|
25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
---|
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
---|
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
---|
28 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
---|
29 | * OF SUCH DAMAGE.
|
---|
30 | *
|
---|
31 | * This file is part of the lwIP TCP/IP stack.
|
---|
32 | *
|
---|
33 | * Author: David Haas <dhaas@alum.rpi.edu>
|
---|
34 | *
|
---|
35 | * Purpose: chargen server for testing and demonstration purposes
|
---|
36 | *
|
---|
37 | * This file implements a nice example of handling multiple tcp sockets in a
|
---|
38 | * server environment. Just call chargen_init() from your application after
|
---|
39 | * you have initialized lwip and added your network interfaces. Change the
|
---|
40 | * MAX_SERV option to increase or decrease the number of sessions supported.
|
---|
41 | *
|
---|
42 | * chargen will jam as much data as possible into the output socket, so it
|
---|
43 | * will take up a lot of CPU time. Therefore it will be a good idea to run it
|
---|
44 | * as the lowest possible priority (just ahead of any idle task).
|
---|
45 | *
|
---|
46 | * This is also a good example of how to support multiple sessions in an
|
---|
47 | * embedded system where you might not have fork().
|
---|
48 | */
|
---|
49 |
|
---|
50 | #include "lwip/opt.h"
|
---|
51 | #include "lwip/sys.h"
|
---|
52 | #include "lwip/sockets.h"
|
---|
53 | #include "lwip/mem.h"
|
---|
54 |
|
---|
55 | #include <string.h>
|
---|
56 |
|
---|
57 | #include "chargen.h"
|
---|
58 |
|
---|
59 | #if LWIP_SOCKET && LWIP_SOCKET_SELECT
|
---|
60 |
|
---|
61 | #define MAX_SERV 5 /* Maximum number of chargen services. Don't need too many */
|
---|
62 | #define CHARGEN_THREAD_NAME "chargen"
|
---|
63 | #define CHARGEN_PRIORITY 254 /* Really low priority */
|
---|
64 | #define CHARGEN_THREAD_STACKSIZE 0
|
---|
65 | #define SEND_SIZE TCP_SNDLOWAT /* If we only send this much, then when select
|
---|
66 | says we can send, we know we won't block */
|
---|
67 |
|
---|
68 | struct charcb {
|
---|
69 | struct charcb *next;
|
---|
70 | int socket;
|
---|
71 | struct sockaddr_storage cliaddr;
|
---|
72 | socklen_t clilen;
|
---|
73 | char nextchar;
|
---|
74 | };
|
---|
75 |
|
---|
76 | static struct charcb *charcb_list = 0;
|
---|
77 |
|
---|
78 | /**************************************************************
|
---|
79 | * void close_chargen(struct charcb *p_charcb)
|
---|
80 | *
|
---|
81 | * Close the socket and remove this charcb from the list.
|
---|
82 | **************************************************************/
|
---|
83 | static void
|
---|
84 | close_chargen(struct charcb *p_charcb)
|
---|
85 | {
|
---|
86 | struct charcb *p_search_charcb;
|
---|
87 |
|
---|
88 | /* Either an error or tcp connection closed on other
|
---|
89 | * end. Close here */
|
---|
90 | lwip_close(p_charcb->socket);
|
---|
91 | /* Free charcb */
|
---|
92 | if (charcb_list == p_charcb) {
|
---|
93 | charcb_list = p_charcb->next;
|
---|
94 | } else {
|
---|
95 | for (p_search_charcb = charcb_list; p_search_charcb; p_search_charcb = p_search_charcb->next) {
|
---|
96 | if (p_search_charcb->next == p_charcb) {
|
---|
97 | p_search_charcb->next = p_charcb->next;
|
---|
98 | break;
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|
102 | mem_free(p_charcb);
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**************************************************************
|
---|
106 | * void do_read(struct charcb *p_charcb)
|
---|
107 | *
|
---|
108 | * Socket definitely is ready for reading. Read a buffer from the socket and
|
---|
109 | * discard the data. If no data is read, then the socket is closed and the
|
---|
110 | * charcb is removed from the list and freed.
|
---|
111 | **************************************************************/
|
---|
112 | static int
|
---|
113 | do_read(struct charcb *p_charcb)
|
---|
114 | {
|
---|
115 | char buffer[80];
|
---|
116 | int readcount;
|
---|
117 |
|
---|
118 | /* Read some data */
|
---|
119 | readcount = lwip_read(p_charcb->socket, &buffer, 80);
|
---|
120 | if (readcount <= 0) {
|
---|
121 | close_chargen(p_charcb);
|
---|
122 | return -1;
|
---|
123 | }
|
---|
124 | return 0;
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**************************************************************
|
---|
128 | * void chargen_thread(void *arg)
|
---|
129 | *
|
---|
130 | * chargen task. This server will wait for connections on well
|
---|
131 | * known TCP port number: 19. For every connection, the server will
|
---|
132 | * write as much data as possible to the tcp port.
|
---|
133 | **************************************************************/
|
---|
134 | static void
|
---|
135 | chargen_thread(void *arg)
|
---|
136 | {
|
---|
137 | int listenfd;
|
---|
138 | #if LWIP_IPV6
|
---|
139 | struct sockaddr_in6 chargen_saddr;
|
---|
140 | #else /* LWIP_IPV6 */
|
---|
141 | struct sockaddr_in chargen_saddr;
|
---|
142 | #endif /* LWIP_IPV6 */
|
---|
143 | fd_set readset;
|
---|
144 | fd_set writeset;
|
---|
145 | int i, maxfdp1;
|
---|
146 | struct charcb *p_charcb;
|
---|
147 | LWIP_UNUSED_ARG(arg);
|
---|
148 |
|
---|
149 | memset(&chargen_saddr, 0, sizeof (chargen_saddr));
|
---|
150 | #if LWIP_IPV6
|
---|
151 | /* First acquire our socket for listening for connections */
|
---|
152 | listenfd = lwip_socket(AF_INET6, SOCK_STREAM, 0);
|
---|
153 | chargen_saddr.sin6_family = AF_INET6;
|
---|
154 | chargen_saddr.sin6_addr = in6addr_any;
|
---|
155 | chargen_saddr.sin6_port = lwip_htons(19); /* Chargen server port */
|
---|
156 | #else /* LWIP_IPV6 */
|
---|
157 | /* First acquire our socket for listening for connections */
|
---|
158 | listenfd = lwip_socket(AF_INET, SOCK_STREAM, 0);
|
---|
159 | chargen_saddr.sin_family = AF_INET;
|
---|
160 | chargen_saddr.sin_addr.s_addr = PP_HTONL(INADDR_ANY);
|
---|
161 | chargen_saddr.sin_port = lwip_htons(19); /* Chargen server port */
|
---|
162 | #endif /* LWIP_IPV6 */
|
---|
163 |
|
---|
164 | LWIP_ASSERT("chargen_thread(): Socket create failed.", listenfd >= 0);
|
---|
165 |
|
---|
166 | if (lwip_bind(listenfd, (struct sockaddr *) &chargen_saddr, sizeof (chargen_saddr)) == -1) {
|
---|
167 | LWIP_ASSERT("chargen_thread(): Socket bind failed.", 0);
|
---|
168 | }
|
---|
169 |
|
---|
170 | /* Put socket into listening mode */
|
---|
171 | if (lwip_listen(listenfd, MAX_SERV) == -1) {
|
---|
172 | LWIP_ASSERT("chargen_thread(): Listen failed.", 0);
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | /* Wait forever for network input: This could be connections or data */
|
---|
177 | for (;;) {
|
---|
178 | maxfdp1 = listenfd + 1;
|
---|
179 |
|
---|
180 | /* Determine what sockets need to be in readset */
|
---|
181 | FD_ZERO(&readset);
|
---|
182 | FD_ZERO(&writeset);
|
---|
183 | FD_SET(listenfd, &readset);
|
---|
184 | for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next) {
|
---|
185 | if (maxfdp1 < p_charcb->socket + 1) {
|
---|
186 | maxfdp1 = p_charcb->socket + 1;
|
---|
187 | }
|
---|
188 | FD_SET(p_charcb->socket, &readset);
|
---|
189 | FD_SET(p_charcb->socket, &writeset);
|
---|
190 | }
|
---|
191 |
|
---|
192 | /* Wait for data or a new connection */
|
---|
193 | i = lwip_select(maxfdp1, &readset, &writeset, 0, 0);
|
---|
194 |
|
---|
195 | if (i == 0) {
|
---|
196 | continue;
|
---|
197 | }
|
---|
198 | /* At least one descriptor is ready */
|
---|
199 | if (FD_ISSET(listenfd, &readset)) {
|
---|
200 | /* We have a new connection request!!! */
|
---|
201 | /* Lets create a new control block */
|
---|
202 | p_charcb = (struct charcb *) mem_malloc(sizeof (struct charcb));
|
---|
203 | if (p_charcb) {
|
---|
204 | p_charcb->socket = lwip_accept(listenfd,
|
---|
205 | (struct sockaddr *) &p_charcb->cliaddr,
|
---|
206 | &p_charcb->clilen);
|
---|
207 | if (p_charcb->socket < 0) {
|
---|
208 | mem_free(p_charcb);
|
---|
209 | } else {
|
---|
210 | /* Keep this tecb in our list */
|
---|
211 | p_charcb->next = charcb_list;
|
---|
212 | charcb_list = p_charcb;
|
---|
213 | p_charcb->nextchar = 0x21;
|
---|
214 | }
|
---|
215 | } else {
|
---|
216 | /* No memory to accept connection. Just accept and then close */
|
---|
217 | int sock;
|
---|
218 | struct sockaddr cliaddr;
|
---|
219 | socklen_t clilen;
|
---|
220 |
|
---|
221 | sock = lwip_accept(listenfd, &cliaddr, &clilen);
|
---|
222 | if (sock >= 0) {
|
---|
223 | lwip_close(sock);
|
---|
224 | }
|
---|
225 | }
|
---|
226 | }
|
---|
227 | /* Go through list of connected clients and process data */
|
---|
228 | for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next) {
|
---|
229 | if (FD_ISSET(p_charcb->socket, &readset)) {
|
---|
230 | /* This socket is ready for reading. This could be because someone typed
|
---|
231 | * some characters or it could be because the socket is now closed. Try reading
|
---|
232 | * some data to see. */
|
---|
233 | if (do_read(p_charcb) < 0) {
|
---|
234 | break;
|
---|
235 | }
|
---|
236 | }
|
---|
237 | if (FD_ISSET(p_charcb->socket, &writeset)) {
|
---|
238 | char line[80];
|
---|
239 | char setchar = p_charcb->nextchar;
|
---|
240 |
|
---|
241 | for (i = 0; i < 59; i++) {
|
---|
242 | line[i] = setchar;
|
---|
243 | if (++setchar == 0x7f) {
|
---|
244 | setchar = 0x21;
|
---|
245 | }
|
---|
246 | }
|
---|
247 | line[i++] = '\n';
|
---|
248 | line[i++] = '\r';
|
---|
249 | line[i] = 0;
|
---|
250 | if (lwip_write(p_charcb->socket, line, strlen(line)) < 0) {
|
---|
251 | close_chargen(p_charcb);
|
---|
252 | break;
|
---|
253 | }
|
---|
254 | if (++p_charcb->nextchar == 0x7f) {
|
---|
255 | p_charcb->nextchar = 0x21;
|
---|
256 | }
|
---|
257 | }
|
---|
258 | }
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 |
|
---|
263 | /**************************************************************
|
---|
264 | * void chargen_init(void)
|
---|
265 | *
|
---|
266 | * This function initializes the chargen service. This function
|
---|
267 | * may only be called either before or after tasking has started.
|
---|
268 | **************************************************************/
|
---|
269 | void
|
---|
270 | chargen_init(void)
|
---|
271 | {
|
---|
272 | sys_thread_new(CHARGEN_THREAD_NAME, chargen_thread, 0, CHARGEN_THREAD_STACKSIZE, CHARGEN_PRIORITY);
|
---|
273 | }
|
---|
274 |
|
---|
275 | #endif /* LWIP_SOCKET && LWIP_SOCKET_SELECT */
|
---|