+/*
+ * Find the gateway that is serving the VPN link
+ */
+static struct gateway_data *find_phy_gateway(int index, const char *gateway)
+{
+ GHashTableIter iter;
+ gpointer value, key;
+
+ if (gateway == NULL)
+ return NULL;
+
+ g_hash_table_iter_init(&iter, gateway_hash);
+
+ while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
+ struct gateway_data *data = value;
+
+ if (data->ipv4_gateway != NULL && data->index != index &&
+ g_str_equal(data->ipv4_gateway->gateway,
+ gateway) == TRUE)
+ return data;
+
+ if (data->ipv6_gateway != NULL && data->index != index &&
+ g_str_equal(data->ipv6_gateway->gateway,
+ gateway) == TRUE)
+ return data;
+ }
+
+ return NULL;
+}
+
+static void set_vpn_routes(struct gateway_config *config,
+ struct connman_service *service,
+ const char *gateway,
+ enum connman_ipconfig_type type,
+ const char *peer)
+{
+ struct gateway_data *data;
+ struct connman_ipconfig *ipconfig;
+ int index;
+
+ config->vpn = TRUE;
+ if (peer != NULL)
+ config->vpn_ip = g_strdup(peer);
+ else if (gateway != NULL)
+ config->vpn_ip = g_strdup(gateway);
+
+ if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
+ ipconfig = __connman_service_get_ip4config(service);
+ else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
+ ipconfig = __connman_service_get_ip6config(service);
+ else
+ return;
+
+ index = __connman_ipconfig_get_index(ipconfig);
+ data = find_phy_gateway(index, gateway);
+
+ if (data != NULL) {
+ /*
+ * data->service points now to original
+ * service that is serving the VPN link
+ */
+ if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
+ ipconfig =
+ __connman_service_get_ip4config(data->service);
+ else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
+ ipconfig =
+ __connman_service_get_ip6config(data->service);
+ else
+ return;
+
+ if (ipconfig != NULL) {
+ const char *address;
+
+ address = __connman_ipconfig_get_local(ipconfig);
+ config->vpn_phy_ip = g_strdup(address);
+ }
+
+ config->vpn_phy_index = data->index;
+ }
+
+ DBG("vpn %s phy %s index %d", config->vpn_ip,
+ config->vpn_phy_ip, config->vpn_phy_index);
+}
+