httpd-cgi.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**
  2. * \addtogroup httpd
  3. * @{
  4. */
  5. /**
  6. * \file
  7. * Web server script interface
  8. * \author
  9. * Adam Dunkels <adam@sics.se>
  10. *
  11. */
  12. /*
  13. * Copyright (c) 2001-2006, Adam Dunkels.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted provided that the following conditions
  18. * are met:
  19. * 1. Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in the
  23. * documentation and/or other materials provided with the distribution.
  24. * 3. The name of the author may not be used to endorse or promote
  25. * products derived from this software without specific prior
  26. * written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  30. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  32. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  35. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  36. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  37. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * This file is part of the uIP TCP/IP stack.
  41. *
  42. * $Id: httpd-cgi.c,v 1.2 2006/06/11 21:46:37 adam Exp $
  43. *
  44. */
  45. #include "uip.h"
  46. #include "psock.h"
  47. #include "httpd.h"
  48. #include "httpd-cgi.h"
  49. #include "httpd-fs.h"
  50. #include <stdio.h>
  51. #include <string.h>
  52. HTTPD_CGI_CALL(file, "file-stats", file_stats);
  53. HTTPD_CGI_CALL(tcp, "tcp-connections", tcp_stats);
  54. HTTPD_CGI_CALL(net, "net-stats", net_stats);
  55. static const struct httpd_cgi_call *calls[] = { &file, &tcp, &net, NULL };
  56. /*---------------------------------------------------------------------------*/
  57. static
  58. PT_THREAD(nullfunction(struct httpd_state *s, char *ptr))
  59. {
  60. PSOCK_BEGIN(&s->sout);
  61. PSOCK_END(&s->sout);
  62. }
  63. /*---------------------------------------------------------------------------*/
  64. httpd_cgifunction
  65. httpd_cgi(char *name)
  66. {
  67. const struct httpd_cgi_call **f;
  68. /* Find the matching name in the table, return the function. */
  69. for(f = calls; *f != NULL; ++f) {
  70. if(strncmp((*f)->name, name, strlen((*f)->name)) == 0) {
  71. return (*f)->function;
  72. }
  73. }
  74. return nullfunction;
  75. }
  76. /*---------------------------------------------------------------------------*/
  77. static unsigned short
  78. generate_file_stats(void *arg)
  79. {
  80. char *f = (char *)arg;
  81. return snprintf((char *)uip_appdata, UIP_APPDATA_SIZE, "%5u", httpd_fs_count(f));
  82. }
  83. /*---------------------------------------------------------------------------*/
  84. static
  85. PT_THREAD(file_stats(struct httpd_state *s, char *ptr))
  86. {
  87. PSOCK_BEGIN(&s->sout);
  88. PSOCK_GENERATOR_SEND(&s->sout, generate_file_stats, strchr(ptr, ' ') + 1);
  89. PSOCK_END(&s->sout);
  90. }
  91. /*---------------------------------------------------------------------------*/
  92. static const char closed[] = /* "CLOSED",*/
  93. {0x43, 0x4c, 0x4f, 0x53, 0x45, 0x44, 0};
  94. static const char syn_rcvd[] = /* "SYN-RCVD",*/
  95. {0x53, 0x59, 0x4e, 0x2d, 0x52, 0x43, 0x56,
  96. 0x44, 0};
  97. static const char syn_sent[] = /* "SYN-SENT",*/
  98. {0x53, 0x59, 0x4e, 0x2d, 0x53, 0x45, 0x4e,
  99. 0x54, 0};
  100. static const char established[] = /* "ESTABLISHED",*/
  101. {0x45, 0x53, 0x54, 0x41, 0x42, 0x4c, 0x49, 0x53, 0x48,
  102. 0x45, 0x44, 0};
  103. static const char fin_wait_1[] = /* "FIN-WAIT-1",*/
  104. {0x46, 0x49, 0x4e, 0x2d, 0x57, 0x41, 0x49,
  105. 0x54, 0x2d, 0x31, 0};
  106. static const char fin_wait_2[] = /* "FIN-WAIT-2",*/
  107. {0x46, 0x49, 0x4e, 0x2d, 0x57, 0x41, 0x49,
  108. 0x54, 0x2d, 0x32, 0};
  109. static const char closing[] = /* "CLOSING",*/
  110. {0x43, 0x4c, 0x4f, 0x53, 0x49,
  111. 0x4e, 0x47, 0};
  112. static const char time_wait[] = /* "TIME-WAIT,"*/
  113. {0x54, 0x49, 0x4d, 0x45, 0x2d, 0x57, 0x41,
  114. 0x49, 0x54, 0};
  115. static const char last_ack[] = /* "LAST-ACK"*/
  116. {0x4c, 0x41, 0x53, 0x54, 0x2d, 0x41, 0x43,
  117. 0x4b, 0};
  118. static const char *states[] = {
  119. closed,
  120. syn_rcvd,
  121. syn_sent,
  122. established,
  123. fin_wait_1,
  124. fin_wait_2,
  125. closing,
  126. time_wait,
  127. last_ack};
  128. static unsigned short
  129. generate_tcp_stats(void *arg)
  130. {
  131. struct uip_conn *conn;
  132. struct httpd_state *s = (struct httpd_state *)arg;
  133. conn = &uip_conns[s->count];
  134. return snprintf((char *)uip_appdata, UIP_APPDATA_SIZE,
  135. "<tr><td>%d</td><td>%u.%u.%u.%u:%u</td><td>%s</td><td>%u</td><td>%u</td><td>%c %c</td></tr>\r\n",
  136. htons(conn->lport),
  137. htons(conn->ripaddr[0]) >> 8,
  138. htons(conn->ripaddr[0]) & 0xff,
  139. htons(conn->ripaddr[1]) >> 8,
  140. htons(conn->ripaddr[1]) & 0xff,
  141. htons(conn->rport),
  142. states[conn->tcpstateflags & UIP_TS_MASK],
  143. conn->nrtx,
  144. conn->timer,
  145. (uip_outstanding(conn))? '*':' ',
  146. (uip_stopped(conn))? '!':' ');
  147. }
  148. /*---------------------------------------------------------------------------*/
  149. static
  150. PT_THREAD(tcp_stats(struct httpd_state *s, char *ptr))
  151. {
  152. PSOCK_BEGIN(&s->sout);
  153. for(s->count = 0; s->count < UIP_CONNS; ++s->count) {
  154. if((uip_conns[s->count].tcpstateflags & UIP_TS_MASK) != UIP_CLOSED) {
  155. PSOCK_GENERATOR_SEND(&s->sout, generate_tcp_stats, s);
  156. }
  157. }
  158. PSOCK_END(&s->sout);
  159. }
  160. /*---------------------------------------------------------------------------*/
  161. static unsigned short
  162. generate_net_stats(void *arg)
  163. {
  164. struct httpd_state *s = (struct httpd_state *)arg;
  165. return snprintf((char *)uip_appdata, UIP_APPDATA_SIZE,
  166. "%5u\n", ((uip_stats_t *)&uip_stat)[s->count]);
  167. }
  168. static
  169. PT_THREAD(net_stats(struct httpd_state *s, char *ptr))
  170. {
  171. PSOCK_BEGIN(&s->sout);
  172. #if UIP_STATISTICS
  173. for(s->count = 0; s->count < sizeof(uip_stat) / sizeof(uip_stats_t);
  174. ++s->count) {
  175. PSOCK_GENERATOR_SEND(&s->sout, generate_net_stats, s);
  176. }
  177. #endif /* UIP_STATISTICS */
  178. PSOCK_END(&s->sout);
  179. }
  180. /*---------------------------------------------------------------------------*/
  181. /** @} */