From 5bd3df7a0f63bdd6c56adbb88c238a629f876f5e Mon Sep 17 00:00:00 2001
From: Marko Mikulicic <mkm@cesanta.com>
Date: Fri, 21 Oct 2016 01:22:52 +0300
Subject: [PATCH] Fix mg_time on mbed and make DNS work around epoch

PUBLISHED_FROM=c1aeef9dc25baba794b3269b44441c5bafbca5a8
---
 mongoose.c | 14 +++++++++++---
 mongoose.h | 45 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 3 deletions(-)

diff --git a/mongoose.c b/mongoose.c
index 26e42e9ee..c0237a247 100644
--- a/mongoose.c
+++ b/mongoose.c
@@ -718,8 +718,12 @@ typedef int cs_dirent_dummy;
 
 #ifndef _WIN32
 #include <stddef.h>
-#if !defined(CS_PLATFORM) || \
-    (CS_PLATFORM != CS_P_CC3200 && CS_PLATFORM != CS_P_MSP432)
+/*
+ * There is no sys/time.h on ARMCC.
+ */
+#if !(defined(__ARMCC_VERSION) || defined(__ICCARM__)) && \
+    (!defined(CS_PLATFORM) ||                             \
+     (CS_PLATFORM != CS_P_CC3200 && CS_PLATFORM != CS_P_MSP432))
 #include <sys/time.h>
 #endif
 #else
@@ -9142,6 +9146,7 @@ static void mg_resolve_async_eh(struct mg_connection *nc, int ev, void *data) {
   time_t now = (time_t) mg_time();
   struct mg_resolve_async_request *req;
   struct mg_dns_message *msg;
+  int first = 0;
 
   DBG(("ev=%d user_data=%p", ev, nc->user_data));
 
@@ -9153,13 +9158,16 @@ static void mg_resolve_async_eh(struct mg_connection *nc, int ev, void *data) {
 
   switch (ev) {
     case MG_EV_CONNECT:
+      /* don't depend on timer not being at epoch for sending out first req */
+      first = 1;
+    /* fallthrough */
     case MG_EV_POLL:
       if (req->retries > req->max_retries) {
         req->err = MG_RESOLVE_EXCEEDED_RETRY_COUNT;
         nc->flags |= MG_F_CLOSE_IMMEDIATELY;
         break;
       }
-      if (now - req->last_time >= req->timeout) {
+      if (first || now - req->last_time >= req->timeout) {
         mg_send_dns_query(nc, req->name, req->query);
         req->last_time = now;
         req->retries++;
diff --git a/mongoose.h b/mongoose.h
index e6edf74b9..29d1bfabe 100644
--- a/mongoose.h
+++ b/mongoose.h
@@ -761,10 +761,55 @@ int _stat(const char *pathname, struct stat *st);
 
 /* Amalgamated: #include "mbed.h" */
 
+#include <assert.h>
+#include <ctype.h>
+#include <errno.h>
+#include <inttypes.h>
+#include <stdint.h>
+#include <string.h>
+#include <time.h>
+
 #ifndef CS_ENABLE_STDIO
 #define CS_ENABLE_STDIO 1
 #endif
 
+/*
+ * mbed can be compiled with the ARM compiler which
+ * just doesn't come with a gettimeofday shim
+ * because it's a BSD API and ARM targets embedded
+ * non-unix platforms.
+ */
+#if defined(__ARMCC_VERSION) || defined(__ICCARM__)
+#define _TIMEVAL_DEFINED
+#define gettimeofday _gettimeofday
+
+/* copied from GCC on ARM; for some reason useconds are signed */
+typedef long suseconds_t; /* microseconds (signed) */
+struct timeval {
+  time_t tv_sec;       /* seconds */
+  suseconds_t tv_usec; /* and microseconds */
+};
+
+#endif
+
+#if MG_NET_IF == MG_NET_IF_SIMPLELINK
+
+typedef int sock_t;
+#define INVALID_SOCKET (-1)
+
+#define to64(x) strtoll(x, NULL, 10)
+#define INT64_FMT PRId64
+#define INT64_X_FMT PRIx64
+#define SIZE_T_FMT "u"
+
+#define SOMAXCONN 8
+
+const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
+char *inet_ntoa(struct in_addr in);
+int inet_pton(int af, const char *src, void *dst);
+
+#endif /* MG_NET_IF == MG_NET_IF_SIMPLELINK */
+
 #endif /* CS_PLATFORM == CS_P_MBED */
 #endif /* CS_COMMON_PLATFORMS_PLATFORM_MBED_H_ */
 #ifdef MG_MODULE_LINES
-- 
GitLab