chargen.c 7.5 KB

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