From 68dde4bb2c3e90a6f48dc48609636228dd186c8c Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Mon, 12 Nov 2012 14:07:18 +0200 Subject: ipconfig: Move IP address API into separate ipaddress.c file Done so that connman_ipaddress_* functions can be used from separate vpn daemon. --- Makefile.am | 4 +- include/ipaddress.h | 56 +++++++++++++++ include/ipconfig.h | 19 +---- src/connman.h | 2 +- src/dhcp.c | 4 +- src/ipaddress.c | 200 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ipconfig.c | 167 +------------------------------------------ src/tethering.c | 4 +- 8 files changed, 266 insertions(+), 190 deletions(-) create mode 100644 include/ipaddress.h create mode 100644 src/ipaddress.c diff --git a/Makefile.am b/Makefile.am index b845d6e2..a489c852 100644 --- a/Makefile.am +++ b/Makefile.am @@ -8,7 +8,7 @@ include_HEADERS = include/types.h include/log.h include/plugin.h \ include/resolver.h include/ipconfig.h \ include/device.h include/network.h include/inet.h \ include/storage.h include/provision.h \ - include/session.h + include/session.h include/ipaddress.h nodist_include_HEADERS = include/version.h @@ -85,7 +85,7 @@ src_connmand_SOURCES = $(gdbus_sources) $(gdhcp_sources) $(gweb_sources) \ src/technology.c src/counter.c src/ntp.c \ src/session.c src/tethering.c src/wpad.c src/wispr.c \ src/stats.c src/iptables.c src/dnsproxy.c src/6to4.c \ - src/ippool.c src/bridge.c src/nat.c + src/ippool.c src/bridge.c src/nat.c src/ipaddress.c src_connmand_LDADD = $(builtin_libadd) @GLIB_LIBS@ @DBUS_LIBS@ \ @XTABLES_LIBS@ @GNUTLS_LIBS@ -lresolv -ldl -lrt diff --git a/include/ipaddress.h b/include/ipaddress.h new file mode 100644 index 00000000..9fff622e --- /dev/null +++ b/include/ipaddress.h @@ -0,0 +1,56 @@ +/* + * + * Connection Manager + * + * Copyright (C) 2007-2012 Intel Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef __CONNMAN_IPADDRESS_H +#define __CONNMAN_IPADDRESS_H + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * SECTION:ipaddress + * @title: IP address premitives + * @short_description: Functions for IP address handling + */ + +struct connman_ipaddress; + +struct connman_ipaddress *connman_ipaddress_alloc(int family); +void connman_ipaddress_free(struct connman_ipaddress *ipaddress); +int connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress, + const char *address, const char *netmask, + const char *gateway); +int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress, + const char *address, + unsigned char prefix_length, + const char *gateway); +void connman_ipaddress_set_peer(struct connman_ipaddress *ipaddress, + const char *peer); +void connman_ipaddress_clear(struct connman_ipaddress *ipaddress); +void connman_ipaddress_copy(struct connman_ipaddress *ipaddress, + struct connman_ipaddress *source); + +#ifdef __cplusplus +} +#endif + +#endif /* __CONNMAN_IPADDRESS_H */ diff --git a/include/ipconfig.h b/include/ipconfig.h index 3612755a..a86b2955 100644 --- a/include/ipconfig.h +++ b/include/ipconfig.h @@ -22,6 +22,8 @@ #ifndef __CONNMAN_IPCONFIG_H #define __CONNMAN_IPCONFIG_H +#include + #ifdef __cplusplus extern "C" { #endif @@ -32,23 +34,6 @@ extern "C" { * @short_description: Functions for IP configuration handling */ -struct connman_ipaddress; - -struct connman_ipaddress *connman_ipaddress_alloc(int family); -void connman_ipaddress_free(struct connman_ipaddress *ipaddress); -int connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress, - const char *address, const char *netmask, - const char *gateway); -int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress, - const char *address, - unsigned char prefix_length, - const char *gateway); -void connman_ipaddress_set_peer(struct connman_ipaddress *ipaddress, - const char *peer); -void connman_ipaddress_clear(struct connman_ipaddress *ipaddress); -void connman_ipaddress_copy(struct connman_ipaddress *ipaddress, - struct connman_ipaddress *source); - enum connman_ipconfig_type { CONNMAN_IPCONFIG_TYPE_UNKNOWN = 0, CONNMAN_IPCONFIG_TYPE_ALL = 0, diff --git a/src/connman.h b/src/connman.h index 613d1dc9..014552a4 100644 --- a/src/connman.h +++ b/src/connman.h @@ -350,7 +350,7 @@ int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig); int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig); int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig); void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig); -unsigned char __connman_ipconfig_netmask_prefix_len(const char *netmask); +unsigned char __connman_ipaddress_netmask_prefix_len(const char *netmask); int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig, const char *url); diff --git a/src/dhcp.c b/src/dhcp.c index e64c975a..f32bfaef 100644 --- a/src/dhcp.c +++ b/src/dhcp.c @@ -227,7 +227,7 @@ static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data) if (option != NULL) gateway = g_strdup(option->data); - prefixlen = __connman_ipconfig_netmask_prefix_len(netmask); + prefixlen = __connman_ipaddress_netmask_prefix_len(netmask); if (prefixlen == 255) connman_warn("netmask: %s is invalid", netmask); @@ -375,7 +375,7 @@ static void ipv4ll_available_cb(GDHCPClient *dhcp_client, gpointer user_data) address = g_dhcp_client_get_address(dhcp_client); netmask = g_dhcp_client_get_netmask(dhcp_client); - prefixlen = __connman_ipconfig_netmask_prefix_len(netmask); + prefixlen = __connman_ipaddress_netmask_prefix_len(netmask); __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP); __connman_ipconfig_set_local(ipconfig, address); diff --git a/src/ipaddress.c b/src/ipaddress.c new file mode 100644 index 00000000..e5af9a06 --- /dev/null +++ b/src/ipaddress.c @@ -0,0 +1,200 @@ +/* + * + * Connection Manager + * + * Copyright (C) 2007-2012 Intel Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include + +#include + +#include "connman.h" + +struct connman_ipaddress *connman_ipaddress_alloc(int family) +{ + struct connman_ipaddress *ipaddress; + + ipaddress = g_try_new0(struct connman_ipaddress, 1); + if (ipaddress == NULL) + return NULL; + + ipaddress->family = family; + ipaddress->prefixlen = 0; + ipaddress->local = NULL; + ipaddress->peer = NULL; + ipaddress->broadcast = NULL; + ipaddress->gateway = NULL; + + return ipaddress; +} + +void connman_ipaddress_free(struct connman_ipaddress *ipaddress) +{ + if (ipaddress == NULL) + return; + + g_free(ipaddress->broadcast); + g_free(ipaddress->peer); + g_free(ipaddress->local); + g_free(ipaddress->gateway); + g_free(ipaddress); +} + +unsigned char __connman_ipaddress_netmask_prefix_len(const char *netmask) +{ + unsigned char bits; + in_addr_t mask; + in_addr_t host; + + if (netmask == NULL) + return 32; + + mask = inet_network(netmask); + host = ~mask; + + /* a valid netmask must be 2^n - 1 */ + if ((host & (host + 1)) != 0) + return -1; + + bits = 0; + for (; mask; mask <<= 1) + ++bits; + + return bits; +} + +static gboolean check_ipv6_address(const char *address) +{ + unsigned char buf[sizeof(struct in6_addr)]; + int err; + + if (address == NULL) + return FALSE; + + err = inet_pton(AF_INET6, address, buf); + if (err > 0) + return TRUE; + + return FALSE; +} + +int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress, + const char *address, + unsigned char prefix_length, + const char *gateway) +{ + if (ipaddress == NULL) + return -EINVAL; + + if (check_ipv6_address(address) == FALSE) + return -EINVAL; + + DBG("prefix_len %d address %s gateway %s", + prefix_length, address, gateway); + + ipaddress->family = AF_INET6; + + ipaddress->prefixlen = prefix_length; + + g_free(ipaddress->local); + ipaddress->local = g_strdup(address); + + g_free(ipaddress->gateway); + ipaddress->gateway = g_strdup(gateway); + + return 0; +} + +int connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress, + const char *address, const char *netmask, const char *gateway) +{ + if (ipaddress == NULL) + return -EINVAL; + + ipaddress->family = AF_INET; + + ipaddress->prefixlen = __connman_ipaddress_netmask_prefix_len(netmask); + + g_free(ipaddress->local); + ipaddress->local = g_strdup(address); + + g_free(ipaddress->gateway); + ipaddress->gateway = g_strdup(gateway); + + return 0; +} + +void connman_ipaddress_set_peer(struct connman_ipaddress *ipaddress, + const char *peer) +{ + if (ipaddress == NULL) + return; + + g_free(ipaddress->peer); + ipaddress->peer = g_strdup(peer); +} + +void connman_ipaddress_clear(struct connman_ipaddress *ipaddress) +{ + if (ipaddress == NULL) + return; + + ipaddress->prefixlen = 0; + + g_free(ipaddress->local); + ipaddress->local = NULL; + + g_free(ipaddress->peer); + ipaddress->peer = NULL; + + g_free(ipaddress->broadcast); + ipaddress->broadcast = NULL; + + g_free(ipaddress->gateway); + ipaddress->gateway = NULL; +} + +void connman_ipaddress_copy(struct connman_ipaddress *ipaddress, + struct connman_ipaddress *source) +{ + if (ipaddress == NULL || source == NULL) + return; + + ipaddress->family = source->family; + ipaddress->prefixlen = source->prefixlen; + + g_free(ipaddress->local); + ipaddress->local = g_strdup(source->local); + + g_free(ipaddress->peer); + ipaddress->peer = g_strdup(source->peer); + + g_free(ipaddress->broadcast); + ipaddress->broadcast = g_strdup(source->broadcast); + + g_free(ipaddress->gateway); + ipaddress->gateway = g_strdup(source->gateway); +} diff --git a/src/ipconfig.c b/src/ipconfig.c index aa7a03db..3cb0d9bb 100644 --- a/src/ipconfig.c +++ b/src/ipconfig.c @@ -36,6 +36,7 @@ #endif #include +#include #include "connman.h" @@ -91,150 +92,6 @@ static GHashTable *ipdevice_hash = NULL; static GList *ipconfig_list = NULL; static connman_bool_t is_ipv6_supported = FALSE; -struct connman_ipaddress *connman_ipaddress_alloc(int family) -{ - struct connman_ipaddress *ipaddress; - - ipaddress = g_try_new0(struct connman_ipaddress, 1); - if (ipaddress == NULL) - return NULL; - - ipaddress->family = family; - ipaddress->prefixlen = 0; - ipaddress->local = NULL; - ipaddress->peer = NULL; - ipaddress->broadcast = NULL; - ipaddress->gateway = NULL; - - return ipaddress; -} - -void connman_ipaddress_free(struct connman_ipaddress *ipaddress) -{ - if (ipaddress == NULL) - return; - - g_free(ipaddress->broadcast); - g_free(ipaddress->peer); - g_free(ipaddress->local); - g_free(ipaddress->gateway); - g_free(ipaddress); -} - -unsigned char __connman_ipconfig_netmask_prefix_len(const char *netmask) -{ - unsigned char bits; - in_addr_t mask; - in_addr_t host; - - if (netmask == NULL) - return 32; - - mask = inet_network(netmask); - host = ~mask; - - /* a valid netmask must be 2^n - 1 */ - if ((host & (host + 1)) != 0) - return -1; - - bits = 0; - for (; mask; mask <<= 1) - ++bits; - - return bits; -} - -static gboolean check_ipv6_address(const char *address) -{ - unsigned char buf[sizeof(struct in6_addr)]; - int err; - - if (address == NULL) - return FALSE; - - err = inet_pton(AF_INET6, address, buf); - if (err > 0) - return TRUE; - - return FALSE; -} - -int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress, - const char *address, - unsigned char prefix_length, - const char *gateway) -{ - if (ipaddress == NULL) - return -EINVAL; - - if (check_ipv6_address(address) == FALSE) - return -EINVAL; - - DBG("prefix_len %d address %s gateway %s", - prefix_length, address, gateway); - - ipaddress->family = AF_INET6; - - ipaddress->prefixlen = prefix_length; - - g_free(ipaddress->local); - ipaddress->local = g_strdup(address); - - g_free(ipaddress->gateway); - ipaddress->gateway = g_strdup(gateway); - - return 0; -} - -int connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress, - const char *address, const char *netmask, const char *gateway) -{ - if (ipaddress == NULL) - return -EINVAL; - - ipaddress->family = AF_INET; - - ipaddress->prefixlen = __connman_ipconfig_netmask_prefix_len(netmask); - - g_free(ipaddress->local); - ipaddress->local = g_strdup(address); - - g_free(ipaddress->gateway); - ipaddress->gateway = g_strdup(gateway); - - return 0; -} - -void connman_ipaddress_set_peer(struct connman_ipaddress *ipaddress, - const char *peer) -{ - if (ipaddress == NULL) - return; - - g_free(ipaddress->peer); - ipaddress->peer = g_strdup(peer); -} - -void connman_ipaddress_clear(struct connman_ipaddress *ipaddress) -{ - if (ipaddress == NULL) - return; - - ipaddress->prefixlen = 0; - - g_free(ipaddress->local); - ipaddress->local = NULL; - - g_free(ipaddress->peer); - ipaddress->peer = NULL; - - g_free(ipaddress->broadcast); - ipaddress->broadcast = NULL; - - g_free(ipaddress->gateway); - ipaddress->gateway = NULL; -} - void __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig) { if (ipconfig == NULL) @@ -243,28 +100,6 @@ void __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig) connman_ipaddress_clear(ipconfig->address); } -void connman_ipaddress_copy(struct connman_ipaddress *ipaddress, - struct connman_ipaddress *source) -{ - if (ipaddress == NULL || source == NULL) - return; - - ipaddress->family = source->family; - ipaddress->prefixlen = source->prefixlen; - - g_free(ipaddress->local); - ipaddress->local = g_strdup(source->local); - - g_free(ipaddress->peer); - ipaddress->peer = g_strdup(source->peer); - - g_free(ipaddress->broadcast); - ipaddress->broadcast = g_strdup(source->broadcast); - - g_free(ipaddress->gateway); - ipaddress->gateway = g_strdup(source->gateway); -} - static void free_address_list(struct connman_ipdevice *ipdevice) { GSList *list; diff --git a/src/tethering.c b/src/tethering.c index b9df21b3..ab892d3a 100644 --- a/src/tethering.c +++ b/src/tethering.c @@ -247,7 +247,7 @@ void __connman_tethering_set_enabled(void) } prefixlen = - __connman_ipconfig_netmask_prefix_len(subnet_mask); + __connman_ipaddress_netmask_prefix_len(subnet_mask); __connman_nat_enable(BRIDGE_NAME, start_ip, prefixlen); DBG("tethering started"); @@ -300,7 +300,7 @@ static void setup_tun_interface(unsigned int flags, unsigned change, server_ip = __connman_ippool_get_start_ip(pn->pool); peer_ip = __connman_ippool_get_end_ip(pn->pool); prefixlen = - __connman_ipconfig_netmask_prefix_len(subnet_mask); + __connman_ipaddress_netmask_prefix_len(subnet_mask); if ((__connman_inet_modify_address(RTM_NEWADDR, NLM_F_REPLACE | NLM_F_ACK, pn->index, AF_INET, -- cgit v1.2.3