weblib.c
来自「vt6528芯片交换机API函数和文档运行程序」· C语言 代码 · 共 392 行
C
392 行
/*
* Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
* All rights reserved.
*
* This software is copyrighted by and is the sole property of
* VIA Networking Technologies, Inc. This software may only be used
* in accordance with the corresponding license agreement. Any unauthorized
* use, duplication, transmission, distribution, or disclosure of this
* software is expressly forbidden.
*
* This software is provided by VIA Networking Technologies, Inc. "as is"
* and any express or implied warranties, including, but not limited to, the
* implied warranties of merchantability and fitness for a particular purpose
* are disclaimed. In no event shall VIA Networking Technologies, Inc.
* be liable for any direct, indirect, incidental, special, exemplary, or
* consequential damages.
*
*
* File: weblib.c
*
* Purpose:
*
* Author: Jenda Jao
*
* Date: Jan 08, 2002
*
* Functions:
*
* Revision History:
*
*/
#if !defined(__STR_H__)
#include "str.h"
#endif
#if !defined(__SOC_H__)
#include "soc.h"
#endif
#if !defined(__TIMER_H__)
#include "timer.h"
#endif
#if !defined(__PLATFORM_H__)
#include "platform.h"
#endif
#if !defined(__SWSYS_H__)
#include "swsys.h"
#endif
#if !defined(__SWPKT_H__)
#include "swpkt.h"
#endif
#if !defined(__NETIF_H__)
#include "netif.h"
#endif
#if !defined(__HTTPD_H__)
#include "httpd.h"
#endif
#include "weblib.h"
#include "cgi_show.h"
#include "cgi_set.h"
#include "webfs.h"
/*--------------------- Static Definitions ------------------------*/
/*--------------------- Static Types ------------------------------*/
// CGI function name/pointer mapping
typedef struct tagSWEBCgiMap {
PSTR szName;
UINT16 (*pfnCgi)(void); // Cgi func ptr
} SWEBCgiMap;
/*--------------------- Static Macros -----------------------------*/
/*--------------------- Static Classes ----------------------------*/
/*--------------------- Static Variables --------------------------*/
SWEBCgiMap s_aSCgiTbl[] = {
{"ShowList", CGI_wShowList},
{"ShowLogin", CGI_wShowLogin},
{"Login", CGI_wLogin},
{"Logout", CGI_wLogout},
{"ShowPortSts", CGI_wShowPortSts},
{"ShowPortCfg", CGI_wShowPortCfg},
{"SetPortCfg", CGI_wSetPortCfg},
{"ShowTrkCfg", CGI_wShowTrkCfg},
{"SetTrkCfg", CGI_wSetTrkCfg},
{"ShowVlanCfg", CGI_wShowVlanCfg},
{"SetVlanCfg", CGI_wSetVlanCfg},
{"ShowVlanGrp", CGI_wShowVlanGrp},
{"SetVlanGrp", CGI_wSetVlanGrp},
{"ShowV1qifCfg", CGI_wShowV1qifCfg},
{"SetV1qifCfg", CGI_wSetV1qifCfg},
{"ShowRateCfg", CGI_wShowRateCfg},
{"SetRateCfg", CGI_wSetRateCfg},
{"ShowACLCfg", CGI_wShowACLCfg},
{"SetACLCfg", CGI_wSetACLCfg},
{"ShowSMACFltCfg", CGI_wShowSMACFltCfg},
{"SetSMACFltCfg", CGI_wSetSMACFltCfg},
{"ShowQosCfg", CGI_wShowQosCfg},
{"SetQosCfg", CGI_wSetQosCfg},
{"ShowMibCnt", CGI_wShowMibCnt},
{"SetMibCntCfg", CGI_wSetMibCntCfg},
{"ShowSnfCfg", CGI_wShowSnfCfg},
{"SetSnfCfg", CGI_wSetSnfCfg},
{"ShowMiscCfg", CGI_wShowMiscCfg},
{"SetMiscCfg", CGI_wSetMiscCfg},
{"ShowAdmCfg", CGI_wShowAdmCfg},
{"SetAdmCfg", CGI_wSetAdmCfg},
{"ShowIpCfg", CGI_wShowIpCfg},
{"SetIpCfg", CGI_wSetIpCfg},
{"ShowFwUpdt", CGI_wShowFwUpdt},
{"SetFwUpdt", CGI_wSetFwUpdt},
{"WriteDefault", CGI_wWriteDefault},
{"Reboot", CGI_wReboot},
{"ShowSysInfo", CGI_wShowSysInfo},
{"ShowSTPCfg", CGI_wShowSTPCfg},
{"SetSTPCfg", CGI_wSetSTPCfg},
{"ShowIGMPCfg", CGI_wShowIGMPCfg},
{"ShowIGMPGrp", CGI_wShowIGMPGrp},
{"SetIGMPCfg", CGI_wSetIGMPCfg},
{"SetIGMPGrp", CGI_wSetIGMPGrp},
{"ShowSMacCfg", CGI_wShowSMacCfg},
{"ShowSMacGrp", CGI_wShowSMacGrp},
{"SetSMacCfg", CGI_wSetSMacCfg},
{"SetSMacGrp", CGI_wSetSMacGrp},
{"ShowGMRPCfg", CGI_wShowGMRPCfg},
{"ShowGMRPGrp", CGI_wShowGMRPGrp},
{"SetGMRPCfg", CGI_wSetGMRPCfg},
{"SetGMRPGrp", CGI_wSetGMRPGrp},
{"Show8021XCfg", CGI_wShow8021XCfg},
{"Set8021XCfg", CGI_wSet8021XCfg},
{"ShowPrvMiscCfg", CGI_wShowPrvMiscCfg},
{"SetPrvMiscCfg", CGI_wSetPrvMiscCfg},
{0, CGI_wShowMain} // this should be the last entry for ending mark
};
/*--------------------- Static Functions --------------------------*/
/*--------------------- Export Variables --------------------------*/
// page config buffer, public for SSI and CGI
UConfigBuf WEBg_UCfgBuf;
SWEBCgiInfo WEBg_SCgiInfo;
BOOL WEBg_bLoginEn;
char g_szHtmlOkHeader[] =
"HTTP/1.1 200 OK\r\nContent-type: text/html\r\nCache-Control: no-cache\r\n\r\n"; //Pragma: no-cache
void WEB_vReboot (void)
{
// reboot
SWSYS_vBoardShutdown();
SWSYS_vBoardReboot();
}
static void s_vParsingCgiInfo(PSTR pchar)
{
WEBg_SCgiInfo.u16ParaNum = 0;
while (*pchar != ' ' /*&& *pchar != '\r' && *pchar != '\n'*/)
{
if (*pchar == '=')
{
WEBg_SCgiInfo.apszParaVal[WEBg_SCgiInfo.u16ParaNum++] = ++pchar; // Directly point parameter string at inpkt array
// add end of string
while (*pchar != '&' && *pchar != ' ')
pchar++;
*pchar = '\0';
}
pchar++;
}
}
// For the purpose of that only one can login at the same time, all webpages
// should be authenticated by an unique key with time factor that generated
// by server before response to browser. The browser should also send a
// request with that unique key. In order to implement that, I use the property
// of "relative path". That is, the browser will auto add the pathname to the
// filename for whole URL. Therefore I replace the pathname by an unique key
// generated by server, and all filenames in webpages are relative. The browser
// will auto add the unique key to the request URL. All the server should do is
// to generate and maintain the unique key, to clear the key after logout or
// timeout.
// PS: For this purpose, the server does not support html file system and SSI,
// all html header and content are generated by CGI functions.
UINT16 WEB_wProcHttpRequest(PUINT8 abyInBuf)
{
UINT8 si, byFilenamePtr;
// check login: "GET /LoginUID/filename?A=a&B=b HTTP/1.1\r\n"
if (!WEBg_bLoginEn) {
byFilenamePtr = 5;
}
// have logined
else if (g_aszWebLoginID[0]) {
if (STR_iMemcmp(abyInBuf + 4, g_aszWebLoginID, 10) != 0)
return WEB_wGenResultMsgPkt("Only one can login at the same time");
byFilenamePtr = 14;
}
// not login yet
else {
if (STR_iMemcmp(abyInBuf + 4, "/Login?", 7) == 0) {
s_vParsingCgiInfo((PSTR)abyInBuf + 11);
return CGI_wLogin();
}
else {
return CGI_wShowLogin();
}
}
// if "GET /LoginUID/ HTTP/1.1"
if (abyInBuf[byFilenamePtr] == ' ')
return CGI_wShowMain();
// Find the CGI filename
for (si = byFilenamePtr; si < 0xFF; si++) {
if (abyInBuf[si] == ' ' || abyInBuf[si] == '?') { /* || abyInBuf[si] == '\r' || abyInBuf[si] == '\n'*/
abyInBuf[si] = 0; // replace EOS
break;
}
}
// Search cgi table
for (si = 0; s_aSCgiTbl[si].szName; si++) {
if (STR_iStrcmp(s_aSCgiTbl[si].szName, (PSTR)abyInBuf + byFilenamePtr) == 0 ) // if found
{
// reload web auto logout counter
g_wWebAutoLogoutCntr = 0;
// parsing cgi parameter
s_vParsingCgiInfo((PSTR)abyInBuf + STR_iStrlen((PSTR)abyInBuf) + 1);
// run cgi
return s_aSCgiTbl[si].pfnCgi();
}
}
// Search static html files
for (si = 0; g_SHtmlFileMap[si].szName; si++) {
if (STR_iStrcmp(g_SHtmlFileMap[si].szName, (PSTR)abyInBuf + byFilenamePtr) == 0 ) // if found
{
// reload web auto logout counter
g_wWebAutoLogoutCntr = 0;
STR_pszStrcpy(HTTPg_acOutBuf, "HTTP/1.1 200 OK\r\n\r\n");
STR_pvMemcpy(HTTPg_acOutBuf + 19, g_SHtmlFileMap[si].abyData, g_SHtmlFileMap[si].wSize);
return 19 + g_SHtmlFileMap[si].wSize;
}
}
// if not found, return 404
return STR_iStrcpy(HTTPg_acOutBuf, "HTTP/1.1 404 OK\r\n\r\n");
}
UINT16 WEB_wGenResultMsgPkt(char* szMsg)
{
char* psz = HTTPg_acOutBuf;
psz += STR_iStrcpy(psz, g_szHtmlOkHeader);
psz += STR_iStrcat(psz, "<CENTER><H2>");
psz += STR_iStrcat(psz, szMsg);
psz += STR_iStrcat(psz, "</H2><A HREF=javascript:history.back()>< Back</A>");
return (psz - HTTPg_acOutBuf);
}
UINT16 WEB_wGenRedirectPkt(char* szFilename)
{
char* psz = HTTPg_acOutBuf;
psz += STR_iStrcpy(psz, "HTTP/1.1 307 OK\r\nLocation: ");
psz += STR_iStrcat(psz, szFilename);
psz += STR_iStrcat(psz, "\r\n\r\n");
return (psz - HTTPg_acOutBuf);
}
UINT16 WEB_wPopupMsgBoxThenRedirect(char* szMsg, char* szUrl)
{
char* psz = HTTPg_acOutBuf;
psz += STR_iStrcpy(psz, g_szHtmlOkHeader);
psz += STR_iStrcat(psz, "<SCRIPT>alert(\"");
psz += STR_iStrcat(psz, szMsg);
psz += STR_iStrcat(psz, "\");top.location=\"");
psz += STR_iStrcat(psz, szUrl);
psz += STR_iStrcat(psz, "\";</SCRIPT>");
return (psz - HTTPg_acOutBuf);
}
// generate an unique key: MAC[2-6] ^ IP ^ time
void WEB_vGenUniqueLoginID(void)
{
UINT8 si;
UINT32 dwVal;
// dwVal = source ip address
dwVal = g_u32MyIpAddr;
// hash timer ticks
for (si = 0; si < 4; si++)
((UINT8*)&dwVal)[si] ^= (UINT8)TMR_u32GetSysTick();
// protect string data structure from being accessed before it's properly set
INTR_vCriticalSectionEnter();
STR_iStrcpy(g_aszWebLoginID, "/");
STR_iU32ToStrHexPad(g_aszWebLoginID + 1, dwVal, 8);
STR_iStrcat(g_aszWebLoginID, "/");
INTR_vCriticalSectionLeave();
// reload web auto logout counter
g_wWebAutoLogoutCntr = 0;
}
// Output <SELECT> tag for listbox option
UINT16 WEB_wAddSelectTag(PSTR pszOutBuf, char cName, UINT8 byName, char** aszStringTable, UINT8 byStringNum, UINT8 bySelected, BOOL bIsAddTD, char* szExtraAttrib)
{
UINT8 si;
char* psz = pszOutBuf;
psz += STR_iStrcpy(psz, (bIsAddTD ? "<TD>" : ""));
psz += STR_iStrcat(psz, "<SELECT name=");
STR_pszStrcatchr(psz, cName);
psz++;
psz += STR_iU32ToStrDec(psz, byName);
psz += STR_iStrcat(psz, ((szExtraAttrib != NULL) ? szExtraAttrib : ""));
psz += STR_iStrcat(psz, ">");
// output all <option> tag
for (si = 0; si < byStringNum; si++) {
psz += STR_iStrcat(psz, "<OPTION value=");
psz += STR_iU32ToStrDec(psz, si);
psz += STR_iStrcat(psz, ((si == bySelected) ? " selected" : ""));
psz += STR_iStrcat(psz, ">");
psz += STR_iStrcat(psz, aszStringTable[si]);
}
psz += STR_iStrcat(psz, "</SELECT>");
return (psz - pszOutBuf);
}
// Output <INPUT type=checkbox> tag for multiple-checked option
UINT16 WEB_wAddCheckboxTag(PSTR pszOutBuf, UINT8 byName, UINT8 byValue, BOOL bChecked)
{
char* psz = pszOutBuf;
psz += STR_iStrcpy(psz, "<INPUT type=checkbox name=X");
psz += STR_iU32ToStrDec(psz, byName);
psz += STR_iStrcat(psz, " value=");
psz += STR_iU32ToStrDec(psz, byValue);
psz += STR_iStrcat(psz, ((bChecked) ? " checked" : ""));
psz += STR_iStrcat(psz, ">");
return (psz - pszOutBuf);
}
// Output <INPUT type=radio> tag for only-one-checked option
UINT16 WEB_wAddRadioTag(PSTR pszOutBuf, UINT8 byName, UINT8 byValue, BOOL bChecked)
{
char* psz = pszOutBuf;
psz += STR_iStrcpy(psz, "<TD><INPUT type=radio name=R");
psz += STR_iU32ToStrDec(psz, byName);
psz += STR_iStrcat(psz, " value=");
psz += STR_iU32ToStrDec(psz, byValue);
psz += STR_iStrcat(psz, ((bChecked) ? " checked" : ""));
psz += STR_iStrcat(psz, ">");
return (psz - pszOutBuf);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?