📄 webpage.c
字号:
/*
* Copyright 2006 by Texas Instruments Incorporated.
* All rights reserved. Property of Texas Instruments Incorporated.
* Restricted rights to use, duplicate or disclose this code are
* granted through contract.
*
* @(#) TCP/IP_Network_Developers_Kit 1.91.00.08 08-22-2006 (ndk-a08)
*/
//--------------------------------------------------------------------------
// IP Stack Configuration Demonstration
//--------------------------------------------------------------------------
// webpage.c
//
// Web page and CGI function
//
// Author: Michael A. Denio
// Copyright 2000, 2001 by Texas Instruments Inc.
//-------------------------------------------------------------------------
#include <std.h>
#include <sys.h>
#include <sem.h>
#include <tsk.h>
#include <netmain.h>
#include "../../../tools/common/console/console.h"
#include "../../../tools/common/cgi/cgiparse.h"
#include "cfgdemo.h"
#include "webinfo.h"
#pragma DATA_SECTION(DEFAULT, ".far:NDK_OBJMEM");
#include "webdata/default.c"
#pragma DATA_SECTION(CONFIG, ".far:NDK_OBJMEM");
#include "webdata/config.c"
#pragma DATA_SECTION(USERMSG, ".far:NDK_OBJMEM");
#include "webdata/usermsg.c"
#pragma DATA_SECTION(TIBUG, ".far:NDK_OBJMEM");
#include "webdata/tibug.c"
static int cgiCfgStart(SOCKET htmlSock, int ContentLength, char *pArgs);
static int cgiCfgPass(SOCKET htmlSock, int ContentLength, char *pArgs);
static int cgiCfgDone(SOCKET htmlSock, int ContentLength, char *pArgs);
static void UserMessage(SOCKET htmlSock, char *pMsg);
void AddWebFiles(void)
{
efs_createfile("index.html", DEFAULT_SIZE, DEFAULT);
efs_createfile("tibug.jpg", TIBUG_SIZE, TIBUG);
efs_createfile("cfgstart.cgi", 0, (UINT8 *)cgiCfgStart);
efs_createfile("cfgpass.cgi", 0, (UINT8 *)cgiCfgPass);
efs_createfile("cfgdone.cgi", 0, (UINT8 *)cgiCfgDone);
}
void RemoveWebFiles(void)
{
efs_destroyfile("cfgstart.cgi");
efs_destroyfile("cfgpass.cgi");
efs_destroyfile("cfgdone.cgi");
efs_destroyfile("tibug.jpg");
efs_destroyfile("index.html");
}
// Default password is 'config'
static char CfgPassword[17] = { 'c', 'o', 'n', 'f', 'i', 'g', 0 };
//
// Page Creation Macro
//
#define html(str) httpSendClientStr(htmlSock, (char *)str)
//
// Config Functions
//
static void ReadConfig();
static void BuildConfig();
//
// Current configuration
//
// Note if MainConfigValid == 1 then we have *some* valid
// configuration. Otherwise, we're still in "ping" IP mode
//
static char Hostname[32];
static char Domainname[32];
static char SessionID[9];
static int SessionValid = 0;
static int fUseDHCP;
static IPN IpAddr;
static IPN IpMask;
static IPN IpGate;
static IPN IpDNS;
//
// cgiCfgStart()
//
// This function is used to request access to a valid confiuration
// page. It will check the password entered, and generate a new
// session ID for the page. Then it will fill in the page with the
// current configuration and display it to the user.
//
static int cgiCfgStart(SOCKET htmlSock, int ContentLength, char *pArgs )
{
char *password = 0;
char *buffer, *key, *value;
int len;
int parseIndex;
UINT32 session,tmp;
char IpString[32];
// 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("password", key) )
password = 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 password is incorrect or missing, generate an error
if( !password || strcmp( CfgPassword, password ) )
{
UserMessage( htmlSock, "The password you entered is invalid" );
goto ERROR;
}
//
// The password is corrent. Gernerate the config screen
//
// Get a new session ID - This is a demo - don't need to be too elaborate
session = llTimerGetTime( &tmp ) * 113417;
session += tmp * 705213;
sprintf( SessionID, "%08x", session );
SessionValid = 1;
// Read in the current configuration
ReadConfig();
// Clear the message fields
memset( CONFIG+CONFIG_SESSIONID_OFFSET, ' ', CONFIG_SESSIONID_MAXLEN );
memset( CONFIG+CONFIG_HOSTNAME_OFFSET, ' ', CONFIG_HOSTNAME_MAXLEN );
memset( CONFIG+CONFIG_DOMAIN_OFFSET, ' ', CONFIG_DOMAIN_MAXLEN );
memset( CONFIG+CONFIG_ADDRTYPE_OFFSET, ' ', CONFIG_ADDRTYPE_MAXLEN );
memset( CONFIG+CONFIG_IPADDR_OFFSET, ' ', CONFIG_IPADDR_MAXLEN );
memset( CONFIG+CONFIG_IPMASK_OFFSET, ' ', CONFIG_IPMASK_MAXLEN );
memset( CONFIG+CONFIG_GATEWAY_OFFSET, ' ', CONFIG_GATEWAY_MAXLEN );
memset( CONFIG+CONFIG_DNS_OFFSET, ' ', CONFIG_DNS_MAXLEN );
// Fill in the fields
// SessionID
*(CONFIG+CONFIG_SESSIONID_OFFSET) = '"';
memcpy(CONFIG+CONFIG_SESSIONID_OFFSET+1,SessionID,8);
*(CONFIG+CONFIG_SESSIONID_OFFSET+9) = '"';
// Hostname
mmCopy( CONFIG+CONFIG_HOSTNAME_OFFSET, Hostname, strlen(Hostname) );
// Domainname
mmCopy( CONFIG+CONFIG_DOMAIN_OFFSET, Domainname, strlen(Domainname) );
// AddrType
if( !MainConfigValid )
mmCopy( CONFIG+CONFIG_ADDRTYPE_OFFSET, "Temporary", 9 );
else if( fUseDHCP )
mmCopy( CONFIG+CONFIG_ADDRTYPE_OFFSET, "DHCP", 4 );
else
mmCopy( CONFIG+CONFIG_ADDRTYPE_OFFSET, "Fixed", 5 );
// IP Addr
NtIPN2Str( IpAddr, IpString );
mmCopy( CONFIG+CONFIG_IPADDR_OFFSET, IpString, strlen(IpString) );
// IP Mask
NtIPN2Str( IpMask, IpString );
mmCopy( CONFIG+CONFIG_IPMASK_OFFSET, IpString, strlen(IpString) );
// IP Gate
if( IpGate )
{
NtIPN2Str( IpGate, IpString );
mmCopy( CONFIG+CONFIG_GATEWAY_OFFSET, IpString, strlen(IpString) );
}
else
mmCopy( CONFIG+CONFIG_GATEWAY_OFFSET, "<i>None</i>", 11 );
// IP Addr
if( IpDNS )
{
NtIPN2Str( IpDNS, IpString );
mmCopy( CONFIG+CONFIG_DNS_OFFSET, IpString, strlen(IpString) );
}
else
mmCopy( CONFIG+CONFIG_DNS_OFFSET, "<i>None</i>", 11 );
// Send the completed form
send( htmlSock, CONFIG, CONFIG_SIZE, 0 );
ERROR:
if( buffer )
mmBulkFree( buffer );
return( 1 );
}
//
// cgiCfgPass()
//
// This function is used to change the user password.
//
static int cgiCfgPass(SOCKET htmlSock, int ContentLength, char *pArgs )
{
char *password = 0, *passnew1 = 0, *passnew2 = 0;
char *buffer, *key, *value;
int len;
int parseIndex;
// 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("password", key) )
password = value;
else if( !strcmp("passnew1", key) )
passnew1 = value;
else if( !strcmp("passnew2", key) )
passnew2 = 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 password is incorrect or missing, generate an error
if( !password || strcmp( CfgPassword, password ) )
{
UserMessage( htmlSock, "The password you entered is invalid" );
goto ERROR;
}
// If the new passwords don't match, generate an error
if( !passnew1 || !passnew2 || strcmp( passnew1, passnew2 ) )
{
UserMessage( htmlSock, "The new passwords you entered do not match" );
goto ERROR;
}
// The new password can not be too long, but we won't chance it (the
// user may have modified the form.
*(passnew1+16) = 0;
strcpy( CfgPassword, passnew1 );
UserMessage( htmlSock, "The password has been changed successfully" );
ERROR:
if( buffer )
mmBulkFree( buffer );
return( 1 );
}
//
// cgiCfgDone()
//
// This function is used to submit a new configuration.
//
static int cgiCfgDone(SOCKET htmlSock, int ContentLength, char *pArgs )
{
char *sessionid = 0, *hostname = 0, *ipaddrtype = 0, *domain = 0;
char *ipaddr = 0, *ipmask = 0, *ipgate = 0, *ipdns = 0;
char *buffer, *key, *value;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -