[PATCH] Class matching based on interface name
Oskar Berggren
oskar.berggren at gmail.com
Fri Aug 14 13:47:34 UTC 2009
In a project I'm currently working on we had a need to select the
available ip-addresses based on what network interface the request
came from. For this purpose I implemented the following patch that
adds the keyword "interface-name" that can be used in class
conditions. During evaluation the keyword will return for instance
"eth0" or "eth4.340".
Is there any interest in applying this patch to the official
distribution? If there is any specific reason not to include it,
please elaborate on why not.
/Oskar
diff -Nru dhcp3-3.1.1-original/common/conflex.c dhcp3-3.1.1/common/conflex.c
--- dhcp3-3.1.1-original/common/conflex.c 2009-05-25 19:39:19.000000000 +0200
+++ dhcp3-3.1.1/common/conflex.c 2009-05-25 20:41:22.000000000 +0200
@@ -796,6 +796,8 @@
return INITIAL_INTERVAL;
if (!strcasecmp (atom + 1, "nterface"))
return INTERFACE;
+ if (!strcasecmp (atom + 1, "nterface-name"))
+ return INTERFACE_NAME;
if (!strcasecmp (atom + 1, "dentifier"))
return IDENTIFIER;
if (!strcasecmp (atom + 1, "f"))
diff -Nru dhcp3-3.1.1-original/common/parse.c dhcp3-3.1.1/common/parse.c
--- dhcp3-3.1.1-original/common/parse.c 2008-01-22 20:02:50.000000000 +0100
+++ dhcp3-3.1.1/common/parse.c 2009-05-25 20:02:17.000000000 +0200
@@ -3098,6 +3098,7 @@
* UCASE LPAREN data_expression RPAREN |
* OPTION option_name |
* HARDWARE |
+ * INTERFACE_NAME |
* PACKET LPAREN numeric-expression COMMA
* numeric-expression RPAREN |
* STRING |
@@ -3902,6 +3903,13 @@
log_fatal ("can't allocate expression");
(*expr) -> op = expr_hardware;
break;
+
+ case INTERFACE_NAME:
+ token = next_token (&val, (unsigned *)0, cfile);
+ if (!expression_allocate (expr, MDL))
+ log_fatal ("can't allocate expression");
+ (*expr) -> op = expr_interface_name;
+ break;
case LEASED_ADDRESS:
token = next_token (&val, (unsigned *)0, cfile);
diff -Nru dhcp3-3.1.1-original/common/tree.c dhcp3-3.1.1/common/tree.c
--- dhcp3-3.1.1-original/common/tree.c 2007-02-14 23:41:22.000000000 +0100
+++ dhcp3-3.1.1/common/tree.c 2009-05-25 20:55:17.000000000 +0200
@@ -895,6 +895,7 @@
case expr_ucase:
case expr_option:
case expr_hardware:
+ case expr_interface_name:
case expr_const_data:
case expr_packet:
case expr_concat:
@@ -991,6 +992,9 @@
client_state, in_options,
cfg_options, scope,
expr -> data.equal [1], MDL);
+
+ log_debug ("e_b_e: type %d %d", bv -> type, obv -> type);
+
if (sleft && sright) {
if (bv -> type != obv -> type)
*result = expr -> op == expr_not_equal;
@@ -1004,6 +1008,17 @@
break;
case binding_data:
+ log_debug ("e_b_e: len %d %d",
+ bv -> value.data.len,
+ obv -> value.data.len);
+ log_debug ("e_b_e: data bv '%s'",
+ print_hex_1(bv -> value.data.len,
+ bv -> value.data.data, 60)
+ );
+ log_debug ("e_b_e: data obv '%s'",
+ print_hex_1(obv -> value.data.len,
+ obv -> value.data.data, 60)
+ );
if ((bv -> value.data.len ==
obv -> value.data.len) &&
!memcmp (bv -> value.data.data,
@@ -1260,6 +1275,7 @@
case expr_ucase:
case expr_option:
case expr_hardware:
+ case expr_interface_name:
case expr_const_data:
case expr_packet:
case expr_concat:
@@ -1592,6 +1608,34 @@
#endif
return 1;
+
+ case expr_interface_name:
+ if (!packet) {
+ log_error ("data: inteface-name: packet not available");
+ return 0;
+ }
+
+ memset(result, 0, sizeof *result);
+ result -> len = strlen(packet -> interface -> name);
+ if (buffer_allocate (&result -> buffer,
+ result -> len + 1, file, line)) {
+ result -> data = &result -> buffer -> data [0];
+ strncpy(result -> buffer -> data,
+ packet -> interface -> name,
+ result -> len + 1);
+ result -> terminated = 1;
+ } else {
+ log_error ("data: interface-name: no buffer memory.");
+ return 0;
+ }
+
+#if defined (DEBUG_EXPRESSIONS)
+ log_debug ("data: interface-name = '%s'",
+ result -> data);
+#endif
+
+ return 1;
+
/* Extract part of the raw packet. */
case expr_packet:
if (!packet || !packet -> raw) {
@@ -3135,6 +3179,7 @@
expr->op == expr_ucase ||
expr->op == expr_option ||
expr->op == expr_hardware ||
+ expr->op == expr_interface_name ||
expr->op == expr_const_data ||
expr->op == expr_packet ||
expr->op == expr_concat ||
@@ -3218,6 +3263,7 @@
case expr_not:
case expr_option:
case expr_hardware:
+ case expr_interface_name:
case expr_packet:
case expr_const_data:
case expr_extract_int8:
@@ -3316,6 +3362,7 @@
case expr_not:
case expr_option:
case expr_hardware:
+ case expr_interface_name:
case expr_packet:
case expr_const_data:
case expr_extract_int8:
diff -Nru dhcp3-3.1.1-original/includes/dhctoken.h
dhcp3-3.1.1/includes/dhctoken.h
--- dhcp3-3.1.1-original/includes/dhctoken.h 2006-08-01 00:19:51.000000000 +0200
+++ dhcp3-3.1.1/includes/dhctoken.h 2009-05-25 19:55:29.000000000 +0200
@@ -325,7 +325,8 @@
MIN_BALANCE = 629,
DOMAIN_LIST = 630,
LEASEQUERY = 631,
- EXECUTE = 632
+ EXECUTE = 632,
+ INTERFACE_NAME = 633
};
#define is_identifier(x) ((x) >= FIRST_TOKEN && \
diff -Nru dhcp3-3.1.1-original/includes/tree.h dhcp3-3.1.1/includes/tree.h
--- dhcp3-3.1.1-original/includes/tree.h 2008-01-22 20:02:51.000000000 +0100
+++ dhcp3-3.1.1/includes/tree.h 2009-05-25 20:05:28.000000000 +0200
@@ -148,6 +148,7 @@
expr_not,
expr_option,
expr_hardware,
+ expr_interface_name,
expr_packet,
expr_const_data,
expr_extract_int8,
More information about the dhcp-hackers
mailing list