-static u8
-ip_is_zero (ip46_address_t * ip46_address, u8 is_ip4)
-{
- if (is_ip4)
- return (ip46_address->ip4.as_u32 == 0);
- else
- return (ip46_address->as_u64[0] == 0 && ip46_address->as_u64[1] == 0);
-}
-
-static u8
-ip_is_local (ip46_address_t * ip46_address, u8 is_ip4)
-{
- fib_node_index_t fei;
- fib_entry_flag_t flags;
- fib_prefix_t prefix;
-
- /* Check if requester is local */
- if (is_ip4)
- {
- prefix.fp_len = 32;
- prefix.fp_proto = FIB_PROTOCOL_IP4;
- }
- else
- {
- prefix.fp_len = 128;
- prefix.fp_proto = FIB_PROTOCOL_IP6;
- }
-
- clib_memcpy (&prefix.fp_addr, ip46_address, sizeof (ip46_address_t));
- fei = fib_table_lookup (0, &prefix);
- flags = fib_entry_get_flags (fei);
-
- return (flags & FIB_ENTRY_FLAG_LOCAL);
-}
-
-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;
-}
-
-int
-vnet_bind_i (u32 app_index, session_type_t sst,
- transport_endpoint_t * tep, u64 * handle)
-{
- application_t *app;
- stream_session_t *listener;
-
- app = application_get_if_valid (app_index);
- if (!app)
- {
- clib_warning ("app not attached");
- return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
- }
-
- listener = stream_session_lookup_listener (&tep->ip,
- clib_host_to_net_u16 (tep->port),
- sst);
- if (listener)
- return VNET_API_ERROR_ADDRESS_IN_USE;
-
- if (!ip_is_zero (&tep->ip, tep->is_ip4)
- && !ip_is_local (&tep->ip, tep->is_ip4))
- return VNET_API_ERROR_INVALID_VALUE_2;
-
- /* Setup listen path down to transport */
- return application_start_listen (app, sst, tep, handle);
-}
-
-int
-vnet_unbind_i (u32 app_index, u64 handle)
-{
- application_t *app = application_get_if_valid (app_index);
-
- if (!app)
- {
- clib_warning ("app not attached");
- return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
- }
-
- /* Clear the listener */
- return application_stop_listen (app, handle);
-}
-
-int
-vnet_connect_i (u32 app_index, u32 api_context, session_type_t sst,
- transport_endpoint_t * tep, void *mp)
-{
- stream_session_t *listener;
- application_t *server, *app;
-
- /*
- * Figure out if connecting to a local server
- */
- listener = stream_session_lookup_listener (&tep->ip,
- clib_host_to_net_u16 (tep->port),
- sst);
- if (listener)
- {
- server = application_get (listener->app_index);
-
- /*
- * Server is willing to have a direct fifo connection created
- * instead of going through the state machine, etc.
- */
- if (server->flags & SESSION_OPTIONS_FLAGS_USE_FIFO)
- return server->cb_fns.
- redirect_connect_callback (server->api_client_index, mp);
- }
-
- /*
- * Not connecting to a local server. Create regular session
- */
- app = application_get (app_index);
- return application_open_session (app, sst, tep, api_context);
-}
-