Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the IP format returned by ServletRequest.getRemoteAddr()

The Javadoc of ServletRequest.getRemoteAddr() does not mention anything about the format of returned IP addresses. This is annoying when implementing filters. What can we count on? Is there any official specification one can rely on? Thanks.

like image 780
Jérôme Verstrynge Avatar asked Dec 12 '25 16:12

Jérôme Verstrynge


2 Answers

Let's start at ServletRequest#getRemoteAddr() javadoc:

getRemoteAddr

    java.lang.String getRemoteAddr()

Returns the Internet Protocol (IP) address of the client or last proxy that sent the request. For HTTP servlets, same as the value of the CGI variable REMOTE_ADDR.

(emphasis mine)

Okay, let's read CGI REMOTE_ADDR spec:

4.1.8. REMOTE_ADDR

The REMOTE_ADDR variable MUST be set to the network address of the client sending the request to the server.

   REMOTE_ADDR  = hostnumber
   hostnumber   = ipv4-address | ipv6-address
   ipv4-address = 1*3digit "." 1*3digit "." 1*3digit "." 1*3digit
   ipv6-address = hexpart [ ":" ipv4-address ]
   hexpart      = hexseq | ( [ hexseq ] "::" [ hexseq ] )
   hexseq       = 1*4hex *( ":" 1*4hex )

The format of an IPv6 address is described in RFC 3513 [15].

There, you've all possible formats.

like image 96
BalusC Avatar answered Dec 14 '25 06:12

BalusC


It's an IPv4 or IPv6 address in standard textual notation.

  • For IPv4, it's the familiar "dot-decimal notation", e.g. 192.168.254.1. I couldn't find an RFC that defines it. Wikipedia mentions that it's acceptable to use octal, binary or hexadecimal notation for any of the quads (and even to mix-and-match), but I've never seen anything but decimal.
  • For IPv6, it's specified in RFC-2373.
like image 42
Barend Avatar answered Dec 14 '25 06:12

Barend