mip_tap_test.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #define MG_ENABLE_TCPIP 1
  2. #define MG_ENABLE_SOCKET 0
  3. #define MG_USING_DHCP 1
  4. #define MG_ENABLE_PACKED_FS 0
  5. #define MG_ENABLE_LINES 1
  6. #include <sys/socket.h>
  7. #ifndef __OpenBSD__
  8. #include <linux/if.h>
  9. #include <linux/if_tun.h>
  10. #else
  11. #include <net/if.h>
  12. #include <net/if_tun.h>
  13. #include <net/if_types.h>
  14. #endif
  15. #include <sys/ioctl.h>
  16. #include "mongoose.c"
  17. #include "driver_mock.c"
  18. static int s_num_tests = 0;
  19. #define ASSERT(expr) \
  20. do { \
  21. s_num_tests++; \
  22. if (!(expr)) { \
  23. printf("FAILURE %s:%d: %s\n", __FILE__, __LINE__, #expr); \
  24. abort(); \
  25. } \
  26. } while (0)
  27. // MIP TUNTAP driver
  28. static size_t tap_rx(void *buf, size_t len, struct mg_tcpip_if *ifp) {
  29. ssize_t received = read(*(int *) ifp->driver_data, buf, len);
  30. usleep(1); // This is to avoid 100% CPU
  31. if (received < 0) return 0;
  32. return (size_t) received;
  33. }
  34. static size_t tap_tx(const void *buf, size_t len, struct mg_tcpip_if *ifp) {
  35. ssize_t res = write(*(int *) ifp->driver_data, buf, len);
  36. if (res < 0) {
  37. MG_ERROR(("tap_tx failed: %d", errno));
  38. return 0;
  39. }
  40. return (size_t) res;
  41. }
  42. static bool tap_up(struct mg_tcpip_if *ifp) {
  43. return ifp->driver_data ? true : false;
  44. }
  45. // HTTP fetches IOs
  46. struct Post_reply {
  47. char *post; // HTTP POST data
  48. void *http_response; // Server response(s)
  49. unsigned int http_responses_received; // Number responses received
  50. };
  51. char *fetch(struct mg_mgr *mgr, const char *url, const char *post_data);
  52. static void f_http_fetch_query(struct mg_connection *c, int ev, void *ev_data);
  53. int get_response_code(char *); // Returns HTTP status code from full char* msg
  54. static void f_http_fetch_query(struct mg_connection *c, int ev, void *ev_data) {
  55. static char *http_response = 0;
  56. static bool http_response_allocated =
  57. 0; // So that we will update out parameter
  58. unsigned int http_responses_received = 0;
  59. struct Post_reply *post_reply_l;
  60. post_reply_l = (struct Post_reply *) c->fn_data;
  61. if (ev == MG_EV_CONNECT) {
  62. mg_printf(c, post_reply_l->post);
  63. } else if (ev == MG_EV_HTTP_MSG) {
  64. struct mg_http_message *hm = (struct mg_http_message *) ev_data;
  65. http_responses_received++;
  66. if (!http_response_allocated) {
  67. http_response = (char *) mg_strdup(hm->message).ptr;
  68. http_response_allocated = 1;
  69. }
  70. if (http_responses_received > 0) {
  71. post_reply_l->http_response = http_response;
  72. post_reply_l->http_responses_received = http_responses_received;
  73. }
  74. }
  75. }
  76. // Fetch utility returns message from fetch(..., URL, POST)
  77. char *fetch(struct mg_mgr *mgr, const char *url, const char *fn_data) {
  78. struct Post_reply post_reply;
  79. {
  80. post_reply.post = (char *) fn_data;
  81. post_reply.http_response = 0;
  82. post_reply.http_responses_received = 0;
  83. }
  84. struct mg_connection *conn;
  85. conn = mg_http_connect(mgr, url, f_http_fetch_query, &post_reply);
  86. ASSERT(conn != NULL); // Assertion on initialisation
  87. for (int i = 0; i < 500 && !post_reply.http_responses_received; i++) {
  88. mg_mgr_poll(mgr, 100);
  89. usleep(10000); // 10 ms. Slow down poll loop to ensure packets transit
  90. }
  91. if (mgr->conns != 0) {
  92. conn->is_closing = 1;
  93. mg_mgr_poll(mgr, 0);
  94. }
  95. mg_mgr_poll(mgr, 0);
  96. if (!post_reply.http_responses_received) {
  97. return 0;
  98. } else {
  99. return (char *) post_reply.http_response;
  100. }
  101. }
  102. // Returns server's HTTP response code
  103. int get_response_code(char *http_msg_raw) {
  104. int http_status = 0;
  105. struct mg_http_message http_msg_parsed;
  106. if (mg_http_parse(http_msg_raw, strlen(http_msg_raw), &http_msg_parsed)) {
  107. http_status = mg_http_status(&http_msg_parsed);
  108. } else {
  109. printf("Error: mg_http_parse()\n");
  110. ASSERT(http_status != 0); // Couldn't parse.
  111. }
  112. return http_status;
  113. }
  114. static void test_http_fetch(struct mg_mgr *mgr) {
  115. char *http_feedback = (char *) "";
  116. const bool ipv6 = 0;
  117. if (ipv6) {
  118. http_feedback = fetch(mgr, "ipv6.google.com",
  119. "GET/ HTTP/1.0\r\nHost: ipv6.google.com\r\n\r\n");
  120. } else {
  121. http_feedback =
  122. fetch(mgr, "http://cesanta.com",
  123. "GET //robots.txt HTTP/1.0\r\nHost: cesanta.com\r\n\r\n");
  124. }
  125. ASSERT(http_feedback != NULL &&
  126. *http_feedback != '\0'); // HTTP response received ?
  127. int http_status = get_response_code(http_feedback);
  128. // printf("Server response HTTP status code: %d\n",http_status);
  129. ASSERT(http_status != 0);
  130. ASSERT(http_status == 301); // OK: Permanently moved (HTTP->HTTPS redirect)
  131. if (http_feedback) {
  132. free(http_feedback);
  133. http_feedback = 0;
  134. }
  135. }
  136. static struct mg_connection *s_conn;
  137. static char s_topic[16];
  138. static void mqtt_fn(struct mg_connection *c, int ev, void *ev_data) {
  139. if (ev == MG_EV_MQTT_OPEN) {
  140. // MQTT connect is successful
  141. struct mg_mqtt_opts sub_opts;
  142. memset(&sub_opts, 0, sizeof(sub_opts));
  143. sub_opts.topic = mg_str(mg_random_str(s_topic, sizeof(s_topic)));
  144. sub_opts.qos = 1;
  145. mg_mqtt_sub(c, &sub_opts);
  146. struct mg_mqtt_opts pub_opts;
  147. memset(&pub_opts, 0, sizeof(pub_opts));
  148. pub_opts.topic = sub_opts.topic;
  149. pub_opts.message = mg_str("hi");
  150. pub_opts.qos = 1, pub_opts.retain = false;
  151. mg_mqtt_pub(c, &pub_opts);
  152. } else if (ev == MG_EV_MQTT_MSG) {
  153. struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
  154. if (mm->topic.len != strlen(s_topic) || strcmp(mm->topic.ptr, s_topic))
  155. ASSERT(0);
  156. if (mm->data.len != 2 || strcmp(mm->data.ptr, "hi")) ASSERT(0);
  157. mg_mqtt_disconnect(c, NULL);
  158. *(bool *) c->fn_data = true;
  159. } else if (ev == MG_EV_CLOSE) {
  160. s_conn = NULL;
  161. }
  162. }
  163. static void test_mqtt_connsubpub(struct mg_mgr *mgr) {
  164. const char *url = "mqtt://broker.hivemq.com:1883";
  165. bool passed = false;
  166. struct mg_mqtt_opts opts;
  167. memset(&opts, 0, sizeof(opts));
  168. opts.clean = true, opts.version = 4;
  169. s_conn = mg_mqtt_connect(mgr, url, &opts, mqtt_fn, &passed);
  170. ASSERT(s_conn != NULL);
  171. for (int i = 0; i < 500 && s_conn != NULL && !s_conn->is_closing; i++) {
  172. mg_mgr_poll(mgr, 0);
  173. usleep(10000); // 10 ms. Slow down poll loop to ensure packets transit
  174. }
  175. ASSERT(passed);
  176. mg_mgr_poll(mgr, 0);
  177. }
  178. int main(void) {
  179. // Setup interface
  180. const char *iface = "tap0"; // Network iface
  181. const char *mac = "00:00:01:02:03:78"; // MAC address
  182. #ifndef __OpenBSD__
  183. const char *tuntap_device = "/dev/net/tun";
  184. #else
  185. const char *tuntap_device = "/dev/tap0";
  186. #endif
  187. int fd = open(tuntap_device, O_RDWR);
  188. struct ifreq ifr;
  189. memset(&ifr, 0, sizeof(ifr));
  190. strncpy(ifr.ifr_name, iface, IFNAMSIZ);
  191. #ifndef __OpenBSD__
  192. ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  193. if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0) {
  194. MG_ERROR(("Failed to setup TAP interface: %s", ifr.ifr_name));
  195. abort(); // return EXIT_FAILURE;
  196. }
  197. #else
  198. ifr.ifr_flags = (short) (IFF_UP | IFF_BROADCAST | IFF_MULTICAST);
  199. if (ioctl(fd, TUNSIFMODE, (void *) &ifr) < 0) {
  200. MG_ERROR(("Failed to setup TAP interface: %s", ifr.ifr_name));
  201. abort(); // return EXIT_FAILURE;
  202. }
  203. #endif
  204. fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK); // Non-blocking mode
  205. MG_INFO(("Opened TAP interface: %s", iface));
  206. // Events
  207. struct mg_mgr mgr; // Event manager
  208. mg_mgr_init(&mgr); // Initialise event manager
  209. // MIP driver
  210. struct mg_tcpip_driver driver;
  211. memset(&driver, 0, sizeof(driver));
  212. driver.tx = tap_tx;
  213. driver.up = tap_up;
  214. driver.rx = tap_rx;
  215. struct mg_tcpip_if mif;
  216. memset(&mif, 0, sizeof(mif));
  217. mif.driver = &driver;
  218. mif.driver_data = &fd;
  219. #if MG_USING_DHCP == 1
  220. #else
  221. mif.ip = mg_htonl(MG_U32(192, 168, 32, 2)); // Triggering a network failure
  222. mif.mask = mg_htonl(MG_U32(255, 255, 255, 0));
  223. mif.gw = mg_htonl(MG_U32(192, 168, 32, 1));
  224. #endif
  225. sscanf(mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &mif.mac[0], &mif.mac[1],
  226. &mif.mac[2], &mif.mac[3], &mif.mac[4], &mif.mac[5]);
  227. mg_tcpip_init(&mgr, &mif);
  228. MG_INFO(("Init done, starting main loop"));
  229. // Stack initialization, Network configuration (DHCP lease, ...)
  230. #if MG_USING_DHCP == 0
  231. MG_INFO(("MIF configuration: Static IP"));
  232. ASSERT(mif.ip != 0); // Check we have a satic IP assigned
  233. mg_mgr_poll(&mgr, 100); // For initialisation
  234. #else
  235. MG_INFO(("MIF configuration: DHCP"));
  236. ASSERT(!mif.ip); // Check we are set for DHCP
  237. int pc = 500; // Timout on DHCP lease 500 ~ approx 5s (typical delay <1s)
  238. while (((pc--) > 0) && !mif.ip) {
  239. mg_mgr_poll(&mgr, 100);
  240. usleep(10000); // 10 ms
  241. }
  242. if (!mif.ip) MG_ERROR(("No ip assigned (DHCP lease may have failed).\n"));
  243. ASSERT(mif.ip); // We have an IP (lease or static)
  244. #endif
  245. // RUN TESTS
  246. test_http_fetch(&mgr);
  247. test_mqtt_connsubpub(&mgr);
  248. printf("SUCCESS. Total tests: %d\n", s_num_tests);
  249. // Clear
  250. mg_mgr_free(&mgr);
  251. mg_tcpip_free(&mif); // Release after mg_mgr
  252. ASSERT(mgr.conns == NULL); // Deconstruction OK
  253. close(fd);
  254. return 0;
  255. }