Thread: [dhcp-agent-commits] dhcp-agent/src dhcp-server-control.c,1.6,1.7 dhcp-server-states.c,1.4,1.5 dhcp-
Status: Alpha
Brought to you by:
actmodern
From: <act...@us...> - 2003-08-05 05:01:05
|
Update of /cvsroot/dhcp-agent/dhcp-agent/src In directory sc8-pr-cvs1:/tmp/cvs-serv26777/src Modified Files: dhcp-server-control.c dhcp-server-states.c dhcp-server.c Log Message: more work on server. wrote initial offer code and hooked it into the lease manager Index: dhcp-server-control.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-server-control.c,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** dhcp-server-control.c 26 Jul 2003 23:49:11 -0000 1.6 --- dhcp-server-control.c 5 Aug 2003 05:01:02 -0000 1.7 *************** *** 43,46 **** --- 43,47 ---- dhcp_server_control->server_conf = server_conf; + sport = rawnet_port_for_service("bootpc", "udp"); dport = rawnet_port_for_service("bootps", "udp"); Index: dhcp-server-states.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-server-states.c,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** dhcp-server-states.c 20 Jul 2003 05:52:31 -0000 1.4 --- dhcp-server-states.c 5 Aug 2003 05:01:02 -0000 1.5 *************** *** 32,40 **** #include "dhcp-libutil.h" #include "dhcp-librawnet.h" #include "dhcp-server-conf.h" #include "dhcp-server.h" - /******************************* * utilities * --- 32,42 ---- #include "dhcp-libutil.h" #include "dhcp-librawnet.h" + #include "dhcp-option.h" + #include "dhcp-lease.h" + #include "dhcp-server-lease-manager.h" #include "dhcp-server-conf.h" #include "dhcp-server.h" /******************************* * utilities * *************** *** 63,66 **** --- 65,258 ---- } + /* given a pointer to a max message size, which may be NULL, do + * the logic to find the right max message size. */ + uint16_t get_max_message_size(dhcp_server_control_t *sc, uint16_t *max_message_size_ptr) + { + if(max_message_size_ptr == NULL) /* none passed. */ + return rawnet_get_mtu(sc->rawnet); /* use the mtu then. */ + + else if(*max_message_size_ptr > rawnet_get_mtu(sc->rawnet)) /* use mtu if the max message size is greater. */ + return rawnet_get_mtu(sc->rawnet); + + /* otherwise use the max message size. */ + else + return *max_message_size_ptr; + } + + /* utility routine to get a list of options from a lease. */ + static list_t *create_options_from_lease(dhcp_server_control_t *sc, lease_t *lease) + { + list_t *options; + dhcp_opt_t *option; + uint32_t period; + ip_addr_t subnet; + + options = dhcp_option_copy_list(lease_get_options(lease)); /* copy out the user defined options. */ + + /* append the renew, rebind, and expiry options. */ + + period = lease_get_rebind(lease); + option = dhcp_opt_create_from_internal_data(TAG_DHCP_REBINDING_TIME, &period, sizeof(uint32_t)); + list_add(options, option); + + period = lease_get_renew(lease); + option = dhcp_opt_create_from_internal_data(TAG_DHCP_RENEWAL_TIME, &period, sizeof(uint32_t)); + list_add(options, option); + + period = lease_get_expiry(lease); + option = dhcp_opt_create_from_internal_data(TAG_DHCP_IP_ADDRESS_LEASE_TIME, &period, sizeof(uint32_t)); + list_add(options, option); + + /* tack on the subnet option. */ + subnet = lease_get_subnet_mask(lease); + option = dhcp_opt_create_from_internal_data(TAG_DHCP_SUBNET_MASK, &subnet, IP_ADDR_LEN); + list_add(options, option); + + return options; + } + + /************************************** + * DHCP server message constructors. * + **************************************/ + + /* construct and send an offer. */ + static int server_do_offer(dhcp_server_control_t *sc, lease_t *lease, uint16_t *max_message_size_ptr, + eth_addr_t eth_addr, uint32_t xid) + { + uint16_t max_message_size = get_max_message_size(sc, max_message_size_ptr); + list_t *options; + dhcp_opt_t *option; + ip_addr_t yiaddr, to_addr; + dhcp_obj *dhcp = sc->rawnet->dhcp_p; + + /* get the options. */ + options = create_options_from_lease(sc, lease); + + /* get assigned address. */ + yiaddr = lease_get_address(lease); + + option = dhcp_opt_create_message_type(DHCP_OFFER_TM); + list_add(options, option); + + /* now build offer. */ + + /* if it was a relay server, we should unicast back to it so + * build a unicast packet and unicast. */ + + /* remove me. and do something with me. */ + max_message_size = 0; + + if(dhcp_get_giaddr(dhcp) != 0) { + + to_addr = ip_get_src_addr(sc->rawnet->ip_p); + + /* eth_addr is ignored because we'll be doing the unicast + * at the socket level and let the OS worry about it. */ + + build_dhcp_offer_unicast(sc->rawnet, dhcp_get_xid(dhcp), yiaddr, rawnet_get_ip_addr(sc->rawnet), + to_addr, eth_get_src_addr(sc->rawnet->ether_p), options); + + if(rawnet_send_packet_udp_socket_unicast(sc->udp_sock, sc->rawnet, to_addr)) { + FATAL_MESSAGE("could not send offer response"); + } + + } else { + + /* otherwise broadcast out. */ + + build_dhcp_offer_broadcast(sc->rawnet, dhcp_get_xid(dhcp), yiaddr, rawnet_get_ip_addr(sc->rawnet), + options); + + if(rawnet_send_packet_udp_socket_broadcast(sc->udp_sock, sc->rawnet)) { + FATAL_MESSAGE("could not send offer response"); + } + } + + return 0; + } + + /************************************** + * DHCP server message handlers. * + **************************************/ + + int server_handle_discover(dhcp_server_control_t *sc) + { + char *hostname = NULL; + eth_addr_t mac_addr; + uint8_t *chaddr; + ip_addr_t giaddr; + dhcp_opt_t *client_id = NULL; + dhcp_opt_t *opt; + lease_t *lease; + uint16_t *max_message_size = NULL; + dhcp_obj *dhcp = sc->rawnet->dhcp_p; + + /* a client wants to discover possible leases. get the + * information we need to pass to the lease manager and ask + * the manager if any leases are available for it. */ + + giaddr = dhcp_get_giaddr(dhcp); + + chaddr = dhcp_get_chaddr(dhcp); + memcpy(&mac_addr, chaddr, ETH_ADDR_LEN); + + dhcp_reset_option_seek(dhcp); + while((opt = dhcp_get_next_option(dhcp)) != NULL) { + + switch(dhcp_opt_get_tag(opt)) { + + case TAG_DHCP_CLIENT_ID: + client_id = opt; + break; + + case TAG_DHCP_MAX_DHCP_SIZE: + max_message_size = dhcp_opt_get_host_data(opt); + break; + + case TAG_DHCP_HOST_NAME: + hostname = dhcp_opt_get_host_data(opt); + break; + + default: + break; + } + } + + lease = lease_manager_lease_available(hostname, mac_addr, + giaddr, client_id); + + if(lease == NULL) + return STATE_LISTEN; /* we have no possible leases. */ + + /* otherwise we do have a lease, send out an OFFER. */ + + if(server_do_offer(sc, lease, max_message_size, mac_addr, dhcp_get_xid(dhcp))) + return STATE_SHUTDOWN; + + return STATE_LISTEN; + } + + int server_handle_request(dhcp_server_control_t *sc) + { + /* a client is requesting a lease, find out from the lease + * manager if we can give it the lease. */ + + return STATE_LISTEN; + } + + int server_handle_decline(dhcp_server_control_t *sc) + { + /* a client is declining a lease. let the lease manager know. */ + + return STATE_LISTEN; + } + + int server_handle_release(dhcp_server_control_t *sc) + { + /* a client is releasing a lease. let the lease manager know. */ + + return STATE_LISTEN; + } + /******************************* * event processing routines. * *************** *** 193,197 **** int server_handle_packet(dhcp_server_control_t *sc) { ! /* TODO. */ return STATE_LISTEN; } --- 385,434 ---- int server_handle_packet(dhcp_server_control_t *sc) { ! int retval; ! int message_type; ! ! retval = rawnet_get_packet_udp_socket(sc->udp_sock, sc->rawnet); ! ! switch(retval) { ! ! case RAWNET_MALFORMED_PACKET: ! return STATE_LISTEN; /* if packet malformed, just ignore it. */ ! ! case RAWNET_OK: ! ! /* check for a valid magic cookie, if not just drop it. */ ! if(!dhcp_valid_magic_cookie(sc->rawnet->dhcp_p)) ! break; ! ! /* based on the packet type, call the handler function. */ ! message_type = dhcp_get_type(sc->rawnet->dhcp_p); ! ! switch(message_type) { ! ! case DHCP_DISCOVER_TM: ! return server_handle_discover(sc); ! ! case DHCP_REQUEST_TM: ! return server_handle_request(sc); ! ! case DHCP_DECLINE_TM: ! return server_handle_decline(sc); ! ! case DHCP_RELEASE_TM: ! return server_handle_release(sc); ! ! default: ! /* anything else is silently ignored. */ ! break; ! } ! ! return STATE_LISTEN; ! ! default: /* anything else is an error. */ ! ERROR_MESSAGE("received error while reading UDP packet."); ! return STATE_SHUTDOWN; ! } ! ! /* we can ask rawnet to read from a socket. */ return STATE_LISTEN; } Index: dhcp-server.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-server.c,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** dhcp-server.c 26 Jul 2003 23:48:01 -0000 1.10 --- dhcp-server.c 5 Aug 2003 05:01:02 -0000 1.11 *************** *** 32,35 **** --- 32,36 ---- #include "dhcp-server-conf.h" #include "dhcp-server.h" + #include "dhcp-guile-util.h" #include "dhcp-server-guile.h" |