Explorar o código

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

bel %!s(int64=10) %!d(string=hai) anos
pai
achega
154b80bb69
Modificáronse 5 ficheiros con 13 adicións e 9 borrados
  1. 2 2
      examples/websocket/WebSockCallbacks.c
  2. 3 1
      include/civetweb.h
  3. 4 1
      src/civetweb.c
  4. 3 4
      src/mod_lua.inl
  5. 1 1
      test/unit_test.c

+ 2 - 2
examples/websocket/WebSockCallbacks.c

@@ -49,7 +49,7 @@ void websocket_ready_handler(struct mg_connection *conn) {
             break;
             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);
     mg_unlock_context(ctx);
 }
 }
 
 
@@ -66,7 +66,7 @@ static void websocket_done(tWebSockContext *ws_ctx, tWebSockInfo * wsock) {
                 break;
                 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);
         free(wsock);
     }
     }
 }
 }

+ 3 - 1
include/civetweb.h

@@ -61,7 +61,9 @@ struct mg_request_info {
                                    NULL */
                                    NULL */
     const char *remote_user;    /* Authenticated user, or NULL if no auth
     const char *remote_user;    /* Authenticated user, or NULL if no auth
                                    used */
                                    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,
     long long content_length;   /* Length (in bytes) of the request body,
                                    can be -1 if no length was given. */
                                    can be -1 if no length was given. */
     int remote_port;            /* Client's port */
     int remote_port;            /* Client's port */

+ 4 - 1
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);
     int is_request, request_length = get_request_len(buf, len);
     if (request_length > 0) {
     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->remote_user = ri->request_method = ri->uri = ri->http_version = NULL;
         ri->num_headers = 0;
         ri->num_headers = 0;
 
 
@@ -6687,9 +6687,12 @@ static void *worker_thread_run(void *thread_func_param)
                Thanks to Johannes Winkelmann for the patch.
                Thanks to Johannes Winkelmann for the patch.
                TODO(lsm): Fix IPv6 case */
                TODO(lsm): Fix IPv6 case */
             conn->request_info.remote_port = ntohs(conn->client.rsa.sin.sin_port);
             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,
             memcpy(&conn->request_info.remote_ip,
                    &conn->client.rsa.sin.sin_addr.s_addr, 4);
                    &conn->client.rsa.sin.sin_addr.s_addr, 4);
             conn->request_info.remote_ip = ntohl(conn->request_info.remote_ip);
             conn->request_info.remote_ip = ntohl(conn->request_info.remote_ip);
+/* #endif */
             conn->request_info.is_ssl = conn->client.is_ssl;
             conn->request_info.is_ssl = conn->client.is_ssl;
 
 
             if (!conn->client.is_ssl
             if (!conn->client.is_ssl

+ 3 - 4
src/mod_lua.inl

@@ -894,12 +894,9 @@ enum {
 
 
 static void prepare_lua_request_info(struct mg_connection *conn, lua_State *L)
 static void prepare_lua_request_info(struct mg_connection *conn, lua_State *L)
 {
 {
-    char src_addr[IP_ADDR_STR_LEN] = "";
     const char *s;
     const char *s;
     int i;
     int i;
 
 
-    sockaddr_to_string(src_addr, sizeof(src_addr), &conn->client.rsa);
-
     /* Export mg.request_info */
     /* Export mg.request_info */
     lua_pushstring(L, "request_info");
     lua_pushstring(L, "request_info");
     lua_newtable(L);
     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, "uri", conn->request_info.uri);
     reg_string(L, "http_version", conn->request_info.http_version);
     reg_string(L, "http_version", conn->request_info.http_version);
     reg_string(L, "query_string", conn->request_info.query_string);
     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_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 */
     /* TODO: ip version */
     reg_int(L, "remote_port", conn->request_info.remote_port);
     reg_int(L, "remote_port", conn->request_info.remote_port);
     reg_int(L, "num_headers", conn->request_info.num_headers);
     reg_int(L, "num_headers", conn->request_info.num_headers);

+ 1 - 1
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(mg_read(conn, post_data, sizeof(post_data)) == 3);
     ASSERT(memcmp(post_data, "b=1", 3) == 0);
     ASSERT(memcmp(post_data, "b=1", 3) == 0);
     ASSERT(ri->query_string != NULL);
     ASSERT(ri->query_string != NULL);
-    ASSERT(ri->remote_ip > 0);
+    ASSERT(ri->remote_addr[0] != 0);
     ASSERT(ri->remote_port > 0);
     ASSERT(ri->remote_port > 0);
     ASSERT(strcmp(ri->http_version, "1.0") == 0);
     ASSERT(strcmp(ri->http_version, "1.0") == 0);