[Bugfix][API-889][wifi] parse hostname length and store mac-hostname pair

Change-Id: I5da1b2debe2a3158d8049581be7948a923f58eb1
diff --git a/meta/meta-mediatek-mt2735/recipes-connectivity/connman/connman/lynq_ap_cache_hostname.patch b/meta/meta-mediatek-mt2735/recipes-connectivity/connman/connman/lynq_ap_cache_hostname.patch
index 8cd4ca2..2c67ba1 100755
--- a/meta/meta-mediatek-mt2735/recipes-connectivity/connman/connman/lynq_ap_cache_hostname.patch
+++ b/meta/meta-mediatek-mt2735/recipes-connectivity/connman/connman/lynq_ap_cache_hostname.patch
@@ -135,35 +135,55 @@
  }
 --- connman-1.41/gdhcp/server.c	2022-01-28 22:48:01.000000000 +0800
 +++ connman-1.41/gdhcp/server_new.c	2022-08-27 15:33:39.326208800 +0800
-@@ -107,13 +107,47 @@
+@@ -107,13 +107,67 @@
  	return NULL;
  }
  
 +static GHashTable *cached_hostname_table = NULL;
-+static int add_cached_hostname(const char * ip, const char * hostname)
++static GHashTable *cached_ip_mac_table = NULL;
++static int add_cached_hostname(const char * mac, const char * hostname)
 +{
-+    if (ip == NULL) {
++    if (mac == NULL) {
 +        return -1;
 +    }
 +    if (cached_hostname_table == NULL) {
 +        cached_hostname_table = g_hash_table_new_full(g_str_hash, g_str_equal,
 +                                NULL, g_free);
 +    }
-+    return g_hash_table_insert(cached_hostname_table, g_strdup(ip), g_strdup(hostname));
++    return g_hash_table_insert(cached_hostname_table, g_strdup(mac), g_strdup(hostname));
 +}
 +
-+static int delete_cached_hostname(const char * ip)
++static int add_cached_ip_mac(const char * ip, const char * mac)
 +{
-+    if (cached_hostname_table != NULL) {
-+        return g_hash_table_remove(cached_hostname_table, ip);
++    if (ip == NULL || mac == NULL) {
++        return -1;
++    }
++    if (cached_ip_mac_table == NULL) {
++        cached_ip_mac_table = g_hash_table_new_full(g_str_hash, g_str_equal,
++                                NULL, g_free);
++    }
++    return g_hash_table_insert(cached_ip_mac_table, g_strdup(ip), g_strdup(mac));
++}
++
++static int delete_cached_ip_mac(const char * ip)
++{
++    if (cached_ip_mac_table != NULL) {
++        return g_hash_table_remove(cached_ip_mac_table, ip);
 +    }
 +    return -1;
 +}
++
 +char * g_dhcp_server_find_hostname(const char * ip) {
-+    if (cached_hostname_table == NULL) {
++    if (ip == NULL || cached_hostname_table == NULL || cached_ip_mac_table == NULL) {
 +        return NULL;
 +    }
-+    char * hostname = g_hash_table_lookup(cached_hostname_table, ip);
++
++	char * mac = g_hash_table_lookup(cached_ip_mac_table, ip);
++	if (mac == NULL) {
++		return NULL;
++	}
++
++    char * hostname = g_hash_table_lookup(cached_hostname_table, mac);
 +    if (hostname != NULL) {
 +        return g_strdup(hostname);
 +    }
@@ -179,32 +199,45 @@
  
  	g_hash_table_remove(dhcp_server->nip_lease_hash,
  				GINT_TO_POINTER((int) lease->lease_nip));
-+    delete_cached_hostname(inet_ntoa(addr));
++    delete_cached_ip_mac(inet_ntoa(addr));
  	g_free(lease);
  }
  
-@@ -653,6 +687,10 @@
+@@ -193,6 +247,7 @@
+ {
+ 	struct dhcp_lease *lease = NULL;
+ 	int ret;
++	struct in_addr addr;
+ 
+ 	ret = get_lease(dhcp_server, yiaddr, chaddr, &lease);
+ 	if (ret != 0)
+@@ -214,6 +269,8 @@
+ 	g_hash_table_insert(dhcp_server->nip_lease_hash,
+ 				GINT_TO_POINTER((int) lease->lease_nip), lease);
+ 
++	addr.s_addr = yiaddr;
++	add_cached_ip_mac(inet_ntoa(addr), chaddr);
+ 	return lease;
+ }
+ 
+@@ -653,6 +710,10 @@
  	uint8_t type, *server_id_option, *request_ip_option;
  	uint16_t packet_len;
  	int re;
 +    const uint8_t *host_name_ptr;
 +    char hostname[128] = {0};
 +    struct in_addr addr;
-+    char *ip = NULL;
++    char *mac=NULL;
  
  	if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
  		dhcp_server->listener_watch = 0;
-@@ -664,7 +702,18 @@
+@@ -664,7 +725,14 @@
  		return TRUE;
  	packet_len = (uint16_t)(unsigned int)re;
  
 +    host_name_ptr = dhcp_get_option(&packet, packet_len, DHCP_HOST_NAME);
 +    if (host_name_ptr != NULL) {
-+        for(int i=0;i<127;i++) {
-+            if( host_name_ptr[i] >= 0x20) {
-+                hostname[i] = host_name_ptr[i];
-+            }
-+        }
++        memcpy(hostname, host_name_ptr, host_name_ptr[-1]);
 +    }
 +
 +
@@ -213,37 +246,27 @@
  	if (type == 0)
  		return TRUE;
  
-@@ -682,12 +731,17 @@
- 		requested_nip = get_be32(request_ip_option);
- 
- 	lease = find_lease_by_mac(dhcp_server, packet.chaddr);
-+    if (lease && lease->lease_nip != 0) {
-+        addr.s_addr = htonl(lease->lease_nip);
-+        ip = inet_ntoa(addr);
-+    }
- 
- 	switch (type) {
+@@ -687,6 +755,7 @@
  	case DHCPDISCOVER:
  		debug(dhcp_server, "Received DISCOVER");
  
++        add_cached_hostname(packet.chaddr, hostname);
  		send_offer(dhcp_server, &packet, lease, requested_nip);
-+        add_cached_hostname(ip, hostname);
  		break;
  	case DHCPREQUEST:
- 		debug(dhcp_server, "Received REQUEST NIP %d",
-@@ -702,6 +756,7 @@
+@@ -700,6 +769,7 @@
+ 
+ 		if (lease && requested_nip == lease->lease_nip) {
  			debug(dhcp_server, "Sending ACK");
++            add_cached_hostname(packet.chaddr, hostname);
  			send_ACK(dhcp_server, &packet,
  				lease->lease_nip);
-+            add_cached_hostname(ip, hostname);
  			break;
- 		}
- 
-@@ -743,6 +798,7 @@
+@@ -743,6 +813,7 @@
  	case DHCPINFORM:
  		debug(dhcp_server, "Received INFORM");
  		send_inform(dhcp_server, &packet);
-+        add_cached_hostname(ip, hostname);
++        add_cached_hostname(packet.chaddr, hostname);
  		break;
  	}