chargen.c 7.4 KB

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