networking.c
资源名称:cgi-web.rar [点击查看]
上传用户:sxdewei
上传日期:2014-06-02
资源大小:67k
文件大小:15k
源码类别:
嵌入式Linux
开发平台:
Unix_Linux
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <getopt.h>
- #include <errno.h>
- #include <sys/ioctl.h>
- #include <linux/config.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include "cgivars.h"
- #include "htmllib.h"
- #include "config_phase.h"
- #include "sysconf.h"
- #define DEBUG 0
- #define ID_GAPFALG 26
- #define ID_IP 27
- #define ID_SUBMASK 28
- #define ID_GAWADD 29
- #define ID_GADNSFALG 30
- #define ID_DNSADD1 31
- #define ID_DNSADD2 32
- #define ID_WANMD 33
- #define ID_PPOEFLAG 34
- #define ID_PPOEUSR 35
- #define ID_PPOEUPW 36
- #define ID_PPOESER 37
- #define ID_UPNPFLAG 38
- static config_item ** items;
- static int config_list[] = {26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38};
- static char *config_name[] = {"GAPFALG", "IP", "SUBMASK", "GAWADD", "GADNSFALG", "DNSADD1", "DNSADD2", "WANMD", "PPOEFLAG",
- "PPOEUSR","PPOEUPW","PPOESER","UPNPFLAG"};
- #define IP_LEN 16
- void netset(char **postvars, int form_method)
- {
- int config_num = sizeof(config_list)/sizeof(int);
- int i;
- //int r_fd, w_fd;
- int ObtainIP_method, temp_int;
- char *GAPFALG;
- char *IP;
- char *SUBMASK;
- char *GAWADD;
- char *GADNSFALG;
- char *DNSADD1;
- char *DNSADD2;
- char *WANMD;
- char *PPOEFLAG;
- char *PPOEUSR;
- char *PPOEUPW;
- char *PPOESER;
- char *UPNPFLAG;
- char java[] ="
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">rn
- <HTML><HEAD>
- <META http-equiv=Pragma content=no-cache>
- <META http-equiv=Cache-Control content="No-cache,must revalidate">
- <META http-equiv=Content-Type content="text/html; charset=gb2312">
- <SCRIPT language=JavaScript src="js/javascript.js"></SCRIPT>
- <STYLE type=text/css>BODY {
- FONT-SIZE: 10pt; FONT-FAMILY: Arial
- }
- TD {
- FONT-SIZE: 10pt; FONT-FAMILY: Arial
- }
- FORM {
- FONT-SIZE: 10pt; FONT-FAMILY: Arial
- }
- OPTION {
- FONT-SIZE: 10pt; FONT-FAMILY: Arial
- }
- TEXTAREA {
- FONT-SIZE: 10pt; FONT-FAMILY: Arial
- }
- P {
- FONT-SIZE: 10pt; FONT-FAMILY: Arial
- }
- OL {
- FONT-SIZE: 10pt; FONT-FAMILY: Arial
- }
- INPUT {
- FONT-SIZE: 10pt; FONT-FAMILY: Arial
- }
- SELECT {
- FONT-SIZE: 10pt; FONT-FAMILY: Arial
- }
- .style1 {
- FONT-WEIGHT: bold; FONT-SIZE: 14pt
- }
- </STYLE>
- <META content="MSHTML 6.00.6000.16414" name=GENERATOR></HEAD>";
- //r_fd = open_read_fifo();
- items = init_config_item(items, config_num, config_list);
- printf("%sn", java);
- // If we got post, write it into flash
- if(form_method == POST)
- {
- GetConfValues(config_list, items, config_num);
- if(SaveConfig(postvars, items, config_list, config_name, config_num))
- {/*
- w_fd = open_write_fifo();
- notify_config(w_fd);
- close_write_fifo(w_fd);
- read_fifo(r_fd);
- */
- }
- }
- GetConfValues(config_list, items, config_num);
- printf("<BODY onload="dhcp_enable(document.networking.GAPFALG.value);dns_auto(document.networking.GADNSFALG.value);" leftMargin=30 topMargin=24 rightMargin=30>n");
- //printf("<BODY leftMargin=30 topMargin=24 rightMargin=30>n");
- printf("<P class=style1>网络设置</P>n");
- printf("<FORM name=networking action=/networking.cgi method=post target=msgFrame>n"); //
- printf("<INPUT type=hidden value=write name=action> <INPUT type=hidden value=tcpip name=cfg_content>n");
- printf("<INPUT type=hidden value=chs name=lang>n"); //
- printf("<TABLE width="100%"><TBODY><TR><TD>n"); //
- printf("<TABLE style="BORDER-COLLAPSE: collapse" borderColor=#dae3eb cellSpacing=0 cellPadding=0 width="100%" border=1>n"); //
- printf("<TBODY><TR bgColor=#cccccc><TD colSpan=2><B>局域网IP地址</B></TD></TR>n"); //
- printf("<TR><TD noWrap width="29%">获取IP方式</TD><TD align=middle>n");
- printf("<DIV align=left><SELECT onchange=dhcp_enable(this.value) name=GAPFALG> n");
- //ID_GAPFALG
- if(GAPFALG = getdata(ID_GAPFALG, items, config_num))
- {
- ObtainIP_method = atoi(GAPFALG);
- if(ObtainIP_method == 2)
- {
- printf("<option value="1">手动</OPTION> n");
- printf("<option value="2" selected>DHCP自动设置</OPTION> n");
- }
- else
- {
- printf("<option value="1" selected>手动</OPTION> n");
- printf("<option value="2">DHCP自动设置</OPTION> n");
- }
- }
- printf("</SELECT></TD></TR>n");
- //ID_IP
- if(IP = getdata(ID_IP, items, config_num))
- {
- printf("<TR><TD width="29%">IP地址</TD><TD align=middle width="71%">n");
- printf("<DIV align=left><INPUT size=15 maxlength=15 value=%s name=IP></DIV></TD></TR>n", IP);
- }
- else
- {
- printf("<TR><TD width="29%">IP地址</TD><TD align=middle width="71%">n");
- printf("<DIV align=left><INPUT size=15 maxlength=15 name=IP></DIV></TD></TR>n");
- }
- //ID_SUBMASK
- if(SUBMASK = getdata(ID_SUBMASK, items, config_num))
- {
- printf("<TR><TD width="29%">子网掩码</TD><TD align=middle>n");
- printf("<DIV align=left><INPUT size=15 maxlength=15 value=%s name=SUBMASK></DIV></TD></TR>n", SUBMASK);
- }
- else
- {
- printf("<TR><TD width="29%">子网掩码</TD><TD align=middle>n");
- printf("<DIV align=left><INPUT size=15 maxlength=15 name=SUBMASK></DIV></TD></TR>n");
- }
- //ID_GAWADD
- if(GAWADD = getdata(ID_GAWADD, items, config_num))
- {
- printf("<TR><TD width="29%">网关地址</TD><TD align=middle>n");
- printf("<DIV align=left><INPUT size=15 maxlength=15 value=%s name=GAWADD></DIV></TD></TR>n", GAWADD);
- }
- else
- {
- printf("<TR><TD width="29%">网关地址</TD><TD align=middle>n");
- printf("<DIV align=left><INPUT size=15 maxlength=15 name=GAWADD></DIV></TD></TR>n");
- }
- printf("</TBODY></TABLE>n");
- //ID_WANMD
- printf("<TABLE style="BORDER-COLLAPSE: collapse" borderColor=#dae3eb cellSpacing=0 cellPadding=0 width="100%" border=1>n");
- printf("<TBODY><TR bgColor=#cccccc><TD colSpan=2><B>WAN口设置</B></TD></TR>n");
- printf("<tr><TD noWrap width="29%">WAN口连接方式</TD>n");
- printf("<TD align=middle><DIV align=left><SELECT name=WANMD>n");
- if(WANMD = getdata(ID_WANMD, items, config_num))
- {
- temp_int = atoi(WANMD);
- if(1 == temp_int)
- {
- printf("<OPTION value="1" selected>PPPoE</OPTION>n");
- printf("<OPTION value="2">Gateway</OPTION>n");
- }
- else
- {
- printf("<OPTION value="1">PPPoE</OPTION>n");
- printf("<OPTION value="2" selected>Gateway</OPTION>n");
- }
- }
- printf("</SELECT> </DIV></TD></tr></TBODY></TABLE>n");
- //GADNSFALG
- printf("<TABLE style="BORDER-COLLAPSE: collapse" borderColor=#dae3eb cellSpacing=0 cellPadding=0 width="100%" border=1>n");
- printf("<TBODY><TR bgColor=#cccccc><TD colSpan=2><B>DNS服务器IP地址</B></TD></TR>n");
- printf("<tr><TD noWrap width="29%">DNS获取方式</TD>n");
- printf("<TD align=middle width="71%"><DIV align=left><SELECT name=GADNSFALG onchange=dns_auto(this.value)>n");
- if(GADNSFALG = getdata(ID_GADNSFALG, items, config_num))
- {
- temp_int = atoi(GADNSFALG);
- if(1 == temp_int)
- {
- printf("<OPTION value="1" selected>手动设置</OPTION>n");
- printf("<OPTION value="3">PPPoE</OPTION>n");
- }
- /*
- else if(2 == temp_int)
- {
- printf("<OPTION value="1">手动设置</OPTION>n");
- printf("<OPTION value="2" selected>DHCP</OPTION>n");
- printf("<OPTION value="3">PPPoE</OPTION>n");
- }*/
- else
- {
- printf("<OPTION value="1">手动设置</OPTION>n");
- printf("<OPTION value="3" selected>PPPoE</OPTION>n");
- }
- }
- printf("</SELECT> </DIV></TD></tr>n");
- //DNSADD1
- printf("<tr><TD noWrap width="29%">主要DNS服务器IP地址</TD><TD align=middle width="71%">n");
- if(DNSADD1 = getdata(ID_DNSADD1, items, config_num))
- printf("<DIV align=left><INPUT size=15 maxlength=15 value=%s name=DNSADD1></DIV></TD></TR>n", DNSADD1);
- else
- printf("<DIV align=left><INPUT size=15 maxlength=15 name=DNSADD1></DIV></TD></TR>n");
- //DNSADD2
- printf("<TR><TD noWrap>次要DNS服务器IP地址</TD><TD align=middle>n");
- if(DNSADD2 = getdata(ID_DNSADD2, items, config_num))
- printf("<DIV align=left><INPUT size=15 maxlength=15 name=DNSADD2 value=%s></DIV></TD></TR>n", DNSADD2);
- else
- printf("<DIV align=left><INPUT size=15 maxlength=15 name=DNSADD2></DIV></TD></TR>n");
- printf("</TBODY></TABLE>n");
- //PPOEFLAG
- printf("<TABLE style="BORDER-COLLAPSE: collapse" borderColor=#dae3eb cellSpacing=0 cellPadding=0 width="100%" border=1>n");
- printf("<TBODY><TR bgColor=#cccccc><TD colSpan=2 height=16><B>PPPoE</B></TD></TR>n");
- printf("<tr><TD noWrap width="29%">网络连线模式</TD>n");
- printf("<TD align=middle width="71%"><DIV align=left><SELECT name=PPOEFLAG>n");
- if(PPOEFLAG = getdata(ID_PPOEFLAG, items, config_num))
- {
- temp_int = atoi(PPOEFLAG);
- if(1 == temp_int)
- {
- printf("<OPTION value="1" selected>启用PPPoE</OPTION>n");
- printf("<OPTION value="0">停用PPPoE</OPTION>n");
- }
- else
- {
- printf("<OPTION value="1">启用PPPoE</OPTION>n");
- printf("<OPTION value="0" selected>停用PPPoE</OPTION>n");
- }
- }
- printf("</SELECT> </DIV></TD></tr>n");
- //PPOEUSR
- printf("<tr><TD noWrap width="29%">登录名称</TD><TD align=middle>n");
- if(PPOEUSR = getdata(ID_PPOEUSR, items, config_num))
- printf("<DIV align=left><INPUT size=32 maxlength=32 name=PPOEUSR value=%s ></DIV></TD></TR>n", PPOEUSR);
- else
- printf("<DIV align=left><INPUT size=32 maxlength=32 name=PPOEUSR></DIV></TD></TR>n");
- //PPOEUPW
- printf("<tr><TD noWrap width="29%">登录口令</TD><TD align=middle>n");
- if(PPOEUPW = getdata(ID_PPOEUPW, items, config_num))
- printf("<DIV align=left><INPUT type=password size=32 maxlength=32 name=PPOEUPW value=%s ></DIV></TD></TR>n", PPOEUPW);
- else
- printf("<DIV align=left><INPUT type=password size=32 maxlength=32 name=PPOEUPW></DIV></TD></TR>n");
- //PPOESER
- printf("<tr><TD noWrap width="29%">服务名称</TD><TD align=middle>n");
- if(PPOESER = getdata(ID_PPOESER, items, config_num))
- printf("<DIV align=left><INPUT size=32 maxlength=32 value=%s name=PPOESER></DIV></TD></TR>n", PPOESER);
- else
- printf("<DIV align=left><INPUT size=32 maxlength=32 name=PPOESER></DIV></TD></TR>n");
- printf("</TBODY></TABLE>n");
- //UPNPFLAG
- printf("<TABLE style="BORDER-COLLAPSE: collapse" borderColor=#dae3eb cellSpacing=0 cellPadding=0 width="100%" border=1>n");
- printf("<TBODY><TR bgColor=#cccccc><TD colSpan=2><B>UPNP</B></TD></TR>n");
- printf("<tr><TD noWrap width="29%">UPNP启用</FONT></TD><TD align=middle width="71%">n");
- printf("<DIV align=left><SELECT size=1 name=UPNPFLAG>n");
- if(UPNPFLAG = getdata(ID_UPNPFLAG, items, config_num))
- {
- temp_int = atoi(UPNPFLAG);
- if(1 == temp_int)
- {
- printf("<OPTION value="1" selected>是</OPTION>n");
- printf("<OPTION value="0">否</OPTION>n");
- }
- else
- {
- printf("<OPTION value="1">是</OPTION>n");
- printf("<OPTION value="0" selected>否</OPTION>n");
- }
- }
- printf("</SELECT> </DIV></TD></tr>n");
- printf("</TBODY></TABLE>n");
- printf("<BR><FONT color=#ff0000>应用更改后,系统将自动重启。</FONT> n");
- printf("<P align=left><INPUT type=submit onclick="return tsettcpip();" value=应用>n");
- printf("<INPUT type=reset value=重置>n");
- printf("<INPUT type=hidden value=chs name=lang> </P></TD></TR></TBODY></TABLE></FORM></BODY></HTML>n");
- // javascript layout
- {
- char jsp[]="
- <script language=JavaScript>
- <!--
- function dhcp_enable(dhcp)
- {
- if (dhcp=="1") {
- document.networking.IP.disabled = 0;
- document.networking.SUBMASK.disabled = 0;
- document.networking.GAWADD.disabled = 0;
- }
- else {
- document.networking.IP.disabled = 1;
- document.networking.SUBMASK.disabled = 1;
- document.networking.GAWADD.disabled = 1;
- }
- }
- function dns_auto(auto)
- {
- if(auto == "1")
- {
- document.networking.DNSADD1.disabled = 0;
- document.networking.DNSADD2.disabled = 0;
- }
- else
- {
- document.networking.DNSADD1.disabled = 1;
- document.networking.DNSADD2.disabled = 1;
- }
- }
- function checinetaddrvalid(inetaddr)
- {
- var reg=/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/
- if(arr=inetaddr.match(reg)){
- for(i=1;i<arr.length;i++)if(parseInt(arr[i])>255){return false;}
- }
- else {
- return false;
- }
- return true;
- }
- function settcpip()
- {
- var element ;
- element = document.getElementById('IP');
- if(element.value == "" && document.networking.IP.disabled == 0)
- {
- alert("IP地址不能为空!");
- element.focus();
- return false;
- }
- if(checinetaddrvalid(element.value) == false && document.networking.IP.disabled == 0)
- {
- alert("IP地址格式错误!");
- element.focus();
- return false;
- }
- element = document.getElementById('SUBMASK');
- if(element.value == "" && document.networking.SUBMASK.disabled == 0)
- {
- alert("子网掩码不能为空!");
- element.focus();
- return false;
- }
- if(checinetaddrvalid(element.value) == false && document.networking.SUBMASK.disabled == 0)
- {
- alert("子网掩码地址格式错误!");
- element.focus();
- return false;
- }
- element = document.getElementById('GAWADD');
- if(element.value == "" && document.networking.GAWADD.disabled == 0)
- {
- alert("网关地址不能为空!");
- element.focus();
- return false;
- }
- if(checinetaddrvalid(element.value) == false && document.networking.GAWADD.disabled == 0)
- {
- alert("网关地址格式错误!");
- element.focus();
- return false;
- }
- element = document.getElementById('DNSADD1');
- if(element.value == "" && document.networking.DNSADD1.disabled == 0)
- {
- alert("主DNS服务器IP地址不能为空!");
- element.focus();
- return false;
- }
- if(checinetaddrvalid(element.value) == false && document.networking.DNSADD1.disabled == 0)
- {
- alert("主DNS服务器IP地址格式错误!");
- element.focus();
- return false;
- }
- element = document.getElementById('DNSADD2');
- if(element.value == "" && document.networking.DNSADD2.disabled == 0)
- {
- alert("次要DNS服务器IP地址不能为空!");
- element.focus();
- return false;
- }
- if(checinetaddrvalid(element.value) == false && document.networking.DNSADD2.disabled == 0)
- {
- alert("次要DNS服务器IP地址格式错误!");
- element.focus();
- return false;
- }
- return true;
- }
- function tsettcpip()
- {
- return settcpip();
- }
- //-->
- </script>";
- printf("%sn", jsp);
- }
- free_config_item(items, config_num);
- //close_read_fifo();
- }
- ////////////////////////////////////////////////////////////////////////////
- // Function: main
- ////////////////////////////////////////////////////////////////////////////
- int main(void)
- {
- char **postvars = NULL; // POST request data repository
- char **getvars = NULL; // GET request data repository
- int form_method; // POST = 1, GET = 0
- // CGI part start here
- adminCheck();
- form_method = getRequestMethod();
- if(form_method == POST)
- {
- getvars = getGETvars();
- postvars = getPOSTvars();
- }
- else if(form_method == GET)
- {
- getvars = getGETvars();
- }
- // CGI start here, set COM port
- netset(postvars, form_method);
- //htmlFooter();
- cleanUp(form_method, getvars, postvars);
- fflush(stdout);
- exit(0);
- } // end of main