diff --git a/examples/websocket/WebSockCallbacks.c b/examples/websocket/WebSockCallbacks.c index ebcbda73..0b24e545 100644 --- a/examples/websocket/WebSockCallbacks.c +++ b/examples/websocket/WebSockCallbacks.c @@ -49,7 +49,7 @@ void websocket_ready_handler(struct mg_connection *conn) { break; } } - printf("\nNew websocket attached: %08lx:%u\n", rq->remote_ip, rq->remote_port); + printf("\nNew websocket attached: %s:%u\n", rq->remote_addr, rq->remote_port); mg_unlock_context(ctx); } @@ -66,7 +66,7 @@ static void websocket_done(tWebSockContext *ws_ctx, tWebSockInfo * wsock) { break; } } - printf("\nClose websocket attached: %08lx:%u\n", mg_get_request_info(wsock->conn)->remote_ip, mg_get_request_info(wsock->conn)->remote_port); + printf("\nClose websocket attached: %s:%u\n", mg_get_request_info(wsock->conn)->remote_addr, mg_get_request_info(wsock->conn)->remote_port); free(wsock); } } diff --git a/include/civetweb.h b/include/civetweb.h index 27cfe562..538500dd 100644 --- a/include/civetweb.h +++ b/include/civetweb.h @@ -61,7 +61,9 @@ struct mg_request_info { NULL */ const char *remote_user; /* Authenticated user, or NULL if no auth used */ - long remote_ip; /* Client's IP address */ + const char remote_addr[48]; /* Client's IP address as a string. */ + long remote_ip; /* Client's IP address. Deprecated: use remote_addr instead */ + long long content_length; /* Length (in bytes) of the request body, can be -1 if no length was given. */ int remote_port; /* Client's port */ diff --git a/src/civetweb.c b/src/civetweb.c index 2c079e28..8ced547c 100644 --- a/src/civetweb.c +++ b/src/civetweb.c @@ -3957,7 +3957,7 @@ static int parse_http_message(char *buf, int len, struct mg_request_info *ri) { int is_request, request_length = get_request_len(buf, len); if (request_length > 0) { - /* Reset attributes. DO NOT TOUCH is_ssl, remote_ip, remote_port */ + /* Reset attributes. DO NOT TOUCH is_ssl, remote_ip, remote_addr, remote_port */ ri->remote_user = ri->request_method = ri->uri = ri->http_version = NULL; ri->num_headers = 0; @@ -6687,9 +6687,12 @@ static void *worker_thread_run(void *thread_func_param) Thanks to Johannes Winkelmann for the patch. TODO(lsm): Fix IPv6 case */ conn->request_info.remote_port = ntohs(conn->client.rsa.sin.sin_port); + sockaddr_to_string(conn->request_info.remote_addr, sizeof(conn->request_info.remote_addr), &conn->client.rsa); +/* TODO: #if defined(MG_LEGACY_INTERFACE) */ memcpy(&conn->request_info.remote_ip, &conn->client.rsa.sin.sin_addr.s_addr, 4); conn->request_info.remote_ip = ntohl(conn->request_info.remote_ip); +/* #endif */ conn->request_info.is_ssl = conn->client.is_ssl; if (!conn->client.is_ssl diff --git a/src/mod_lua.inl b/src/mod_lua.inl index bc95a17d..407cbfda 100644 --- a/src/mod_lua.inl +++ b/src/mod_lua.inl @@ -894,12 +894,9 @@ enum { static void prepare_lua_request_info(struct mg_connection *conn, lua_State *L) { - char src_addr[IP_ADDR_STR_LEN] = ""; const char *s; int i; - sockaddr_to_string(src_addr, sizeof(src_addr), &conn->client.rsa); - /* Export mg.request_info */ lua_pushstring(L, "request_info"); lua_newtable(L); @@ -907,8 +904,10 @@ static void prepare_lua_request_info(struct mg_connection *conn, lua_State *L) reg_string(L, "uri", conn->request_info.uri); reg_string(L, "http_version", conn->request_info.http_version); reg_string(L, "query_string", conn->request_info.query_string); +#if defined(MG_LEGACY_INTERFACE) reg_int(L, "remote_ip", conn->request_info.remote_ip); /* remote_ip is deprecated, use remote_addr instead */ - reg_string(L, "remote_addr", src_addr); +#endif + reg_string(L, "remote_addr", conn->request_info.remote_addr); /* TODO: ip version */ reg_int(L, "remote_port", conn->request_info.remote_port); reg_int(L, "num_headers", conn->request_info.num_headers); diff --git a/test/unit_test.c b/test/unit_test.c index 05a9b3bc..169aedcc 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -851,7 +851,7 @@ static int api_callback(struct mg_connection *conn) { ASSERT(mg_read(conn, post_data, sizeof(post_data)) == 3); ASSERT(memcmp(post_data, "b=1", 3) == 0); ASSERT(ri->query_string != NULL); - ASSERT(ri->remote_ip > 0); + ASSERT(ri->remote_addr[0] != 0); ASSERT(ri->remote_port > 0); ASSERT(strcmp(ri->http_version, "1.0") == 0);