📄 netconf.c
字号:
* about down interfaces. */static void netconfig_route(void){ char *addr_string, *route_string, *route_argv[7]; int error_code, route_argc; ipaddr_t gw, dest, mask; bits8_t pfxlen; struct net *net; route_entry_t *r; int found_default, pref; route_head_t *rh; found_default = 0; /* * routes to routers at the other end of a point to point link * usage: route-net=dest mask net [priority] * dest and mask are in IP address format * net is an interface name (net->s_name field) */ for (route_string = INI_ITER_START(attache_section, "route-net"); route_string; route_string = INI_ITER_NEXT()) { route_argc = parse_line(route_string, route_argv, 7); if ((route_argc != 3) && (route_argc != 4)) printf("Bad route command for '%s'\n", route_string); else if (string_to_ipaddr(route_argv[0], &gw), GET_IPADDR_TYPE(&gw) != IPV4) printf("Bad route destination %s\n", route_argv[0]); else if ((string_to_ipaddr(route_argv[1], &mask), GET_IPADDR_TYPE(&mask) != IPV4) || ((pfxlen = ipv4_mask_to_pfxlen(&mask)) == 0)) printf("Bad route mask %s\n", route_argv[1]); else if ((net = if_lookup(route_argv[2])) == 0) printf("No network interface named %s\n", route_argv[2]); else if (route_add(&gw, pfxlen, &gw, net, 0, (route_argc == 4 ? (bits8_t) atoi(route_argv[3]) : static_routes_owner.default_priority), RF_FIXED, TOS_ANY, static_routes_owner.default_tag, 0, &static_routes_owner, 0, &error_code) == 0) printf("Error %d creating route to %s\n", error_code, route_argv[0]); } /* * default route, two forms for historical reasons * usage: route-default=gw [pref] * (or) router-default=gw [pref] * * argument is in IP address format */ if (((addr_string = INI_LOOKUP(attache_section, "route-default")) != 0) || ((addr_string = INI_LOOKUP(attache_section, "router-default")) != 0)) { route_argc = parse_line(addr_string, route_argv, 7); if ((route_argc < 1) || (route_argc > 2)) printf("Bad route-default command, '%s'\n", addr_string); printf(" -- Setting up IPv4 default router -- %s.\n", route_argv[0]); if (route_argc == 2) pref = atoi(route_argv[1]); else pref = 0; if (string_to_ipaddr(route_argv[0], &gw), GET_IPADDR_TYPE(&gw) != IPV4) fprintf(stderr, "Error parsing address `%s' for default gateway\n", addr_string); else if ((rh = route_host_lookup(&gw, &error_code)) == 0) fprintf(stderr, "No route to default gateway %s\n", addr_string); else if ((GET_RTE_ENTRY_TYPE(GET_RTE_ACTIVE_ENTRY(rh)) & RF_DIRECT) == 0) fprintf(stderr, "Route to default gateway %s not direct\n", addr_string); else if ((error_code = route_add_default_router(&gw, GET_RTE_INTERFACE(rh), pref)) != RTE_ERR_NONE) fprintf(stderr, "Error %d creating default route through %s\n", error_code, addr_string); else found_default = 1; }#if INSTALL_ATTACHE_IPV6 /* set the ipv6 default route */ if ((addr_string = INI_LOOKUP(attache_section, "ipv6-route-default")) != 0) { printf(" -- Setting up IPv6 default router -- '%s'\n", addr_string); string_to_ipaddr(addr_string, &gw); if ((GET_IPADDR_TYPE(&gw)) != IPV6) fprintf(stderr, "Error parsing address `%s' for default gateway\n", addr_string); else if ((rh = route_host_lookup(&gw, &error_code)) == 0) fprintf(stderr, "No route to default gateway %s\n", addr_string); else if ((GET_RTE_ENTRY_TYPE(GET_RTE_ACTIVE_ENTRY(rh)) & RF_DIRECT) == 0) fprintf(stderr, "Route to default gateway %s not direct\n", addr_string); else if ((error_code = route_add_default_router(&gw, GET_RTE_INTERFACE(rh), RD_PREF_DEFAULT)) != RTE_ERR_NONE) fprintf(stderr, "Error %d creating default route through %s\n", error_code, addr_string); else found_default = 1; }#endif /* * default route to a network * usage: route-default-net=gw net * gw is in IP address format * net is an interface name (net->s_name field) */ if ((route_string = INI_LOOKUP(attache_section, "route-default-net")) != 0) { route_argc = parse_line(route_string, route_argv, 7); if (route_argc != 2) fprintf(stderr, "Bad route-default-net command for '%s'\n", route_string); else if (string_to_ipaddr(route_argv[0], &gw), GET_IPADDR_TYPE(&gw) != IPV4) fprintf(stderr, "Error parsing address `%s' for default gateway\n", route_argv[0]); else if ((net = if_lookup(route_argv[1])) == 0) fprintf(stderr, "No net `%s' for default route\n", route_argv[1]); else if ((error_code = route_add_default_router(&gw, net, RD_PREF_DEFAULT)) != RTE_ERR_NONE) printf("Error %d creating default route to %s\n", error_code, route_argv[0]); else found_default = 1; } if (found_default == 0) printf(" -- No default route configured --\n"); /* * static routes. * usage: route-gw=dest mask gw [priority] * all arguments are in IP address format */ for (route_string = INI_ITER_START(attache_section, "route-gw"); route_string; route_string = INI_ITER_NEXT()) { route_argc = parse_line(route_string, route_argv, 7); if ((route_argc != 3) && (route_argc != 4)) fprintf(stderr, "Bad route command for '%s'\n", route_string); else if (string_to_ipaddr(route_argv[0], &dest), GET_IPADDR_TYPE(&dest) != IPV4) fprintf(stderr, "Bad route destination %s\n", route_argv[0]); else if ((string_to_ipaddr(route_argv[1], &mask), GET_IPADDR_TYPE(&mask) != IPV4) || ((pfxlen = ipv4_mask_to_pfxlen(&mask)) == 0)) fprintf(stderr, "Bad route mask %s\n", route_argv[1]); else if (string_to_ipaddr(route_argv[2], &gw), GET_IPADDR_TYPE(&gw) != IPV4) fprintf(stderr, "Bad route gateway named %s\n", route_argv[2]); else if (((rh = route_host_lookup(&gw, &error_code)) == 0) || ((r = GET_RTE_ACTIVE_ENTRY(rh)) == 0)) fprintf(stderr, "No route to gateway %s for route to %s\n", route_argv[2], route_argv[0]); else if (GET_RTE_ENTRY_TYPE(r) != RF_FIXED) fprintf(stderr, "Route to %s not direct\n", route_argv[0]); else if (route_add(&dest, pfxlen, &gw, GET_RTE_ENTRY_INTERFACE(r), 0, (route_argc == 4 ? (bits8_t) atoi(route_argv[3]) : static_routes_owner.default_priority), RF_DYNAMIC, TOS_ANY, static_routes_owner.default_tag, 0, &static_routes_owner, 0, &error_code) == 0) fprintf(stderr, "Error %d creating route to %s\n", error_code, route_argv[0]); }}#if INSTALL_ATTACHE_RIP/* * RIP uses more than one of these filters, so a separate function * is easier than handling this inline. */static struct rip_filter *netconfig_rip_filter(char *name){ struct rip_filter *f; char *s, *argv[5]; int i = 0, n = 0; /* * count the number of filter entries, allocate the filter, fill it in. */ for (s = INI_ITER_START(attache_section, name); s; s = INI_ITER_NEXT()) n++; if (n == 0) return 0; if ((f = GLUE_ALLOC(n * sizeof(*f))) == 0) { fprintf(stderr, "couldn't allocate RIP %s\n", name); return 0; } for (s = INI_ITER_START(attache_section, name); s; s = INI_ITER_NEXT()) { if (parse_line(s, argv, 5) != 5) { printf("Bad filter command \"%s\"\n", s); continue; } switch (*argv[0]) { case 'a': case 'A': f[i].action = rip_filter_accept; break; case 'r': case 'R': f[i].action = rip_filter_reject; break; default: printf("Bad filter action \"%s\"\n", argv[0]); continue; } f[i].gw_addr = atoinet(argv[1]); f[i].gw_mask = atoinet(argv[2]); f[i].route_addr = atoinet(argv[3]); f[i].route_mask = atoinet(argv[4]); i++; } return f;}static void netconfig_rip(void){ bits16_t port; bits8_t flags = RIP_POISONING_REVERSE|RIP_ADVERTISING|RIP_BELIEVING; char *s; if ((s = INI_LOOKUP(attache_section, "rip-port")) == 0) return; if ((port = (bits16_t) atoi(s)) == 0) { printf("Bad RIP port number \"%s\"\n", s); return; } if ((s = INI_LOOKUP(attache_section, "rip-poisoning")) != 0 && !atoi(s)) flags &= RIP_POISONING_REVERSE; if ((s = INI_LOOKUP(attache_section, "rip-advertising")) != 0 && !atoi(s)) flags &= RIP_ADVERTISING; if ((s = INI_LOOKUP(attache_section, "rip-believing")) != 0 && !atoi(s)) flags &= RIP_BELIEVING; if (!rip_create(RIP_VERSION_1, port, RT_RIP, flags, netconfig_rip_filter("rip-advertise-filter"), netconfig_rip_filter("rip-believe-filter"))) printf("RIP initialization failed\n");}#endif /* INSTALL_ATTACHE_RIP */#if INSTALL_ATTACHE_MULTICAST && INSTALL_ATTACHE_SNTP_CLIENTstatic void netconfig_sntp(void){ net_if *sntp_if; char *s; if ((s = INI_LOOKUP(attache_section, "sntp")) != 0) { if ((sntp_if = if_lookup(s)) != 0) printf(" -- Initializing SNTP on interface %s\n", s); sntp_init(sntp_if); }}#endif /* INSTALL_ATTACHE_MULTICAST && INSTALL_ATTACHE_SNTP_CLIENT */#if (INSTALL_ATTACHE_IPV4_ROUTER_DISCOVERY && INSTALL_ATTACHE_IP_FORWARDING)static void netconfig_rd(void){ char *av[3], *s; ipaddr_t ra_addr; net_if *ra_if; bits32_t pref; int i; for (s = INI_ITER_START(attache_section, "router-advert"); s != 0; s = INI_ITER_NEXT()) { if ((i = parse_line(s, av, 3)) != 3) { printf("Wrong number of arguments to router-advert, ignoring\n"); } else { ra_if = if_lookup(av[0]); string_to_ipaddr(av[1], &ra_addr); pref = atoi(av[2]); printf(" -- Advertising Router: %s, pref = %d, if = %s\n", av[1], (int)pref, av[0]); if ((ra_if != 0) && (GET_IPADDR_TYPE(&ra_addr) == IPV4)) { rd_add_advert_router(ra_if, &ra_addr, pref); } } }}#endif /* (INSTALL_ATTACHE_IPV4_ROUTER_DISCOVERY ... */ #if INSTALL_COURIER_BGP_TRAPStrap_addr *Trap_Addr_Head;static void netconfig_trap(void){ static char Trap_Comm[] = "Trap_Comm"; struct trap_addr **tail = &Trap_Addr_Head; char *s; Trap_Addr_Head = 0; for (s = INI_ITER_START(courier_section, "trap-addr"); s; s = INI_ITER_NEXT()) { if (!(*tail = GLUE_ALLOC(sizeof(trap_addr)))) return; MEMSET(*tail, 0, sizeof(**tail)); (*tail)->send_trap = 1; (*tail)->trap_comm = Trap_Comm; (*tail)->addr = atoinet(s); tail = &(*tail)->next; }}#endif#if INSTALL_COURIERtypedef struct policy_dispatch { char *keyword; void (*handler)(bits32_t *entity, int argc, char *argv[]);} policy_dispatch_t;static void netconfig_courier_policies(bits32_t *entity, policy_dispatch_t *dispatches){ char *s, *argv[255]; int argc; struct ini_handle_saved saved_ini; ini_save(ini_handle, &saved_ini); for (; dispatches && dispatches->keyword && dispatches->handler; dispatches++) { for (s = INI_ITER_START(courier_section, dispatches->keyword); s; s = INI_ITER_NEXT()) { argc = parse_line(s, argv, sizeof(argv)/sizeof(*argv)); (dispatches->handler)(entity, argc, argv); } } ini_restore(ini_handle, &saved_ini);}#endif#if INSTALL_COURIER_OSPF#if OSPF_IMPORT_TEMPLATE_KEY_LEN > OSPF_EXPORT_TEMPLATE_KEY_LEN#define OSPF_TEMPLATE_KEY_LEN OSPF_IMPORT_TEMPLATE_KEY_LEN#else#define OSPF_TEMPLATE_KEY_LEN OSPF_EXPORT_TEMPLATE_KEY_LEN#endifvoid netconfig_ospf_import_template(bits32_t *entity, int argc, char *argv[]){ struct OSPF *ospf = (struct OSPF *) *entity; bits32_t ospf_import_template_key[OSPF_IMPORT_TEMPLATE_KEY_LEN]; bits32_t template_num, template_bits, template_flags, pref; if (argc != (OSPF_IMPORT_TEMPLATE_KEY_LEN + 4)) { fprintf(stderr, "---OSPF Import Template Size Mismatch---\n"); return; } MEMSET(&ospf_import_template_key, 0, sizeof(ospf_import_template_key)); template_num = STRTOUL(argv[0], 0, 0); ospf_import_template_key[OSPF_IMPORT_TEMPLATE_ROUTE_ADDR] = STRTOUL(argv[1], 0, 0); ospf_import_template_key[OSPF_IMPORT_TEMPLATE_ROUTE_MASK] = STRTOUL(argv[2], 0, 0); ospf_import_template_key[OSPF_IMPORT_TEMPLATE_PEER_ADDR] = STRTOUL(argv[3], 0, 0); ospf_import_template_key[OSPF_IMPORT_TEMPLATE_PEER_MASK] = STRTOUL(argv[4], 0, 0); ospf_import_template_key[OSPF_IMPORT_TEMPLATE_TAG] = STRTOUL(argv[5], 0, 0); template_bits = STRTOUL(argv[6], 0, 0); template_flags = STRTOUL(argv[7], 0, 0); pref = STRTOUL(argv[8], 0, 0); if (!ospf_mgr_add_import_template(ospf, template_num, ospf_import_template_key, template_bits, template_flags, pref)) fprintf(stderr, "Couldn't set OSPF import template\n");}void netconfig_ospf_export_template(bits32_t *entity, int argc, char *argv[]){ struct OSPF *ospf = (struct OSPF *) *entity; bits32_t ospf_export_template_key[OSPF_EXPORT_TEMPLATE_KEY_LEN]; bits32_t template_num, template_bits, template_flags, metric, tag; if (argc != (OSPF_EXPORT_TEMPLATE_KEY_LEN + 5)) { fprintf(stderr, "---OSPF Export Template Size Mismatch---\n"); return; } MEMSET(&ospf_export_template_key, 0, sizeof(ospf_export_template_key)); template_num = STRTOUL(argv[0], 0, 0); ospf_export_template_key[OSPF_EXPORT_TEMPLATE_ROUTE_ADDR] = STRTOUL(argv[1], 0, 0); ospf_export_template_key[OSPF_EXPORT_TEMPLATE_ROUTE_MASK] = STRTOUL(argv[2], 0, 0); ospf_export_template_key[OSPF_EXPORT_TEMPLATE_PROTO] = STRTOUL(argv[3], 0, 0); ospf_export_template_key[OSPF_EXPORT_TEMPLATE_SPECIFIC1] = STRTOUL(argv[4], 0, 0); ospf_export_template_key[OSPF_EXPORT_TEMPLATE_SPECIFIC2] = STRTOUL(argv[5], 0, 0); ospf_export_template_key[OSPF_EXPORT_TEMPLATE_TAG] = STRTOUL(argv[6], 0, 0); template_bits = STRTOUL(argv[7], 0, 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -