Add aaa-12 添加dhcp模块
SOL 添加的dhcp模块 修改人:wuqihy 检视人:yinbin Signed-off-by: wuqihy <wuqihy@cmhi.chinamobile.com>
This commit is contained in:
parent
e897f3214d
commit
f80ec7665e
|
@ -26,6 +26,8 @@
|
|||
/*nat config */
|
||||
#define NAT_CONFIG_MODULE 0x00000008
|
||||
|
||||
/*DHCP CONFIG*/
|
||||
#define DHCP_CONFIG_MODULE 0x00000006
|
||||
/************************* 模块定义结束 **********************/
|
||||
|
||||
/************************ config id定义 **********************/
|
||||
|
@ -51,6 +53,14 @@
|
|||
|
||||
#define NAT4_CONFIG (uint64)((uint64)NAT_CONFIG_MODULE<<32|1)
|
||||
|
||||
#define DHCP_SUBNET_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|1)
|
||||
#define DHCP_HOST_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|2)
|
||||
#define DHCP_SHARED_NETWORK_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|3)
|
||||
#define DHCP_RELAY_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|4)
|
||||
#define DHCP_CLIENT_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|5)
|
||||
#define DHCP_DHCPD_LEASE (uint64)((uint64)DHCP_CONFIG_MODULE<<32|6)
|
||||
|
||||
|
||||
/************************ config id定义 end**********************/
|
||||
|
||||
#endif
|
||||
|
|
|
@ -36,8 +36,8 @@ COMMON_SRCS = configserver.c \
|
|||
web_config/authfree.c web_config/auth_parameters.c\
|
||||
user_manager_config/user_group_config.c user_manager_config/user_account_config.c user_manager_config/usermanager-server/array_index.c \
|
||||
user_manager_config/usermanager-server/user_group.c user_manager_config/usermanager-server/user_mod.c user_manager_config/usermanager-server/user.c \
|
||||
log_config/log_config_console.c log_config/log_config_init.c log_config/log_config_cm.c log_config/log_config_monitor.c log_config/log_config_remote.c log_config/log_config_file.c
|
||||
|
||||
log_config/log_config_console.c log_config/log_config_init.c log_config/log_config_cm.c log_config/log_config_monitor.c log_config/log_config_remote.c log_config/log_config_file.c\
|
||||
dhcp_config/dhcp_client_config.c dhcp_config/dhcp_dhcpd_lease.c dhcp_config/dhcp_host_config.c dhcp_config/dhcp_lib.c dhcp_config/dhcp_relay_config.c dhcp_config/dhcp_shared_network_config.c dhcp_config/dhcp_subnet_config.c\
|
||||
# MRS Board Source Files
|
||||
PLAT_LINUX_SRCS = $(COMMON_SRCS)
|
||||
PLAT_ARM64_SRCS = $(COMMON_SRCS)
|
||||
|
|
|
@ -0,0 +1,209 @@
|
|||
#include "dhcp_client_config.h"
|
||||
|
||||
ret_code dhcp_client_config_chk(uint source, uint *config_type,
|
||||
pointer input, int *input_len,
|
||||
pointer output, int *output_len)
|
||||
{
|
||||
ret_code ret = RET_OK;
|
||||
cJSON *root=NULL, *operate=NULL;
|
||||
root = cJSON_Parse(input);
|
||||
if(NULL == root)
|
||||
{
|
||||
ret = RET_INPUTERR;
|
||||
return ret;
|
||||
}
|
||||
operate = cJSON_GetObjectItem(root, "operate");
|
||||
if(operate){
|
||||
*config_type = CM_CONFIG_SET;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
ret_code dhcp_client_config_proc(uint source, uint config_type,
|
||||
pointer input, int input_len,
|
||||
pointer output, int *output_len)
|
||||
{
|
||||
ret_code ret = RET_OK;
|
||||
int i, count;
|
||||
cJSON *root=NULL, *operate=NULL, *interfaces=NULL;
|
||||
|
||||
int len = 50;
|
||||
char *cmd = (char *)malloc(len + 1);
|
||||
if(NULL == cmd)
|
||||
{
|
||||
return RET_ERR;
|
||||
}
|
||||
|
||||
|
||||
/*json解析*/
|
||||
root = cJSON_Parse(input);
|
||||
if(NULL == root)
|
||||
{
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
operate = cJSON_GetObjectItem(root, "operate");
|
||||
if(NULL == operate)
|
||||
{
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
interfaces = cJSON_GetObjectItem(root, "interfaces");
|
||||
if(NULL == interfaces)
|
||||
{
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
|
||||
count = cJSON_GetArraySize(interfaces);
|
||||
if(!strcmp(operate->valuestring, "obtain ip")){
|
||||
for(i = 0; i < count; i++)
|
||||
{
|
||||
cJSON *one = cJSON_GetArrayItem(interfaces, i);
|
||||
if(NULL == one)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
cJSON *name = cJSON_GetObjectItem(one, "name");
|
||||
if(NULL == name)
|
||||
{
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
memset(cmd, 0, len + 1);
|
||||
snprintf(cmd, len, "dhclient %s", name->valuestring);
|
||||
system(cmd);
|
||||
//system("dhclient %s", name->valuestring);
|
||||
}
|
||||
}
|
||||
if(!strcmp(operate->valuestring, "lease ip")){
|
||||
for(i = 0; i < count; i++)
|
||||
{
|
||||
cJSON *one = cJSON_GetArrayItem(interfaces, i);
|
||||
if(NULL == one)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
cJSON *name = cJSON_GetObjectItem(one, "name");
|
||||
if(NULL == name)
|
||||
{
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
memset(cmd, 0, len + 1);
|
||||
snprintf(cmd, len, "dhclient -r %s", name->valuestring);
|
||||
system(cmd);
|
||||
//system("dhclient -r %s", name->valuestring);
|
||||
}
|
||||
}
|
||||
|
||||
INPUT_ERROR:
|
||||
if(cmd)
|
||||
{
|
||||
memset(cmd, 0, len + 1);
|
||||
free(cmd);
|
||||
|
||||
}
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
ret_code dhcp_client_get(uint source,
|
||||
pointer input, int input_len,
|
||||
pointer output, int *output_len)
|
||||
{
|
||||
ret_code ret = RET_OK;
|
||||
cJSON *root=NULL, *interface=NULL;
|
||||
/*json解析*/
|
||||
root = cJSON_Parse(input);
|
||||
if(NULL == root)
|
||||
{
|
||||
return RET_INPUTERR;
|
||||
}
|
||||
interface = cJSON_GetObjectItem(root, "interface");
|
||||
if(NULL == interface)
|
||||
{
|
||||
return RET_INPUTERR;
|
||||
}
|
||||
|
||||
//读取配置文件root@ubuntu:/var/lib/dhcp# cat dhclient.leases 最后一个
|
||||
char *fname="/var/lib/dhcp/dhclient.leases";
|
||||
FILE *fp;
|
||||
char *lease_interface, *ip, *router;
|
||||
cJSON *json = NULL;
|
||||
char *out = NULL;
|
||||
char line[1000];
|
||||
if ((fp=fopen(fname,"r"))==NULL){
|
||||
printf("打开文件%s错误\n",fname);
|
||||
return RET_ERR;
|
||||
}
|
||||
fseek(fp,0,SEEK_END);
|
||||
rewind(fp);
|
||||
|
||||
lease_interface = (char *)malloc(20);
|
||||
memset(lease_interface, 0, 20);
|
||||
ip = (char *)malloc(20);
|
||||
memset(ip, 0, 20);
|
||||
router = (char *)malloc(20);
|
||||
memset(router, 0, 20);
|
||||
root = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(root, "interface", interface->valuestring);
|
||||
|
||||
|
||||
while((fgets(line,1000,fp))!=NULL){
|
||||
char *startINTER = strstr(line, "interface");
|
||||
char *endINTER = strstr(line, ";");
|
||||
if((startINTER!=NULL) && (endINTER!=NULL)){
|
||||
memcpy(lease_interface,startINTER+11,endINTER-startINTER-12);
|
||||
if(!strcmp(interface->valuestring, lease_interface)){
|
||||
memset(ip, 0, 20);
|
||||
memset(router, 0, 20);
|
||||
}
|
||||
//printf("ip: %s\n", ip);
|
||||
//json = cJSON_CreateObject();
|
||||
//cJSON_AddStringToObject(json , "ip", ip);
|
||||
}
|
||||
if(lease_interface != NULL){
|
||||
//不相等
|
||||
if(strcmp(interface->valuestring, lease_interface)){
|
||||
continue;
|
||||
}
|
||||
}
|
||||
char *startIP = strstr(line, "fixed-address");
|
||||
char *endIP = strstr(line, ";");
|
||||
if((startIP!=NULL) && (endIP!=NULL)){
|
||||
memcpy(ip,startIP+14,endIP-startIP-14);
|
||||
//printf("ip: %s\n", ip);
|
||||
//json = cJSON_CreateObject();
|
||||
//cJSON_AddStringToObject(json , "ip", ip);
|
||||
}
|
||||
char *startROUTER = strstr(line, "option routers");
|
||||
char *endROUTER = strstr(line, ";");
|
||||
if((startROUTER!=NULL) && (endROUTER!=NULL)){
|
||||
memcpy(router,startROUTER+15,endROUTER-startROUTER-15);
|
||||
//printf("mac: %s\n", mac);
|
||||
//cJSON_AddStringToObject(json , "mac", mac);
|
||||
}
|
||||
}
|
||||
if(ip != NULL){
|
||||
cJSON_AddStringToObject(root, "ip", ip);
|
||||
memset(ip, 0, 20);
|
||||
free(ip);
|
||||
}
|
||||
if(router != NULL){
|
||||
cJSON_AddStringToObject(root, "router", router);
|
||||
memset(router, 0, 20);
|
||||
free(router);
|
||||
}
|
||||
if(lease_interface != NULL){
|
||||
memset(lease_interface, 0, 20);
|
||||
free(lease_interface);
|
||||
}
|
||||
fclose(fp);
|
||||
out = cJSON_PrintUnformatted(root);
|
||||
*output_len = strlen(out) + 1;
|
||||
memcpy(output, out, *output_len);
|
||||
free(out);
|
||||
return ret;
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
#include "dhcp_dhcpd_lease.h"
|
||||
|
||||
ret_code dhcp_dhcpd_lease_config_chk(uint source,uint *config_type,
|
||||
pointer input, int *input_len,
|
||||
pointer output, int *output_len)
|
||||
{
|
||||
ret_code ret = RET_OK;
|
||||
*config_type = CM_CONFIG_GET_ALL;
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret_code dhcp_dhcpd_lease_get_all(uint source, pointer output, int *output_len)
|
||||
{
|
||||
//ip_config_t *ip_conf;
|
||||
ret_code ret = RET_OK;
|
||||
|
||||
|
||||
char *fname="/var/lib/dhcp/dhcpd.leases";
|
||||
FILE *fp;
|
||||
char *ip=NULL, *mac=NULL, *hname=NULL, *time=NULL;
|
||||
cJSON *root = NULL, *arr = NULL, *json = NULL;
|
||||
char *out = NULL;
|
||||
char line[1000];
|
||||
if ((fp=fopen(fname,"r"))==NULL){
|
||||
printf("´ò¿ªÎļþ%s´íÎó\n",fname);
|
||||
return RET_ERR;
|
||||
}
|
||||
fseek(fp,0,SEEK_END);
|
||||
rewind(fp);
|
||||
|
||||
ip = (char *)malloc(20);
|
||||
memset(ip, 0, 20);
|
||||
mac = (char *)malloc(20);
|
||||
memset(mac, 0, 20);
|
||||
hname = (char *)malloc(100);
|
||||
memset(hname, 0, 100);
|
||||
time = (char *)malloc(100);
|
||||
memset(time, 0, 100);
|
||||
root = cJSON_CreateObject();
|
||||
cJSON_AddItemToObject(root, "lease", arr = cJSON_CreateArray());
|
||||
|
||||
|
||||
while((fgets(line,1000,fp))!=NULL){
|
||||
char *startIP = strstr(line, "lease");
|
||||
char *endIP = strstr(line, "{");
|
||||
if((startIP!=NULL) && (endIP!=NULL)){
|
||||
memcpy(ip,startIP+6,endIP-startIP-7);
|
||||
//printf("ip: %s\n", ip);
|
||||
json = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(json , "ip", ip);
|
||||
}
|
||||
char *startMAC = strstr(line, "hardware ethernet");
|
||||
char *endMAC = strstr(line, ";");
|
||||
if((startMAC!=NULL) && (endMAC!=NULL)){
|
||||
memcpy(mac,startMAC+18,endMAC-startMAC-18);
|
||||
//printf("mac: %s\n", mac);
|
||||
cJSON_AddStringToObject(json , "mac", mac);
|
||||
}
|
||||
char *startNAME = strstr(line, "client-hostname");
|
||||
char *endNAME = strstr(line, ";");
|
||||
if((startNAME!=NULL) && (endNAME!=NULL)){
|
||||
memcpy(hname,startNAME+17,endNAME-startNAME-18);
|
||||
//printf("name: %s\n", hname);
|
||||
cJSON_AddStringToObject(json , "host-name", hname);
|
||||
}
|
||||
char *startTIME = strstr(line, "ends");
|
||||
char *endTIME = strstr(line, ";");
|
||||
if((startTIME!=NULL) && (endTIME!=NULL)){
|
||||
memcpy(time,startTIME+7,endTIME-startTIME-7);
|
||||
//printf("time: %s\n", time);
|
||||
cJSON_AddStringToObject(json , "end-time", time);
|
||||
}
|
||||
char *end = strstr(line, "}");
|
||||
if(end != NULL){
|
||||
//printf("\n");
|
||||
cJSON_AddItemToArray(arr, json);
|
||||
memset(ip, 0, 20);
|
||||
memset(mac, 0, 20);
|
||||
memset(hname, 0, 100);
|
||||
memset(time, 0, 100);
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
if(ip != NULL){
|
||||
memset(ip, 0, 20);
|
||||
free(ip);
|
||||
}
|
||||
if(mac != NULL){
|
||||
memset(mac, 0, 20);
|
||||
free(mac);
|
||||
}
|
||||
if(hname != NULL){
|
||||
memset(hname, 0, 100);
|
||||
free(hname);
|
||||
}
|
||||
if(time != NULL){
|
||||
memset(time, 0, 100);
|
||||
free(time);
|
||||
}
|
||||
out = cJSON_PrintUnformatted(root);
|
||||
*output_len = strlen(out) + 1;
|
||||
memcpy(output, out, *output_len);
|
||||
free(out);
|
||||
//out = cJSON_Print(root);
|
||||
//printf("%s\n",out);
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,316 @@
|
|||
#include "dhcp_host_config.h"
|
||||
|
||||
ret_code dhcp_host_config_chk(uint source, uint *config_type,
|
||||
pointer input, int *input_len,
|
||||
pointer output, int *output_len)
|
||||
{
|
||||
ret_code ret = RET_OK;
|
||||
cJSON *root=NULL, *operate=NULL;
|
||||
root = cJSON_Parse(input);
|
||||
if(NULL == root)
|
||||
{
|
||||
return RET_INPUTERR;
|
||||
}
|
||||
operate = cJSON_GetObjectItem(root, "operate");
|
||||
if(operate)
{
|
||||
*config_type = CM_CONFIG_SET;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*input格式:
|
||||
{
|
||||
""operate" : "add",
|
||||
""host": [
|
||||
{
|
||||
"host-name" : "h1",
|
||||
"hardware-ethernet" : "00:00:00:00:00:01",
|
||||
"fixed-address" : "10.0.0.1"
|
||||
},
|
||||
{
|
||||
"host-name" : "h2",
|
||||
"hardware-ethernet" : "00:00:00:00:00:02",
|
||||
"fixed-address" : "10.0.0.2"
|
||||
},
|
||||
]
|
||||
}
|
||||
*/
|
||||
ret_code dhcp_host_config_proc(uint source, uint config_type,
|
||||
pointer input, int input_len,
|
||||
pointer output, int *output_len)
|
||||
{
|
||||
ret_code ret = RET_OK;
|
||||
int i, count;
|
||||
cJSON *root=NULL, *operate=NULL, *host=NULL;
|
||||
|
||||
/*json解析*/
|
||||
root = cJSON_Parse(input);
|
||||
if(NULL == root)
|
||||
{
|
||||
return RET_INPUTERR;
|
||||
}
|
||||
operate = cJSON_GetObjectItem(root, "operate");
|
||||
if(NULL == operate)
|
||||
{
|
||||
return RET_INPUTERR;
|
||||
}
|
||||
host = cJSON_GetObjectItem(root, "host");
|
||||
if(NULL == host)
|
||||
{
|
||||
return RET_INPUTERR;
|
||||
}
|
||||
count = cJSON_GetArraySize(host);
|
||||
int len = 500;
|
||||
char *cmd = (char *)malloc(len + 1);
|
||||
if(NULL == cmd)
|
||||
{
|
||||
return RET_ERR;
|
||||
}
|
||||
for(i = 0; i < count; i++)
|
||||
{
|
||||
cJSON *one = cJSON_GetArrayItem(host, i);
|
||||
if(NULL == one)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
cJSON *hostName = cJSON_GetObjectItem(one, "host-name");
|
||||
if(NULL == hostName)
|
||||
{
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
//check host name
|
||||
if(!check_name(hostName->valuestring)){
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
cJSON *macAddr = cJSON_GetObjectItem(one, "hardware-ethernet");
|
||||
cJSON *fixedAddr = cJSON_GetObjectItem(one, "fixed-address");
|
||||
|
||||
/*写配置文件*/
|
||||
memset(cmd, 0, len + 1);
|
||||
if(!strcmp(operate->valuestring, "add"))
|
||||
{
|
||||
if((NULL == macAddr) || (NULL == fixedAddr))
|
||||
{
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
//判断之前是否配置过这个host
|
||||
snprintf(cmd, len, "grep -Pzo '[\\s\\t]*host[\\s\\t]+%s[\\s\\t]*{[a-zA-Z#:;\\s\\t\\n0-9\\.\\-]+}' /etc/dhcp/dhcpd.conf > /tmp/subconf.conf", hostName->valuestring);
|
||||
system(cmd);
|
||||
if(NULL != getfileall("/tmp/subconf.conf")){
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
//check hardware ethernet and fixed address
|
||||
if((!check_mac(macAddr->valuestring)) || (!check_ip(fixedAddr->valuestring))){
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
//echo 'xxx' >> targetfile
|
||||
/*
|
||||
host h1 {
|
||||
hardware ethernet 08:00:07:26:c0:a5;
|
||||
fixed-address 10.0.7.2;
|
||||
}
|
||||
*/
|
||||
//echo 按照道理需要-e 这里不需要
|
||||
memset(cmd, 0, len + 1);
|
||||
snprintf(cmd, len, "echo -e 'host %s {\\n hardware ethernet %s;\\n fixed-address %s;\\n}' >> /etc/dhcp/dhcpd.conf",
|
||||
hostName->valuestring, macAddr->valuestring, fixedAddr->valuestring);
|
||||
system(cmd);
|
||||
}
|
||||
|
||||
if(!strcmp(operate->valuestring, "del"))
|
||||
{
|
||||
//判断之前是否配置过这个host
|
||||
snprintf(cmd, len, "grep -Pzo '[\\s\\t]*host[\\s\\t]+%s[\\s\\t]*{[a-zA-Z#:;\\s\\t\\n0-9\\.\\-]+}' /etc/dhcp/dhcpd.conf > /tmp/subconf.conf", hostName->valuestring);
|
||||
system(cmd);
|
||||
if(NULL == getfileall("/tmp/subconf.conf")){
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
memset(cmd, 0, len + 1);
|
||||
snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/[ \\t]*host[ \\t]+%s[ \\t]*\\{[a-zA-Z#:; \\t\\n0-9\\.\\-]+\\}/#/g' /etc/dhcp/dhcpd.conf", hostName->valuestring);
|
||||
system(cmd);
|
||||
system("sed -r -i '/#/d' /etc/dhcp/dhcpd.conf");
|
||||
//system("sed -r -i '/^#*$/d' /etc/dhcp/dhcpd.conf");
|
||||
}
|
||||
if(!strcmp(operate->valuestring, "mod"))
|
||||
{
|
||||
snprintf(cmd, len, "grep -Pzo 'host[\\s\\t]+%s[\\s\\t]*{[a-zA-Z#:;\\s\\t\\n0-9\\.\\-]+}' /etc/dhcp/dhcpd.conf > /tmp/subconf.conf", hostName->valuestring);
|
||||
system(cmd);
|
||||
if(NULL == getfileall("/tmp/subconf.conf")){
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
if(NULL != macAddr)
|
||||
{
|
||||
if(file_consist_str("/tmp/subconf.conf", macAddr->valuestring)){
|
||||
//printf("mac already exists\n");
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
//check mac
|
||||
if(!check_mac(macAddr->valuestring)){
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
memset(cmd, 0, len + 1);
|
||||
snprintf(cmd, len, "sed -r -i 's/hardware[ \\t]+ethernet[ \\ta-fA-F0-9:]+;/hardware ethernet %s;/g' /tmp/subconf.conf", macAddr->valuestring);
|
||||
system(cmd);
|
||||
}
|
||||
if(NULL != fixedAddr)
|
||||
{
|
||||
if(file_consist_str("/tmp/subconf.conf", fixedAddr->valuestring)){
|
||||
//printf("fixed ip already exists\n");
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
if(!check_ip(fixedAddr->valuestring)){
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
memset(cmd, 0, len + 1);
|
||||
snprintf(cmd, len, "sed -r -i 's/fixed-address[ \\t0-9\\.]+;/fixed-address %s;/g' /tmp/subconf.conf", fixedAddr->valuestring);
|
||||
system(cmd);
|
||||
}
|
||||
//从临时文件读取字符串,写入原配置文件
|
||||
char *conf_after;
|
||||
char *temp_conf_file = "/tmp/subconf.conf";
|
||||
conf_after = getfileall_with_linefeed(temp_conf_file);
|
||||
if(NULL != conf_after)
|
||||
{
|
||||
memset(cmd, 0, len+1);
|
||||
snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/[ \\t]*host[ \\t]+%s[ \\t]*\\{[a-zA-Z#:; \\t\\n0-9\\.\\-]+\\}/%s/g' /etc/dhcp/dhcpd.conf", hostName->valuestring, conf_after);
|
||||
system(cmd);
|
||||
}
|
||||
if(conf_after){
|
||||
free(conf_after);
|
||||
conf_after = NULL;
|
||||
}
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*运行配置文件*/
|
||||
system("service isc-dhcp-server restart");
|
||||
|
||||
INPUT_ERROR:
|
||||
/*释放内存*/
|
||||
if(cmd)
|
||||
{
|
||||
memset(cmd, 0, len + 1);
|
||||
free(cmd);
|
||||
}
|
||||
//ret = RET_INPUTERR;
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret_code dhcp_host_config_get(uint source,
|
||||
pointer input, int input_len,
|
||||
pointer output, int *output_len)
|
||||
{
|
||||
ret_code ret = RET_OK;
|
||||
cJSON *root=NULL, *host=NULL;
|
||||
/*json解析*/
|
||||
root = cJSON_Parse(input);
|
||||
if(NULL == root)
|
||||
{
|
||||
return RET_INPUTERR;
|
||||
}
|
||||
host = cJSON_GetObjectItem(root, "host-name");
|
||||
if(NULL == host)
|
||||
{
|
||||
return RET_INPUTERR;
|
||||
}
|
||||
if(!check_name(host->valuestring)){
|
||||
return RET_INPUTERR;
|
||||
}
|
||||
|
||||
//读取配置文件
|
||||
char *fname="/etc/dhcp/dhcpd.conf";
|
||||
FILE *fp;
|
||||
char *name=NULL, *mac=NULL, *ip=NULL;
|
||||
cJSON *json = NULL;
|
||||
char *out = NULL;
|
||||
char line[1000];
|
||||
int len;
|
||||
if ((fp=fopen(fname,"r"))==NULL){
|
||||
printf("打开文件%s错误\n",fname);
|
||||
return RET_ERR;
|
||||
}
|
||||
fseek(fp,0,SEEK_END);
|
||||
rewind(fp);
|
||||
|
||||
len = strlen(host->valuestring);
|
||||
name = (char *)malloc(len+1);
|
||||
memset(name, 0, 20);
|
||||
ip = (char *)malloc(20);
|
||||
memset(ip, 0, 20);
|
||||
mac = (char *)malloc(20);
|
||||
memset(mac, 0, 20);
|
||||
root = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(root, "host-name", host->valuestring);
|
||||
|
||||
while((fgets(line,1000,fp))!=NULL){
|
||||
char *startNAME = strstr(line, "host");
|
||||
char *endNAME = strstr(line, "{");
|
||||
if((startNAME!=NULL) && (endNAME!=NULL)){
|
||||
memcpy(name,startNAME+5,endNAME-startNAME-6);
|
||||
if(!strcmp(host->valuestring, name)){
|
||||
memset(ip, 0, 20);
|
||||
memset(mac, 0, 20);
|
||||
}
|
||||
}
|
||||
if(name != NULL){
|
||||
//不相等
|
||||
if(strcmp(host->valuestring, name)){
|
||||
continue;
|
||||
}
|
||||
}
|
||||
char *startIP = strstr(line, "fixed-address");
|
||||
char *endIP = strstr(line, ";");
|
||||
if((startIP!=NULL) && (endIP!=NULL)){
|
||||
memcpy(ip,startIP+14,endIP-startIP-14);
|
||||
}
|
||||
char *startMAC = strstr(line, "hardware ethernet");
|
||||
char *endMAC = strstr(line, ";");
|
||||
if((startMAC!=NULL) && (endMAC!=NULL)){
|
||||
memcpy(mac,startMAC+18,endMAC-startMAC-18);
|
||||
}
|
||||
}
|
||||
if(ip != NULL){
|
||||
cJSON_AddStringToObject(root, "fixed-address", ip);
|
||||
memset(ip, 0, 20);
|
||||
free(ip);
|
||||
}
|
||||
if(mac != NULL){
|
||||
cJSON_AddStringToObject(root, "hardware-ethernet", mac);
|
||||
memset(mac, 0, 20);
|
||||
free(mac);
|
||||
}
|
||||
if(name != NULL){
|
||||
memset(name, 0, 20);
|
||||
free(name);
|
||||
}
|
||||
fclose(fp);
|
||||
out = cJSON_PrintUnformatted(root);
|
||||
*output_len = strlen(out) + 1;
|
||||
memcpy(output, out, *output_len);
|
||||
free(out);
|
||||
return ret;
|
||||
}
|
||||
|
|
@ -0,0 +1,425 @@
|
|||
#include "dhcp_lib.h"
|
||||
|
||||
|
||||
char *getfileall(char *fname)
|
||||
{
|
||||
if(NULL == fname){
|
||||
return NULL;
|
||||
}
|
||||
FILE *fp;
|
||||
char *str;
|
||||
char txt[1000];
|
||||
int filesize;
|
||||
if ((fp=fopen(fname,"r"))==NULL){
|
||||
printf("打开文件%s错误\n",fname);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fseek(fp,0,SEEK_END);
|
||||
|
||||
filesize = ftell(fp);
|
||||
str=(char *)malloc(filesize);
|
||||
str[0]=0;
|
||||
|
||||
rewind(fp);
|
||||
while((fgets(txt,1000,fp))!=NULL){
|
||||
strcat(str,txt);
|
||||
}
|
||||
fclose(fp);
|
||||
if(!strcmp(str, "")){
|
||||
return NULL;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
char *getfilefirstline(char *fname)
|
||||
{
|
||||
if(NULL == fname){
|
||||
return NULL;
|
||||
}
|
||||
FILE *fp;
|
||||
char *str;
|
||||
char txt[1000];
|
||||
int filesize;
|
||||
if ((fp=fopen(fname,"r"))==NULL){
|
||||
printf("打开文件%s错误\n",fname);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fseek(fp,0,SEEK_END);
|
||||
|
||||
filesize = ftell(fp);
|
||||
str=(char *)malloc(filesize);
|
||||
str[0]=0;
|
||||
|
||||
rewind(fp);
|
||||
|
||||
while((fgets(txt,1000,fp))!=NULL){
|
||||
int len = strlen(txt);
|
||||
if(txt[len-1] == '\n'){
|
||||
txt[len-1] = '\0';
|
||||
}
|
||||
strcat(str,txt);
|
||||
break;
|
||||
}
|
||||
fclose(fp);
|
||||
if(!strcmp(str, "")){
|
||||
return NULL;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
char *getfileall_with_linefeed(char *fname)
|
||||
{
|
||||
if(NULL == fname){
|
||||
return NULL;
|
||||
}
|
||||
FILE *fp;
|
||||
char *str;
|
||||
char *p;
|
||||
char line[1000];
|
||||
int filesize;
|
||||
if ((fp=fopen(fname,"r"))==NULL){
|
||||
printf("打开文件%s错误\n",fname);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fseek(fp,0,SEEK_END);
|
||||
|
||||
filesize = ftell(fp);
|
||||
str=(char *)malloc(filesize+100);
|
||||
str[0]=0;
|
||||
|
||||
rewind(fp);
|
||||
while((fgets(line,1000,fp))!=NULL){
|
||||
int len = strlen(line);
|
||||
if(line[len-1] == '\n'){
|
||||
line[len-1] = ' ';
|
||||
//printf("%s\n", line);
|
||||
p = line+len;
|
||||
stpcpy(p, "\\n");
|
||||
//printf("%s\n", line);
|
||||
}
|
||||
strcat(str,line);
|
||||
}
|
||||
fclose(fp);
|
||||
//printf("%s\n", str);
|
||||
if(!strcmp(str, "")){
|
||||
return NULL;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
int check_name(char *name){
|
||||
if(NULL == name){
|
||||
return FALSE;
|
||||
}
|
||||
//1、数字+字母组成
|
||||
int i=0;
|
||||
while(name[i] != '\0'){
|
||||
if(((name[i]>='0') && (name[i]<='9')) || ((name[i]>='a') && (name[i]<='z')) || ((name[i]>='A') && (name[i]<='Z'))){
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
//2、唯一,在add的时候再判断
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
int check_mac(char *mac){
|
||||
if(NULL == mac){
|
||||
return FALSE;
|
||||
}
|
||||
int status;
|
||||
const char * pattern = "^([A-Fa-f0-9]{2}[-,:]){5}[A-Fa-f0-9]{2}$";
|
||||
const int cflags = REG_EXTENDED | REG_NEWLINE;
|
||||
|
||||
char ebuf[128];
|
||||
regmatch_t pmatch[1];
|
||||
int nmatch = 10;
|
||||
regex_t reg;
|
||||
|
||||
|
||||
status = regcomp(®, pattern, cflags);//编译正则模式
|
||||
if(status != 0) {
|
||||
regerror(status, ®, ebuf, sizeof(ebuf));
|
||||
//fprintf(stderr, "regcomp fail: %s , pattern '%s' \n",ebuf, pattern);
|
||||
goto failed;
|
||||
}
|
||||
|
||||
status = regexec(®, mac, nmatch, pmatch,0);//执行正则表达式和缓存的比较,
|
||||
if(status != 0) {
|
||||
regerror(status, ®, ebuf, sizeof(ebuf));
|
||||
//fprintf(stderr, "regexec fail: %s , mac:\"%s\" \n", ebuf, mac);
|
||||
goto failed;
|
||||
}
|
||||
|
||||
//printf("[%s] match success.\n", __FUNCTION__);
|
||||
regfree(®);
|
||||
return TRUE;
|
||||
|
||||
failed:
|
||||
regfree(®);
|
||||
return FALSE;
|
||||
}
|
||||
int file_consist_str(char *fname, char *str){
|
||||
//读取文件
|
||||
FILE *fp;
|
||||
//char *name, *mac, *ip;
|
||||
char line[1000];
|
||||
if ((fp=fopen(fname,"r"))==NULL){
|
||||
printf("打开文件%s错误\n",fname);
|
||||
return FALSE;
|
||||
}
|
||||
fseek(fp,0,SEEK_END);
|
||||
rewind(fp);
|
||||
|
||||
while((fgets(line,1000,fp))!=NULL){
|
||||
char *p = strstr(line, str);
|
||||
if(p){
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
int check_range(char *range, char *mask, char *subnet){
|
||||
if((NULL==range) || (NULL==mask) || (NULL==subnet)){
|
||||
return FALSE;
|
||||
}
|
||||
int len = strlen(range);
|
||||
char rangeARR[len+1];
|
||||
strcpy(rangeARR, range);
|
||||
|
||||
//1、分出两个ip & 判断是ip
|
||||
struct in_addr low, high, subnetadd, maskaddr;
|
||||
char *item;
|
||||
item = strtok(rangeARR, " ");
|
||||
int code, lowsub, highsub;
|
||||
if(NULL == item){
|
||||
return FALSE;
|
||||
}
|
||||
code = inet_aton(item, &low);
|
||||
if(!(code)){
|
||||
return FALSE;
|
||||
}
|
||||
item = strtok(NULL, " ");
|
||||
if(NULL == item){
|
||||
return FALSE;
|
||||
}
|
||||
code = inet_aton(item, &high);
|
||||
if(!(code)){
|
||||
return FALSE;
|
||||
}
|
||||
item = strtok(NULL, " ");
|
||||
if(NULL != item){
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// 2、在subnet范围内
|
||||
inet_aton(subnet, &subnetadd);
|
||||
inet_aton(mask, &maskaddr);
|
||||
lowsub = low.s_addr & maskaddr.s_addr;
|
||||
if(lowsub != subnetadd.s_addr){
|
||||
return FALSE;
|
||||
}
|
||||
highsub = high.s_addr & maskaddr.s_addr;
|
||||
if(highsub != subnetadd.s_addr){
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
//3、后一个比前一个大
|
||||
char lowARR[15];
|
||||
strcpy(lowARR, inet_ntoa(low));
|
||||
char highARR[15];
|
||||
strcpy(highARR, inet_ntoa(high));
|
||||
if(strcmp(lowARR, highARR) >= 0){
|
||||
return FALSE;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
int check_dns(char *dns){
|
||||
if(NULL == dns){
|
||||
return FALSE;
|
||||
}
|
||||
int len = strlen(dns);
|
||||
char dnsARR[len+1];
|
||||
strcpy(dnsARR, dns);
|
||||
|
||||
//拆分DNS并判断是否是ip
|
||||
char *item;
|
||||
item = strtok(dnsARR, ",");
|
||||
if(NULL == item){
|
||||
return FALSE;
|
||||
}
|
||||
if(!check_ip(item)){
|
||||
return FALSE;
|
||||
}
|
||||
item = strtok(NULL, ",");
|
||||
//只有一个dns
|
||||
if(NULL == item){
|
||||
return TRUE;
|
||||
}
|
||||
if(!check_ip(item)){
|
||||
return FALSE;
|
||||
}
|
||||
item = strtok(NULL, ",");
|
||||
if(NULL != item){
|
||||
return FALSE;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
int check_lease(char *lease){
|
||||
//是数字就可以
|
||||
int i=0;
|
||||
while(lease[i] != '\0'){
|
||||
if((lease[i]>='0') && (lease[i]<='9')){
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
int check_ip(char *ip){
|
||||
if(NULL == ip){
|
||||
return FALSE;
|
||||
}
|
||||
struct in_addr ipadd;
|
||||
//int s_addr;
|
||||
int code;
|
||||
code = inet_aton(ip, &ipadd);
|
||||
/*
|
||||
int s_addr;
|
||||
s_addr = htonl(ipadd.s_addr);
|
||||
printf("[test]ipadd.s_addr=%x, s_addr=%x\n", ipadd.s_addr, s_addr);
|
||||
ipadd.s_addr = s_addr;*/
|
||||
if((1 == code)){
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
char *get_interface_subnet(char *interface) {
|
||||
if(NULL == interface){
|
||||
return NULL;
|
||||
}
|
||||
struct in_addr ipadd, maskadd, subnet;
|
||||
char *ip, *mask;
|
||||
ip = get_interface_ip(interface);
|
||||
mask = get_interface_mask(interface);
|
||||
|
||||
if(ip == NULL){
|
||||
return NULL;
|
||||
}
|
||||
inet_aton(ip, &ipadd);
|
||||
if(mask == NULL){
|
||||
return NULL;
|
||||
}
|
||||
inet_aton(mask, &maskadd);
|
||||
subnet.s_addr = ipadd.s_addr & maskadd.s_addr;
|
||||
if(ip != NULL){
|
||||
free(ip);
|
||||
ip = NULL;
|
||||
}
|
||||
if(mask != NULL){
|
||||
free(mask);
|
||||
mask = NULL;
|
||||
}
|
||||
//printf ("%s\n", inet_ntoa(subnet));
|
||||
char *str = (char *)malloc(15);
|
||||
memset(str, 0, 15);
|
||||
strcpy(str, inet_ntoa(subnet));
|
||||
return str;
|
||||
}
|
||||
|
||||
char *get_interface_ip(char *interface) {
|
||||
if(NULL == interface){
|
||||
return NULL;
|
||||
}
|
||||
char *cmd = (char *)malloc(101);
|
||||
if(!cmd){
|
||||
return NULL;
|
||||
}
|
||||
memset(cmd, 0, 101);
|
||||
//snprintf(cmd, 100, "ifconfig %s | grep \"inet addr\" | awk '{ print $2}' | awk -F: '{print $2}' > /tmp/ip", interface);
|
||||
snprintf(cmd, 100, "ifconfig %s | grep \"inet\" | awk '{ print $2}' > /tmp/ip", interface);
|
||||
system(cmd);
|
||||
memset(cmd, 0, 101);
|
||||
free(cmd);
|
||||
return getfilefirstline("/tmp/ip");
|
||||
}
|
||||
|
||||
char *get_interface_mask(char *interface) {
|
||||
if(NULL == interface){
|
||||
return NULL;
|
||||
}
|
||||
char *cmd = (char *)malloc(101);
|
||||
memset(cmd, 0, 101);
|
||||
//snprintf(cmd, 100, "ifconfig %s | grep \"Mask\" | awk '{ print $4}' | awk -F: '{print $2}' > /tmp/mask", interface);
|
||||
snprintf(cmd, 100, "ifconfig %s | grep \"netmask\" | awk '{ print $4}' > /tmp/mask", interface);
|
||||
system(cmd);
|
||||
memset(cmd, 0, 101);
|
||||
free(cmd);
|
||||
return getfilefirstline("/tmp/mask");
|
||||
}
|
||||
|
||||
int check_servers(char *servers){
|
||||
if(NULL == servers){
|
||||
return FALSE;
|
||||
}
|
||||
int len = strlen(servers);
|
||||
char arr[len+1];
|
||||
strcpy(arr, servers);
|
||||
|
||||
//拆分servers并判断是否是ip
|
||||
char *item;
|
||||
item = strtok(arr, " ");
|
||||
if(NULL == item){
|
||||
return FALSE;
|
||||
}
|
||||
while(item != NULL){
|
||||
if(!check_ip(item)){
|
||||
return FALSE;
|
||||
}
|
||||
item = strtok(NULL, " ");
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
int check_segment(char *segment, char *mask){
|
||||
//1、是ip,是网段ip
|
||||
struct in_addr segmentadd, maskadd;
|
||||
|
||||
if(check_ip(segment)){
|
||||
inet_aton(segment, &segmentadd);
|
||||
inet_aton(mask, &maskadd);
|
||||
if((maskadd.s_addr & segmentadd.s_addr) == segmentadd.s_addr){
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
int check_mask(char *mask)
|
||||
{
|
||||
if(check_ip(mask))
|
||||
{
|
||||
unsigned int b = 0, i, n[4];
|
||||
sscanf(mask, "%u.%u.%u.%u", &n[3], &n[2], &n[1], &n[0]);
|
||||
for(i = 0; i < 4; ++i){ //将子网掩码存入32位无符号整型
|
||||
b += n[i] << (i * 8);
|
||||
}
|
||||
b = ~b + 1;
|
||||
if((b & (b - 1)) == 0){ //判断是否为2^n
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
|
@ -0,0 +1,84 @@
|
|||
#include "dhcp_relay_config.h"
|
||||
|
||||
ret_code dhcp_relay_config_chk(uint source,uint *config_type,
|
||||
pointer input, int *input_len,
|
||||
pointer output, int *output_len)
|
||||
{
|
||||
ret_code ret = RET_OK;
|
||||
*config_type = CM_CONFIG_SET;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
ret_code dhcp_relay_config_proc(uint source, uint config_type,
|
||||
pointer input, int input_len,
|
||||
pointer output, int *output_len)
|
||||
{
|
||||
ret_code ret = RET_OK;
|
||||
//int i, count;
|
||||
cJSON *root=NULL, *operate=NULL, *interfaces=NULL, *servers=NULL;
|
||||
int len = 100;
|
||||
char *cmd = (char *)malloc(len + 1);
|
||||
if(NULL == cmd)
|
||||
{
|
||||
ret = RET_ERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
/*json解析*/
|
||||
root = cJSON_Parse(input);
|
||||
if(NULL == root)
|
||||
{
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
operate = cJSON_GetObjectItem(root, "operate");
|
||||
if(NULL == operate)
|
||||
{
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
|
||||
if(!strcmp(operate->valuestring, "close relay")){
|
||||
//删除配置文件
|
||||
memset(cmd, 0, len + 1);
|
||||
snprintf(cmd, len, "sed -r -i 's/SERVERS=\"[ \\t0-9\\.]*\"/SERVERS=\"\"/g' /etc/default/isc-dhcp-relay"); //\" 不能写成\\"
|
||||
system(cmd);
|
||||
memset(cmd, 0, len + 1);
|
||||
snprintf(cmd, len, "sed -r -i 's/INTERFACES=\"[ \\ta-zA-Z0-9\\.]*\"/INTERFACES=\"\"/g' /etc/default/isc-dhcp-relay"); //\" 不能写成\\"
|
||||
system(cmd);
|
||||
system("service isc-dhcp-relay stop");
|
||||
}
|
||||
if(!strcmp(operate->valuestring, "start relay")){
|
||||
interfaces = cJSON_GetObjectItem(root, "interfaces");
|
||||
servers = cJSON_GetObjectItem(root, "servers");
|
||||
if((NULL == interfaces) || (NULL == servers))
|
||||
{
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
//先不check interface
|
||||
//check servers
|
||||
if(!check_servers(servers->valuestring)){
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
|
||||
memset(cmd, 0, len + 1);
|
||||
snprintf(cmd, len, "sed -r -i 's/SERVERS=\"[ \\t0-9\\.]*\"/SERVERS=\"%s\"/g' /etc/default/isc-dhcp-relay", servers->valuestring); //\" 不能写成\\"
|
||||
system(cmd);
|
||||
memset(cmd, 0, len + 1);
|
||||
snprintf(cmd, len, "sed -r -i 's/INTERFACES=\"[ \\ta-zA-Z0-9\\.]*\"/INTERFACES=\"%s\"/g' /etc/default/isc-dhcp-relay", interfaces->valuestring); //\" 不能写成\\"
|
||||
system(cmd);
|
||||
/*运行配置文件*/
|
||||
system("service isc-dhcp-relay restart");
|
||||
}
|
||||
|
||||
INPUT_ERROR:
|
||||
if(cmd)
|
||||
{
|
||||
memset(cmd, 0, len + 1);
|
||||
free(cmd);
|
||||
}
|
||||
return ret;
|
||||
|
||||
}
|
|
@ -0,0 +1,467 @@
|
|||
#include "dhcp_subnet_config.h"
|
||||
|
||||
ret_code dhcp_shared_network_config_chk(uint source, uint *config_type,
|
||||
pointer input, int *input_len,
|
||||
pointer output, int *output_len)
|
||||
{
|
||||
ret_code ret = RET_OK;
|
||||
cJSON *root=NULL, *operate=NULL;
|
||||
root = cJSON_Parse(input);
|
||||
if(NULL == root)
|
||||
{
|
||||
return RET_INPUTERR;
|
||||
}
|
||||
operate = cJSON_GetObjectItem(root, "operate");
|
||||
if(operate){
|
||||
*config_type = CM_CONFIG_SET;
|
||||
}
|
||||
else {
|
||||
*config_type = CM_CONFIG_GET_ALL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret_code dhcp_shared_network_config_proc(uint source, uint config_type,
|
||||
pointer input, int input_len,
|
||||
pointer output, int *output_len)
|
||||
{
|
||||
ret_code ret = RET_OK;
|
||||
int i, count;
|
||||
cJSON *root=NULL, *operate=NULL, *shared=NULL, *name=NULL, *subnet=NULL, *segment=NULL, *netmask=NULL, *range=NULL, *routers=NULL, *dns=NULL, *lease=NULL;
|
||||
|
||||
/*json解析*/
|
||||
root = cJSON_Parse(input);
|
||||
if(NULL == root)
|
||||
{
|
||||
return RET_INPUTERR;
|
||||
}
|
||||
operate = cJSON_GetObjectItem(root, "operate");
|
||||
if(NULL == operate)
|
||||
{
|
||||
return RET_INPUTERR;
|
||||
}
|
||||
shared = cJSON_GetObjectItem(root, "shared-network");
|
||||
if(NULL == shared)
|
||||
{
|
||||
return RET_INPUTERR;
|
||||
}
|
||||
|
||||
count = cJSON_GetArraySize(shared);
|
||||
int len = 5000;
|
||||
char *cmd = (char *)malloc(len + 1);
|
||||
if(NULL == cmd)
|
||||
{
|
||||
return RET_ERR;
|
||||
}
|
||||
|
||||
for(i = 0; i < count; i++)
|
||||
|
||||
{
|
||||
cJSON *one = cJSON_GetArrayItem(shared, i);
|
||||
if(NULL == one)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
name = cJSON_GetObjectItem(one, "name");
|
||||
if(NULL == name){
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
if(!check_name(name->valuestring)){
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
subnet = cJSON_GetObjectItem(one, "subnet");
|
||||
if((!strcmp(operate->valuestring, "add")) || (!strcmp(operate->valuestring, "mod"))){
|
||||
if(NULL == subnet){
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
segment = cJSON_GetObjectItem(subnet, "segment");
|
||||
netmask = cJSON_GetObjectItem(subnet, "netmask");
|
||||
range = cJSON_GetObjectItem(subnet, "range");
|
||||
routers = cJSON_GetObjectItem(subnet, "routers");
|
||||
dns = cJSON_GetObjectItem(subnet, "domain-name-servers");
|
||||
//submask = cJSON_GetObjectItem(subnet, "subnet-mask");
|
||||
lease = cJSON_GetObjectItem(subnet, "max-lease-time");
|
||||
}
|
||||
//写配置文件
|
||||
memset(cmd, 0, len + 1);
|
||||
if(!strcmp(operate->valuestring, "add"))
|
||||
{
|
||||
if((NULL==segment) || (NULL==netmask) || (NULL==range) || (NULL==routers) || (NULL==dns)){
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
//check mask
|
||||
if(!check_mask(netmask->valuestring)){
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
//check segment
|
||||
if(!check_segment(segment->valuestring, netmask->valuestring)){
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
//check range
|
||||
if(!check_range(range->valuestring, netmask->valuestring, segment->valuestring)){
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
//check routers
|
||||
if(!check_ip(routers->valuestring)){
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
//check dns
|
||||
if(!check_dns(dns->valuestring)){
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
//check lease
|
||||
if(lease && !check_lease(lease->valuestring)){
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
//判断之前是否有这个shared-network和这个subnet
|
||||
snprintf(cmd, len, "grep -Pzo '[\\s\\t]*shared-network[\\s\\t]+%s[\\s\\t]*{' /etc/dhcp/dhcpd.conf > /tmp/subconf.conf", name->valuestring);
|
||||
system(cmd);
|
||||
if(NULL != getfileall("/tmp/subconf.conf")){
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
memset(cmd, 0, len + 1);
|
||||
snprintf(cmd, len, "grep -Pzo '[\\s\\t]*subnet[\\s\\t]+%s[\\s\\t]+netmask[\\s\\t]+%s[\\s\\t]*{' /etc/dhcp/dhcpd.conf > /tmp/subconf.conf", segment->valuestring, netmask->valuestring);
|
||||
system(cmd);
|
||||
if(NULL != getfileall("/tmp/subconf.conf")){
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
memset(cmd, 0, len + 1);
|
||||
snprintf(cmd, len, "echo -e 'shared-network %s {\\n subnet %s netmask %s {\\n range %s;\\n option domain-name-servers %s;\\n option subnet-mask %s;\\n option routers %s;' >> /etc/dhcp/dhcpd.conf",
|
||||
name->valuestring, segment->valuestring, netmask->valuestring, range->valuestring, dns->valuestring, netmask->valuestring, routers->valuestring);
|
||||
system(cmd);
|
||||
if(NULL != lease){
|
||||
memset(cmd, 0, len + 1);
|
||||
snprintf(cmd, len, "echo ' max-lease-time %s;' >> /etc/dhcp/dhcpd.conf", lease->valuestring);
|
||||
system(cmd);
|
||||
}
|
||||
system("echo -e ' }\\n}' >> /etc/dhcp/dhcpd.conf");
|
||||
}
|
||||
if(!strcmp(operate->valuestring, "del")){
|
||||
//判断之前是否有配置
|
||||
snprintf(cmd, len, "grep -Pzo '[\\s\\t]*shared-network[\\s\\t]+%s[\\s\\t]*{' /etc/dhcp/dhcpd.conf > /tmp/subconf.conf", name->valuestring);
|
||||
system(cmd);
|
||||
if(NULL == getfileall("/tmp/subconf.conf")){
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
memset(cmd, 0, len + 1);
|
||||
//snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/[ \\t]*shared-network[ \\t]+%s[ \\t]*\\{\\n[ \\t]*subnet[ \\t]+%s[ \\t]+netmask[ \\t]+%s[ \\t]*\\{[a-zA-Z#:; \\t\\n0-9\\.\\-]+\\}\\n[ \\t]*\\}//g' /etc/dhcp/dhcpd.conf",
|
||||
//name->valuestring, segment->valuestring, netmask->valuestring);
|
||||
snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/[ \\t]*shared-network[ \\t]+%s[ \\t]*\\{[a-zA-Z#:; \\t\\n0-9\\.\\,\\{\\-]+\\}[ \\t\\n]*\\}/#/g' /etc/dhcp/dhcpd.conf", name->valuestring); //正则里面的\\-得放在最后
|
||||
system(cmd);
|
||||
system("sed -r -i '/#/d' /etc/dhcp/dhcpd.conf");
|
||||
}
|
||||
if(!strcmp(operate->valuestring, "mod")){
|
||||
snprintf(cmd, len, "grep -Pzo 'shared-network[\\s\\t]+%s[\\s\\t]*{[\\n\\s\\t]*subnet[\\s\\t]+%s[\\s\\t]+netmask[\\s\\t]+%s[\\s\\t]*{[a-zA-Z#:;\\s\\t\\n0-9\\.\\,\\-]+}[\\n\\s\\t]*}' /etc/dhcp/dhcpd.conf > /tmp/subconf.conf",
|
||||
name->valuestring, segment->valuestring, netmask->valuestring);
|
||||
system(cmd);
|
||||
if(NULL == getfileall("/tmp/subconf.conf")){
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
if(NULL != range)
|
||||
{
|
||||
if(!check_range(range->valuestring, netmask->valuestring, segment->valuestring)){
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
if(file_consist_str("/tmp/subconf.conf",range->valuestring)){
|
||||
//printf("range already exists\n");
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
memset(cmd, 0, len + 1);
|
||||
snprintf(cmd, len, "sed -r -i 's/range[ \\t0-9\\.]+;/range %s;/g' /tmp/subconf.conf", range->valuestring);
|
||||
system(cmd);
|
||||
}
|
||||
if(NULL != dns)
|
||||
{
|
||||
if(!check_dns(dns->valuestring)){
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
char *strdns = (char *)malloc(80);
|
||||
memset(strdns, 0, 80);
|
||||
snprintf(strdns, 80, "option domain-name-servers %s;", dns->valuestring);
|
||||
//printf("strdns: %s\n", strdns);
|
||||
if(file_consist_str("/tmp/subconf.conf",strdns)){
|
||||
//printf("dns already exists\n");
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
if(strdns){
|
||||
memset(strdns, 0, 80);
|
||||
free(strdns);
|
||||
}
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
if(strdns){
|
||||
memset(strdns, 0, 80);
|
||||
free(strdns);
|
||||
}
|
||||
memset(cmd, 0, len + 1);
|
||||
snprintf(cmd, len, "sed -r -i 's/option[ \\t]+domain-name-servers[ \\t0-9\\.\\,]+;/option domain-name-servers %s;/g' /tmp/subconf.conf", dns->valuestring);
|
||||
system(cmd);
|
||||
}
|
||||
|
||||
if(NULL != routers)
|
||||
{
|
||||
if(!check_ip(routers->valuestring)){
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
char *strrouters = (char *)malloc(40);
|
||||
memset(strrouters, 0, 40);
|
||||
snprintf(strrouters, 40, "option routers %s;", routers->valuestring);
|
||||
//printf("strdns: %s\n", strdns);
|
||||
if(file_consist_str("/tmp/subconf.conf",strrouters)){
|
||||
//printf("dns already exists\n");
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
if(strrouters){
|
||||
memset(strrouters, 0, 40);
|
||||
free(strrouters);
|
||||
}
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
if(strrouters){
|
||||
memset(strrouters, 0, 40);
|
||||
free(strrouters);
|
||||
}
|
||||
memset(cmd, 0, len + 1);
|
||||
snprintf(cmd, len, "sed -r -i 's/option[ \\t]+routers[ \\t0-9\\.]+;/option routers %s;/g' /tmp/subconf.conf", routers->valuestring);
|
||||
system(cmd);
|
||||
}
|
||||
if(NULL != lease)
|
||||
{
|
||||
if(!check_lease(lease->valuestring)){
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
char *strlease = (char *)malloc(1000);
|
||||
memset(strlease, 0, 1000);
|
||||
snprintf(strlease, 1000, "max-lease-time %s;", lease->valuestring);
|
||||
if(file_consist_str("/tmp/subconf.conf",strlease)){
|
||||
//printf("lease already exists\n");
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
if(strlease){
|
||||
memset(strlease, 0, 1000);
|
||||
free(strlease);
|
||||
}
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
if(strlease){
|
||||
memset(strlease, 0, 1000);
|
||||
free(strlease);
|
||||
}
|
||||
/*lease 是0,就是恢复默认,需要删除配置文件中的max-lease-time*/
|
||||
memset(cmd, 0, len+1);
|
||||
snprintf(cmd, len, "sed -r -i 's/max-lease-time[ \\t0-9]+;/#/g' /tmp/subconf.conf");
|
||||
system(cmd);
|
||||
system("sed -r -i '/#/d' /tmp/subconf.conf");
|
||||
if(strcmp(lease->valuestring, "0")){
|
||||
memset(cmd, 0, len+1);
|
||||
snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/\\}[ \\t\\n]*\\}/ max-lease-time %s;\\n \\}\\n\\}/g' /tmp/subconf.conf", lease->valuestring); //测试的时候看一下替换的\\}
|
||||
//printf("cmd: %s\n", cmd);
|
||||
system(cmd);
|
||||
}
|
||||
}
|
||||
//从临时文件读取字符串,写入原配置文件
|
||||
char *conf_after;
|
||||
char *temp_conf_file = "/tmp/subconf.conf";
|
||||
conf_after = getfileall_with_linefeed(temp_conf_file);
|
||||
if(NULL != conf_after)
|
||||
{
|
||||
memset(cmd, 0, len+1);
|
||||
snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/[ \\t]*shared-network[ \\t]+%s[ \\t]*\\{[ \\t\\n]*subnet[ \\t]+%s[ \\t]+netmask[ \\t]+%s[ \\t]*\\{[a-zA-Z#:; \\t\\n0-9\\.\\,\\-]+\\}[ \\t\\n]*\\}/%s/g' /etc/dhcp/dhcpd.conf",
|
||||
name->valuestring, segment->valuestring, netmask->valuestring, conf_after);
|
||||
system(cmd);
|
||||
}
|
||||
if(conf_after){
|
||||
free(conf_after);
|
||||
conf_after = NULL;
|
||||
}
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
}
|
||||
}
|
||||
/*运行配置文件*/
|
||||
system("service isc-dhcp-server restart");
|
||||
|
||||
INPUT_ERROR:
|
||||
if(cmd)
|
||||
{
|
||||
memset(cmd, 0, len + 1);
|
||||
free(cmd);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret_code dhcp_shared_network_config_get_all(uint source, pointer output, int *output_len){
|
||||
ret_code ret = RET_OK;
|
||||
char *fname="/etc/dhcp/dhcpd.conf";
|
||||
FILE *fp;
|
||||
char *name=NULL, *segment=NULL, *netmask=NULL, *range=NULL, *dns=NULL, *submask=NULL, *routers=NULL, *lease=NULL;
|
||||
cJSON *root = NULL, *arr=NULL, *json = NULL, *subnet = NULL;
|
||||
char *out = NULL;
|
||||
char line[1000];
|
||||
if ((fp=fopen(fname,"r"))==NULL){
|
||||
printf("打开文件%s错误\n",fname);
|
||||
return RET_ERR;
|
||||
}
|
||||
fseek(fp,0,SEEK_END);
|
||||
rewind(fp);
|
||||
|
||||
name = (char *)malloc(100);
|
||||
memset(name, 0, 100);
|
||||
segment = (char *)malloc(20);
|
||||
memset(segment, 0, 20);
|
||||
netmask = (char *)malloc(20);
|
||||
memset(netmask, 0, 20);
|
||||
range = (char *)malloc(40);
|
||||
memset(range, 0, 40);
|
||||
dns = (char *)malloc(40);
|
||||
memset(dns, 0, 40);
|
||||
submask = (char *)malloc(20);
|
||||
memset(submask, 0, 20);
|
||||
routers= (char *)malloc(20);
|
||||
memset(routers, 0, 20);
|
||||
lease = (char *)malloc(1000);
|
||||
memset(lease, 0, 1000);
|
||||
|
||||
root = cJSON_CreateObject();
|
||||
cJSON_AddItemToObject(root, "shared-network", arr = cJSON_CreateArray());
|
||||
int count = 0; //计算}的个数
|
||||
|
||||
while((fgets(line,1000,fp))!=NULL){
|
||||
char *startNAME = strstr(line, "shared-network");
|
||||
char *endNAME = strstr(line, "{");
|
||||
if((startNAME!=NULL) && (endNAME!=NULL)){
|
||||
memcpy(name,startNAME+15,endNAME-startNAME-16);
|
||||
//printf("ip: %s\n", ip);
|
||||
json = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(json , "name", name);
|
||||
cJSON_AddItemToObject(json, "subnet", subnet = cJSON_CreateObject());
|
||||
memset(segment, 0, 20);
|
||||
memset(netmask, 0, 20);
|
||||
memset(range, 0, 40);
|
||||
memset(dns, 0, 40);
|
||||
memset(submask, 0, 20);
|
||||
memset(routers, 0, 20);
|
||||
memset(lease, 0, 1000);
|
||||
}
|
||||
char *startSUB = strstr(line, "subnet");
|
||||
char *startMASK = strstr(line, "netmask");
|
||||
char *endMASK = strstr(line, "{");
|
||||
if((startSUB!=NULL) && (startMASK!=NULL) && (endMASK!=NULL)){
|
||||
memcpy(segment,startSUB+7,startMASK-startSUB-8);
|
||||
cJSON_AddStringToObject(subnet, "segment", segment);
|
||||
memcpy(netmask,startMASK+8,endMASK-startMASK-9);
|
||||
cJSON_AddStringToObject(subnet, "netmask", netmask);
|
||||
}
|
||||
char *startRANGE = strstr(line, "range");
|
||||
char *endRANGE = strstr(line, ";");
|
||||
if((startRANGE!=NULL) && (endRANGE!=NULL)){
|
||||
memcpy(range,startRANGE+6,endRANGE-startRANGE-6);
|
||||
cJSON_AddStringToObject(subnet, "range", range);
|
||||
}
|
||||
char *startDNS = strstr(line, "option domain-name-servers");
|
||||
char *endDNS = strstr(line, ";");
|
||||
if((startDNS!=NULL) && (endDNS!=NULL)){
|
||||
memcpy(dns,startDNS+27,endDNS-startDNS-27);
|
||||
cJSON_AddStringToObject(subnet, "domain-name-servers", dns);
|
||||
}
|
||||
char *startSUNMASK = strstr(line, "option subnet-mask");
|
||||
char *endSUBMASK = strstr(line, ";");
|
||||
if((startSUNMASK!=NULL) && (endSUBMASK!=NULL)){
|
||||
memcpy(submask,startSUNMASK+19,endSUBMASK-startSUNMASK-19);
|
||||
cJSON_AddStringToObject(subnet, "subnet-mask", submask);
|
||||
}
|
||||
char *startROUTERS = strstr(line, "option routers");
|
||||
char *endROUTERS = strstr(line, ";");
|
||||
if((startROUTERS!=NULL) && (endROUTERS!=NULL)){
|
||||
memcpy(routers,startROUTERS+15,endROUTERS-startROUTERS-15);
|
||||
cJSON_AddStringToObject(subnet, "routers", routers);
|
||||
}
|
||||
char *startLEASE = strstr(line, "max-lease-time");
|
||||
char *endLEASE = strstr(line, ";");
|
||||
if((startLEASE!=NULL) && (endLEASE!=NULL)){
|
||||
memcpy(lease,startLEASE+15,endLEASE-startLEASE-15);
|
||||
cJSON_AddStringToObject(subnet, "max-lease-time", lease);
|
||||
}
|
||||
char *end = strstr(line, "}");
|
||||
if(end && strcmp(name, "")){
|
||||
count++;
|
||||
if(count == 2){
|
||||
count = 0;
|
||||
cJSON_AddItemToArray(arr, json);
|
||||
//printf("=====================\n");
|
||||
memset(name, 0, 100);
|
||||
}
|
||||
}
|
||||
}
|
||||
if(name != NULL){
|
||||
memset(name, 0, 100);
|
||||
free(name);
|
||||
}
|
||||
if(range != NULL){
|
||||
memset(range, 0, 40);
|
||||
free(range);
|
||||
}
|
||||
if(dns != NULL){
|
||||
memset(dns, 0, 40);
|
||||
free(dns);
|
||||
}
|
||||
if(submask != NULL){
|
||||
memset(submask, 0, 20);
|
||||
free(submask);
|
||||
}
|
||||
if(routers != NULL){
|
||||
memset(routers, 0, 20);
|
||||
free(routers);
|
||||
}
|
||||
if(lease != NULL){;
|
||||
memset(lease, 0, 1000);
|
||||
free(lease);
|
||||
}
|
||||
if(segment != NULL){
|
||||
memset(segment, 0, 20);
|
||||
free(segment);
|
||||
}
|
||||
if(netmask != NULL){
|
||||
memset(netmask, 0, 20);
|
||||
free(netmask);
|
||||
}
|
||||
fclose(fp);
|
||||
out = cJSON_PrintUnformatted(root);
|
||||
*output_len = strlen(out) + 1;
|
||||
memcpy(output, out, *output_len);
|
||||
free(out);
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,452 @@
|
|||
#include "dhcp_subnet_config.h"
|
||||
|
||||
ret_code dhcp_subnet_config_chk(uint source, uint *config_type,
|
||||
pointer input, int *input_len,
|
||||
pointer output, int *output_len)
|
||||
{
|
||||
ret_code ret = RET_OK;
|
||||
cJSON *root=NULL, *operate=NULL;
|
||||
root = cJSON_Parse(input);
|
||||
if(NULL == root)
|
||||
{
|
||||
return RET_INPUTERR;
|
||||
}
|
||||
operate = cJSON_GetObjectItem(root, "operate");
|
||||
if(operate)
{
|
||||
*config_type = CM_CONFIG_SET;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret_code dhcp_subnet_config_proc(uint source, uint config_type,
|
||||
pointer input, int input_len,
|
||||
pointer output, int *output_len)
|
||||
{
|
||||
ret_code ret = RET_OK;
|
||||
int i, count;
|
||||
cJSON *root=NULL, *operate=NULL, *subnet=NULL;
|
||||
char *segment=NULL, *mask=NULL, *router=NULL;
|
||||
|
||||
/*json解析*/
|
||||
root = cJSON_Parse(input);
|
||||
if(NULL == root)
|
||||
{
|
||||
return RET_INPUTERR;
|
||||
}
|
||||
operate = cJSON_GetObjectItem(root, "operate");
|
||||
if(NULL == operate)
|
||||
{
|
||||
return RET_INPUTERR;
|
||||
}
|
||||
subnet= cJSON_GetObjectItem(root, "subnet");
|
||||
if(NULL == subnet)
|
||||
{
|
||||
return RET_INPUTERR;
|
||||
}
|
||||
|
||||
count = cJSON_GetArraySize(subnet);
|
||||
int len = 500;
|
||||
char *cmd = (char *)malloc(len + 1);
|
||||
if(NULL == cmd)
|
||||
{
|
||||
return RET_ERR;
|
||||
}
|
||||
for(i = 0; i < count; i++)
|
||||
{
|
||||
cJSON *one = cJSON_GetArrayItem(subnet, i);
|
||||
if(NULL == one)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
cJSON *interface = cJSON_GetObjectItem(one, "interface");
|
||||
if(NULL == interface){
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
cJSON *range = cJSON_GetObjectItem(one, "range");
|
||||
//cJSON *routers = cJSON_GetObjectItem(one, "routers");
|
||||
cJSON *dns = cJSON_GetObjectItem(one, "domain-name-servers");
|
||||
cJSON *lease = cJSON_GetObjectItem(one, "max-lease-time");
|
||||
|
||||
//根据接口获取ip的网段、掩码、网关ip 之后可以放外面,这样便于出错的时候释放
|
||||
segment = get_interface_subnet(interface->valuestring);
|
||||
if(NULL == segment){
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
mask = get_interface_mask(interface->valuestring);
|
||||
if(NULL == mask){
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
router = get_interface_ip(interface->valuestring);
|
||||
if(NULL == router){
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
|
||||
//写配置文件
|
||||
memset(cmd, 0, len + 1);
|
||||
if(!strcmp(operate->valuestring, "add"))
|
||||
{
|
||||
//判断是否已经有这个配置
|
||||
if(file_consist_str("/etc/default/isc-dhcp-server", interface->valuestring)){
|
||||
//printf("interface is already configured\n");
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
if(NULL == range){
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
//check range
|
||||
if(!check_range(range->valuestring, mask,segment)){
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
if(lease && !check_lease(lease->valuestring)){
|
||||
ret = RET_INPUTERR;
|
||||
//system("echo '}' >> /etc/dhcp/dhcpd.conf");
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
if(dns && !check_dns(dns->valuestring)){
|
||||
ret = RET_INPUTERR;
|
||||
//system("echo '}' >> /etc/dhcp/dhcpd.conf");
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
/*
|
||||
subnet 10.0.7.0 netmask 255.255.255.0 {
|
||||
range 10.0.7.1 10.0.7.10;
|
||||
option domain-name-servers 202.206.192.33;
|
||||
option subnet-mask 255.255.255.0;
|
||||
option routers 10.0.7.1;
|
||||
max-lease-time 7200;
|
||||
}
|
||||
*/
|
||||
|
||||
//添加接口
|
||||
snprintf(cmd, len, "sed -r -i 's/INTERFACESv4=\"/INTERFACESv4=\"%s /g' /etc/default/isc-dhcp-server", interface->valuestring);
|
||||
system(cmd);
|
||||
memset(cmd, 0, len + 1);
|
||||
snprintf(cmd, len, "echo -e 'subnet %s netmask %s {\\n range %s;\\n option subnet-mask %s;\\n option routers %s;' >> /etc/dhcp/dhcpd.conf",
|
||||
segment, mask, range->valuestring, mask, router);
|
||||
system(cmd);
|
||||
if(NULL != lease){
|
||||
memset(cmd, 0, len + 1);
|
||||
snprintf(cmd, len, "echo ' max-lease-time %s;' >> /etc/dhcp/dhcpd.conf", lease->valuestring);
|
||||
system(cmd);
|
||||
}
|
||||
if(NULL != dns){
|
||||
memset(cmd, 0, len + 1);
|
||||
snprintf(cmd, len, "echo ' option domain-name-servers %s;' >> /etc/dhcp/dhcpd.conf", dns->valuestring);
|
||||
system(cmd);
|
||||
}
|
||||
system("echo '}' >> /etc/dhcp/dhcpd.conf");
|
||||
}
|
||||
if(!strcmp(operate->valuestring, "del")){
|
||||
if(!file_consist_str("/etc/default/isc-dhcp-server", interface->valuestring)){
|
||||
//printf("interface does not exist\n");
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
snprintf(cmd, len, "sed -r -i 's/%s //g' /etc/default/isc-dhcp-server", interface->valuestring);
|
||||
system(cmd);
|
||||
memset(cmd, 0, len + 1);
|
||||
snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/[ \\t]*subnet[ \\t]+%s[ \\t]+netmask[ \\t]+%s[ \\t]*\\{[a-zA-Z#:; \\t\\n0-9\\.\\,\\-]+\\}/#/g' /etc/dhcp/dhcpd.conf", segment, mask);
|
||||
system(cmd);
|
||||
system("sed -r -i '/#/d' /etc/dhcp/dhcpd.conf");
|
||||
//system("sed -r -i '/^#*$/d' /etc/dhcp/dhcpd.conf");
|
||||
}
|
||||
if(!strcmp(operate->valuestring, "mod")){
|
||||
if(!file_consist_str("/etc/default/isc-dhcp-server", interface->valuestring)){
|
||||
//printf("interface does not exist\n");
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
snprintf(cmd, len, "grep -Pzo 'subnet[\\s\\t]+%s[\\s\\t]+netmask[\\s\\t]+%s[\\s\\t]*{[a-zA-Z#:;\\s\\t\\n0-9\\.\\,\\-]+}' /etc/dhcp/dhcpd.conf > /tmp/subconf.conf", segment, mask);
|
||||
system(cmd);
|
||||
if(NULL == getfileall("/tmp/subconf.conf")){
|
||||
//printf("configuration do not exist\n");
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
if(NULL != range)
|
||||
{
|
||||
if(!check_range(range->valuestring, mask,segment)){
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
if(file_consist_str("/tmp/subconf.conf",range->valuestring)){
|
||||
//printf("range already exists\n");
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
memset(cmd, 0, len + 1);
|
||||
snprintf(cmd, len, "sed -r -i 's/range[ \\t0-9\\.]+;/range %s;/g' /tmp/subconf.conf", range->valuestring);
|
||||
system(cmd);
|
||||
}
|
||||
if(NULL != dns)
|
||||
{
|
||||
if(!check_dns(dns->valuestring)){
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
char *strdns = (char *)malloc(80);
|
||||
memset(strdns, 0, 80);
|
||||
snprintf(strdns, 80, "option domain-name-servers %s;", dns->valuestring);
|
||||
if(file_consist_str("/tmp/subconf.conf",strdns)){
|
||||
//printf("dns already exists\n");
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
if(strdns){
|
||||
memset(strdns, 0, 80);
|
||||
free(strdns);
|
||||
}
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
if(strdns){
|
||||
memset(strdns, 0, 80);
|
||||
free(strdns);
|
||||
}
|
||||
memset(cmd, 0, len + 1);
|
||||
snprintf(cmd, len, "sed -r -i 's/option[ \\t]+domain-name-servers[ \\t0-9\\.\\,]+;/option domain-name-servers %s;/g' /tmp/subconf.conf", dns->valuestring);
|
||||
system(cmd);
|
||||
}
|
||||
/*
|
||||
if(NULL != subnetMask)
|
||||
{
|
||||
memset(cmd, 0, len + 1);
|
||||
snprintf(cmd, len, "sed -r -i 's/option[ \\t]+subnet-mask[ \\t0-9\\.]+;/option subnet-mask %s;/g' /tmp/subconf.conf", subnetMask->valuestring);
|
||||
system(cmd);
|
||||
}
|
||||
if(NULL != routers)
|
||||
{
|
||||
memset(cmd, 0, len + 1);
|
||||
snprintf(cmd, len, "sed -r -i 's/option[ \\t]+routers[ \\t0-9\\.]+;/option routers %s;/g' /tmp/subconf.conf", routers->valuestring);
|
||||
system(cmd);
|
||||
}*/
|
||||
if(NULL != lease)
|
||||
{
|
||||
if(!check_lease(lease->valuestring)){
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
char *strlease = (char *)malloc(1000);
|
||||
memset(strlease, 0, 1000);
|
||||
snprintf(strlease, 1000, "max-lease-time %s;", lease->valuestring);
|
||||
if(file_consist_str("/tmp/subconf.conf",strlease)){
|
||||
//printf("lease already exists\n");
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
if(strlease){
|
||||
memset(strlease, 0, 1000);
|
||||
free(strlease);
|
||||
}
|
||||
ret = RET_INPUTERR;
|
||||
goto INPUT_ERROR;
|
||||
}
|
||||
if(strlease){
|
||||
memset(strlease, 0, 1000);
|
||||
free(strlease);
|
||||
}
|
||||
/*lease 是0,就是恢复默认,需要删除配置文件中的max-lease-time*/
|
||||
memset(cmd, 0, len+1);
|
||||
snprintf(cmd, len, "sed -r -i 's/max-lease-time[ \\t0-9]+;/#/g' /tmp/subconf.conf");
|
||||
system(cmd);
|
||||
system("sed -r -i '/#/d' /tmp/subconf.conf");
|
||||
if(strcmp(lease->valuestring, "0")){
|
||||
memset(cmd, 0, len+1);
|
||||
snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/[ \\t]*\\}/ max-lease-time %s;\\n\\}/g' /tmp/subconf.conf", lease->valuestring); //测试的时候看一下替换的\\}
|
||||
system(cmd);
|
||||
}
|
||||
}
|
||||
//从临时文件读取字符串,写入原配置文件
|
||||
char *conf_after;
|
||||
char *temp_conf_file = "/tmp/subconf.conf";
|
||||
conf_after = getfileall_with_linefeed(temp_conf_file);
|
||||
if(NULL != conf_after)
|
||||
{
|
||||
memset(cmd, 0, len+1);
|
||||
snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/[ \\t]*subnet[ \\t]+%s[ \\t]+netmask[ \\t]+%s[ \\t]*\\{[a-zA-Z#:; \\t\\n0-9\\.\\,\\-]+\\}/%s/g' /etc/dhcp/dhcpd.conf", segment, mask, conf_after);
|
||||
system(cmd);
|
||||
}
|
||||
if(conf_after){
|
||||
free(conf_after);
|
||||
conf_after = NULL;
|
||||
}
|
||||
system("rm -rf /tmp/subconf.conf");
|
||||
}
|
||||
}
|
||||
/*运行配置文件*/
|
||||
system("service isc-dhcp-server restart");
|
||||
|
||||
INPUT_ERROR:
|
||||
if(cmd)
|
||||
{
|
||||
memset(cmd, 0, len + 1);
|
||||
free(cmd);
|
||||
}
|
||||
if(segment){
|
||||
free(segment);
|
||||
}
|
||||
if(mask){
|
||||
free(mask);
|
||||
}
|
||||
if(router){
|
||||
free(router);
|
||||
}
|
||||
//ret = RET_INPUTERR;
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret_code dhcp_subnet_config_get(uint source,
|
||||
pointer input, int input_len,
|
||||
pointer output, int *output_len)
|
||||
{
|
||||
ret_code ret = RET_OK;
|
||||
cJSON *root=NULL, *interface=NULL;
|
||||
/*json解析*/
|
||||
root = cJSON_Parse(input);
|
||||
if(NULL == root)
|
||||
{
|
||||
return RET_INPUTERR;
|
||||
}
|
||||
interface = cJSON_GetObjectItem(root, "interface");
|
||||
if(NULL == interface)
|
||||
{
|
||||
return RET_INPUTERR;
|
||||
}
|
||||
//获取接口网段和掩码
|
||||
char *segment = get_interface_subnet(interface->valuestring);
|
||||
if(NULL == segment){
|
||||
return RET_INPUTERR;
|
||||
}
|
||||
char *mask = get_interface_mask(interface->valuestring);
|
||||
if(NULL == mask){
|
||||
return RET_INPUTERR;
|
||||
}
|
||||
|
||||
//读取配置文件
|
||||
char *fname="/etc/dhcp/dhcpd.conf";
|
||||
FILE *fp;
|
||||
char *subnet=NULL, *netmask=NULL, *range=NULL, *dns=NULL, *submask=NULL, *routers=NULL, *lease=NULL;
|
||||
cJSON *json = NULL;
|
||||
char *out = NULL;
|
||||
char line[1000];
|
||||
if ((fp=fopen(fname,"r"))==NULL){
|
||||
printf("打开文件%s错误\n",fname);
|
||||
return RET_ERR;
|
||||
}
|
||||
fseek(fp,0,SEEK_END);
|
||||
rewind(fp);
|
||||
|
||||
subnet = (char *)malloc(20);
|
||||
memset(subnet, 0, 20);
|
||||
netmask = (char *)malloc(20);
|
||||
memset(netmask, 0, 20);
|
||||
range = (char *)malloc(40);
|
||||
memset(range, 0, 40);
|
||||
dns = (char *)malloc(40);
|
||||
memset(dns, 0, 40);
|
||||
submask = (char *)malloc(20);
|
||||
memset(submask, 0, 20);
|
||||
routers= (char *)malloc(20);
|
||||
memset(routers, 0, 20);
|
||||
lease = (char *)malloc(1000);
|
||||
memset(lease, 0, 1000);
|
||||
|
||||
root = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(root, "segment", segment);
|
||||
cJSON_AddStringToObject(root, "netmask", mask);
|
||||
|
||||
while((fgets(line,1000,fp))!=NULL){
|
||||
char *startSUB = strstr(line, "subnet");
|
||||
char *startMASK = strstr(line, "netmask");
|
||||
char *endMASK = strstr(line, "{");
|
||||
if((startSUB!=NULL) && (startMASK!=NULL) && (endMASK!=NULL)){
|
||||
memcpy(subnet,startSUB+7,startMASK-startSUB-8);
|
||||
memcpy(netmask,startMASK+8,endMASK-startMASK-9);
|
||||
if(!strcmp(subnet, segment) && !strcmp(netmask, mask)){
|
||||
memset(range, 0, 40);
|
||||
memset(dns, 0, 40);
|
||||
memset(submask, 0, 20);
|
||||
memset(routers, 0, 20);
|
||||
memset(lease, 0, 1000);
|
||||
}
|
||||
}
|
||||
if((subnet!= NULL) || (netmask!=NULL)){
|
||||
//不相等
|
||||
if(strcmp(subnet, segment) || strcmp(netmask, mask)){
|
||||
continue;
|
||||
}
|
||||
}
|
||||
char *startRANGE = strstr(line, "range");
|
||||
char *endRANGE = strstr(line, ";");
|
||||
if((startRANGE!=NULL) && (endRANGE!=NULL)){
|
||||
memcpy(range,startRANGE+6,endRANGE-startRANGE-6);
|
||||
}
|
||||
char *startDNS = strstr(line, "option domain-name-servers");
|
||||
char *endDNS = strstr(line, ";");
|
||||
if((startDNS!=NULL) && (endDNS!=NULL)){
|
||||
memcpy(dns,startDNS+27,endDNS-startDNS-27);
|
||||
}
|
||||
char *startSUNMASK = strstr(line, "option subnet-mask");
|
||||
char *endSUBMASK = strstr(line, ";");
|
||||
if((startSUNMASK!=NULL) && (endSUBMASK!=NULL)){
|
||||
memcpy(submask,startSUNMASK+19,endSUBMASK-startSUNMASK-19);
|
||||
}
|
||||
char *startROUTERS = strstr(line, "option routers");
|
||||
char *endROUTERS = strstr(line, ";");
|
||||
if((startROUTERS!=NULL) && (endROUTERS!=NULL)){
|
||||
memcpy(routers,startROUTERS+15,endROUTERS-startROUTERS-15);
|
||||
}
|
||||
char *startLEASE = strstr(line, "max-lease-time");
|
||||
char *endLEASE = strstr(line, ";");
|
||||
if((startLEASE!=NULL) && (endLEASE!=NULL)){
|
||||
memcpy(lease,startLEASE+15,endLEASE-startLEASE-15);
|
||||
}
|
||||
}
|
||||
if(range != NULL){
|
||||
cJSON_AddStringToObject(root, "range", range);
|
||||
memset(range, 0, 40);
|
||||
free(range);
|
||||
}
|
||||
if(dns != NULL){
|
||||
cJSON_AddStringToObject(root, "domain-name-servers", dns);
|
||||
memset(dns, 0, 40);
|
||||
free(dns);
|
||||
}
|
||||
if(submask != NULL){
|
||||
cJSON_AddStringToObject(root, "subnet-mask", submask);
|
||||
memset(submask, 0, 20);
|
||||
free(submask);
|
||||
}
|
||||
if(routers != NULL){
|
||||
cJSON_AddStringToObject(root, "routers", routers);
|
||||
memset(routers, 0, 20);
|
||||
free(routers);
|
||||
}
|
||||
if(lease != NULL){
|
||||
cJSON_AddStringToObject(root, "max-lease-time", lease);
|
||||
memset(lease, 0, 1000);
|
||||
free(lease);
|
||||
}
|
||||
if(subnet != NULL){
|
||||
memset(subnet, 0, 20);
|
||||
free(subnet);
|
||||
}
|
||||
if(netmask != NULL){
|
||||
memset(netmask, 0, 20);
|
||||
free(netmask);
|
||||
}
|
||||
fclose(fp);
|
||||
out = cJSON_PrintUnformatted(root);
|
||||
*output_len = strlen(out) + 1;
|
||||
memcpy(output, out, *output_len);
|
||||
free(out);
|
||||
return ret;
|
||||
}
|
||||
|
|
@ -11,6 +11,13 @@
|
|||
#include "../web_config/authfree.h"
|
||||
#include "../web_config/auth_parameters.h"
|
||||
|
||||
#include "dhcp_client_config.h"
|
||||
#include "dhcp_host_config.h"
|
||||
#include "dhcp_lib.h"
|
||||
#include "dhcp_relay_config.h"
|
||||
#include "dhcp_shared_network_config.h"
|
||||
#include "dhcp_subnet_config.h"
|
||||
#include "dhcp_dhcpd_lease.h"
|
||||
|
||||
#define CONFIG_INIT_ARRAY \
|
||||
{\
|
||||
|
@ -162,6 +169,60 @@
|
|||
log_file_config_proc, \
|
||||
NULL, \
|
||||
NULL \
|
||||
},\
|
||||
{\
|
||||
DHCP_HOST_CONFIG, \
|
||||
CONFIG_FROM_WEB, \
|
||||
FALSE, \
|
||||
dhcp_host_config_chk, \
|
||||
dhcp_host_config_proc, \
|
||||
dhcp_host_config_get, \
|
||||
NULL \
|
||||
},\
|
||||
{\
|
||||
DHCP_SUBNET_CONFIG, \
|
||||
CONFIG_FROM_WEB, \
|
||||
FALSE, \
|
||||
dhcp_subnet_config_chk, \
|
||||
dhcp_subnet_config_proc, \
|
||||
dhcp_subnet_config_get, \
|
||||
NULL \
|
||||
},\
|
||||
{\
|
||||
DHCP_RELAY_CONFIG, \
|
||||
CONFIG_FROM_WEB, \
|
||||
FALSE, \
|
||||
dhcp_relay_config_chk, \
|
||||
dhcp_relay_config_proc, \
|
||||
NULL, \
|
||||
NULL \
|
||||
},\
|
||||
{\
|
||||
DHCP_CLIENT_CONFIG, \
|
||||
CONFIG_FROM_WEB, \
|
||||
FALSE, \
|
||||
dhcp_client_config_chk, \
|
||||
dhcp_client_config_proc, \
|
||||
dhcp_client_get, \
|
||||
NULL \
|
||||
},\
|
||||
{\
|
||||
DHCP_DHCPD_LEASE, \
|
||||
CONFIG_FROM_WEB, \
|
||||
FALSE, \
|
||||
dhcp_dhcpd_lease_config_chk, \
|
||||
NULL, \
|
||||
NULL, \
|
||||
dhcp_dhcpd_lease_get_all \
|
||||
},\
|
||||
{\
|
||||
DHCP_SHARED_NETWORK_CONFIG, \
|
||||
CONFIG_FROM_WEB, \
|
||||
FALSE, \
|
||||
dhcp_shared_network_config_chk, \
|
||||
dhcp_shared_network_config_proc, \
|
||||
NULL, \
|
||||
dhcp_shared_network_config_get_all \
|
||||
}\
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
#ifndef DHCP_CLIENT_CONFIG_H_
|
||||
#define DHCP_CLIENT_CONFIG_H_
|
||||
#include "dhcp_lib.h"
|
||||
|
||||
|
||||
ret_code dhcp_client_config_chk(uint source, uint *config_type,
|
||||
pointer input, int *input_len,
|
||||
pointer output, int *output_len);
|
||||
|
||||
ret_code dhcp_client_config_proc(uint source, uint config_type,
|
||||
pointer input, int input_len,
|
||||
pointer output, int *output_len);
|
||||
|
||||
ret_code dhcp_client_get(uint source,
|
||||
pointer input, int input_len,
|
||||
pointer output, int *output_len);
|
||||
#endif
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
#ifndef DHCP_DHCPD_LEASE_H_
|
||||
#define DHCP_DHCPD_LEASE_H_
|
||||
#include "dhcp_lib.h"
|
||||
|
||||
|
||||
ret_code dhcp_dhcpd_lease_config_chk(uint source,uint *config_type,
|
||||
pointer input, int *input_len,
|
||||
pointer output, int *output_len);
|
||||
|
||||
ret_code dhcp_dhcpd_lease_get_all(uint source, pointer output, int *output_len);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
#ifndef DHCP_HOST_CONFIG_H_
|
||||
#define DHCP_HOST_CONFIG_H_
|
||||
#include "dhcp_lib.h"
|
||||
|
||||
|
||||
|
||||
/*
|
||||
struct host {
|
||||
char *host_name;
|
||||
char *hardware_ethernet;
|
||||
char *fixed_address;
|
||||
}*/
|
||||
|
||||
ret_code dhcp_host_config_chk(uint source, uint *config_type,
|
||||
pointer input, int *input_len,
|
||||
pointer output, int *output_len);
|
||||
|
||||
ret_code dhcp_host_config_proc(uint source, uint config_type,
|
||||
pointer input, int input_len,
|
||||
pointer output, int *output_len);
|
||||
|
||||
ret_code dhcp_host_config_get(uint source,
|
||||
pointer input, int input_len,
|
||||
pointer output, int *output_len);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,62 @@
|
|||
#ifndef DHCP_LIB_H_
|
||||
#define DHCP_LIB_H_
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <net/if.h>
|
||||
#include <cjson/cJSON.h>
|
||||
#include "rpc_common.h"
|
||||
#include <regex.h>
|
||||
#include "ipconfig.h"
|
||||
|
||||
#include "configm.h"
|
||||
#include "rpc.h"
|
||||
#include "netconfig.h"
|
||||
|
||||
char *getfileall(char *fname);
|
||||
|
||||
char *getfilefirstline(char *fname);
|
||||
|
||||
|
||||
char *getfileall_with_linefeed(char *fname);
|
||||
|
||||
int check_name(char *name);
|
||||
|
||||
int check_mac(char *mac);
|
||||
|
||||
int file_consist_str(char *fname, char *str);
|
||||
|
||||
int check_range(char *range, char *mask, char *subnet);
|
||||
|
||||
int check_dns(char *dns);
|
||||
|
||||
int check_lease(char *lease);
|
||||
|
||||
int check_ip(char *ip);
|
||||
|
||||
|
||||
char *get_interface_subnet(char *interface);
|
||||
|
||||
char *get_interface_ip(char *interface) ;
|
||||
|
||||
char *get_interface_mask(char *interface);
|
||||
|
||||
int check_servers(char *servers);
|
||||
|
||||
|
||||
int check_segment(char *segment, char *mask);
|
||||
|
||||
int check_mask(char *mask);
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
#ifndef DHCP_RELAY_CONFIG_H_
|
||||
#define DHCP_RELAY_CONFIG_H_
|
||||
#include "dhcp_lib.h"
|
||||
|
||||
|
||||
|
||||
|
||||
ret_code dhcp_relay_config_chk(uint source,uint *config_type,
|
||||
pointer input, int *input_len,
|
||||
pointer output, int *output_len);
|
||||
|
||||
ret_code dhcp_relay_config_proc(uint source, uint config_type,
|
||||
pointer input, int input_len,
|
||||
pointer output, int *output_len);
|
||||
|
||||
|
||||
#endif
|
|
@ -0,0 +1,17 @@
|
|||
#ifndef DHCP_SHARED_NETWORK_CONFIG_H_
|
||||
#define DHCP_SHARED_NETWORK_CONFIG_H_
|
||||
#include "dhcp_lib.h"
|
||||
|
||||
|
||||
ret_code dhcp_shared_network_config_chk(uint source, uint *config_type,
|
||||
pointer input, int *input_len,
|
||||
pointer output, int *output_len);
|
||||
|
||||
ret_code dhcp_shared_network_config_proc(uint source, uint config_type,
|
||||
pointer input, int input_len,
|
||||
pointer output, int *output_len);
|
||||
|
||||
ret_code dhcp_shared_network_config_get_all(uint source, pointer output, int *output_len);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
#ifndef DHCP_SUBNET_CONFIG_H_
|
||||
#define DHCP_SUBNET_CONFIG_H_
|
||||
#include "dhcp_lib.h"
|
||||
|
||||
|
||||
|
||||
ret_code dhcp_subnet_config_chk(uint source, uint *config_type,
|
||||
pointer input, int *input_len,
|
||||
pointer output, int *output_len);
|
||||
|
||||
ret_code dhcp_subnet_config_proc(uint source, uint config_type,
|
||||
pointer input, int input_len,
|
||||
pointer output, int *output_len);
|
||||
|
||||
ret_code dhcp_subnet_config_get(uint source,
|
||||
pointer input, int input_len,
|
||||
pointer output, int *output_len);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
Loading…
Reference in New Issue