1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-08-07 16:02:55 +03:00

Make client address available in mg_request_info also for IPv6 (see also issue #33)

This commit is contained in:
bel
2014-09-14 11:04:42 +02:00
parent ec902803d5
commit 154b80bb69
5 changed files with 13 additions and 9 deletions

View File

@@ -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);
}
}

View File

@@ -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 */

View File

@@ -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

View File

@@ -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);

View File

@@ -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);