Commit 33c56b0e authored by Sergey Lyubka's avatar Sergey Lyubka

Init local_ip and local_port properly in mg_connection

parent e62e4839
...@@ -229,7 +229,7 @@ int ns_vprintf(struct ns_connection *, const char *fmt, va_list ap); ...@@ -229,7 +229,7 @@ int ns_vprintf(struct ns_connection *, const char *fmt, va_list ap);
void *ns_start_thread(void *(*f)(void *), void *p); void *ns_start_thread(void *(*f)(void *), void *p);
int ns_socketpair(sock_t [2]); int ns_socketpair(sock_t [2]);
void ns_set_close_on_exec(sock_t); void ns_set_close_on_exec(sock_t);
void ns_sock_to_str(sock_t sock, char *buf, size_t len, int add_port); void ns_sock_to_str(sock_t sock, char *buf, size_t len, int flags);
int ns_hexdump(const void *buf, int len, char *dst, int dst_len); int ns_hexdump(const void *buf, int len, char *dst, int dst_len);
#ifdef __cplusplus #ifdef __cplusplus
...@@ -613,14 +613,19 @@ static int ns_is_error(int n) { ...@@ -613,14 +613,19 @@ static int ns_is_error(int n) {
); );
} }
void ns_sock_to_str(sock_t sock, char *buf, size_t len, int add_port) { void ns_sock_to_str(sock_t sock, char *buf, size_t len, int flags) {
union socket_address sa; union socket_address sa;
socklen_t slen = sizeof(sa); socklen_t slen = sizeof(sa);
if (buf != NULL && len > 0) { if (buf != NULL && len > 0) {
buf[0] = '\0'; buf[0] = '\0';
memset(&sa, 0, sizeof(sa)); memset(&sa, 0, sizeof(sa));
if (flags & 4) {
getpeername(sock, &sa.sa, &slen);
} else {
getsockname(sock, &sa.sa, &slen); getsockname(sock, &sa.sa, &slen);
}
if (flags & 1) {
#if defined(NS_ENABLE_IPV6) #if defined(NS_ENABLE_IPV6)
inet_ntop(sa.sa.sa_family, sa.sa.sa_family == AF_INET ? inet_ntop(sa.sa.sa_family, sa.sa.sa_family == AF_INET ?
(void *) &sa.sin.sin_addr : (void *) &sa.sin.sin_addr :
...@@ -631,7 +636,8 @@ void ns_sock_to_str(sock_t sock, char *buf, size_t len, int add_port) { ...@@ -631,7 +636,8 @@ void ns_sock_to_str(sock_t sock, char *buf, size_t len, int add_port) {
#else #else
inet_ntop(sa.sa.sa_family, (void *) &sa.sin.sin_addr, buf, len); inet_ntop(sa.sa.sa_family, (void *) &sa.sin.sin_addr, buf, len);
#endif #endif
if (add_port) { }
if (flags & 2) {
snprintf(buf + strlen(buf), len - (strlen(buf) + 1), ":%d", snprintf(buf + strlen(buf), len - (strlen(buf) + 1), ":%d",
(int) ntohs(sa.sin.sin_port)); (int) ntohs(sa.sin.sin_port));
} }
...@@ -1174,7 +1180,6 @@ struct mg_server { ...@@ -1174,7 +1180,6 @@ struct mg_server {
union socket_address lsa; // Listening socket address union socket_address lsa; // Listening socket address
mg_handler_t event_handler; mg_handler_t event_handler;
char *config_options[NUM_OPTIONS]; char *config_options[NUM_OPTIONS];
char local_ip[48];
}; };
// Local endpoint representation // Local endpoint representation
...@@ -1774,7 +1779,7 @@ static void prepare_cgi_environment(struct connection *conn, ...@@ -1774,7 +1779,7 @@ static void prepare_cgi_environment(struct connection *conn,
if ((s = getenv("SERVER_NAME")) != NULL) { if ((s = getenv("SERVER_NAME")) != NULL) {
addenv(blk, "SERVER_NAME=%s", s); addenv(blk, "SERVER_NAME=%s", s);
} else { } else {
addenv(blk, "SERVER_NAME=%s", conn->server->local_ip); addenv(blk, "SERVER_NAME=%s", ri->local_ip);
} }
addenv(blk, "SERVER_ROOT=%s", opts[DOCUMENT_ROOT]); addenv(blk, "SERVER_ROOT=%s", opts[DOCUMENT_ROOT]);
addenv(blk, "DOCUMENT_ROOT=%s", opts[DOCUMENT_ROOT]); addenv(blk, "DOCUMENT_ROOT=%s", opts[DOCUMENT_ROOT]);
...@@ -4441,8 +4446,6 @@ const char *mg_set_option(struct mg_server *server, const char *name, ...@@ -4441,8 +4446,6 @@ const char *mg_set_option(struct mg_server *server, const char *name,
if (port < 0) { if (port < 0) {
error_msg = "Cannot bind to port"; error_msg = "Cannot bind to port";
} else { } else {
ns_sock_to_str(server->ns_server.listening_sock, server->local_ip,
sizeof(server->local_ip), 0);
if (!strcmp(value, "0")) { if (!strcmp(value, "0")) {
char buf[10]; char buf[10];
mg_snprintf(buf, sizeof(buf), "%d", port); mg_snprintf(buf, sizeof(buf), "%d", port);
...@@ -4477,6 +4480,16 @@ const char *mg_set_option(struct mg_server *server, const char *name, ...@@ -4477,6 +4480,16 @@ const char *mg_set_option(struct mg_server *server, const char *name,
return error_msg; return error_msg;
} }
static void set_ips(struct connection *conn, int is_rem) {
struct mg_connection *c = &conn->mg_conn;
char buf[100];
ns_sock_to_str(conn->ns_conn->sock, buf, sizeof(buf), is_rem ? 7 : 3);
sscanf(buf, "%47[^:]:%hu",
is_rem ? c->remote_ip : c->local_ip,
is_rem ? &c->remote_port : &c->local_port);
}
static void on_accept(struct ns_connection *nc, union socket_address *sa) { static void on_accept(struct ns_connection *nc, union socket_address *sa) {
struct mg_server *server = (struct mg_server *) nc->server; struct mg_server *server = (struct mg_server *) nc->server;
struct connection *conn; struct connection *conn;
...@@ -4486,17 +4499,15 @@ static void on_accept(struct ns_connection *nc, union socket_address *sa) { ...@@ -4486,17 +4499,15 @@ static void on_accept(struct ns_connection *nc, union socket_address *sa) {
(conn = (struct connection *) calloc(1, sizeof(*conn))) == NULL) { (conn = (struct connection *) calloc(1, sizeof(*conn))) == NULL) {
nc->flags |= NSF_CLOSE_IMMEDIATELY; nc->flags |= NSF_CLOSE_IMMEDIATELY;
} else { } else {
conn->server = server;
ns_sock_to_str(nc->sock, conn->mg_conn.remote_ip,
sizeof(conn->mg_conn.remote_ip), 0);
conn->mg_conn.remote_port = ntohs(sa->sin.sin_port);
conn->mg_conn.server_param = nc->server->server_data;
conn->mg_conn.local_ip = server->local_ip;
conn->mg_conn.local_port = ntohs(server->lsa.sin.sin_port);
// Circularly link two connection structures // Circularly link two connection structures
nc->connection_data = conn; nc->connection_data = conn;
conn->ns_conn = nc; conn->ns_conn = nc;
// Initialize the rest of connection attributes
conn->server = server;
conn->mg_conn.server_param = nc->server->server_data;
set_ips(conn, 1);
set_ips(conn, 0);
} }
} }
......
...@@ -37,7 +37,7 @@ struct mg_connection { ...@@ -37,7 +37,7 @@ struct mg_connection {
const char *query_string; // URL part after '?', not including '?', or NULL const char *query_string; // URL part after '?', not including '?', or NULL
char remote_ip[48]; // Max IPv6 string length is 45 characters char remote_ip[48]; // Max IPv6 string length is 45 characters
const char *local_ip; // Local IP address char local_ip[48]; // Local IP address
unsigned short remote_port; // Client's port unsigned short remote_port; // Client's port
unsigned short local_port; // Local port number unsigned short local_port; // Local port number
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment