#include <iostream>
#include <fstream> //读写文件 c++标准库
#include <string> //字符串类 c++标准库
#include <sstream> //字符串流 c++标准库
#include <assert.h>
#include "json/json.h" //jsoncpp的头文件
#include <cgicc/CgiDefs.h>
#include <cgicc/Cgicc.h>
#include <cgicc/HTTPHTMLHeader.h>
#include <cgicc/HTMLClasses.h>
using namespace cgicc;
using namespace std;
#define DEBUG false
/********************* 测试模块 ********************/
void TestMode()
{
Cgicc formData; //加载Cgicc,获取Cgicc对象
//获取 input标签 值
const vector<cgicc::FormEntry>& form_entries = formData.getElements(); //getElements()函数,获取该from表单中全部 input标签
cout << "form_entries.size()=" << form_entries.size() << endl;
for (vector<cgicc::FormEntry>::size_type i=0; i<form_entries.size(); ++i)
{
const cgicc::FormEntry& form_entry = form_entries[i];
//打印每个 input标签 中 name属性 和 value属性值
cout << "getName=" << form_entry.getName() << "; getValue=" << form_entry.getValue() << endl;
}
/*
string name = formData("single_ip_type_ip"); //根据id获取其value
cout << "name=" << name << endl;
form_iterator fi = formData.getElement("RadioGroup_Eth_Select"); //根据相同的name属性获取全部的radio对象
if(!fi->isEmpty() && fi != (*formData).end())
cout << "select eth:" << **fi << endl;
else
cout << "no select." << endl;
*/
}
/********************* 写json文件 ********************/
/* 判断是否成功打开json文件 */
bool isReadJsonFileOk(const char *jsonFileName,Json::Value *jsonInfo)
{
ifstream jsonFile(jsonFileName,ios::binary);
if(!jsonFile.is_open())
{
#if (DEBUG == true)
cout << "OPEN " << jsonFileName << "ERROR" << endl;
#endif
return false;
}
else
{
Json::Reader reader;
if(reader.parse(jsonFile,*jsonInfo))
{
jsonFile.close();
return true;
}
else
{
#if (DEBUG == true)
cout << "parse json ERROR." << endl;
#endif
jsonFile.close();
return false;
}
}
return true;
}
/* 判断是否成功更新json文件 */
bool isWriteJsonFileOK(const char *jsonFileName,const Json::Value writeJson)
{
ofstream jsonFile(jsonFileName,ios::out);
if(!jsonFile.is_open())
{
#if (DEBUG == true)
cout << "OPEN " << jsonFileName << " ERROR." << endl;
#endif
return false;
}
else
{
Json::StyledWriter sw;
jsonFile << sw.write(writeJson);
jsonFile.close();
return true;
}
return true;
}
/*
* 函数名: WriteSingleIpToJson: 将单ip配置写入json
* 参数:
* jsonFileName: json文件
* select_eth_type: 网口类型选择
* ip: IP地址
* subnet_mask: 子网掩码
* gateway: 默认网关
*/
void WriteSingleIpToJson(const char *jsonFileName, int select_eth_type, string ip, string subnet_mask, string gateway)
{
Json::Value jsonInfo;
if(isReadJsonFileOk(jsonFileName, &jsonInfo) != true)
{
#if (DEBUG == true)
cout << "write radio.josn file: open false." << endl;
#endif
return;
}
//更新json文件
jsonInfo["native"]["client_eth_choose"] = select_eth_type;
jsonInfo["native"]["native_ip_address"] = ip;
jsonInfo["native"]["native_subnet_mask"] = subnet_mask;
jsonInfo["native"]["native_default_gateway"] = gateway;
jsonInfo["native"]["client_type"] = 1;
isWriteJsonFileOK(jsonFileName, jsonInfo);
}
/*
* WriteVipToJson: 将虚拟IP数据写入json
* 参数:
* jsonFileName: json文件
* eth0_ip: eth0网口ip
* eth1_ip: eth1网口ip
* vip: 虚拟IP
* subnet_mask: 子网掩码
* gateway: 默认网关
*/
void WriteVipToJson(const char *jsonFileName, string eth0_ip, string eth1_ip, string vip, string subnet_mask, string gateway)
{
Json::Value jsonInfo;
if(isReadJsonFileOk(jsonFileName, &jsonInfo) != true)
{
#if (DEBUG == true)
cout << "write radio.json file: open false." << endl;
#endif
return;
}
//更新json文件
jsonInfo["native"]["native_ip_address"] = eth0_ip;
jsonInfo["native"]["native_eth1_ip_address"] = eth1_ip;
jsonInfo["native"]["native_vip_ip_address"] = vip;
jsonInfo["native"]["native_subnet_mask"] = subnet_mask;
jsonInfo["native"]["native_default_gateway"] = gateway;
jsonInfo["native"]["client_type"] = 2;
isWriteJsonFileOK(jsonFileName, jsonInfo);
}
/******************* 提交单IP类型 *******************/
void SubmitSingleIpType()
{
Cgicc formData;
string single_ip_type_ip = formData("single_ip_type_ip");
string single_ip_type_subnet_mask = formData("single_ip_type_subnet_mask");
string single_ip_type_gateway = formData("single_ip_type_gateway");
form_iterator fi = formData.getElement("RadioGroup_Eth_Select");
string select_eth_name;
int select_eth_value = 0;
if(!fi->isEmpty() && fi != (*formData).end())
select_eth_name = **fi;
#if (DEBUG == true)
cout << "[single_ip_type] select_eth_name = " << select_eth_name
<< "; ip = " << single_ip_type_ip
<< "; single_ip_type_subnet_mask = " << single_ip_type_subnet_mask
<< "; single_ip_type_gateway = " << single_ip_type_gateway
<< endl << endl;
#endif
if(select_eth_name == "eth0")
select_eth_value = 0;
else
select_eth_value = 1;
WriteSingleIpToJson("/opt/conf/radio.json", select_eth_value, single_ip_type_ip, single_ip_type_subnet_mask, single_ip_type_gateway);
}
/***************** 提交虚拟IP类型 *****************/
void SubmitVipType()
{
Cgicc formData;
string vip_type_eth0_ip = formData("vip_type_eth0_ip");
string vip_type_eth1_ip = formData("vip_type_eth1_ip");
string vip_type_vip = formData("vip_type_vip");
string vip_type_subnet_mask = formData("vip_type_subnet_mask");
string vip_type_gateway = formData("vip_type_gateway");
#if (DEBUG == true)
cout << "[vip_type] vip_type_eth0_ip = " << vip_type_eth0_ip
<< "; vip_type_eth1_ip = " << vip_type_eth1_ip
<< "; vip_type_vip = " << vip_type_vip
<< "; vip_type_subnet_mask = " << vip_type_subnet_mask
<< "; vip_type_gateway = " << vip_type_gateway
<< endl << endl;
#endif
WriteVipToJson("/opt/conf/radio.json", vip_type_eth0_ip, vip_type_eth1_ip, vip_type_vip, vip_type_subnet_mask, vip_type_gateway);
}
/******************* main函数 *********************/
int main(int argc, char *argv[])
{
cout << "Content-type:text/html;charset=utf-8\r\n\r\n";
#if (DEBUG == true)
if(argc > 1){
for(int i = 0; i < argc; i++){
cout << "TIME SYN: argv[" << i << "]=" << argv[i] << endl;
}
}
TestMode();
#endif
string submit_single_ip_type = "single_ip_type"; //提交"单IP"表单数据
string submit_vip_type = "vip_type"; //提交"虚拟IP"表单数据
if(argc > 1 && argv[1] == submit_single_ip_type){
SubmitSingleIpType();
}
else if(argc > 1 && argv[1] == submit_vip_type){
SubmitVipType();
}
#if (DEBUG == false)
//cout << "<meta http-equiv=\"refresh\" content=\"0;url=../sjtb.html\">";//刷新当前页面
cout << "<script>window.location.href = '../../wlsz.html';</script>\n";
#endif
return 0;
}
web通过 submit 提交form形式执行cgi程序。
cgicc的安装和使用,参考:Cgicc搭建交叉编译环境(移植到arm)-CSDN博客
jsoncpp的安装和使用,参考:CentOS7编译jsoncpp静态库_jsoncpp-master生成静态库-CSDN博客