Run DHCP internally on P2P Group Formation, if enabled [-g]

Try to run DHCP Client/Server on group formation. This option can only
be enabled while starting sigma_dut with "-g" option. If no such option
is provided, external application need to start DHCP process(es).

A separate thread is started which keeps monitoring for
P2P-GROUP-STARTED and P2P-GROUP-REMOVED events and start DHCP
client/server based on P2P Client/GO role.

Signed-off-by: Jouni Malinen <jouni@qca.qualcomm.com>
diff --git a/sigma_dut.c b/sigma_dut.c
index 574fc9f..637945c 100644
--- a/sigma_dut.c
+++ b/sigma_dut.c
@@ -467,6 +467,7 @@
 static void handle_term(int sig)
 {
 	stop_loop = 1;
+	stop_event_thread();
 	printf("sigma_dut terminating\n");
 }
 #endif /* __linux__ */
@@ -656,13 +657,13 @@
 }
 
 
-static void determine_sigma_p2p_ifname(void)
+static char * determine_sigma_p2p_ifname(void)
 {
 	char buf[256];
 	struct wpa_ctrl *ctrl;
 
 	if (sigma_p2p_ifname)
-		return;
+		return sigma_p2p_ifname;
 
 	snprintf(buf, sizeof(buf), "p2p-dev-%s", get_station_ifname());
 	ctrl = open_wpa_mon(buf);
@@ -678,6 +679,8 @@
 	} else {
 		sigma_p2p_ifname = get_station_ifname();
 	}
+
+	return sigma_p2p_ifname;
 }
 
 
@@ -745,6 +748,7 @@
 	int daemonize = 0;
 	int port = SIGMA_DUT_PORT;
 	char *local_cmd = NULL;
+	int internal_dhcp_enabled = 0;
 #ifdef __QNXNTO__
 	char *env_str = NULL;
 	char buf[20];
@@ -759,7 +763,7 @@
 
 	for (;;) {
 		c = getopt(argc, argv,
-			   "aAb:Bc:C:dDE:e:fhH:i:Ik:l:L:m:M:nN:o:O:p:P:qr:R:s:S:tT:uv:VWw:");
+			   "aAb:Bc:C:dDE:e:fghH:i:Ik:l:L:m:M:nN:o:O:p:P:qr:R:s:S:tT:uv:VWw:");
 		if (c < 0)
 			break;
 		switch (c) {
@@ -794,6 +798,11 @@
 			/* Disable writing stats */
 			sigma_dut.write_stats = 0;
 			break;
+		case 'g':
+			/* Enable internal processing of P2P group formation
+			 * events to start/stop DHCP server/client. */
+			internal_dhcp_enabled = 1;
+			break;
 		case 'H':
 			sigma_dut.hostapd_debug_log = optarg;
 			break;
@@ -939,7 +948,7 @@
 		}
 	}
 
-	determine_sigma_p2p_ifname();
+	sigma_dut.p2p_ifname = determine_sigma_p2p_ifname();
 	if (local_cmd)
 		return run_local_cmd(port, local_cmd);
 
@@ -999,6 +1008,9 @@
 #endif /* __linux__ */
 	}
 
+	if (internal_dhcp_enabled)
+		p2p_create_event_thread(&sigma_dut);
+
 	run_loop(&sigma_dut);
 
 #ifdef CONFIG_SNIFFER