[dhcp-agent-commits] dhcp-agent/src dhcp-server-conf.c,NONE,1.1 dhcp-server-conf.h,NONE,1.1 dhcp-ser
Status: Alpha
Brought to you by:
actmodern
From: <act...@us...> - 2003-07-05 19:17:50
|
Update of /cvsroot/dhcp-agent/dhcp-agent/src In directory sc8-pr-cvs1:/tmp/cvs-serv20000/src Modified Files: dhcp-server.c dhcp-server.h Added Files: dhcp-server-conf.c dhcp-server-conf.h dhcp-server-control.c Log Message: added initial dhcp-server code --- NEW FILE: dhcp-server-conf.c --- /* $Header: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-server-conf.c,v 1.1 2003/07/05 19:17:48 actmodern Exp $ * * Copyright 2002 Thamer Alharbash <tm...@wh...> * * 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. * */ #define MODULE_NAME "dhcp-server-conf" #include "dhcp-local.h" #include "dhcp-limits.h" #include "dhcp-libutil.h" #include "dhcp-librawnet.h" #include "dhcp-option.h" #include "dhcp-conf.h" #include "dhcp-conf-var.h" #include "dhcp-server-conf.h" static const char *var_strings[] = { "default-renew-percent", "default-rebind-percent", }; static const arg_symbol_t var_symbols[] = { SERVER_VAR_RENEW_PERCENT, SERVER_VAR_REBIND_PERCENT, }; static const arg_type_t var_arg_types[] = { CONF_IDENTIFIER, CONF_ASSIGNMENT, CONF_STRING }; static const char **var_arg_strings[] = { var_strings, NULL, NULL }; static const arg_symbol_t *var_arg_symbols[] = { var_symbols, NULL, NULL }; /* command structures. */ /* set command. */ static const command_t command_set = { DIRECTIVE_SET, "set", 3, var_arg_strings, var_arg_types, var_arg_symbols, }; static const command_t *commands[] = { &command_set, NULL, }; /* forward declaration of directive handlers. */ static int directive_set_handler(server_conf_t *server_conf, directive_t *directive); /* indexed by directive types in dhcp-client-conf.h . */ directive_handler_t directive_handlers[] = { directive_set_handler, }; /* * * * * * * * * * * * utility routines. * * * * * * * * * * * */ /* get name of configuration file for server. */ static char *get_conf_options_fname(const char *interface) { char *fname; stringbuffer_t *sb; sb = stringbuffer_create(); stringbuffer_aprintf(sb, "%s/default.conf", DHCPSYSCONF_SERVERDIR); if(file_exists(stringbuffer_getstring(sb))) { fname = xstrdup(stringbuffer_getstring(sb)); stringbuffer_destroy(sb); return fname; } stringbuffer_clear(sb); stringbuffer_aprintf(sb, "%s/%s.conf", DHCPSYSCONF_SERVERDIR, interface); if(file_exists(stringbuffer_getstring(sb))) { fname = xstrdup(stringbuffer_getstring(sb)); stringbuffer_destroy(sb); return fname; } /* otherwise there is no configuration file and this is * bad. signal a fatal error. */ FATAL_MESSAGE("configuration file missing -- looked for default.conf and <interface>.conf. exiting..."); exit(1); /* get rid of compiler warning. */ } /* set variable. */ static int server_conf_set_variable(server_conf_t *server_conf, arg_symbol_t var_symbol, const char *var_value) { switch(var_symbol) { case SERVER_VAR_RENEW_PERCENT: if(!is_unsigned_numeric(var_value)) { ERROR_MESSAGE("value not unsigned numeric as expected : %s\n", var_value); return 1; } sscanf(var_value, "%"SCNu16, &server_conf->default_renew_percent); if(server_conf->default_renew_percent > 100) { /* check if the percent is higher than it should be. */ FATAL_MESSAGE("value specified for default-renew-percent is an illegal percent (over 100)"); } break; case SERVER_VAR_REBIND_PERCENT: if(!is_unsigned_numeric(var_value)) { ERROR_MESSAGE("value not unsigned numeric as expected : %s", var_value); return 1; } sscanf(var_value, "%"SCNu16, &server_conf->default_rebind_percent); if(server_conf->default_renew_percent > 100) { /* check if the percent is higher than it should be. */ FATAL_MESSAGE("value specified for default-rebind-percent is an illegal percent (over 100)"); } break; default: ERROR_MESSAGE("unable to understand assignment value of : %s", var_value); return 1; } return 0; } /* load server options. */ static int server_conf_load_options(server_conf_t *sc) { conf_t *conf; list_t *directives; directive_t *directive; conf = conf_create(commands, sc->conf_file); if(conf_compile_directives(conf) == CONF_ERROR) { ERROR_MESSAGE("error reading conf file: %s at line %d", sc->conf_file, conf_get_line_no(conf)); conf_destroy(conf); return 1; } directives = conf_get_directives(conf); list_rewind(directives); while((directive = list_next(directives)) != NULL) { /* read in the compiled directives. */ if(directive_handlers[directive->command_code](sc, directive)) return 1; } return 0; } /* * * * * * * * * * * * * directive handlers. * * * * * * * * * * * * */ static int directive_set_handler(server_conf_t *server_conf, directive_t *directive) { list_t *args; arg_symbol_t *var_symbol; char *var_value; args = directive->arguments; var_symbol = list_get_by_index(args, 0); var_value = list_get_by_index(args, 2); if(server_conf_set_variable(server_conf, *var_symbol, var_value)) { ERROR_MESSAGE("configuration: error unable to set variable %s to %s", var_strings[*var_symbol], var_value); return 1; } return 0; } /* * * * * * * * * * * * public interface * * * * * * * * * * * */ server_conf_t *create_server_conf(const char *interface) { server_conf_t *sc; sc = xcalloc(sizeof(server_conf_t)); sc->interface = interface; sc->conf_file = get_conf_options_fname(interface); sc->variables = list_create(); sc->lease_defs = list_create(); if(server_conf_load_options(sc)) { server_conf_destroy(sc); return NULL; } return sc; } void server_conf_destroy(server_conf_t *sc) { /* not implemented yet. */ return; } --- NEW FILE: dhcp-server-conf.h --- /* $Header: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-server-conf.h,v 1.1 2003/07/05 19:17:48 actmodern Exp $ * * Copyright 2002 Thamer Alharbash <tm...@wh...> * * 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. * */ #ifndef DHCP_SERVER_CONF_H #define DHCP_SERVER_CONF_H /* data structures. */ typedef struct { list_t *variables; /* variable settings. */ list_t *lease_defs; /* list of lease definitions. */ const char *interface; /* interface name: read only. */ char *conf_file; /* path to configuration file. */ uint16_t default_rebind_percent; /* percent of expiry to assign as rebind time. */ uint16_t default_renew_percent; /* percent of expiry to assign as renew time. */ } server_conf_t; typedef int (*directive_handler_t)(server_conf_t *server_conf, directive_t *directive_data); /* constants. */ enum var_symbols { SERVER_VAR_RENEW_PERCENT = 0, SERVER_VAR_REBIND_PERCENT }; enum directive_types { DIRECTIVE_SET = 0 }; /* prototypes. */ extern server_conf_t *create_server_conf(const char *interface); extern void server_conf_destroy(server_conf_t *sc); #endif /* DHCP_SERVER_CONF_H */ --- NEW FILE: dhcp-server-control.c --- /* $Header: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-server-control.c,v 1.1 2003/07/05 19:17:48 actmodern Exp $ * * Copyright 2002 Thamer Alharbash <tm...@wh...> * * 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. * */ #define MODULE_NAME "dhcp-server-control" #include "dhcp-local.h" #include "dhcp-libutil.h" #include "dhcp-librawnet.h" #include "dhcp-lease.h" #include "dhcp-server-conf.h" #include "dhcp-server.h" dhcp_server_control_t *dhcp_server_control_create(const char *interface) { int dport, sport; stringbuffer_t *filter; dhcp_server_control_t *dhcp_server_control; dhcp_server_control = xcalloc(sizeof(dhcp_server_control_t)); dhcp_server_control->interface_name = xstrdup(interface); /* read configuration information. */ if((dhcp_server_control->server_conf = create_server_conf(interface)) == NULL) { FATAL_MESSAGE("could not read configuration file"); } /* get the source port and destination port for BOOTP. */ dport = rawnet_port_for_service("bootps", "udp"); sport = rawnet_port_for_service("bootpc", "udp"); dport = ntohs(dport); sport = ntohs(sport); if(dport == -1 || sport == -1) { WARN_MESSAGE ("could not lookup dhcp services in service db (%s) will use reasonable defaults."); sport = BOOTP_CLIENT; dport = BOOTP_SERVER; } /* Create filter. */ filter = stringbuffer_create(); stringbuffer_aprintf(filter, "arp or icmp or (udp and src port %d)", sport); /* create rawnet handler. */ dhcp_server_control->rawnet = rawnet_create(interface, stringbuffer_getstring(filter), -1, sport, dport, 0, 0); return dhcp_server_control; } void dhcp_server_control_destroy(dhcp_server_control_t *dhcp_server_control) { INFO_MESSAGE("not implemented yet."); return; } Index: dhcp-server.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-server.c,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** dhcp-server.c 2 Jul 2003 15:23:30 -0000 1.1 --- dhcp-server.c 5 Jul 2003 19:17:48 -0000 1.2 *************** *** 23,26 **** --- 23,28 ---- */ + #define MODULE_NAME "dhcp-server" + #include "dhcp-local.h" #include "dhcp-libutil.h" *************** *** 28,31 **** --- 30,34 ---- #include "dhcp-lease.h" + #include "dhcp-server-conf.h" #include "dhcp-server.h" #include "dhcp-server-guile.h" *************** *** 84,87 **** --- 87,94 ---- static void do_server(char *interface_name) { + dhcp_server_control_t *dhcp_server_control; + + dhcp_server_control = dhcp_server_control_create(interface_name); + INFO_MESSAGE("not implemented yet."); return; *************** *** 152,156 **** commands[command_code](interface_name); - exit(0); } --- 159,162 ---- Index: dhcp-server.h =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-server.h,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** dhcp-server.h 2 Jul 2003 15:23:30 -0000 1.1 --- dhcp-server.h 5 Jul 2003 19:17:48 -0000 1.2 *************** *** 26,44 **** #define DHCP_SERVER_H ! enum command_codes { DO_VERSION = 0, DO_KILL, DO_STATUS, DO_SERVER }; ! typedef void (*server_command) (char *interface); typedef struct { ! rawnet_t *rawnet; /* raw network handle * ! / char *interface_name; /* name of interface */ ! } dhcp_server_control_t; ! #endif /* DHCP_SERVER_H */ --- 26,47 ---- #define DHCP_SERVER_H ! /* type definitions. */ typedef void (*server_command) (char *interface); typedef struct { ! rawnet_t *rawnet; /* raw network handle */ char *interface_name; /* name of interface */ ! server_conf_t *server_conf; /* server configuration. */ } dhcp_server_control_t; ! /* constants. */ ! enum command_codes { DO_VERSION = 0, DO_KILL, DO_STATUS, DO_SERVER }; + /* prototypes. */ + extern dhcp_server_control_t *dhcp_server_control_create(const char *interface); + + #endif /* DHCP_SERVER_H */ |