c7a7ae21c84d8cddcac91c7d12c61635f4d55a3d
[connman] / scripts / udhcpc-script.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2008  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 <stdlib.h>
28 #include <string.h>
29
30 #include <dbus/dbus.h>
31
32 #define UDHCPC_INTF  "org.busybox.udhcpc"
33 #define UDHCPC_PATH  "/org/busybox/udhcpc"
34
35 int main(int argc, char *argv[])
36 {
37         DBusConnection *conn;
38         DBusError error;
39         DBusMessage *msg;
40         char *busname, *interface, *address, *netmask, *broadcast;
41         char *gateway, *dns;
42
43         if (argc < 2)
44                 return 0;
45
46         if (strcmp(argv[1], "bound") != 0 && strcmp(argv[1], "renew") != 0)
47                 return 0;
48
49         busname = "org.moblin.connman";
50
51         interface = getenv("interface");
52
53         address = getenv("ip");
54         netmask = getenv("subnet");
55         broadcast = getenv("broadcast");
56         gateway = getenv("router");
57         dns = getenv("dns");
58
59         dbus_error_init(&error);
60
61         conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
62         if (conn == NULL) {
63                 if (dbus_error_is_set(&error) == TRUE) {
64                         fprintf(stderr, "%s\n", error.message);
65                         dbus_error_free(&error);
66                 } else
67                         fprintf(stderr, "Failed to get on system bus\n");
68                 return 0;
69         }
70
71         msg = dbus_message_new_method_call(busname, UDHCPC_PATH,
72                                                 UDHCPC_INTF, argv[1]);
73         if (msg == NULL) {
74                 dbus_connection_unref(conn);
75                 fprintf(stderr, "Failed to allocate method call\n");
76                 return 0;
77         }
78
79         dbus_message_set_no_reply(msg, TRUE);
80
81         dbus_message_append_args(msg, DBUS_TYPE_STRING, &interface,
82                                         DBUS_TYPE_STRING, &address,
83                                         DBUS_TYPE_STRING, &netmask,
84                                         DBUS_TYPE_STRING, &broadcast,
85                                         DBUS_TYPE_STRING, &gateway,
86                                         DBUS_TYPE_STRING, &dns,
87                                                         DBUS_TYPE_INVALID);
88
89         if (dbus_connection_send(conn, msg, NULL) == FALSE)
90                 fprintf(stderr, "Failed to send message\n");
91
92         dbus_message_unref(msg);
93
94         dbus_connection_unref(conn);
95
96         return 0;
97 }