Browse Source

[dhcp] Centralise DHCP successful state transitions

Move all the DHCP state transition logic into a single function
dhcp_next_state().  This will make it easier to add support for PXE
Boot Servers, since it abstracts away the difference between "mark
DHCP as complete" and "transition to boot server discovery".
tags/v0.9.7
Michael Brown 16 years ago
parent
commit
7be5fa82e3
1 changed files with 35 additions and 19 deletions
  1. 35
    19
      src/net/udp/dhcp.c

+ 35
- 19
src/net/udp/dhcp.c View File

640
 	       dhcp, dhcp_state_name ( state ) );
640
 	       dhcp, dhcp_state_name ( state ) );
641
 	dhcp->state = state;
641
 	dhcp->state = state;
642
 	dhcp->start = currticks();
642
 	dhcp->start = currticks();
643
+	dhcp->timer.min_timeout = 0;
643
 	start_timer_nodelay ( &dhcp->timer );
644
 	start_timer_nodelay ( &dhcp->timer );
644
 }
645
 }
645
 
646
 
647
+/**
648
+ * Transition to next DHCP state
649
+ *
650
+ * @v dhcp		DHCP session
651
+ */
652
+static void dhcp_next_state ( struct dhcp_session *dhcp ) {
653
+
654
+	switch ( dhcp->state ) {
655
+	case DHCP_STATE_DISCOVER:
656
+		dhcp_set_state ( dhcp, DHCP_STATE_REQUEST );
657
+		break;
658
+	case DHCP_STATE_REQUEST:
659
+		if ( dhcp->proxydhcpoffer ) {
660
+			dhcp_set_state ( dhcp, DHCP_STATE_PROXYREQUEST );
661
+			break;
662
+		}
663
+		/* Fall through */
664
+	case DHCP_STATE_PROXYREQUEST:
665
+		dhcp_finished ( dhcp, 0 );
666
+		break;
667
+	default:
668
+		assert ( 0 );
669
+		return;
670
+	}
671
+
672
+}
673
+
646
 /**
674
 /**
647
  * Store received DHCPOFFER
675
  * Store received DHCPOFFER
648
  *
676
  *
759
 		return;
787
 		return;
760
 
788
 
761
 	/* Transition to DHCPREQUEST */
789
 	/* Transition to DHCPREQUEST */
762
-	dhcp_set_state ( dhcp, DHCP_STATE_REQUEST );
790
+	dhcp_next_state ( dhcp );
763
 }
791
 }
764
 
792
 
765
 /**
793
 /**
822
 	if ( ( rc = dhcp_store_dhcpack ( dhcp, dhcpack, parent ) ) !=0 )
850
 	if ( ( rc = dhcp_store_dhcpack ( dhcp, dhcpack, parent ) ) !=0 )
823
 		return;
851
 		return;
824
 
852
 
825
-	/* If we have a ProxyDHCPOFFER, transition to PROXYDHCPREQUEST */
826
-	if ( dhcp->proxydhcpoffer ) {
827
-		dhcp->timer.min_timeout = 0;
828
-		dhcp_set_state ( dhcp, DHCP_STATE_PROXYREQUEST );
829
-		return;
830
-	}
831
-
832
-	/* Terminate DHCP */
833
-	dhcp_finished ( dhcp, 0 );
853
+	/* Transition to next state */
854
+	dhcp_next_state ( dhcp );
834
 }
855
 }
835
 
856
 
836
 /**
857
 /**
868
 	if ( ( rc = dhcp_store_dhcpack ( dhcp, proxydhcpack, NULL ) ) != 0 )
889
 	if ( ( rc = dhcp_store_dhcpack ( dhcp, proxydhcpack, NULL ) ) != 0 )
869
 		return;
890
 		return;
870
 
891
 
871
-	/* Terminate DHCP */
872
-	dhcp_finished ( dhcp, 0 );
892
+	/* Transition to next state */
893
+	dhcp_next_state ( dhcp );
873
 }
894
 }
874
 
895
 
875
 /**
896
 /**
991
 
1012
 
992
 	/* Give up waiting for ProxyDHCP before we reach the failure point */
1013
 	/* Give up waiting for ProxyDHCP before we reach the failure point */
993
 	if ( dhcp->dhcpoffer && ( elapsed > PROXYDHCP_WAIT_TIME ) ) {
1014
 	if ( dhcp->dhcpoffer && ( elapsed > PROXYDHCP_WAIT_TIME ) ) {
994
-		if ( dhcp->state == DHCP_STATE_DISCOVER ) {
995
-			dhcp_set_state ( dhcp, DHCP_STATE_REQUEST );
996
-			return;
997
-		} else if ( dhcp->state == DHCP_STATE_PROXYREQUEST ) {
998
-			dhcp_finished ( dhcp, 0 );
999
-			return;
1000
-		}
1015
+		dhcp_next_state ( dhcp );
1016
+		return;
1001
 	}
1017
 	}
1002
 
1018
 
1003
 	/* Otherwise, retransmit current packet */
1019
 	/* Otherwise, retransmit current packet */

Loading…
Cancel
Save