📄 config.c
字号:
/* $Id: config.c,v 1.9 2003/04/12 00:25:32 shirleyma Exp $ *//* ported from KAME: config.c,v 1.21 2002/09/24 14:20:49 itojun Exp *//* * Copyright (C) 2002 WIDE Project. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the project nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */#include <sys/types.h>#include <sys/socket.h>#include <net/if.h>#include <netinet/in.h>#include <syslog.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <ifaddrs.h>#include "queue.h"#include "dhcp6.h"#include "config.h"#include "common.h"extern int errno;static struct dhcp6_ifconf *dhcp6_ifconflist;static struct host_conf *host_conflist0, *host_conflist;static struct dhcp6_list dnslist0; enum { DHCPOPTCODE_SEND, DHCPOPTCODE_REQUEST, DHCPOPTCODE_ALLOW };static int add_options __P((int, struct dhcp6_ifconf *, struct cf_list *));static int add_address __P((struct dhcp6_list *, struct dhcp6_addr *));static void clear_ifconf __P((struct dhcp6_ifconf *));static void clear_hostconf __P((struct host_conf *));static void clear_options __P((struct dhcp6_optconf *));intconfigure_interface(const struct cf_namelist *iflist){ const struct cf_namelist *ifp; struct dhcp6_ifconf *ifc; for (ifp = iflist; ifp; ifp = ifp->next) { struct cf_list *cfl; if ((ifc = malloc(sizeof(*ifc))) == NULL) { dprintf(LOG_ERR, "%s" "memory allocation for %s failed", FNAME, ifp->name); goto bad; } memset(ifc, 0, sizeof(*ifc)); ifc->next = dhcp6_ifconflist; dhcp6_ifconflist = ifc; if ((ifc->ifname = strdup(ifp->name)) == NULL) { dprintf(LOG_ERR, "%s" "failed to copy ifname", FNAME); goto bad; } ifc->server_pref = DH6OPT_PREF_UNDEF; TAILQ_INIT(&ifc->reqopt_list); TAILQ_INIT(&ifc->addr_list); for (cfl = ifp->params; cfl; cfl = cfl->next) { switch(cfl->type) { case DECL_REQUEST: if (dhcp6_mode != DHCP6_MODE_CLIENT) { dprintf(LOG_INFO, "%s" "%s:%d " "client-only configuration", FNAME, configfilename, cfl->line); goto bad; } if (add_options(DHCPOPTCODE_REQUEST, ifc, cfl->list)) { goto bad; } break; case DECL_SEND: if (add_options(DHCPOPTCODE_SEND, ifc, cfl->list)) { goto bad; } break; case DECL_ALLOW: if (add_options(DHCPOPTCODE_ALLOW, ifc, cfl->list)) { goto bad; } break; case DECL_INFO_ONLY: if (dhcp6_mode == DHCP6_MODE_CLIENT) ifc->send_flags |= DHCIFF_INFO_ONLY; break; case DECL_TEMP_ADDR: if (dhcp6_mode == DHCP6_MODE_CLIENT) ifc->send_flags |= DHCIFF_TEMP_ADDRS; break; case DECL_PREFERENCE: if (dhcp6_mode != DHCP6_MODE_SERVER) { dprintf(LOG_INFO, "%s" "%s:%d " "server-only configuration", FNAME, configfilename, cfl->line); goto bad; } ifc->server_pref = (int)cfl->num; if (ifc->server_pref < 0 || ifc->server_pref > 255) { dprintf(LOG_INFO, "%s" "%s:%d " "bad value: %d", FNAME, configfilename, cfl->line, ifc->server_pref); goto bad; } break; case DECL_IAID: if (ifc->iaidinfo.iaid) { dprintf(LOG_ERR, "%s" "%s:%d " "duplicated IAID for %s", FNAME, configfilename, cfl->line, ifc->ifname); goto bad; } else ifc->iaidinfo.iaid = (u_int32_t)cfl->num; break; case DECL_RENEWTIME: if (ifc->iaidinfo.renewtime) { dprintf(LOG_ERR, "%s" "%s:%d " "duplicated renewtime for %s", FNAME, configfilename, cfl->line, ifc->ifname); goto bad; } else ifc->iaidinfo.renewtime = (u_int32_t)cfl->num; break; case DECL_REBINDTIME: if (ifc->iaidinfo.iaid) { dprintf(LOG_ERR, "%s" "%s:%d " "duplicated rebindtime for %s", FNAME, configfilename, cfl->line, ifc->ifname); goto bad; } else ifc->iaidinfo.rebindtime = (u_int32_t)cfl->num; break; case DECL_ADDRESS: if (add_address(&ifc->addr_list, cfl->ptr)) { dprintf(LOG_ERR, "%s" "failed " "to configure ipv6address for %s", FNAME, ifc->ifname); goto bad; } break; case DECL_PREFIX_REQ: /* XX: ToDo */ break; case DECL_PREFIX_INFO: break; default: dprintf(LOG_ERR, "%s" "%s:%d " "invalid interface configuration", FNAME, configfilename, cfl->line); goto bad; } } } return (0); bad: clear_ifconf(dhcp6_ifconflist); dhcp6_ifconflist = NULL; return (-1);}intconfigure_host(const struct cf_namelist *hostlist){ const struct cf_namelist *host; struct host_conf *hconf; for (host = hostlist; host; host = host->next) { struct cf_list *cfl; if ((hconf = malloc(sizeof(*hconf))) == NULL) { dprintf(LOG_ERR, "%s" "memory allocation failed " "for host %s", FNAME, host->name); goto bad; } memset(hconf, 0, sizeof(*hconf)); TAILQ_INIT(&hconf->addr_list); TAILQ_INIT(&hconf->addr_binding_list); TAILQ_INIT(&hconf->prefix_list); TAILQ_INIT(&hconf->prefix_binding_list); hconf->next = host_conflist0; host_conflist0 = hconf; if ((hconf->name = strdup(host->name)) == NULL) { dprintf(LOG_ERR, "%s" "failed to copy host name: %s", FNAME, host->name); goto bad; } for (cfl = host->params; cfl; cfl = cfl->next) { switch(cfl->type) { case DECL_DUID: if (hconf->duid.duid_id) { dprintf(LOG_ERR, "%s" "%s:%d " "duplicated DUID for %s", FNAME, configfilename, cfl->line, host->name); goto bad; } if ((configure_duid((char *)cfl->ptr, &hconf->duid)) != 0) { dprintf(LOG_ERR, "%s" "%s:%d " "failed to configure " "DUID for %s", FNAME, configfilename, cfl->line, host->name); goto bad; } dprintf(LOG_DEBUG, "%s" "configure DUID for %s: %s", FNAME, host->name, duidstr(&hconf->duid)); break; case DECL_PREFIX: if (add_address(&hconf->prefix_list, cfl->ptr)) { dprintf(LOG_ERR, "%s" "failed " "to configure prefix for %s", FNAME, host->name); goto bad; } break; case DECL_IAID: if (hconf->iaidinfo.iaid) { dprintf(LOG_ERR, "%s" "%s:%d " "duplicated IAID for %s", FNAME, configfilename, cfl->line, host->name); goto bad; } else hconf->iaidinfo.iaid = (u_int32_t)cfl->num; break; case DECL_RENEWTIME: if (hconf->iaidinfo.renewtime) { dprintf(LOG_ERR, "%s" "%s:%d " "duplicated renewtime for %s", FNAME, configfilename, cfl->line, host->name); goto bad; } else hconf->iaidinfo.renewtime = (u_int32_t)cfl->num; break; case DECL_REBINDTIME: if (hconf->iaidinfo.rebindtime) { dprintf(LOG_ERR, "%s" "%s:%d " "duplicated rebindtime for %s", FNAME, configfilename, cfl->line, host->name); goto bad; } else hconf->iaidinfo.rebindtime = (u_int32_t)cfl->num; break; case DECL_ADDRESS: if (add_address(&hconf->addr_list, cfl->ptr)) { dprintf(LOG_ERR, "%s" "failed " "to configure ipv6address for %s", FNAME, host->name); goto bad; } break; case DECL_LINKLOCAL: if (IN6_IS_ADDR_UNSPECIFIED(&hconf->linklocal)) { dprintf(LOG_ERR, "%s" "%s:%d " "duplicated linklocal for %s", FNAME, configfilename, cfl->line, host->name); goto bad; } else memcpy(&hconf->linklocal, cfl->ptr, sizeof(hconf->linklocal) ); break; default: dprintf(LOG_ERR, "%s: %d invalid host configuration for %s", configfilename, cfl->line, host->name); goto bad; } } } return (0); bad: /* there is currently nothing special to recover the error */ return (-1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -