chargen.c 6.8 KB

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