This commit is contained in:
gechangwei 2019-09-18 16:13:55 +08:00
commit 31a50ce4d9
5 changed files with 137 additions and 7 deletions

View File

@ -5,6 +5,7 @@ ret_code dhcp_client_config_chk(uint source, uint *config_type,
pointer output, int *output_len)
{
ret_code ret = RET_OK;
/*
cJSON *root=NULL, *operate=NULL;
root = cJSON_Parse(input);
if(NULL == root)
@ -15,7 +16,7 @@ ret_code dhcp_client_config_chk(uint source, uint *config_type,
operate = cJSON_GetObjectItem(root, "operate");
if(operate){
*config_type = CM_CONFIG_SET;
}
}*/
return ret;
}

View File

@ -503,3 +503,23 @@ int check_mask(char *mask)
return FALSE;
}
char *wan_if_str(){
char ifname[MAX_IF_NUM][INTERFACE_NAMSIZ];
memset(ifname, 0, sizeof(ifname));
//printf("ifname size: %d\n", sizeof(ifname));
int wan_count = if_wan_get(ifname, MAX_IF_NUM);
char *wanif;
memset(wanif, 0, sizeof(ifname)+wan_count+1);
//printf("wan get cnt %d\n", wan_count);
int i;
for(i = 0; i < wan_count; i++)
{
strcat(wanif, " ");
strcat(wanif, ifname[i]);
}
free(ifname);
if(!strcmp(wanif, "")){
return NULL;
}
return wanif;
}

View File

@ -5,7 +5,7 @@ ret_code dhcp_relay_config_chk(uint source,uint *config_type,
pointer output, int *output_len)
{
ret_code ret = RET_OK;
*config_type = CM_CONFIG_SET;
//*config_type = CM_CONFIG_SET;
return ret;
}
@ -44,13 +44,24 @@ ret_code dhcp_relay_config_proc(uint source, uint config_type,
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");
system("service isc-dhcp-relay stop");
dhcp_relay_info_save("","");*/
memset(cmd, 0, len + 1);
snprintf(cmd, len, "sed -r -i 's/SERVERS=/#SERVERS/g' /etc/default/isc-dhcp-relay"); //\" 不能写成\\"
system(cmd);
memset(cmd, 0, len + 1);
snprintf(cmd, len, "sed -r -i 's/INTERFACES=/#INTERFACES=/g' /etc/default/isc-dhcp-relay"); //\" 不能写成\\"
system(cmd);
system("sed -r -i '/#/d' /etc/default/isc-dhcp-relay");
system("systemctl stop isc-dhcp-relay.service");
}
if(!strcmp(operate->valuestring, "start relay")){
interfaces = cJSON_GetObjectItem(root, "interfaces");
@ -65,24 +76,52 @@ ret_code dhcp_relay_config_proc(uint source, uint config_type,
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);
if(!wan_if_str()){
ret = RET_INPUTERR;
goto INPUT_ERROR;
}
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); //\" 不能写成\\"
snprintf(cmd, len, "sed -r -i 's/INTERFACES=\"[ \\ta-zA-Z0-9\\.]*\"/INTERFACES=\"%s%s\"/g' /etc/default/isc-dhcp-relay", interfaces->valuestring, wan_if_str()); //\" 不能写成\\"
system(cmd);
/*运行配置文件*/
//system("service isc-dhcp-relay restart");
system("systemctl restart isc-dhcp-relay.service");
dhcp_relay_info_save(servers->valuestring, interfaces->valuestring);*/
if(!wan_if_str()){
ret = RET_INPUTERR;
goto INPUT_ERROR;
}
memset(cmd, 0, len + 1);
snprintf(cmd, len, "echo 'SERVERS=\"%s\"' >> /etc/default/isc-dhcp-relay", servers->valuestring);
system(cmd);
memset(cmd, 0, len + 1);
snprintf(cmd, len, "echo 'INTERFACES=\"%s%s\"' >> /etc/default/isc-dhcp-relay", interfaces->valuestring, wan_if_str());
system(cmd);
//save relay info
memset(cmd, 0, len + 1);
snprintf(cmd, len, "echo '#serversINFO=%s;' >> /etc/default/isc-dhcp-relay", servers->valuestring);
system(cmd);
memset(cmd, 0, len + 1);
snprintf(cmd, len, "echo '#ifINFO=%s;' >> /etc/default/isc-dhcp-relay", interfaces->valuestring);
system(cmd);
//resatrt service
system("systemctl restart isc-dhcp-relay.service");
}
INPUT_ERROR:
if(cmd)
{
memset(cmd, 0, len + 1);
free(cmd);
cmd = NULL;
}
return ret;
@ -91,6 +130,51 @@ ret_code dhcp_relay_config_get_all(uint source, pointer output, int *output_len)
ret_code ret = RET_OK;
char *fname="/etc/default/isc-dhcp-relay";
FILE *fp;
char *servers = NULL, *interfaces = NULL;
cJSON *root = NULL;
char *out = NULL;
char line[1000] = {0};
if ((fp=fopen(fname,"r"))==NULL){
printf("open file %s failed\n",fname);
return RET_ERR;
}
fseek(fp,0,SEEK_END);
rewind(fp);
servers = (char *)malloc(50);
memset(servers, 0, 50);
interfaces = (char *)malloc(1000);
memset(interfaces, 0, 1000);
root = cJSON_CreateObject();
while((fgets(line,1000,fp))!=NULL){
char *startSERVERS = strstr(line, "#serversINFO=");
char *endSERVERS = strstr(line, ";");
if((startSERVERS!=NULL) && (endSERVERS!=NULL)){
memcpy(servers,startSERVERS+13,endSERVERS-startSERVERS-13);
cJSON_AddStringToObject(root, "servers", servers);
}
char *startIF = strstr(line, "#ifINFO=");
char *endIF = strstr(line, ";");
if((startIF!=NULL) && (endIF!=NULL)){
memcpy(interfaces,startIF+8,endIF-startIF-8);
cJSON_AddStringToObject(root, "interfaces", interfaces);
}
}
if(servers != NULL){
free(servers);
servers = NULL;
}
if(interfaces!= NULL){
free(interfaces);
interfaces= NULL;
}
fclose(fp);
out = cJSON_PrintUnformatted(root);
*output_len = strlen(out) + 1;
memcpy(output, out, *output_len);
free(out);
return ret;
}

