[PATCH 1/6] dhcrelay: Refactor DHCPv6 interface-id adding code

Bjørn Mork bjorn at mork.no
Sun Sep 25 12:59:44 UTC 2011


simplifying a bit with a common error path, in preparation for
adding more relay options.

Signed-off-by: Bjørn Mork <bjorn at mork.no>
---
 relay/dhcrelay.c |   43 +++++++++++++++++--------------------------
 1 files changed, 17 insertions(+), 26 deletions(-)

diff --git a/relay/dhcrelay.c b/relay/dhcrelay.c
index f21f16f..1c8c522 100644
--- a/relay/dhcrelay.c
+++ b/relay/dhcrelay.c
@@ -1414,40 +1414,31 @@ process_up6(struct packet *packet, struct stream_list *dp) {
 	if (!option_state_allocate(&opts, MDL)) {
 		log_fatal("No memory for upwards options.");
 	}
-	
-	/* Add an interface-id (if used). */
-	if (use_if_id) {
-		int if_id;
 
-		if (dp) {
-			if_id = dp->id;
-		} else if (!downstreams->next) {
-			if_id = downstreams->id;
-		} else {
-			log_info("Don't know the interface.");
-			option_state_dereference(&opts, MDL);
-			return;
-		}
+	/* we need a specific interface for relay options */
+	if (!dp && !downstreams->next) /* only one downstream? */
+		dp = downstreams;
 
-		if (!save_option_buffer(&dhcpv6_universe, opts,
-					NULL, (unsigned char *) &if_id,
-					sizeof(int),
-					D6O_INTERFACE_ID, 0)) {
-			log_error("Can't save interface-id.");
-			option_state_dereference(&opts, MDL);
-			return;
-		}
+	if (!dp && use_if_id) {
+opt_error:
+		log_info("Can't save option.");
+		option_state_dereference(&opts, MDL);
+		return;
 	}
 
+	/* Add an interface-id (if used). */
+	if (use_if_id && !save_option_buffer(&dhcpv6_universe, opts,
+					NULL, (unsigned char *) &dp->id,
+					sizeof(int),
+					D6O_INTERFACE_ID, 0))
+		goto opt_error;
+
 	/* Add the relay-msg carrying the packet. */
 	if (!save_option_buffer(&dhcpv6_universe, opts,
 				NULL, (unsigned char *) packet->raw,
 				packet->packet_length,
-				D6O_RELAY_MSG, 0)) {
-		log_error("Can't save relay-msg.");
-		option_state_dereference(&opts, MDL);
-		return;
-	}
+				D6O_RELAY_MSG, 0))
+		goto opt_error;
 
 	/* Finish the relay-forward message. */
 	cursor += store_options6(forw_data + cursor,
-- 
1.7.2.5




More information about the dhcp-users mailing list