adamdunkels | 7483407 | 2003-08-09 13:24:36 +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 | fb24dff | 2004-09-12 14:07:30 +0000 | [diff] [blame] | 13 | * 3. The name of the author may not be used to endorse or promote |
adamdunkels | 7483407 | 2003-08-09 13:24:36 +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 | fb24dff | 2004-09-12 14:07:30 +0000 | [diff] [blame] | 31 | * $Id: httpd-cgi.c,v 1.2 2004/09/12 14:07:31 adamdunkels Exp $ |
adamdunkels | 7483407 | 2003-08-09 13:24:36 +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 | #include "uip.h" |
| 46 | #include "httpd.h" |
| 47 | #include "httpd-cgi.h" |
| 48 | #include "httpd-fs.h" |
| 49 | |
| 50 | #include "petsciiconv.h" |
| 51 | |
| 52 | #ifdef __CBM__ |
| 53 | #include <cbm.h> |
| 54 | #include <c64.h> |
| 55 | #endif /* __CBM__ */ |
| 56 | |
| 57 | #include <stdio.h> |
| 58 | #include <string.h> |
| 59 | |
| 60 | static u8_t file_stats(void); |
| 61 | static u8_t tcp_stats(void); |
| 62 | static u8_t processes(void); |
| 63 | |
| 64 | static u8_t d64output(void); |
| 65 | static u8_t diroutput(void); |
| 66 | |
| 67 | httpd_cgifunction httpd_cgitab[] = { |
| 68 | NULL, |
| 69 | tcp_stats, /* CGI function "b" */ |
| 70 | processes, /* CGI function "c" */ |
| 71 | |
| 72 | d64output, /* CGI function "d" */ |
| 73 | }; |
| 74 | |
| 75 | static const char closed[] = /* "CLOSED",*/ |
| 76 | {0x43, 0x4c, 0x4f, 0x53, 0x45, 0x44, 0}; |
| 77 | static const char syn_rcvd[] = /* "SYN-RCVD",*/ |
| 78 | {0x53, 0x59, 0x4e, 0x2d, 0x52, 0x43, 0x56, |
| 79 | 0x44, 0}; |
| 80 | static const char syn_sent[] = /* "SYN-SENT",*/ |
| 81 | {0x53, 0x59, 0x4e, 0x2d, 0x53, 0x45, 0x4e, |
| 82 | 0x54, 0}; |
| 83 | static const char established[] = /* "ESTABLISHED",*/ |
| 84 | {0x45, 0x53, 0x54, 0x41, 0x42, 0x4c, 0x49, 0x53, 0x48, |
| 85 | 0x45, 0x44, 0}; |
| 86 | static const char fin_wait_1[] = /* "FIN-WAIT-1",*/ |
| 87 | {0x46, 0x49, 0x4e, 0x2d, 0x57, 0x41, 0x49, |
| 88 | 0x54, 0x2d, 0x31, 0}; |
| 89 | static const char fin_wait_2[] = /* "FIN-WAIT-2",*/ |
| 90 | {0x46, 0x49, 0x4e, 0x2d, 0x57, 0x41, 0x49, |
| 91 | 0x54, 0x2d, 0x32, 0}; |
| 92 | static const char closing[] = /* "CLOSING",*/ |
| 93 | {0x43, 0x4c, 0x4f, 0x53, 0x49, |
| 94 | 0x4e, 0x47, 0}; |
| 95 | static const char time_wait[] = /* "TIME-WAIT,"*/ |
| 96 | {0x54, 0x49, 0x4d, 0x45, 0x2d, 0x57, 0x41, |
| 97 | 0x49, 0x54, 0}; |
| 98 | static const char last_ack[] = /* "LAST-ACK"*/ |
| 99 | {0x4c, 0x41, 0x53, 0x54, 0x2d, 0x41, 0x43, |
| 100 | 0x4b, 0}; |
| 101 | |
| 102 | static const char *states[] = { |
| 103 | closed, |
| 104 | syn_rcvd, |
| 105 | syn_sent, |
| 106 | established, |
| 107 | fin_wait_1, |
| 108 | fin_wait_2, |
| 109 | closing, |
| 110 | time_wait, |
| 111 | last_ack}; |
| 112 | |
| 113 | |
| 114 | /*-----------------------------------------------------------------------------------*/ |
| 115 | #if 0 |
| 116 | static u8_t |
| 117 | file_stats(void) |
| 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 | uip_send(uip_appdata, sprintf((char *)uip_appdata, "%5u", httpd_fs_count(&hs->script[4]))); |
| 126 | return 0; |
| 127 | } |
| 128 | #endif /* 0 */ |
| 129 | /*-----------------------------------------------------------------------------------*/ |
| 130 | static u8_t |
| 131 | tcp_stats(void) |
| 132 | { |
| 133 | register struct uip_conn *conn; |
| 134 | |
| 135 | if(uip_acked()) { |
| 136 | /* If the previously sent data has been acknowledged, we move |
| 137 | forward one connection. */ |
| 138 | if(++hs->count == UIP_CONNS) { |
| 139 | /* If all connections has been printed out, we are done and |
| 140 | return 1. */ |
| 141 | return 1; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | conn = &uip_conns[hs->count]; |
| 146 | while((conn->tcpstateflags & TS_MASK) == CLOSED) { |
| 147 | if(++hs->count == UIP_CONNS) { |
| 148 | /* If all connections has been printed out, we are done and |
| 149 | return 1. */ |
| 150 | return 1; |
| 151 | } |
| 152 | conn = &uip_conns[hs->count]; |
| 153 | } |
| 154 | |
| 155 | uip_send(uip_appdata, sprintf((char *)uip_appdata, |
| 156 | "<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", |
| 157 | htons(conn->lport), |
| 158 | htons(conn->ripaddr[0]) >> 8, |
| 159 | htons(conn->ripaddr[0]) & 0xff, |
| 160 | htons(conn->ripaddr[1]) >> 8, |
| 161 | htons(conn->ripaddr[1]) & 0xff, |
| 162 | htons(conn->rport), |
| 163 | states[conn->tcpstateflags & TS_MASK], |
| 164 | conn->nrtx, |
| 165 | conn->timer, |
| 166 | (uip_outstanding(conn))? '*':' ', |
| 167 | (uip_stopped(conn))? '!':' ')); |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | /*-----------------------------------------------------------------------------------*/ |
| 172 | static u8_t |
| 173 | processes(void) |
| 174 | { |
| 175 | u8_t i; |
| 176 | register struct dispatcher_proc *p; |
| 177 | char name[40]; |
| 178 | |
| 179 | p = DISPATCHER_PROCS(); |
| 180 | for(i = 0; i < hs->count; ++i) { |
| 181 | if(p != NULL) { |
| 182 | p = p->next; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | if(uip_acked()) { |
| 187 | /* If the previously sent data has been acknowledged, we move |
| 188 | forward one connection. */ |
| 189 | ++hs->count; |
| 190 | if(p != NULL) { |
| 191 | p = p->next; |
| 192 | } |
| 193 | if(p == NULL) { |
| 194 | /* If all processes have been printed out, we are done and |
| 195 | return 1. */ |
| 196 | return 1; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | strncpy(name, p->name, 40); |
| 201 | petsciiconv_toascii(name, 40); |
| 202 | uip_send(uip_appdata, |
| 203 | sprintf((char *)uip_appdata, |
| 204 | "<tr align=\"center\"><td>%3d</td><td>%s</td><td>0x%04x</td><td>0x%04x</td><td>0x%04x</td></tr>\r\n", |
| 205 | p->id, name, |
| 206 | p->idle, p->signalhandler, p->uiphandler)); |
| 207 | return 0; |
| 208 | } |
| 209 | /*-----------------------------------------------------------------------------------*/ |
| 210 | struct drv_state { |
| 211 | u8_t track; |
| 212 | u8_t sect; |
| 213 | }; |
| 214 | |
| 215 | static struct drv_state ds; |
| 216 | |
| 217 | |
| 218 | #include "c64-dio.h" |
| 219 | |
| 220 | static void |
| 221 | read_sector(void) |
| 222 | { |
| 223 | c64_dio_read_block(ds.track, ds.sect, uip_appdata); |
| 224 | } |
| 225 | |
| 226 | static u8_t |
| 227 | next_sector(void) |
| 228 | { |
| 229 | ++ds.sect; |
| 230 | if(ds.track < 18) { |
| 231 | if(ds.sect == 21) { |
| 232 | ++ds.track; |
| 233 | ds.sect = 0; |
| 234 | } |
| 235 | } else if(ds.track < 25) { |
| 236 | if(ds.sect == 19) { |
| 237 | ++ds.track; |
| 238 | ds.sect = 0; |
| 239 | } |
| 240 | } else if(ds.track < 31) { |
| 241 | if(ds.sect == 18) { |
| 242 | ++ds.track; |
| 243 | ds.sect = 0; |
| 244 | } |
| 245 | } else if(ds.track < 36) { |
| 246 | if(ds.sect == 17) { |
| 247 | ++ds.track; |
| 248 | ds.sect = 0; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | if(ds.track == 36) { |
| 253 | return 1; |
| 254 | } |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static u8_t |
| 259 | d64output(void) |
| 260 | { |
| 261 | if(hs->count == 0) { |
| 262 | ds.track = 1; |
| 263 | ds.sect = 0; |
| 264 | /* c64_dio_init(8);*/ |
| 265 | } |
| 266 | |
| 267 | if(uip_acked()) { |
| 268 | ++hs->count; |
| 269 | if(next_sector()) { |
| 270 | return 1; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | read_sector(); |
| 275 | uip_send(uip_appdata, 256); |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | /*-----------------------------------------------------------------------------------*/ |