View File

@ -68,6 +68,7 @@ int check_segment(char *segment, char *mask);
int check_mask(char *mask);
char *wan_if_str();
#endif

View File

@ -47,9 +47,12 @@ typedef enum { WEBM_HANDLE_INVALID_INDEX = -1,
WEBM_HANDLE_CONFIG_HOST_SET,
WEBM_HANDLE_CONFIG_HOST_GET,
WEBM_HANDLE_CONFIG_HOST_GET_ALL,
WEBM_HANDLE_CONFIG_SUBNET_SET,
WEBM_HANDLE_CONFIG_SUBNET_GET,
WEBM_HANDLE_CONFIG_SUBNET_GET_ALL,
WEBM_HANDLE_CONFIG_RELAY_SET,
WEBM_HANDLE_CONFIG_RELAY_GET_ALL,
WEBM_HANDLE_CONFIG_CLIENT_SET,
WEBM_HANDLE_CONFIG_CLIENT_GET,
WEBM_HANDLE_CONFIG_DHCPD_LEASE_GET,
@ -241,6 +244,13 @@ extern int webm_config_send_proc(server *srv, uint32_t config_type, uint64 confg
DHCP_HOST_CONFIG, \
webm_config_send_proc \
}, \
{\
WEBM_HANDLE_CONFIG_HOST_GET_ALL, \
"/FSG-GF/host-get-all", \
CM_CONFIG_GET_ALL, \
DHCP_HOST_CONFIG, \
webm_config_send_proc \
}, \
{\
WEBM_HANDLE_CONFIG_SUBNET_SET, \
"/FSG-CF/subnet-set", \
@ -255,6 +265,13 @@ extern int webm_config_send_proc(server *srv, uint32_t config_type, uint64 confg
DHCP_SUBNET_CONFIG, \
webm_config_send_proc \
}, \
{\
WEBM_HANDLE_CONFIG_SUBNET_GET_ALL, \
"/FSG-GF/subnet-get-all", \
CM_CONFIG_GET_ALL, \
DHCP_SUBNET_CONFIG, \
webm_config_send_proc \
}, \
{\
WEBM_HANDLE_CONFIG_RELAY_SET, \
"/FSG-CF/relay-set", \
@ -262,6 +279,13 @@ extern int webm_config_send_proc(server *srv, uint32_t config_type, uint64 confg
DHCP_RELAY_CONFIG, \
webm_config_send_proc \
}, \
{\
WEBM_HANDLE_CONFIG_RELAY_GET_ALL, \
"/FSG-GF/relay-get-all", \
CM_CONFIG_GET_ALL, \
DHCP_RELAY_CONFIG, \
webm_config_send_proc \
}, \
{\
WEBM_HANDLE_CONFIG_CLIENT_SET, \
"/FSG-CF/client-set", \