1 /*
2  * Copyright (c) 2006-2021, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  */
9 
10 #include <rtthread.h>
11 #include <sys/socket.h>
12 
13 #ifdef SAL_USING_POSIX
14 #include <sys/select.h> // only dfs_net
15 #include <dfs_file.h>
16 #include <unistd.h>
17 #include <stdio.h>
18 #include <sys/stat.h>
19 #include <sys/statfs.h>
20 #else
21 #define read        lwip_read
22 #define write       lwip_write
23 #endif /* SAL_USING_POSIX */
24 
25 #include "netdb.h"
26 
27 #define MAX_SERV                 32         /* Maximum number of chargen services. Don't need too many */
28 #define CHARGEN_THREAD_NAME      "chargen"
29 #if RT_THREAD_PRIORITY_MAX == 32
30 #define CHARGEN_PRIORITY         20        /* Really low priority */
31 #else
32 #define CHARGEN_PRIORITY         200       /* Really low priority */
33 #endif
34 #define CHARGEN_THREAD_STACKSIZE 1024
35 struct charcb
36 {
37     struct charcb *next;
38     int socket;
39     struct sockaddr_in cliaddr;
40     socklen_t clilen;
41     char nextchar;
42 };
43 
44 static struct charcb *charcb_list = 0;
45 static int do_read(struct charcb *p_charcb);
46 static void close_chargen(struct charcb *p_charcb);
47 extern int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
48 
49 /**************************************************************
50  * void chargen_thread(void *arg)
51  *
52  * chargen task. This server will wait for connections on well
53  * known TCP port number: 19. For every connection, the server will
54  * write as much data as possible to the tcp port.
55  **************************************************************/
chargen_thread(void * arg)56 static void chargen_thread(void *arg)
57 {
58     int listenfd;
59     struct sockaddr_in chargen_saddr;
60     fd_set readset;
61     fd_set writeset;
62     int i, maxfdp1;
63     struct charcb *p_charcb;
64 
65     /* First acquire our socket for listening for connections */
66     listenfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
67     LWIP_ASSERT("chargen_thread(): Socket create failed.", listenfd >= 0);
68     memset(&chargen_saddr, 0, sizeof(chargen_saddr));
69     chargen_saddr.sin_family = AF_INET;
70     chargen_saddr.sin_addr.s_addr = htonl(INADDR_ANY);
71     chargen_saddr.sin_port = htons(19);     // Chargen server port
72 
73     if (bind(listenfd, (struct sockaddr *) &chargen_saddr, sizeof(chargen_saddr)) == -1)
74         LWIP_ASSERT("chargen_thread(): Socket bind failed.", 0);
75 
76     /* Put socket into listening mode */
77     if (listen(listenfd, MAX_SERV) == -1)
78         LWIP_ASSERT("chargen_thread(): Listen failed.", 0);
79 
80 
81     /* Wait forever for network input: This could be connections or data */
82     for (;;)
83     {
84         maxfdp1 = listenfd + 1;
85 
86         /* Determine what sockets need to be in readset */
87         FD_ZERO(&readset);
88         FD_ZERO(&writeset);
89         FD_SET(listenfd, &readset);
90         for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
91         {
92             if (maxfdp1 < p_charcb->socket + 1)
93                 maxfdp1 = p_charcb->socket + 1;
94             FD_SET(p_charcb->socket, &readset);
95             FD_SET(p_charcb->socket, &writeset);
96         }
97 
98         /* Wait for data or a new connection */
99         i = select(maxfdp1, &readset, &writeset, 0, 0);
100 
101         if (i == 0) continue;
102 
103         /* At least one descriptor is ready */
104         if (FD_ISSET(listenfd, &readset))
105         {
106             /* We have a new connection request!!! */
107             /* Lets create a new control block */
108             p_charcb = (struct charcb *)rt_calloc(1, sizeof(struct charcb));
109             if (p_charcb)
110             {
111                 p_charcb->socket = accept(listenfd,
112                                           (struct sockaddr *) &p_charcb->cliaddr,
113                                           &p_charcb->clilen);
114                 if (p_charcb->socket < 0)
115                     rt_free(p_charcb);
116                 else
117                 {
118                     /* Keep this tecb in our list */
119                     p_charcb->next = charcb_list;
120                     charcb_list = p_charcb;
121                     p_charcb->nextchar = 0x21;
122                 }
123             }
124             else
125             {
126                 /* No memory to accept connection. Just accept and then close */
127                 int sock;
128                 struct sockaddr cliaddr;
129                 socklen_t clilen;
130 
131                 sock = accept(listenfd, &cliaddr, &clilen);
132                 if (sock >= 0)
133                     closesocket(sock);
134             }
135         }
136 
137         /* Go through list of connected clients and process data */
138         for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
139         {
140             if (FD_ISSET(p_charcb->socket, &readset))
141             {
142                 /* This socket is ready for reading. This could be because someone typed
143                  * some characters or it could be because the socket is now closed. Try reading
144                  * some data to see. */
145                 if (do_read(p_charcb) < 0)
146                     break;
147             }
148 
149             if (FD_ISSET(p_charcb->socket, &writeset))
150             {
151                 char line[80];
152                 char setchar = p_charcb->nextchar;
153 
154                 for (i = 0; i < 59; i++)
155                 {
156                     line[i] = setchar;
157                     if (++setchar == 0x7f)
158                         setchar = 0x21;
159                 }
160 
161                 line[i] = 0;
162                 strcat(line, "\n\r");
163                 if (write(p_charcb->socket, line, strlen(line)) < 0)
164                 {
165                     close_chargen(p_charcb);
166                     break;
167                 }
168 
169                 if (++p_charcb->nextchar == 0x7f)
170                     p_charcb->nextchar = 0x21;
171             }
172         }
173     }
174 }
175 
176 /**************************************************************
177  * void close_chargen(struct charcb *p_charcb)
178  *
179  * Close the socket and remove this charcb from the list.
180  **************************************************************/
close_chargen(struct charcb * p_charcb)181 static void close_chargen(struct charcb *p_charcb)
182 {
183     struct charcb *p_search_charcb;
184 
185     /* Either an error or tcp connection closed on other
186      * end. Close here */
187     closesocket(p_charcb->socket);
188 
189     /* Free charcb */
190     if (charcb_list == p_charcb)
191         charcb_list = p_charcb->next;
192     else
193         for (p_search_charcb = charcb_list; p_search_charcb; p_search_charcb = p_search_charcb->next)
194         {
195             if (p_search_charcb->next == p_charcb)
196             {
197                 p_search_charcb->next = p_charcb->next;
198                 break;
199             }
200         }
201 
202     rt_free(p_charcb);
203 }
204 
205 /**************************************************************
206  * void do_read(struct charcb *p_charcb)
207  *
208  * Socket definitely is ready for reading. Read a buffer from the socket and
209  * discard the data.  If no data is read, then the socket is closed and the
210  * charcb is removed from the list and freed.
211  **************************************************************/
do_read(struct charcb * p_charcb)212 static int do_read(struct charcb *p_charcb)
213 {
214     char buffer[80];
215     int readcount;
216 
217     /* Read some data */
218     readcount = read(p_charcb->socket, &buffer, 80);
219     if (readcount <= 0)
220     {
221         close_chargen(p_charcb);
222         return -1;
223     }
224     return 0;
225 }
226 
chargen_init(void)227 void chargen_init(void)
228 {
229     rt_thread_t chargen;
230 
231     chargen = rt_thread_create(CHARGEN_THREAD_NAME,
232                                chargen_thread, RT_NULL,
233                                CHARGEN_THREAD_STACKSIZE,
234                                CHARGEN_PRIORITY, 5);
235     if (chargen != RT_NULL) rt_thread_startup(chargen);
236 }
237 #ifdef RT_USING_FINSH
238 #include <finsh.h>
chargen()239 void chargen()
240 {
241     chargen_init();
242 }
243 FINSH_FUNCTION_EXPORT(chargen, start chargen server);
244 #endif
245