chargen.c 7.0 KB

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