Skip to content

Commit 492c638

Browse files
authored
Merge pull request #3358 from cesanta/diab
Add support for Windriver's Diab v4 (stricter C89 w/ extensions)
2 parents 4717a15 + 5b023cb commit 492c638

File tree

19 files changed

+347
-215
lines changed

19 files changed

+347
-215
lines changed

mongoose.c

Lines changed: 145 additions & 85 deletions
Large diffs are not rendered by default.

mongoose.h

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,12 +1267,18 @@ void mg_log_set_fn(mg_pfn_t fn, void *param);
12671267
#define mg_log_set(level_) mg_log_level = (level_)
12681268

12691269
#if MG_ENABLE_LOG
1270-
#define MG_LOG(level, args) \
1271-
do { \
1272-
if ((level) <= mg_log_level) { \
1273-
mg_log_prefix((level), __FILE__, __LINE__, __func__); \
1274-
mg_log args; \
1275-
} \
1270+
#if !defined(_MSC_VER) && \
1271+
(!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L)
1272+
#define MG___FUNC__ ""
1273+
#else
1274+
#define MG___FUNC__ __func__ // introduced in C99
1275+
#endif
1276+
#define MG_LOG(level, args) \
1277+
do { \
1278+
if ((level) <= mg_log_level) { \
1279+
mg_log_prefix((level), __FILE__, __LINE__, MG___FUNC__); \
1280+
mg_log args; \
1281+
} \
12761282
} while (0)
12771283
#else
12781284
#define MG_LOG(level, args) \
@@ -1663,11 +1669,11 @@ struct mg_dns {
16631669
};
16641670

16651671
struct mg_addr {
1666-
union { // Holds IPv4 or IPv6 address, in network byte order
1672+
union { // Holds IPv4 or IPv6 address, in network byte order
16671673
uint8_t ip[16];
16681674
uint32_t ip4;
16691675
uint64_t ip6[2];
1670-
};
1676+
} addr;
16711677
uint16_t port; // TCP or UDP port in network byte order
16721678
uint8_t scope_id; // IPv6 scope ID
16731679
bool is_ip6; // True when address is IPv6 address
@@ -2954,7 +2960,6 @@ struct mg_dns_rr {
29542960
uint16_t alen; // Address length
29552961
};
29562962

2957-
29582963
// DNS-SD response record
29592964
struct mg_dnssd_record {
29602965
struct mg_str srvcproto; // service.proto, service name
@@ -2966,8 +2971,8 @@ struct mg_dnssd_record {
29662971
struct mg_mdns_req {
29672972
struct mg_dns_rr *rr;
29682973
struct mg_dnssd_record *r;
2969-
struct mg_str reqname; // requested name in RR
2970-
struct mg_str respname; // actual name in response
2974+
struct mg_str reqname; // requested name in RR
2975+
struct mg_str respname; // actual name in response
29712976
struct mg_addr addr;
29722977
bool is_listing;
29732978
bool is_resp;
@@ -2979,8 +2984,9 @@ void mg_resolve_cancel(struct mg_connection *);
29792984
bool mg_dns_parse(const uint8_t *buf, size_t len, struct mg_dns_message *);
29802985
size_t mg_dns_parse_rr(const uint8_t *buf, size_t len, size_t ofs,
29812986
bool is_question, struct mg_dns_rr *);
2982-
2983-
struct mg_connection *mg_mdns_listen(struct mg_mgr *mgr, mg_event_handler_t fn, void *fn_data);
2987+
2988+
struct mg_connection *mg_mdns_listen(struct mg_mgr *mgr, mg_event_handler_t fn,
2989+
void *fn_data);
29842990

29852991

29862992

src/dns.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@ bool mg_dns_parse(const uint8_t *buf, size_t len, struct mg_dns_message *dm) {
136136

137137
if (rr.alen == 4 && rr.atype == 1 && rr.aclass == 1) {
138138
dm->addr.is_ip6 = false;
139-
memcpy(&dm->addr.ip, &buf[ofs - 4], 4);
139+
memcpy(&dm->addr.addr.ip, &buf[ofs - 4], 4);
140140
dm->resolved = true;
141141
break; // Return success
142142
} else if (rr.alen == 16 && rr.atype == 28 && rr.aclass == 1) {
143143
dm->addr.is_ip6 = true;
144-
memcpy(&dm->addr.ip, &buf[ofs - 16], 16);
144+
memcpy(&dm->addr.addr.ip, &buf[ofs - 16], 16);
145145
dm->resolved = true;
146146
break; // Return success
147147
}

src/dns.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ struct mg_dns_rr {
3131
uint16_t alen; // Address length
3232
};
3333

34-
3534
// DNS-SD response record
3635
struct mg_dnssd_record {
3736
struct mg_str srvcproto; // service.proto, service name
@@ -43,8 +42,8 @@ struct mg_dnssd_record {
4342
struct mg_mdns_req {
4443
struct mg_dns_rr *rr;
4544
struct mg_dnssd_record *r;
46-
struct mg_str reqname; // requested name in RR
47-
struct mg_str respname; // actual name in response
45+
struct mg_str reqname; // requested name in RR
46+
struct mg_str respname; // actual name in response
4847
struct mg_addr addr;
4948
bool is_listing;
5049
bool is_resp;
@@ -56,5 +55,6 @@ void mg_resolve_cancel(struct mg_connection *);
5655
bool mg_dns_parse(const uint8_t *buf, size_t len, struct mg_dns_message *);
5756
size_t mg_dns_parse_rr(const uint8_t *buf, size_t len, size_t ofs,
5857
bool is_question, struct mg_dns_rr *);
59-
60-
struct mg_connection *mg_mdns_listen(struct mg_mgr *mgr, mg_event_handler_t fn, void *fn_data);
58+
59+
struct mg_connection *mg_mdns_listen(struct mg_mgr *mgr, mg_event_handler_t fn,
60+
void *fn_data);

src/fmt.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ static int xisinf(double x) {
2222
union {
2323
double f;
2424
uint64_t u;
25-
} ieee754 = {x};
25+
} ieee754;
26+
ieee754.f = x;
2627
return ((unsigned) (ieee754.u >> 32) & 0x7fffffff) == 0x7ff00000 &&
2728
((unsigned) ieee754.u == 0);
2829
}
@@ -31,7 +32,8 @@ static int xisnan(double x) {
3132
union {
3233
double f;
3334
uint64_t u;
34-
} ieee754 = {x};
35+
} ieee754;
36+
ieee754.f = x;
3537
return ((unsigned) (ieee754.u >> 32) & 0x7fffffff) +
3638
((unsigned) ieee754.u != 0) >
3739
0x7ff00000;
@@ -99,7 +101,7 @@ static size_t mg_dtoa(char *dst, size_t dstlen, double d, int width, bool tz) {
99101
}
100102

101103
while (tz && n > 0 && buf[s + n - 1] == '0') n--; // Trim trailing zeroes
102-
if (tz && n > 0 && buf[s + n - 1] == '.') n--; // Trim trailing dot
104+
if (tz && n > 0 && buf[s + n - 1] == '.') n--; // Trim trailing dot
103105
n += s;
104106
if (n >= (int) sizeof(buf)) n = (int) sizeof(buf) - 1;
105107
buf[n] = '\0';
@@ -129,7 +131,7 @@ static size_t mg_lld(char *buf, int64_t val, bool is_signed, bool is_hex) {
129131
}
130132

131133
static size_t scpy(void (*out)(char, void *), void *ptr, char *buf,
132-
size_t len) {
134+
size_t len) {
133135
size_t i = 0;
134136
while (i < len && buf[i] != '\0') out(buf[i++], ptr);
135137
return i;

src/fs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ static void mg_fs_ls_fn(const char *filename, void *param) {
8686
}
8787

8888
bool mg_fs_ls(struct mg_fs *fs, const char *path, char *buf, size_t len) {
89-
struct mg_str s = {buf, len};
89+
struct mg_str s;
90+
s.buf = buf, s.len = len;
9091
fs->ls(path, mg_fs_ls_fn, &s);
9192
return buf[0] != '\0';
9293
}

src/log.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@ void mg_log_set_fn(mg_pfn_t fn, void *param);
1616
#define mg_log_set(level_) mg_log_level = (level_)
1717

1818
#if MG_ENABLE_LOG
19-
#define MG_LOG(level, args) \
20-
do { \
21-
if ((level) <= mg_log_level) { \
22-
mg_log_prefix((level), __FILE__, __LINE__, __func__); \
23-
mg_log args; \
24-
} \
19+
#if !defined(_MSC_VER) && \
20+
(!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L)
21+
#define MG___FUNC__ ""
22+
#else
23+
#define MG___FUNC__ __func__ // introduced in C99
24+
#endif
25+
#define MG_LOG(level, args) \
26+
do { \
27+
if ((level) <= mg_log_level) { \
28+
mg_log_prefix((level), __FILE__, __LINE__, MG___FUNC__); \
29+
mg_log args; \
30+
} \
2531
} while (0)
2632
#else
2733
#define MG_LOG(level, args) \

src/mqtt.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ void mg_mqtt_login(struct mg_connection *c, const struct mg_mqtt_opts *opts) {
266266
char client_id[21];
267267
struct mg_str cid = opts->client_id;
268268
size_t total_len = 7 + 1 + 2 + 2;
269-
uint8_t hdr[8] = {0, 4, 'M', 'Q', 'T', 'T', opts->version, 0};
269+
uint8_t hdr[8] = {0, 4, 'M', 'Q', 'T', 'T', 0, 0};
270+
hdr[6] = opts->version;
270271

271272
if (cid.len == 0) {
272273
mg_random_str(client_id, sizeof(client_id) - 1);

src/net.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ size_t mg_printf(struct mg_connection *c, const char *fmt, ...) {
3131
static bool mg_atonl(struct mg_str str, struct mg_addr *addr) {
3232
uint32_t localhost = mg_htonl(0x7f000001);
3333
if (mg_strcasecmp(str, mg_str("localhost")) != 0) return false;
34-
memcpy(addr->ip, &localhost, sizeof(uint32_t));
34+
memcpy(addr->addr.ip, &localhost, sizeof(uint32_t));
3535
addr->is_ip6 = false;
3636
return true;
3737
}
3838

3939
static bool mg_atone(struct mg_str str, struct mg_addr *addr) {
4040
if (str.len > 0) return false;
41-
memset(addr->ip, 0, sizeof(addr->ip));
41+
memset(addr->addr.ip, 0, sizeof(addr->addr.ip));
4242
addr->is_ip6 = false;
4343
return true;
4444
}
@@ -59,7 +59,7 @@ static bool mg_aton4(struct mg_str str, struct mg_addr *addr) {
5959
}
6060
}
6161
if (num_dots != 3 || str.buf[i - 1] == '.') return false;
62-
memcpy(&addr->ip, data, sizeof(data));
62+
memcpy(&addr->addr.ip, data, sizeof(data));
6363
addr->is_ip6 = false;
6464
return true;
6565
}
@@ -74,10 +74,10 @@ static bool mg_v4mapped(struct mg_str str, struct mg_addr *addr) {
7474
}
7575
// struct mg_str s = mg_str_n(&str.buf[7], str.len - 7);
7676
if (!mg_aton4(mg_str_n(&str.buf[7], str.len - 7), addr)) return false;
77-
memcpy(&ipv4, addr->ip, sizeof(ipv4));
78-
memset(addr->ip, 0, sizeof(addr->ip));
79-
addr->ip[10] = addr->ip[11] = 255;
80-
memcpy(&addr->ip[12], &ipv4, 4);
77+
memcpy(&ipv4, addr->addr.ip, sizeof(ipv4));
78+
memset(addr->addr.ip, 0, sizeof(addr->addr.ip));
79+
addr->addr.ip[10] = addr->addr.ip[11] = 255;
80+
memcpy(&addr->addr.ip[12], &ipv4, 4);
8181
addr->is_ip6 = true;
8282
return true;
8383
}
@@ -95,8 +95,8 @@ static bool mg_aton6(struct mg_str str, struct mg_addr *addr) {
9595
if (i > j + 3) return false;
9696
// MG_DEBUG(("%lu %lu [%.*s]", i, j, (int) (i - j + 1), &str.buf[j]));
9797
mg_str_to_num(mg_str_n(&str.buf[j], i - j + 1), 16, &val, sizeof(val));
98-
addr->ip[n] = (uint8_t) ((val >> 8) & 255);
99-
addr->ip[n + 1] = (uint8_t) (val & 255);
98+
addr->addr.ip[n] = (uint8_t) ((val >> 8) & 255);
99+
addr->addr.ip[n + 1] = (uint8_t) (val & 255);
100100
} else if (str.buf[i] == ':') {
101101
j = i + 1;
102102
if (i > 0 && str.buf[i - 1] == ':') {
@@ -106,8 +106,8 @@ static bool mg_aton6(struct mg_str str, struct mg_addr *addr) {
106106
n += 2;
107107
}
108108
if (n > 14) return false;
109-
addr->ip[n] = addr->ip[n + 1] = 0; // For trailing ::
110-
} else if (str.buf[i] == '%') { // Scope ID, last in string
109+
addr->addr.ip[n] = addr->addr.ip[n + 1] = 0; // For trailing ::
110+
} else if (str.buf[i] == '%') { // Scope ID, last in string
111111
if (mg_str_to_num(mg_str_n(&str.buf[i + 1], str.len - i - 1), 10,
112112
&addr->scope_id, sizeof(uint8_t))) {
113113
addr->is_ip6 = true;
@@ -121,8 +121,8 @@ static bool mg_aton6(struct mg_str str, struct mg_addr *addr) {
121121
}
122122
if (n < 14 && dc == 42) return false;
123123
if (n < 14) {
124-
memmove(&addr->ip[dc + (14 - n)], &addr->ip[dc], n - dc + 2);
125-
memset(&addr->ip[dc], 0, 14 - n);
124+
memmove(&addr->addr.ip[dc + (14 - n)], &addr->addr.ip[dc], n - dc + 2);
125+
memset(&addr->addr.ip[dc], 0, 14 - n);
126126
}
127127

128128
addr->is_ip6 = true;

src/net.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ struct mg_dns {
1414
};
1515

1616
struct mg_addr {
17-
union { // Holds IPv4 or IPv6 address, in network byte order
17+
union { // Holds IPv4 or IPv6 address, in network byte order
1818
uint8_t ip[16];
1919
uint32_t ip4;
2020
uint64_t ip6[2];
21-
};
21+
} addr;
2222
uint16_t port; // TCP or UDP port in network byte order
2323
uint8_t scope_id; // IPv6 scope ID
2424
bool is_ip6; // True when address is IPv6 address

0 commit comments

Comments
 (0)