[dhcp-agent-commits] dhcp-agent/src dhcp-option-guile.h,NONE,1.1 dhcp-option-guile.c,NONE,1.1
Status: Alpha
Brought to you by:
actmodern
From: <act...@us...> - 2003-08-10 01:21:03
|
Update of /cvsroot/dhcp-agent/dhcp-agent/src In directory sc8-pr-cvs1:/tmp/cvs-serv18912 Added Files: dhcp-option-guile.h dhcp-option-guile.c Log Message: added new dhcp option SMOB --- NEW FILE: dhcp-option-guile.h --- /* $Header: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-option-guile.h,v 1.1 2003/08/10 01:20:58 actmodern Exp $ * * Copyright 2002 Thamer Alharbash * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. The names of the authors may not be used to endorse or promote * products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. * * dhcp option SMOB * */ #ifndef DHCP_OPT_GUILE_H #define DHCP_OPT_GUILE_H typedef struct { dhcp_opt_t *dhcp_opt; } dhcp_opt_smob_t; extern SCM scm_dhcp_opt_c2scm(dhcp_opt_t *dhcp_opt); extern void init_dhcp_opt_smob(void); #endif /* DHCP_OPT_GUILE_H */ --- NEW FILE: dhcp-option-guile.c --- /* $Header: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-option-guile.c,v 1.1 2003/08/10 01:20:58 actmodern Exp $ * * Copyright 2002 Thamer Alharbash * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. The names of the authors may not be used to endorse or promote * products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. * * dhcp option SMOB * */ #include "dhcp-local.h" #include "dhcp-libutil.h" #include "dhcp-librawnet.h" #include "dhcp-option.h" #include "dhcp-guile-util.h" #include "dhcp-option-guile.h" #include "dhcp-limits.h" static scm_t_bits dhcp_opt_tag; /* * * * * * * * * * * * utility routines. * * * * * * * * * * * */ static int scm_to_dhcp_tag(SCM scm_tag, dhcp_opt_tag_t *tag) { int inum; SCM_ASSERT(SCM_STRINGP(scm_tag)|SCM_INUMP(scm_tag), scm_tag, SCM_ARG1, "dhcp-opt-make"); if(!(SCM_STRINGP(scm_tag))) { inum = scm_num2int(scm_string_to_number(scm_tag, SCM_MAKINUM(10)), 10, "scm_to_dhcp_tag"); } else { /* FIXME: guile doesn't support C99 types yet. when it * does create a function to convert specifically to * dhcp_opt_tag_t which is actually a uint8_t */ inum = scm_num2int(scm_tag, 10, "scm_to_dhcp_tag"); } /* make sure its not greater than what we're prepared to * handle, and that it's not equal to 0 (pad) or less than * zero. */ if(inum > MAX_OPTIONS_HANDLED || inum <= 0) { return 1; } else { *tag = (uint8_t)inum; return 0; } } /* take a single datum option and turn it into a dhcp option. */ static dhcp_opt_t *scm_dhcp_opt_string_to_option(dhcp_opt_tag_t tag, SCM scm_dhcp_opt_data) { dhcp_opt_t *option; char *user_string; list_t *user_string_list; /* just call the user string creation function. */ user_string = x_scm_string2newstr(scm_dhcp_opt_data); user_string_list = list_create(); list_add(user_string_list, user_string); option = dhcp_opt_create_from_user_string(tag, user_string); list_destroy(user_string_list, xfree); return option; } /* take a list datum option and turn it into a dhcp option. */ static dhcp_opt_t *scm_dhcp_opt_list_to_option(dhcp_opt_tag_t tag, SCM scm_dhcp_opt_data) { dhcp_opt_t *option; list_t *string_list; /* just call the user string creation function. */ string_list = x_scm_guile_string_list_to_list(scm_dhcp_opt_data); option = dhcp_opt_create_from_user_string(tag, string_list); list_destroy(string_list, xfree); return option; } /* * * * * * * * * * * * dhcp option smob. * * * * * * * * * * * */ /* mark a dhcp_opt smob: we don't need to do any marking. */ static SCM scm_dhcp_opt_mark(SCM scm_dhcp_opt) { return SCM_BOOL_F; } /* free a dhcp_opt. */ static size_t scm_dhcp_opt_free(SCM scm_dhcp_opt) { dhcp_opt_smob_t *dhcp_opt_smob; dhcp_opt_smob = (dhcp_opt_smob_t *)SCM_SMOB_DATA(scm_dhcp_opt); dhcp_opt_destroy(dhcp_opt_smob->dhcp_opt); xfree(dhcp_opt_smob); return sizeof(dhcp_opt_smob); } /* print out a dhcp opt smob. */ static int scm_dhcp_opt_print(SCM scm_dhcp_opt, SCM port, scm_print_state *pstate) { scm_puts("#<dhcp_opt>", port); return 1; } /* create a dhcp_opt smob from a C level dhcp option */ SCM scm_dhcp_opt_c2scm(dhcp_opt_t *dhcp_opt) { dhcp_opt_smob_t *dhcp_opt_smob; dhcp_opt_t *new_dhcp_opt; new_dhcp_opt = dhcp_option_copy(dhcp_opt); dhcp_opt_smob = scm_must_malloc(sizeof(dhcp_opt_smob_t), "dhcp-opt-make"); dhcp_opt_smob->dhcp_opt = new_dhcp_opt; SCM_RETURN_NEWSMOB(dhcp_opt_tag, dhcp_opt_smob); } /* create a dhcp option: called from scheme code. */ static SCM scm_dhcp_opt_make(SCM scm_dhcp_tag, SCM scm_dhcp_opt_data) { dhcp_opt_tag_t tag; dhcp_opt_t *option; SCM_ASSERT(SCM_STRINGP(scm_dhcp_tag)|SCM_INUMP(scm_dhcp_tag), scm_dhcp_tag, SCM_ARG1, "dhcp-opt-make"); SCM_ASSERT(SCM_CONSP(scm_dhcp_opt_data)|SCM_STRINGP(scm_dhcp_opt_data), scm_dhcp_tag, SCM_ARG1, "dhcp-opt-make"); if(scm_to_dhcp_tag(scm_dhcp_tag, &tag)) { scm_error(scm_str2symbol("illegal-dhcp-tag"), "scm_dhcp_opt_make", "dhcp tag unhandled or out of range", SCM_EOL, SCM_EOL); } switch(dhcp_opt_get_type_by_tag(tag)) { case DHCP_OPT_LIST: if(!(SCM_CONSP(scm_dhcp_tag))) scm_error(scm_str2symbol("bad-option-data"), "scm_dhcp_opt_make", "dhcp data in atom form while list expected", SCM_EOL, SCM_EOL); option = scm_dhcp_opt_list_to_option(tag, scm_dhcp_opt_data); break; case DHCP_OPT_ATOM: case DHCP_OPT_STRING: case DHCP_OPT_ARRAY: if(!(SCM_CONSP(scm_dhcp_tag))) scm_error(scm_str2symbol("bad-option-data"), "scm_dhcp_opt_make", "dhcp data in list form while atom expected", SCM_EOL, SCM_EOL); option = scm_dhcp_opt_string_to_option(tag, scm_dhcp_opt_data); break; default: scm_error(scm_str2symbol("unknown-option-data-type"), "scm_dhcp_opt_make", "unable to convert dhcp option data. this is a bug. report me.", SCM_EOL, SCM_EOL); } if(option == NULL) { scm_error(scm_str2symbol("bad-option-data"), "scm_dhcp_opt_make", "could not create option from data passed", SCM_EOL, SCM_EOL); } return scm_dhcp_opt_c2scm(option); } /* accessors. */ static SCM scm_dhcp_opt_get_tag(SCM scm_dhcp_opt) { dhcp_opt_smob_t *dhcp_opt_smob; SCM_ASSERT(SCM_SMOB_PREDICATE(dhcp_opt_tag, scm_dhcp_opt), scm_dhcp_opt, SCM_ARG1, "scm_dhcp_opt_get_tag"); dhcp_opt_smob = (dhcp_opt_smob_t *)SCM_SMOB_DATA(scm_dhcp_opt); return SCM_MAKINUM(dhcp_opt_get_tag(dhcp_opt_smob->dhcp_opt)); } static SCM scm_dhcp_opt_get_name(SCM scm_dhcp_opt) { dhcp_opt_smob_t *dhcp_opt_smob; SCM_ASSERT(SCM_SMOB_PREDICATE(dhcp_opt_tag, scm_dhcp_opt), scm_dhcp_opt, SCM_ARG1, "scm_dhcp_opt_get_name"); dhcp_opt_smob = (dhcp_opt_smob_t *)SCM_SMOB_DATA(scm_dhcp_opt); return scm_makfrom0str(dhcp_opt_get_user_string(dhcp_opt_smob->dhcp_opt)); } static SCM scm_dhcp_opt_get_type(SCM scm_dhcp_opt) { dhcp_opt_smob_t *dhcp_opt_smob; dhcp_opt_type_t dhcp_type; SCM_ASSERT(SCM_SMOB_PREDICATE(dhcp_opt_tag, scm_dhcp_opt), scm_dhcp_opt, SCM_ARG1, "scm_dhcp_opt_get_type"); dhcp_opt_smob = (dhcp_opt_smob_t *)SCM_SMOB_DATA(scm_dhcp_opt); dhcp_type = dhcp_opt_get_type(dhcp_opt_smob->dhcp_opt); switch(dhcp_type) { case DHCP_OPT_LIST: return scm_str2symbol("dhcp-opt-type-list"); case DHCP_OPT_STRING: case DHCP_OPT_ATOM: case DHCP_OPT_ARRAY: return scm_str2symbol("dhcp-opt-type-atom"); default: return scm_str2symbol("dhcp-opt-type-unknown"); } } static SCM scm_dhcp_opt_get_val_type(SCM scm_dhcp_opt) { dhcp_opt_smob_t *dhcp_opt_smob; dhcp_opt_val_type_t dhcp_type; SCM_ASSERT(SCM_SMOB_PREDICATE(dhcp_opt_tag, scm_dhcp_opt), scm_dhcp_opt, SCM_ARG1, "scm_dhcp_opt_val_type"); dhcp_opt_smob = (dhcp_opt_smob_t *)SCM_SMOB_DATA(scm_dhcp_opt); dhcp_type = dhcp_opt_get_val_type(dhcp_opt_smob->dhcp_opt); switch(dhcp_type) { case DHCP_OPT_VAL_UINT32: return scm_str2symbol("dhcp-opt-val-type-uint32"); case DHCP_OPT_VAL_INT32: return scm_str2symbol("dhcp-opt-val-type-int32"); case DHCP_OPT_VAL_UINT16: return scm_str2symbol("dhcp-opt-val-type-int32"); case DHCP_OPT_VAL_INT16: return scm_str2symbol("dhcp-opt-val-type-int16"); case DHCP_OPT_VAL_UINT8: return scm_str2symbol("dhcp-opt-val-type-uint8"); case DHCP_OPT_VAL_INT8: return scm_str2symbol("dhcp-opt-val-type-int8"); case DHCP_OPT_VAL_ADDRESS: return scm_str2symbol("dhcp-opt-val-type-address"); case DHCP_OPT_VAL_ADDRESS_PAIR: return scm_str2symbol("dhcp-opt-val-type-address-pair"); default: return scm_str2symbol("dhcp-opt-val-type-unknown"); } } /* get data from a dhcp option. */ static SCM scm_dhcp_opt_get_data(SCM scm_dhcp_opt) { dhcp_opt_smob_t *dhcp_opt_smob; char *string_val; SCM scm_string_val; dhcp_opt_type_t dhcp_type; SCM_ASSERT(SCM_SMOB_PREDICATE(dhcp_opt_tag, scm_dhcp_opt), scm_dhcp_opt, SCM_ARG1, "scm_dhcp_opt_get_data"); dhcp_opt_smob = (dhcp_opt_smob_t *)SCM_SMOB_DATA(scm_dhcp_opt); dhcp_type = dhcp_opt_get_type(dhcp_opt_smob->dhcp_opt); string_val = dhcp_opt_get_user_string(dhcp_opt_smob->dhcp_opt); switch(dhcp_type) { case DHCP_OPT_LIST: /* string val is comma delimited list, convert here. */ scm_string_val = x_scm_comma_delimited_string_to_scm_list(string_val); default: scm_string_val = scm_makfrom0str(string_val); } xfree(string_val); return scm_string_val; } /* setters. */ /* set dhcp option data overwriting old data. */ static SCM scm_dhcp_opt_set_data(SCM scm_dhcp_opt, SCM scm_data) { dhcp_opt_smob_t *dhcp_opt_smob; dhcp_opt_type_t dhcp_type; list_t *strings; dhcp_opt_tag_t tag; dhcp_opt_t *new_option; SCM_ASSERT(SCM_SMOB_PREDICATE(dhcp_opt_tag, scm_dhcp_opt), scm_dhcp_opt, SCM_ARG1, "scm_dhcp_opt_set_data"); SCM_ASSERT(SCM_CONSP(scm_data)|SCM_STRINGP(scm_data), scm_data, SCM_ARG2, "scm_dhcp_opt_set_data"); dhcp_opt_smob = (dhcp_opt_smob_t *)SCM_SMOB_DATA(scm_dhcp_opt); dhcp_type = dhcp_opt_get_type(dhcp_opt_smob->dhcp_opt); switch(dhcp_type) { case DHCP_OPT_LIST: if(!SCM_CONSP(scm_data)) { scm_error(scm_str2symbol("bad-option-data"), "scm_dhcp_opt_make", "expected list and received atom", SCM_EOL, SCM_EOL); } strings = x_scm_guile_string_list_to_list(scm_data); break; case DHCP_OPT_ATOM: case DHCP_OPT_ARRAY: case DHCP_OPT_STRING: if(!SCM_STRINGP(scm_data)) { scm_error(scm_str2symbol("bad-option-data"), "scm_dhcp_opt_make", "expected atom and received list", SCM_EOL, SCM_EOL); } strings = list_create(); list_add(strings, x_scm_string2newstr(scm_data)); break; default: scm_error(scm_str2symbol("bad-option-data"), "scm_dhcp_opt_make", "cannot set data since option has unknown type", SCM_EOL, SCM_EOL); } tag = dhcp_opt_get_tag(dhcp_opt_smob->dhcp_opt); new_option = dhcp_opt_create_from_user_string(tag, strings); list_destroy(strings, xfree); if(new_option == NULL) { scm_error(scm_str2symbol("bad-option-data"), "scm_dhcp_opt_make", "could not create option from data passed", SCM_EOL, SCM_EOL); } dhcp_opt_destroy(dhcp_opt_smob->dhcp_opt); dhcp_opt_smob->dhcp_opt = new_option; return SCM_BOOL_T; } /* initialization routine for option smob. */ void init_dhcp_opt_smob(void) { dhcp_opt_tag = scm_make_smob_type("dhcp_opt_smob", sizeof(dhcp_opt_smob_t)); /* set smob functions .*/ scm_set_smob_free(dhcp_opt_tag, scm_dhcp_opt_free); scm_set_smob_mark(dhcp_opt_tag, scm_dhcp_opt_mark); scm_set_smob_print(dhcp_opt_tag, scm_dhcp_opt_print); /* bind the subroutines. */ scm_c_define_gsubr("dhcp-opt-make", 2, 0, 0, scm_dhcp_opt_make); scm_c_define_gsubr("dhcp-opt-get-tag", 1, 0, 0, scm_dhcp_opt_get_tag); scm_c_define_gsubr("dhcp-opt-get-name", 1, 0, 0, scm_dhcp_opt_get_name); scm_c_define_gsubr("dhcp-opt-get-type", 1, 0, 0, scm_dhcp_opt_get_type); scm_c_define_gsubr("dhcp-opt-get-val-type", 1, 0, 0, scm_dhcp_opt_get_val_type); scm_c_define_gsubr("dhcp-opt-get-data", 1, 0, 0, scm_dhcp_opt_get_data); scm_c_define_gsubr("dhcp-opt-set-data!", 2, 0, 0, scm_dhcp_opt_set_data); return; } |