Skip to content

Commit

Permalink
Merge branch 'dhcp'
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonsm committed Jul 2, 2020
2 parents 68f41c3 + f694d69 commit 13bb8b1
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 3 deletions.
43 changes: 43 additions & 0 deletions trunk/user/dnsmasq/dnsmasq-2.7x/src/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,49 @@ static void add_hosts_entry(struct crec *cache, struct all_addr *addr, int addrl
add_hosts_cname(cache);
}

inline static int add_dhcp_to_host(char *hostname, struct all_addr *addr, int addrlen, int flags)
{
struct crec *cache;
char *domain_suffix;
int count = 0, revhashsz = daemon->packet_buff_sz / sizeof(struct crec *);
if (option_bool(OPT_EXPAND) && (domain_suffix = (addrlen == INADDRSZ) ? get_domain(addr->addr.addr4) : get_domain6(&addr->addr.addr6)) && (cache = whine_malloc(sizeof(struct crec) + strlen(hostname)+2+strlen(domain_suffix)-SMALLDNAME))) {
strcpy(cache->name.sname, hostname);
strcat(cache->name.sname, ".");
strcat(cache->name.sname, domain_suffix);
cache->ttd = daemon->local_ttl;
cache->flags = flags;
add_hosts_entry(cache, addr, addrlen, SRC_CONFIG, (struct crec **)daemon->packet, revhashsz);
count++;
}
if ((cache = whine_malloc(sizeof(struct crec)))) {
cache->name.namep = hostname;
cache->ttd = daemon->local_ttl;
cache->flags = flags | F_NAMEP;
add_hosts_entry(cache, addr, addrlen, SRC_CONFIG, (struct crec **)daemon->packet, revhashsz);
count++;
}
return count;
}

void dhcp_to_host()
{
int count = 0;
for (struct dhcp_config *conf = daemon->dhcp_conf; conf; conf = conf->next) {
if (conf->hostname) {
if (conf->flags & CONFIG_ADDR)
count += add_dhcp_to_host(conf->hostname, (struct all_addr *)&conf->addr, INADDRSZ, F_HOSTS | F_IMMORTAL | F_FORWARD | F_REVERSE | F_IPV4 | F_CONFIG);
#ifdef HAVE_IPV6
if (conf->flags & CONFIG_ADDR6)
count += add_dhcp_to_host(conf->hostname, (struct all_addr *)&conf->addr6, IN6ADDRSZ, F_HOSTS | F_IMMORTAL | F_FORWARD | F_REVERSE | F_IPV6 | F_CONFIG);
#endif
}
}
if (count) {
rehash(daemon->cachesize + count);
my_syslog(LOG_INFO, _("dhcp to host - %d address"), count);
}
}

