5 * Copyright (C) 2007-2013 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <connman/ipaddress.h>
36 unsigned char connman_ipaddress_calc_netmask_len(const char *netmask)
45 mask = inet_network(netmask);
48 /* a valid netmask must be 2^n - 1 */
49 if ((host & (host + 1)) != 0)
53 for (; mask; mask <<= 1)
59 struct connman_ipaddress *connman_ipaddress_alloc(int family)
61 struct connman_ipaddress *ipaddress;
63 ipaddress = g_try_new0(struct connman_ipaddress, 1);
67 ipaddress->family = family;
68 ipaddress->prefixlen = 0;
69 ipaddress->local = NULL;
70 ipaddress->peer = NULL;
71 ipaddress->broadcast = NULL;
72 ipaddress->gateway = NULL;
77 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
82 g_free(ipaddress->broadcast);
83 g_free(ipaddress->peer);
84 g_free(ipaddress->local);
85 g_free(ipaddress->gateway);
89 static bool check_ipv6_address(const char *address)
91 unsigned char buf[sizeof(struct in6_addr)];
97 err = inet_pton(AF_INET6, address, buf);
104 int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress,
106 unsigned char prefix_length,
112 if (!check_ipv6_address(address))
115 DBG("prefix_len %d address %s gateway %s",
116 prefix_length, address, gateway);
118 ipaddress->family = AF_INET6;
120 ipaddress->prefixlen = prefix_length;
122 g_free(ipaddress->local);
123 ipaddress->local = g_strdup(address);
125 g_free(ipaddress->gateway);
126 ipaddress->gateway = g_strdup(gateway);
131 int connman_ipaddress_get_ip(struct connman_ipaddress *ipaddress,
132 const char **address,
133 unsigned char *netmask_prefix_length)
138 *netmask_prefix_length = ipaddress->prefixlen;
139 *address = ipaddress->local;
144 int connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
145 const char *address, const char *netmask, const char *gateway)
150 ipaddress->family = AF_INET;
152 ipaddress->prefixlen = connman_ipaddress_calc_netmask_len(netmask);
154 g_free(ipaddress->local);
155 ipaddress->local = g_strdup(address);
157 g_free(ipaddress->gateway);
158 ipaddress->gateway = g_strdup(gateway);
163 void connman_ipaddress_set_peer(struct connman_ipaddress *ipaddress,
169 g_free(ipaddress->peer);
170 ipaddress->peer = g_strdup(peer);
173 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
178 ipaddress->prefixlen = 0;
180 g_free(ipaddress->local);
181 ipaddress->local = NULL;
183 g_free(ipaddress->peer);
184 ipaddress->peer = NULL;
186 g_free(ipaddress->broadcast);
187 ipaddress->broadcast = NULL;
189 g_free(ipaddress->gateway);
190 ipaddress->gateway = NULL;
194 * Note that this copy function only copies the actual address and
195 * prefixlen. Use the other copy function to copy the whole struct.
197 void connman_ipaddress_copy_address(struct connman_ipaddress *ipaddress,
198 struct connman_ipaddress *source)
200 if (!ipaddress || !source)
203 ipaddress->family = source->family;
204 ipaddress->prefixlen = source->prefixlen;
206 g_free(ipaddress->local);
207 ipaddress->local = g_strdup(source->local);
210 struct connman_ipaddress *
211 connman_ipaddress_copy(struct connman_ipaddress *ipaddress)
213 struct connman_ipaddress *copy;
218 copy = g_new0(struct connman_ipaddress, 1);
220 copy->family = ipaddress->family;
221 copy->prefixlen = ipaddress->prefixlen;
222 copy->local = g_strdup(ipaddress->local);
223 copy->peer = g_strdup(ipaddress->peer);
224 copy->broadcast = g_strdup(ipaddress->broadcast);
225 copy->gateway = g_strdup(ipaddress->gateway);