summaryrefslogtreecommitdiff
path: root/src/ipaddress.c
diff options
context:
space:
mode:
authorZhang zhengguang <zhengguang.zhang@intel.com>2014-10-29 11:03:47 +0800
committerZhang zhengguang <zhengguang.zhang@intel.com>2014-10-29 11:03:47 +0800
commitbcae74da8fa2958b3fec9153fc33e41f0e0317bf (patch)
tree06a00f6457307467fee4f6580dce4a1a857751c1 /src/ipaddress.c
parent1b9d0a62f59bb48c8deb2f0b98d9acdffdd9abe7 (diff)
downloadconnman-bcae74da8fa2958b3fec9153fc33e41f0e0317bf.tar.gz
connman-bcae74da8fa2958b3fec9153fc33e41f0e0317bf.tar.bz2
connman-bcae74da8fa2958b3fec9153fc33e41f0e0317bf.zip
Imported Upstream version 1.26upstream/1.26
Diffstat (limited to 'src/ipaddress.c')
-rw-r--r--src/ipaddress.c84
1 files changed, 58 insertions, 26 deletions
diff --git a/src/ipaddress.c b/src/ipaddress.c
index 57f9435d..d63d95c3 100644
--- a/src/ipaddress.c
+++ b/src/ipaddress.c
@@ -33,6 +33,29 @@
#include "connman.h"
+unsigned char connman_ipaddress_calc_netmask_len(const char *netmask)
+{
+ unsigned char bits;
+ in_addr_t mask;
+ in_addr_t host;
+
+ if (!netmask)
+ 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;
+}
+
struct connman_ipaddress *connman_ipaddress_alloc(int family)
{
struct connman_ipaddress *ipaddress;
@@ -63,29 +86,6 @@ void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
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)
- 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 bool check_ipv6_address(const char *address)
{
unsigned char buf[sizeof(struct in6_addr)];
@@ -128,6 +128,19 @@ int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress,
return 0;
}
+int connman_ipaddress_get_ip(struct connman_ipaddress *ipaddress,
+ const char **address,
+ unsigned char *netmask_prefix_length)
+{
+ if (!ipaddress)
+ return -EINVAL;
+
+ *netmask_prefix_length = ipaddress->prefixlen;
+ *address = ipaddress->local;
+
+ return 0;
+}
+
int connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
const char *address, const char *netmask, const char *gateway)
{
@@ -136,7 +149,7 @@ int connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
ipaddress->family = AF_INET;
- ipaddress->prefixlen = __connman_ipaddress_netmask_prefix_len(netmask);
+ ipaddress->prefixlen = connman_ipaddress_calc_netmask_len(netmask);
g_free(ipaddress->local);
ipaddress->local = g_strdup(address);
@@ -179,8 +192,7 @@ void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
/*
* Note that this copy function only copies the actual address and
- * prefixlen. If you need full copy of ipaddress struct, then you need
- * to create a new function that does that.
+ * prefixlen. Use the other copy function to copy the whole struct.
*/
void connman_ipaddress_copy_address(struct connman_ipaddress *ipaddress,
struct connman_ipaddress *source)
@@ -194,3 +206,23 @@ void connman_ipaddress_copy_address(struct connman_ipaddress *ipaddress,
g_free(ipaddress->local);
ipaddress->local = g_strdup(source->local);
}
+
+struct connman_ipaddress *
+connman_ipaddress_copy(struct connman_ipaddress *ipaddress)
+{
+ struct connman_ipaddress *copy;
+
+ if (!ipaddress)
+ return NULL;
+
+ copy = g_new0(struct connman_ipaddress, 1);
+
+ copy->family = ipaddress->family;
+ copy->prefixlen = ipaddress->prefixlen;
+ copy->local = g_strdup(ipaddress->local);
+ copy->peer = g_strdup(ipaddress->peer);
+ copy->broadcast = g_strdup(ipaddress->broadcast);
+ copy->gateway = g_strdup(ipaddress->gateway);
+
+ return copy;
+}