-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));
- 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 api_client_index, ip46_address_t * ip46, u16 port_host_order,
- session_type_t sst, u64 * options, session_cb_vft_t * cb_fns,
- application_t ** app, u32 * len_seg_name, char *seg_name)
-{
- u8 *segment_name = 0;
- application_t *server = 0;
- stream_session_t *listener;
- u8 is_ip4;
-
- listener =
- stream_session_lookup_listener (ip46,
- clib_host_to_net_u16 (port_host_order),
- sst);
-
- if (listener)
- return VNET_API_ERROR_ADDRESS_IN_USE;
-
- if (application_lookup (api_client_index))
- {
- clib_warning ("Only one bind supported for now");
- return VNET_API_ERROR_ADDRESS_IN_USE;
- }
-
- is_ip4 = SESSION_TYPE_IP4_UDP == sst || SESSION_TYPE_IP4_TCP == sst;
- if (!ip_is_zero (ip46, is_ip4) && !ip_is_local (ip46, is_ip4))
- return VNET_API_ERROR_INVALID_VALUE;
-
- /* Allocate and initialize stream server */
- server = application_new (APP_SERVER, sst, api_client_index,
- options[SESSION_OPTIONS_FLAGS], cb_fns);
-
- application_server_init (server, options[SESSION_OPTIONS_SEGMENT_SIZE],
- options[SESSION_OPTIONS_ADD_SEGMENT_SIZE],
- options[SESSION_OPTIONS_RX_FIFO_SIZE],
- options[SESSION_OPTIONS_TX_FIFO_SIZE],
- &segment_name);
-
- /* Setup listen path down to transport */
- stream_session_start_listen (server->index, ip46, port_host_order);
-
- /*
- * Return values
- */
-
- ASSERT (vec_len (segment_name) <= 128);
- *len_seg_name = vec_len (segment_name);
- memcpy (seg_name, segment_name, *len_seg_name);
- *app = server;
-
- return 0;
-}
-
-int
-vnet_unbind_i (u32 api_client_index)
-{
- application_t *server;
-
- /*
- * Find the stream_server_t corresponding to the api client
- */
- server = application_lookup (api_client_index);
- if (!server)
- return VNET_API_ERROR_INVALID_VALUE_2;
-
- /* Clear the listener */
- stream_session_stop_listen (server->index);
- application_del (server);
-
- return 0;
-}
-
-int
-vnet_connect_i (u32 api_client_index, u32 api_context, session_type_t sst,
- ip46_address_t * ip46, u16 port, u64 * options, void *mp,
- session_cb_vft_t * cb_fns)
-{
- stream_session_t *listener;
- application_t *server, *app;
-
- /*
- * Figure out if connecting to a local server
- */
- listener = stream_session_lookup_listener (ip46,
- clib_host_to_net_u16 (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);
- }
-
- /* Create client app */
- app = application_new (APP_CLIENT, sst, api_client_index,
- options[SESSION_OPTIONS_FLAGS], cb_fns);
-
- app->api_context = api_context;
-
- /*
- * Not connecting to a local server. Create regular session
- */
- stream_session_open (sst, ip46, port, app->index);
-
- return 0;
-}
-