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

📄 webserve.c

📁 一个功能精简的TCP/IP协议栈
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Web Server for 'TCP/IP Lean' (c) Iosoft Ltd. 2000

This software is only licensed for distribution with the book 'TCP/IP Lean',
and may only be used for personal experimentation by the purchaser
of that book, on condition that this copyright notice is retained.
For commercial licensing, contact license@iosoft.co.uk

This is experimental software; use it entirely at your own risk. The author
offers no warranties of any kind, including its fitness for purpose. */

/*
** v0.01 JPB 20/3/00  Derived from Telnet v0.12
** v0.02 JPB 23/3/00  Used function upcall ptrs in TCP.C and IP.C
** v0.03 JPB 11/4/00  Added HTTP header type selection
** v0.04 JPB 12/4/00  Added clickable HTML directory
** v0.05 JPB 13/4/00  Added state machine to handle large directories
** v0.06 JPB 15/4/00  Return directory only if null filename
**                    Return error response if file not found
** v0.07 JPB 3/7/00   Changed default config file to TCPLEAN.CFG
**                    Revised header for book CD
*/

#define VERSION "0.07"

/* HTTP response header: 0=none, 1=simple OK, 2=content-type */
#define HTTP_HEAD 2
/* Directory type: 0=text, 1=simple HTML, 2=table, 3=large dir support */
#define HTML_DIR 3

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <signal.h>
#include <time.h>
#include <stdarg.h>

#include "ether.h"
#include "netutil.h"
#include "net.h"
#include "ip.h"
#include "tcp.h"

#define CFGFILE     "tcplean.cfg"   /* Default config filename */
#define CFGEXT      ".cfg"          /* Default config extension */
#define MAXNETCFG   40              /* Max length of a net config string */
#define FILEDIR     ".\\webdocs\\"  /* Default directory */

/* HTTP and HTML text */
#define HTTP_OK     "HTTP/1.0 200 OK\r\n"
#define HTTP_NOFILE "HTTP/1.0 404 Not found\r\n"
#define HTTP_HTM    "Content-type: text/html\r\n"
#define HTTP_TXT    "Content-type: text/plain\r\n"
#define HTTP_GIF    "Content-type: image/gif\r\n"
#define HTTP_XBM    "Content-type: image/x-xbitmap\r\n"
#define HTTP_BLANK  "\r\n"
#define HTTP_MAXLEN 80

GENFRAME genframe;                  /* Frame for network Tx/Rx */
char cfgfile[MAXPATH+5]=CFGFILE;    /* Config filename */
char netcfg[MAXNETCFG+1]="??";      /* Network config string */
extern BYTE bcast[MACLEN];          /* Broadcast Ethernet addr */
NODE  locnode;                      /* My Ethernet and IP addresses */
int breakflag;                      /* Flag to indicate ctrl-break pressed */
char filedir[MAXPATH+1]=FILEDIR;    /* Directory for files */
char filepath[MAXPATH+1];           /* Filename */
char httpreq[HTTP_MAXLEN+1];        /* Buffer for first part of HTTP request */

/* Debug flags */
extern int netdebug;                /* Verbose packet display */
extern int tcpdebug;                /* TCP frame display */
extern int statedebug;              /* TCP state display */
int webdebug;                       /* Web (HTTP) diagnostic display */

/* Socket storage */
#define NSOCKS 2
TSOCK tsocks[NSOCKS] =              /* Initialise index num and buffer sizes */
{
    {1,{_CBUFFLEN_},{_CBUFFLEN_}},
    {2,{_CBUFFLEN_},{_CBUFFLEN_}}
};

/* Application-specific storage */
typedef struct {
    FILE *in;                       /* File I/P pointer */
    long count;                     /* State variable/counter */
} APPDATA;
APPDATA appdata[NSOCKS];

/* Function pointers: upcalls from TCP/IP stack */
extern NODE *(*get_locnode_n)(int n);                   /* Get local node */
extern int (*server_upcall)(TSOCK *ts, CONN_STATE conn);/* TCP server action */

/* Prototypes */
WORD read_netconfig(char *fname, NODE *np);
NODE *locnode_n(int n);
int server_action(TSOCK *ts, CONN_STATE conn);
void http_get(TSOCK *ts, char *fname);
void http_data(TSOCK *ts);
int dir_head(CBUFF *bp, char *path);
int dir_entry(CBUFF *bp, char *name);
int dir_tail(CBUFF *bp);
void do_receive(GENFRAME *gfp);
void do_poll(GENFRAME *gfp);
int buff_inprintf(CBUFF *bp, char *str, ...);
void disp_usage(void);
void break_handler(int sig);

int main(int argc, char *argv[])
{
    int args=0, err=0, fail, n;
    char *p, temps[18], k=0, c;
    WORD dtype;
    GENFRAME *gfp;
    LWORD mstimer;

    printf("WEBSERVE v" VERSION "\n");          /* Sign on */
    get_locnode_n = locnode_n;                  /* Set upcall func ptrs */
    server_upcall = server_action;
    signal(SIGINT, break_handler);              /* Trap ctrl-C */
    for (n=0; n<NSOCKS; n++)
        tsocks[n].app = &appdata[n];
    while (argc > ++args)                       /* Process command-line args */
    {
        if ((c=argv[args][0])=='-' || c=='/')
        {
            switch (toupper(argv[args][1]))
            {
            case 'C':                           /* -C: config filename */
                strncpy(cfgfile, argv[++args], MAXPATH);
                if ((p=strrchr(cfgfile, '.'))==0 || !isalpha(*(p+1)))
                    strcat(cfgfile, CFGEXT);
                break;
            case 'S':                           /* -S: display TCP states */
                statedebug = 1;
                break;
            case 'T':                           /* -T: display TCP segments */
                tcpdebug = 1;
                break;
            case 'V':                           /* -V: verbose packet display */
                netdebug |= 1;
                break;
            case 'W':                           /* -W: Web (HTTP) diagnostics */
                webdebug = 1;
                break;
            case 'X':                           /* -X: hex packet display */
                netdebug |= 2;
                break;
            default:
                err = 1;
            }
        }
        else
        {
            *filedir = 0;                       /* File directory */
            if (argv[args][0]!='\\' && argv[args][1]!=':' && argv[args][0]!='.')
                strcpy(filedir, ".\\");
            strcat(filedir, argv[args]);
            if (filedir[strlen(filedir)-1] != '\\')
                strcat(filedir, "\\");
        }
    }
    if (err)                                    /* Prompt user if error */
        disp_usage();
    else if (!(dtype=read_netconfig(cfgfile, &locnode)))
        printf("Invalid configuration '%s'\n", cfgfile);
    else
    {
        genframe.g.dtype = dtype;               /* Set frame driver type */
        gfp = &genframe;                        /* Get pointer to frame */
        printf("IP %s", ipstr(locnode.ip, temps));
        printf(" mask %s", ipstr(locnode.mask, temps));
        if (locnode.gate)
            printf(" gate %s", ipstr(locnode.gate, temps));
        if (dtype & DTYPE_ETHER)
            printf(" Ethernet %s", ethstr(locnode.mac, temps));
        printf("\nPress ESC or ctrl-C to exit\n\n");
        while (!breakflag && k!=0x1b)           /* Main loop.. */
        {
            do_receive(gfp);                    /* Receive frames */
            do_poll(gfp);                       /* Poll net drivers */
            if (kbhit())                        /* Get keypress */
                k = getch();
        }
        if (k)
            printf("Closing...\n");
        mstimeout(&mstimer, 0);
        do
        {
            for (n=fail=0; n<NSOCKS; n++)
                fail += !close_tcp(&tsocks[n]);
            do_receive(gfp);
            do_poll(gfp);
        } while (!mstimeout(&mstimer, 1000) && fail);
        if (fail)
        {
            printf("Resetting connections\n");
            for (n=0; n<NSOCKS; n++)
                reset_tcp(&tsocks[n], gfp);
            while (!mstimeout(&mstimer, 1000))
            {
                do_receive(gfp);
                do_poll(gfp);
            }
        }
        close_net(dtype);                       /* Shut down net driver */
    }
    return(0);
}

/* Read network config file to get IP address netmask and gateway
** Return driver type, 0 if error */
WORD read_netconfig(char *fname, NODE *np)
{
    char temps[31];
    WORD dtype=0;
    BYTE b;

    if (read_cfgstr(fname, "net", netcfg, MAXNETCFG))
    {                                               /* Get IP address */
        if (!read_cfgstr(fname, "ip", temps, 30) || (np->ip=atoip(temps))==0)
            printf("No IP address\n");
        else if (!(dtype = open_net(netcfg)))       /* Open net driver */
            printf("Can't open net driver '%s'\n", netcfg);
        else
        {                                           /* Save ether address */
            memcpy(np->mac, ether_addr(dtype), MACLEN);
            np->dtype = dtype;                      /* ..and driver type */
            b = (BYTE)(np->ip >> 24);
            if (read_cfgstr(fname, "mask", temps, 30))
                np->mask = atoip(temps);            /* Get netmask */
            else
                np->mask = b<128 ? 0xff000000L: b<192 ? 0xffff0000L:0xffffff00L;
            if (read_cfgstr(fname, "gate", temps, 30))
                np->gate = atoip(temps);            /* Get gateway IP addr */
            else
                np->gate = 0;
        }
    }
    return(dtype);
}

/* Return ptr to local node 'n' (n=0 for first), return 0 if doesn't exist
** Used by IP functions to get my netmask & gateway addresses */
NODE *locnode_n(int n)
{
    return(n==0 ? &locnode : 0);
}

/* Upcall from TCP stack to server when opening, connecting, receiving data
** or closing. Return 0 to prevent connection opening, or close if connected */
int server_action(TSOCK *ts, CONN_STATE conn)
{
    int ok=1, len;
    WORD port;
    char *s, *name;
    APPDATA *adp;

    port = ts->loc.port;
    adp = (APPDATA *)ts->app;
    if (port != HTTPORT)

⌨️ 快捷键说明

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