Put getting local/remote address behinf net_if

PUBLISHED_FROM=f88056e79b632d9eee288dacf61c2f87ff3ceca5
This commit is contained in:
Deomid Ryabkov 2015-11-27 10:32:36 +00:00 committed by Sergey Lyubka
parent 6c77351c58
commit 7951dac9d5
2 changed files with 79 additions and 45 deletions

View File

@ -1865,12 +1865,16 @@ MG_INTERNAL void mg_call(struct mg_connection *nc,
ev_handler == nc->handler ? "user" : "proto", ev, ev_data, nc->flags, ev_handler == nc->handler ? "user" : "proto", ev, ev_data, nc->flags,
(int) nc->recv_mbuf.len, (int) nc->send_mbuf.len)); (int) nc->recv_mbuf.len, (int) nc->send_mbuf.len));
#ifndef MG_DISABLE_FILESYSTEM #if !defined(NO_LIBC) && !defined(MG_DISABLE_HEXDUMP)
/* LCOV_EXCL_START */ /* LCOV_EXCL_START */
if (nc->mgr->hexdump_file != NULL && ev != MG_EV_POLL && if (nc->mgr->hexdump_file != NULL && ev != MG_EV_POLL &&
ev != MG_EV_SEND /* handled separately */) { ev != MG_EV_SEND /* handled separately */) {
int len = (ev == MG_EV_RECV ? *(int *) ev_data : 0); if (ev == MG_EV_RECV) {
mg_hexdump_connection(nc, nc->mgr->hexdump_file, len, ev); mg_hexdump_connection(nc, nc->mgr->hexdump_file, nc->recv_mbuf.buf,
*(int *) ev_data, ev);
} else {
mg_hexdump_connection(nc, nc->mgr->hexdump_file, NULL, 0, ev);
}
} }
/* LCOV_EXCL_STOP */ /* LCOV_EXCL_STOP */
#endif #endif
@ -2385,9 +2389,9 @@ void mg_send(struct mg_connection *nc, const void *buf, int len) {
} else { } else {
mg_if_tcp_send(nc, buf, len); mg_if_tcp_send(nc, buf, len);
} }
#ifndef MG_DISABLE_FILESYSTEM #if !defined(NO_LIBC) && !defined(MG_DISABLE_HEXDUMP)
if (nc->mgr && nc->mgr->hexdump_file != NULL) { if (nc->mgr && nc->mgr->hexdump_file != NULL) {
mg_hexdump_connection(nc, nc->mgr->hexdump_file, len, MG_EV_SEND); mg_hexdump_connection(nc, nc->mgr->hexdump_file, buf, len, MG_EV_SEND);
} }
#endif #endif
} }
@ -3464,23 +3468,32 @@ int mg_socketpair(sock_t sp[2], int sock_type) {
} }
#endif /* MG_DISABLE_SOCKETPAIR */ #endif /* MG_DISABLE_SOCKETPAIR */
void mg_sock_to_str(sock_t sock, char *buf, size_t len, int flags) { static void mg_sock_get_addr(sock_t sock, int remote,
union socket_address sa; union socket_address *sa) {
#ifndef MG_CC3200 #ifndef MG_CC3200
socklen_t slen = sizeof(sa); socklen_t slen = sizeof(sa);
#endif memset(sa, 0, sizeof(*sa));
if (remote) {
memset(&sa, 0, sizeof(sa)); getpeername(sock, &sa->sa, &slen);
#ifndef MG_CC3200
if (flags & MG_SOCK_STRINGIFY_REMOTE) {
getpeername(sock, &sa.sa, &slen);
} else { } else {
getsockname(sock, &sa.sa, &slen); getsockname(sock, &sa->sa, &slen);
} }
#else
memset(sa, 0, sizeof(*sa));
#endif #endif
}
void mg_sock_to_str(sock_t sock, char *buf, size_t len, int flags) {
union socket_address sa;
mg_sock_get_addr(sock, flags & MG_SOCK_STRINGIFY_REMOTE, &sa);
mg_sock_addr_to_str(&sa, buf, len, flags); mg_sock_addr_to_str(&sa, buf, len, flags);
} }
void mg_if_get_conn_addr(struct mg_connection *nc, int remote,
union socket_address *sa) {
mg_sock_get_addr(nc->sock, remote, sa);
}
#endif /* !MG_DISABLE_SOCKET_IF */ #endif /* !MG_DISABLE_SOCKET_IF */
#ifdef NS_MODULE_LINES #ifdef NS_MODULE_LINES
#line 1 "src/multithreading.c" #line 1 "src/multithreading.c"
@ -6495,6 +6508,15 @@ void mg_sock_addr_to_str(const union socket_address *sa, char *buf, size_t len,
} }
} }
void mg_conn_addr_to_str(struct mg_connection *nc, char *buf, size_t len,
int flags) {
union socket_address sa;
memset(&sa, 0, sizeof(sa));
mg_if_get_conn_addr(nc, flags & MG_SOCK_STRINGIFY_REMOTE, &sa);
mg_sock_addr_to_str(&sa, buf, len, flags);
}
#ifndef MG_DISABLE_HEXDUMP
int mg_hexdump(const void *buf, int len, char *dst, int dst_len) { int mg_hexdump(const void *buf, int len, char *dst, int dst_len) {
const unsigned char *p = (const unsigned char *) buf; const unsigned char *p = (const unsigned char *) buf;
char ascii[17] = ""; char ascii[17] = "";
@ -6516,6 +6538,7 @@ int mg_hexdump(const void *buf, int len, char *dst, int dst_len) {
return n; return n;
} }
#endif
int mg_avprintf(char **buf, size_t size, const char *fmt, va_list ap) { int mg_avprintf(char **buf, size_t size, const char *fmt, va_list ap) {
va_list ap_copy; va_list ap_copy;
@ -6553,23 +6576,29 @@ int mg_avprintf(char **buf, size_t size, const char *fmt, va_list ap) {
return len; return len;
} }
#ifndef MG_DISABLE_FILESYSTEM #if !defined(NO_LIBC) && !defined(MG_DISABLE_HEXDUMP)
void mg_hexdump_connection(struct mg_connection *nc, const char *path, void mg_hexdump_connection(struct mg_connection *nc, const char *path,
int num_bytes, int ev) { const void *buf, int num_bytes, int ev) {
const struct mbuf *io = ev == MG_EV_SEND ? &nc->send_mbuf : &nc->recv_mbuf; FILE *fp = NULL;
FILE *fp; char *hexbuf, src[60], dst[60];
char *buf, src[60], dst[60];
int buf_size = num_bytes * 5 + 100; int buf_size = num_bytes * 5 + 100;
if ((fp = fopen(path, "a")) != NULL) { if (strcmp(path, "-") == 0) {
#ifndef MG_DISABLE_SOCKET_IF fp = stdout;
mg_sock_to_str(nc->sock, src, sizeof(src), 3); } else if (strcmp(path, "--") == 0) {
mg_sock_to_str(nc->sock, dst, sizeof(dst), 7); fp = stderr;
#else #ifndef MG_DISABLE_FILESYSTEM
/* TODO (alashkin): should we request info from net_if? */ } else {
strcpy(src, "n/a"); fp = fopen(path, "a");
strcpy(dst, "n/a");
#endif #endif
}
if (fp == NULL) return;
mg_conn_addr_to_str(nc, src, sizeof(src),
MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
mg_conn_addr_to_str(nc, dst, sizeof(dst), MG_SOCK_STRINGIFY_IP |
MG_SOCK_STRINGIFY_PORT |
MG_SOCK_STRINGIFY_REMOTE);
fprintf( fprintf(
fp, "%lu %p %s %s %s %d\n", (unsigned long) time(NULL), nc, src, fp, "%lu %p %s %s %s %d\n", (unsigned long) time(NULL), nc, src,
ev == MG_EV_RECV ? "<-" : ev == MG_EV_SEND ev == MG_EV_RECV ? "<-" : ev == MG_EV_SEND
@ -6578,15 +6607,12 @@ void mg_hexdump_connection(struct mg_connection *nc, const char *path,
? "<A" ? "<A"
: ev == MG_EV_CONNECT ? "C>" : "XX", : ev == MG_EV_CONNECT ? "C>" : "XX",
dst, num_bytes); dst, num_bytes);
if (num_bytes > 0 && (buf = (char *) MG_MALLOC(buf_size)) != NULL) { if (num_bytes > 0 && (hexbuf = (char *) MG_MALLOC(buf_size)) != NULL) {
mg_hexdump(io->buf + (ev == MG_EV_SEND ? 0 : io->len) - mg_hexdump(buf, num_bytes, hexbuf, buf_size);
(ev == MG_EV_SEND ? 0 : num_bytes), fprintf(fp, "%s", hexbuf);
num_bytes, buf, buf_size); MG_FREE(hexbuf);
fprintf(fp, "%s", buf);
MG_FREE(buf);
}
fclose(fp);
} }
if (fp != stdin && fp != stdout) fclose(fp);
} }
#endif #endif

View File

@ -1117,6 +1117,10 @@ void mg_if_destroy_conn(struct mg_connection *nc);
void mg_close_conn(struct mg_connection *nc); void mg_close_conn(struct mg_connection *nc);
/* Put connection's address into *sa, local (remote = 0) or remote. */
void mg_if_get_conn_addr(struct mg_connection *nc, int remote,
union socket_address *sa);
#endif /* MG_NET_IF_HEADER_INCLUDED */ #endif /* MG_NET_IF_HEADER_INCLUDED */
/* /*
* Copyright (c) 2014 Cesanta Software Limited * Copyright (c) 2014 Cesanta Software Limited
@ -1242,7 +1246,7 @@ void mg_set_close_on_exec(sock_t);
#define MG_SOCK_STRINGIFY_PORT 2 #define MG_SOCK_STRINGIFY_PORT 2
#define MG_SOCK_STRINGIFY_REMOTE 4 #define MG_SOCK_STRINGIFY_REMOTE 4
/* /*
* Convert socket's local or remote address into string. * Convert connection's local or remote address into string.
* *
* The `flags` parameter is a bit mask that controls the behavior, * The `flags` parameter is a bit mask that controls the behavior,
* see `MG_SOCK_STRINGIFY_*` definitions. * see `MG_SOCK_STRINGIFY_*` definitions.
@ -1254,7 +1258,11 @@ void mg_set_close_on_exec(sock_t);
* If both port number and IP address are printed, they are separated by `:`. * If both port number and IP address are printed, they are separated by `:`.
* If compiled with `-DMG_ENABLE_IPV6`, IPv6 addresses are supported. * If compiled with `-DMG_ENABLE_IPV6`, IPv6 addresses are supported.
*/ */
void mg_conn_addr_to_str(struct mg_connection *nc, char *buf, size_t len,
int flags);
#ifndef MG_DISABLE_SOCKET_IF /* Legacy interface. */
void mg_sock_to_str(sock_t sock, char *buf, size_t len, int flags); void mg_sock_to_str(sock_t sock, char *buf, size_t len, int flags);
#endif
/* /*
* Convert socket's address into string. * Convert socket's address into string.
@ -1282,7 +1290,7 @@ int mg_hexdump(const void *buf, int len, char *dst, int dst_len);
* event handler. * event handler.
*/ */
void mg_hexdump_connection(struct mg_connection *nc, const char *path, void mg_hexdump_connection(struct mg_connection *nc, const char *path,
int num_bytes, int ev); const void *buf, int num_bytes, int ev);
/* /*
* Print message to buffer. If buffer is large enough to hold the message, * Print message to buffer. If buffer is large enough to hold the message,
* return buffer. If buffer is to small, allocate large enough buffer on heap, * return buffer. If buffer is to small, allocate large enough buffer on heap,