Fix regression when removing watches
[framework/connectivity/connman.git] / src / ipv4.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <unistd.h>
27 #include <string.h>
28 #include <sys/ioctl.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <net/if.h>
33
34 #include "connman.h"
35
36 struct connman_ipv4 {
37         enum connman_ipconfig_method method;
38         struct in_addr address;
39         struct in_addr netmask;
40         struct in_addr broadcast;
41 };
42
43 static int set_ipv4(struct connman_element *element,
44                         struct connman_ipv4 *ipv4, const char *nameserver)
45 {
46         struct ifreq ifr;
47         struct sockaddr_in addr;
48         int sk, err;
49
50         DBG("element %p ipv4 %p", element, ipv4);
51
52         sk = socket(PF_INET, SOCK_DGRAM, 0);
53         if (sk < 0)
54                 return -1;
55
56         memset(&ifr, 0, sizeof(ifr));
57         ifr.ifr_ifindex = element->index;
58
59         if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
60                 close(sk);
61                 return -1;
62         }
63
64         DBG("ifname %s", ifr.ifr_name);
65
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));
70
71         err = ioctl(sk, SIOCSIFADDR, &ifr);
72
73         if (err < 0)
74                 DBG("address setting failed (%s)", strerror(errno));
75
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));
80
81         err = ioctl(sk, SIOCSIFNETMASK, &ifr);
82
83         if (err < 0)
84                 DBG("netmask setting failed (%s)", strerror(errno));
85
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));
90
91         err = ioctl(sk, SIOCSIFBRDADDR, &ifr);
92
93         if (err < 0)
94                 DBG("broadcast setting failed (%s)", strerror(errno));
95
96         close(sk);
97
98         if (nameserver == NULL)
99                 connman_error("No nameserver for %s defined", ifr.ifr_name);
100         else
101                 connman_resolver_append(ifr.ifr_name, NULL, nameserver);
102
103         return 0;
104 }
105
106 static int clear_ipv4(struct connman_element *element)
107 {
108         struct ifreq ifr;
109         struct sockaddr_in addr;
110         int sk, err;
111
112         DBG("element %p", element);
113
114         sk = socket(PF_INET, SOCK_DGRAM, 0);
115         if (sk < 0)
116                 return -1;
117
118         memset(&ifr, 0, sizeof(ifr));
119         ifr.ifr_ifindex = element->index;
120
121         if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
122                 close(sk);
123                 return -1;
124         }
125
126         DBG("ifname %s", ifr.ifr_name);
127
128         connman_resolver_remove_all(ifr.ifr_name);
129
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));
134
135         //err = ioctl(sk, SIOCDIFADDR, &ifr);
136         err = ioctl(sk, SIOCSIFADDR, &ifr);
137
138         close(sk);
139
140         if (err < 0 && errno != EADDRNOTAVAIL) {
141                 DBG("address removal failed (%s)", strerror(errno));
142                 return -1;
143         }
144
145         return 0;
146 }
147
148 static char *index2name(int index)
149 {
150         struct ifreq ifr;
151         int sk, err;
152
153         if (index < 0)
154                 return NULL;
155
156         sk = socket(PF_INET, SOCK_DGRAM, 0);
157         if (sk < 0)
158                 return NULL;
159
160         memset(&ifr, 0, sizeof(ifr));
161         ifr.ifr_ifindex = index;
162
163         err = ioctl(sk, SIOCGIFNAME, &ifr);
164
165         close(sk);
166
167         if (err < 0)
168                 return NULL;
169
170         return strdup(ifr.ifr_name);
171 }
172
173 static int ipv4_probe(struct connman_element *element)
174 {
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;
179
180         DBG("element %p name %s", element, element->name);
181
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);
188
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, &timeserver);
193
194         DBG("address %s", address);
195         DBG("netmask %s", netmask);
196         DBG("broadcast %s", broadcast);
197
198         if (address == NULL || netmask == NULL)
199                 return -EINVAL;
200
201         memset(&ipv4, 0, sizeof(ipv4));
202         ipv4.address.s_addr = inet_addr(address);
203         ipv4.netmask.s_addr = inet_addr(netmask);
204         if (broadcast)
205                 ipv4.broadcast.s_addr = inet_addr(broadcast);
206         else
207                 ipv4.broadcast.s_addr = ipv4.address.s_addr |
208                                                 ~ipv4.netmask.s_addr;
209
210         set_ipv4(element, &ipv4, nameserver);
211
212         connman_timeserver_append(timeserver);
213
214         connection = connman_element_create(NULL);
215
216         connection->type    = CONNMAN_ELEMENT_TYPE_CONNECTION;
217         connection->index   = element->index;
218         connection->devname = index2name(element->index);
219
220         if (connman_element_register(connection, element) < 0)
221                 connman_element_unref(connection);
222
223         return 0;
224 }
225
226 static void ipv4_remove(struct connman_element *element)
227 {
228         const char *timeserver = NULL;
229
230         DBG("element %p name %s", element, element->name);
231
232         connman_element_get_value(element,
233                         CONNMAN_PROPERTY_ID_IPV4_TIMESERVER, &timeserver);
234
235         connman_timeserver_remove(timeserver);
236
237         clear_ipv4(element);
238 }
239
240 static struct connman_driver ipv4_driver = {
241         .name           = "ipv4",
242         .type           = CONNMAN_ELEMENT_TYPE_IPV4,
243         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
244         .probe          = ipv4_probe,
245         .remove         = ipv4_remove,
246 };
247
248 int __connman_ipv4_init(void)
249 {
250         return connman_driver_register(&ipv4_driver);
251 }
252
253 void __connman_ipv4_cleanup(void)
254 {
255         connman_driver_unregister(&ipv4_driver);
256 }