Remove obsolete file.
[connman] / plugins / udhcp.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  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 <stdio.h>
27 #include <unistd.h>
28
29 #define CONNMAN_API_SUBJECT_TO_CHANGE
30 #include <connman/plugin.h>
31 #include <connman/driver.h>
32 #include <connman/inet.h>
33 #include <connman/dbus.h>
34 #include <connman/log.h>
35
36 #include "task.h"
37
38 #define UDHCPC_INTF  "net.busybox.udhcpc"
39 #define UDHCPC_PATH  "/net/busybox/udhcpc"
40
41 static int udhcp_probe(struct connman_element *element)
42 {
43         struct task_data *task;
44         char *argv[9], *envp[2], *ifname;
45         char pidfile[PATH_MAX], script[PATH_MAX];
46
47         DBG("element %p name %s", element, element->name);
48
49         if (access(UDHCPC, X_OK) < 0)
50                 return -errno;
51
52         ifname = connman_inet_ifname(element->index);
53         if (ifname == NULL)
54                 return -ENOMEM;
55
56         snprintf(pidfile, sizeof(pidfile) - 1,
57                                 "%s/udhcpc.%s.pid", STATEDIR, ifname);
58         snprintf(script, sizeof(script) - 1, "%s/udhcpc-script", SCRIPTDIR);
59
60         argv[0] = UDHCPC;
61         argv[1] = "-f";
62         argv[2] = "-i";
63         argv[3] = ifname;
64         argv[4] = "-p";
65         argv[5] = pidfile;
66         argv[6] = "-s";
67         argv[7] = script;
68         argv[8] = NULL;
69
70         envp[0] = NULL;
71
72         task = task_spawn(element->index, argv, envp, NULL, element);
73         if (task == NULL) {
74                 g_free(ifname);
75                 return -EIO;
76         }
77
78         g_free(ifname);
79
80         return 0;
81 }
82
83 static void udhcp_remove(struct connman_element *element)
84 {
85         struct task_data *task;
86
87         DBG("element %p name %s", element, element->name);
88
89         task = task_find_by_index(element->index);
90         if (task == NULL)
91                 return;
92
93         task_kill(task);
94 }
95
96 static struct connman_driver udhcp_driver = {
97         .name           = "udhcp",
98         .type           = CONNMAN_ELEMENT_TYPE_DHCP,
99         .priority       = CONNMAN_DRIVER_PRIORITY_HIGH,
100         .probe          = udhcp_probe,
101         .remove         = udhcp_remove,
102 };
103
104 static void udhcp_bound(DBusMessage *msg, gboolean renew)
105 {
106         struct task_data *task;
107         struct connman_element *element, *parent;
108         const char *interface, *address, *netmask, *broadcast, *gateway, *dns;
109         int index;
110
111         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &interface,
112                                         DBUS_TYPE_STRING, &address,
113                                         DBUS_TYPE_STRING, &netmask,
114                                         DBUS_TYPE_STRING, &broadcast,
115                                         DBUS_TYPE_STRING, &gateway,
116                                         DBUS_TYPE_STRING, &dns,
117                                                         DBUS_TYPE_INVALID);
118
119         DBG("%s ==> address %s gateway %s", interface, address, gateway);
120
121         index = connman_inet_ifindex(interface);
122         if (index < 0)
123                 return;
124
125         task = task_find_by_index(index);
126         if (task == NULL)
127                 return;
128
129         parent = task_get_data(task);
130         if (parent == NULL)
131                 return;
132
133         g_free(parent->ipv4.address);
134         parent->ipv4.address = g_strdup(address);
135
136         g_free(parent->ipv4.netmask);
137         parent->ipv4.netmask = g_strdup(netmask);
138
139         g_free(parent->ipv4.broadcast);
140         parent->ipv4.broadcast = g_strdup(broadcast);
141
142         g_free(parent->ipv4.gateway);
143         parent->ipv4.gateway = g_strdup(gateway);
144
145         g_free(parent->ipv4.nameserver);
146         parent->ipv4.nameserver = g_strdup(dns);
147
148         connman_element_update(parent);
149
150         if (renew == TRUE)
151                 return;
152
153         element = connman_element_create(NULL);
154         if (element == NULL)
155                 return;
156
157         element->type = CONNMAN_ELEMENT_TYPE_IPV4;
158         element->index = index;
159
160         if (connman_element_register(element, parent) < 0)
161                 connman_element_unref(element);
162 }
163
164 static DBusHandlerResult udhcp_filter(DBusConnection *conn,
165                                                 DBusMessage *msg, void *data)
166 {
167         if (dbus_message_is_method_call(msg, UDHCPC_INTF, "bound") == TRUE) {
168                 udhcp_bound(msg, FALSE);
169                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
170         }
171
172         if (dbus_message_is_method_call(msg, UDHCPC_INTF, "renew") == TRUE) {
173                 udhcp_bound(msg, TRUE);
174                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
175         }
176
177         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
178 }
179
180 static DBusConnection *connection;
181
182 static const char *udhcp_rule = "path=" UDHCPC_PATH ",interface=" UDHCPC_INTF;
183
184 static int udhcp_init(void)
185 {
186         int err;
187
188         connection = connman_dbus_get_connection();
189
190         dbus_connection_add_filter(connection, udhcp_filter, NULL, NULL);
191
192         dbus_bus_add_match(connection, udhcp_rule, NULL);
193
194         err = connman_driver_register(&udhcp_driver);
195         if (err < 0) {
196                 dbus_connection_unref(connection);
197                 return err;
198         }
199
200         return 0;
201 }
202
203 static void udhcp_exit(void)
204 {
205         connman_driver_unregister(&udhcp_driver);
206
207         dbus_bus_remove_match(connection, udhcp_rule, NULL);
208
209         dbus_connection_remove_filter(connection, udhcp_filter, NULL);
210
211         dbus_connection_unref(connection);
212 }
213
214 CONNMAN_PLUGIN_DEFINE(udhcp, "uDHCP client plugin", VERSION,
215                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, udhcp_init, udhcp_exit)