From: Gustavo F. Padovan <[email protected]>
---
Makefile.am | 4 +-
tools/private-network-test.c | 229 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 232 insertions(+), 1 deletions(-)
create mode 100644 tools/private-network-test.c
diff --git a/Makefile.am b/Makefile.am
index 23805e7..fb7f8df 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -141,7 +141,7 @@ noinst_PROGRAMS += tools/wispr tools/wifi-scan
tools/supplicant-test \
tools/addr-test tools/web-test tools/resolv-test \
tools/dbus-test tools/polkit-test \
tools/iptables-test tools/tap-test tools/wpad-test \
- tools/stats-tool
+ tools/stats-tool tools/private-network-test
tools_wispr_SOURCES = $(gweb_sources) tools/wispr.c
tools_wispr_LDADD = @GLIB_LIBS@ @GNUTLS_LIBS@ -lresolv
@@ -176,6 +176,8 @@ tools_dbus_test_LDADD = @GLIB_LIBS@ @DBUS_LIBS@
tools_polkit_test_LDADD = @DBUS_LIBS@
tools_iptables_test_LDADD = @GLIB_LIBS@ @XTABLES_LIBS@
+
+tools_private_network_test_LDADD = @GLIB_LIBS@ @DBUS_LIBS@
endif
test_scripts = test/get-state test/list-profiles test/list-services \
diff --git a/tools/private-network-test.c b/tools/private-network-test.c
new file mode 100644
index 0000000..8bcf4c1
--- /dev/null
+++ b/tools/private-network-test.c
@@ -0,0 +1,229 @@
+/*
+ *
+ * oFono - Open Source Telephony
+ *
+ * Copyright (C) 2011 Intel Corporation. All rights reserved.
+ * Copyright (C) 2011 ProFUSION embedded systems
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#define CONNMAN_SERVICE "net.connman"
+
+#define MANAGER_PATH "/"
+#define MANAGER_INTERFACE CONNMAN_SERVICE ".Manager"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <signal.h>
+#include <sys/poll.h>
+#include <sys/signalfd.h>
+#include <unistd.h>
+
+#include <dbus/dbus.h>
+
+#ifndef DBUS_TYPE_UNIX_FD
+#define DBUS_TYPE_UNIX_FD -1
+#endif
+
+static int release_private_network(DBusConnection *conn)
+{
+ DBusMessage *msg, *reply;
+ DBusError error;
+
+ msg = dbus_message_new_method_call(CONNMAN_SERVICE, MANAGER_PATH,
+ MANAGER_INTERFACE, "ReleasePrivateNetwork");
+
+ dbus_error_init(&error);
+
+ printf("Releasing private-network...\n");
+ reply = dbus_connection_send_with_reply_and_block(conn, msg, -1,
+ &error);
+ dbus_message_unref(msg);
+
+ if (!reply) {
+ if (dbus_error_is_set(&error)) {
+ fprintf(stderr, "1. %s\n", error.message);
+ dbus_error_free(&error);
+ } else {
+ fprintf(stderr, "Release() failed");
+ }
+
+ return -1;
+ }
+
+ return 0;
+}
+
+static void request_private_network(DBusConnection *conn, int *out_fd,
+ char **out_server_ip,
+ char **out_peer_ip,
+ char **out_primary_dns,
+ char **out_secondary_dns)
+{
+ DBusMessage *msg, *reply;
+ DBusError error;
+
+ msg = dbus_message_new_method_call(CONNMAN_SERVICE, MANAGER_PATH,
+ MANAGER_INTERFACE, "RequestPrivateNetwork");
+
+ dbus_error_init(&error);
+
+ printf("Requesting private-network...\n");
+ reply = dbus_connection_send_with_reply_and_block(conn, msg, -1,
+ &error);
+ dbus_message_unref(msg);
+
+ if (!reply) {
+ if (dbus_error_is_set(&error)) {
+ fprintf(stderr, "1. %s\n", error.message);
+ dbus_error_free(&error);
+ } else {
+ fprintf(stderr, "Request() failed");
+ }
+
+ return;
+ }
+
+ dbus_error_init(&error);
+
+ if (dbus_message_get_args(reply, &error, DBUS_TYPE_UNIX_FD, out_fd,
+ DBUS_TYPE_STRING, out_server_ip,
+ DBUS_TYPE_STRING, out_peer_ip,
+ DBUS_TYPE_STRING, out_primary_dns,
+ DBUS_TYPE_STRING, out_secondary_dns,
+ DBUS_TYPE_INVALID) == FALSE) {
+ fprintf(stderr, "2. %s\n", error.message);
+ dbus_error_free(&error);
+
+ return;
+ }
+
+ printf("Using fd=%d\n", *out_fd);
+ printf("Using server IP:%s\n", *out_server_ip);
+ printf("Using peer IP:%s\n", *out_peer_ip);
+ printf("Using primary dns=%s\n", *out_primary_dns);
+ printf("Using secondary dns=%s\n", *out_secondary_dns);
+
+
+ return;
+}
+
+int main(int argc, char *argv[])
+{
+ DBusConnection *conn;
+ int fd = -1;
+ char *server_ip;
+ char *peer_ip;
+ char *primary_dns;
+ char *secondary_dns;
+
+ /*
+ * IP packet: src: 192.168.219.2 dst www.connman.net
+ * HTTP GET / request
+ */
+ int buf[81] = { 0x45, 0x00, 0x00, 0x51, 0x5a, 0xbe, 0x00, 0x00, 0x40,
+ 0x06, 0x50, 0x73, 0xc0, 0xa8, 0xdb, 0x01, 0x3e, 0x4b,
+ 0xf5, 0x80, 0x30, 0x3b, 0x00, 0x50, 0x00, 0x00, 0x00,
+ 0x28, 0x04, 0xfd, 0xac, 0x9b, 0x50, 0x18, 0x02, 0x00,
+ 0xa1, 0xb3, 0x00, 0x00, 0x47, 0x45, 0x54, 0x20, 0x2f,
+ 0x20, 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31,
+ 0x0d, 0x0a, 0x48, 0x6f, 0x73, 0x74, 0x3a, 0x20, 0x77,
+ 0x77, 0x77, 0x2e, 0x63, 0x6f, 0x6e, 0x6e, 0x6d, 0x61,
+ 0x6e, 0x2e, 0x6e, 0x65, 0x74, 0x0d, 0x0a, 0x0d, 0x0a};
+
+
+ int buf2[81] = { 0x45, 0x00, 0x00, 0x51, 0x57, 0x9d, 0x00, 0x00, 0x40,
+ 0x06, 0x53, 0x93, 0xc0, 0xa8, 0xdb, 0x02, 0x3e, 0x4b,
+ 0xf5, 0x80, 0x30, 0x3b, 0x00, 0x50, 0x00, 0x00, 0x00,
+ 0x28, 0x17, 0xdb, 0x2e, 0x6d, 0x50, 0x18, 0x02, 0x00,
+ 0x0d, 0x03, 0x00, 0x00, 0x47, 0x45, 0x54, 0x20, 0x2f,
+ 0x20, 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31,
+ 0x0d, 0x0a, 0x48, 0x6f, 0x73, 0x74, 0x3a, 0x20, 0x77,
+ 0x77, 0x77, 0x2e, 0x63, 0x6f, 0x6e, 0x6e, 0x6d, 0x61,
+ 0x6e, 0x2e, 0x6e, 0x65, 0x74, 0x0d, 0x0a, 0x0d, 0x0a};
+
+ if (DBUS_TYPE_UNIX_FD < 0) {
+ fprintf(stderr, "File-descriptor passing not supported\n");
+ exit(1);
+ }
+
+ conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
+ if (!conn) {
+ fprintf(stderr, "Can't get on system bus\n");
+ exit(1);
+ }
+
+ request_private_network(conn, &fd, &server_ip, &peer_ip,
+ &primary_dns, &secondary_dns);
+ if (fd < 0)
+ return -1;
+
+ fcntl(fd, F_SETFL, O_NONBLOCK);
+
+ printf("Press ENTER to write data to the network.\n");
+ getchar();
+
+
+ if (!fork()) {
+ if (write(fd, buf, 81) < 0) {
+ fprintf(stderr, "err on write()");
+ return -1;
+ }
+
+ if (write(fd, buf2, 81) < 0) {
+ fprintf(stderr, "err on write()");
+ return -1;
+ }
+
+ printf("Press ENTER to release private network.\n");
+ getchar();
+
+ if (release_private_network(conn) < 0)
+ return -1;
+
+ close(fd);
+
+ dbus_connection_unref(conn);
+
+ } else {
+ struct pollfd p;
+ char buf[1500];
+ int len;
+
+ p.fd = fd;
+ p.events = POLLIN | POLLERR | POLLHUP;
+
+ while (1) {
+ p.revents = 0;
+ if (poll(&p, 1, -1) <= 0)
+ return -1;
+
+ if (p.revents & (POLLERR | POLLHUP))
+ return -1;
+
+ len = read(fd, buf, sizeof(buf));
+ if (len < 0)
+ return -1;
+
+ printf("%d bytes received\n", len);
+ }
+ }
+
+ return 0;
+}
--
1.7.1
_______________________________________________
connman mailing list
[email protected]
http://lists.connman.net/listinfo/connman