Hi! Please, consider applying the patch for getting the IP-address of a client.

Regards,
Sergey
# HG changeset patch
# User Sergey Mironov <[email protected]>
# Date 1411459464 0
#      Tue Sep 23 08:04:24 2014 +0000
# Node ID 1ff5c342527da7a91d07339cfc69be97ba8f92ef
# Parent  a0a75a5b5199e32b2110672f232f0f4a3ef87650
Add Basis.clientIP function

diff --git a/include/urweb/urweb_cpp.h b/include/urweb/urweb_cpp.h
--- a/include/urweb/urweb_cpp.h
+++ b/include/urweb/urweb_cpp.h
@@ -92,6 +92,9 @@
 void uw_set_could_write_db(struct uw_context *, int);
 void uw_set_at_most_one_query(struct uw_context *, int);
 
+void uw_setSocket(struct uw_context *ctx, int sock);
+uw_Basis_string uw_Basis_clientIP(struct uw_context *ctx);
+
 char *uw_Basis_htmlifyInt(struct uw_context *, uw_Basis_int);
 char *uw_Basis_htmlifyFloat(struct uw_context *, uw_Basis_float);
 char *uw_Basis_htmlifyString(struct uw_context *, uw_Basis_string);
diff --git a/lib/ur/basis.urs b/lib/ur/basis.urs
--- a/lib/ur/basis.urs
+++ b/lib/ur/basis.urs
@@ -782,6 +782,7 @@
 val url : transaction page -> url
 val effectfulUrl : (option queryString -> transaction page) -> url
 val redirect : t ::: Type -> url -> transaction t
+val clientIP : transaction string
 
 type id
 val fresh : transaction id
diff --git a/src/c/request.c b/src/c/request.c
--- a/src/c/request.c
+++ b/src/c/request.c
@@ -478,6 +478,7 @@
 
   while (1) {
     uw_setQueryString(ctx, rc->queryString);
+    uw_setSocket(ctx, sock);
 
     if (!had_error) {
       size_t path_len = strlen(path);
diff --git a/src/c/urweb.c b/src/c/urweb.c
--- a/src/c/urweb.c
+++ b/src/c/urweb.c
@@ -13,6 +13,7 @@
 #include <stdint.h>
 #include <sys/types.h>
 #include <sys/socket.h>
+#include <netinet/in.h>
 #include <openssl/des.h>
 #include <openssl/rand.h>
 #include <time.h>
@@ -476,6 +477,10 @@
 
   char *output_buffer;
   size_t output_buffer_size;
+
+  /* A copy of connection socet. Should be used for statistics only.
+   * We don't call send/recv on it. */
+  int sock;
 };
 
 size_t uw_headers_max = SIZE_MAX;
@@ -559,6 +564,8 @@
   ctx->output_buffer = malloc(1);
   ctx->output_buffer_size = 1;
 
+  ctx->sock = -1;
+
   return ctx;
 }
 
@@ -625,6 +632,35 @@
   free(ctx->output_buffer);
 
   free(ctx);
+}   
+
+void *uw_malloc(uw_context ctx, size_t len);
+
+void uw_setSocket(struct uw_context *ctx, int sock)
+{
+  ctx->sock = sock;
+}
+
+uw_Basis_string uw_Basis_clientIP(struct uw_context *ctx)
+{
+  int ret;
+  socklen_t sz;
+  struct sockaddr_in  peer_addr;
+  sz = sizeof(struct sockaddr_in);
+  memset(&peer_addr, 0, sz);
+  ret = getpeername(ctx->sock, (struct sockaddr*)&peer_addr, &sz);
+  if(ret!=0)
+    uw_error(ctx, FATAL, "Unable to obtain peer name");
+
+  uint32_t client_addr = ntohl(peer_addr.sin_addr.s_addr);
+  char* str = (char*)uw_malloc(ctx, 50);
+  snprintf(str, 50, "%d.%d.%d.%d",
+    (int)((client_addr&0xFF000000)>>24),
+    (int)((client_addr&0xFF0000)>>16),
+    (int)((client_addr&0xFF00)>>8),
+    (int)(client_addr&0xFF));
+
+  return str;
 }
 
 void uw_reset_keep_error_message(uw_context ctx) {
@@ -1059,8 +1095,6 @@
   return 0;
 }
 
-void *uw_malloc(uw_context ctx, size_t len);
-
 uw_Basis_file uw_get_file_input(uw_context ctx, int n) {
   if (n < 0)
     uw_error(ctx, FATAL, "Negative file input index %d", n);
_______________________________________________
Ur mailing list
[email protected]
http://www.impredicative.com/cgi-bin/mailman/listinfo/ur

Reply via email to