Bug 37673

Summary: Http11AprProcessor.java - getLocalAddr() returns HostName
Product: Tomcat 5 Reporter: Nate Rock <nrock>
Component: Connector:HTTPAssignee: Tomcat Developers Mailing List <dev>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: 5.5.12   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Attachments: JSP example demonstrating the bug

Description Nate Rock 2005-11-29 00:35:49 UTC
I have a jsp that uses the getLocalAddr() method for some ssl certificate 
generation utility and it's currently returning the hostname instaed of the IP.

After much digging through the Tomcat source I found the snippet below:

org\apache\coyote\http11\Http11AprProcessor.java

        } else if (actionCode == ActionCode.ACTION_REQ_LOCAL_ADDR_ATTRIBUTE) {

            // Get local host address
            if (localAddr == null) {
                try {
                    long sa = Address.get(Socket.APR_LOCAL, socket);
                    remoteAddr = Address.getip(sa);
                    if (Address.fill(addr, sa)) {
                        localAddr = addr.hostname;
                        localPort = addr.port;
                    }
                } catch (Exception e) {
                    log.warn(sm.getString("http11processor.socket.info"), e);
                }
            }

            request.localAddr().setString(localAddr);

        }

Should the code above be changed to the snipit below so that the ip address is 
coming back instead of the hostname as expected?


        } else if (actionCode == ActionCode.ACTION_REQ_LOCAL_ADDR_ATTRIBUTE) {

            // Get local host address
            if (localAddr == null) {
                try {
                    long sa = Address.get(Socket.APR_LOCAL, socket);
                    localAddr = Address.getip(sa);
                } catch (Exception e) {
                    log.warn(sm.getString("http11processor.socket.info"), e);
                }
            }

            request.localAddr().setString(localAddr);

        }

If not I would love to know why the hostName/port are being returned instead of 
the IP thanx!
Comment 1 Remy Maucherat 2005-11-29 11:45:13 UTC
First, the current code is:

        } else if (actionCode == ActionCode.ACTION_REQ_LOCAL_ADDR_ATTRIBUTE) {

            // Get local host address
            if (localAddr == null && (socket != 0)) {
                try {
                    long sa = Address.get(Socket.APR_LOCAL, socket);
                    Sockaddr addr = new Sockaddr();
                    if (Address.fill(addr, sa)) {
                        localAddr = addr.hostname;
                        localPort = addr.port;
                    }
                } catch (Exception e) {
                    log.warn(sm.getString("http11processor.socket.info"), e);
                }
            }

            request.localAddr().setString(localAddr);

Then, I don't really care about this issue, so please stop spamming me about it.
Comment 2 Remy Maucherat 2005-11-29 14:31:48 UTC
The issue is now fixed.
Comment 3 Remy Maucherat 2005-11-29 17:01:12 UTC
*** Bug 37693 has been marked as a duplicate of this bug. ***
Comment 4 Nate Rock 2005-11-29 17:06:31 UTC
Remy you do not care if your code is up to servlet 2.4 specification? 

Trust me, I am not doing this to waste your or my time...

I am a professional Java developer using Tomcat in a production evironment. We 
want to use the APR connector, but this bug is forcing me to recommend we NOT 
use it. If you won't fix it I can look into fixing it myself.
Comment 5 Nate Rock 2005-11-29 17:54:24 UTC
Created attachment 17077 [details]
JSP example demonstrating the bug

Simple JSP demonstrating the bug

This JSP demonstrates the bug clearly. It has both request.getLocalName() which

should return the local server's hostname. It also has request.getLocalAddr()
which returns the machines hostname, it should return the local server's IP
address. Open the .jsp in Tomcat 5.5.12 using the APR native connector.
Comment 6 Nate Rock 2005-11-29 18:40:18 UTC
Looks like it's been resolved and fixed! Remy is the MAN!!!! /bow

http://svn.apache.org/viewcvs.cgi/tomcat/connectors/trunk/http11/src/java/org/ap
ache/coyote/http11/Http11AprProcessor.java?rev=349715&view=markup

My bad Remy, didn't see your fixed post above... I was too busy creating the 
duplicate bug 37693... still learning about the whole apache dev process sorry 
for any inconvenience it might have caused you.