Fix PASSED_TO_PROC_AFTER_FREE.EX
[platform/upstream/connman.git] / tools / private-network-test.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2012  Intel Corporation. All rights reserved.
6  *  Copyright (C) 2011  ProFUSION embedded systems
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 #define CONNMAN_SERVICE "net.connman"
24
25 #define MANAGER_PATH    "/"
26 #define MANAGER_INTERFACE CONNMAN_SERVICE ".Manager"
27
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <stdio.h>
31 #include <gdbus.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <signal.h>
35 #include <poll.h>
36 #include <sys/signalfd.h>
37 #include <unistd.h>
38
39 #include <dbus/dbus.h>
40
41 #ifndef DBUS_TYPE_UNIX_FD
42 #define DBUS_TYPE_UNIX_FD -1
43 #endif
44
45 static int release_private_network(DBusConnection *conn)
46 {
47         DBusMessage *msg, *reply;
48         DBusError error;
49
50         msg = dbus_message_new_method_call(CONNMAN_SERVICE, MANAGER_PATH,
51                                 MANAGER_INTERFACE, "ReleasePrivateNetwork");
52
53         dbus_error_init(&error);
54
55         printf("Releasing private-network...\n");
56         reply = dbus_connection_send_with_reply_and_block(conn, msg, -1,
57                                                                         &error);
58         dbus_message_unref(msg);
59
60         if (!reply) {
61                 if (dbus_error_is_set(&error)) {
62                         fprintf(stderr, "1. %s\n", error.message);
63                         dbus_error_free(&error);
64                 } else {
65                         fprintf(stderr, "Release() failed");
66                 }
67
68                 return -1;
69         }
70
71         return 0;
72 }
73
74 static void request_private_network(DBusConnection *conn, int *out_fd,
75                                         char **out_server_ip,
76                                         char **out_peer_ip,
77                                         char **out_primary_dns,
78                                         char **out_secondary_dns)
79 {
80         DBusMessageIter array, dict, entry;
81         DBusMessage *msg, *reply;
82         DBusError error;
83
84         msg = dbus_message_new_method_call(CONNMAN_SERVICE, MANAGER_PATH,
85                                 MANAGER_INTERFACE, "RequestPrivateNetwork");
86
87         dbus_error_init(&error);
88
89         printf("Requesting private-network...\n");
90         reply = dbus_connection_send_with_reply_and_block(conn, msg, -1,
91                                                                         &error);
92         dbus_message_unref(msg);
93
94         if (!reply) {
95                 if (dbus_error_is_set(&error)) {
96                         fprintf(stderr, "1. %s\n", error.message);
97                         dbus_error_free(&error);
98                 } else {
99                         fprintf(stderr, "Request() failed");
100                 }
101                 return;
102         }
103
104         if (!dbus_message_iter_init(reply, &array))
105                 goto done;
106
107         if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_UNIX_FD)
108                 goto done;
109
110         dbus_message_iter_get_basic(&array, out_fd);
111         printf("Fildescriptor = %d\n", *out_fd);
112
113         dbus_message_iter_next(&array);
114
115         if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
116                 goto done;
117
118         dbus_message_iter_recurse(&array, &dict);
119
120         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
121                 DBusMessageIter iter;
122                 const char *key;
123                 int type;
124
125                 dbus_message_iter_recurse(&dict, &entry);
126
127                 dbus_message_iter_get_basic(&entry, &key);
128
129                 printf("key %s", key);
130
131                 dbus_message_iter_next(&entry);
132                 dbus_message_iter_recurse(&entry, &iter);
133
134                 type = dbus_message_iter_get_arg_type(&iter);
135                 if (type != DBUS_TYPE_STRING)
136                         break;
137
138                 if (g_str_equal(key, "ServerIPv4")
139                                 && type == DBUS_TYPE_STRING) {
140                         dbus_message_iter_get_basic(&iter, out_server_ip);
141                         printf(" = %s\n", *out_server_ip);
142
143                 } else if (g_str_equal(key, "PeerIPv4")
144                                 && type == DBUS_TYPE_STRING) {
145                         dbus_message_iter_get_basic(&iter, out_peer_ip);
146                         printf(" = %s\n", *out_peer_ip);
147
148                 } else if (g_str_equal(key, "PrimaryDNS")
149                                 && type == DBUS_TYPE_STRING) {
150                         dbus_message_iter_get_basic(&iter, out_primary_dns);
151                         printf(" = %s\n", *out_primary_dns);
152
153                 } else if (g_str_equal(key, "SecondaryDNS")
154                                 && type == DBUS_TYPE_STRING) {
155                         dbus_message_iter_get_basic(&iter, out_secondary_dns);
156                         printf(" = %s\n", *out_secondary_dns);
157                 }
158
159                 dbus_message_iter_next(&dict);
160         }
161
162 done:
163         dbus_message_unref(reply);
164 }
165
166 int main(int argc, char *argv[])
167 {
168         DBusConnection *conn;
169         int fd = -1;
170         char *server_ip;
171         char *peer_ip;
172         char *primary_dns;
173         char *secondary_dns;
174
175         /*
176          * IP packet: src: 192.168.219.2 dst www.connman.net
177          * HTTP GET / request
178          */
179         int buf1[81] = { 0x45, 0x00, 0x00, 0x51, 0x5a, 0xbe, 0x00, 0x00, 0x40,
180                          0x06, 0x50, 0x73, 0xc0, 0xa8, 0xdb, 0x01, 0x3e, 0x4b,
181                          0xf5, 0x80, 0x30, 0x3b, 0x00, 0x50, 0x00, 0x00, 0x00,
182                          0x28, 0x04, 0xfd, 0xac, 0x9b, 0x50, 0x18, 0x02, 0x00,
183                          0xa1, 0xb3, 0x00, 0x00, 0x47, 0x45, 0x54, 0x20, 0x2f,
184                          0x20, 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31,
185                          0x0d, 0x0a, 0x48, 0x6f, 0x73, 0x74, 0x3a, 0x20, 0x77,
186                          0x77, 0x77, 0x2e, 0x63, 0x6f, 0x6e, 0x6e, 0x6d, 0x61,
187                          0x6e, 0x2e, 0x6e, 0x65, 0x74, 0x0d, 0x0a, 0x0d, 0x0a};
188
189
190         int buf2[81] = { 0x45, 0x00, 0x00, 0x51, 0x57, 0x9d, 0x00, 0x00, 0x40,
191                          0x06, 0x53, 0x93, 0xc0, 0xa8, 0xdb, 0x02, 0x3e, 0x4b,
192                          0xf5, 0x80, 0x30, 0x3b, 0x00, 0x50, 0x00, 0x00, 0x00,
193                          0x28, 0x17, 0xdb, 0x2e, 0x6d, 0x50, 0x18, 0x02, 0x00,
194                          0x0d, 0x03, 0x00, 0x00, 0x47, 0x45, 0x54, 0x20, 0x2f,
195                          0x20, 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31,
196                          0x0d, 0x0a, 0x48, 0x6f, 0x73, 0x74, 0x3a, 0x20, 0x77,
197                          0x77, 0x77, 0x2e, 0x63, 0x6f, 0x6e, 0x6e, 0x6d, 0x61,
198                          0x6e, 0x2e, 0x6e, 0x65, 0x74, 0x0d, 0x0a, 0x0d, 0x0a};
199
200         if (DBUS_TYPE_UNIX_FD < 0) {
201                 fprintf(stderr, "File-descriptor passing not supported\n");
202                 exit(1);
203         }
204
205         conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
206         if (!conn) {
207                 fprintf(stderr, "Can't get on system bus\n");
208                 exit(1);
209         }
210
211         request_private_network(conn, &fd, &server_ip, &peer_ip,
212                                         &primary_dns, &secondary_dns);
213         if (fd < 0)
214                 return -1;
215
216         fcntl(fd, F_SETFL, O_NONBLOCK);
217
218         printf("Press ENTER to write data to the network.\n");
219         getchar();
220
221
222         if (!fork()) {
223                 if (write(fd, buf1, sizeof(buf1)) < 0) {
224                         fprintf(stderr, "err on write() buf1\n");
225                         return -1;
226                 }
227
228                 if (write(fd, buf2, sizeof(buf2)) < 0) {
229                         fprintf(stderr, "err on write() buf2\n");
230                         return -1;
231                 }
232
233                 printf("Press ENTER to release private network.\n");
234                 getchar();
235
236                 if (release_private_network(conn) < 0)
237                         return -1;
238
239                 close(fd);
240
241                 dbus_connection_unref(conn);
242
243         } else {
244                 struct pollfd p;
245                 char buf[1500];
246                 int len;
247
248                 p.fd = fd;
249                 p.events = POLLIN | POLLERR | POLLHUP;
250
251                 while (1) {
252                         p.revents = 0;
253                         if (poll(&p, 1, -1) <= 0)
254                                 return -1;
255
256                         if (p.revents & (POLLERR | POLLHUP))
257                                 return -1;
258
259                         len = read(fd, buf, sizeof(buf));
260                         if (len < 0)
261                                 return -1;
262
263                         printf("%d bytes received\n", len);
264                 }
265         }
266
267         return 0;
268 }