session: lookup listener with iface address

We add interface address to the global lookup table, so we should
use it as the key when lookup listener. Otherwise, when multiple
threads listen on 0.0.0.0 (local scope disable), duplicate
listeners and sessions would be allocated but only one works.

Type: fix

Signed-off-by: liuyacan <liuyacan@corp.netease.com>
Change-Id: I86f36475c16e217c6c5293a62c4fb5c9477a191e
diff --git a/src/vnet/session/application.c b/src/vnet/session/application.c
index 56a5141..2f6bfa9 100644
--- a/src/vnet/session/application.c
+++ b/src/vnet/session/application.c
@@ -101,6 +101,8 @@
   session_endpoint_t *sep;
   session_handle_t handle;
   session_t *ls;
+  void *iface_ip;
+  ip46_address_t original_ip;
 
   sep = (session_endpoint_t *) sep_ext;
   if (application_has_local_scope (app) && session_endpoint_is_local (sep))
@@ -123,6 +125,30 @@
       return app_listener_get_w_session ((session_t *) ls);
     }
 
+  /*
+   * When binds to "inaddr_any", we add zero address in the local lookup table
+   * and interface address in the global lookup table. If local scope disable,
+   * the latter is the only clue to find the listener.
+   */
+  if (!application_has_local_scope (app) &&
+      ip_is_zero (&sep_ext->ip, sep_ext->is_ip4) &&
+      sep_ext->sw_if_index != ENDPOINT_INVALID_INDEX)
+    {
+      if ((iface_ip = ip_interface_get_first_ip (sep_ext->sw_if_index,
+						 sep_ext->is_ip4)))
+	{
+	  ip_copy (&original_ip, &sep_ext->ip, sep_ext->is_ip4);
+	  ip_set (&sep_ext->ip, iface_ip, sep_ext->is_ip4);
+	  handle = session_lookup_endpoint_listener (table_index, sep, 1);
+	  ip_copy (&sep_ext->ip, &original_ip, sep_ext->is_ip4);
+	  if (handle != SESSION_INVALID_HANDLE)
+	    {
+	      ls = listen_session_get_from_handle (handle);
+	      return app_listener_get_w_session ((session_t *) ls);
+	    }
+	}
+    }
+
   return 0;
 }