⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cgi.c

📁 STC51系列的源码
💻 C
字号:


#include "uip.h"
#include "cgi.h"
#include "httpd.h"
#include "fs.h"

#include <stdio.h>
#include <string.h>

u8_t print_stats(u8_t next);
u8_t file_stats(u8_t next);
u8_t tcp_stats(u8_t next);

//cgifunction code cgitab[] = {
//  print_stats,   /* CGI function "a" */
//  file_stats,    /* CGI function "b" */
//  tcp_stats      /* CGI function "c" */
//};

static const char code closed[] =   /*  "CLOSED",*/
{0x43, 0x4c, 0x4f, 0x53, 0x45, 0x44, 0};
static const char code syn_rcvd[] = /*  "SYN-RCVD",*/
{0x53, 0x59, 0x4e, 0x2d, 0x52, 0x43, 0x56, 
 0x44,  0};
static const char code syn_sent[] = /*  "SYN-SENT",*/
{0x53, 0x59, 0x4e, 0x2d, 0x53, 0x45, 0x4e, 
 0x54,  0};
static const char code established[] = /*  "ESTABLISHED",*/
{0x45, 0x53, 0x54, 0x41, 0x42, 0x4c, 0x49, 0x53, 0x48, 
 0x45, 0x44, 0};
static const char code fin_wait_1[] = /*  "FIN-WAIT-1",*/
{0x46, 0x49, 0x4e, 0x2d, 0x57, 0x41, 0x49, 
 0x54, 0x2d, 0x31, 0};
static const char code fin_wait_2[] = /*  "FIN-WAIT-2",*/
{0x46, 0x49, 0x4e, 0x2d, 0x57, 0x41, 0x49, 
 0x54, 0x2d, 0x32, 0};
static const char code closing[] = /*  "CLOSING",*/
{0x43, 0x4c, 0x4f, 0x53, 0x49, 
 0x4e, 0x47, 0};
static const char code time_wait[] = /*  "TIME-WAIT,"*/
{0x54, 0x49, 0x4d, 0x45, 0x2d, 0x57, 0x41, 
 0x49, 0x54, 0};
static const char code last_ack[] = /*  "LAST-ACK"*/
{0x4c, 0x41, 0x53, 0x54, 0x2d, 0x41, 0x43, 
 0x4b, 0};

static const char code *states[] = {
  closed,
  syn_rcvd,
  syn_sent,
  established,
  fin_wait_1,
  fin_wait_2,
  closing,
  time_wait,
  last_ack};
  

/*-----------------------------------------------------------------------------------*/
/* print_stats:
 *
 * Prints out a part of the uIP statistics. The statistics data is
 * written into the uip_appdata buffer. It overwrites any incoming
 * packet.
 */
u8_t print_stats(u8_t next)
{
#if UIP_STATISTICS
  u16_t i, j;
  u8_t *buf;
  u16_t *databytes;
  
  if(next) {
    /* If our last data has been acknowledged, we move on the next
       chunk of statistics. */
    hs->count = hs->count + 4;
    if(hs->count >= sizeof(struct uip_stats)/sizeof(u16_t)) {
      /* We have printed out all statistics, so we return 1 to
	 indicate that we are done. */
      return 1;
    }
  }

  /* Write part of the statistics into the uip_appdata buffer. */
  databytes = (u16_t *)&uip_stat + hs->count;
  buf       = (u8_t *)uip_appdata;

  j = 4 + 1;
  i = hs->count;
  while (i < sizeof(struct uip_stats)/sizeof(u16_t) && --j > 0) {
    sprintf((char *)buf, "%5u\r\n", *databytes);
    ++databytes;
    buf += 6;
    ++i;
  }

  /* Send the data. */
  uip_send(uip_appdata, buf - uip_appdata);
  
  return 0;
#else
  return 1;
#endif /* UIP_STATISTICS */
}
/*-----------------------------------------------------------------------------------*/
u8_t file_stats(u8_t next)
{
  /* We use sprintf() to print the number of file accesses to a
     particular file (given as an argument to the function in the
     script). We then use uip_send() to actually send the data. */
  if(next) {
    return 1;
  }
  uip_send(uip_appdata, sprintf((char *)uip_appdata, "%5u", fs_count(&hs->script[4])));  
  return 0;
}
/*-----------------------------------------------------------------------------------*/
u8_t  tcp_stats(u8_t next)
{
  struct uip_conn *conn;  
  u16_t   chrs_wrtn_to_buf = 0;

  if(next) {
    /* If the previously sent data has been acknowledged, we move
       forward one connection. */
    if(++hs->count == UIP_CONNS) {
      /* If all connections has been printed out, we are done and
	 return 1. */
      return 1;
    }
  }
  
  conn = &uip_conns[hs->count];
  if((conn->tcpstateflags & TS_MASK) == CLOSED) {
    uip_send(uip_appdata, sprintf((char *)uip_appdata,
				  "<tr align=\"center\"><td>-</td><td>-</td><td>%bu</td><td>%bu</td><td>%c %c</td></tr>\r\n",
				  conn->nrtx,
				  conn->timer,
				  (uip_outstanding(conn))? '*':' ',
    				  (uip_stopped(conn))? '!':' '));
  } else {
        /* In SMALL & COMPACT models Keil C's printf() & sprintf() routines 
           can only be passed up to 15 bytes of data, then they fall over. 
           We do successive sprintf() calls here to get around this 
           limitation. */
        chrs_wrtn_to_buf = sprintf((char *)uip_appdata,
                 "<tr align=\"center\"><td>%u.%u.%u.%u:%u</td>",
                 htons(conn->ripaddr[0]) >> 8,
                 htons(conn->ripaddr[0]) & 0xff,
                 htons(conn->ripaddr[1]) >> 8,
                 htons(conn->ripaddr[1]) & 0xff,
                 htons(conn->rport));
        chrs_wrtn_to_buf += sprintf((char *)uip_appdata + chrs_wrtn_to_buf,
                 "<td>%s</td><td>%bu</td><td>%bu</td><td>%c %c</td></tr>\r\n",
                 states[conn->tcpstateflags & TS_MASK],
                 conn->nrtx,
                 conn->timer,
                 (uip_outstanding(conn))? '*':' ',
                 (uip_stopped(conn))? '!':' ');
        uip_send(uip_appdata, chrs_wrtn_to_buf);
  }
  return 0;
}
/*-----------------------------------------------------------------------------------*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -