| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489 |
- /**
- * @file ncd.c
- * @author Ambroz Bizjak <ambrop7@gmail.com>
- *
- * @section LICENSE
- *
- * This file is part of BadVPN.
- *
- * BadVPN is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2
- * as published by the Free Software Foundation.
- *
- * BadVPN is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
- #include <stdint.h>
- #include <stdio.h>
- #include <stddef.h>
- #include <string.h>
- #include <misc/version.h>
- #include <misc/loggers_string.h>
- #include <misc/loglevel.h>
- #include <misc/offset.h>
- #include <misc/read_file.h>
- #include <misc/string_begins_with.h>
- #include <misc/ipaddr.h>
- #include <structure/LinkedList2.h>
- #include <system/BLog.h>
- #include <system/BReactor.h>
- #include <system/BSignal.h>
- #include <system/BSocket.h>
- #include <dhcpclient/BDHCPClient.h>
- #include <ncdconfig/NCDConfigParser.h>
- #include <ncd/NCDIfConfig.h>
- #include <ncd/NCDInterfaceMonitor.h>
- #ifndef BADVPN_USE_WINAPI
- #include <system/BLog_syslog.h>
- #endif
- #include <ncd/ncd.h>
- #include <generated/blog_channel_ncd.h>
- #define LOGGER_STDOUT 1
- #define LOGGER_SYSLOG 2
- #define INTERFACE_TYPE_PHYSICAL 1
- #define INTERFACE_STATE_WAITDEPS 1
- #define INTERFACE_STATE_RESETTING 2
- #define INTERFACE_STATE_WAITDEVICE 3
- #define INTERFACE_STATE_WAITLINK 4
- #define INTERFACE_STATE_DHCP 5
- #define INTERFACE_STATE_FINISHED 6
- // command-line options
- struct {
- int help;
- int version;
- int logger;
- #ifndef BADVPN_USE_WINAPI
- char *logger_syslog_facility;
- char *logger_syslog_ident;
- #endif
- int loglevel;
- int loglevels[BLOG_NUM_CHANNELS];
- char *config_file;
- } options;
- struct interface {
- LinkedList2Node list_node; // node in interfaces
- struct NCDConfig_interfaces *conf;
- int type;
- int state;
- BTimer reset_timer;
- LinkedList2 deps_out;
- LinkedList2 deps_in;
- int up;
- int have_dhcp;
- BDHCPClient dhcp;
- LinkedList2 ipv4_addresses;
- LinkedList2 ipv4_routes;
- LinkedList2 ipv4_dns_servers;
- };
- struct dependency {
- struct interface *src;
- struct interface *dst;
- LinkedList2Node src_node;
- LinkedList2Node dst_node;
- };
- struct ipv4_addr_entry {
- LinkedList2Node list_node; // node in interface.ipv4_addresses
- struct ipv4_ifaddr ifaddr;
- };
- struct ipv4_route_entry {
- LinkedList2Node list_node; // node in interface.ipv4_routes
- struct ipv4_ifaddr dest;
- uint32_t gateway;
- int metric;
- };
- struct ipv4_dns_entry {
- LinkedList2Node list_node; // node in interface.ipv4_dns_servers
- uint32_t addr;
- int priority;
- };
- // reactor
- BReactor ss;
- // configuration
- struct NCDConfig_interfaces *configuration;
- // interface monitor
- NCDInterfaceMonitor monitor;
- // interfaces
- LinkedList2 interfaces;
- // number of DNS servers
- size_t num_ipv4_dns_servers;
- static void terminate (void);
- static void print_help (const char *name);
- static void print_version (void);
- static int parse_arguments (int argc, char *argv[]);
- static void signal_handler (void *unused);
- static void load_interfaces (struct NCDConfig_interfaces *conf);
- static void free_interfaces (void);
- static int set_dns_servers (void);
- static int dns_qsort_comparator (const void *v1, const void *v2);
- static struct interface * find_interface (const char *name);
- static void monitor_handler (void *unused, const char *ifname, int if_flags);
- static int interface_init (struct NCDConfig_interfaces *conf);
- static void interface_free (struct interface *iface);
- static void interface_down_to (struct interface *iface, int state);
- static void interface_reset (struct interface *iface);
- static void interface_start (struct interface *iface);
- static void interface_link_up (struct interface *iface);
- static void interface_log (struct interface *iface, int level, const char *fmt, ...);
- static void interface_reset_timer_handler (struct interface *iface);
- static void interface_dhcp_handler (struct interface *iface, int event);
- static void interface_remove_dependencies (struct interface *iface);
- static int interface_add_dependency (struct interface *iface, struct interface *dst);
- static void remove_dependency (struct dependency *d);
- static int interface_dependencies_satisfied (struct interface *iface);
- static void interface_satisfy_incoming_depenencies (struct interface *iface);
- static void interface_unsatisfy_incoming_depenencies (struct interface *iface);
- static int interface_set_up (struct interface *iface);
- static void interface_set_down (struct interface *iface);
- static int interface_configure_ipv4 (struct interface *iface);
- static void interface_deconfigure_ipv4 (struct interface *iface);
- static int interface_add_ipv4_addresses (struct interface *iface);
- static void interface_remove_ipv4_addresses (struct interface *iface);
- static int interface_add_ipv4_routes (struct interface *iface);
- static void interface_remove_ipv4_routes (struct interface *iface);
- static int interface_add_ipv4_dns_servers (struct interface *iface);
- static void interface_remove_ipv4_dns_servers (struct interface *iface);
- static int interface_add_ipv4_addr (struct interface *iface, struct ipv4_ifaddr ifaddr);
- static void interface_remove_ipv4_addr (struct interface *iface, struct ipv4_addr_entry *entry);
- static int interface_add_ipv4_route (struct interface *iface, struct ipv4_ifaddr dest, uint32_t gateway, int metric);
- static void interface_remove_ipv4_route (struct interface *iface, struct ipv4_route_entry *entry);
- static struct ipv4_addr_entry * interface_add_ipv4_addr_entry (struct interface *iface, struct ipv4_ifaddr ifaddr);
- static void interface_remove_ipv4_addr_entry (struct interface *iface, struct ipv4_addr_entry *entry);
- static struct ipv4_route_entry * interface_add_ipv4_route_entry (struct interface *iface, struct ipv4_ifaddr dest, uint32_t gateway, int metric);
- static void interface_remove_ipv4_route_entry (struct interface *iface, struct ipv4_route_entry *entry);
- static struct ipv4_dns_entry * interface_add_ipv4_dns_entry (struct interface *iface, uint32_t addr, int priority);
- static void interface_remove_ipv4_dns_entry (struct interface *iface, struct ipv4_dns_entry *entry);
- int main (int argc, char **argv)
- {
- if (argc <= 0) {
- return 1;
- }
-
- // parse command-line arguments
- if (!parse_arguments(argc, argv)) {
- fprintf(stderr, "Failed to parse arguments\n");
- print_help(argv[0]);
- goto fail0;
- }
-
- // handle --help and --version
- if (options.help) {
- print_version();
- print_help(argv[0]);
- return 0;
- }
- if (options.version) {
- print_version();
- return 0;
- }
-
- // initialize logger
- switch (options.logger) {
- case LOGGER_STDOUT:
- BLog_InitStdout();
- break;
- #ifndef BADVPN_USE_WINAPI
- case LOGGER_SYSLOG:
- if (!BLog_InitSyslog(options.logger_syslog_ident, options.logger_syslog_facility)) {
- fprintf(stderr, "Failed to initialize syslog logger\n");
- goto fail0;
- }
- break;
- #endif
- default:
- ASSERT(0);
- }
-
- // configure logger channels
- for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
- if (options.loglevels[i] >= 0) {
- BLog_SetChannelLoglevel(i, options.loglevels[i]);
- }
- else if (options.loglevel >= 0) {
- BLog_SetChannelLoglevel(i, options.loglevel);
- }
- }
-
- BLog(BLOG_NOTICE, "initializing "GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION);
-
- // initialize sockets
- if (BSocket_GlobalInit() < 0) {
- BLog(BLOG_ERROR, "BSocket_GlobalInit failed");
- goto fail1;
- }
-
- // init time
- BTime_Init();
-
- // init reactor
- if (!BReactor_Init(&ss)) {
- BLog(BLOG_ERROR, "BReactor_Init failed");
- goto fail1;
- }
-
- // setup signal handler
- if (!BSignal_Init()) {
- BLog(BLOG_ERROR, "BSignal_Init failed");
- goto fail2;
- }
- BSignal_Capture();
- if (!BSignal_SetHandler(&ss, signal_handler, NULL)) {
- BLog(BLOG_ERROR, "BSignal_SetHandler failed");
- goto fail2;
- }
-
- // read config file
- uint8_t *file;
- size_t file_len;
- if (!read_file(options.config_file, &file, &file_len)) {
- BLog(BLOG_ERROR, "failed to read config file");
- goto fail3;
- }
-
- // parse config file
- if (!NCDConfigParser_Parse((char *)file, file_len, &configuration)) {
- BLog(BLOG_ERROR, "NCDConfigParser_Parse failed");
- free(file);
- goto fail3;
- }
-
- // fee config file memory
- free(file);
-
- // init interface monitor
- if (!NCDInterfaceMonitor_Init(&monitor, &ss, monitor_handler, NULL)) {
- BLog(BLOG_ERROR, "NCDInterfaceMonitor_Init failed");
- goto fail4;
- }
-
- // init interfaces list
- LinkedList2_Init(&interfaces);
-
- // set no DNS servers
- num_ipv4_dns_servers = 0;
- if (!set_dns_servers()) {
- BLog(BLOG_ERROR, "failed to set no DNS servers");
- goto fail5;
- }
-
- // init interfaces
- load_interfaces(configuration);
-
- goto event_loop;
-
- fail5:
- NCDInterfaceMonitor_Free(&monitor);
- fail4:
- NCDConfig_free_interfaces(configuration);
- fail3:
- BSignal_RemoveHandler();
- fail2:
- BReactor_Free(&ss);
- fail1:
- BLog(BLOG_ERROR, "initialization failed");
- BLog_Free();
- fail0:
- // finish objects
- DebugObjectGlobal_Finish();
- return 1;
-
- event_loop:
- // enter event loop
- BLog(BLOG_NOTICE, "entering event loop");
- int ret = BReactor_Exec(&ss);
-
- // free reactor
- BReactor_Free(&ss);
-
- // free logger
- BLog(BLOG_NOTICE, "exiting");
- BLog_Free();
-
- // finish objects
- DebugObjectGlobal_Finish();
-
- return ret;
- }
- void terminate (void)
- {
- BLog(BLOG_NOTICE, "tearing down");
-
- // free interfaces
- free_interfaces();
-
- // free interface monitor
- NCDInterfaceMonitor_Free(&monitor);
-
- // free configuration
- NCDConfig_free_interfaces(configuration);
-
- // remove signal handler
- BSignal_RemoveHandler();
-
- // exit reactor
- BReactor_Quit(&ss, 1);
- }
- void print_help (const char *name)
- {
- printf(
- "Usage:\n"
- " %s\n"
- " [--help]\n"
- " [--version]\n"
- " [--logger <"LOGGERS_STRING">]\n"
- #ifndef BADVPN_USE_WINAPI
- " (logger=syslog?\n"
- " [--syslog-facility <string>]\n"
- " [--syslog-ident <string>]\n"
- " )\n"
- #endif
- " [--loglevel <0-5/none/error/warning/notice/info/debug>]\n"
- " [--channel-loglevel <channel-name> <0-5/none/error/warning/notice/info/debug>] ...\n"
- " --config-file <file>\n",
- name
- );
- }
- void print_version (void)
- {
- printf(GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION"\n"GLOBAL_COPYRIGHT_NOTICE"\n");
- }
- int parse_arguments (int argc, char *argv[])
- {
- if (argc <= 0) {
- return 0;
- }
-
- options.help = 0;
- options.version = 0;
- options.logger = LOGGER_STDOUT;
- #ifndef BADVPN_USE_WINAPI
- options.logger_syslog_facility = "daemon";
- options.logger_syslog_ident = argv[0];
- #endif
- options.loglevel = -1;
- for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
- options.loglevels[i] = -1;
- }
- options.config_file = NULL;
-
- for (int i = 1; i < argc; i++) {
- char *arg = argv[i];
- if (!strcmp(arg, "--help")) {
- options.help = 1;
- }
- else if (!strcmp(arg, "--version")) {
- options.version = 1;
- }
- else if (!strcmp(arg, "--logger")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- char *arg2 = argv[i + 1];
- if (!strcmp(arg2, "stdout")) {
- options.logger = LOGGER_STDOUT;
- }
- #ifndef BADVPN_USE_WINAPI
- else if (!strcmp(arg2, "syslog")) {
- options.logger = LOGGER_SYSLOG;
- }
- #endif
- else {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- #ifndef BADVPN_USE_WINAPI
- else if (!strcmp(arg, "--syslog-facility")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.logger_syslog_facility = argv[i + 1];
- i++;
- }
- else if (!strcmp(arg, "--syslog-ident")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.logger_syslog_ident = argv[i + 1];
- i++;
- }
- #endif
- else if (!strcmp(arg, "--loglevel")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- if ((options.loglevel = parse_loglevel(argv[i + 1])) < 0) {
- fprintf(stderr, "%s: wrong argument\n", arg);
- return 0;
- }
- i++;
- }
- else if (!strcmp(arg, "--channel-loglevel")) {
- if (2 >= argc - i) {
- fprintf(stderr, "%s: requires two arguments\n", arg);
- return 0;
- }
- int channel = BLogGlobal_GetChannelByName(argv[i + 1]);
- if (channel < 0) {
- fprintf(stderr, "%s: wrong channel argument\n", arg);
- return 0;
- }
- int loglevel = parse_loglevel(argv[i + 2]);
- if (loglevel < 0) {
- fprintf(stderr, "%s: wrong loglevel argument\n", arg);
- return 0;
- }
- options.loglevels[channel] = loglevel;
- i += 2;
- }
- else if (!strcmp(arg, "--config-file")) {
- if (1 >= argc - i) {
- fprintf(stderr, "%s: requires an argument\n", arg);
- return 0;
- }
- options.config_file = argv[i + 1];
- i++;
- }
- else {
- fprintf(stderr, "unknown option: %s\n", arg);
- return 0;
- }
- }
-
- if (options.help || options.version) {
- return 1;
- }
-
- if (!options.config_file) {
- fprintf(stderr, "--config-file is required\n");
- return 0;
- }
-
- return 1;
- }
- void signal_handler (void *unused)
- {
- BLog(BLOG_NOTICE, "termination requested");
-
- terminate();
- return;
- }
- void load_interfaces (struct NCDConfig_interfaces *conf)
- {
- while (conf) {
- interface_init(conf);
- conf = conf->next;
- }
- }
- void free_interfaces (void)
- {
- // remove in reverse order so we don't have to remove incoming dependencies
- LinkedList2Node *node;
- while (node = LinkedList2_GetLast(&interfaces)) {
- struct interface *iface = UPPER_OBJECT(node, struct interface, list_node);
- interface_free(iface);
- }
- }
- struct dns_sort_entry {
- uint32_t addr;
- int priority;
- };
- int set_dns_servers (void)
- {
- // collect servers
-
- struct dns_sort_entry servers[num_ipv4_dns_servers];
- size_t num_servers = 0;
-
- LinkedList2Iterator if_it;
- LinkedList2Iterator_InitForward(&if_it, &interfaces);
- LinkedList2Node *if_node;
- while (if_node = LinkedList2Iterator_Next(&if_it)) {
- struct interface *iface = UPPER_OBJECT(if_node, struct interface, list_node);
-
- LinkedList2Iterator serv_it;
- LinkedList2Iterator_InitForward(&serv_it, &iface->ipv4_dns_servers);
- LinkedList2Node *serv_node;
- while (serv_node = LinkedList2Iterator_Next(&serv_it)) {
- struct ipv4_dns_entry *dns = UPPER_OBJECT(serv_node, struct ipv4_dns_entry, list_node);
-
- servers[num_servers].addr = dns->addr;
- servers[num_servers].priority= dns->priority;
- num_servers++;
- }
- }
-
- ASSERT(num_servers == num_ipv4_dns_servers)
-
- // sort by priority
- qsort(servers, num_servers, sizeof(servers[0]), dns_qsort_comparator);
-
- // copy addresses into an array
- uint32_t addrs[num_servers];
- for (size_t i = 0; i < num_servers; i++) {
- addrs[i] = servers[i].addr;
- }
-
- // set servers
- if (!NCDIfConfig_set_dns_servers(addrs, num_servers)) {
- BLog(BLOG_ERROR, "failed to set DNS servers");
- return 0;
- }
-
- return 1;
- }
- int dns_qsort_comparator (const void *v1, const void *v2)
- {
- const struct dns_sort_entry *e1 = v1;
- const struct dns_sort_entry *e2 = v2;
-
- if (e1->priority < e2->priority) {
- return -1;
- }
- if (e1->priority > e2->priority) {
- return 1;
- }
- return 0;
- }
- struct interface * find_interface (const char *name)
- {
- LinkedList2Iterator it;
- LinkedList2Iterator_InitForward(&it, &interfaces);
- LinkedList2Node *node;
- while (node = LinkedList2Iterator_Next(&it)) {
- struct interface *iface = UPPER_OBJECT(node, struct interface, list_node);
- if (!strcmp(iface->conf->name, name)) {
- LinkedList2Iterator_Free(&it);
- return iface;
- }
- }
-
- return NULL;
- }
- void monitor_handler (void *unused, const char *ifname, int if_flags)
- {
- struct interface *iface = find_interface(ifname);
- if (!iface) {
- return;
- }
-
- if (!(if_flags&NCDIFCONFIG_FLAG_EXISTS)) {
- if (iface->state > INTERFACE_STATE_WAITDEVICE) {
- interface_log(iface, BLOG_INFO, "device down");
-
- interface_down_to(iface, INTERFACE_STATE_WAITDEVICE);
- }
- } else {
- if (iface->state == INTERFACE_STATE_RESETTING || iface->state == INTERFACE_STATE_WAITDEVICE) {
- interface_log(iface, BLOG_INFO, "device up");
-
- // stop reset timer (if it was resetting)
- BReactor_RemoveTimer(&ss, &iface->reset_timer);
-
- // start
- interface_start(iface);
-
- return;
- }
-
- if ((if_flags&NCDIFCONFIG_FLAG_RUNNING)) {
- if (iface->state == INTERFACE_STATE_WAITLINK) {
- interface_log(iface, BLOG_INFO, "link up");
-
- interface_link_up(iface);
- }
- } else {
- if (iface->state > INTERFACE_STATE_WAITLINK) {
- interface_log(iface, BLOG_INFO, "link down");
-
- interface_down_to(iface, INTERFACE_STATE_WAITLINK);
- }
- }
- }
- }
- int interface_init (struct NCDConfig_interfaces *conf)
- {
- // check for existing interface
- if (find_interface(conf->name)) {
- BLog(BLOG_ERROR, "interface %s already exists", conf->name);
- goto fail0;
- }
-
- // allocate interface entry
- struct interface *iface = malloc(sizeof(*iface));
- if (!iface) {
- BLog(BLOG_ERROR, "malloc failed for interface %s", conf->name);
- goto fail0;
- }
-
- // set conf
- iface->conf = conf;
-
- // set type
- struct NCDConfig_statements *type_st = NCDConfig_find_statement(conf->statements, "type");
- if (!type_st) {
- interface_log(iface, BLOG_ERROR, "missing type");
- goto fail1;
- }
- char *type;
- if (!NCDConfig_statement_has_one_arg(type_st, &type)) {
- interface_log(iface, BLOG_ERROR, "type: wrong arity");
- goto fail1;
- }
- if (!strcmp(type, "physical")) {
- iface->type = INTERFACE_TYPE_PHYSICAL;
- }
- else {
- interface_log(iface, BLOG_ERROR, "type: unknown value");
- goto fail1;
- }
-
- // init reset timer
- BTimer_Init(&iface->reset_timer, INTERFACE_RETRY_TIME, (BTimer_handler)interface_reset_timer_handler, iface);
-
- // init outgoing dependencies list
- LinkedList2_Init(&iface->deps_out);
-
- // init outgoing dependencies
- struct NCDConfig_statements *need_st = conf->statements;
- while (need_st = NCDConfig_find_statement(need_st, "need")) {
- char *need_ifname;
- if (!NCDConfig_statement_has_one_arg(need_st, &need_ifname)) {
- interface_log(iface, BLOG_ERROR, "need: wrong arity");
- goto fail2;
- }
-
- struct interface *need_if = find_interface(need_ifname);
- if (!need_if) {
- interface_log(iface, BLOG_ERROR, "need: %s: unknown interface", need_ifname);
- goto fail2;
- }
-
- if (!interface_add_dependency(iface, need_if)) {
- interface_log(iface, BLOG_ERROR, "need: %s: failed to add dependency", need_ifname);
- goto fail2;
- }
-
- need_st = need_st->next;
- }
-
- // init incoming dependencies list
- LinkedList2_Init(&iface->deps_in);
-
- // set not up
- iface->up = 0;
-
- // set no DHCP
- iface->have_dhcp = 0;
-
- // init ipv4 addresses list
- LinkedList2_Init(&iface->ipv4_addresses);
-
- // init ipv4 routes list
- LinkedList2_Init(&iface->ipv4_routes);
-
- // init ipv4 dns servers list
- LinkedList2_Init(&iface->ipv4_dns_servers);
-
- // insert to interfaces list
- LinkedList2_Append(&interfaces, &iface->list_node);
-
- interface_start(iface);
-
- return 1;
-
- fail2:
- interface_remove_dependencies(iface);
- fail1:
- free(iface);
- fail0:
- return 0;
- }
- void interface_free (struct interface *iface)
- {
- ASSERT(LinkedList2_IsEmpty(&iface->deps_in))
-
- // deconfigure IPv4
- interface_deconfigure_ipv4(iface);
-
- // free DHCP
- if (iface->have_dhcp) {
- BDHCPClient_Free(&iface->dhcp);
- }
-
- // set down
- if (iface->up) {
- interface_set_down(iface);
- }
-
- // remove from interfaces list
- LinkedList2_Remove(&interfaces, &iface->list_node);
-
- // remove outgoing dependencies
- interface_remove_dependencies(iface);
-
- // stop reset timer
- BReactor_RemoveTimer(&ss, &iface->reset_timer);
-
- // free memory
- free(iface);
- }
- void interface_down_to (struct interface *iface, int state)
- {
- ASSERT(state >= INTERFACE_STATE_WAITDEPS)
-
- if (state < INTERFACE_STATE_FINISHED) {
- // unsatisfy incoming dependencies
- interface_unsatisfy_incoming_depenencies(iface);
-
- // deconfigure IPv4
- interface_deconfigure_ipv4(iface);
- }
-
- // deconfigure DHCP
- if (state < INTERFACE_STATE_DHCP && iface->have_dhcp) {
- BDHCPClient_Free(&iface->dhcp);
- iface->have_dhcp = 0;
- }
-
- // set down
- if (state < INTERFACE_STATE_WAITLINK && iface->up) {
- interface_set_down(iface);
- }
-
- // start/stop reset timer
- if (state == INTERFACE_STATE_RESETTING) {
- BReactor_SetTimer(&ss, &iface->reset_timer);
- } else {
- BReactor_RemoveTimer(&ss, &iface->reset_timer);
- }
-
- // set state
- iface->state = state;
- }
- void interface_reset (struct interface *iface)
- {
- interface_log(iface, BLOG_INFO, "will try again later");
-
- interface_down_to(iface, INTERFACE_STATE_RESETTING);
- }
- void interface_start (struct interface *iface)
- {
- ASSERT(!BTimer_IsRunning(&iface->reset_timer))
- ASSERT(!iface->up)
- ASSERT(!iface->have_dhcp)
- ASSERT(LinkedList2_IsEmpty(&iface->ipv4_addresses))
- ASSERT(LinkedList2_IsEmpty(&iface->ipv4_routes))
- ASSERT(LinkedList2_IsEmpty(&iface->ipv4_dns_servers))
-
- // check dependencies
- if (!interface_dependencies_satisfied(iface)) {
- interface_log(iface, BLOG_INFO, "waiting for dependencies");
-
- // waiting for dependencies
- iface->state = INTERFACE_STATE_WAITDEPS;
- return;
- }
-
- // query interface state
- int flags = NCDIfConfig_query(iface->conf->name);
-
- if (!(flags&NCDIFCONFIG_FLAG_EXISTS)) {
- interface_log(iface, BLOG_INFO, "device doesn't exist");
-
- // waiting for device
- iface->state = INTERFACE_STATE_WAITDEVICE;
- return;
- }
-
- if ((flags&NCDIFCONFIG_FLAG_UP)) {
- interface_log(iface, BLOG_ERROR, "device already up - NOT configuring");
- goto fail;
- }
-
- // set interface up
- if (!interface_set_up(iface)) {
- goto fail;
- }
-
- interface_log(iface, BLOG_INFO, "waiting for link");
-
- // waiting for link
- iface->state = INTERFACE_STATE_WAITLINK;
-
- return;
-
- fail:
- interface_reset(iface);
- }
- void interface_link_up (struct interface *iface)
- {
- ASSERT(iface->up)
- ASSERT(!iface->have_dhcp)
- ASSERT(LinkedList2_IsEmpty(&iface->ipv4_addresses))
- ASSERT(LinkedList2_IsEmpty(&iface->ipv4_routes))
- ASSERT(LinkedList2_IsEmpty(&iface->ipv4_dns_servers))
-
- // check for DHCP
- struct NCDConfig_statements *dhcp_st = NCDConfig_find_statement(iface->conf->statements, "dhcp");
- if (dhcp_st) {
- if (dhcp_st->args) {
- interface_log(iface, BLOG_ERROR, "dhcp: wrong arity");
- goto fail;
- }
-
- // init DHCP client
- if (!BDHCPClient_Init(&iface->dhcp, iface->conf->name, &ss, (BDHCPClient_handler)interface_dhcp_handler, iface)) {
- interface_log(iface, BLOG_ERROR, "BDHCPClient_Init failed");
- goto fail;
- }
-
- // set have DHCP
- iface->have_dhcp = 1;
-
- // set state
- iface->state = INTERFACE_STATE_DHCP;
-
- return;
- }
-
- // configure IPv4
- if (!interface_configure_ipv4(iface)) {
- goto fail;
- }
-
- // set state finished
- iface->state = INTERFACE_STATE_FINISHED;
-
- // satisfy incoming dependencies
- interface_satisfy_incoming_depenencies(iface);
-
- return;
-
- fail:
- interface_reset(iface);
- }
- void interface_log (struct interface *iface, int level, const char *fmt, ...)
- {
- va_list vl;
- va_start(vl, fmt);
- BLog_Append("interface %s: ", iface->conf->name);
- BLog_LogToChannelVarArg(BLOG_CURRENT_CHANNEL, level, fmt, vl);
- va_end(vl);
- }
- void interface_reset_timer_handler (struct interface *iface)
- {
- ASSERT(iface->state == INTERFACE_STATE_RESETTING)
-
- // start interface
- interface_start(iface);
- }
- void interface_dhcp_handler (struct interface *iface, int event)
- {
- ASSERT(iface->have_dhcp)
-
- switch (event) {
- case BDHCPCLIENT_EVENT_UP: {
- ASSERT(iface->state == INTERFACE_STATE_DHCP)
-
- interface_log(iface, BLOG_INFO, "DHCP up");
-
- // configure IPv4
- if (!interface_configure_ipv4(iface)) {
- goto fail;
- }
-
- // set state
- iface->state = INTERFACE_STATE_FINISHED;
-
- // satisfy incoming dependencies
- interface_satisfy_incoming_depenencies(iface);
-
- return;
-
- fail:
- interface_reset(iface);
- } break;
-
- case BDHCPCLIENT_EVENT_DOWN: {
- ASSERT(iface->state == INTERFACE_STATE_FINISHED)
-
- interface_log(iface, BLOG_INFO, "DHCP down");
-
- interface_down_to(iface, INTERFACE_STATE_DHCP);
- } break;
-
- default:
- ASSERT(0);
- }
- }
- void interface_remove_dependencies (struct interface *iface)
- {
- LinkedList2Node *node;
- while (node = LinkedList2_GetFirst(&iface->deps_out)) {
- struct dependency *d = UPPER_OBJECT(node, struct dependency, src_node);
- ASSERT(d->src == iface)
-
- remove_dependency(d);
- }
- }
- int interface_add_dependency (struct interface *iface, struct interface *dst_iface)
- {
- // allocate entry
- struct dependency *d = malloc(sizeof(*d));
- if (!d) {
- return 0;
- }
-
- // set src and dst
- d->src = iface;
- d->dst = dst_iface;
-
- // insert to src list
- LinkedList2_Append(&iface->deps_out, &d->src_node);
-
- // insert to dst list
- LinkedList2_Append(&dst_iface->deps_in, &d->dst_node);
-
- return 1;
- }
- void remove_dependency (struct dependency *d)
- {
- // remove from dst list
- LinkedList2_Remove(&d->dst->deps_in, &d->dst_node);
-
- // remove from src list
- LinkedList2_Remove(&d->src->deps_out, &d->src_node);
-
- // free entry
- free(d);
- }
- int interface_dependencies_satisfied (struct interface *iface)
- {
- LinkedList2Iterator it;
- LinkedList2Iterator_InitForward(&it, &iface->deps_out);
- LinkedList2Node *node;
- while (node = LinkedList2Iterator_Next(&it)) {
- struct dependency *d = UPPER_OBJECT(node, struct dependency, src_node);
- ASSERT(d->src == iface)
-
- if (d->dst->state != INTERFACE_STATE_FINISHED) {
- LinkedList2Iterator_Free(&it);
- return 0;
- }
- }
-
- return 1;
- }
- void interface_satisfy_incoming_depenencies (struct interface *iface)
- {
- LinkedList2Iterator it;
- LinkedList2Iterator_InitForward(&it, &iface->deps_in);
- LinkedList2Node *node;
- while (node = LinkedList2Iterator_Next(&it)) {
- struct dependency *d = UPPER_OBJECT(node, struct dependency, dst_node);
- ASSERT(d->dst == iface)
-
- if (d->src->state == INTERFACE_STATE_WAITDEPS) {
- interface_log(d->src, BLOG_INFO, "dependency %s satisfied", iface->conf->name);
-
- interface_start(d->src);
- }
- }
- }
- void interface_unsatisfy_incoming_depenencies (struct interface *iface)
- {
- LinkedList2Iterator it;
- LinkedList2Iterator_InitForward(&it, &iface->deps_in);
- LinkedList2Node *node;
- while (node = LinkedList2Iterator_Next(&it)) {
- struct dependency *d = UPPER_OBJECT(node, struct dependency, dst_node);
- ASSERT(d->dst == iface)
-
- if (d->src->state > INTERFACE_STATE_WAITDEPS) {
- interface_log(d->src, BLOG_INFO, "dependency %s no longer satisfied", iface->conf->name);
-
- interface_down_to(d->src, INTERFACE_STATE_WAITDEPS);
- }
- }
- }
- int interface_set_up (struct interface *iface)
- {
- ASSERT(!iface->up)
-
- // set up
- if (!NCDIfConfig_set_up(iface->conf->name)) {
- interface_log(iface, BLOG_ERROR, "failed to set up");
- return 0;
- }
-
- iface->up = 1;
-
- return 1;
- }
- void interface_set_down (struct interface *iface)
- {
- ASSERT(iface->up)
-
- // set down
- if (!NCDIfConfig_set_down(iface->conf->name)) {
- interface_log(iface, BLOG_ERROR, "failed to set down");
- }
-
- iface->up = 0;
- }
- int interface_configure_ipv4 (struct interface *iface)
- {
- ASSERT(!(iface->have_dhcp) || BDHCPClient_IsUp(&iface->dhcp))
- ASSERT(LinkedList2_IsEmpty(&iface->ipv4_addresses))
- ASSERT(LinkedList2_IsEmpty(&iface->ipv4_routes))
- ASSERT(LinkedList2_IsEmpty(&iface->ipv4_dns_servers))
-
- // configure addresses
- if (!interface_add_ipv4_addresses(iface)) {
- return 0;
- }
-
- // configure routes
- if (!interface_add_ipv4_routes(iface)) {
- return 0;
- }
-
- // configure DNS servers
- if (!interface_add_ipv4_dns_servers(iface)) {
- return 0;
- }
-
- return 1;
- }
- void interface_deconfigure_ipv4 (struct interface *iface)
- {
- // remove DNS servers
- interface_remove_ipv4_dns_servers(iface);
-
- // remove routes
- interface_remove_ipv4_routes(iface);
-
- // remove addresses
- interface_remove_ipv4_addresses(iface);
- }
- int interface_add_ipv4_addresses (struct interface *iface)
- {
- ASSERT(!(iface->have_dhcp) || BDHCPClient_IsUp(&iface->dhcp))
- ASSERT(LinkedList2_IsEmpty(&iface->ipv4_addresses))
-
- struct NCDConfig_statements *st = iface->conf->statements;
-
- while (st = NCDConfig_find_statement(st, "ipv4.addr")) {
- // get address string
- char *addrstr;
- if (!NCDConfig_statement_has_one_arg(st, &addrstr)) {
- interface_log(iface, BLOG_ERROR, "ipv4.addr: wrong arity");
- return 0;
- }
-
- struct ipv4_ifaddr ifaddr;
-
- // is this a DHCP address?
- if (!strcmp(addrstr, "dhcp_addr")) {
- // check if DHCP is enabled
- if (!iface->have_dhcp) {
- interface_log(iface, BLOG_ERROR, "ipv4.addr: %s: DHCP not enabled", addrstr);
- return 0;
- }
-
- // get address and mask
- uint32_t addr;
- BDHCPClient_GetClientIP(&iface->dhcp, &addr);
- uint32_t mask;
- BDHCPClient_GetClientMask(&iface->dhcp, &mask);
-
- // convert to ifaddr
- if (!ipaddr_ipv4_ifaddr_from_addr_mask(addr, mask, &ifaddr)) {
- interface_log(iface, BLOG_ERROR, "ipv4.addr: %s: wrong mask", addrstr);
- return 0;
- }
- } else {
- // parse address string
- if (!ipaddr_parse_ipv4_ifaddr(addrstr, &ifaddr)) {
- interface_log(iface, BLOG_ERROR, "ipv4.addr: %s: wrong format", addrstr);
- return 0;
- }
- }
-
- // add this address
- if (!interface_add_ipv4_addr(iface, ifaddr)) {
- interface_log(iface, BLOG_ERROR, "ipv4.addr: %s: failed to add", addrstr);
- return 0;
- }
-
- st = st->next;
- }
-
- return 1;
- }
- void interface_remove_ipv4_addresses (struct interface *iface)
- {
- LinkedList2Node *node;
- while (node = LinkedList2_GetLast(&iface->ipv4_addresses)) {
- struct ipv4_addr_entry *entry = UPPER_OBJECT(node, struct ipv4_addr_entry, list_node);
- interface_remove_ipv4_addr(iface, entry);
- }
- }
- int interface_add_ipv4_routes (struct interface *iface)
- {
- ASSERT(!(iface->have_dhcp) || BDHCPClient_IsUp(&iface->dhcp))
- ASSERT(LinkedList2_IsEmpty(&iface->ipv4_routes))
-
- struct NCDConfig_statements *st = iface->conf->statements;
-
- while (st = NCDConfig_find_statement(st, "ipv4.route")) {
- // read statement
- char *dest_str;
- char *gateway_str;
- char *metric_str;
- if (!NCDConfig_statement_has_three_args(st, &dest_str, &gateway_str, &metric_str)) {
- interface_log(iface, BLOG_ERROR, "ipv4.route: wrong arity");
- return 0;
- }
-
- // parse dest string
- struct ipv4_ifaddr dest;
- if (!ipaddr_parse_ipv4_ifaddr(dest_str, &dest)) {
- interface_log(iface, BLOG_ERROR, "ipv4.route: (%s,%s,%s): dest: wrong format", dest_str, gateway_str, metric_str);
- return 0;
- }
-
- uint32_t gateway;
-
- // is gateway a DHCP router?
- if (!strcmp(gateway_str, "dhcp_router")) {
- // check if DHCP is enabled
- if (!iface->have_dhcp) {
- interface_log(iface, BLOG_ERROR, "ipv4.route: (%s,%s,%s): gateway: DHCP not enabled", dest_str, gateway_str, metric_str);
- return 0;
- }
-
- // obtain gateway from DHCP
- if (!BDHCPClient_GetRouter(&iface->dhcp, &gateway)) {
- interface_log(iface, BLOG_ERROR, "ipv4.route: (%s,%s,%s): gateway: DHCP did not provide a router", dest_str, gateway_str, metric_str);
- return 0;
- }
- } else {
- // parse gateway string
- if (!ipaddr_parse_ipv4_addr(gateway_str, strlen(gateway_str), &gateway)) {
- interface_log(iface, BLOG_ERROR, "ipv4.route: (%s,%s,%s): gateway: wrong format", dest_str, gateway_str, metric_str);
- return 0;
- }
- }
-
- // parse metric string
- int metric = atoi(metric_str);
-
- // add this address
- if (!interface_add_ipv4_route(iface, dest, gateway, metric)) {
- interface_log(iface, BLOG_ERROR, "ipv4.route: (%s,%s,%s): failed to add", dest_str, gateway_str, metric_str);
- return 0;
- }
-
- st = st->next;
- }
-
- return 1;
- }
- void interface_remove_ipv4_routes (struct interface *iface)
- {
- LinkedList2Node *node;
- while (node = LinkedList2_GetLast(&iface->ipv4_routes)) {
- struct ipv4_route_entry *entry = UPPER_OBJECT(node, struct ipv4_route_entry, list_node);
- interface_remove_ipv4_route(iface, entry);
- }
- }
- int interface_add_ipv4_dns_servers (struct interface *iface)
- {
- ASSERT(!(iface->have_dhcp) || BDHCPClient_IsUp(&iface->dhcp))
- ASSERT(LinkedList2_IsEmpty(&iface->ipv4_dns_servers))
-
- // read servers into entries
-
- struct NCDConfig_statements *st = iface->conf->statements;
-
- while (st = NCDConfig_find_statement(st, "ipv4.dns")) {
- // read statement
- char *addr_str;
- char *priority_str;
- if (!NCDConfig_statement_has_two_args(st, &addr_str, &priority_str)) {
- interface_log(iface, BLOG_ERROR, "ipv4.dns: wrong arity");
- return 0;
- }
-
- // parse priority string
- int priority = atoi(priority_str);
-
- // are these DHCP DNS servers?
- if (!strcmp(addr_str, "dhcp_dns_servers")) {
- // get servers from DHCP
- uint32_t addrs[BDHCPCLIENT_MAX_DOMAIN_NAME_SERVERS];
- int num_addrs = BDHCPClient_GetDNS(&iface->dhcp, addrs, BDHCPCLIENT_MAX_DOMAIN_NAME_SERVERS);
-
- // add entries
- for (int i = 0; i < num_addrs; i++) {
- // add entry
- if (!interface_add_ipv4_dns_entry(iface, addrs[i], priority)) {
- interface_log(iface, BLOG_ERROR, "ipv4.dns: (%s,%s): failed to add entry", addr_str, priority_str);
- return 0;
- }
- }
- } else {
- // parse addr string
- uint32_t addr;
- if (!ipaddr_parse_ipv4_addr(addr_str, strlen(addr_str), &addr)) {
- interface_log(iface, BLOG_ERROR, "ipv4.dns: (%s,%s): addr: wrong format", addr_str, priority_str);
- return 0;
- }
-
- // add entry
- if (!interface_add_ipv4_dns_entry(iface, addr, priority)) {
- interface_log(iface, BLOG_ERROR, "ipv4.dns: (%s,%s): failed to add entry", addr_str, priority_str);
- return 0;
- }
- }
-
- st = st->next;
- }
-
- // set servers
- if (!set_dns_servers()) {
- return 0;
- }
-
- return 1;
- }
- void interface_remove_ipv4_dns_servers (struct interface *iface)
- {
- // remove entries
- LinkedList2Node *node;
- while (node = LinkedList2_GetFirst(&iface->ipv4_dns_servers)) {
- struct ipv4_dns_entry *entry = UPPER_OBJECT(node, struct ipv4_dns_entry, list_node);
- interface_remove_ipv4_dns_entry(iface, entry);
- }
-
- // set servers
- set_dns_servers();
- }
- int interface_add_ipv4_addr (struct interface *iface, struct ipv4_ifaddr ifaddr)
- {
- // add address entry
- struct ipv4_addr_entry *entry = interface_add_ipv4_addr_entry(iface, ifaddr);
- if (!entry) {
- interface_log(iface, BLOG_ERROR, "failed to add ipv4 address entry");
- return 0;
- }
-
- // assign the address
- if (!NCDIfConfig_add_ipv4_addr(iface->conf->name, ifaddr)) {
- interface_log(iface, BLOG_ERROR, "failed to assign ipv4 address");
- interface_remove_ipv4_addr_entry(iface, entry);
- return 0;
- }
-
- return 1;
- }
- void interface_remove_ipv4_addr (struct interface *iface, struct ipv4_addr_entry *entry)
- {
- // remove the address
- if (!NCDIfConfig_remove_ipv4_addr(iface->conf->name, entry->ifaddr)) {
- interface_log(iface, BLOG_ERROR, "failed to remove ipv4 address");
- }
-
- // remove address entry
- interface_remove_ipv4_addr_entry(iface, entry);
- }
- int interface_add_ipv4_route (struct interface *iface, struct ipv4_ifaddr dest, uint32_t gateway, int metric)
- {
- // add address entry
- struct ipv4_route_entry *entry = interface_add_ipv4_route_entry(iface, dest, gateway, metric);
- if (!entry) {
- interface_log(iface, BLOG_ERROR, "failed to add ipv4 route entry");
- return 0;
- }
-
- // add the route
- if (!NCDIfConfig_add_ipv4_route(dest, gateway, metric, iface->conf->name)) {
- interface_log(iface, BLOG_ERROR, "failed to add ipv4 route");
- interface_remove_ipv4_route_entry(iface, entry);
- return 0;
- }
-
- return 1;
- }
- void interface_remove_ipv4_route (struct interface *iface, struct ipv4_route_entry *entry)
- {
- // remove the route
- if (!NCDIfConfig_remove_ipv4_route(entry->dest, entry->gateway, entry->metric, iface->conf->name)) {
- interface_log(iface, BLOG_ERROR, "failed to remove ipv4 route");
- }
-
- // remove address entry
- interface_remove_ipv4_route_entry(iface, entry);
- }
- struct ipv4_addr_entry * interface_add_ipv4_addr_entry (struct interface *iface, struct ipv4_ifaddr ifaddr)
- {
- // allocate entry
- struct ipv4_addr_entry *entry = malloc(sizeof(*entry));
- if (!entry) {
- return NULL;
- }
-
- // set ifaddr
- entry->ifaddr = ifaddr;
-
- // add to list
- LinkedList2_Append(&iface->ipv4_addresses, &entry->list_node);
-
- return entry;
- }
- void interface_remove_ipv4_addr_entry (struct interface *iface, struct ipv4_addr_entry *entry)
- {
- // remove from list
- LinkedList2_Remove(&iface->ipv4_addresses, &entry->list_node);
-
- // free entry
- free(entry);
- }
- struct ipv4_route_entry * interface_add_ipv4_route_entry (struct interface *iface, struct ipv4_ifaddr dest, uint32_t gateway, int metric)
- {
- // allocate entry
- struct ipv4_route_entry *entry = malloc(sizeof(*entry));
- if (!entry) {
- return NULL;
- }
-
- // set info
- entry->dest = dest;
- entry->gateway = gateway;
- entry->metric = metric;
-
- // add to list
- LinkedList2_Append(&iface->ipv4_routes, &entry->list_node);
-
- return entry;
- }
- void interface_remove_ipv4_route_entry (struct interface *iface, struct ipv4_route_entry *entry)
- {
- // remove from list
- LinkedList2_Remove(&iface->ipv4_routes, &entry->list_node);
-
- // free entry
- free(entry);
- }
- struct ipv4_dns_entry * interface_add_ipv4_dns_entry (struct interface *iface, uint32_t addr, int priority)
- {
- // allocate entry
- struct ipv4_dns_entry *entry = malloc(sizeof(*entry));
- if (!entry) {
- return NULL;
- }
-
- // set info
- entry->addr = addr;
- entry->priority = priority;
-
- // add to list
- LinkedList2_Append(&iface->ipv4_dns_servers, &entry->list_node);
-
- // increment number of DNS servers
- num_ipv4_dns_servers++;
-
- return entry;
- }
- void interface_remove_ipv4_dns_entry (struct interface *iface, struct ipv4_dns_entry *entry)
- {
- // decrement number of DNS servers
- num_ipv4_dns_servers--;
-
- // remove from list
- LinkedList2_Remove(&iface->ipv4_dns_servers, &entry->list_node);
-
- // free entry
- free(entry);
- }
|