5 * Copyright (C) 2012 Intel Corporation. All rights reserved.
6 * Copyright (C) 2011 ProFUSION embedded systems
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.
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.
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
23 #define CONNMAN_SERVICE "net.connman"
25 #define MANAGER_PATH "/"
26 #define MANAGER_INTERFACE CONNMAN_SERVICE ".Manager"
36 #include <sys/signalfd.h>
39 #include <dbus/dbus.h>
41 #ifndef DBUS_TYPE_UNIX_FD
42 #define DBUS_TYPE_UNIX_FD -1
45 static int release_private_network(DBusConnection *conn)
47 DBusMessage *msg, *reply;
50 msg = dbus_message_new_method_call(CONNMAN_SERVICE, MANAGER_PATH,
51 MANAGER_INTERFACE, "ReleasePrivateNetwork");
53 dbus_error_init(&error);
55 printf("Releasing private-network...\n");
56 reply = dbus_connection_send_with_reply_and_block(conn, msg, -1,
58 dbus_message_unref(msg);
61 if (dbus_error_is_set(&error)) {
62 fprintf(stderr, "1. %s\n", error.message);
63 dbus_error_free(&error);
65 fprintf(stderr, "Release() failed");
74 static void request_private_network(DBusConnection *conn, int *out_fd,
77 char **out_primary_dns,
78 char **out_secondary_dns)
80 DBusMessageIter array, dict, entry;
81 DBusMessage *msg, *reply;
84 msg = dbus_message_new_method_call(CONNMAN_SERVICE, MANAGER_PATH,
85 MANAGER_INTERFACE, "RequestPrivateNetwork");
87 dbus_error_init(&error);
89 printf("Requesting private-network...\n");
90 reply = dbus_connection_send_with_reply_and_block(conn, msg, -1,
92 dbus_message_unref(msg);
95 if (dbus_error_is_set(&error)) {
96 fprintf(stderr, "1. %s\n", error.message);
97 dbus_error_free(&error);
99 fprintf(stderr, "Request() failed");
104 if (!dbus_message_iter_init(reply, &array))
107 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_UNIX_FD)
110 dbus_message_iter_get_basic(&array, out_fd);
111 printf("Fildescriptor = %d\n", *out_fd);
113 dbus_message_iter_next(&array);
115 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
118 dbus_message_iter_recurse(&array, &dict);
120 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
121 DBusMessageIter iter;
125 dbus_message_iter_recurse(&dict, &entry);
127 dbus_message_iter_get_basic(&entry, &key);
129 printf("key %s", key);
131 dbus_message_iter_next(&entry);
132 dbus_message_iter_recurse(&entry, &iter);
134 type = dbus_message_iter_get_arg_type(&iter);
135 if (type != DBUS_TYPE_STRING)
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);
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);
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);
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);
159 dbus_message_iter_next(&dict);
163 dbus_message_unref(reply);
166 int main(int argc, char *argv[])
168 DBusConnection *conn;
176 * IP packet: src: 192.168.219.2 dst www.connman.net
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};
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};
200 if (DBUS_TYPE_UNIX_FD < 0) {
201 fprintf(stderr, "File-descriptor passing not supported\n");
205 conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
207 fprintf(stderr, "Can't get on system bus\n");
211 request_private_network(conn, &fd, &server_ip, &peer_ip,
212 &primary_dns, &secondary_dns);
216 fcntl(fd, F_SETFL, O_NONBLOCK);
218 printf("Press ENTER to write data to the network.\n");
223 if (write(fd, buf1, sizeof(buf1)) < 0) {
224 fprintf(stderr, "err on write() buf1\n");
228 if (write(fd, buf2, sizeof(buf2)) < 0) {
229 fprintf(stderr, "err on write() buf2\n");
233 printf("Press ENTER to release private network.\n");
236 if (release_private_network(conn) < 0)
241 dbus_connection_unref(conn);
249 p.events = POLLIN | POLLERR | POLLHUP;
253 if (poll(&p, 1, -1) <= 0)
256 if (p.revents & (POLLERR | POLLHUP))
259 len = read(fd, buf, sizeof(buf));
263 printf("%d bytes received\n", len);