adamdunkels | 3023dee | 2003-07-04 10:54:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2001, Adam Dunkels. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
adamdunkels | 55637d9 | 2004-08-09 22:22:47 +0000 | [diff] [blame] | 13 | * 3. The name of the author may not be used to endorse or promote |
adamdunkels | 3023dee | 2003-07-04 10:54:51 +0000 | [diff] [blame] | 14 | * products derived from this software without specific prior |
| 15 | * written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
| 18 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 23 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | * |
| 29 | * This file is part of the uIP TCP/IP stack. |
| 30 | * |
adamdunkels | 55637d9 | 2004-08-09 22:22:47 +0000 | [diff] [blame] | 31 | * $Id: httpd-cgi.c,v 1.2 2004/08/09 22:22:47 adamdunkels Exp $ |
adamdunkels | 3023dee | 2003-07-04 10:54:51 +0000 | [diff] [blame] | 32 | * |
| 33 | */ |
| 34 | |
| 35 | /* |
| 36 | * This file includes functions that are called by the web server |
| 37 | * scripts. The functions takes no argument, and the return value is |
| 38 | * interpreted as follows. A zero means that the function did not |
| 39 | * complete and should be invoked for the next packet as well. A |
| 40 | * non-zero value indicates that the function has completed and that |
| 41 | * the web server should move along to the next script line. |
| 42 | * |
| 43 | */ |
| 44 | |
| 45 | |
| 46 | #include "uip.h" |
| 47 | #include "httpd.h" |
| 48 | #include "httpd-cgi.h" |
| 49 | #include "httpd-fs.h" |
| 50 | |
| 51 | #include "petsciiconv.h" |
| 52 | |
adamdunkels | 3023dee | 2003-07-04 10:54:51 +0000 | [diff] [blame] | 53 | #include <stdio.h> |
| 54 | #include <string.h> |
| 55 | |
| 56 | static u8_t file_stats(void); |
| 57 | static u8_t tcp_stats(void); |
| 58 | static u8_t processes(void); |
| 59 | |
| 60 | static u8_t d64output(void); |
| 61 | |
| 62 | static u8_t file_totals(void); |
| 63 | |
| 64 | httpd_cgifunction httpd_cgitab[] = { |
| 65 | file_stats, /* CGI function "a" */ |
| 66 | tcp_stats, /* CGI function "b" */ |
| 67 | processes, /* CGI function "c" */ |
| 68 | |
| 69 | d64output, /* CGI function "d" */ |
| 70 | |
| 71 | file_totals, /* CGI function "e" */ |
| 72 | }; |
| 73 | |
| 74 | static const char closed[] = /* "CLOSED",*/ |
| 75 | {0x43, 0x4c, 0x4f, 0x53, 0x45, 0x44, 0}; |
| 76 | static const char syn_rcvd[] = /* "SYN-RCVD",*/ |
| 77 | {0x53, 0x59, 0x4e, 0x2d, 0x52, 0x43, 0x56, |
| 78 | 0x44, 0}; |
| 79 | static const char syn_sent[] = /* "SYN-SENT",*/ |
| 80 | {0x53, 0x59, 0x4e, 0x2d, 0x53, 0x45, 0x4e, |
| 81 | 0x54, 0}; |
| 82 | static const char established[] = /* "ESTABLISHED",*/ |
| 83 | {0x45, 0x53, 0x54, 0x41, 0x42, 0x4c, 0x49, 0x53, 0x48, |
| 84 | 0x45, 0x44, 0}; |
| 85 | static const char fin_wait_1[] = /* "FIN-WAIT-1",*/ |
| 86 | {0x46, 0x49, 0x4e, 0x2d, 0x57, 0x41, 0x49, |
| 87 | 0x54, 0x2d, 0x31, 0}; |
| 88 | static const char fin_wait_2[] = /* "FIN-WAIT-2",*/ |
| 89 | {0x46, 0x49, 0x4e, 0x2d, 0x57, 0x41, 0x49, |
| 90 | 0x54, 0x2d, 0x32, 0}; |
| 91 | static const char closing[] = /* "CLOSING",*/ |
| 92 | {0x43, 0x4c, 0x4f, 0x53, 0x49, |
| 93 | 0x4e, 0x47, 0}; |
| 94 | static const char time_wait[] = /* "TIME-WAIT,"*/ |
| 95 | {0x54, 0x49, 0x4d, 0x45, 0x2d, 0x57, 0x41, |
| 96 | 0x49, 0x54, 0}; |
| 97 | static const char last_ack[] = /* "LAST-ACK"*/ |
| 98 | {0x4c, 0x41, 0x53, 0x54, 0x2d, 0x41, 0x43, |
| 99 | 0x4b, 0}; |
| 100 | |
| 101 | static const char *states[] = { |
| 102 | closed, |
| 103 | syn_rcvd, |
| 104 | syn_sent, |
| 105 | established, |
| 106 | fin_wait_1, |
| 107 | fin_wait_2, |
| 108 | closing, |
| 109 | time_wait, |
| 110 | last_ack}; |
| 111 | |
| 112 | |
| 113 | /*-----------------------------------------------------------------------------------*/ |
| 114 | static u8_t |
| 115 | file_stats(void) |
| 116 | { |
| 117 | char tmp[40]; |
| 118 | |
| 119 | /* We use sprintf() to print the number of file accesses to a |
| 120 | particular file (given as an argument to the function in the |
| 121 | script). We then use uip_send() to actually send the data. */ |
| 122 | if(uip_acked()) { |
| 123 | return 1; |
| 124 | } |
| 125 | memcpy_P(tmp, &hs->script[4], sizeof(tmp)); |
| 126 | uip_send(uip_appdata, sprintf((char *)uip_appdata, "%10lu", httpd_fs_count(tmp))); |
| 127 | return 0; |
| 128 | } |
| 129 | /*-----------------------------------------------------------------------------------*/ |
| 130 | static u8_t |
| 131 | file_totals(void) |
| 132 | { |
| 133 | /* We use sprintf() to print the number of file accesses to a |
| 134 | particular file (given as an argument to the function in the |
| 135 | script). We then use uip_send() to actually send the data. */ |
| 136 | if(uip_acked()) { |
| 137 | return 1; |
| 138 | } |
| 139 | uip_send(uip_appdata, sprintf((char *)uip_appdata, "%10lu", httpd_fs_total())); |
| 140 | return 0; |
| 141 | } |
| 142 | /*-----------------------------------------------------------------------------------*/ |
| 143 | static u8_t |
| 144 | tcp_stats(void) |
| 145 | { |
| 146 | struct uip_conn *conn; |
| 147 | |
| 148 | if(uip_acked()) { |
| 149 | /* If the previously sent data has been acknowledged, we move |
| 150 | forward one connection. */ |
| 151 | if(++hs->count == UIP_CONNS) { |
| 152 | /* If all connections has been printed out, we are done and |
| 153 | return 1. */ |
| 154 | return 1; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | conn = &uip_conns[hs->count]; |
| 159 | while((conn->tcpstateflags & TS_MASK) == CLOSED) { |
| 160 | if(++hs->count == UIP_CONNS) { |
| 161 | /* If all connections has been printed out, we are done and |
| 162 | return 1. */ |
| 163 | return 1; |
| 164 | } |
| 165 | conn = &uip_conns[hs->count]; |
| 166 | } |
| 167 | |
| 168 | uip_send(uip_appdata, sprintf((char *)uip_appdata, |
| 169 | "<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", |
| 170 | htons(conn->lport), |
| 171 | htons(conn->ripaddr[0]) >> 8, |
| 172 | htons(conn->ripaddr[0]) & 0xff, |
| 173 | htons(conn->ripaddr[1]) >> 8, |
| 174 | htons(conn->ripaddr[1]) & 0xff, |
| 175 | htons(conn->rport), |
| 176 | states[conn->tcpstateflags & TS_MASK], |
| 177 | conn->nrtx, |
| 178 | conn->timer, |
| 179 | (uip_outstanding(conn))? '*':' ', |
| 180 | (uip_stopped(conn))? '!':' ')); |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | /*-----------------------------------------------------------------------------------*/ |
| 185 | static u8_t |
| 186 | processes(void) |
| 187 | { |
| 188 | u8_t i; |
adamdunkels | 55637d9 | 2004-08-09 22:22:47 +0000 | [diff] [blame] | 189 | struct ek_proc *p; |
adamdunkels | 3023dee | 2003-07-04 10:54:51 +0000 | [diff] [blame] | 190 | char name[40]; |
| 191 | |
adamdunkels | 55637d9 | 2004-08-09 22:22:47 +0000 | [diff] [blame] | 192 | p = EK_PROCS(); |
adamdunkels | 3023dee | 2003-07-04 10:54:51 +0000 | [diff] [blame] | 193 | for(i = 0; i < hs->count; ++i) { |
| 194 | if(p != NULL) { |
| 195 | p = p->next; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | if(uip_acked()) { |
| 200 | /* If the previously sent data has been acknowledged, we move |
| 201 | forward one connection. */ |
| 202 | ++hs->count; |
| 203 | if(p != NULL) { |
| 204 | p = p->next; |
| 205 | } |
| 206 | if(p == NULL) { |
| 207 | /* If all processes have been printed out, we are done and |
| 208 | return 1. */ |
| 209 | return 1; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | strncpy(name, p->name, 40); |
| 214 | petsciiconv_toascii(name, 40); |
| 215 | uip_send(uip_appdata, |
| 216 | sprintf((char *)uip_appdata, |
adamdunkels | 55637d9 | 2004-08-09 22:22:47 +0000 | [diff] [blame] | 217 | "<tr align=\"center\"><td>%3d</td><td>%s</td><td>0x%02x</td><td>0x%04x</td><td>0x%04x</td></tr>\r\n", |
| 218 | p->id, name, p->prio, |
| 219 | p->pollhandler, p->eventhandler)); |
adamdunkels | 3023dee | 2003-07-04 10:54:51 +0000 | [diff] [blame] | 220 | return 0; |
| 221 | } |
| 222 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 3023dee | 2003-07-04 10:54:51 +0000 | [diff] [blame] | 223 | static u8_t |
| 224 | d64output(void) |
| 225 | { |
| 226 | |
| 227 | } |
adamdunkels | 3023dee | 2003-07-04 10:54:51 +0000 | [diff] [blame] | 228 | /*-----------------------------------------------------------------------------------*/ |