Commit 4576b69e authored by Sergey Lyubka's avatar Sergey Lyubka

Initializing remote_ip and remote_port properly

parent 4fd6eb59
...@@ -359,14 +359,10 @@ static const char *test_next_option(void) { ...@@ -359,14 +359,10 @@ static const char *test_next_option(void) {
} }
static int cb1(struct mg_connection *conn) { static int cb1(struct mg_connection *conn) {
char buf[100];
// We're not sending HTTP headers here, to make testing easier // We're not sending HTTP headers here, to make testing easier
snprintf(buf, sizeof(buf), "%s %s", mg_printf(conn, "%s %s %s",
conn->server_param == NULL ? "?" : (char *) conn->server_param, conn->server_param == NULL ? "?" : (char *) conn->server_param,
conn->connection_param == NULL ? "?" : "!"); conn->connection_param == NULL ? "?" : "!", conn->remote_ip);
mg_write(conn, buf, strlen(buf));
return 1; return 1;
} }
...@@ -401,9 +397,10 @@ static const char *test_server_param(void) { ...@@ -401,9 +397,10 @@ static const char *test_server_param(void) {
reply = wget("127.0.0.1", atoi(HTTP_PORT), &reply_len, "%s", reply = wget("127.0.0.1", atoi(HTTP_PORT), &reply_len, "%s",
"GET /cb1 HTTP/1.0\r\n\r\n"); "GET /cb1 HTTP/1.0\r\n\r\n");
ASSERT(reply != NULL); ASSERT(reply != NULL);
ASSERT(reply_len == 5); ASSERT(reply_len == 15);
ASSERT(memcmp(reply, "foo ?", 5) == 0); // cb1() does not send HTTP headers // cb1() does not send HTTP headers
printf("%d [%.*s]\n", reply_len, reply_len, reply); ASSERT(memcmp(reply, "foo ? 127.0.0.1", 5) == 0);
//printf("%d [%.*s]\n", reply_len, reply_len, reply);
free(reply); free(reply);
return NULL; return NULL;
......
...@@ -935,6 +935,23 @@ static int check_acl(const char *acl, uint32_t remote_ip) { ...@@ -935,6 +935,23 @@ static int check_acl(const char *acl, uint32_t remote_ip) {
return allowed == '+'; return allowed == '+';
} }
static void sockaddr_to_string(char *buf, size_t len,
const union socket_address *usa) {
buf[0] = '\0';
#if defined(USE_IPV6)
inet_ntop(usa->sa.sa_family, usa->sa.sa_family == AF_INET ?
(void *) &usa->sin.sin_addr :
(void *) &usa->sin6.sin6_addr, buf, len);
#elif defined(_WIN32)
// Only Windoze Vista (and newer) have inet_ntop()
strncpy(buf, inet_ntoa(usa->sin.sin_addr), len);
#else
inet_ntop(usa->sa.sa_family, (void *) &usa->sin.sin_addr, buf, len);
#endif
}
static struct connection *accept_new_connection(struct mg_server *server) { static struct connection *accept_new_connection(struct mg_server *server) {
union socket_address sa; union socket_address sa;
socklen_t len = sizeof(sa); socklen_t len = sizeof(sa);
...@@ -963,6 +980,10 @@ static struct connection *accept_new_connection(struct mg_server *server) { ...@@ -963,6 +980,10 @@ static struct connection *accept_new_connection(struct mg_server *server) {
set_non_blocking_mode(sock); set_non_blocking_mode(sock);
conn->server = server; conn->server = server;
conn->client_sock = sock; conn->client_sock = sock;
sockaddr_to_string(conn->mg_conn.remote_ip,
sizeof(conn->mg_conn.remote_ip), &sa);
conn->mg_conn.remote_port = ntohs(sa.sin.sin_port);
conn->mg_conn.server_param = server->server_data;
mutex_init(&conn->mutex); mutex_init(&conn->mutex);
LINKED_LIST_ADD_TO_FRONT(&server->active_connections, &conn->link); LINKED_LIST_ADD_TO_FRONT(&server->active_connections, &conn->link);
DBG(("added conn %p", conn)); DBG(("added conn %p", conn));
...@@ -1103,7 +1124,11 @@ static int is_valid_http_method(const char *method) { ...@@ -1103,7 +1124,11 @@ static int is_valid_http_method(const char *method) {
static int parse_http_message(char *buf, int len, struct mg_connection *ri) { static int parse_http_message(char *buf, int len, struct mg_connection *ri) {
int is_request, n; int is_request, n;
memset(ri, 0, sizeof(*ri)); // Reset the connection. Make sure that we don't touch fields that are
// set elsewhere: remote_ip, remote_port, server_param
ri->request_method = ri->uri = ri->http_version = ri->query_string = NULL;
ri->num_headers = ri->status_code = ri->is_websocket = ri->content_len = 0;
buf[len - 1] = '\0'; buf[len - 1] = '\0';
// RFC says that all initial whitespaces should be ingored // RFC says that all initial whitespaces should be ingored
...@@ -1822,10 +1847,6 @@ static void call_uri_handler_if_data_is_buffered(struct connection *conn) { ...@@ -1822,10 +1847,6 @@ static void call_uri_handler_if_data_is_buffered(struct connection *conn) {
struct iobuf *loc = &conn->local_iobuf; struct iobuf *loc = &conn->local_iobuf;
struct mg_connection *c = &conn->mg_conn; struct mg_connection *c = &conn->mg_conn;
// Header parsing does memset() on the whole mg_connection, nullifying
// connection_param and server_param. Set them just before calling back.
c->server_param = conn->server->server_data;
c->content = loc->buf; c->content = loc->buf;
#ifndef NO_WEBSOCKET #ifndef NO_WEBSOCKET
if (conn->mg_conn.is_websocket) { if (conn->mg_conn.is_websocket) {
...@@ -3166,21 +3187,6 @@ static void log_header(const struct mg_connection *conn, const char *header, ...@@ -3166,21 +3187,6 @@ static void log_header(const struct mg_connection *conn, const char *header,
} }
} }
static void sockaddr_to_string(char *buf, size_t len,
const union socket_address *usa) {
buf[0] = '\0';
#if defined(USE_IPV6)
inet_ntop(usa->sa.sa_family, usa->sa.sa_family == AF_INET ?
(void *) &usa->sin.sin_addr :
(void *) &usa->sin6.sin6_addr, buf, len);
#elif defined(_WIN32)
// Only Windoze Vista (and newer) have inet_ntop()
strncpy(buf, inet_ntoa(usa->sin.sin_addr), len);
#else
inet_ntop(usa->sa.sa_family, (void *) &usa->sin.sin_addr, buf, len);
#endif
}
static void log_access(const struct connection *conn, const char *path) { static void log_access(const struct connection *conn, const char *path) {
const struct mg_connection *c = &conn->mg_conn; const struct mg_connection *c = &conn->mg_conn;
FILE *fp = (path == NULL) ? NULL : fopen(path, "a+"); FILE *fp = (path == NULL) ? NULL : fopen(path, "a+");
......
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