Use nl80211/cfg80211 WiFi configuration by default
[framework/connectivity/connman.git] / scripts / dhclient-script.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 <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <dbus/dbus.h>
31
32 extern char **environ;
33
34 static void append(DBusMessageIter *dict, const char *pattern)
35 {
36         DBusMessageIter entry;
37         const char *key, *value;
38         char *delim;
39
40         delim = strchr(pattern, '=');
41         *delim = '\0';
42
43         key = pattern;
44         value = delim + 1;
45
46         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
47                                                         NULL, &entry);
48
49         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
50
51         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &value);
52
53         dbus_message_iter_close_container(dict, &entry);
54 }
55
56 int main(int argc, char *argv[])
57 {
58         DBusConnection *conn;
59         DBusError error;
60         DBusMessage *msg;
61         DBusMessageIter iter, dict;
62         dbus_uint32_t pid;
63         char **envp, *busname, *busintf, *buspath, *reason, *interface;
64
65         busname = getenv("BUSNAME");
66         busintf = getenv("BUSINTF");
67         buspath = getenv("BUSPATH");
68
69         if (busname == NULL || busintf == NULL || buspath == NULL)
70                 return 0;
71
72         pid = atoi(getenv("pid"));
73         reason = getenv("reason");
74         interface = getenv("interface");
75
76         if (pid == 0 || reason == NULL)
77                 return 0;
78
79         dbus_error_init(&error);
80
81         conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
82         if (conn == NULL) {
83                 if (dbus_error_is_set(&error) == TRUE) {
84                         fprintf(stderr, "%s\n", error.message);
85                         dbus_error_free(&error);
86                 } else
87                         fprintf(stderr, "Failed to get on system bus\n");
88                 return 0;
89         }
90
91         msg = dbus_message_new_method_call(busname, buspath,
92                                                         busintf, "Notify");
93         if (msg == NULL) {
94                 dbus_connection_unref(conn);
95                 fprintf(stderr, "Failed to allocate method call\n");
96                 return 0;
97         }
98
99         dbus_message_set_no_reply(msg, TRUE);
100
101         dbus_message_append_args(msg, DBUS_TYPE_UINT32, &pid,
102                                 DBUS_TYPE_STRING, &reason, DBUS_TYPE_INVALID);
103
104         dbus_message_iter_init_append(msg, &iter);
105
106         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
107                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
108                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_STRING_AS_STRING
109                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
110
111         for (envp = environ; envp && *envp; envp++) {
112                 if (strlen(*envp) < 5)
113                         continue;
114
115                 if (strncmp(*envp, "new_", 4) == 0 ||
116                                 strncmp(*envp, "old_", 4) == 0 ||
117                                         strncmp(*envp, "alia", 4) == 0)
118                         append(&dict, *envp);
119         }
120
121         dbus_message_iter_close_container(&iter, &dict);
122
123         if (dbus_connection_send(conn, msg, NULL) == FALSE)
124                 fprintf(stderr, "Failed to send message\n");
125
126         dbus_connection_flush(conn);
127
128         dbus_message_unref(msg);
129
130         dbus_connection_unref(conn);
131
132         return 0;
133 }