1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| typedef struct dhcp_info { unsigned int enable; unsigned int lease; string ipaddr; string netmask; string start_ip; string end_ip; } dhcp_info;
int my_lan::dnsmasq_config() { FILE *fp; char cmd[128];
if ((fp = fopen(DNSMASQ_DHCPS_CONFIG_FILE, "w")) != NULL) { if(1 == mdhcp_info.enable) { fprintf(fp, "port=53\n");
fprintf(fp, "bind-interfaces\n"); fprintf(fp, "interface=%s\n", IFNAME); fprintf(fp, "dhcp-range=%s,%s,%dh\n", mdhcp_info.start_ip.c_str(), mdhcp_info.end_ip.c_str(), mdhcp_info.lease); fprintf(fp, "dhcp-option=1,%s\n", mdhcp_info.netmask.c_str()); fprintf(fp, "dhcp-option=3,%s\n", mdhcp_info.ipaddr.c_str()); fprintf(fp, "dhcp-option=6,%s\n", mdhcp_info.ipaddr.c_str()); fprintf(fp, "dhcp-authoritative\n"); fprintf(fp, "dhcp-leasefile=%s\n", LEASES_FILE); fprintf(fp, "resolv-file=%s\n", DNS_FILE); fprintf(fp, "address=/%s/%s\n", HOSTNAME, mdhcp_info.ipaddr.c_str()); fflush(fp); fclose(fp); } else { fprintf(fp, "port=53\n"); fprintf(fp, "resolv-file=%s\n", DNS_FILE); fprintf(fp, "address=/%s/%s\n", MYNAME, mdhcp_info.ipaddr.c_str()); fflush(fp); fclose(fp); } } else { printf("open %s failed", DNSMASQ_DHCPS_CONFIG_FILE); return -1; }
memset(cmd, 0x00, sizeof(cmd)); sprintf(cmd, "ifconfig %s %s netmask %s", IFNAME, mdhcp_info.ipaddr.c_str(), mdhcp_info.netmask.c_str()); if (system(cmd) == -1) { printf("cmd:%s failed", cmd); return -1; }
system("killall dnsmasq");
memset(cmd, 0x00, sizeof(cmd)); sprintf(cmd, "/usr/bin/dnsmasq -C %s -k -x %s &", DNSMASQ_DHCPS_CONFIG_FILE, DNSMASQ_PID_FILE); if (system(cmd) == -1) { printf("cmd:%s failed", cmd); return -1; }
return 0; }
|