[Dhcp-agent-commits] CVS: dhcp-agent dhcp-agent.h,1.15,1.16 dhcp-align.c,1.2,1.3 dhcp-arp.c,1.5,1.6
Status: Alpha
Brought to you by:
actmodern
From: Thamer Al-H. <act...@us...> - 2002-02-03 16:56:04
|
Update of /cvsroot/dhcp-agent/dhcp-agent In directory usw-pr-cvs1:/tmp/cvs-serv3766 Modified Files: dhcp-agent.h dhcp-align.c dhcp-arp.c dhcp-cache-entry.c dhcp-client-cache.c dhcp-client-control.c dhcp-client-states.c dhcp-client.c dhcp-com.c dhcp-util.c dhcpclient.1 Log Message: fixed small discrepency in secs field after renew; fixed up xid random number generator; added tonnage of comments and much code cleaning; Index: dhcp-agent.h =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/dhcp-agent.h,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** dhcp-agent.h 2002/02/02 15:28:47 1.15 --- dhcp-agent.h 2002/02/03 16:55:59 1.16 *************** *** 504,514 **** extern void block_interrupts(void); extern void add_interrupt_handler(int sigtype, void (*sighandler)(int)); - extern struct timeval timeval_diff(struct timeval begin, struct timeval end); extern void info_message(char *fmt, ...); extern char *splice_string(char *s1, char *s2); extern void trim_string(char *s); extern int string_matches(char *s1, char *s2); - extern uint32_t dhcp_gen_xid(uint32_t seed); extern int is_string(const char *string, int len); /* Replacement vsnprintf, snprintf if needed. */ --- 504,513 ---- extern void block_interrupts(void); extern void add_interrupt_handler(int sigtype, void (*sighandler)(int)); extern void info_message(char *fmt, ...); extern char *splice_string(char *s1, char *s2); extern void trim_string(char *s); extern int string_matches(char *s1, char *s2); extern int is_string(const char *string, int len); + extern int get_random(void); /* Replacement vsnprintf, snprintf if needed. */ *************** *** 614,617 **** --- 613,617 ---- extern dhcp_option *dhcp_build_requested_ip_address(uint32_t address); extern dhcp_option *dhcp_build_server_identifier(uint32_t address); + extern uint32_t dhcp_gen_xid(void); /* Ether obj routines. */ Index: dhcp-align.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/dhcp-align.c,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** dhcp-align.c 2002/02/02 15:28:47 1.2 --- dhcp-align.c 2002/02/03 16:56:00 1.3 *************** *** 45,81 **** /* TOS */ ! memcpy(&iphdr->ip_tos, data, 1); data++; /* TOS is one octet. */ /* len */ ! memcpy(&iphdr->ip_len, data, 2); ! data += 2; /* len is two octets. */ /* ID */ ! memcpy(&iphdr->ip_id, data, 2); ! data += 2; /* ID is two octets. */ /* offset */ ! memcpy(&iphdr->ip_off, data, 2); ! data += 2; /* offset is two octets. */ /* ttl */ ! memcpy(&iphdr->ip_ttl, data, 1); data++; /* ttl is one octet. */ /* protocol */ ! memcpy(&iphdr->ip_p, data, 1); data++; /* protocol is one octet. */ /* checksum */ ! memcpy(&iphdr->ip_sum, data, 2); ! data += 2; /* checksum is one octet. */ /* source address */ ! memcpy(&iphdr->ip_src, data, 4); ! data += 4; /* source addr is four octets. */ /* dest address */ ! memcpy(&iphdr->ip_dst, data, 4); return; --- 45,81 ---- /* TOS */ ! memcpy(&iphdr->ip_tos, data, sizeof(iphdr->ip_tos)); data++; /* TOS is one octet. */ /* len */ ! memcpy(&iphdr->ip_len, data, sizeof(iphdr->ip_len)); ! data += sizeof(iphdr->ip_len); /* len is two octets. */ /* ID */ ! memcpy(&iphdr->ip_id, data, sizeof(iphdr->ip_id)); ! data += sizeof(iphdr->ip_id); /* ID is two octets. */ /* offset */ ! memcpy(&iphdr->ip_off, data, sizeof(iphdr->ip_off)); ! data += sizeof(iphdr->ip_off); /* offset is two octets. */ /* ttl */ ! memcpy(&iphdr->ip_ttl, data, sizeof(iphdr->ip_ttl)); data++; /* ttl is one octet. */ /* protocol */ ! memcpy(&iphdr->ip_p, data, sizeof(iphdr->ip_p)); data++; /* protocol is one octet. */ /* checksum */ ! memcpy(&iphdr->ip_sum, data, sizeof(iphdr->ip_sum)); ! data += sizeof(iphdr->ip_sum); /* checksum is one octet. */ /* source address */ ! memcpy(&iphdr->ip_src, data, IP_ADDR_LEN); ! data += IP_ADDR_LEN; /* source addr is four octets. */ /* dest address */ ! memcpy(&iphdr->ip_dst, data, IP_ADDR_LEN); return; *************** *** 86,99 **** /* all 2 octets long. */ ! memcpy(&udp->uh_sport, data, 2); ! data += 2; ! memcpy(&udp->uh_dport, data, 2); ! data += 2; ! memcpy(&udp->uh_ulen, data, 2); ! data += 2; ! memcpy(&udp->uh_sum, data, 2); return; --- 86,99 ---- /* all 2 octets long. */ ! memcpy(&udp->uh_sport, data, sizeof(udp->uh_sport)); ! data += sizeof(udp->uh_sport); ! memcpy(&udp->uh_dport, data, sizeof(udp->uh_dport)); ! data += sizeof(udp->uh_dport); ! memcpy(&udp->uh_ulen, data, sizeof(udp->uh_ulen)); ! data += sizeof(udp->uh_ulen); ! memcpy(&udp->uh_sum, data, sizeof(udp->uh_sum)); return; *************** *** 103,146 **** dhcphdr *dhcp) { ! memcpy(&dhcp->op, data, 1); data++; ! memcpy(&dhcp->htype, data, 1); data++; ! memcpy(&dhcp->hlen, data, 1); data++; ! memcpy(&dhcp->hops, data, 1); data++; ! memcpy(&dhcp->xid, data, 4); ! data += 4; ! memcpy(&dhcp->secs, data, 2); ! data += 2; ! memcpy(&dhcp->flags, data, 2); ! data += 2; ! memcpy(&dhcp->ciaddr, data, 4); ! data += 4; ! memcpy(&dhcp->yiaddr, data, 4); ! data += 4; ! memcpy(&dhcp->siaddr, data, 4); ! data += 4; ! memcpy(&dhcp->giaddr, data, 4); ! data += 4; ! memcpy(&dhcp->chaddr, data, 16); ! data += 16; ! memcpy(&dhcp->sname, data, 64); ! data += 64; ! memcpy(&dhcp->file, data, 128); return; --- 103,146 ---- dhcphdr *dhcp) { ! memcpy(&dhcp->op, data, sizeof(dhcp->op)); data++; ! memcpy(&dhcp->htype, data, sizeof(dhcp->htype)); data++; ! memcpy(&dhcp->hlen, data, sizeof(dhcp->hlen)); data++; ! memcpy(&dhcp->hops, data, sizeof(dhcp->hops)); data++; ! memcpy(&dhcp->xid, data, sizeof(dhcp->xid)); ! data += sizeof(dhcp->xid); ! memcpy(&dhcp->secs, data, sizeof(dhcp->secs)); ! data += sizeof(dhcp->secs); ! memcpy(&dhcp->flags, data, sizeof(dhcp->flags)); ! data += sizeof(dhcp->flags); ! memcpy(&dhcp->ciaddr, data, IP_ADDR_LEN); ! data += IP_ADDR_LEN; ! memcpy(&dhcp->yiaddr, data, IP_ADDR_LEN); ! data += IP_ADDR_LEN; ! memcpy(&dhcp->siaddr, data, IP_ADDR_LEN); ! data += IP_ADDR_LEN; ! memcpy(&dhcp->giaddr, data, IP_ADDR_LEN); ! data += IP_ADDR_LEN; ! memcpy(&dhcp->chaddr, data, sizeof(dhcp->chaddr)); ! data += sizeof(dhcp->chaddr); ! memcpy(&dhcp->sname, data, sizeof(dhcp->sname)); ! data += sizeof(dhcp->sname); ! memcpy(&dhcp->file, data, sizeof(dhcp->file)); return; Index: dhcp-arp.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/dhcp-arp.c,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** dhcp-arp.c 2002/02/02 12:28:05 1.5 --- dhcp-arp.c 2002/02/03 16:56:00 1.6 *************** *** 25,28 **** --- 25,29 ---- #include <dhcp-agent.h> + /* constructor */ arp_obj *arp_create(void) { *************** *** 34,37 **** --- 35,39 ---- } + /* destructor */ void arp_destroy(arp_obj *arp) { *************** *** 40,43 **** --- 42,46 ---- } + /* set routines. */ void arp_set_hardware_type(arp_obj *arp, uint16_t hdr_addr) { *************** *** 93,96 **** --- 96,100 ---- } + /* dump image to packet. */ void arp_write_packet_image(arp_obj *arp, unsigned char *packet) { Index: dhcp-cache-entry.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/dhcp-cache-entry.c,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** dhcp-cache-entry.c 2002/01/30 09:26:02 1.2 --- dhcp-cache-entry.c 2002/02/03 16:56:00 1.3 *************** *** 21,28 **** --- 21,33 ---- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. * + * Cache entry object: + * + * We use this object to hold a name/value duplet. + * */ #include <dhcp-agent.h> + /* constructors. */ cache_entry_t *make_cache_entry(char *name, char *value) { *************** *** 45,48 **** --- 50,54 ---- } + /* use alongside purge_list or similar routines. */ static void destroy_cache_entry_in_list(void *data) { *************** *** 50,53 **** --- 56,60 ---- } + /* purge list of cache entries. */ void cache_entry_purge_list(list_t *cache_list) { Index: dhcp-client-cache.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/dhcp-client-cache.c,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** dhcp-client-cache.c 2002/02/02 15:28:47 1.4 --- dhcp-client-cache.c 2002/02/03 16:56:00 1.5 *************** *** 1,3 **** --- 1,4 ---- /* $Header$ + * * Copyright 2001 Thamer Alharbash <tm...@wh...> * *************** *** 16,23 **** --- 17,30 ---- * written permission. * + * Client cache object: + * + * We use this to load cached DHCP options, save them, and conversion with + * dhcp-convert are done here to network or serialized state. + * */ #include <dhcp-agent.h> + /* constructor. */ client_cache_t *create_client_cache(dhcp_client_control_t *dc) { *************** *** 31,34 **** --- 38,42 ---- } + /* purges list in cache object. */ void purge_cache(client_cache_t *cc) { *************** *** 51,54 **** --- 59,63 ---- } + /* destructor. */ void client_cache_destroy(client_cache_t *cc) { *************** *** 60,63 **** --- 69,73 ---- } + /* read next cache entry from file. */ static cache_entry_t *read_next_cache_var(FILE *fp) { *************** *** 70,73 **** --- 80,87 ---- fatal_error("corrupt cache! try removing it and restarting it."); + /* as long as file_parse_string didn't return an error we're + * guaranteed proper strings with file_get_var_name(), + * file_get_var_val() */ + cache_entry = make_cache_entry(file_get_var_name(), file_get_var_val()); return cache_entry; *************** *** 75,78 **** --- 89,93 ---- } + /* Load up cache from filename. */ static int load_client_cache_proc(client_cache_t *cc, char *filename) { *************** *** 94,97 **** --- 109,113 ---- } + /* filename creation routines. */ static char *get_fname(client_cache_t *cc) { *************** *** 104,107 **** --- 120,124 ---- } + /* load cache (interface) */ int load_client_cache(client_cache_t *cc, unsigned char use_tmp) { *************** *** 122,125 **** --- 139,150 ---- } + /* check to see if cache is empty: + * as long as the cache file exists + * we're always guaranteed a cache + * except on filesystem corruption. + * in that case we can't do much except + * complain. using file_exists() is + * as good as it gets. + */ int client_cache_is_empty(client_cache_t *cc) { *************** *** 135,138 **** --- 160,164 ---- } + /* work house routine to convert from serialized state to network datum. */ static void *convert_to_network(unsigned char tag, char *string) { *************** *** 156,162 **** return opt; } - - /* Convert options from cache struct straight to dhcp options. */ static list_t *get_cache_options_list_proc(client_cache_t *cc, void* (*convert_func)(unsigned char tag, char *string)) --- 182,191 ---- return opt; } + /* + * Convert options from cache serialized form to whatever form convert_func, returns. + * If convert_func is NULL then just return in serialized form (sysconf needs it that way). + * + */ static list_t *get_cache_options_list_proc(client_cache_t *cc, void* (*convert_func)(unsigned char tag, char *string)) *************** *** 195,198 **** --- 224,230 ---- } + /* dump options into file. here we take data in network form and dump them in serialized form. + * we always do this to the temp file. + */ int client_cache_dump_options(client_cache_t *cc, list_t *options) *************** *** 243,246 **** --- 275,279 ---- } + /* work house load options routine. */ static list_t *client_cache_load_options_list_proc(client_cache_t *cc, unsigned char use_tmp, void* (*convert_func)(unsigned char tag, char *string)) *************** *** 258,266 **** } list_t *client_cache_load_option_network_list(client_cache_t *cc, unsigned char use_tmp) { return(client_cache_load_options_list_proc(cc, use_tmp, convert_to_network)); } ! list_t *client_cache_load_option_string_list(client_cache_t *cc, unsigned char use_tmp) { --- 291,300 ---- } + /* load options and return network list. */ list_t *client_cache_load_option_network_list(client_cache_t *cc, unsigned char use_tmp) { return(client_cache_load_options_list_proc(cc, use_tmp, convert_to_network)); } ! /* load options and return serialized list. */ list_t *client_cache_load_option_string_list(client_cache_t *cc, unsigned char use_tmp) { *************** *** 268,271 **** --- 302,306 ---- } + /* update cache: move temp cache over real cache. */ void client_cache_update(client_cache_t *cc) { *************** *** 283,286 **** --- 318,322 ---- } + /* delete old temporary cache. */ void client_cache_delete_tmp_cache(client_cache_t *cc) { *************** *** 293,297 **** } ! void client_cache_delete_cache(client_cache_t *cc) { --- 329,333 ---- } ! /* delete cache. */ void client_cache_delete_cache(client_cache_t *cc) { Index: dhcp-client-control.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/dhcp-client-control.c,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** dhcp-client-control.c 2002/02/02 15:28:47 1.7 --- dhcp-client-control.c 2002/02/03 16:56:00 1.8 *************** *** 24,33 **** #include <dhcp-agent.h> void dhcp_client_update_secs(dhcp_client_control_t *dc) { ! dc->secs = (dc->started - time(NULL)); return; } void dhcp_client_reset_secs(dhcp_client_control_t *dc) { --- 24,35 ---- #include <dhcp-agent.h> + /* Update seconds field for dhcp discover packets. */ void dhcp_client_update_secs(dhcp_client_control_t *dc) { ! dc->secs = (time(NULL) - dc->started); return; } + /* reset started field for when we revert back to discover again. */ void dhcp_client_reset_secs(dhcp_client_control_t *dc) { *************** *** 36,39 **** --- 38,42 ---- } + /* create client id from a string in the format of 0x:0x:0x:0x:0x:0x */ static unsigned char *create_fake_client_id(unsigned char *mac_string) { *************** *** 54,57 **** --- 57,61 ---- } + /* create a proper client id from the interface address passed. */ static unsigned char *create_client_id(eth_addr_t interface_address) { *************** *** 67,70 **** --- 71,75 ---- } + /* dummy routine. we use this to manipulate conf/cache. */ dhcp_client_control_t *create_dhcp_client_control_dummy(char *interface) { *************** *** 78,81 **** --- 83,93 ---- } + static void update_dhcp_xid(dhcp_client_control_t *dc) + { + dc->xid = dhcp_gen_xid(); /* this is redone at discover_request too */ + return; + } + + /* create client control object */ dhcp_client_control_t *create_dhcp_client_control(char *interface) { *************** *** 143,152 **** /* xid setting */ ! /* FIXME: rfc compliant for now but robustness dictates ! * that we should update this per transaction ! * basis. */ ! ! dc->xid = dhcp_gen_xid((time(NULL) + getpid())); dhcp_client_reset_secs(dc); dhcp_client_update_secs(dc); --- 155,161 ---- /* xid setting */ + update_dhcp_xid(dc); ! /* secs settings. */ dhcp_client_reset_secs(dc); dhcp_client_update_secs(dc); *************** *** 169,175 **** return dc; } - - /* fake_hw_addr is handed to us in a C string form. */ void dhcp_control_use_fake_hw_addr(dhcp_client_control_t *dc, char *fake_hw_addr) { --- 178,184 ---- return dc; } + /* use fake address in 0x:0x:0x:0x:0x we need to notify rawnet about this. + * achtung: arp packets still have proper address so we don't fudge arp caches up. */ void dhcp_control_use_fake_hw_addr(dhcp_client_control_t *dc, char *fake_hw_addr) { *************** *** 183,186 **** --- 192,196 ---- } + /* destructor. */ void destroy_dhcp_client_control(dhcp_client_control_t *dc) { Index: dhcp-client-states.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/dhcp-client-states.c,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** dhcp-client-states.c 2002/02/02 15:28:47 1.11 --- dhcp-client-states.c 2002/02/03 16:56:00 1.12 *************** *** 39,43 **** } ! /* Along with the parameter request list we always build these options. */ static list_t *client_build_option_list(unsigned char *opts, unsigned char message_type, --- 39,44 ---- } ! /* Along with the parameter request list we always build these options ! * upon creating new dhcp packets. */ static list_t *client_build_option_list(unsigned char *opts, unsigned char message_type, *************** *** 75,78 **** --- 76,83 ---- } + /* these checking routines are independant of the checks done + * in client_transact. they're a more specific type of check.*/ + + /* filter to check for valid discover responses. */ static int client_discover_check(dhcp_client_control_t *dc) { *************** *** 84,87 **** --- 89,93 ---- } + /* filter to check for valid request responses. */ static int client_request_check(dhcp_client_control_t *dc) { *************** *** 127,131 **** /* UNARP (rfc 1868). This makes us more network friendly. */ - static void client_broadcast_unarp(dhcp_client_control_t *dc) { --- 133,136 ---- *************** *** 137,141 **** /* Send and wait for reply. */ - static int client_transact(dhcp_client_control_t *dc, unsigned update_secs, int (*check)(dhcp_client_control_t *dc)) --- 142,145 ---- *************** *** 196,201 **** /* run tests. */ - /* is it dhcp?. */ if(dc->rawnet->type != RAWNET_DHCP) break; --- 200,205 ---- /* run tests. */ + /* is it dhcp?. */ if(dc->rawnet->type != RAWNET_DHCP) break; *************** *** 213,216 **** --- 217,221 ---- break; + /* great: valid packet. destroy rtt object and return. */ rtt_destroy(rtt); return 0; *************** *** 223,226 **** --- 228,232 ---- } + /* do discover/offer transaction. */ int client_discover_offer(dhcp_client_control_t *dc) { *************** *** 229,234 **** options = client_build_option_list(dc->conf->options, DHCP_DISCOVER_TM, dc->client_id, dc->class_id); ! dc->secs = (time(NULL) - dc->started); /* begin keeping track of DHCP secs. */ /* Our options cleared by rawnet_build_* since they are passed * down and later purged. */ --- 235,246 ---- options = client_build_option_list(dc->conf->options, DHCP_DISCOVER_TM, dc->client_id, dc->class_id); ! ! /* when we begin transmitting discover_offer we setup our secs field. ! * this is because relay agents and dhcp servers use this to see how ! * bad off we are. */ + dhcp_client_reset_secs(dc); + dhcp_client_update_secs(dc); + /* Our options cleared by rawnet_build_* since they are passed * down and later purged. */ *************** *** 255,259 **** /* dump cache file. */ - if(client_cache_dump_options(dc->cache, dc->rawnet->dhcp_p->options)) { error_message("could not create temporary cache"); --- 267,270 ---- *************** *** 266,272 **** } ! /* We need to rebuild an option list of all the options we ! * got, and then send out a request see what we get back. */ ! int client_request_ack(dhcp_client_control_t *dc) { --- 277,281 ---- } ! /* do client_request_ack transaction. */ int client_request_ack(dhcp_client_control_t *dc) { *************** *** 295,298 **** --- 304,310 ---- } + /* if we got a pack we're good to go, if we haven't + * then we need to revert to discover/offer state. */ + if(dhcp_is_type(dc->rawnet->dhcp_p, DHCP_DHCPACK_TM)) return STATE_SETUP; *************** *** 302,305 **** --- 314,318 ---- } + /* do setup (call do_sysconf) */ int client_setup(dhcp_client_control_t *dc) { *************** *** 308,312 **** options = client_cache_load_option_string_list(dc->cache, 0); if(options == NULL) { ! error_message("have empty configuration cache! cannot configure host"); return -1; } --- 321,325 ---- options = client_cache_load_option_string_list(dc->cache, 0); if(options == NULL) { ! error_message("received empty configuration cache! cannot configure host"); return -1; } *************** *** 323,326 **** --- 336,340 ---- } + /* wait for renewal time. */ int client_wait(dhcp_client_control_t *dc) { *************** *** 356,359 **** --- 370,374 ---- } + /* release lease. */ int client_release(dhcp_client_control_t *dc) { Index: dhcp-client.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/dhcp-client.c,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** dhcp-client.c 2002/02/02 15:34:03 1.10 --- dhcp-client.c 2002/02/03 16:56:00 1.11 *************** *** 25,32 **** #include <dhcp-agent.h> ! int interactive = 1; ! char *binname = NULL; ! char *work_dir = "/etc/dhcpclient"; static void do_shutdown(dhcp_client_control_t *dc) { --- 25,33 ---- #include <dhcp-agent.h> ! int interactive = 1; /* by default we begin interactive (messages on stdout/stderr). */ ! char *binname = NULL; /* we need a name. */ ! char *work_dir = CLIENT_WORK_DIR; /* our default working directory */ + /* do a graceful shutdown. */ static void do_shutdown(dhcp_client_control_t *dc) { *************** *** 42,50 **** /* * Our initialize client state function. ! * We can be in two states. * The discover/offer state, or we can * be in the request state where we * already have a configuration. * */ --- 43,55 ---- /* * Our initialize client state function. ! * We can be in two states. ! * * The discover/offer state, or we can * be in the request state where we * already have a configuration. * + * We always ask for configuration info on + * startup so we can't begin in any other + * state except these two. */ *************** *** 56,60 **** return STATE_REQUEST_ACK; } ! static void usage(char *s) { --- 61,66 ---- return STATE_REQUEST_ACK; } ! ! /* print usage info. */ static void usage(char *s) { *************** *** 63,66 **** --- 69,73 ---- } + /* main loop: if only_setup is called we exit after setup. */ static int do_client_dhcp_loop(int only_setup, dhcp_client_control_t *dc, int state) { *************** *** 121,124 **** --- 128,132 ---- } + /* kill a running client. */ static void do_kill_client(char *interface) { *************** *** 139,142 **** --- 147,151 ---- } + /* wake up a client. send sigalarm. */ static void do_wake_client(char *interface) { *************** *** 157,162 **** } ! /* A bit of a hack here since we need only parts of a control structure. */ ! static void do_clear_cache(char *interface) { --- 166,170 ---- } ! /* delete existing cache. make sure no other client is running. */ static void do_clear_cache(char *interface) { *************** *** 164,168 **** dhcp_client_control_t *dc; client_cache_t *cc; ! if(!get_pid_file(interface, &pid)) { /* A race condition exists between the discover and request state. --- 172,176 ---- dhcp_client_control_t *dc; client_cache_t *cc; ! if(!get_pid_file(interface, &pid)) { /* A race condition exists between the discover and request state. *************** *** 192,195 **** --- 200,204 ---- } + /* check for existing client. */ static int client_process_exists(char *interface) { Index: dhcp-com.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/dhcp-com.c,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** dhcp-com.c 2002/01/30 16:06:46 1.2 --- dhcp-com.c 2002/02/03 16:56:00 1.3 *************** *** 90,93 **** --- 90,94 ---- } + /* create option. */ dhcp_option *create_dhcp_option(const unsigned char *opt_data, unsigned char len, unsigned char tag) { *************** *** 104,107 **** --- 105,109 ---- } + /* destroy dhcp option. */ void destroy_dhcp_option(dhcp_option *option) { *************** *** 111,117 **** return; } - - /* Copy DHCP option safely. */ static dhcp_option *process_next_dhcp_option(const unsigned char *opt_data, int len, unsigned char *optlen) --- 113,118 ---- return; } + /* copy DHCP option safely. */ static dhcp_option *process_next_dhcp_option(const unsigned char *opt_data, int len, unsigned char *optlen) *************** *** 131,140 **** } ! /* ! * After creating a new object this function ! * will read in a packet image. ! * ! */ ! int dhcp_read_packet_image(dhcp_obj *dhcp, const unsigned char *dhcp_packet, int len) { --- 132,136 ---- } ! /* read packet image. */ int dhcp_read_packet_image(dhcp_obj *dhcp, const unsigned char *dhcp_packet, int len) { *************** *** 333,338 **** /* tests. */ ! /* Return 1 on valid magic cookie, and 0 on invalid cookie. */ ! int dhcp_valid_magic_cookie(dhcp_obj *dhcp) { --- 329,333 ---- /* tests. */ ! /* test for valid cookie. */ int dhcp_valid_magic_cookie(dhcp_obj *dhcp) { *************** *** 345,349 **** } ! static int dhcp_is_type_proc(dhcp_obj *dhcp, unsigned char type) { dhcp_option *opt; --- 340,345 ---- } ! /* test for type. */ ! int dhcp_is_type(dhcp_obj *dhcp, unsigned char type) { dhcp_option *opt; *************** *** 366,375 **** return 0; } - - int dhcp_is_type(dhcp_obj *dhcp, unsigned char type) - { - return(dhcp_is_type_proc(dhcp, type)); - } int dhcp_option_is_valid(unsigned char tag, unsigned char tag_len) { --- 362,367 ---- return 0; } + /* test for option validity. */ int dhcp_option_is_valid(unsigned char tag, unsigned char tag_len) { *************** *** 380,383 **** --- 372,376 ---- } + /* test for existance of option. */ int dhcp_have_option(dhcp_obj *dhcp, unsigned char tag) { *************** *** 402,431 **** int dhcp_have_atleast_requested_options(dhcp_obj *dhcp, unsigned char *options) { - dhcp_option *option; - int len = 0; - int matches = 0; int i; for(i = 0;i < MAX_OPTIONS_HANDLED;i++) { ! if(options[i]) ! len++; ! } ! ! dhcp_reset_option_seek(dhcp); ! ! while((option = dhcp_get_next_option(dhcp)) != NULL) { ! ! if(option->tag < MAX_OPTIONS_HANDLED && ! options[option->tag]) ! matches++; } - ! /* At least or greater than. */ ! if(len >= matches) ! return 1; ! else ! return 0; ! } --- 395,406 ---- int dhcp_have_atleast_requested_options(dhcp_obj *dhcp, unsigned char *options) { int i; for(i = 0;i < MAX_OPTIONS_HANDLED;i++) { ! if(options[i] && !dhcp_have_option(dhcp, i)) ! return 1; } ! return 0; } *************** *** 457,462 **** } ! /* accept an array of 1/0s telling us which options to build the request option. */ ! dhcp_option *dhcp_build_parameter_request_list_option(unsigned char *requested_options) { --- 432,436 ---- } ! /* accept bit array telling us which options to build the request option. */ dhcp_option *dhcp_build_parameter_request_list_option(unsigned char *requested_options) { *************** *** 466,470 **** /* Grab length so we can just malloc all at once. */ - for(i = 0;i < MAX_OPTIONS_HANDLED;i++) { if(requested_options[i]) --- 440,443 ---- *************** *** 650,653 **** --- 623,627 ---- /* dhcp set options, add options. */ + /* replace options with list passed (convenience). */ void dhcp_set_options(dhcp_obj *dhcp, list_t *options) { *************** *** 657,660 **** --- 631,635 ---- } + /* write options to packet pointer. */ void dhcp_write_options(dhcp_obj *dhcp, unsigned char *packet) { *************** *** 695,701 **** return; } - - /* Write image out to packet. */ void dhcp_write_packet_image(dhcp_obj *dhcp, unsigned char *packet) { --- 670,675 ---- return; } + /* write dhcp image out to packet. */ void dhcp_write_packet_image(dhcp_obj *dhcp, unsigned char *packet) { *************** *** 703,737 **** memcpy(packet, &dhcp->fixedheader.op, 1); ! packet += 1; memcpy(packet, &dhcp->fixedheader.htype, 1); ! packet += 1; memcpy(packet, &dhcp->fixedheader.hlen, 1); ! packet += 1; memcpy(packet, &dhcp->fixedheader.hops, 1); ! packet += 1; memcpy(packet, &dhcp->fixedheader.xid, 4); ! packet += 4; memcpy(packet, &dhcp->fixedheader.secs, 2); ! packet += 2; memcpy(packet, &dhcp->fixedheader.flags, 2); ! packet += 2; ! memcpy(packet, &dhcp->fixedheader.ciaddr, 4); ! packet += 4; ! memcpy(packet, &dhcp->fixedheader.yiaddr, 4); ! packet += 4; ! memcpy(packet, &dhcp->fixedheader.siaddr, 4); ! packet += 4; ! memcpy(packet, &dhcp->fixedheader.giaddr, 4); ! packet += 4; memcpy(packet, &dhcp->fixedheader.chaddr, DHCP_CHADDR_SIZE); --- 677,711 ---- memcpy(packet, &dhcp->fixedheader.op, 1); ! packet += sizeof(dhcp->fixedheader.op); memcpy(packet, &dhcp->fixedheader.htype, 1); ! packet += sizeof(dhcp->fixedheader.htype); memcpy(packet, &dhcp->fixedheader.hlen, 1); ! packet += sizeof(dhcp->fixedheader.hlen); memcpy(packet, &dhcp->fixedheader.hops, 1); ! packet += sizeof(dhcp->fixedheader.hops); memcpy(packet, &dhcp->fixedheader.xid, 4); ! packet += sizeof(dhcp->fixedheader.xid); memcpy(packet, &dhcp->fixedheader.secs, 2); ! packet += sizeof(dhcp->fixedheader.secs); memcpy(packet, &dhcp->fixedheader.flags, 2); ! packet += sizeof(dhcp->fixedheader.flags); ! memcpy(packet, &dhcp->fixedheader.ciaddr, IP_ADDR_LEN); ! packet += IP_ADDR_LEN; ! memcpy(packet, &dhcp->fixedheader.yiaddr, IP_ADDR_LEN); ! packet += IP_ADDR_LEN; ! memcpy(packet, &dhcp->fixedheader.siaddr, IP_ADDR_LEN); ! packet += IP_ADDR_LEN; ! memcpy(packet, &dhcp->fixedheader.giaddr, IP_ADDR_LEN); ! packet += IP_ADDR_LEN; memcpy(packet, &dhcp->fixedheader.chaddr, DHCP_CHADDR_SIZE); *************** *** 746,751 **** /* Now dump cookie. */ ! memcpy(packet, &dhcp->magic_cookie, 4); ! packet += 4; /* Now write options. */ --- 720,725 ---- /* Now dump cookie. */ ! memcpy(packet, &dhcp->magic_cookie, sizeof(dhcp->magic_cookie)); ! packet += sizeof(dhcp->magic_cookie); /* Now write options. */ *************** *** 756,763 **** } ! uint32_t dhcp_gen_xid(uint32_t seed) { ! /* fixme: no need to keep setting the seed. */ ! srand(seed); ! return rand(); } --- 730,736 ---- } ! /* generate xid. */ ! uint32_t dhcp_gen_xid(void) { ! return(get_random()); } Index: dhcp-util.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/dhcp-util.c,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** dhcp-util.c 2002/01/30 09:26:03 1.2 --- dhcp-util.c 2002/02/03 16:56:00 1.3 *************** *** 29,32 **** --- 29,33 ---- static char msgbuff[MSG_BUFFER_SIZE]; + /* send error message. */ void error_message(char *fmt, ...) { *************** *** 45,49 **** va_end(ap); } ! void fatal_error(char *fmt, ...) { --- 46,51 ---- va_end(ap); } ! ! /* send error message and exit. (convenience) */ void fatal_error(char *fmt, ...) { *************** *** 64,67 **** --- 66,70 ---- } + /* send info message (warnings included) */ void info_message(char *fmt, ...) { *************** *** 137,159 **** #endif - - /* Calculate the difference between two timeval structs. - * An assumption is made that end is greater than begin */ - - struct timeval timeval_diff(struct timeval begin, struct timeval end) - { - struct timeval difference = { 0, 0 }; - - if(begin.tv_usec > end.tv_usec) { - end.tv_sec--; - end.tv_usec += 1000000; - } - - difference.tv_sec = end.tv_sec - begin.tv_sec; - difference.tv_usec = end.tv_usec - begin.tv_usec; ! return difference; ! } void add_interrupt_handler(int sigtype, void (*sighandler)(int)) { --- 140,147 ---- #endif ! /* signal manipulation routines. */ + /* add signal handler. */ void add_interrupt_handler(int sigtype, void (*sighandler)(int)) { *************** *** 172,175 **** --- 160,164 ---- } + /* block all signals. */ void block_interrupts(void) { *************** *** 182,188 **** return; } - - /* Check for signals */ int check_for_interrupts(void) { --- 171,176 ---- return; } + /* unblock signals temporarily so we can receive pending signals. */ int check_for_interrupts(void) { *************** *** 201,204 **** --- 189,193 ---- } + /* suspend indefinetly for a signal (useful for alarm). */ void suspend_for_interrupts(void) { *************** *** 211,214 **** --- 200,204 ---- /* String routines. */ + /* * Splice two strings together. *************** *** 234,243 **** } ! /* ! * Trim any trailing or prepended white spaces. ! * Make a copy, then copy out then copy back in ! * with a trailing null. ! */ ! void trim_string(char *s) { --- 224,228 ---- } ! /* trim preprending and trailing space. also get rid of newlines. */ void trim_string(char *s) { *************** *** 277,280 **** --- 262,266 ---- } + /* is a valid C string? */ int is_string(const char *string, int len) { *************** *** 293,298 **** } ! /* More string ugliness. This needs to be made cleaner. */ ! int hex_string_to_value(char *string, unsigned char *dst) { --- 279,283 ---- } ! /* parse hex string (0x:0x...0x) to value. (not fun to write) */ int hex_string_to_value(char *string, unsigned char *dst) { *************** *** 333,336 **** --- 318,322 ---- } + /* check if two strings match using strlen and strcmp */ int string_matches(char *s1, char *s2) { *************** *** 340,342 **** --- 326,346 ---- else return 0; + } + + /* initialize random seed. */ + static void init_rand(void) + { + srandom(getpid() + time(NULL)); + } + + int get_random(void) + { + static unsigned char have_init_random = 0; + + if(!have_init_random) { + init_rand(); + have_init_random = 1; + } + + return(random()); } Index: dhcpclient.1 =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/dhcpclient.1,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** dhcpclient.1 2002/02/02 15:34:04 1.5 --- dhcpclient.1 2002/02/03 16:56:00 1.6 *************** *** 16,23 **** foreground until it initializes its interface, and then go into the background and renew its lease when it needs to. You may safely run ! more than one dhcpclient per interface since it stores all its data ! in control files named after the interface it is configuring. ! Currently only the basic networking DHCP options are handled. The ! interfaces IP address, routing, and domain name system are setup. .Sh OPTIONS .Bl -tag --- 16,24 ---- foreground until it initializes its interface, and then go into the background and renew its lease when it needs to. You may safely run ! more than one dhcpclient per host since it stores all its data in ! control files named after the interface it is configuring. It is not ! safe to run more than one dhcpclient per interface. Currently only ! the basic networking DHCP options are handled. The interfaces IP ! address, routing, and domain name system are setup. .Sh OPTIONS .Bl -tag |