[Dhcp-agent-commits] CVS: dhcp-agent TODO,1.7,1.8 dhcp-agent.h,1.24,1.25 dhcp-daemon.c,1.3,1.4 dhcp-
Status: Alpha
Brought to you by:
actmodern
From: Thamer Al-H. <act...@us...> - 2002-02-14 10:06:58
|
Update of /cvsroot/dhcp-agent/dhcp-agent In directory usw-pr-cvs1:/tmp/cvs-serv29302 Modified Files: TODO dhcp-agent.h dhcp-daemon.c dhcp-icmp.c dhcp-net.c Log Message: fixed up rawnet a bit and added build icmp routines to it; fixed up daemon a bit; both these need a lot more work; Index: TODO =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/TODO,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** TODO 11 Feb 2002 18:02:24 -0000 1.7 --- TODO 14 Feb 2002 10:06:53 -0000 1.8 *************** *** 45,46 **** --- 45,53 ---- clean up in areas where it seems prudent. or come up with guidelines we can stick to. + -- fixup error messages to use "name : name : name : error" + convention where name could be the name of the process, + module, or function. + -- rawnet's packet construction isn't squeaky clean -- make a new + source file called dhcp-build-packets.c and polish it up + -- rawnet and the dhcp client control are foobared by a fake mac + address. check, fix, clean. Index: dhcp-agent.h =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/dhcp-agent.h,v retrieving revision 1.24 retrieving revision 1.25 diff -C2 -d -r1.24 -r1.25 *** dhcp-agent.h 12 Feb 2002 15:23:59 -0000 1.24 --- dhcp-agent.h 14 Feb 2002 10:06:53 -0000 1.25 *************** *** 274,278 **** uint16_t src_port, dst_port; /* udp ports for writing packets. */ ! unsigned char *client_hw_addr; /* used for faking client mac address. */ int promiscious; /* run in promiscious mode flag. */ --- 274,278 ---- uint16_t src_port, dst_port; /* udp ports for writing packets. */ ! eth_addr_t hw_addr; /* used for faking client mac address. */ int promiscious; /* run in promiscious mode flag. */ *************** *** 408,412 **** #define DHCP_BROADCAST_FLAG 0x8000 ! /* Option tags. Just PAD and END */ # define TAG_PAD 0 --- 408,412 ---- #define DHCP_BROADCAST_FLAG 0x8000 ! /* Option tags. */ # define TAG_PAD 0 *************** *** 493,498 **** eth_addr_t source_hw_addr, eth_addr_t dest_hw_addr); ! void rawnet_build_unarp(rawnet_t *net, uint32_t source_addr, ! eth_addr_t source_hw_addr); extern void rawnet_dhcp_update(rawnet_t *net, time_t seconds); --- 493,498 ---- eth_addr_t source_hw_addr, eth_addr_t dest_hw_addr); ! extern void rawnet_build_unarp(rawnet_t *net, uint32_t source_addr, ! eth_addr_t source_hw_addr); extern void rawnet_dhcp_update(rawnet_t *net, time_t seconds); *************** *** 501,504 **** --- 501,507 ---- extern int rawnet_is_valid(rawnet_t *net); + extern void rawnet_build_icmp_mask_request(rawnet_t *net, uint32_t id, uint32_t seq); + extern void rawnet_build_icmp_mask_reply(rawnet_t *net, uint32_t id, uint32_t seq, uint32_t subnet_mask); + extern int rawnet_up(rawnet_t *net); extern void rawnet_down(rawnet_t *net); *************** *** 720,726 **** extern icmp_obj *icmp_create(void); extern void icmp_destroy(icmp_obj *icmp); extern void icmp_set_type(icmp_obj *icmp, uint8_t type); extern void icmp_set_code(icmp_obj *icmp, uint8_t code); ! extern void icmp_set_cksum(icmp_obj *icmp, uint16_t cksum); extern void icmp_mask_set_id(icmp_obj *icmp, uint32_t id); extern void icmp_mask_set_seq(icmp_obj *icmp, uint32_t seq); --- 723,730 ---- extern icmp_obj *icmp_create(void); extern void icmp_destroy(icmp_obj *icmp); + extern void icmp_set_type(icmp_obj *icmp, uint8_t type); extern void icmp_set_code(icmp_obj *icmp, uint8_t code); ! extern void icmp_clear_cksum(icmp_obj *icmp); extern void icmp_mask_set_id(icmp_obj *icmp, uint32_t id); extern void icmp_mask_set_seq(icmp_obj *icmp, uint32_t seq); *************** *** 728,731 **** --- 732,739 ---- extern void icmp_echo_set_id(icmp_obj *icmp, uint32_t id); extern void icmp_echo_set_seq(icmp_obj *icmp, uint32_t seq); + + extern uint8_t icmp_get_type(icmp_obj *icmp); + + extern void icmp_write_packet_image(icmp_obj *icmp, unsigned char *packet); /* Alignment functions. */ Index: dhcp-daemon.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/dhcp-daemon.c,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** dhcp-daemon.c 5 Feb 2002 13:47:38 -0000 1.3 --- dhcp-daemon.c 14 Feb 2002 10:06:53 -0000 1.4 *************** *** 82,118 **** */ ! void go_background(char *dir) ! { ! long i, max_descriptors; ! ! /* If we have daemon() use it. */ ! #ifdef HAVE_DAEMON ! daemon(0 , 0); ! #else ! ! /* Take process into the background. ! * ! * There are seven steps we need to ! * take. ! */ ! /* Step 1: fork out of current parent. */ switch(fork()) { case -1: ! /* Not good. ! * ! * And our problem is logging hasn't been setup yet, ! * open the log up and log an error. ! * ! */ ! ! init_log(binname); ! fatal_error("initialization: fork: %s", strerror(errno)); case 0: ! /* We're the child. Just break out of here. */ ! break; default: --- 82,103 ---- */ ! /* The name describes the function: we fork and only return as ! the child. Useful utility routine. */ ! #ifndef HAVE_DAEMON ! static void do_fork_and_return_as_child(void) ! { switch(fork()) { case -1: ! /* Not good. */ ! /* Also we haven't opened logging yet, so do it now and exit. */ ! init_log(binname); ! fatal_error("initialization: fork: %s", strerror(errno)); case 0: ! /* We're the child. Just break out of here. */ ! break; default: *************** *** 125,129 **** } ! /* Step 2: Grab a new session. */ setsid(); /* Get new session. */ --- 110,137 ---- } ! return; ! } ! #endif /* HAVE_DAEMON */ ! ! void go_background(char *dir) ! { ! long i, max_descriptors; ! ! /* If we have daemon() use it. */ ! #ifdef HAVE_DAEMON ! daemon(0 , 0); ! #else ! ! /* Take process into the background. ! * ! * There are seven steps we need to ! * take. ! */ ! ! /* Step 1: fork out of current parent. */ ! do_fork_and_return_as_child(); ! ! /* Step 2: Grab a new session and relieve ourselves of ! session leadership. */ setsid(); /* Get new session. */ *************** *** 133,156 **** * of the responsibility of being * a session leader. */ - - - switch(fork()) { - - case -1: - /* Not good. */ - init_log(binname); - fatal_error("initialization: fork: %s", strerror(errno)); - - case 0: - /* We're the child. Just break out of here. */ - break; - - default: - /* We're the parent. - * Die. - */ - exit(0); ! } #endif --- 141,146 ---- * of the responsibility of being * a session leader. */ ! do_fork_and_return_as_child(); #endif Index: dhcp-icmp.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/dhcp-icmp.c,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** dhcp-icmp.c 12 Feb 2002 15:23:59 -0000 1.3 --- dhcp-icmp.c 14 Feb 2002 10:06:53 -0000 1.4 *************** *** 39,42 **** --- 39,49 ---- } + /* get routines. */ + uint8_t icmp_get_type(icmp_obj *icmp) + { + return(icmp->icmp_header.icmp_type); + } + + /* set routines. */ void icmp_set_type(icmp_obj *icmp, uint8_t type) { *************** *** 49,55 **** } ! void icmp_clear_cksum(icmp_obj *icmp, uint16_t cksum) { ! icmp->icmp_header.icmp_cksum = cksum; } --- 56,62 ---- } ! void icmp_clear_cksum(icmp_obj *icmp) { ! memset(&icmp->icmp_header.icmp_cksum, 0, sizeof(icmp->icmp_header.icmp_cksum)); } *************** *** 119,123 **** packet += sizeof(icmp->icmp_header.icmp_cksum); ! switch(ntohl(icmp->icmp_header.icmp_type)) { --- 126,130 ---- packet += sizeof(icmp->icmp_header.icmp_cksum); ! switch(icmp->icmp_header.icmp_type) { Index: dhcp-net.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/dhcp-net.c,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** dhcp-net.c 9 Feb 2002 16:34:42 -0000 1.12 --- dhcp-net.c 14 Feb 2002 10:06:53 -0000 1.13 *************** *** 222,241 **** net->dhcp_p = dhcp_create(); return net; } void rawnet_use_fake_hw_addr(rawnet_t *net, char *mac_string) { unsigned char *mac_addr; ! ! if(net->client_hw_addr != NULL) ! xfree(net->client_hw_addr); ! mac_addr = xmalloc(ETH_ADDR_LEN); if(hex_string_to_value(mac_string, mac_addr)) fatal_error("malformed mac_string for fake client hw addr. please check mac address."); ! ! net->client_hw_addr = mac_addr; ! return; } --- 222,250 ---- net->dhcp_p = dhcp_create(); + /* For now we use the real hardware address in hw_addr. + * rawnet_use_fake_hw_addr can always be called to change + * this. */ + + rawnet_get_interface_hw_addr(net, &net->hw_addr); + return net; } + /* Use a fake hardware address (diagnostic purposes). We need to + * reinitialize our rawnet to use promiscious packet + * capturing. */ + void rawnet_use_fake_hw_addr(rawnet_t *net, char *mac_string) { unsigned char *mac_addr; ! ! mac_addr = xmalloc(ETH_ADDR_LEN); + if(hex_string_to_value(mac_string, mac_addr)) fatal_error("malformed mac_string for fake client hw addr. please check mac address."); ! memcpy(net->hw_addr.data, mac_addr, ETH_ADDR_LEN); ! xfree(mac_addr); ! return; } *************** *** 271,277 **** dhcp_destroy(net->dhcp_p); - if(net->client_hw_addr) - xfree(net->client_hw_addr); - xfree(net); --- 280,283 ---- *************** *** 446,450 **** } ! static void build_ip_broadcast(rawnet_t *net, uint16_t ip_len) { ip_set_hl(net->ip_p, IP_HDR_LEN); --- 452,456 ---- } ! static void build_ip_broadcast(rawnet_t *net, uint16_t ip_len, uint8_t protocol) { ip_set_hl(net->ip_p, IP_HDR_LEN); *************** *** 454,458 **** ip_set_off(net->ip_p, 0); ip_set_ttl(net->ip_p, IP_TTL_MAX); ! ip_set_proto(net->ip_p, IP_PROTO_UDP); /* Do checksum later. */ --- 460,464 ---- ip_set_off(net->ip_p, 0); ip_set_ttl(net->ip_p, IP_TTL_MAX); ! ip_set_proto(net->ip_p, protocol); /* Do checksum later. */ *************** *** 559,562 **** --- 565,590 ---- } + static void rawnet_write_packet_icmp(rawnet_t *net) + { + unsigned char *packet_ptr; + + /* Dump packet images for ICMP. */ + + packet_ptr = net->packet_data; + memset(packet_ptr, 0, DEFAULT_MTU); /* fixme: fix the default mtu issue! */ + + eth_write_packet_image(net->ether_p, packet_ptr); + packet_ptr += ETH_HDR_LEN; + + ip_write_packet_image(net->ip_p, packet_ptr); + packet_ptr += (ip_get_hl(net->ip_p)); + + icmp_write_packet_image(net->icmp_p, packet_ptr); + + ip_checksum((net->packet_data + ETH_HDR_LEN), (net->packet_len - ETH_HDR_LEN)); + + return; + } + static void rawnet_write_packet(rawnet_t *net) { *************** *** 572,575 **** --- 600,607 ---- break; + case RAWNET_ICMP: + rawnet_write_packet_icmp(net); + break; + default: warn_message("warning: invalid send type -- this is a bug report it please."); *************** *** 587,600 **** uint16_t ip_len, udp_len; int total_len; - eth_addr_t client_hw_addr; - - - /* Used for faking source mac address. */ - - if(net->client_hw_addr) - memcpy(&client_hw_addr, net->client_hw_addr, ETH_ADDR_LEN); - else - eth_get(net->eth, &client_hw_addr); /* FIXME: redundant since we already got it higher up in - * the code. */ dhcp_purge(net->dhcp_p); /* clear up old options. */ --- 619,622 ---- *************** *** 626,633 **** fatal_error("Outgoing DHCP packet too large. I'm currently not implementing this properly so I'll have to exit!"); ! build_eth_broadcast(net, client_hw_addr, ETH_TYPE_IP); ! build_ip_broadcast(net, ip_len); build_udp(net, udp_len); ! build_dhcp(net, xid, secs, client_hw_addr, ciaddr, yiaddr, siaddr, giaddr, options, bootp_type); /* Write packet. */ --- 648,655 ---- fatal_error("Outgoing DHCP packet too large. I'm currently not implementing this properly so I'll have to exit!"); ! build_eth_broadcast(net, net->hw_addr, ETH_TYPE_IP); ! build_ip_broadcast(net, ip_len, IP_PROTO_UDP); build_udp(net, udp_len); ! build_dhcp(net, xid, secs, net->hw_addr, ciaddr, yiaddr, siaddr, giaddr, options, bootp_type); /* Write packet. */ *************** *** 706,709 **** --- 728,784 ---- } + /* Create icmp packet: procify this so we can use it for all other icmp building. */ + static void rawnet_build_icmp_header_proc(rawnet_t *net, uint8_t icmp_type, uint8_t icmp_code) + { + icmp_set_type(net->icmp_p, icmp_type); + icmp_set_code(net->icmp_p, icmp_code); + icmp_clear_cksum(net->icmp_p); + + return; + } + + /* Create icmp mask reply/request packet: procify this to act as + a subroutine for the icmp mask interface routines. */ + static void rawnet_build_icmp_mask_proc(rawnet_t *net, uint8_t icmp_type, uint32_t icmp_id, + uint32_t icmp_seq, uint32_t mask) + { + rawnet_build_icmp_header_proc(net, icmp_type, 0); + icmp_mask_set_seq(net->icmp_p, icmp_id); + icmp_mask_set_seq(net->icmp_p, icmp_seq); + icmp_mask_set_mask(net->icmp_p, mask); + + return; + } + + /* build icmp mask request packet. */ + void rawnet_build_icmp_mask_request(rawnet_t *net, uint32_t id, uint32_t seq) + { + int ip_len = ICMP_HDR_LEN + 12 + IP_HDR_LEN; /* 12 bytes for the mask request. */ + + build_eth_broadcast(net, net->hw_addr, ETH_TYPE_IP); + build_ip_broadcast(net, ip_len, IP_PROTO_ICMP); + rawnet_build_icmp_mask_proc(net, ICMP_MASK, id, seq, 0); + + net->type = RAWNET_ICMP; + net->packet_len = ETH_HDR_LEN + ip_len; + + rawnet_write_packet(net); + } + + /* build icmp mask reply packet. */ + void rawnet_build_icmp_mask_reply(rawnet_t *net, uint32_t id, uint32_t seq, uint32_t subnet_mask) + { + int ip_len = IP_HDR_LEN + ICMP_HDR_LEN + 12; /* 12 bytes for the mask reply. */ + + build_eth_broadcast(net, net->hw_addr, ETH_TYPE_IP); + build_ip_broadcast(net, ip_len, IP_PROTO_ICMP); + rawnet_build_icmp_mask_proc(net, ICMP_MASK, id, seq, subnet_mask); + + net->type = RAWNET_ICMP; + net->packet_len = ETH_HDR_LEN + ip_len; + + rawnet_write_packet(net); + } + /* Just update the seconds field. * *************** *** 729,733 **** memcpy(packet, &secs, 2); ! ip_checksum((net->packet_data + ETH_HDR_LEN), net->packet_len - ETH_HDR_LEN); return; --- 804,808 ---- memcpy(packet, &secs, 2); ! ip_checksum((net->packet_data + ETH_HDR_LEN), (net->packet_len - ETH_HDR_LEN)); return; |