2019-08-21 10:32:05 +00:00
|
|
|
|
#include "dhcp_lib.h"
|
|
|
|
|
|
2019-09-02 07:56:35 +00:00
|
|
|
|
ret_code dhcp_config_init(void)
|
2019-08-28 08:28:37 +00:00
|
|
|
|
{
|
|
|
|
|
ret_code ret = RET_OK;
|
|
|
|
|
ret = br_event_register(BR_DELETE_EVENT_PRE,del_interface_dhcp_cb);
|
2019-09-02 07:50:40 +00:00
|
|
|
|
if(ret != RET_OK){
|
|
|
|
|
printf("register failed.\n");
|
2019-09-02 07:56:35 +00:00
|
|
|
|
return ret;
|
2019-09-02 07:50:40 +00:00
|
|
|
|
}
|
2019-09-02 07:56:35 +00:00
|
|
|
|
return ret;
|
2019-08-28 08:28:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int del_interface_dhcp_cb(BR_EVENT_TYPE event_type, br_event_t event_arg)
|
|
|
|
|
{
|
|
|
|
|
ret_code ret = RET_OK;
|
|
|
|
|
char *segment = get_interface_subnet(event_arg.br_name);
|
|
|
|
|
if(NULL == segment){
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
char *mask = get_interface_mask(event_arg.br_name);
|
|
|
|
|
if(NULL == mask){
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
int len = 500;
|
|
|
|
|
char *cmd = (char *)malloc(len + 1);
|
|
|
|
|
if(NULL == cmd)
|
|
|
|
|
{
|
|
|
|
|
return RET_ERR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(cmd, 0, len + 1);
|
|
|
|
|
snprintf(cmd, len, "sed -r -i 's/%s //g' /etc/default/isc-dhcp-server", event_arg.br_name);
|
|
|
|
|
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");
|
|
|
|
|
if(segment){
|
|
|
|
|
free(segment);
|
|
|
|
|
}
|
|
|
|
|
if(mask){
|
|
|
|
|
free(mask);
|
|
|
|
|
}
|
2019-09-02 10:08:10 +00:00
|
|
|
|
if(cmd){
|
|
|
|
|
free(cmd);
|
|
|
|
|
}
|
2019-08-28 08:28:37 +00:00
|
|
|
|
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>*/
|
2019-09-09 04:25:10 +00:00
|
|
|
|
//system("service isc-dhcp-server restart");
|
|
|
|
|
system("systemctl restart isc-dhcp-server.service");
|
2019-08-28 08:28:37 +00:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
2019-08-21 10:32:05 +00:00
|
|
|
|
|
|
|
|
|
char *getfileall(char *fname)
|
|
|
|
|
{
|
|
|
|
|
if(NULL == fname){
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
FILE *fp;
|
|
|
|
|
char *str;
|
2019-09-09 04:25:10 +00:00
|
|
|
|
char txt[1000] = {0};
|
2019-08-21 10:32:05 +00:00
|
|
|
|
int filesize;
|
|
|
|
|
if ((fp=fopen(fname,"r"))==NULL){
|
2019-09-09 04:25:10 +00:00
|
|
|
|
printf("open file %s failed\n",fname);
|
2019-08-21 10:32:05 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fseek(fp,0,SEEK_END);
|
|
|
|
|
|
|
|
|
|
filesize = ftell(fp);
|
|
|
|
|
str=(char *)malloc(filesize);
|
2019-09-09 04:25:10 +00:00
|
|
|
|
if(!str){
|
|
|
|
|
fclose(fp);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
memset(str, 0, filesize);
|
2019-08-21 10:32:05 +00:00
|
|
|
|
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;
|
2019-09-09 04:25:10 +00:00
|
|
|
|
char txt[1000] = {0};
|
2019-08-21 10:32:05 +00:00
|
|
|
|
int filesize;
|
|
|
|
|
if ((fp=fopen(fname,"r"))==NULL){
|
2019-09-09 04:25:10 +00:00
|
|
|
|
printf("open file %s failed\n",fname);
|
2019-08-21 10:32:05 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fseek(fp,0,SEEK_END);
|
|
|
|
|
|
|
|
|
|
filesize = ftell(fp);
|
|
|
|
|
str=(char *)malloc(filesize);
|
2019-09-09 04:25:10 +00:00
|
|
|
|
if(!str){
|
|
|
|
|
fclose(fp);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
memset(str, 0, filesize);
|
2019-08-21 10:32:05 +00:00
|
|
|
|
|
|
|
|
|
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;
|
2019-09-09 04:25:10 +00:00
|
|
|
|
char line[1000] = {0};
|
2019-08-21 10:32:05 +00:00
|
|
|
|
int filesize;
|
|
|
|
|
if ((fp=fopen(fname,"r"))==NULL){
|
2019-09-09 04:25:10 +00:00
|
|
|
|
printf("open file %s failed\n",fname);
|
2019-08-21 10:32:05 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fseek(fp,0,SEEK_END);
|
|
|
|
|
|
|
|
|
|
filesize = ftell(fp);
|
|
|
|
|
str=(char *)malloc(filesize+100);
|
2019-09-09 04:25:10 +00:00
|
|
|
|
if(!str){
|
|
|
|
|
fclose(fp);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2019-09-09 04:32:59 +00:00
|
|
|
|
memset(str, 0, filesize+100);
|
2019-08-21 10:32:05 +00:00
|
|
|
|
|
|
|
|
|
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<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>+<2B><>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD>
|
|
|
|
|
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<><32>Ψһ<CEA8><D2BB><EFBFBD><EFBFBD>add<64><64>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2019-09-09 04:25:10 +00:00
|
|
|
|
char ebuf[128] = {0};
|
|
|
|
|
regmatch_t pmatch[1] = {0};
|
2019-08-21 10:32:05 +00:00
|
|
|
|
int nmatch = 10;
|
|
|
|
|
regex_t reg;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
status = regcomp(®, pattern, cflags);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
|
|
|
|
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);//ִ<><D6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD>ıȽ<C4B1>,
|
|
|
|
|
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){
|
2019-09-09 04:25:10 +00:00
|
|
|
|
if(!fname || !str){
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2019-08-21 10:32:05 +00:00
|
|
|
|
//<2F><>ȡ<EFBFBD>ļ<EFBFBD>
|
|
|
|
|
FILE *fp;
|
|
|
|
|
//char *name, *mac, *ip;
|
2019-09-09 04:25:10 +00:00
|
|
|
|
char line[1000] = {0};
|
2019-08-21 10:32:05 +00:00
|
|
|
|
if ((fp=fopen(fname,"r"))==NULL){
|
2019-09-09 04:25:10 +00:00
|
|
|
|
printf("open file %s failed\n",fname);
|
2019-08-21 10:32:05 +00:00
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
fseek(fp,0,SEEK_END);
|
|
|
|
|
rewind(fp);
|
|
|
|
|
|
|
|
|
|
while((fgets(line,1000,fp))!=NULL){
|
|
|
|
|
char *p = strstr(line, str);
|
|
|
|
|
if(p){
|
2019-09-02 10:08:10 +00:00
|
|
|
|
fclose(fp);
|
2019-08-21 10:32:05 +00:00
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-02 10:08:10 +00:00
|
|
|
|
fclose(fp);
|
2019-08-21 10:32:05 +00:00
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int check_range(char *range, char *mask, char *subnet){
|
|
|
|
|
if((NULL==range) || (NULL==mask) || (NULL==subnet)){
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
int len = strlen(range);
|
2019-09-09 06:34:26 +00:00
|
|
|
|
char rangeARR[len+1];
|
|
|
|
|
rangeARR[len] = 0;
|
2019-08-21 10:32:05 +00:00
|
|
|
|
strcpy(rangeARR, range);
|
|
|
|
|
|
|
|
|
|
//1<><31><EFBFBD>ֳ<EFBFBD><D6B3><EFBFBD><EFBFBD><EFBFBD>ip & <20>ж<EFBFBD><D0B6><EFBFBD>ip
|
|
|
|
|
struct in_addr low, high, subnetadd, maskaddr;
|
2019-09-09 04:25:10 +00:00
|
|
|
|
char *item = NULL;
|
2019-08-21 10:32:05 +00:00
|
|
|
|
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<><32><EFBFBD><EFBFBD>subnet<65><74>Χ<EFBFBD><CEA7>
|
|
|
|
|
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<><33><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ǰһ<C7B0><D2BB><EFBFBD><EFBFBD>
|
2019-09-09 04:25:10 +00:00
|
|
|
|
char lowARR[15] = {0};
|
2019-08-21 10:32:05 +00:00
|
|
|
|
strcpy(lowARR, inet_ntoa(low));
|
2019-09-09 04:25:10 +00:00
|
|
|
|
char highARR[15] = {0};
|
2019-08-21 10:32:05 +00:00
|
|
|
|
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);
|
2019-09-09 06:34:26 +00:00
|
|
|
|
char dnsARR[len+1];
|
|
|
|
|
dnsARR[len] = 0;
|
2019-08-21 10:32:05 +00:00
|
|
|
|
strcpy(dnsARR, dns);
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>DNS<4E><53><EFBFBD>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD>ip
|
2019-09-09 04:25:10 +00:00
|
|
|
|
char *item = NULL;
|
2019-08-21 10:32:05 +00:00
|
|
|
|
item = strtok(dnsARR, ",");
|
|
|
|
|
if(NULL == item){
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
if(!check_ip(item)){
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
item = strtok(NULL, ",");
|
|
|
|
|
//ֻ<><D6BB>һ<EFBFBD><D2BB>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){
|
2019-09-09 04:25:10 +00:00
|
|
|
|
if(!lease){
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2019-08-21 10:32:05 +00:00
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>־Ϳ<D6BE><CDBF><EFBFBD>
|
|
|
|
|
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;
|
2019-09-09 04:25:10 +00:00
|
|
|
|
char *ip = NULL, *mask = NULL;
|
2019-08-21 10:32:05 +00:00
|
|
|
|
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);
|
2019-09-02 07:56:35 +00:00
|
|
|
|
free(cmd);
|
|
|
|
|
|
2019-08-21 10:32:05 +00:00
|
|
|
|
return getfilefirstline("/tmp/ip");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *get_interface_mask(char *interface) {
|
|
|
|
|
if(NULL == interface){
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
char *cmd = (char *)malloc(101);
|
2019-09-09 04:25:10 +00:00
|
|
|
|
if(!cmd){
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2019-08-21 10:32:05 +00:00
|
|
|
|
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);
|
2019-09-02 07:56:35 +00:00
|
|
|
|
free(cmd);
|
|
|
|
|
|
2019-08-21 10:32:05 +00:00
|
|
|
|
return getfilefirstline("/tmp/mask");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int check_servers(char *servers){
|
|
|
|
|
if(NULL == servers){
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
int len = strlen(servers);
|
2019-09-09 06:34:26 +00:00
|
|
|
|
char arr[len+1];
|
|
|
|
|
arr[len] = 0;
|
2019-08-21 10:32:05 +00:00
|
|
|
|
strcpy(arr, servers);
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>servers<72><73><EFBFBD>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD>ip
|
2019-09-09 04:25:10 +00:00
|
|
|
|
char *item = NULL;
|
2019-08-21 10:32:05 +00:00
|
|
|
|
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){
|
2019-09-09 04:25:10 +00:00
|
|
|
|
if(!segment || !mask){
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2019-08-21 10:32:05 +00:00
|
|
|
|
//1<><31><EFBFBD><EFBFBD>ip<69><70><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>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))
|
|
|
|
|
{
|
2019-09-09 04:25:10 +00:00
|
|
|
|
unsigned int b = 0, i, n[4] = {0};
|
2019-08-21 10:32:05 +00:00
|
|
|
|
sscanf(mask, "%u.%u.%u.%u", &n[3], &n[2], &n[1], &n[0]);
|
|
|
|
|
for(i = 0; i < 4; ++i){ //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>32λ<32><EFBFBD><DEB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
b += n[i] << (i * 8);
|
|
|
|
|
}
|
|
|
|
|
b = ~b + 1;
|
|
|
|
|
if((b & (b - 1)) == 0){ //<2F>ж<EFBFBD><D0B6>Ƿ<EFBFBD>Ϊ2^n
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-18 06:55:02 +00:00
|
|
|
|
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);
|
|
|
|
|
for(i = 0; i < wan_count; i++)
|
|
|
|
|
{
|
|
|
|
|
strcat(wanif, " ");
|
|
|
|
|
strcat(wanif, ifname[i]);
|
|
|
|
|
}
|
|
|
|
|
free(ifname);
|
|
|
|
|
if(!strcmp(wanif, "")){
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
return wanif;
|
|
|
|
|
}
|