-static u8
-session_endpoint_is_local (session_endpoint_t * sep)
-{
- return (ip_is_zero (&sep->ip, sep->is_ip4)
- || ip_is_local_host (&sep->ip, sep->is_ip4));
-}
-
-static u8
-session_endpoint_is_zero (session_endpoint_t * sep)
-{
- return ip_is_zero (&sep->ip, sep->is_ip4);
-}
-
-u8
-session_endpoint_in_ns (session_endpoint_t * sep)
-{
- u8 is_zero = ip_is_zero (&sep->ip, sep->is_ip4);
- if (!is_zero && sep->sw_if_index != ENDPOINT_INVALID_INDEX
- && !ip_interface_has_address (sep->sw_if_index, &sep->ip, sep->is_ip4))
- {
- clib_warning ("sw_if_index %u not configured with ip %U",
- sep->sw_if_index, format_ip46_address, &sep->ip,
- sep->is_ip4);
- return 0;
- }
- return (is_zero || ip_is_local (sep->fib_index, &sep->ip, sep->is_ip4));
-}
-
-int
-api_parse_session_handle (u64 handle, u32 * session_index, u32 * thread_index)
-{
- session_manager_main_t *smm = vnet_get_session_manager_main ();
- stream_session_t *pool;
-
- *thread_index = handle & 0xFFFFFFFF;
- *session_index = handle >> 32;
-
- if (*thread_index >= vec_len (smm->sessions))
- return VNET_API_ERROR_INVALID_VALUE;
-
- pool = smm->sessions[*thread_index];
-
- if (pool_is_free_index (pool, *session_index))
- return VNET_API_ERROR_INVALID_VALUE_2;
-
- return 0;
-}
-
-static void
-session_endpoint_update_for_app (session_endpoint_t * sep,
- application_t * app)
-{
- app_namespace_t *app_ns;
- app_ns = app_namespace_get (app->ns_index);
- if (app_ns)
- {
- /* Ask transport and network to bind to/connect using local interface
- * that "supports" app's namespace. This will fix our local connection
- * endpoint.
- */
- sep->sw_if_index = app_ns->sw_if_index;
- sep->fib_index =
- sep->is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
- }
-}
-
-static int
-vnet_bind_i (u32 app_index, session_endpoint_t * sep, u64 * handle)
-{
- application_t *app;
- u32 table_index, listener_index;
- int rv, have_local = 0;
-
- app = application_get_if_valid (app_index);
- if (!app)
- {
- SESSION_DBG ("app not attached");
- return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
- }
-
- session_endpoint_update_for_app (sep, app);
- if (!session_endpoint_in_ns (sep))
- return VNET_API_ERROR_INVALID_VALUE_2;
-
- table_index = application_session_table (app,
- session_endpoint_fib_proto (sep));
- listener_index = session_lookup_session_endpoint (table_index, sep);
- if (listener_index != SESSION_INVALID_INDEX)
- return VNET_API_ERROR_ADDRESS_IN_USE;
-
- /*
- * Add session endpoint to local session table. Only binds to "inaddr_any"
- * (i.e., zero address) are added to local scope table.
- */
- if (application_has_local_scope (app) && session_endpoint_is_zero (sep))
- {
- table_index = application_local_session_table (app);
- listener_index = session_lookup_session_endpoint (table_index, sep);
- if (listener_index != SESSION_INVALID_INDEX)
- return VNET_API_ERROR_ADDRESS_IN_USE;
- session_lookup_add_session_endpoint (table_index, sep, app->index);
- *handle = session_lookup_local_listener_make_handle (sep);
- have_local = 1;
- }
-
- if (!application_has_global_scope (app))
- return (have_local - 1);
-
- /*
- * Add session endpoint to global session table
- */
-
- /* Setup listen path down to transport */
- rv = application_start_listen (app, sep, handle);
- if (rv && have_local)
- session_lookup_del_session_endpoint (table_index, sep);
- return rv;
-}
-
-int
-vnet_unbind_i (u32 app_index, u64 handle)
-{
- application_t *app = application_get_if_valid (app_index);
- stream_session_t *listener = 0;
- u32 table_index;
-
- if (!app)
- {
- SESSION_DBG ("app (%d) not attached", app_index);
- return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
- }
-
- /*
- * Clean up local session table. If we have a listener session use it to
- * find the port and proto. If not, the handle must be a local table handle
- * so parse it.
- */
-
- if (application_has_local_scope (app))
- {
- session_endpoint_t sep = SESSION_ENDPOINT_NULL;
- if (!session_lookup_local_is_handle (handle))
- listener = listen_session_get_from_handle (handle);
- if (listener)
- {
- if (listen_session_get_local_session_endpoint (listener, &sep))
- {
- clib_warning ("broken listener");
- return -1;
- }
- }
- else
- {
- if (session_lookup_local_listener_parse_handle (handle, &sep))
- {
- clib_warning ("can't parse handle");
- return -1;
- }
- }
- table_index = application_local_session_table (app);
- session_lookup_del_session_endpoint (table_index, &sep);
- }
-
- /*
- * Clear the global scope table of the listener
- */
- if (application_has_global_scope (app))
- return application_stop_listen (app, handle);
- return 0;
-}
-
-static int
-app_connect_redirect (application_t * server, void *mp)
-{
- return server->cb_fns.redirect_connect_callback (server->api_client_index,
- mp);
-}
-
-int
-vnet_connect_i (u32 app_index, u32 api_context, session_endpoint_t * sep,
- void *mp)
-{
- application_t *server, *app;
- u32 table_index;
-
- if (session_endpoint_is_zero (sep))
- return VNET_API_ERROR_INVALID_VALUE;
-
- app = application_get (app_index);
- session_endpoint_update_for_app (sep, app);
-
- /*
- * First check the the local scope for locally attached destinations.
- * If we have local scope, we pass *all* connects through it since we may
- * have special policy rules even for non-local destinations, think proxy.
- */
- if (application_has_local_scope (app))
- {
- table_index = application_local_session_table (app);
- app_index = session_lookup_local_session_endpoint (table_index, sep);
- server = application_get (app_index);
- /*
- * Server is willing to have a direct fifo connection created
- * instead of going through the state machine, etc.
- */
- if (server && (server->flags & APP_OPTIONS_FLAGS_ACCEPT_REDIRECT))
- return app_connect_redirect (server, mp);
- }
-
- /*
- * If nothing found, check the global scope for locally attached
- * destinations. Make sure first that we're allowed to.
- */
- if (session_endpoint_is_local (sep))
- return VNET_API_ERROR_SESSION_CONNECT;
-
- if (!application_has_global_scope (app))
- return VNET_API_ERROR_APP_CONNECT_SCOPE;
-
- table_index = application_session_table (app,
- session_endpoint_fib_proto (sep));
- app_index = session_lookup_session_endpoint (table_index, sep);
- server = application_get (app_index);
- if (server && (server->flags & APP_OPTIONS_FLAGS_ACCEPT_REDIRECT))
- return app_connect_redirect (server, mp);
-
- /*
- * Not connecting to a local server, propagate to transport
- */
- if (application_open_session (app, sep, api_context))
- return VNET_API_ERROR_SESSION_CONNECT;
- return 0;
-}
-