📄 dhcp-arp-discovery.c
字号:
/* $Header: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-arp-discovery.c,v 1.5 2003/06/08 22:14:16 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. * */#include "dhcp-local.h"#include "dhcp-libutil.h"#include "dhcp-librawnet.h"#define MODULE_NAME "dhcp-arp-discovery"/* Accept a list of arguments. Will contain rawnet first, and * then the ip address we're interested in. * * Returns: 1 on error, 0 on success. */static int check_for_arp_reply(void *data){ rawnet_t *net; unsigned char *addr, *received_addr; list_t *argument_list = data; net = list_first(argument_list); addr = list_second(argument_list); if(addr == NULL) { WARN_MESSAGE("no address passed to function -- skipping"); return 0; } received_addr = arp_get_sender_protocol_address(net->arp_p); if((net->type == RAWNET_ARP) && (arp_get_hardware_type(net->arp_p) == ARP_HRD_ETH) && (arp_get_protocol_type(net->arp_p) == ARP_PRO_IP) && (arp_get_hardware_len(net->arp_p) == ETH_ADDR_LEN) && (arp_get_protocol_len(net->arp_p) == IP_ADDR_LEN) && (arp_get_op(net->arp_p) == ARP_OP_REPLY)) { /* now check if its the address we're looking for */ if(!memcmp(addr, received_addr, IP_ADDR_LEN)) return 1; /* yes! address match */ else return 0; /* nope we got another response. */ } return 0; /* not the right kind of packet. */}int arp_discover_hardware_address(rawnet_t *net, int retries, int timeout, uint32_t address, eth_addr_t *mac_addr){ list_t *arg_list; unsigned char *sender_addr; int retval; arg_list = list_create(); list_add(arg_list, &address); list_add(arg_list, net); /* 0 for source ip, address to discover for dest, our * hardware address so we get a reply back. */ build_arp_request(net, net->cip_addr, address, net->chw_addr); while(retries--) { retval = rawnet_packet_transact(net, arg_list, NULL, check_for_arp_reply, timeout); switch (retval) { case RAWNET_TIMEOUT: /* we timed out. retry. */ break; case RAWNET_OK: /* Yummy! We got a valid reply! */ sender_addr = arp_get_sender_hardware_address(net->arp_p); memcpy(mac_addr->data, sender_addr, ETH_ADDR_LEN); list_destroy(arg_list, NULL); return 0; case RAWNET_ERROR: /* wops error. this is bad. */ ERROR_MESSAGE("received error from raw network handler."); list_destroy(arg_list, NULL); return 1; case RAWNET_USER_INTERRUPT: FATAL_MESSAGE("user interrupt. bailing out!"); default: /* not good. */ FATAL_MESSAGE("invalid return from rawnet_transact. this a bug report it."); } } list_destroy(arg_list, NULL); WARN_MESSAGE("timeout on discovery."); /* no response, return error. */ return 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -