+static clib_error_t *
+application_start_stop_proxy_fib_proto (application_t * app, u8 fib_proto,
+ u8 transport_proto, u8 is_start)
+{
+ app_namespace_t *app_ns = app_namespace_get (app->ns_index);
+ u8 is_ip4 = (fib_proto == FIB_PROTOCOL_IP4);
+ session_endpoint_t sep = SESSION_ENDPOINT_NULL;
+ transport_connection_t *tc;
+ stream_session_t *s;
+ u64 handle;
+
+ if (is_start)
+ {
+ sep.is_ip4 = is_ip4;
+ sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
+ sep.sw_if_index = app_ns->sw_if_index;
+ sep.transport_proto = transport_proto;
+ application_start_listen (app, &sep, &handle);
+ s = listen_session_get_from_handle (handle);
+ s->listener_index = SESSION_PROXY_LISTENER_INDEX;
+ }
+ else
+ {
+ s = application_first_listener (app, fib_proto, transport_proto);
+ }
+ tc = listen_session_get_transport (s);
+
+ if (!ip_is_zero (&tc->lcl_ip, 1))
+ {
+ u32 sti;
+ sep.is_ip4 = is_ip4;
+ sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
+ sep.transport_proto = transport_proto;
+ sep.port = 0;
+ sti = session_lookup_get_index_for_fib (fib_proto, sep.fib_index);
+ session_lookup_add_session_endpoint (sti, &sep, s->session_index);
+ }
+ return 0;
+}
+
+void
+application_start_stop_proxy (application_t * app, u8 transport_proto,
+ u8 is_start)
+{
+ if (application_has_local_scope (app))
+ {
+ session_endpoint_t sep = SESSION_ENDPOINT_NULL;
+ app_namespace_t *app_ns;
+ app_ns = app_namespace_get (app->ns_index);
+ sep.is_ip4 = 1;
+ sep.transport_proto = transport_proto;
+ sep.port = 0;
+ session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
+ app->index);
+
+ sep.is_ip4 = 0;
+ session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
+ app->index);
+ }
+
+ if (application_has_global_scope (app))
+ {
+ application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP4,
+ transport_proto, is_start);
+ application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP6,
+ transport_proto, is_start);
+ }
+}
+
+void
+application_setup_proxy (application_t * app)
+{
+ u16 transports = app->proxied_transports;
+ ASSERT (application_is_proxy (app));
+ if (application_is_builtin (app))
+ return;
+ if (transports & (1 << TRANSPORT_PROTO_TCP))
+ application_start_stop_proxy (app, TRANSPORT_PROTO_TCP, 1);
+ if (transports & (1 << TRANSPORT_PROTO_UDP))
+ application_start_stop_proxy (app, TRANSPORT_PROTO_UDP, 1);
+}
+
+void
+application_remove_proxy (application_t * app)
+{
+ u16 transports = app->proxied_transports;
+ ASSERT (application_is_proxy (app));
+ if (transports & (1 << TRANSPORT_PROTO_TCP))
+ application_start_stop_proxy (app, TRANSPORT_PROTO_TCP, 0);
+ if (transports & (1 << TRANSPORT_PROTO_UDP))
+ application_start_stop_proxy (app, TRANSPORT_PROTO_UDP, 0);
+}
+