📄 dhcp-com.c
字号:
{ dhcp->fixedheader.flags = htons(DHCP_BROADCAST_FLAG); return;}/* dhcp flags: do conversion */void dhcp_unset_flag_broadcast(dhcp_obj * dhcp){ dhcp->fixedheader.flags = 0; return;}/* dhcp ciaddr: no conversion necessary */void dhcp_set_ciaddr(dhcp_obj * dhcp, uint32_t ciaddr){ memcpy(&dhcp->fixedheader.ciaddr, &ciaddr, 4); return;}/* dhcp yiaddr: no conversion necessary */void dhcp_set_yiaddr(dhcp_obj * dhcp, uint32_t yiaddr){ memcpy(&dhcp->fixedheader.yiaddr, &yiaddr, 4); return;}/* dhcp siaddr: no conversion necessary */void dhcp_set_siaddr(dhcp_obj * dhcp, uint32_t siaddr){ memcpy(&dhcp->fixedheader.siaddr, &siaddr, 4); return;}/* dhcp giaddr: no conversion necessary */void dhcp_set_giaddr(dhcp_obj * dhcp, uint32_t giaddr){ memcpy(&dhcp->fixedheader.giaddr, &giaddr, 4); return;}/* dhcp chaddr */void dhcp_set_chaddr(dhcp_obj * dhcp, uint8_t *chaddr, int len){ memcpy(&dhcp->fixedheader.chaddr, chaddr, len);}/* dhcp chaddr */void dhcp_clear_chaddr(dhcp_obj * dhcp){ memset(dhcp->fixedheader.chaddr, 0, DHCP_CHADDR_SIZE); return;}/* dhcp sname */void dhcp_set_sname(dhcp_obj * dhcp, uint8_t *sname){ memcpy(&dhcp->fixedheader.sname, sname, DHCP_SNAME_SIZE); return;}/* dhcp file */void dhcp_set_filename(dhcp_obj * dhcp, uint8_t *file){ memcpy(&dhcp->fixedheader.file, file, DHCP_FILE_SIZE); return;}/* dhcp sname */void dhcp_clear_sname(dhcp_obj * dhcp){ memset(&dhcp->fixedheader.sname, 0, DHCP_SNAME_SIZE); return;}/* dhcp file */void dhcp_clear_filename(dhcp_obj * dhcp){ memset(&dhcp->fixedheader.file, 0, DHCP_FILE_SIZE); return;}/* cookie. */void dhcp_set_magic_cookie(dhcp_obj * dhcp){ memcpy(&dhcp->magic_cookie, dhcp_magic_cookie, 4); return;}/* dhcp set options, add options. *//* replace options with list passed (convenience). */void dhcp_set_options(dhcp_obj * dhcp, list_t *options){ dhcp->options = options; return;}/* Options routines. *//* Reset the options seek point. */void dhcp_reset_option_seek(dhcp_obj * dhcp){ list_rewind(dhcp->options);}/* Return the next option or NULL if at the end. */dhcp_opt_t *dhcp_get_next_option(dhcp_obj * dhcp){ return(list_next(dhcp->options));}/* tests. *//* test for valid cookie. */int dhcp_valid_magic_cookie(dhcp_obj * dhcp){ if(!memcmp(&dhcp->magic_cookie, dhcp_magic_cookie, 4)) return 1; else return 0;}/* test for type. */int dhcp_is_type(dhcp_obj * dhcp, uint8_t type){ dhcp_opt_t *opt; uint8_t *message_type; dhcp_reset_option_seek(dhcp); while((opt = dhcp_get_next_option(dhcp)) != NULL) { if(dhcp_opt_get_tag(opt) == TAG_DHCP_MESSAGE_TYPE) { message_type = dhcp_opt_get_host_data(opt); if(*message_type == type) { return 1; } else { return 0; } } } return 0;}/* test for existance of option. */int dhcp_have_option(dhcp_obj *dhcp, uint8_t tag){ dhcp_opt_t *option; /* Check for a option by tag. */ dhcp_reset_option_seek(dhcp); while((option = dhcp_get_next_option(dhcp)) != NULL) { if(dhcp_opt_get_tag(option) == tag) return 1; } return 0;}/* get the overload which may be for file, sname, or both. */static uint8_t dhcp_get_overload_option(dhcp_obj * dhcp){ dhcp_opt_t *option; dhcp_reset_option_seek(dhcp); while((option = dhcp_get_next_option(dhcp)) != NULL) { if(dhcp_opt_get_tag(option) == TAG_DHCP_OVERLOAD) { return *(uint8_t *)(dhcp_opt_get_host_data(option)); } } return 0;}/* options overloaded on file field? */int dhcp_is_file_overload(dhcp_obj * dhcp){ uint8_t overload_val = dhcp_get_overload_option(dhcp); if(overload_val == DHCP_OVERLOAD_FILE || overload_val == DHCP_OVERLOAD_BOTH) return 1; else return 0;}/* options overloaded on sname field? */int dhcp_is_sname_overload(dhcp_obj * dhcp){ uint8_t overload_val = dhcp_get_overload_option(dhcp); if(overload_val == DHCP_OVERLOAD_SNAME || overload_val == DHCP_OVERLOAD_BOTH) return 1; else return 0;}/* This is a little tricky since we haven't written anything * yet. We know that we must end on a 32-bit boundary, and * we know that we must include the tag, len on each option * as well as the end option '255' */uint16_t dhcp_get_options_len(list_t *options){ uint16_t len = 0; dhcp_opt_t *opt; int padding; list_rewind(options); while((opt = list_next(options)) != NULL) { len += 2; /* for tag and len bytes. */ len += dhcp_opt_get_total_len(opt); } len += 1; /* For the end option. */ if((padding = len % 4)) len += padding; return len;}/* write options to packet pointer. */void dhcp_write_options(dhcp_obj * dhcp, uint8_t *packet){ dhcp_opt_t *option; size_t len = 0; size_t padding; uint8_t *network_data; dhcp_reset_option_seek(dhcp); while((option = dhcp_get_next_option(dhcp)) != NULL) { *packet = dhcp_opt_get_tag(option); packet += 1; len++; *packet = dhcp_opt_get_total_len(option); packet += 1; len++; network_data = dhcp_opt_get_network_data(option); memcpy(packet, network_data, dhcp_opt_get_total_len(option)); xfree(network_data); packet += dhcp_opt_get_total_len(option); len += dhcp_opt_get_total_len(option); } *packet = TAG_DHCP_END; packet += 1; len += 1; /* Make sure we pad up. */ if((padding = len % 4)) { while(padding--) { *packet = TAG_DHCP_PAD; packet++; } } return;}/* write dhcp image out to packet. */void dhcp_write_packet_image(dhcp_obj * dhcp, uint8_t *packet){ /* Dump fixed header first. */ 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); packet += DHCP_CHADDR_SIZE; memcpy(packet, &dhcp->fixedheader.sname, DHCP_SNAME_SIZE); packet += DHCP_SNAME_SIZE; memcpy(packet, &dhcp->fixedheader.file, DHCP_FILE_SIZE); packet += DHCP_FILE_SIZE; /* Now dump cookie. */ memcpy(packet, &dhcp->magic_cookie, sizeof(dhcp->magic_cookie)); packet += sizeof(dhcp->magic_cookie); /* Now write options. */ dhcp_write_options(dhcp, packet); return;}/* generate xid. */uint32_t dhcp_gen_xid(void){ return (get_random_uint32());}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -