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;
73 ipaddress->is_p2p = false;
78 void connman_ipaddress_set_p2p(struct connman_ipaddress *ipaddress, bool value)
83 ipaddress->is_p2p = value;
86 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
91 g_free(ipaddress->broadcast);
92 g_free(ipaddress->peer);
93 g_free(ipaddress->local);
94 g_free(ipaddress->gateway);
98 static bool check_ipv6_address(const char *address)
100 unsigned char buf[sizeof(struct in6_addr)];
106 err = inet_pton(AF_INET6, address, buf);
113 int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress,
115 unsigned char prefix_length,
121 if (!check_ipv6_address(address))
124 DBG("prefix_len %d address %s gateway %s",
125 prefix_length, address, gateway);
127 ipaddress->family = AF_INET6;
129 ipaddress->prefixlen = prefix_length;
131 g_free(ipaddress->local);
132 ipaddress->local = g_strdup(address);
134 g_free(ipaddress->gateway);
135 ipaddress->gateway = g_strdup(gateway);
140 int connman_ipaddress_get_ip(struct connman_ipaddress *ipaddress,
141 const char **address,
142 unsigned char *netmask_prefix_length)
147 *netmask_prefix_length = ipaddress->prefixlen;
148 *address = ipaddress->local;
153 int connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
154 const char *address, const char *netmask, const char *gateway)
159 ipaddress->family = AF_INET;
161 ipaddress->prefixlen = connman_ipaddress_calc_netmask_len(netmask);
163 g_free(ipaddress->local);
164 ipaddress->local = g_strdup(address);
166 g_free(ipaddress->gateway);
167 ipaddress->gateway = g_strdup(gateway);
172 void connman_ipaddress_set_peer(struct connman_ipaddress *ipaddress,
178 g_free(ipaddress->peer);
179 ipaddress->peer = g_strdup(peer);
182 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
187 ipaddress->prefixlen = 0;
189 g_free(ipaddress->local);
190 ipaddress->local = NULL;
192 g_free(ipaddress->peer);
193 ipaddress->peer = NULL;
195 g_free(ipaddress->broadcast);
196 ipaddress->broadcast = NULL;
198 g_free(ipaddress->gateway);
199 ipaddress->gateway = NULL;
203 * Note that this copy function only copies the actual address and
204 * prefixlen. Use the other copy function to copy the whole struct.
206 void connman_ipaddress_copy_address(struct connman_ipaddress *ipaddress,
207 struct connman_ipaddress *source)
209 if (!ipaddress || !source)
212 ipaddress->family = source->family;
213 ipaddress->prefixlen = source->prefixlen;
215 g_free(ipaddress->local);
216 ipaddress->local = g_strdup(source->local);
219 struct connman_ipaddress *
220 connman_ipaddress_copy(struct connman_ipaddress *ipaddress)
222 struct connman_ipaddress *copy;
227 copy = g_new0(struct connman_ipaddress, 1);
229 copy->family = ipaddress->family;
230 copy->prefixlen = ipaddress->prefixlen;
231 copy->local = g_strdup(ipaddress->local);
232 copy->peer = g_strdup(ipaddress->peer);
233 copy->broadcast = g_strdup(ipaddress->broadcast);
234 copy->gateway = g_strdup(ipaddress->gateway);
235 copy->is_p2p = ipaddress->is_p2p;