5 * Copyright (C) 2007-2009 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>
34 #define CONNMAN_API_SUBJECT_TO_CHANGE
35 #include <connman/plugin.h>
36 #include <connman/driver.h>
37 #include <connman/resolver.h>
38 #include <connman/rtnl.h>
39 #include <connman/log.h>
43 enum connman_ipv4_method {
44 CONNMAN_IPV4_METHOD_UNKNOWN = 0,
45 CONNMAN_IPV4_METHOD_OFF = 1,
46 CONNMAN_IPV4_METHOD_STATIC = 2,
47 CONNMAN_IPV4_METHOD_DHCP = 3,
51 enum connman_ipv4_method method;
52 struct in_addr address;
53 struct in_addr netmask;
54 struct in_addr broadcast;
57 static int set_ipv4(struct connman_element *element,
58 struct connman_ipv4 *ipv4, const char *nameserver)
61 struct sockaddr_in *addr;
64 DBG("element %p ipv4 %p", element, ipv4);
66 sk = socket(PF_INET, SOCK_DGRAM, 0);
70 memset(&ifr, 0, sizeof(ifr));
71 ifr.ifr_ifindex = element->index;
73 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
78 DBG("ifname %s", ifr.ifr_name);
80 addr = (struct sockaddr_in *) &ifr.ifr_addr;
81 addr->sin_family = AF_INET;
82 addr->sin_addr = ipv4->address;
84 err = ioctl(sk, SIOCSIFADDR, &ifr);
87 DBG("address setting failed (%s)", strerror(errno));
89 addr = (struct sockaddr_in *) &ifr.ifr_netmask;
90 addr->sin_family = AF_INET;
91 addr->sin_addr = ipv4->netmask;
93 err = ioctl(sk, SIOCSIFNETMASK, &ifr);
96 DBG("netmask setting failed (%s)", strerror(errno));
98 addr = (struct sockaddr_in *) &ifr.ifr_broadaddr;
99 addr->sin_family = AF_INET;
100 addr->sin_addr = ipv4->broadcast;
102 err = ioctl(sk, SIOCSIFBRDADDR, &ifr);
105 DBG("broadcast setting failed (%s)", strerror(errno));
109 connman_resolver_append(ifr.ifr_name, NULL, nameserver);
114 static int clear_ipv4(struct connman_element *element)
117 struct sockaddr_in *addr;
120 DBG("element %p", element);
122 sk = socket(PF_INET, SOCK_DGRAM, 0);
126 memset(&ifr, 0, sizeof(ifr));
127 ifr.ifr_ifindex = element->index;
129 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
134 DBG("ifname %s", ifr.ifr_name);
136 connman_resolver_remove_all(ifr.ifr_name);
138 addr = (struct sockaddr_in *) &ifr.ifr_addr;
139 addr->sin_family = AF_INET;
140 addr->sin_addr.s_addr = INADDR_ANY;
142 //err = ioctl(sk, SIOCDIFADDR, &ifr);
143 err = ioctl(sk, SIOCSIFADDR, &ifr);
147 if (err < 0 && errno != EADDRNOTAVAIL) {
148 DBG("address removal failed (%s)", strerror(errno));
155 static int ipv4_probe(struct connman_element *element)
157 struct connman_element *connection;
158 struct connman_ipv4 ipv4;
159 const char *address = NULL, *netmask = NULL, *broadcast = NULL;
160 const char *nameserver = NULL;
162 DBG("element %p name %s", element, element->name);
164 connman_element_get_value(element,
165 CONNMAN_PROPERTY_ID_IPV4_ADDRESS, &address);
166 connman_element_get_value(element,
167 CONNMAN_PROPERTY_ID_IPV4_NETMASK, &netmask);
168 connman_element_get_value(element,
169 CONNMAN_PROPERTY_ID_IPV4_BROADCAST, &broadcast);
171 connman_element_get_value(element,
172 CONNMAN_PROPERTY_ID_IPV4_NAMESERVER, &nameserver);
174 DBG("address %s", address);
175 DBG("netmask %s", netmask);
176 DBG("broadcast %s", broadcast);
178 if (address == NULL || netmask == NULL)
181 memset(&ipv4, 0, sizeof(ipv4));
182 ipv4.address.s_addr = inet_addr(address);
183 ipv4.netmask.s_addr = inet_addr(netmask);
184 ipv4.broadcast.s_addr = inet_addr(broadcast);
186 set_ipv4(element, &ipv4, nameserver);
188 connection = connman_element_create(NULL);
190 connection->type = CONNMAN_ELEMENT_TYPE_CONNECTION;
191 connection->index = element->index;
192 connection->devname = inet_index2name(element->index);
194 if (connman_element_register(connection, element) < 0)
195 connman_element_unref(connection);
200 static void ipv4_remove(struct connman_element *element)
202 DBG("element %p name %s", element, element->name);
207 static struct connman_driver ipv4_driver = {
209 .type = CONNMAN_ELEMENT_TYPE_IPV4,
211 .remove = ipv4_remove,
214 static int ipv4_init(void)
216 return connman_driver_register(&ipv4_driver);
219 static void ipv4_exit(void)
221 connman_driver_unregister(&ipv4_driver);
224 CONNMAN_PLUGIN_DEFINE(ipv4, "IPv4 configuration plugin", VERSION,
225 ipv4_init, ipv4_exit)