mirror of
https://github.com/cesanta/mongoose.git
synced 2024-11-28 05:39:00 +08:00
Merge pull request #1995 from cesanta/mg_print_ip
Add mg_print_ip, mg_print_ip_port helper functions
This commit is contained in:
commit
476232ea22
38
mongoose.c
38
mongoose.c
@ -273,9 +273,7 @@ static void dns_cb(struct mg_connection *c, int ev, void *ev_data,
|
||||
if (dm.resolved) {
|
||||
dm.addr.port = d->c->rem.port; // Save port
|
||||
d->c->rem = dm.addr; // Copy resolved address
|
||||
MG_DEBUG(
|
||||
("%lu %s is %I", d->c->id, dm.name, d->c->rem.is_ip6 ? 16 : 4,
|
||||
d->c->rem.is_ip6 ? &d->c->rem.ip6 : (void *) &d->c->rem.ip));
|
||||
MG_DEBUG(("%lu %s is %M", d->c->id, dm.name, mg_print_ip, &c->rem));
|
||||
mg_connect_resolved(d->c);
|
||||
#if MG_ENABLE_IPV6
|
||||
} else if (dm.addr.is_ip6 == false && dm.name[0] != '\0' &&
|
||||
@ -3519,6 +3517,25 @@ struct mg_timer *mg_timer_add(struct mg_mgr *mgr, uint64_t milliseconds,
|
||||
return t;
|
||||
}
|
||||
|
||||
size_t mg_print_ip(void (*out)(char, void *), void *arg, va_list *ap) {
|
||||
struct mg_addr *addr = va_arg(*ap, struct mg_addr *);
|
||||
if (addr->is_ip6) {
|
||||
uint16_t *p = (uint16_t *) addr->ip6;
|
||||
return mg_xprintf(out, arg, "[%x:%x:%x:%x:%x:%x:%x:%x]", mg_htons(p[0]),
|
||||
mg_htons(p[1]), mg_htons(p[2]), mg_htons(p[3]),
|
||||
mg_htons(p[4]), mg_htons(p[5]), mg_htons(p[6]),
|
||||
mg_htons(p[7]));
|
||||
} else {
|
||||
uint8_t *p = (uint8_t *) &addr->ip;
|
||||
return mg_xprintf(out, arg, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
|
||||
}
|
||||
}
|
||||
|
||||
size_t mg_print_ip_port(void (*out)(char, void *), void *arg, va_list *ap) {
|
||||
struct mg_addr *a = va_arg(*ap, struct mg_addr *);
|
||||
return mg_xprintf(out, arg, "%M:%hu", mg_print_ip, a, mg_ntohs(a->port));
|
||||
}
|
||||
|
||||
void mg_mgr_free(struct mg_mgr *mgr) {
|
||||
struct mg_connection *c;
|
||||
struct mg_timer *tmp, *t = mgr->timers;
|
||||
@ -4062,8 +4079,8 @@ static void iolog(struct mg_connection *c, char *buf, long n, bool r) {
|
||||
union usa usa;
|
||||
socklen_t slen = sizeof(usa.sin);
|
||||
if (getsockname(FD(c), &usa.sa, &slen) < 0) (void) 0; // Ignore result
|
||||
MG_INFO(("\n-- %lu %I %s %I %ld", c->id, 4, &usa.sin.sin_addr,
|
||||
r ? "<-" : "->", 4, &c->rem.ip, n));
|
||||
MG_INFO(("\n-- %lu %M %s %M %ld", c->id, mg_print_ip_port, &c->loc,
|
||||
r ? "<-" : "->", mg_print_ip_port, &c->rem, n));
|
||||
|
||||
mg_hexdump(buf, (size_t) n);
|
||||
}
|
||||
@ -4318,8 +4335,7 @@ void mg_connect_resolved(struct mg_connection *c) {
|
||||
if ((rc = connect(FD(c), &usa.sa, slen)) == 0) {
|
||||
mg_call(c, MG_EV_CONNECT, NULL);
|
||||
} else if (mg_sock_would_block()) {
|
||||
MG_DEBUG(("%lu %p -> %I:%hu pend", c->id, c->fd, 4, &c->rem.ip,
|
||||
mg_ntohs(c->rem.port)));
|
||||
MG_DEBUG(("%lu %p -> %M pend", c->id, c->fd, mg_print_ip_port, &c->rem));
|
||||
c->is_connecting = 1;
|
||||
} else {
|
||||
mg_error(c, "connect: %d", MG_SOCKET_ERRNO);
|
||||
@ -4374,8 +4390,8 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) {
|
||||
c->pfn_data = lsn->pfn_data;
|
||||
c->fn = lsn->fn;
|
||||
c->fn_data = lsn->fn_data;
|
||||
MG_DEBUG(("%lu %p accepted %I.%hu -> %I.%hu", c->id, c->fd, 4, &c->rem.ip,
|
||||
mg_ntohs(c->rem.port), 4, &c->loc.ip, mg_ntohs(c->loc.port)));
|
||||
MG_DEBUG(("%lu %p accepted %M -> %M", c->id, c->fd, mg_print_ip_port,
|
||||
&c->rem, mg_print_ip_port, &c->loc));
|
||||
mg_call(c, MG_EV_OPEN, NULL);
|
||||
mg_call(c, MG_EV_ACCEPT, NULL);
|
||||
}
|
||||
@ -5061,7 +5077,7 @@ void mg_tls_free(struct mg_connection *c) {
|
||||
|
||||
static int mg_net_send(void *ctx, const unsigned char *buf, size_t len) {
|
||||
long n = mg_io_send((struct mg_connection *) ctx, buf, len);
|
||||
MG_VERBOSE(("%lu n=%ld", ((struct mg_connection *) ctx)->id, n));
|
||||
MG_VERBOSE(("%lu n=%ld e=%d", ((struct mg_connection *) ctx)->id, n, errno));
|
||||
if (n == MG_IO_WAIT) return MBEDTLS_ERR_SSL_WANT_WRITE;
|
||||
if (n == MG_IO_RESET) return MBEDTLS_ERR_NET_CONN_RESET;
|
||||
if (n == MG_IO_ERR) return MBEDTLS_ERR_NET_SEND_FAILED;
|
||||
@ -5101,7 +5117,7 @@ static int mbed_rng(void *ctx, unsigned char *buf, size_t len) {
|
||||
|
||||
static void debug_cb(void *c, int lev, const char *s, int n, const char *s2) {
|
||||
n = (int) strlen(s2) - 1;
|
||||
MG_VERBOSE(("%lu %d %.*s", ((struct mg_connection *) c)->id, lev, n, s2));
|
||||
MG_INFO(("%lu %d %.*s", ((struct mg_connection *) c)->id, lev, n, s2));
|
||||
(void) s;
|
||||
}
|
||||
|
||||
|
11
mongoose.h
11
mongoose.h
@ -896,10 +896,15 @@ uint64_t mg_millis(void);
|
||||
#define mg_htons(x) mg_ntohs(x)
|
||||
#define mg_htonl(x) mg_ntohl(x)
|
||||
|
||||
#define MG_U32(a, b, c, d) \
|
||||
#define MG_U32(a, b, c, d) \
|
||||
(((uint32_t) ((a) &255) << 24) | ((uint32_t) ((b) &255) << 16) | \
|
||||
((uint32_t) ((c) &255) << 8) | (uint32_t) ((d) &255))
|
||||
|
||||
// For printing IPv4 addresses: printf("%d.%d.%d.%d\n", MG_IPADDR_PARTS(&ip))
|
||||
#define MG_U8P(ADDR) ((uint8_t *) (ADDR))
|
||||
#define MG_IPADDR_PARTS(ADDR) \
|
||||
MG_U8P(ADDR)[0], MG_U8P(ADDR)[1], MG_U8P(ADDR)[2], MG_U8P(ADDR)[3]
|
||||
|
||||
// Linked list management macros
|
||||
#define LIST_ADD_HEAD(type_, head_, elem_) \
|
||||
do { \
|
||||
@ -1102,8 +1107,12 @@ int mg_mkpipe(struct mg_mgr *, mg_event_handler_t, void *, bool udp);
|
||||
struct mg_connection *mg_alloc_conn(struct mg_mgr *);
|
||||
void mg_close_conn(struct mg_connection *c);
|
||||
bool mg_open_listener(struct mg_connection *c, const char *url);
|
||||
|
||||
// Utility functions
|
||||
struct mg_timer *mg_timer_add(struct mg_mgr *mgr, uint64_t milliseconds,
|
||||
unsigned flags, void (*fn)(void *), void *arg);
|
||||
size_t mg_print_ip(void (*out)(char, void *), void *ptr, va_list *ap);
|
||||
size_t mg_print_ip_port(void (*out)(char, void *), void *ptr, va_list *ap);
|
||||
|
||||
// Low-level IO primives used by TLS layer
|
||||
enum { MG_IO_ERR = -1, MG_IO_WAIT = -2, MG_IO_RESET = -3 };
|
||||
|
@ -161,9 +161,7 @@ static void dns_cb(struct mg_connection *c, int ev, void *ev_data,
|
||||
if (dm.resolved) {
|
||||
dm.addr.port = d->c->rem.port; // Save port
|
||||
d->c->rem = dm.addr; // Copy resolved address
|
||||
MG_DEBUG(
|
||||
("%lu %s is %I", d->c->id, dm.name, d->c->rem.is_ip6 ? 16 : 4,
|
||||
d->c->rem.is_ip6 ? &d->c->rem.ip6 : (void *) &d->c->rem.ip));
|
||||
MG_DEBUG(("%lu %s is %M", d->c->id, dm.name, mg_print_ip, &c->rem));
|
||||
mg_connect_resolved(d->c);
|
||||
#if MG_ENABLE_IPV6
|
||||
} else if (dm.addr.is_ip6 == false && dm.name[0] != '\0' &&
|
||||
|
19
src/net.c
19
src/net.c
@ -207,6 +207,25 @@ struct mg_timer *mg_timer_add(struct mg_mgr *mgr, uint64_t milliseconds,
|
||||
return t;
|
||||
}
|
||||
|
||||
size_t mg_print_ip(void (*out)(char, void *), void *arg, va_list *ap) {
|
||||
struct mg_addr *addr = va_arg(*ap, struct mg_addr *);
|
||||
if (addr->is_ip6) {
|
||||
uint16_t *p = (uint16_t *) addr->ip6;
|
||||
return mg_xprintf(out, arg, "[%x:%x:%x:%x:%x:%x:%x:%x]", mg_htons(p[0]),
|
||||
mg_htons(p[1]), mg_htons(p[2]), mg_htons(p[3]),
|
||||
mg_htons(p[4]), mg_htons(p[5]), mg_htons(p[6]),
|
||||
mg_htons(p[7]));
|
||||
} else {
|
||||
uint8_t *p = (uint8_t *) &addr->ip;
|
||||
return mg_xprintf(out, arg, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
|
||||
}
|
||||
}
|
||||
|
||||
size_t mg_print_ip_port(void (*out)(char, void *), void *arg, va_list *ap) {
|
||||
struct mg_addr *a = va_arg(*ap, struct mg_addr *);
|
||||
return mg_xprintf(out, arg, "%M:%hu", mg_print_ip, a, mg_ntohs(a->port));
|
||||
}
|
||||
|
||||
void mg_mgr_free(struct mg_mgr *mgr) {
|
||||
struct mg_connection *c;
|
||||
struct mg_timer *tmp, *t = mgr->timers;
|
||||
|
@ -94,8 +94,12 @@ int mg_mkpipe(struct mg_mgr *, mg_event_handler_t, void *, bool udp);
|
||||
struct mg_connection *mg_alloc_conn(struct mg_mgr *);
|
||||
void mg_close_conn(struct mg_connection *c);
|
||||
bool mg_open_listener(struct mg_connection *c, const char *url);
|
||||
|
||||
// Utility functions
|
||||
struct mg_timer *mg_timer_add(struct mg_mgr *mgr, uint64_t milliseconds,
|
||||
unsigned flags, void (*fn)(void *), void *arg);
|
||||
size_t mg_print_ip(void (*out)(char, void *), void *ptr, va_list *ap);
|
||||
size_t mg_print_ip_port(void (*out)(char, void *), void *ptr, va_list *ap);
|
||||
|
||||
// Low-level IO primives used by TLS layer
|
||||
enum { MG_IO_ERR = -1, MG_IO_WAIT = -2, MG_IO_RESET = -3 };
|
||||
|
11
src/sock.c
11
src/sock.c
@ -101,8 +101,8 @@ static void iolog(struct mg_connection *c, char *buf, long n, bool r) {
|
||||
union usa usa;
|
||||
socklen_t slen = sizeof(usa.sin);
|
||||
if (getsockname(FD(c), &usa.sa, &slen) < 0) (void) 0; // Ignore result
|
||||
MG_INFO(("\n-- %lu %I %s %I %ld", c->id, 4, &usa.sin.sin_addr,
|
||||
r ? "<-" : "->", 4, &c->rem.ip, n));
|
||||
MG_INFO(("\n-- %lu %M %s %M %ld", c->id, mg_print_ip_port, &c->loc,
|
||||
r ? "<-" : "->", mg_print_ip_port, &c->rem, n));
|
||||
|
||||
mg_hexdump(buf, (size_t) n);
|
||||
}
|
||||
@ -357,8 +357,7 @@ void mg_connect_resolved(struct mg_connection *c) {
|
||||
if ((rc = connect(FD(c), &usa.sa, slen)) == 0) {
|
||||
mg_call(c, MG_EV_CONNECT, NULL);
|
||||
} else if (mg_sock_would_block()) {
|
||||
MG_DEBUG(("%lu %p -> %I:%hu pend", c->id, c->fd, 4, &c->rem.ip,
|
||||
mg_ntohs(c->rem.port)));
|
||||
MG_DEBUG(("%lu %p -> %M pend", c->id, c->fd, mg_print_ip_port, &c->rem));
|
||||
c->is_connecting = 1;
|
||||
} else {
|
||||
mg_error(c, "connect: %d", MG_SOCKET_ERRNO);
|
||||
@ -413,8 +412,8 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) {
|
||||
c->pfn_data = lsn->pfn_data;
|
||||
c->fn = lsn->fn;
|
||||
c->fn_data = lsn->fn_data;
|
||||
MG_DEBUG(("%lu %p accepted %I.%hu -> %I.%hu", c->id, c->fd, 4, &c->rem.ip,
|
||||
mg_ntohs(c->rem.port), 4, &c->loc.ip, mg_ntohs(c->loc.port)));
|
||||
MG_DEBUG(("%lu %p accepted %M -> %M", c->id, c->fd, mg_print_ip_port,
|
||||
&c->rem, mg_print_ip_port, &c->loc));
|
||||
mg_call(c, MG_EV_OPEN, NULL);
|
||||
mg_call(c, MG_EV_ACCEPT, NULL);
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ void mg_tls_free(struct mg_connection *c) {
|
||||
|
||||
static int mg_net_send(void *ctx, const unsigned char *buf, size_t len) {
|
||||
long n = mg_io_send((struct mg_connection *) ctx, buf, len);
|
||||
MG_VERBOSE(("%lu n=%ld", ((struct mg_connection *) ctx)->id, n));
|
||||
MG_VERBOSE(("%lu n=%ld e=%d", ((struct mg_connection *) ctx)->id, n, errno));
|
||||
if (n == MG_IO_WAIT) return MBEDTLS_ERR_SSL_WANT_WRITE;
|
||||
if (n == MG_IO_RESET) return MBEDTLS_ERR_NET_CONN_RESET;
|
||||
if (n == MG_IO_ERR) return MBEDTLS_ERR_NET_SEND_FAILED;
|
||||
@ -66,7 +66,7 @@ static int mbed_rng(void *ctx, unsigned char *buf, size_t len) {
|
||||
|
||||
static void debug_cb(void *c, int lev, const char *s, int n, const char *s2) {
|
||||
n = (int) strlen(s2) - 1;
|
||||
MG_VERBOSE(("%lu %d %.*s", ((struct mg_connection *) c)->id, lev, n, s2));
|
||||
MG_INFO(("%lu %d %.*s", ((struct mg_connection *) c)->id, lev, n, s2));
|
||||
(void) s;
|
||||
}
|
||||
|
||||
|
@ -14,10 +14,15 @@ uint64_t mg_millis(void);
|
||||
#define mg_htons(x) mg_ntohs(x)
|
||||
#define mg_htonl(x) mg_ntohl(x)
|
||||
|
||||
#define MG_U32(a, b, c, d) \
|
||||
#define MG_U32(a, b, c, d) \
|
||||
(((uint32_t) ((a) &255) << 24) | ((uint32_t) ((b) &255) << 16) | \
|
||||
((uint32_t) ((c) &255) << 8) | (uint32_t) ((d) &255))
|
||||
|
||||
// For printing IPv4 addresses: printf("%d.%d.%d.%d\n", MG_IPADDR_PARTS(&ip))
|
||||
#define MG_U8P(ADDR) ((uint8_t *) (ADDR))
|
||||
#define MG_IPADDR_PARTS(ADDR) \
|
||||
MG_U8P(ADDR)[0], MG_U8P(ADDR)[1], MG_U8P(ADDR)[2], MG_U8P(ADDR)[3]
|
||||
|
||||
// Linked list management macros
|
||||
#define LIST_ADD_HEAD(type_, head_, elem_) \
|
||||
do { \
|
||||
|
@ -360,8 +360,8 @@ static void mqtt_cb(struct mg_connection *c, int ev, void *evd, void *fnd) {
|
||||
}
|
||||
} else if (ev == MG_EV_MQTT_MSG) {
|
||||
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) evd;
|
||||
snprintf(buf + 1, test_data->bufsize, "%.*s/%.*s", (int) mm->topic.len, mm->topic.ptr,
|
||||
(int) mm->data.len, mm->data.ptr);
|
||||
snprintf(buf + 1, test_data->bufsize, "%.*s/%.*s", (int) mm->topic.len,
|
||||
mm->topic.ptr, (int) mm->data.len, mm->data.ptr);
|
||||
}
|
||||
(void) c;
|
||||
}
|
||||
@ -1284,9 +1284,7 @@ static void test_http_range(void) {
|
||||
ASSERT(mgr.conns == NULL);
|
||||
}
|
||||
|
||||
static void f1(void *arg) {
|
||||
(*(int *) arg)++;
|
||||
}
|
||||
static void f1(void *arg) { (*(int *) arg)++; }
|
||||
|
||||
static void test_timer(void) {
|
||||
int v1 = 0, v2 = 0, v3 = 0;
|
||||
@ -1641,16 +1639,24 @@ static void test_str(void) {
|
||||
|
||||
{
|
||||
char buf[100];
|
||||
struct mg_addr a = {0, mg_htonl(0x10111213), {1, 100, 33}, false};
|
||||
ASSERT(mg_snprintf(buf, sizeof(buf), "%I", 4, &a.ip) == 11);
|
||||
ASSERT(strcmp(buf, "16.17.18.19") == 0);
|
||||
ASSERT(mg_snprintf(buf, sizeof(buf), "%I", 6, &a.ip6) == 20);
|
||||
ASSERT(strcmp(buf, "164:2100:0:0:0:0:0:0") == 0);
|
||||
struct mg_addr a = {mg_htons(3), mg_htonl(0x2000001), {1, 100, 33}, false};
|
||||
ASSERT(mg_snprintf(buf, sizeof(buf), "%M %d", mg_print_ip, &a, 7) == 9);
|
||||
ASSERT(strcmp(buf, "2.0.0.1 7") == 0);
|
||||
ASSERT(mg_snprintf(buf, sizeof(buf), "%M %d", mg_print_ip_port, &a, 7) ==
|
||||
11);
|
||||
ASSERT(strcmp(buf, "2.0.0.1:3 7") == 0);
|
||||
a.is_ip6 = true;
|
||||
ASSERT(mg_snprintf(buf, sizeof(buf), "%M %d", mg_print_ip, &a, 7) == 24);
|
||||
ASSERT(strcmp(buf, "[164:2100:0:0:0:0:0:0] 7") == 0);
|
||||
ASSERT(mg_snprintf(buf, sizeof(buf), "%M %d", mg_print_ip_port, &a, 7) ==
|
||||
26);
|
||||
ASSERT(strcmp(buf, "[164:2100:0:0:0:0:0:0]:3 7") == 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void fn1(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (ev == MG_EV_ERROR) *(char **) fn_data = mg_mprintf("%s", (char *) ev_data);
|
||||
if (ev == MG_EV_ERROR)
|
||||
*(char **) fn_data = mg_mprintf("%s", (char *) ev_data);
|
||||
(void) c;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user