📄 execute.c
字号:
/* execute.c Support for executable statements. *//* * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") * Copyright (c) 1998-2003 by Internet Software Consortium * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * * Internet Systems Consortium, Inc. * 950 Charter Street * Redwood City, CA 94063 * <info@isc.org> * http://www.isc.org/ * * This software has been written for Internet Systems Consortium * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc. * To learn more about Internet Systems Consortium, see * ``http://www.isc.org/''. To learn more about Vixie Enterprises, * see ``http://www.vix.com''. To learn more about Nominum, Inc., see * ``http://www.nominum.com''. */#ifndef lintstatic char copyright[] ="$Id: execute.c,v 1.44.2.10 2004/06/10 17:59:17 dhankins Exp $ Copyright (c) 2004 Internet Systems Consortium. All rights reserved.\n";#endif /* not lint */#include "dhcpd.h"#include <omapip/omapip_p.h>int execute_statements (result, packet, lease, client_state, in_options, out_options, scope, statements) struct binding_value **result; struct packet *packet; struct lease *lease; struct client_state *client_state; struct option_state *in_options; struct option_state *out_options; struct binding_scope **scope; struct executable_statement *statements;{ struct executable_statement *r, *e, *next; int rc; int status; unsigned long num; struct binding_scope *outer; struct binding *binding; struct data_string ds; struct binding_scope *ns; if (!statements) return 1; r = (struct executable_statement *)0; next = (struct executable_statement *)0; e = (struct executable_statement *)0; executable_statement_reference (&r, statements, MDL); while (r && !(result && *result)) { if (r -> next) executable_statement_reference (&next, r -> next, MDL); switch (r -> op) { case statements_statement:#if defined (DEBUG_EXPRESSIONS) log_debug ("exec: statements");#endif status = execute_statements (result, packet, lease, client_state, in_options, out_options, scope, r -> data.statements);#if defined (DEBUG_EXPRESSIONS) log_debug ("exec: statements returns %d", status);#endif if (!status) return 0; break; case on_statement: if (lease) { if (r -> data.on.evtypes & ON_EXPIRY) {#if defined (DEBUG_EXPRESSIONS) log_debug ("exec: on expiry");#endif if (lease -> on_expiry) executable_statement_dereference (&lease -> on_expiry, MDL); if (r -> data.on.statements) executable_statement_reference (&lease -> on_expiry, r -> data.on.statements, MDL); } if (r -> data.on.evtypes & ON_RELEASE) {#if defined (DEBUG_EXPRESSIONS) log_debug ("exec: on release");#endif if (lease -> on_release) executable_statement_dereference (&lease -> on_release, MDL); if (r -> data.on.statements) executable_statement_reference (&lease -> on_release, r -> data.on.statements, MDL); } if (r -> data.on.evtypes & ON_COMMIT) {#if defined (DEBUG_EXPRESSIONS) log_debug ("exec: on commit");#endif if (lease -> on_commit) executable_statement_dereference (&lease -> on_commit, MDL); if (r -> data.on.statements) executable_statement_reference (&lease -> on_commit, r -> data.on.statements, MDL); } } break; case switch_statement:#if defined (DEBUG_EXPRESSIONS) log_debug ("exec: switch");#endif status = (find_matching_case (&e, packet, lease, client_state, in_options, out_options, scope, r -> data.s_switch.expr, r -> data.s_switch.statements));#if defined (DEBUG_EXPRESSIONS) log_debug ("exec: switch: case %lx", (unsigned long)e);#endif if (status) { if (!(execute_statements (result, packet, lease, client_state, in_options, out_options, scope, e))) { executable_statement_dereference (&e, MDL); return 0; } executable_statement_dereference (&e, MDL); } break; /* These have no effect when executed. */ case case_statement: case default_statement: break; case if_statement: status = (evaluate_boolean_expression (&rc, packet, lease, client_state, in_options, out_options, scope, r -> data.ie.expr)); #if defined (DEBUG_EXPRESSIONS) log_debug ("exec: if %s", (status ? (rc ? "true" : "false") : "NULL"));#endif /* XXX Treat NULL as false */ if (!status) rc = 0; if (!execute_statements (result, packet, lease, client_state, in_options, out_options, scope, rc ? r -> data.ie.tc : r -> data.ie.fc)) return 0; break; case eval_statement: status = evaluate_expression ((struct binding_value **)0, packet, lease, client_state, in_options, out_options, scope, r -> data.eval, MDL);#if defined (DEBUG_EXPRESSIONS) log_debug ("exec: evaluate: %s", (status ? "succeeded" : "failed"));#endif break; case return_statement: status = evaluate_expression (result, packet, lease, client_state, in_options, out_options, scope, r -> data.retval, MDL);#if defined (DEBUG_EXPRESSIONS) log_debug ("exec: return: %s", (status ? "succeeded" : "failed"));#endif break; case add_statement:#if defined (DEBUG_EXPRESSIONS) log_debug ("exec: add %s", (r -> data.add -> name ? r -> data.add -> name : "<unnamed class>"));#endif classify (packet, r -> data.add); break; case break_statement:#if defined (DEBUG_EXPRESSIONS) log_debug ("exec: break");#endif return 1; case supersede_option_statement: case send_option_statement:#if defined (DEBUG_EXPRESSIONS) log_debug ("exec: %s option %s.%s", (r -> op == supersede_option_statement ? "supersede" : "send"), r -> data.option -> option -> universe -> name, r -> data.option -> option -> name); goto option_statement;#endif case default_option_statement:#if defined (DEBUG_EXPRESSIONS) log_debug ("exec: default option %s.%s", r -> data.option -> option -> universe -> name, r -> data.option -> option -> name); goto option_statement;#endif case append_option_statement:#if defined (DEBUG_EXPRESSIONS) log_debug ("exec: append option %s.%s", r -> data.option -> option -> universe -> name, r -> data.option -> option -> name); goto option_statement;#endif case prepend_option_statement:#if defined (DEBUG_EXPRESSIONS) log_debug ("exec: prepend option %s.%s", r -> data.option -> option -> universe -> name, r -> data.option -> option -> name); option_statement:#endif set_option (r -> data.option -> option -> universe, out_options, r -> data.option, r -> op); break; case set_statement: case define_statement: if (!scope) { log_error ("set %s: no scope", r -> data.set.name); status = 0; break; } if (!*scope) { if (!binding_scope_allocate (scope, MDL)) { log_error ("set %s: can't allocate scope", r -> data.set.name); status = 0; break; } } binding = find_binding (*scope, r -> data.set.name);#if defined (DEBUG_EXPRESSIONS) log_debug ("exec: set %s", r -> data.set.name);#endif if (!binding) { binding = dmalloc (sizeof *binding, MDL); if (binding) { memset (binding, 0, sizeof *binding); binding -> name = dmalloc (strlen (r -> data.set.name) + 1, MDL); if (binding -> name) { strcpy (binding -> name, r -> data.set.name); binding -> next = (*scope) -> bindings; (*scope) -> bindings = binding; } else { badalloc: dfree (binding, MDL); binding = (struct binding *)0; } } } if (binding) { if (binding -> value) binding_value_dereference (&binding -> value, MDL); if (r -> op == set_statement) { status = (evaluate_expression (&binding -> value, packet, lease, client_state, in_options, out_options, scope, r -> data.set.expr, MDL)); } else { if (!(binding_value_allocate (&binding -> value, MDL))) { dfree (binding, MDL); binding = (struct binding *)0; } if (binding -> value) { binding -> value -> type = binding_function; (fundef_reference (&binding -> value -> value.fundef, r -> data.set.expr -> data.func, MDL)); } } }#if defined (DEBUG_EXPRESSIONS) log_debug ("exec: set %s%s", r -> data.set.name, (binding && status ? "" : " (failed)"));#endif break; case unset_statement: if (!scope || !*scope) { status = 0; break; } binding = find_binding (*scope, r -> data.unset); if (binding) { if (binding -> value) binding_value_dereference (&binding -> value, MDL); status = 1; } else status = 0;#if defined (DEBUG_EXPRESSIONS) log_debug ("exec: unset %s: %s", r -> data.unset, (status ? "found" : "not found"));#endif break; case let_statement:#if defined (DEBUG_EXPRESSIONS) log_debug ("exec: let %s", r -> data.let.name);#endif ns = (struct binding_scope *)0; binding_scope_allocate (&ns, MDL); e = r; next_let: if (ns) { binding = dmalloc (sizeof *binding, MDL); memset (binding, 0, sizeof *binding); if (!binding) { blb: binding_scope_dereference (&ns, MDL); } else { binding -> name = dmalloc (strlen (e -> data.let.name + 1), MDL); if (binding -> name) strcpy (binding -> name, e -> data.let.name); else { dfree (binding, MDL); binding = (struct binding *)0; goto blb; } } } if (ns && binding) { status = (evaluate_expression (&binding -> value, packet, lease, client_state, in_options, out_options, scope, e -> data.set.expr, MDL)); binding -> next = ns -> bindings; ns -> bindings = binding; }#if defined (DEBUG_EXPRESSIONS) log_debug ("exec: let %s%s", e -> data.let.name, (binding && status ? "" : "failed"));#endif if (!e -> data.let.statements) { } else if (e -> data.let.statements -> op == let_statement) { e = e -> data.let.statements; goto next_let; } else if (ns) { if (scope && *scope) binding_scope_reference (&ns -> outer, *scope, MDL); execute_statements (result, packet, lease, client_state, in_options, out_options, &ns, e -> data.let.statements); } if (ns) binding_scope_dereference (&ns, MDL); break; case log_statement: memset (&ds, 0, sizeof ds); status = (evaluate_data_expression (&ds, packet, lease, client_state, in_options, out_options, scope, r -> data.log.expr, MDL)); #if defined (DEBUG_EXPRESSIONS) log_debug ("exec: log");#endif if (status) { switch (r -> data.log.priority) { case log_priority_fatal: log_fatal ("%.*s", (int)ds.len, ds.buffer -> data); break; case log_priority_error: log_error ("%.*s", (int)ds.len, ds.buffer -> data); break; case log_priority_debug: log_debug ("%.*s", (int)ds.len, ds.buffer -> data); break; case log_priority_info: log_info ("%.*s", (int)ds.len, ds.buffer -> data); break; } data_string_forget (&ds, MDL); } break; default: log_error ("bogus statement type %d", r -> op); break; } executable_statement_dereference (&r, MDL); if (next) { executable_statement_reference (&r, next, MDL); executable_statement_dereference (&next, MDL); } } return 1;}/* Execute all the statements in a particular scope, and all statements in scopes outer from that scope, but if a particular limiting scope is reached, do not execute statements in that scope or in scopes outer from it. More specific scopes need to take precedence over less specific scopes, so we recursively traverse the scope list, executing the most outer scope first. */void execute_statements_in_scope (result, packet, lease, client_state, in_options, out_options, scope, group, limiting_group) struct binding_value **result; struct packet *packet; struct lease *lease; struct client_state *client_state; struct option_state *in_options; struct option_state *out_options; struct binding_scope **scope; struct group *group; struct group *limiting_group;{ struct group *limit; /* If we've recursed as far as we can, return. */ if (!group) return; /* As soon as we get to a scope that is outer than the limiting scope, we are done. This is so that if somebody does something like this, it does the expected thing: domain-name "fugue.com"; shared-network FOO { host bar { domain-name "othello.fugue.com"; fixed-address 10.20.30.40; } subnet 10.20.30.0 netmask 255.255.255.0 { domain-name "manhattan.fugue.com"; } } The problem with the above arrangement is that the host's group nesting will be host -> shared-network -> top-level, and the limiting scope when we evaluate the host's scope will be the subnet -> shared-network -> top-level, so we need to know when we evaluate the host's scope to stop before we evaluate the shared-networks scope, because it's outer than the limiting scope, which means we've already evaluated it. */ for (limit = limiting_group; limit; limit = limit -> next) { if (group == limit) return; } if (group -> next) execute_statements_in_scope (result, packet, lease, client_state, in_options, out_options, scope, group -> next, limiting_group); execute_statements (result, packet, lease, client_state, in_options, out_options, scope, group -> statements);}/* Dereference or free any subexpressions of a statement being freed. */int executable_statement_dereference (ptr, file, line) struct executable_statement **ptr; const char *file; int line;{ struct executable_statement *bp;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -