⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dhcp-interface.c

📁 this is sample about DHCP-agent
💻 C
字号:
/* $Header: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-interface.c,v 1.9 2003/07/05 19:18:18 actmodern Exp $ *  * Copyright 2002 Thamer Alharbash <tmh@whitefang.com> *  * 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. The names of the authors may not be used to endorse or promote * products derived from this software without specific prior * written permission. *  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. *  * Wrapper around dnet's interface manipulation functions. * */#define MODULE_NAME "dhcp-interface"#include "dhcp-local.h"#include "dhcp-limits.h"#include "dhcp-libutil.h"#include "dhcp-librawnet.h"/* Internal utilities *//* get interface info. */static int interface_get_info(interface_control_t *ic){    if(intf_get(ic->interface_handle, ic->interface_entry) < 0) {        ERROR_MESSAGE("could not lookup interface %s : %s",                      ic->interface_entry->intf_name, strerror(errno));        return -1;    }    return 0;}/* set interface info. */static int interface_set_info(interface_control_t *ic){    if(intf_set(ic->interface_handle, ic->interface_entry) < 0) {        ERROR_MESSAGE("could not apply settings to interface %s : %s",                      ic->interface_entry->intf_name, strerror(errno));        return -1;    }    return 0;}/* Initialize interface handle. */interface_control_t *create_interface_control(char *name){    interface_control_t *ic;    void *buf;    ic = xcalloc(sizeof(interface_control_t));    ic->interface_handle = intf_open();    if(ic->interface_handle == NULL) { /* XXX -- update to dnet's error strings when dnet has some */        ERROR_MESSAGE("could not acquire interface handler");        return NULL;    }    buf = xcalloc(1024);    ic->interface_entry = buf;    strncpy(ic->interface_entry->intf_name, name, sizeof(ic->interface_entry->intf_name) - 1);    ic->interface_entry->intf_name[sizeof(ic->interface_entry->intf_name) - 1] = 0;    return ic;}void destroy_interface_control(interface_control_t *ic){    intf_close(ic->interface_handle);    xfree(ic->interface_entry);    xfree(ic);}/* Check to see if an interface is up. */int interface_is_up(interface_control_t *ic){    if(interface_get_info(ic))        return -1;    return (ic->interface_entry->intf_flags & INTF_FLAG_UP);}/* bring an interface up */int interface_up(interface_control_t *ic, ip_addr_t addr, uint32_t netmask, int mtu, uint8_t set_addr){    if(interface_get_info(ic)) {        ERROR_MESSAGE("could not bring up interface %s : %s",                      ic->interface_entry->intf_name, strerror(errno));        return -1;    }    if(set_addr) /* set an address as we bring this up. */ {        ic->interface_entry->intf_addr.addr_type = ADDR_TYPE_IP;        addr_mtob(&netmask, IP_ADDR_LEN, &ic->interface_entry->intf_addr.addr_bits);        memcpy(&ic->interface_entry->intf_addr.addr_ip, &addr, IP_ADDR_LEN);    }    if(mtu != -1)        ic->interface_entry->intf_mtu = mtu;    ic->interface_entry->intf_flags |= INTF_FLAG_UP;    if(interface_set_info(ic)) {        /* setting 0 address under Linux with dnet currently         * fails. so we'll ignore the error for now.  FIXME:         * update when dnet is fixed. (the address is set to 0         * but dnet doesn't get further than netmask settings).         */        /* ERROR_MESSAGE("could not bring up interface %s : %s",           ic->interface_entry->intf_name, strerror(errno));           return -1; */    }    return 0;}/* take down an interface. */int interface_down(interface_control_t *ic){    if(interface_get_info(ic)) {        ERROR_MESSAGE("could not bring down interface: %s : %s",                      ic->interface_entry->intf_name, strerror(errno));        return -1;    }    ic->interface_entry->intf_addr.addr_type = ADDR_TYPE_NONE;    ic->interface_entry->intf_flags &= ~INTF_FLAG_UP;    if(interface_set_info(ic)) {        ERROR_MESSAGE("could not bring down interface: %s : %s",                      ic->interface_entry->intf_name, strerror(errno));        return -1;    }    return 0;}/* get ip address of an interface. */int interface_get_ip_addr(interface_control_t *ic, uint32_t *addr){    if(interface_get_info(ic)) {        ERROR_MESSAGE("could not get interface IP address %s : %s",                      ic->interface_entry->intf_name, strerror(errno));        return -1;    }    memcpy(addr, &ic->interface_entry->intf_addr.addr_ip, IP_ADDR_LEN);    return 0;}/* utility check function for looping on interface list. * check whether is down or does not have an IP assigned to it. */static int check_interface_down(const struct intf_entry *entry){    if(entry->intf_type == INTF_TYPE_ETH) {        if(((entry->intf_flags & INTF_FLAG_UP) == 0) /* true if not up. */           ||(entry->intf_addr.addr_type == ADDR_TYPE_NONE)) /* or true if no address                                                                set. */            return 1;    }    return 0;}/* utility check function for looping on interface list. * check whether is down or does not have an IP assigned to it. */static int check_interface_up(const struct intf_entry *entry){    if(entry->intf_type == INTF_TYPE_ETH && (entry->intf_flags & INTF_FLAG_UP))        return 1;    return 0;}/* utility to get interface list and place it in a list. */static int list_interfaces(const struct intf_entry *entry, void *arg){    list_t *interfaces;    int (*check) (const struct intf_entry * entry);    char *intf_name = NULL;    interfaces = arg;    check = list_first(interfaces); /* first is always the routine to                                 check routine. */    /* Check the interface type is ethernet,     * is down OR has no IP address assigned to it.      * If it passes then add the name to the list.     */    if(check(entry)) {        intf_name = xstrdup(entry->intf_name);        list_add_to_end(interfaces, intf_name);    }    return 0;}static list_t *interfaces_get_proc(int (*check) (const struct intf_entry * entry)){    intf_t *interfaces;    list_t *interface_list;    interface_list = list_create();    /* add check as first datum. */    list_add(interface_list, check);    interfaces = intf_open();    if(interfaces == NULL) {        list_destroy(interface_list, NULL);        intf_close(interfaces);        ERROR_MESSAGE("could not obtain interface handle: %s", strerror(errno));        return NULL;    }    intf_loop(interfaces, list_interfaces, interface_list);    intf_close(interfaces);    list_remove_by_datum(interface_list, check); /* remove our check routine. */    return interface_list;}list_t *interface_get_active_interfaces(void){    return (interfaces_get_proc(check_interface_up));}list_t *interface_get_inactive_interfaces(void){    return (interfaces_get_proc(check_interface_down));}uint16_t interface_get_type(interface_control_t *ic){    if(interface_get_info(ic)) {        ERROR_MESSAGE("could not get interface data link type %s : %s",                      ic->interface_entry->intf_name, strerror(errno));        return DLT_NULL; /* do our best. */    }    return ic->interface_entry->intf_type;}uint16_t interface_get_mtu(interface_control_t *ic){    if(interface_get_info(ic)) {        ERROR_MESSAGE("could not get interface data link type %s : %s",                      ic->interface_entry->intf_name, strerror(errno));    }    return ic->interface_entry->intf_type;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -