⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dhcp-option.c

📁 this is sample about DHCP-agent
💻 C
📖 第 1 页 / 共 3 页
字号:
      dhcp_opt_from_user_string_list_ip_addr,      dhcp_opt_from_internal_string_list_ip_addr,      dhcp_opt_destroy_list,    },    /* SMTP Server */    { TAG_DHCP_SMTP_SERVER,      IP_ADDR_LEN,      DHCP_OPT_LIST,      DHCP_OPT_VAL_ADDRESS,      dhcp_opt_to_network_list_ip_addr,      dhcp_opt_to_user_string_list_ip_addr,      dhcp_opt_to_internal_string_list_ip_addr,      dhcp_opt_from_network_list_ip_addr,      dhcp_opt_from_user_string_list_ip_addr,      dhcp_opt_from_internal_string_list_ip_addr,      dhcp_opt_destroy_list,    },    /* POP Server */    { TAG_DHCP_POP_SERVER,      IP_ADDR_LEN,      DHCP_OPT_LIST,      DHCP_OPT_VAL_ADDRESS,      dhcp_opt_to_network_list_ip_addr,      dhcp_opt_to_user_string_list_ip_addr,      dhcp_opt_to_internal_string_list_ip_addr,      dhcp_opt_from_network_list_ip_addr,      dhcp_opt_from_user_string_list_ip_addr,      dhcp_opt_from_internal_string_list_ip_addr,      dhcp_opt_destroy_list,    },    /* POP Server */    { TAG_DHCP_POP_SERVER,      IP_ADDR_LEN,      DHCP_OPT_LIST,      DHCP_OPT_VAL_ADDRESS,      dhcp_opt_to_network_list_ip_addr,      dhcp_opt_to_user_string_list_ip_addr,      dhcp_opt_to_internal_string_list_ip_addr,      dhcp_opt_from_network_list_ip_addr,      dhcp_opt_from_user_string_list_ip_addr,      dhcp_opt_from_internal_string_list_ip_addr,      dhcp_opt_destroy_list,    },    /* NNTP Server */    { TAG_DHCP_NNTP_SERVER,      IP_ADDR_LEN,      DHCP_OPT_LIST,      DHCP_OPT_VAL_ADDRESS,      dhcp_opt_to_network_list_ip_addr,      dhcp_opt_to_user_string_list_ip_addr,      dhcp_opt_to_internal_string_list_ip_addr,      dhcp_opt_from_network_list_ip_addr,      dhcp_opt_from_user_string_list_ip_addr,      dhcp_opt_from_internal_string_list_ip_addr,      dhcp_opt_destroy_list,    },    /* Default WWW Server */    { TAG_DHCP_DEFAULT_WWW_SERVER,      IP_ADDR_LEN,      DHCP_OPT_LIST,      DHCP_OPT_VAL_ADDRESS,      dhcp_opt_to_network_list_ip_addr,      dhcp_opt_to_user_string_list_ip_addr,      dhcp_opt_to_internal_string_list_ip_addr,      dhcp_opt_from_network_list_ip_addr,      dhcp_opt_from_user_string_list_ip_addr,      dhcp_opt_from_internal_string_list_ip_addr,      dhcp_opt_destroy_list,    },    /* Default Finger Server */    { TAG_DHCP_DEFAULT_FINGER_SERVER,      IP_ADDR_LEN,      DHCP_OPT_LIST,      DHCP_OPT_VAL_ADDRESS,      dhcp_opt_to_network_list_ip_addr,      dhcp_opt_to_user_string_list_ip_addr,      dhcp_opt_to_internal_string_list_ip_addr,      dhcp_opt_from_network_list_ip_addr,      dhcp_opt_from_user_string_list_ip_addr,      dhcp_opt_from_internal_string_list_ip_addr,      dhcp_opt_destroy_list,    },    /* Default IRC Server */    { TAG_DHCP_DEFAULT_IRC_SERVER,      IP_ADDR_LEN,      DHCP_OPT_LIST,      DHCP_OPT_VAL_ADDRESS,      dhcp_opt_to_network_list_ip_addr,      dhcp_opt_to_user_string_list_ip_addr,      dhcp_opt_to_internal_string_list_ip_addr,      dhcp_opt_from_network_list_ip_addr,      dhcp_opt_from_user_string_list_ip_addr,      dhcp_opt_from_internal_string_list_ip_addr,      dhcp_opt_destroy_list,    },    /* Default Street Talk Server */    { TAG_DHCP_DEFAULT_ST_SERVER,      IP_ADDR_LEN,      DHCP_OPT_LIST,      DHCP_OPT_VAL_ADDRESS,      dhcp_opt_to_network_list_ip_addr,      dhcp_opt_to_user_string_list_ip_addr,      dhcp_opt_to_internal_string_list_ip_addr,      dhcp_opt_from_network_list_ip_addr,      dhcp_opt_from_user_string_list_ip_addr,      dhcp_opt_from_internal_string_list_ip_addr,      dhcp_opt_destroy_list,    },    /* Default Street Talk Directory Server */    { TAG_DHCP_DEFAULT_STDA_SERVER,      IP_ADDR_LEN,      DHCP_OPT_LIST,      DHCP_OPT_VAL_ADDRESS,      dhcp_opt_to_network_list_ip_addr,      dhcp_opt_to_user_string_list_ip_addr,      dhcp_opt_to_internal_string_list_ip_addr,      dhcp_opt_from_network_list_ip_addr,      dhcp_opt_from_user_string_list_ip_addr,      dhcp_opt_from_internal_string_list_ip_addr,      dhcp_opt_destroy_list,    },};/* utilities. *//* create a template: an option datum pointing to the appropriate attributes. */static dhcp_opt_t *dhcp_opt_create_template(dhcp_opt_tag_t tag){    dhcp_opt_t *opt;    if(tag > MAX_OPTIONS_HANDLED)  {        ERROR_MESSAGE("passed out of range option tag: %d", tag);        return NULL;    }    opt = xcalloc(sizeof(dhcp_opt_t));    opt->opt_attr = &dhcp_opt_attr[tag];    return opt;}/* * * * * * * * * * * create/destroy. * * * * * * * * * * *//* destruction routines. *//* destroy an atom type value. */static void dhcp_opt_destroy_atom(dhcp_opt_t *opt){    if(opt->val != NULL)        xfree(opt->val);    return;}/* destroy a list type value. */static void dhcp_opt_destroy_list(dhcp_opt_t *opt){    list_t  *list;    list = opt->val;    if(list != NULL)        list_destroy(list, xfree);    return;}/* destroy a string type value. */static void dhcp_opt_destroy_string(dhcp_opt_t *opt){    return dhcp_opt_destroy_atom(opt);}/* destroy an array type value. */static void dhcp_opt_destroy_array(dhcp_opt_t *opt){    return dhcp_opt_destroy_atom(opt);}void dhcp_opt_destroy(dhcp_opt_t *opt){    DHCP_OPT_DESTROY(opt);    xfree(opt);    return;}/* create routines. *//* create a new dhcp option datum from network data. */dhcp_opt_t *dhcp_opt_create_from_network(const uint8_t *data, dhcp_opt_len_t len, dhcp_opt_tag_t tag){    dhcp_opt_t *opt;    /* create a template. */    opt = dhcp_opt_create_template(tag);    if(opt == NULL)        return NULL;    /* now that we have a template populate the data. */    if(DHCP_OPT_FROM_NETWORK_DATA(opt, data, len))  {        dhcp_opt_destroy(opt);        return NULL;    }    return opt;}/* create from internal data. */dhcp_opt_t *dhcp_opt_create_from_internal_data(uint8_t tag, void *data, size_t len){    dhcp_opt_t *opt;    /* create a template. */    opt = dhcp_opt_create_template(tag);    if(opt == NULL)        return NULL;    switch(DHCP_OPT_GET_TYPE(opt))  {    case DHCP_OPT_LIST:        /* if its a list we need to do a list copy. otherwise just         * copy the memory over. arg len doesn't apply here. */        opt->val = list_copy(data, DHCP_OPT_GET_MEM_LEN(opt));        opt->num = list_get_len(data);        break;    case DHCP_OPT_ATOM:        /* an atom means a single datum the size of the option.         * copy the memory over. arg len doesn't apply here. */        opt->val = xmalloc(DHCP_OPT_GET_MEM_LEN(opt));        memcpy(opt->val, data, DHCP_OPT_GET_MEM_LEN(opt));        opt->num = 1;        break;    case DHCP_OPT_ARRAY:        /* array means multiple datums which are better stored in         * a contiginous space of memory (not a list).  len is         * the total size, so we need to divide by the size of         * the datum to find num. */        opt->val = xmalloc(len);        opt->num = len/DHCP_OPT_GET_MEM_LEN(opt);        memcpy(opt->val, data, len);        break;    case DHCP_OPT_STRING:        /* a string is easily copied. since it's internal we         * expect it to be null terminated. */        opt->val = xstrdup(data);        opt->num = strlen(data);        break;    default:        FATAL_MESSAGE("illegal type indicated by dhcp option. this is a bug report me.");        exit(1);    }    return opt;}/* create from a user supplied string. */dhcp_opt_t *dhcp_opt_create_from_user_string(uint8_t tag, void *string_data){    dhcp_opt_t *opt;    /* create a template. */    opt = dhcp_opt_create_template(tag);    if(opt == NULL)        return NULL;    if(DHCP_OPT_FROM_USER_STRING(opt, string_data))  {        dhcp_opt_destroy(opt);        return NULL;    }    return opt;}/* create from an internally formed string. */dhcp_opt_t *dhcp_opt_create_from_host_string(uint8_t tag, char *input){    dhcp_opt_t *opt;    /* create a template. */    opt = dhcp_opt_create_template(tag);    if(opt == NULL)        return NULL;    if(DHCP_OPT_FROM_INTERNAL_STRING(opt, input))  {        dhcp_opt_destroy(opt);        return NULL;    }    return opt;}/* get routines. *//* get network data. */void *dhcp_opt_get_network_data(dhcp_opt_t *opt){    return DHCP_OPT_TO_NETWORK_DATA(opt);}/* get host data. */void *dhcp_opt_get_host_data(dhcp_opt_t *opt){    return opt->val;}/* get user string. */char *dhcp_opt_get_user_string(dhcp_opt_t *opt){    return DHCP_OPT_TO_USER_STRING(opt);}/* get internal string. */char *dhcp_opt_get_internal_string(dhcp_opt_t *opt){    return DHCP_OPT_TO_INTERNAL_STRING(opt);}/* get member length. */size_t dhcp_opt_get_mem_len(dhcp_opt_t *opt){    return DHCP_OPT_GET_MEM_LEN(opt);}/* get total length. */dhcp_opt_len_t dhcp_opt_get_total_len(dhcp_opt_t *opt){    return DHCP_OPT_GET_TOTAL_LEN(opt);}/* get num. */size_t dhcp_opt_get_num(dhcp_opt_t *opt){    return opt->num;}dhcp_opt_type_t dhcp_opt_get_type(dhcp_opt_t *opt){    return DHCP_OPT_GET_TYPE(opt);}dhcp_opt_type_t dhcp_opt_get_val_type(dhcp_opt_t *opt){    return DHCP_OPT_GET_VAL_TYPE(opt);}dhcp_opt_tag_t dhcp_opt_get_tag(dhcp_opt_t *opt){    return DHCP_OPT_GET_TAG(opt);}/* convenience routines. *//* create a dhcp message type option. */dhcp_opt_t *dhcp_opt_create_message_type(uint8_t message_type){    return dhcp_opt_create_from_internal_data(TAG_DHCP_MESSAGE_TYPE, &message_type, sizeof(message_type));}/* accept bit array telling us which options to build the request option. */dhcp_opt_t *dhcp_opt_create_parameter_request_list(uint8_t *requested_options){    uint8_t i, j;    uint8_t len = 0;    dhcp_opt_t *option;    uint8_t *opt_request;    /* Grab length so we can just malloc all at once. */    for(i = 0; i < MAX_OPTIONS_HANDLED; i++) {        if(requested_options[i])            len++;    }    if(len == 0) {        /* no parameter list. return empty list. */        return NULL;    }    opt_request = xmalloc(len * sizeof(uint8_t));    j = 0;    for(i = 0; i < MAX_OPTIONS_HANDLED; i++) {        if(requested_options[i]) {            opt_request[j] = i;            j++;        }    }    option = dhcp_opt_create_from_internal_data(TAG_DHCP_PARAMETERS, opt_request, len);    xfree(opt_request);    return option;}/* destroy a list of options by calling their destruction routine. */void dhcp_opt_destroy_option_list(list_t *list){    dhcp_opt_t *option;    list_rewind(list);    while((option = list_next(list)) != NULL)  {        list_remove_by_datum(list, option);        dhcp_opt_destroy(option);    }    list_destroy(list, NULL);    return;}/* copy a dhcp option. */dhcp_opt_t *dhcp_option_copy(dhcp_opt_t *option){    return dhcp_opt_create_from_internal_data(dhcp_opt_get_tag(option),                                               dhcp_opt_get_host_data(option),                                              dhcp_opt_get_total_len(option));}/* append to a dhcp list datum. */void dhcp_option_append(dhcp_opt_t *orig, dhcp_opt_t *data){    uint8_t *new_data, *orig_data;    list_t *orig_list, *append_list;    dhcp_opt_len_t len;    orig_list = dhcp_opt_get_host_data(orig);    append_list = dhcp_opt_get_host_data(data);    len = dhcp_opt_get_mem_len(orig);    list_rewind(append_list);    while((orig_data = list_next(append_list)) != NULL) {        new_data = xmalloc(len);        memcpy(new_data, orig_data, len);        list_add_to_end(orig_list, new_data);        orig->num++;    }    return;}/* prepend to a dhcp list datum. */void dhcp_option_prepend(dhcp_opt_t *orig, dhcp_opt_t *data){    uint8_t *new_data, *orig_data;    list_t *orig_list, *prepend_list;    dhcp_opt_len_t len;    orig_list = dhcp_opt_get_host_data(orig);    prepend_list = dhcp_opt_get_host_data(data);    len = dhcp_opt_get_mem_len(orig);    list_rewind(prepend_list);    while((orig_data = list_next(prepend_list)) != NULL) {        new_data = xmalloc(len);        memcpy(new_data, orig_data, len);        list_add(orig_list, new_data);        orig->num++;    }    return;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -