+ fprintf(f, "nameserver %s\n", str);
+ }
+
+ vlist_simple_for_each_element(&ip->dns_search, d, node) {
+ fprintf(f, "search %s\n", d->name);
+ }
+}
+
+void
+interface_write_resolv_conf(void)
+{
+ struct interface *iface;
+ char *path = alloca(strlen(resolv_conf) + 5);
+ FILE *f;
+
+ sprintf(path, "%s.tmp", resolv_conf);
+ unlink(path);
+ f = fopen(path, "w");
+ if (!f) {
+ D(INTERFACE, "Failed to open %s for writing\n", path);
+ return;
+ }
+
+ vlist_for_each_element(&interfaces, iface, node) {
+ if (iface->state != IFS_UP)
+ continue;
+
+ if (vlist_simple_empty(&iface->proto_ip.dns_search) &&
+ vlist_simple_empty(&iface->proto_ip.dns_servers) &&
+ vlist_simple_empty(&iface->config_ip.dns_search) &&
+ vlist_simple_empty(&iface->config_ip.dns_servers))
+ continue;
+
+ fprintf(f, "# Interface %s\n", iface->name);
+ write_resolv_conf_entries(f, &iface->config_ip);
+ write_resolv_conf_entries(f, &iface->proto_ip);
+ }
+ fclose(f);
+ if (rename(path, resolv_conf) < 0) {
+ D(INTERFACE, "Failed to replace %s\n", resolv_conf);
+ unlink(path);
+ }
+}
+
+void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
+{
+ struct device_addr *addr;
+ struct device_route *route;
+ struct device *dev;
+
+ ip->enabled = enabled;
+ dev = ip->iface->l3_dev.dev;
+ if (!dev)
+ return;
+
+ vlist_for_each_element(&ip->addr, addr, node) {
+ if (addr->enabled == enabled)
+ continue;
+
+ if (enabled)
+ system_add_address(dev, addr);
+ else
+ system_del_address(dev, addr);
+ addr->enabled = enabled;
+ }
+
+ vlist_for_each_element(&ip->route, route, node) {
+ bool _enabled = enabled;
+
+ if (!enable_route(ip, route))
+ _enabled = false;
+
+ if (route->enabled == _enabled)
+ continue;
+
+ if (_enabled) {
+ if (!(route->flags & DEVROUTE_METRIC))
+ route->metric = ip->iface->metric;
+
+ system_add_route(dev, route);
+ } else
+ system_del_route(dev, route);
+ route->enabled = _enabled;
+ }
+}
+
+void
+interface_ip_update_start(struct interface_ip_settings *ip)
+{
+ vlist_simple_update(&ip->dns_servers);
+ vlist_simple_update(&ip->dns_search);
+ vlist_update(&ip->route);
+ vlist_update(&ip->addr);
+}
+
+void
+interface_ip_update_complete(struct interface_ip_settings *ip)
+{
+ vlist_simple_flush(&ip->dns_servers);
+ vlist_simple_flush(&ip->dns_search);
+ vlist_flush(&ip->route);
+ vlist_flush(&ip->addr);
+}
+
+void
+interface_ip_flush(struct interface_ip_settings *ip)
+{
+ vlist_simple_flush_all(&ip->dns_servers);
+ vlist_simple_flush_all(&ip->dns_search);
+ vlist_flush_all(&ip->route);
+ vlist_flush_all(&ip->addr);
+}
+
+void
+interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
+{
+ ip->iface = iface;
+ ip->enabled = true;
+ vlist_simple_init(&ip->dns_search, struct dns_search_domain, node);
+ vlist_simple_init(&ip->dns_servers, struct dns_server, node);
+ vlist_init(&ip->route, route_cmp, interface_update_proto_route);
+ vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr);