dnsproxy: Only one copy of the relevant buffers will be made to a TCP request
[framework/connectivity/connman.git] / scripts / openvpn-script.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
6  *  Copyright (C) 2010  BMW Car IT GmbH. All rights reserved.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2 as
10  *  published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <dbus/dbus.h>
32
33 extern char **environ;
34
35 static void append(DBusMessageIter *dict, const char *pattern)
36 {
37         DBusMessageIter entry;
38         const char *key, *value;
39         char *delim;
40
41         delim = strchr(pattern, '=');
42         *delim = '\0';
43
44         key = pattern;
45         value = delim + 1;
46
47         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
48                                                         NULL, &entry);
49
50         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
51
52         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &value);
53
54         dbus_message_iter_close_container(dict, &entry);
55 }
56
57 int main(int argc, char *argv[])
58 {
59         DBusConnection *conn;
60         DBusError error;
61         DBusMessage *msg;
62         DBusMessageIter iter, dict;
63         char **envp, *busname, *interface, *path, *reason;
64
65         busname = getenv("CONNMAN_BUSNAME");
66         interface = getenv("CONNMAN_INTERFACE");
67         path = getenv("CONNMAN_PATH");
68
69         reason = getenv("script_type");
70
71         if (!busname || !interface || !path || !reason) {
72                 fprintf(stderr, "Required environment variables not set\n");
73                 return 1;
74         }
75         dbus_error_init(&error);
76
77         conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
78         if (conn == NULL) {
79                 if (dbus_error_is_set(&error) == TRUE) {
80                         fprintf(stderr, "%s\n", error.message);
81                         dbus_error_free(&error);
82                 } else
83                         fprintf(stderr, "Failed to get on system bus\n");
84                 return 0;
85         }
86
87         msg = dbus_message_new_method_call(busname, path,
88                                                 interface, "notify");
89         if (msg == NULL) {
90                 dbus_connection_unref(conn);
91                 fprintf(stderr, "Failed to allocate method call\n");
92                 return 0;
93         }
94
95         dbus_message_set_no_reply(msg, TRUE);
96
97         dbus_message_append_args(msg,
98                                  DBUS_TYPE_STRING, &reason,
99                                  DBUS_TYPE_INVALID);
100
101         dbus_message_iter_init_append(msg, &iter);
102
103         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
104                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
105                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_STRING_AS_STRING
106                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
107
108         for (envp = environ; envp && *envp; envp++)
109                 append(&dict, *envp);
110
111         dbus_message_iter_close_container(&iter, &dict);
112
113         if (dbus_connection_send(conn, msg, NULL) == FALSE)
114                 fprintf(stderr, "Failed to send message\n");
115
116         dbus_connection_flush(conn);
117
118         dbus_message_unref(msg);
119
120         dbus_connection_unref(conn);
121
122         return 0;
123 }