0

I have this function to get the HostAddress from my request (HttpServletRequest) on Java. But using Jetty 7.x and my IP is ipV6 I have always this error with iPv6 address.

My function:

xxxx.getIP(request, false);

public static String getIP(HttpServletRequest request, boolean proxy) {
    String ip = "";
    log.debug("X-getHeaderNames ["+ request.getHeaderNames()+"]");
    if (proxy) {
        ip = XFordwardedInetAddressUtil.getAddressFromRequest(request);
    } else {
        String _ip = request.getRemoteAddr();
        ip = InetAddresses.forString(_ip).getHostAddress();
    }
    return ip;
}

The error:

DEBUG: org.encuestame.core.exception.EnMeMappingExceptionResolver - Resolving exception from handler [org.encuestame.mvc.controller.TweetPollController@4fc23996]: java.lang.IllegalArgumentException: '0:0:0:0:0:0:0:1%0' is not an IP string literal. java.lang.IllegalArgumentException: '0:0:0:0:0:0:0:1%0' is not an IP string literal. at org.encuestame.utils.net.InetAddresses.forString(InetAddresses.java:59) at org.encuestame.core.util.EnMeUtils.getIP(EnMeUtils.java:210) at org.encuestame.mvc.controller.AbstractBaseOperations.getIpClient(AbstractBaseOperations.java:262) at org.encuestame.mvc.controller.TweetPollController.detailTweetPollController(TweetPollController.java:332) at org.encuestame.mvc.controller.TweetPollController$$FastClassByCGLIB$$6990b004.invoke() at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191) at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedIntercepto

I know the iPv6 localhost format should be '0:0:0:0:0:0:0:1' but my request always return this string '0:0:0:0:0:0:0:1%0'

Anyone can help me?

Juan Picado
  • 1,823
  • 18
  • 33

2 Answers2

3

The problem is that the class you're using (org.encuestame.utils.net.InetAddresses) clearly doesn't support IPv6. Try using the java InetAddress class that Joachim mentioned in his answer.

Augusto
  • 28,839
  • 5
  • 58
  • 88
2

When you're using a link local address, the % should be included in the address.

This is due to the fact that the computer needs to know which interface/zone the request came from to be able to reply out the correct interface.

If you're using correctly configured, Internet routable IPv6 addresses, the zone index will not be a part of the address.

In this case, I can't see a way to solve your problem for localhost/link local testing except to filter out anything after the % sign, or use another class that works with link local addresses to parse the address.

EDIT: Here's another - similar - question I didn't see earlier.

Community
  • 1
  • 1
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • ::1 (which is the same as 0:0:0:0:0:0:0:1) is the localhost address. It is not a link-local address (which are from fe80::/10) – Sander Steffann Oct 06 '12 at 13:39
  • @SanderSteffann You're right of course, trying to locate a reference, but a zone index is optional/allowed even on localhost addresses. Tomcat7 always seems to add it. – Joachim Isaksson Oct 06 '12 at 13:54