[dhcp-agent-commits] dhcp-agent/src dhcp-arp-discovery.c,1.5,1.6 dhcp-client-control.c,1.20,1.21 dhc
Status: Alpha
Brought to you by:
actmodern
Update of /cvsroot/dhcp-agent/dhcp-agent/src In directory sc8-pr-cvs1:/tmp/cvs-serv23174/src Modified Files: dhcp-arp-discovery.c dhcp-client-control.c dhcp-icmp-discovery.c dhcp-interface.c dhcp-interface.h dhcp-librawnet.h dhcp-rawnet.c dhcp-sniff.c Log Message: revamped raw network; now two creation routines are used; less system calls used Index: dhcp-arp-discovery.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-arp-discovery.c,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** dhcp-arp-discovery.c 8 Jun 2003 22:14:16 -0000 1.5 --- dhcp-arp-discovery.c 25 Jul 2003 02:36:21 -0000 1.6 *************** *** 74,77 **** --- 74,80 ---- unsigned char *sender_addr; int retval; + eth_addr_t eth_addr; + + rawnet_get_real_hw_addr(net, ð_addr); arg_list = list_create(); *************** *** 82,86 **** * hardware address so we get a reply back. */ ! build_arp_request(net, net->cip_addr, address, net->chw_addr); while(retries--) { --- 85,89 ---- * hardware address so we get a reply back. */ ! build_arp_request(net, rawnet_get_ip_addr(net), address, eth_addr); while(retries--) { Index: dhcp-client-control.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-client-control.c,v retrieving revision 1.20 retrieving revision 1.21 diff -C2 -d -r1.20 -r1.21 *** dhcp-client-control.c 17 Jul 2003 00:35:52 -0000 1.20 --- dhcp-client-control.c 25 Jul 2003 02:36:21 -0000 1.21 *************** *** 166,171 **** if((dc->rawnet = ! rawnet_create(interface, stringbuffer_getstring(filter), client_conf_get_default_mtu(dc->conf), sport, dport, ! promiscuous, clear_interface)) == NULL) { ERROR_MESSAGE("could not acquire rawnet handler."); dhcp_client_control_destroy(dc); --- 166,170 ---- if((dc->rawnet = ! rawnet_create_initialize(interface, stringbuffer_getstring(filter), client_conf_get_default_mtu(dc->conf), sport, dport, 1)) == NULL) { ERROR_MESSAGE("could not acquire rawnet handler."); dhcp_client_control_destroy(dc); Index: dhcp-icmp-discovery.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-icmp-discovery.c,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** dhcp-icmp-discovery.c 8 Jun 2003 22:14:16 -0000 1.5 --- dhcp-icmp-discovery.c 25 Jul 2003 02:36:21 -0000 1.6 *************** *** 123,131 **** struct timeval before, after, difference; int latency; id = get_random_uint16(); seq = 1; ! build_icmp_echo_request(net, net->cip_addr, dest_addr, net->chw_addr, dest_mac, id, seq); gettimeofday(&before, NULL); --- 123,134 ---- struct timeval before, after, difference; int latency; + eth_addr_t eth_addr; + + rawnet_get_real_hw_addr(net, ð_addr); id = get_random_uint16(); seq = 1; ! build_icmp_echo_request(net, rawnet_get_ip_addr(net), dest_addr, eth_addr, dest_mac, id, seq); gettimeofday(&before, NULL); Index: dhcp-interface.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-interface.c,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** dhcp-interface.c 5 Jul 2003 19:18:18 -0000 1.9 --- dhcp-interface.c 25 Jul 2003 02:36:21 -0000 1.10 *************** *** 59,63 **** /* Initialize interface handle. */ ! interface_control_t *create_interface_control(char *name) { interface_control_t *ic; --- 59,63 ---- /* Initialize interface handle. */ ! interface_control_t *create_interface_control(const char *name) { interface_control_t *ic; *************** *** 278,281 **** } ! return ic->interface_entry->intf_type; } --- 278,281 ---- } ! return ic->interface_entry->intf_mtu; } Index: dhcp-interface.h =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-interface.h,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** dhcp-interface.h 5 Jul 2003 19:18:18 -0000 1.5 --- dhcp-interface.h 25 Jul 2003 02:36:21 -0000 1.6 *************** *** 26,30 **** #define DHCP_INTERFACE_H ! extern interface_control_t *create_interface_control(char *name); extern void destroy_interface_control(interface_control_t *ic); extern int interface_up(interface_control_t *ic, ip_addr_t addr, uint32_t netmask, int mtu, uint8_t set_addr); --- 26,30 ---- #define DHCP_INTERFACE_H ! extern interface_control_t *create_interface_control(const char *name); extern void destroy_interface_control(interface_control_t *ic); extern int interface_up(interface_control_t *ic, ip_addr_t addr, uint32_t netmask, int mtu, uint8_t set_addr); Index: dhcp-librawnet.h =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-librawnet.h,v retrieving revision 1.22 retrieving revision 1.23 diff -C2 -d -r1.22 -r1.23 *** dhcp-librawnet.h 20 Jul 2003 05:50:56 -0000 1.22 --- dhcp-librawnet.h 25 Jul 2003 02:36:21 -0000 1.23 *************** *** 180,194 **** uint16_t src_port, dst_port; /* udp ports for writing packets. */ ! char *device; /* device name. */ char *packet_data; /* packet data. */ ! int packet_len; /* total length of packet. */ ! struct timeval tstamp; /* timestamp. */ ! eth_addr_t chw_addr; /* our hardware address. */ ! ip_addr_t cip_addr; /* our ip address. */ ! eth_addr_t *fake_hw_addr; /* our fake hardware address. */ int promiscuous; /* run in promiscuous mode flag. */ --- 180,194 ---- uint16_t src_port, dst_port; /* udp ports for writing packets. */ ! char *device; /* device name. */ char *packet_data; /* packet data. */ ! int packet_len; /* total length of packet that we read. */ ! struct timeval tstamp; /* timestamp of most recently read packet. */ ! eth_addr_t hw_addr; /* our hardware address. */ ! ip_addr_t ip_addr; /* our ip address. */ ! eth_addr_t *fake_hw_addr; /* our fake hardware address, if any. */ int promiscuous; /* run in promiscuous mode flag. */ *************** *** 363,395 **** /* rawnet routines. */ ! extern rawnet_t *rawnet_create(const char *device, const char *filter, int mtu, int sport, ! int dport, int promiscuous, int clear_address); ! extern void rawnet_destroy(rawnet_t *net); ! extern int rawnet_get_packet(rawnet_t *net, struct timeval *tm); extern int rawnet_get_hw_addr(rawnet_t *net, eth_addr_t *addr); extern int rawnet_get_real_hw_addr(rawnet_t *net, eth_addr_t *addr); ! extern ip_addr_t rawnet_get_ip_addr(rawnet_t *net); ! extern uint16_t rawnet_get_mtu(rawnet_t *net); ! extern int rawnet_send_packet(rawnet_t *net); ! extern int rawnet_use_fake_hw_addr(rawnet_t *raw, char *mac_string); ! extern void reinitialize_rawnet(rawnet_t *net, int promiscuous); ! ! extern int rawnet_up(rawnet_t *net); ! extern void rawnet_down(rawnet_t *net); ! ! extern int rawnet_packet_transact(rawnet_t *net, void *arg, ! void (*update) (void *arg), ! int (*check) (void *arg), time_t max_timeout); ! ! extern int rawnet_interface_up(rawnet_t *net, ip_addr_t addr, ip_addr_t netmask, int mtu, int clear_address); extern int rawnet_interface_down(rawnet_t *net); extern list_t *rawnet_list_active_interfaces(void); extern list_t *rawnet_list_inactive_interfaces(void); ! extern void rawnet_dhcp_update(rawnet_t *net, time_t seconds); - extern int rawnet_is_dhcp_offer(rawnet_t *net); extern int rawnet_is_valid(rawnet_t *net); ! extern const char *rawnet_get_device_name(rawnet_t *net); extern int rawnet_get_datalink_type(rawnet_t *net); /* packet building routines. */ --- 363,392 ---- /* rawnet routines. */ ! extern ip_addr_t rawnet_get_ip_addr(rawnet_t *net); ! extern int rawnet_port_for_service(const char *serv, const char *proto); ! extern char *rawnet_network_address_to_string_static(uint32_t addr); extern int rawnet_get_hw_addr(rawnet_t *net, eth_addr_t *addr); extern int rawnet_get_real_hw_addr(rawnet_t *net, eth_addr_t *addr); ! extern int rawnet_interface_up(rawnet_t *net, ip_addr_t addr, ip_addr_t netmask, int mtu, int set_address); extern int rawnet_interface_down(rawnet_t *net); + extern uint16_t rawnet_get_mtu(rawnet_t *net); extern list_t *rawnet_list_active_interfaces(void); extern list_t *rawnet_list_inactive_interfaces(void); ! extern rawnet_t *rawnet_create_initialize(const char *device, const char *filter, int mtu, ! int dhcp_src_port, int dhcp_dst_port, int promiscious); ! extern rawnet_t *rawnet_create_open(const char *device, const char *filter, int dhcp_src_port, ! int dhcp_dst_port, int promiscious); ! extern int rawnet_use_fake_hw_addr(rawnet_t *net, char *mac_string); ! extern void rawnet_destroy(rawnet_t *net); ! extern void rawnet_down(rawnet_t *net); ! extern int rawnet_up(rawnet_t *net); ! extern void reinitialize_rawnet(rawnet_t *net, int promiscuous); ! extern int rawnet_get_packet(rawnet_t *net, struct timeval *tm); extern void rawnet_dhcp_update(rawnet_t *net, time_t seconds); extern int rawnet_is_valid(rawnet_t *net); ! extern int rawnet_packet_transact(rawnet_t *net, void *arg, void (*update) (void *arg), ! int (*check) (void *arg), time_t max_timeout); extern int rawnet_get_datalink_type(rawnet_t *net); + extern int rawnet_send_packet(rawnet_t *net); /* packet building routines. */ Index: dhcp-rawnet.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-rawnet.c,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** dhcp-rawnet.c 16 Jul 2003 05:42:24 -0000 1.13 --- dhcp-rawnet.c 25 Jul 2003 02:36:21 -0000 1.14 *************** *** 23,26 **** --- 23,28 ---- * Raw network module. * + * All interface manipulation, raw packet I/O is done here. + * */ *************** *** 38,48 **** ************************/ ! /* get IP address. */ ip_addr_t rawnet_get_ip_addr(rawnet_t *net) { ! return net->cip_addr; } ! /* get port number for named service. */ int rawnet_port_for_service(const char *serv, const char *proto) { --- 40,50 ---- ************************/ ! /* get our ip address. */ ip_addr_t rawnet_get_ip_addr(rawnet_t *net) { ! return net->ip_addr; } ! /* get a port for a service. */ int rawnet_port_for_service(const char *serv, const char *proto) { *************** *** 59,67 **** } ! /* variation on network_addr_to_string in dhcp-convert.c we use ! * this when we don't want to run around freeing results up, and ! * know that it will be statically stored. Useful for insertion ! * in message log routines. */ ! char *rawnet_network_address_to_string_static(uint32_t addr) { --- 61,65 ---- } ! /* network address to a string: static version. */ char *rawnet_network_address_to_string_static(uint32_t addr) { *************** *** 84,88 **** return 0; } else { ! memcpy(addr, &net->chw_addr, ETH_ADDR_LEN); return 0; } --- 82,86 ---- return 0; } else { ! memcpy(addr, &net->hw_addr, ETH_ADDR_LEN); return 0; } *************** *** 92,96 **** int rawnet_get_real_hw_addr(rawnet_t *net, eth_addr_t *addr) { ! return (eth_get(net->eth, addr)); } --- 90,95 ---- int rawnet_get_real_hw_addr(rawnet_t *net, eth_addr_t *addr) { ! memcpy(addr, &net->hw_addr, ETH_ADDR_LEN); ! return 0; } *************** *** 98,102 **** int rawnet_interface_up(rawnet_t *net, ip_addr_t addr, ip_addr_t netmask, int mtu, int set_address) { - if(interface_up(net->intf_handle, addr, netmask, mtu, set_address)) { ERROR_MESSAGE("could not bring interface up."); --- 97,100 ---- *************** *** 104,111 **** } ! interface_get_ip_addr(net->intf_handle, &net->cip_addr); xfree(net->packet_data); ! net->packet_data = xmalloc(mtu); return 0; --- 102,112 ---- } ! /* set our new vales from the interface. */ + eth_get(net->eth, &net->hw_addr); + interface_get_ip_addr(net->intf_handle, &net->ip_addr); + net->mtu = interface_get_mtu(net->intf_handle); xfree(net->packet_data); ! net->packet_data = xcalloc(net->mtu); return 0; *************** *** 120,132 **** } ! memset(&net->cip_addr, 0, IP_ADDR_LEN); /* clear our IP address -- this isn't very useful though. */ return 0; } uint16_t rawnet_get_mtu(rawnet_t *net) { ! return interface_get_mtu(net->intf_handle); } list_t *rawnet_list_active_interfaces(void) { --- 121,135 ---- } ! memset(&net->ip_addr, 0, IP_ADDR_LEN); /* clear our IP address -- this isn't very useful though. */ return 0; } + /* get the interface mtu. */ uint16_t rawnet_get_mtu(rawnet_t *net) { ! return net->mtu; } + /* get list of active interfaces. */ list_t *rawnet_list_active_interfaces(void) { *************** *** 134,138 **** } ! list_t *rawnet_list_inactive_interfaces(void) { --- 137,141 ---- } ! /* get list of inactive interfaces. */ list_t *rawnet_list_inactive_interfaces(void) { *************** *** 216,285 **** } ! /* Create the workhorse raw network module. */ ! ! rawnet_t *rawnet_create(const char *device, const char *filter, int mtu, ! int dhcp_src_port, int dhcp_dst_port, int promiscuous, int clear_address) { rawnet_t *net; int retval; - net = xcalloc(sizeof(rawnet_t)); - net->src_port = dhcp_src_port; - net->dst_port = dhcp_dst_port; - net->device = xstrdup(device); - net->pcap_filter = xstrdup(filter); - net->promiscuous = promiscuous; - net->mtu = mtu; - - /* get the interface handle for bringing it up or down and querying it. */ - net->intf_handle = create_interface_control(net->device); - if(net->intf_handle == NULL) { - ERROR_MESSAGE("could not acquire interface handle"); - rawnet_destroy(net); - return NULL; - } - /* Is the interface up? */ ! retval = interface_is_up(net->intf_handle); if(retval == -1) { /* error on interface detection */ ERROR_MESSAGE("error on interface detection for device: %s", device); - rawnet_destroy(net); return NULL; } if(retval == 0) { ! /* interface down */ ! /* bring it up with dum values. */ ! if(rawnet_interface_up(net, 0, 0, net->mtu, clear_address)) { ! ERROR_MESSAGE("error trying to bring device up: %s", device); ! rawnet_destroy(net); ! return NULL; ! } ! } else if(clear_address) { ! ! /* interface down but we've been told to clear its address, ! * so our caller wants us to initialize it either way. */ ! ! if(rawnet_interface_up(net, 0, 0, net->mtu, clear_address)) { ! ERROR_MESSAGE("error trying to bring device up: %s", device); ! rawnet_destroy(net); ! return NULL; ! } } ! if(net->mtu == -1) { /* were we passed an mtu? */ ! /* if not get it from the interface that was just brought up. */ ! net->mtu = rawnet_get_mtu(net); ! } ! /* now allocate packet data since we know the mtu. */ ! net->packet_data = xcalloc(net->mtu); ! /* any other value of retval and the interface is already up. */ ! /* Get the interface and make sure its an ethernet interface or ! * else we're hosed. */ net->eth = eth_open(device); --- 219,261 ---- } ! /* create the raw network module. */ ! static rawnet_t *rawnet_create_proc(const char *device, interface_control_t *intf_handle, const char *filter, ! int dhcp_src_port, int dhcp_dst_port, int promiscuous) { rawnet_t *net; int retval; /* Is the interface up? */ ! retval = interface_is_up(intf_handle); if(retval == -1) { /* error on interface detection */ + destroy_interface_control(intf_handle); ERROR_MESSAGE("error on interface detection for device: %s", device); return NULL; } if(retval == 0) { ! destroy_interface_control(intf_handle); ! ERROR_MESSAGE("attempt to create raw network handler on downed interface: %s", device); ! return NULL; } ! /* setup values. */ ! /* base values we already know about. */ ! net = xcalloc(sizeof(rawnet_t)); ! net->src_port = dhcp_src_port; ! net->dst_port = dhcp_dst_port; ! net->device = xstrdup(device); ! net->pcap_filter = xstrdup(filter); ! net->promiscuous = promiscuous; ! net->intf_handle = intf_handle; ! /* other values and handler we setup here. ! * begin by setting up our ethernet writing handler. */ ! /* Get the libdnet ethernet handler and make sure the device ! * is an ethernet interface or else we're hosed. */ net->eth = eth_open(device); *************** *** 290,297 **** --- 266,282 ---- } + /* we can go ahead and get mtu, along with addresses. */ + eth_get(net->eth, &net->hw_addr); + interface_get_ip_addr(net->intf_handle, &net->ip_addr); + net->mtu = interface_get_mtu(intf_handle); + + /* allocate our packet data buffer based on our mtu. */ + net->packet_data = xcalloc(net->mtu); + /* get pcap handler */ net->pcap = initialize_pcap_device(net->device, net->mtu, net->pcap_filter, promiscuous); if(net->pcap == NULL) { ERROR_MESSAGE("could not initialize pcap device for: %s", device); + rawnet_destroy(net); return NULL; } *************** *** 304,312 **** } - /* store a copy of our hardware and ip address. */ - - eth_get(net->eth, &net->chw_addr); - interface_get_ip_addr(net->intf_handle, &net->cip_addr); - /* Packet handler objects. * --- 289,292 ---- *************** *** 322,328 **** --- 302,351 ---- net->dhcp_p = dhcp_create(); + /* dont. */ return net; } + /* initialize an interface, and bring it up if needed. we also clear the address. */ + rawnet_t *rawnet_create_initialize(const char *device, const char *filter, int mtu, + int dhcp_src_port, int dhcp_dst_port, int promiscious) + { + interface_control_t *intf_handle; + + /* get the interface handle for bringing it up or down and querying it. */ + intf_handle = create_interface_control(device); + if(intf_handle == NULL) { + ERROR_MESSAGE("could not acquire interface handle"); + return NULL; + } + + /* initialize the interface. */ + if(interface_up(intf_handle, 0, 0, mtu, 1) < 0) { + ERROR_MESSAGE("error trying to bring device up: %s", device); + destroy_interface_control(intf_handle); + return NULL; + } + + return rawnet_create_proc(device, intf_handle, filter, + dhcp_src_port, dhcp_dst_port, promiscious); + } + + /* open an existing interface. use current interface values. */ + rawnet_t *rawnet_create_open(const char *device, const char *filter, int dhcp_src_port, + int dhcp_dst_port, int promiscious) + { + interface_control_t *intf_handle; + + /* get the interface handle for bringing it up or down and querying it. */ + intf_handle = create_interface_control(device); + if(intf_handle == NULL) { + ERROR_MESSAGE("could not acquire interface handle"); + return NULL; + } + + return rawnet_create_proc(device, intf_handle, filter, + dhcp_src_port, dhcp_dst_port, promiscious); + } + + /* Use a fake hardware address (diagnostic purposes). We need to * reinitialize our rawnet to use promiscuous packet capturing *************** *** 342,346 **** } ! memcpy(net->chw_addr.data, mac_addr, ETH_ADDR_LEN); /* now reinitialize in promiscuous mode */ --- 365,369 ---- } ! memcpy(net->fake_hw_addr->data, mac_addr, ETH_ADDR_LEN); /* now reinitialize in promiscuous mode */ *************** *** 350,353 **** --- 373,377 ---- } + /* destroy the rawnetwork handler. */ void rawnet_destroy(rawnet_t *net) { *************** *** 397,400 **** --- 421,425 ---- * to fill up. */ + /* bring raw networking down. */ void rawnet_down(rawnet_t *net) { *************** *** 412,415 **** --- 437,441 ---- } + /* bring raw networking up. */ int rawnet_up(rawnet_t *net) { *************** *** 437,440 **** --- 463,467 ---- } + /* reinitialize the raw network handler. useful for bringing up and down. */ void reinitialize_rawnet(rawnet_t *net, int promiscuous) { *************** *** 456,459 **** --- 483,487 ---- */ + /* get the next packet waiting us. */ int rawnet_get_packet(rawnet_t *net, struct timeval *tm) { *************** *** 580,583 **** --- 608,612 ---- } + /* send a packet. */ int rawnet_send_packet(rawnet_t *net) { *************** *** 591,598 **** /* Just update the seconds field. ! * ! * FIXME: mod this so that it hooks ! * into the rest of the framework. ! * right now it's error prone. */ --- 620,624 ---- /* Just update the seconds field. ! * field in an existing dhcp packet. */ Index: dhcp-sniff.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-sniff.c,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** dhcp-sniff.c 28 Jun 2003 16:30:01 -0000 1.8 --- dhcp-sniff.c 25 Jul 2003 02:36:21 -0000 1.9 *************** *** 119,123 **** /* It's ok not to set ports since we're not writing any packets. */ ! net = rawnet_create(interface_name, stringbuffer_getstring(filter), DEFAULT_SNIFF_SNAPLEN, 0, 0, 1, 0); if(net == NULL) { FATAL_MESSAGE("unable to access raw network"); --- 119,123 ---- /* It's ok not to set ports since we're not writing any packets. */ ! net = rawnet_create_open(interface_name, stringbuffer_getstring(filter), dport, sport, 1); if(net == NULL) { FATAL_MESSAGE("unable to access raw network"); |