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

📄 webpage.c

📁 本软件是TI公司免费提供的网络开发包 现在好象很难找到,有黑心的公司把它改一改,就卖价5000元,对网络开发和网络驱动开发有参考价值
💻 C
📖 第 1 页 / 共 2 页
字号:
    int     len;
    int     parseIndex;
    char    StrIp[16];

    // CGI Functions can now support URI arguments as well if the
    // pArgs pointer is not NULL, and the ContentLength were zero,
    // we could parse the arguments off of pArgs instead.

    // First, allocate a buffer for the request
    buffer = (char*) mmBulkAlloc( ContentLength + 1 );
    if ( !buffer )
        goto ERROR;

    // Now read the data from the client
    len = recv( htmlSock, buffer, ContentLength, MSG_WAITALL );
    if ( len < 1 )
        goto ERROR;

    // Setup to parse the post data
    parseIndex = 0;
    buffer[ContentLength] = '\0';

    // Process request variables until there are none left
    do
    {
        key   = cgiParseVars( buffer, &parseIndex );
        value = cgiParseVars( buffer, &parseIndex );

        if( !strcmp("sessionid", key) )
            sessionid = value;
        else if( !strcmp("hostname", key) )
            hostname = value;
        else if( !strcmp("ipaddrtype", key) )
            ipaddrtype = value;
        else if( !strcmp("domain", key) )
            domain = value;
        else if( !strcmp("IPADDR", key) )
            ipaddr = value;
        else if( !strcmp("IPMASK", key) )
            ipmask = value;
        else if( !strcmp("IPGATE", key) )
            ipgate = value;
        else if( !strcmp("IPDNS", key) )
            ipdns = value;
    } while ( parseIndex != -1 );

    //
    // Output the data we read in...
    //

    httpSendStatusLine(htmlSock, HTTP_OK, CONTENT_TYPE_HTML);
    // CRLF before entity
    html( CRLF );

    //
    // Generate response
    //

    // If the sessionid is incorrect or missing, generate an error
    if( !SessionValid || !sessionid || strcmp( sessionid, SessionID ) )
    {
        UserMessage( htmlSock, "The configuration session has expired. Operation aborted." );
        goto ERROR;
    }

    // The form limits string input, but bound the length anyway in case the form
    // is user-modified.

    strncpy( Hostname, hostname, 16 );
    Hostname[16] = 0;

    if( !strcmp( ipaddrtype, "auto") )
        fUseDHCP = 1;
    else
    {
        fUseDHCP = 0;

        strncpy( Domainname, domain, 31 );
        Hostname[31] = 0;

        strncpy( StrIp, ipaddr, 15 );
        StrIp[15] = 0;
        IpAddr = inet_addr(StrIp);

        strncpy( StrIp, ipmask, 15 );
        StrIp[15] = 0;
        IpMask = inet_addr(StrIp);

        strncpy( StrIp, ipgate, 15 );
        StrIp[15] = 0;
        IpGate = inet_addr(StrIp);

        strncpy( StrIp, ipdns, 15 );
        StrIp[15] = 0;
        IpDNS = inet_addr(StrIp);
    }

    // Sanity check the results.
    // A real product may want to do more checks than this!
    if( !strlen(Hostname) || (!fUseDHCP &&(!IpAddr || !IpMask)) )
    {
        UserMessage( htmlSock, "The configuration entries are not valid." );
        goto ERROR;
    }

    // Install this configuration
    BuildConfig();

    UserMessage( htmlSock, "The system is reconfigured and restarting. \
    If the IP address has changed, you may no longer be able to access these pages.");

    // Close the socket
    fdClose( htmlSock );
    htmlSock = INVALID_SOCKET;

    // Don't accept this session handle again
    SessionValid = 0;

    // Wait a second for the confirmation message to go out
    TaskSleep(1000);

    // Request stack reboot
    NC_NetStop(1);

ERROR:
    if( buffer )
        mmBulkFree( buffer );

    if( htmlSock != INVALID_SOCKET )
        return( 1 );
    return( 0 );
}

//
// UserMessage
//
// This function alters the usermsg.htm file with the supplied message
// and sends it to the supplied socket
//
static void UserMessage( SOCKET htmlSock, char *pMsg )
{
    // Clear out any old message
    memset( USERMSG+USERMSG_MESSAGE_OFFSET, ' ', USERMSG_MESSAGE_MAXLEN);

    // Copy in new message
    mmCopy( USERMSG+USERMSG_MESSAGE_OFFSET, pMsg, strlen(pMsg) );

    // Send out the message
    send( htmlSock, USERMSG, USERMSG_SIZE, 0 );
}

