browser-wpadebug.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Hotspot 2.0 client - Web browser using wpadebug on Android
  3. * Copyright (c) 2013, Qualcomm Atheros, Inc.
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "utils/eloop.h"
  11. #include "wps/http_server.h"
  12. #include "browser.h"
  13. struct browser_data {
  14. int success;
  15. };
  16. static void browser_timeout(void *eloop_data, void *user_ctx)
  17. {
  18. wpa_printf(MSG_INFO, "Timeout on waiting browser interaction to "
  19. "complete");
  20. eloop_terminate();
  21. }
  22. static void http_req(void *ctx, struct http_request *req)
  23. {
  24. struct browser_data *data = ctx;
  25. struct wpabuf *resp;
  26. const char *url;
  27. int done = 0;
  28. url = http_request_get_uri(req);
  29. wpa_printf(MSG_INFO, "Browser response received: %s", url);
  30. if (os_strcmp(url, "/") == 0) {
  31. data->success = 1;
  32. done = 1;
  33. } else if (os_strncmp(url, "/osu/", 5) == 0) {
  34. data->success = atoi(url + 5);
  35. done = 1;
  36. }
  37. resp = wpabuf_alloc(100);
  38. if (resp == NULL) {
  39. http_request_deinit(req);
  40. if (done)
  41. eloop_terminate();
  42. return;
  43. }
  44. wpabuf_put_str(resp, "HTTP/1.1\r\n\r\nUser input completed");
  45. if (done) {
  46. eloop_cancel_timeout(browser_timeout, NULL, NULL);
  47. eloop_register_timeout(0, 500000, browser_timeout, &data, NULL);
  48. }
  49. http_request_send_and_deinit(req, resp);
  50. }
  51. int hs20_web_browser(const char *url)
  52. {
  53. struct http_server *http;
  54. struct in_addr addr;
  55. struct browser_data data;
  56. pid_t pid;
  57. wpa_printf(MSG_INFO, "Launching wpadebug browser to %s", url);
  58. os_memset(&data, 0, sizeof(data));
  59. if (eloop_init() < 0) {
  60. wpa_printf(MSG_ERROR, "eloop_init failed");
  61. return -1;
  62. }
  63. addr.s_addr = htonl((127 << 24) | 1);
  64. http = http_server_init(&addr, 12345, http_req, &data);
  65. if (http == NULL) {
  66. wpa_printf(MSG_ERROR, "http_server_init failed");
  67. eloop_destroy();
  68. return -1;
  69. }
  70. pid = fork();
  71. if (pid < 0) {
  72. wpa_printf(MSG_ERROR, "fork: %s", strerror(errno));
  73. http_server_deinit(http);
  74. eloop_destroy();
  75. return -1;
  76. }
  77. if (pid == 0) {
  78. /* run the external command in the child process */
  79. char *argv[14];
  80. argv[0] = "browser-wpadebug";
  81. argv[1] = "start";
  82. argv[2] = "-a";
  83. argv[3] = "android.action.MAIN";
  84. argv[4] = "-c";
  85. argv[5] = "android.intent.category.LAUNCHER";
  86. argv[6] = "-n";
  87. argv[7] = "w1.fi.wpadebug/.WpaWebViewActivity";
  88. argv[8] = "-e";
  89. argv[9] = "w1.fi.wpadebug.URL";
  90. argv[10] = (void *) url;
  91. argv[11] = "--user";
  92. argv[12] = "-3"; /* USER_CURRENT_OR_SELF */
  93. argv[13] = NULL;
  94. execv("/system/bin/am", argv);
  95. wpa_printf(MSG_ERROR, "execv: %s", strerror(errno));
  96. exit(0);
  97. return -1;
  98. }
  99. eloop_register_timeout(300, 0, browser_timeout, &data, NULL);
  100. eloop_run();
  101. eloop_cancel_timeout(browser_timeout, &data, NULL);
  102. http_server_deinit(http);
  103. eloop_destroy();
  104. wpa_printf(MSG_INFO, "Closing Android browser");
  105. if (os_exec("/system/bin/am",
  106. "start -a android.action.MAIN "
  107. "-c android.intent.category.LAUNCHER "
  108. "-n w1.fi.wpadebug/.WpaWebViewActivity "
  109. "-e w1.fi.wpadebug.URL FINISH", 1) != 0) {
  110. wpa_printf(MSG_INFO, "Failed to close wpadebug browser");
  111. }
  112. return data.success;
  113. }