static int eatspace(FILE *f)
{
int c, nl = 0;
Expand Down
4 changes: 4 additions & 0 deletions trunk/user/dnsmasq/dnsmasq-2.7x/src/dnsmasq.c
Original file line number Diff line number Diff line change
Expand Up @@ -1472,6 +1472,10 @@ void clear_cache_and_reload(time_t now)
if (option_bool(OPT_ETHERS))
dhcp_read_ethers();
reread_dhcp();
if (option_bool(OPT_DHCP_TO_HOST)) {
void dhcp_to_host();
dhcp_to_host();
}
#ifdef HAVE_INOTIFY
set_dynamic_inotify(AH_DHCP_HST | AH_DHCP_OPT, 0, NULL, 0);
#endif
Expand Down
3 changes: 2 additions & 1 deletion trunk/user/dnsmasq/dnsmasq-2.7x/src/dnsmasq.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ struct event_desc {
#define OPT_MAC_B64 54
#define OPT_MAC_HEX 55
#define OPT_TFTP_APREF_MAC 56
#define OPT_LAST 57
#define OPT_DHCP_TO_HOST 57
#define OPT_LAST 58

/* extra flags for my_syslog, we use a couple of facilities since they are known
not to occupy the same bits as priorities, no matter how syslog.h is set up. */
Expand Down
83 changes: 81 additions & 2 deletions trunk/user/dnsmasq/dnsmasq-2.7x/src/option.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ struct myoption {
#define LOPT_DHCPTTL 348
#define LOPT_TFTP_MTU 349
#define LOPT_REPLY_DELAY 350
#define LOPT_GFWLIST 351
#define LOPT_DHCP_TO_HOST 352

#ifdef HAVE_GETOPT_LONG
static const struct option opts[] =
Expand Down Expand Up @@ -325,6 +327,8 @@ static const struct myoption opts[] =
{ "script-arp", 0, 0, LOPT_SCRIPT_ARP },
{ "dhcp-ttl", 1, 0 , LOPT_DHCPTTL },
{ "dhcp-reply-delay", 1, 0, LOPT_REPLY_DELAY },
{ "gfwlist", 1, 0, LOPT_GFWLIST },
{ "dhcp-to-host", 0, 0, LOPT_DHCP_TO_HOST },
{ NULL, 0, 0, 0 }
};

Expand Down Expand Up @@ -497,6 +501,8 @@ static struct {
{ LOPT_IGNORE_ADDR, ARG_DUP, "<ipaddr>", gettext_noop("Ignore DNS responses containing ipaddr."), NULL },
{ LOPT_DHCPTTL, ARG_ONE, "<ttl>", gettext_noop("Set TTL in DNS responses with DHCP-derived addresses."), NULL },
{ LOPT_REPLY_DELAY, ARG_ONE, "<integer>", gettext_noop("Delay DHCP replies for at least number of seconds."), NULL },
{ LOPT_GFWLIST, ARG_DUP, "<path|domain>[@server][^ipset]", gettext_noop("Gfwlist path or domain to special server (default 8.8.8.8~53) and ipset (default gfwlist, pass ^ only to skip default ipset)"), NULL },
{ LOPT_DHCP_TO_HOST, OPT_DHCP_TO_HOST, NULL, gettext_noop("Keep DHCP hostname valid at all times."), NULL },
{ 0, 0, NULL, NULL, NULL }
};

Expand Down Expand Up @@ -1781,7 +1787,14 @@ static int one_opt(int option, char *arg, char *errstr, char *gen_err, int comma
case LOPT_SERVERS_FILE:
daemon->servers_file = opt_string_alloc(arg);
break;


case LOPT_GFWLIST:
{
void load_gfwlist(char *gfwlist);
load_gfwlist(arg);
}
break;

case 'm': /* --mx-host */
{
int pref = 1;
Expand Down Expand Up @@ -4522,7 +4535,73 @@ void read_servers_file(void)

read_file(daemon->servers_file, f, LOPT_REV_SERV);
}


void add_gfwline(char *gfwline, const char *server, const char *ipset)
{
char *end = gfwline + 1;
while (*end && *end != '#' && *end != '\n' && *end != '\r') end++;
for (char *buf = end; buf >= gfwline; buf--) {
if (*buf == ',' || buf == gfwline) {
if (buf + 1 < end) {
*buf = '/';
*end++ = '/';

#ifdef HAVE_IPSET
if (*ipset) {
strcpy(end, ipset);
one_opt(LOPT_IPSET, buf, _("gfwlist"), _("error"), 0, 0);
end[-1] = '/';
}
#endif
strcpy(end, server);
one_opt('S', buf, _("gfwlist"), _("error"), 0, 0);
}
end = buf;
}
}
}

void load_gfwlist(char *gfwlist)
{
char *cfg_server = NULL, *cfg_ipset = NULL;
for (char *p = gfwlist; *p; p++) {
if (*p == '@') cfg_server = p;
else if (*p == '^') cfg_ipset = p;
}

const char *server, *ipset;
if (cfg_server) {
*cfg_server = 0;
server = cfg_server + 1;
} else {
server = "8.8.8.8~53";
}
if (cfg_ipset) {
*cfg_ipset = 0;
ipset = cfg_ipset + 1;
} else {
ipset = "gfwlist";
}

FILE *f = NULL;
do {
if (*gfwlist == '/') {
if (!(f = fopen(gfwlist, "r"))) {
my_syslog(LOG_ERR, _("cannot read %s: %s"), gfwlist, strerror(errno));
break;
}
for (char buf[MAXDNAME]; fgets(buf+ 1, MAXDNAME - 1, f); add_gfwline(buf, server, ipset));
} else {
char old = gfwlist[-1];
add_gfwline(gfwlist - 1, server, ipset);
gfwlist[-1] = old;
}
} while (0);

if (f) fclose(f);
if (cfg_server) *cfg_server = '@';
if (cfg_ipset) *cfg_ipset = '^';
}

#ifdef HAVE_DHCP
void reread_dhcp(void)
Expand Down
3 changes: 3 additions & 0 deletions trunk/user/scripts/mtd_storage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,9 @@ dhcp-option=252,"\n"
### Log for all queries
#log-queries
### Keep DHCP host name valid at any times
#dhcp-to-host
EOF
if [ -f /usr/bin/vlmcsd ]; then
cat >> "$user_dnsmasq_conf" <<EOF
Expand Down

0 comments on commit 13bb8b1

Please sign in to comment.