Updated connman to version 1.35
[platform/upstream/connman.git] / src / tethering.c
old mode 100644 (file)
new mode 100755 (executable)
index c7e17f5..891ee51
@@ -52,6 +52,9 @@
 
 #define DEFAULT_MTU    1500
 
+#define CONNMAN_STATION_STR_INFO_LEN           64
+#define CONNMAN_STATION_MAC_INFO_LEN           32
+
 static char *private_network_primary_dns = NULL;
 static char *private_network_secondary_dns = NULL;
 
@@ -60,6 +63,7 @@ static GDHCPServer *tethering_dhcp_server = NULL;
 static struct connman_ippool *dhcp_ippool = NULL;
 static DBusConnection *connection;
 static GHashTable *pn_hash;
+static GHashTable *sta_hash;
 
 struct connman_private_network {
        char *owner;
@@ -76,6 +80,164 @@ struct connman_private_network {
        char *secondary_dns;
 };
 
+struct connman_station_info {
+       bool is_connected;
+       char *path;
+       char *type;
+       char ip[CONNMAN_STATION_STR_INFO_LEN];
+       char mac[CONNMAN_STATION_MAC_INFO_LEN];
+       char hostname[CONNMAN_STATION_STR_INFO_LEN];
+};
+
+static void emit_station_signal(char *action_str,
+                       const struct connman_station_info *station_info)
+{
+       char *ip, *mac, *hostname;
+
+       if (station_info->path == NULL || station_info->type == NULL
+               || station_info->ip == NULL || station_info->mac == NULL
+                       || station_info->hostname == NULL)
+               return;
+
+       ip = g_strdup(station_info->ip);
+       mac = g_strdup(station_info->mac);
+       hostname = g_strdup(station_info->hostname);
+
+       g_dbus_emit_signal(connection, station_info->path,
+                       CONNMAN_TECHNOLOGY_INTERFACE, action_str,
+                       DBUS_TYPE_STRING, &station_info->type,
+                       DBUS_TYPE_STRING, &ip,
+                       DBUS_TYPE_STRING, &mac,
+                       DBUS_TYPE_STRING, &hostname,
+                       DBUS_TYPE_INVALID);
+
+       g_free(ip);
+       g_free(mac);
+       g_free(hostname);
+}
+static void destroy_station(gpointer key, gpointer value, gpointer user_data)
+{
+       struct connman_station_info *station_info;
+
+       __sync_synchronize();
+
+       station_info = value;
+
+       if (station_info->is_connected) {
+               station_info->is_connected = FALSE;
+               emit_station_signal("DhcpLeaseDeleted", station_info);
+       }
+
+       g_free(station_info->path);
+       g_free(station_info->type);
+       g_free(station_info);
+}
+
+static void save_dhcp_ack_lease_info(char *hostname,
+                                    unsigned char *mac, unsigned int nip)
+{
+       char *lower_mac;
+       const char *ip;
+       char sta_mac[CONNMAN_STATION_MAC_INFO_LEN];
+       struct connman_station_info *info_found;
+       struct in_addr addr;
+       int str_len;
+
+       __sync_synchronize();
+
+       snprintf(sta_mac, CONNMAN_STATION_MAC_INFO_LEN,
+                "%02x:%02x:%02x:%02x:%02x:%02x",
+                mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
+       lower_mac = g_ascii_strdown(sta_mac, -1);
+
+       info_found = g_hash_table_lookup(sta_hash, lower_mac);
+       if (info_found == NULL) {
+               g_free(lower_mac);
+               return;
+       }
+
+       /* get the ip */
+       addr.s_addr = nip;
+       ip = inet_ntoa(addr);
+       str_len = strlen(ip) + 1;
+       if (str_len > CONNMAN_STATION_STR_INFO_LEN)
+               str_len = CONNMAN_STATION_STR_INFO_LEN - 1;
+       memcpy(info_found->ip, ip, str_len);
+
+       /* get hostname */
+       str_len = strlen(hostname) + 1;
+       if (str_len > CONNMAN_STATION_STR_INFO_LEN)
+               str_len = CONNMAN_STATION_STR_INFO_LEN - 1;
+       memcpy(info_found->hostname, hostname, str_len);
+
+       /* emit a signal */
+       info_found->is_connected = TRUE;
+       emit_station_signal("DhcpConnected", info_found);
+       g_free(lower_mac);
+}
+
+int connman_technology_tethering_add_station(enum connman_service_type type,
+                                                       const char *mac)
+{
+       const char *str_type;
+       char *lower_mac;
+       char *path;
+       struct connman_station_info *station_info;
+
+       __sync_synchronize();
+
+       DBG("type %d", type);
+
+       str_type = __connman_service_type2string(type);
+       if (str_type == NULL)
+               return 0;
+
+       path = g_strdup_printf("%s/technology/%s", CONNMAN_PATH, str_type);
+
+       station_info = g_try_new0(struct connman_station_info, 1);
+       if (station_info == NULL)
+               return -ENOMEM;
+
+       lower_mac = g_ascii_strdown(mac, -1);
+
+       memcpy(station_info->mac, lower_mac, strlen(lower_mac) + 1);
+       station_info->path = path;
+       station_info->type = g_strdup(str_type);
+
+       g_hash_table_insert(sta_hash, station_info->mac, station_info);
+
+       g_free(lower_mac);
+       return 0;
+}
+
+int connman_technology_tethering_remove_station(const char *mac)
+{
+       char *lower_mac;
+       struct connman_station_info *info_found;
+
+       __sync_synchronize();
+
+       lower_mac = g_ascii_strdown(mac, -1);
+
+       info_found = g_hash_table_lookup(sta_hash, lower_mac);
+       if (info_found == NULL) {
+               g_free(lower_mac);
+               return -EACCES;
+       }
+
+       if (info_found->is_connected) {
+               info_found->is_connected = FALSE;
+               emit_station_signal("DhcpLeaseDeleted", info_found);
+       }
+       g_free(lower_mac);
+       g_hash_table_remove(sta_hash, info_found->mac);
+       g_free(info_found->path);
+       g_free(info_found->type);
+       g_free(info_found);
+
+       return 0;
+}
+
 const char *__connman_tethering_get_bridge(void)
 {
        int sk, err;
@@ -161,6 +323,9 @@ static GDHCPServer *dhcp_server_start(const char *bridge,
        g_dhcp_server_set_option(dhcp_server, G_DHCP_DNS_SERVER, dns);
        g_dhcp_server_set_ip_range(dhcp_server, start_ip, end_ip);
 
+       g_dhcp_server_set_save_ack_lease(dhcp_server,
+                                        save_dhcp_ack_lease_info, NULL);
+
        g_dhcp_server_start(dhcp_server);
 
        return dhcp_server;
@@ -181,7 +346,7 @@ static void tethering_restart(struct connman_ippool *pool, void *user_data)
        __connman_tethering_set_enabled();
 }
 
-void __connman_tethering_set_enabled(void)
+int __connman_tethering_set_enabled(void)
 {
        int index;
        int err;
@@ -197,12 +362,12 @@ void __connman_tethering_set_enabled(void)
        DBG("enabled %d", tethering_enabled + 1);
 
        if (__sync_fetch_and_add(&tethering_enabled, 1) != 0)
-               return;
+               return 0;
 
        err = __connman_bridge_create(BRIDGE_NAME);
        if (err < 0) {
                __sync_fetch_and_sub(&tethering_enabled, 1);
-               return;
+               return -EOPNOTSUPP;
        }
 
        index = connman_inet_ifindex(BRIDGE_NAME);
@@ -212,7 +377,7 @@ void __connman_tethering_set_enabled(void)
                connman_error("Fail to create IP pool");
                __connman_bridge_remove(BRIDGE_NAME);
                __sync_fetch_and_sub(&tethering_enabled, 1);
-               return;
+               return -EADDRNOTAVAIL;
        }
 
        gateway = __connman_ippool_get_gateway(dhcp_ippool);
@@ -222,13 +387,13 @@ void __connman_tethering_set_enabled(void)
        end_ip = __connman_ippool_get_end_ip(dhcp_ippool);
 
        err = __connman_bridge_enable(BRIDGE_NAME, gateway,
-                       __connman_ipaddress_netmask_prefix_len(subnet_mask),
+                       connman_ipaddress_calc_netmask_len(subnet_mask),
                        broadcast);
        if (err < 0 && err != -EALREADY) {
                __connman_ippool_unref(dhcp_ippool);
                __connman_bridge_remove(BRIDGE_NAME);
                __sync_fetch_and_sub(&tethering_enabled, 1);
-               return;
+               return -EADDRNOTAVAIL;
        }
 
        ns = connman_setting_get_string_list("FallbackNameservers");
@@ -264,10 +429,10 @@ void __connman_tethering_set_enabled(void)
                __connman_ippool_unref(dhcp_ippool);
                __connman_bridge_remove(BRIDGE_NAME);
                __sync_fetch_and_sub(&tethering_enabled, 1);
-               return;
+               return -EOPNOTSUPP;
        }
 
-       prefixlen = __connman_ipaddress_netmask_prefix_len(subnet_mask);
+       prefixlen = connman_ipaddress_calc_netmask_len(subnet_mask);
        err = __connman_nat_enable(BRIDGE_NAME, start_ip, prefixlen);
        if (err < 0) {
                connman_error("Cannot enable NAT %d/%s", err, strerror(-err));
@@ -276,7 +441,7 @@ void __connman_tethering_set_enabled(void)
                __connman_ippool_unref(dhcp_ippool);
                __connman_bridge_remove(BRIDGE_NAME);
                __sync_fetch_and_sub(&tethering_enabled, 1);
-               return;
+               return -EOPNOTSUPP;
        }
 
        err = __connman_ipv6pd_setup(BRIDGE_NAME);
@@ -285,6 +450,8 @@ void __connman_tethering_set_enabled(void)
                        strerror(-err));
 
        DBG("tethering started");
+
+       return 0;
 }
 
 void __connman_tethering_set_disabled(void)
