apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeff Trawick <traw...@attglobal.net>
Subject [PATCH] Re: How to get remote (client) address,...
Date Fri, 25 Oct 2002 13:12:33 GMT
Damir Dezeljin <programing@nib.si> writes:

> Hi.
> 
> I writen an daemon which is executed in inetd (part of the code is below).
> I have problems getting remote IP address and remote port. I use:
>   apr_socket_addr_get(&addr_local, APR_LOCAL, apr_sock);
> to get local address and port, and
>   apr_socket_addr_get(&addr_local, APR_LOCAL, apr_sock);
> to get the remote one, but this doesn't work (I get 0.0.0.0 for the IP and
> port 0).

Please try this patch.  I'm 99% sure it is golden, but I can't commit
until I have Win32 and OS/2 paths fixed up and if you're awake in the
meantime you might as well test it too :)

Index: include/arch/unix/networkio.h
===================================================================
RCS file: /home/cvs/apr/include/arch/unix/networkio.h,v
retrieving revision 1.56
diff -u -r1.56 networkio.h
--- include/arch/unix/networkio.h	22 Oct 2002 20:05:35 -0000	1.56
+++ include/arch/unix/networkio.h	25 Oct 2002 13:04:04 -0000
@@ -135,6 +135,7 @@
 #endif
     int local_port_unknown;
     int local_interface_unknown;
+    int remote_addr_unknown;
     apr_int32_t netmask;
     apr_int32_t inherit;
 };
Index: network_io/unix/sa_common.c
===================================================================
RCS file: /home/cvs/apr/network_io/unix/sa_common.c,v
retrieving revision 1.68
diff -u -r1.68 sa_common.c
--- network_io/unix/sa_common.c	15 Oct 2002 12:27:38 -0000	1.68
+++ network_io/unix/sa_common.c	25 Oct 2002 13:04:05 -0000
@@ -209,6 +209,13 @@
         *sa = sock->local_addr;
     }
     else if (which == APR_REMOTE) {
+        if (sock->remote_addr_unknown) {
+            apr_status_t rv = get_remote_addr(sock);
+
+            if (rv != APR_SUCCESS) {
+                return rv;
+            }
+        }
         *sa = sock->remote_addr;
     }
     else {
Index: network_io/unix/sockaddr.c
===================================================================
RCS file: /home/cvs/apr/network_io/unix/sockaddr.c,v
retrieving revision 1.29
diff -u -r1.29 sockaddr.c
--- network_io/unix/sockaddr.c	5 Apr 2002 22:15:04 -0000	1.29
+++ network_io/unix/sockaddr.c	25 Oct 2002 13:04:05 -0000
@@ -70,6 +70,21 @@
     }
 }
 
+static apr_status_t get_remote_addr(apr_socket_t *sock)
+{
+    sock->remote_addr->salen = sizeof(sock->remote_addr->sa);
+    if (getpeername(sock->socketdes, (struct sockaddr *)&sock->remote_addr->sa,
+                    &sock->remote_addr->salen) < 0) {
+        return errno;
+    }
+    else {
+        sock->remote_addr_unknown = 0;
+        /* XXX assumes sin_port and sin6_port at same offset */
+        sock->remote_addr->port = ntohs(sock->remote_addr->sa.sin.sin_port);
+        return APR_SUCCESS;
+    }
+}
+
 /* included here to allow us to use get_local_addr().
 
    NOTE: this file (sockaddr.c) can be included from other directories. If
Index: network_io/unix/sockets.c
===================================================================
RCS file: /home/cvs/apr/network_io/unix/sockets.c,v
retrieving revision 1.103
diff -u -r1.103 sockets.c
--- network_io/unix/sockets.c	18 Oct 2002 14:13:38 -0000	1.103
+++ network_io/unix/sockets.c	25 Oct 2002 13:04:05 -0000
@@ -362,6 +362,9 @@
         /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */
         (*apr_sock)->remote_addr->port = ntohs((*apr_sock)->remote_addr->sa.sin.sin_port);
     }
+    else {
+        (*apr_sock)->remote_addr_unknown = 1;
+    }
         
     (*apr_sock)->inherit = 0;
     apr_pool_cleanup_register((*apr_sock)->cntxt, (void *)(*apr_sock), 
@@ -381,6 +384,7 @@
         (*sock)->timeout = -1;
     }
     (*sock)->local_port_unknown = (*sock)->local_interface_unknown = 1;
+    (*sock)->remote_addr_unknown = 1;
     (*sock)->socketdes = *thesock;
     return APR_SUCCESS;
 }

-- 
Jeff Trawick | trawick@attglobal.net
Born in Roswell... married an alien...

Mime
View raw message