5 * Copyright (C) 2007-2010 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
28 #include <sys/ioctl.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
37 enum connman_ipconfig_method method;
38 struct in_addr address;
39 struct in_addr netmask;
40 struct in_addr broadcast;
43 static int set_ipv4(struct connman_element *element,
44 struct connman_ipv4 *ipv4, const char *nameserver)
47 struct sockaddr_in addr;
50 DBG("element %p ipv4 %p", element, ipv4);
52 sk = socket(PF_INET, SOCK_DGRAM, 0);
56 memset(&ifr, 0, sizeof(ifr));
57 ifr.ifr_ifindex = element->index;
59 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
64 DBG("ifname %s", ifr.ifr_name);
66 memset(&addr, 0, sizeof(addr));
67 addr.sin_family = AF_INET;
68 addr.sin_addr = ipv4->address;
69 memcpy(&ifr.ifr_addr, &addr, sizeof(ifr.ifr_addr));
71 err = ioctl(sk, SIOCSIFADDR, &ifr);
74 DBG("address setting failed (%s)", strerror(errno));
76 memset(&addr, 0, sizeof(addr));
77 addr.sin_family = AF_INET;
78 addr.sin_addr = ipv4->netmask;
79 memcpy(&ifr.ifr_netmask, &addr, sizeof(ifr.ifr_netmask));
81 err = ioctl(sk, SIOCSIFNETMASK, &ifr);
84 DBG("netmask setting failed (%s)", strerror(errno));
86 memset(&addr, 0, sizeof(addr));
87 addr.sin_family = AF_INET;
88 addr.sin_addr = ipv4->broadcast;
89 memcpy(&ifr.ifr_broadaddr, &addr, sizeof(ifr.ifr_broadaddr));
91 err = ioctl(sk, SIOCSIFBRDADDR, &ifr);
94 DBG("broadcast setting failed (%s)", strerror(errno));
98 if (nameserver == NULL)
99 connman_error("No nameserver for %s defined", ifr.ifr_name);
101 connman_resolver_append(ifr.ifr_name, NULL, nameserver);
106 static int clear_ipv4(struct connman_element *element)
109 struct sockaddr_in addr;
112 DBG("element %p", element);
114 sk = socket(PF_INET, SOCK_DGRAM, 0);
118 memset(&ifr, 0, sizeof(ifr));
119 ifr.ifr_ifindex = element->index;
121 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
126 DBG("ifname %s", ifr.ifr_name);
128 connman_resolver_remove_all(ifr.ifr_name);
130 memset(&addr, 0, sizeof(addr));
131 addr.sin_family = AF_INET;
132 addr.sin_addr.s_addr = INADDR_ANY;
133 memcpy(&ifr.ifr_addr, &addr, sizeof(ifr.ifr_addr));
135 //err = ioctl(sk, SIOCDIFADDR, &ifr);
136 err = ioctl(sk, SIOCSIFADDR, &ifr);
140 if (err < 0 && errno != EADDRNOTAVAIL) {
141 DBG("address removal failed (%s)", strerror(errno));
148 static char *index2name(int index)
156 sk = socket(PF_INET, SOCK_DGRAM, 0);
160 memset(&ifr, 0, sizeof(ifr));
161 ifr.ifr_ifindex = index;
163 err = ioctl(sk, SIOCGIFNAME, &ifr);
170 return strdup(ifr.ifr_name);
173 static int ipv4_probe(struct connman_element *element)
175 struct connman_element *connection;
176 struct connman_ipv4 ipv4;
177 const char *address = NULL, *netmask = NULL, *broadcast = NULL;
178 const char *nameserver = NULL, *timeserver = NULL;
180 DBG("element %p name %s", element, element->name);
182 connman_element_get_value(element,
183 CONNMAN_PROPERTY_ID_IPV4_ADDRESS, &address);
184 connman_element_get_value(element,
185 CONNMAN_PROPERTY_ID_IPV4_NETMASK, &netmask);
186 connman_element_get_value(element,
187 CONNMAN_PROPERTY_ID_IPV4_BROADCAST, &broadcast);
189 connman_element_get_value(element,
190 CONNMAN_PROPERTY_ID_IPV4_NAMESERVER, &nameserver);
191 connman_element_get_value(element,
192 CONNMAN_PROPERTY_ID_IPV4_TIMESERVER, ×erver);
194 DBG("address %s", address);
195 DBG("netmask %s", netmask);
196 DBG("broadcast %s", broadcast);
198 if (address == NULL || netmask == NULL)
201 memset(&ipv4, 0, sizeof(ipv4));
202 ipv4.address.s_addr = inet_addr(address);
203 ipv4.netmask.s_addr = inet_addr(netmask);
205 ipv4.broadcast.s_addr = inet_addr(broadcast);
207 ipv4.broadcast.s_addr = ipv4.address.s_addr |
208 ~ipv4.netmask.s_addr;
210 set_ipv4(element, &ipv4, nameserver);
212 connman_timeserver_append(timeserver);
214 connection = connman_element_create(NULL);
216 connection->type = CONNMAN_ELEMENT_TYPE_CONNECTION;
217 connection->index = element->index;
218 connection->devname = index2name(element->index);
220 if (connman_element_register(connection, element) < 0)
221 connman_element_unref(connection);
226 static void ipv4_remove(struct connman_element *element)
228 const char *timeserver = NULL;
230 DBG("element %p name %s", element, element->name);
232 connman_element_get_value(element,
233 CONNMAN_PROPERTY_ID_IPV4_TIMESERVER, ×erver);
235 connman_timeserver_remove(timeserver);
240 static struct connman_driver ipv4_driver = {
242 .type = CONNMAN_ELEMENT_TYPE_IPV4,
243 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
245 .remove = ipv4_remove,
248 int __connman_ipv4_init(void)
250 return connman_driver_register(&ipv4_driver);
253 void __connman_ipv4_cleanup(void)
255 connman_driver_unregister(&ipv4_driver);