//
// ReadConfig()
//
// Read Configuration Info from hCfg Handle
//
static void ReadConfig()
{
    CI_SERVICE_DHCPC  dhcpc;
    CI_IPNET          ci_net;
    CI_ROUTE          RT;
    IPN               IP;
    int               i,tmp;

    // Get hostname
    tmp = CfgGetImmediate( 0, CFGTAG_SYSINFO, CFGITEM_DHCP_HOSTNAME, 1,
                           31, (UINT8 *)Hostname);
    Hostname[tmp]=0;

    // See if we're using DHCP
    tmp = CfgGetImmediate( 0, CFGTAG_SERVICE, CFGITEM_SERVICE_DHCPCLIENT,
                           1, sizeof(dhcpc), (UINT8 *)&dhcpc );
    if( tmp )
        fUseDHCP = 1;
    else
        fUseDHCP = 0;

    // Get the IP address information
    IpAddr = 0;
    IpMask = 0;
    strcpy( Domainname, "<i>None</i>" );

    tmp = CfgGetImmediate( 0, CFGTAG_IPNET, 1, 1,
                           sizeof(ci_net), (UINT8 *)&ci_net );
    if( tmp )
    {
        IpAddr = ci_net.IPAddr;
        IpMask = ci_net.IPMask;
        *(ci_net.Domain+31) = 0;
        strcpy( Domainname, ci_net.Domain );
    }

    // Get the default route (if any)
    IpGate = 0;
    for( i=1; ; i++ )
    {
        tmp = CfgGetImmediate( 0, CFGTAG_ROUTE, 0, i,
                               sizeof(RT), (UINT8 *)&RT );
        if( !tmp )
            break;

        // Look only for a default route
        if( RT.IPDestAddr == 0 && RT.IPDestMask == 0 )
        {
            IpGate = RT.IPGateAddr;
            break;
        }
    }

    // Get the DNS values
    tmp = CfgGetImmediate( 0, CFGTAG_SYSINFO, CFGITEM_DHCP_DOMAINNAMESERVER,
                           1, 4, (UINT8 *)&IP );
    if( tmp == 4 )
        IpDNS = IP;
    else
        IpDNS = 0;
}

//
// BuildConfig()
//
// Build configuration into linear buffer
//
static void BuildConfig()
{
    int               rc;
    HANDLE            hCfg;
    CI_SERVICE_TELNET telnet;
    CI_SERVICE_HTTP   http;

    // Create a new configuration
    hCfg = CfgNew();
    if( !hCfg )
        return;

    // Add our global hostname to hCfg (to be claimed in all connected domains)
    CfgAddEntry( hCfg, CFGTAG_SYSINFO, CFGITEM_DHCP_HOSTNAME, 0,
                 strlen(Hostname), (UINT8 *)Hostname, 0 );

    // If the IP address is specified, use it
    if( !fUseDHCP )
    {
        CI_IPNET NA;
        CI_ROUTE RT;
        IPN      IPTmp;

        // Setup manual IP address
        bzero( &NA, sizeof(NA) );
        NA.IPAddr  = IpAddr;
        NA.IPMask  = IpMask;
        strcpy( NA.Domain, Domainname );
        NA.NetType = 0;

        // Add the address to interface 1
        CfgAddEntry( hCfg, CFGTAG_IPNET, 1, 0,
                           sizeof(CI_IPNET), (UINT8 *)&NA, 0 );

        // Add the default gateway. Since it is the default, the
        // destination address and mask are both zero (we go ahead
        // and show the assignment for clarity).
        bzero( &RT, sizeof(RT) );
        RT.IPDestAddr = 0;
        RT.IPDestMask = 0;
        RT.IPGateAddr = IpGate;

        // Add the route
        CfgAddEntry( hCfg, CFGTAG_ROUTE, 0, 0,
                           sizeof(CI_ROUTE), (UINT8 *)&RT, 0 );

        // Manually add the DNS server when specified
        if( IpDNS )
            CfgAddEntry( hCfg, CFGTAG_SYSINFO, CFGITEM_DHCP_DOMAINNAMESERVER,
                         0, sizeof(IPTmp), (UINT8 *)&IpDNS, 0 );
    }
    // Else we specify DHCP
    else
    {
        CI_SERVICE_DHCPC dhcpc;

        // Specify DHCP Service on IF-1
        bzero( &dhcpc, sizeof(dhcpc) );
        dhcpc.cisargs.Mode   = CIS_FLG_IFIDXVALID;
        dhcpc.cisargs.IfIdx  = 1;
        dhcpc.cisargs.pCbSrv = &ServiceReport;
        CfgAddEntry( hCfg, CFGTAG_SERVICE, CFGITEM_SERVICE_DHCPCLIENT, 0,
                     sizeof(dhcpc), (UINT8 *)&dhcpc, 0 );
    }

    // Specify TELNET service for our Console example
    bzero( &telnet, sizeof(telnet) );
    telnet.cisargs.IPAddr = INADDR_ANY;
    telnet.cisargs.pCbSrv = &ServiceReport;
    telnet.param.MaxCon   = 2;
    telnet.param.Callback = &ConsoleOpen;
    CfgAddEntry( hCfg, CFGTAG_SERVICE, CFGITEM_SERVICE_TELNET, 0,
                 sizeof(telnet), (UINT8 *)&telnet, 0 );

    // Specify HTTP service
    bzero( &http, sizeof(http) );
    http.cisargs.IPAddr = INADDR_ANY;
    http.cisargs.pCbSrv = &ServiceReport;
    CfgAddEntry( hCfg, CFGTAG_SERVICE, CFGITEM_SERVICE_HTTP, 0,
                 sizeof(http), (UINT8 *)&http, 0 );

    //
    // Configure IPStack/OS Options
    //

    // We don't want to see debug messages less than WARNINGS
    rc = DBG_WARN;
    CfgAddEntry( hCfg, CFGTAG_OS, CFGITEM_OS_DBGPRINTLEVEL,
                 CFG_ADDMODE_UNIQUE, sizeof(uint), (UINT8 *)&rc, 0 );

    // Save the configuration to a linear buffer. If the size is not
    // over our max, install the new configuration.
    rc=0;
    CfgSave( hCfg, &rc, 0 );
    if( rc <= 512 )
    {
        MainConfigSize=512;
        CfgSave( hCfg, &MainConfigSize, MainConfig );
        MainConfigValid = 1;
    }

    CfgFree( hCfg );
}

⌨️ 快捷键说明

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