@@ -340,8 +507,7 @@ static void setup_tun_interface(unsigned int flags, unsigned change,
        subnet_mask = __connman_ippool_get_subnet_mask(pn->pool);
        server_ip = __connman_ippool_get_start_ip(pn->pool);
        peer_ip = __connman_ippool_get_end_ip(pn->pool);
-       prefixlen =
-               __connman_ipaddress_netmask_prefix_len(subnet_mask);
+       prefixlen = connman_ipaddress_calc_netmask_len(subnet_mask);
 
        if ((__connman_inet_modify_address(RTM_NEWADDR,
                                NLM_F_REPLACE | NLM_F_ACK, pn->index, AF_INET,
@@ -506,6 +672,8 @@ error:
        close(fd);
        g_free(iface);
        g_free(path);
+       if (pn)
+               g_free(pn->owner);
        g_free(pn);
        return err;
 }
@@ -535,6 +703,9 @@ int __connman_tethering_init(void)
        pn_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
                                                NULL, remove_private_network);
 
+       sta_hash = g_hash_table_new_full(g_str_hash,
+                                        g_str_equal, NULL, NULL);
+
        return 0;
 }
 
@@ -555,5 +726,7 @@ void __connman_tethering_cleanup(void)
                return;
 
        g_hash_table_destroy(pn_hash);
+       g_hash_table_foreach(sta_hash, destroy_station, NULL);
+       g_hash_table_destroy(sta_hash);
        dbus_connection_unref(connection);
 }