5 * Copyright (C) 2007-2012 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 struct connman_ipaddress *connman_ipaddress_alloc(int family)
38 struct connman_ipaddress *ipaddress;
40 ipaddress = g_try_new0(struct connman_ipaddress, 1);
41 if (ipaddress == NULL)
44 ipaddress->family = family;
45 ipaddress->prefixlen = 0;
46 ipaddress->local = NULL;
47 ipaddress->peer = NULL;
48 ipaddress->broadcast = NULL;
49 ipaddress->gateway = NULL;
54 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
56 if (ipaddress == NULL)
59 g_free(ipaddress->broadcast);
60 g_free(ipaddress->peer);
61 g_free(ipaddress->local);
62 g_free(ipaddress->gateway);
66 unsigned char __connman_ipaddress_netmask_prefix_len(const char *netmask)
75 mask = inet_network(netmask);
78 /* a valid netmask must be 2^n - 1 */
79 if ((host & (host + 1)) != 0)
83 for (; mask; mask <<= 1)
89 static gboolean 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,
109 if (ipaddress == NULL)
112 if (check_ipv6_address(address) == FALSE)
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_set_ipv4(struct connman_ipaddress *ipaddress,
132 const char *address, const char *netmask, const char *gateway)
134 if (ipaddress == NULL)
137 ipaddress->family = AF_INET;
139 ipaddress->prefixlen = __connman_ipaddress_netmask_prefix_len(netmask);
141 g_free(ipaddress->local);
142 ipaddress->local = g_strdup(address);
144 g_free(ipaddress->gateway);
145 ipaddress->gateway = g_strdup(gateway);
150 void connman_ipaddress_set_peer(struct connman_ipaddress *ipaddress,
153 if (ipaddress == NULL)
156 g_free(ipaddress->peer);
157 ipaddress->peer = g_strdup(peer);
160 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
162 if (ipaddress == NULL)
165 ipaddress->prefixlen = 0;
167 g_free(ipaddress->local);
168 ipaddress->local = NULL;
170 g_free(ipaddress->peer);
171 ipaddress->peer = NULL;
173 g_free(ipaddress->broadcast);
174 ipaddress->broadcast = NULL;
176 g_free(ipaddress->gateway);
177 ipaddress->gateway = NULL;
181 * Note that this copy function only copies the actual address and
182 * prefixlen. If you need full copy of ipaddress struct, then you need
183 * to create a new function that does that.
185 void connman_ipaddress_copy_address(struct connman_ipaddress *ipaddress,
186 struct connman_ipaddress *source)
188 if (ipaddress == NULL || source == NULL)
191 ipaddress->family = source->family;
192 ipaddress->prefixlen = source->prefixlen;
194 g_free(ipaddress->local);
195 ipaddress->local = g_strdup(source->local);