0

How would you get the domain name of from a TCP connection?

As I'm trying to make a proxy type software but it must detect what the domain is and then go where it needs to. However I'm unsure how to get the domain name from the client.

user3642449
  • 51
  • 1
  • 1
  • 9
  • https://stackoverflow.com/questions/10554380/converting-an-ip-address-to-host-name is this what you're asking? – Matthew Brzezinski May 25 '15 at 19:10
  • No, so I need to detect like this. If someone connects via the domain google.com they get set to 1.1.1.1 ip address. However if someone connects via the domain bing.com they get set to 3.3.3.3 ip address. I need to grab the domain name from the connection. – user3642449 May 25 '15 at 19:11

2 Answers2

1

There is no general way to get the target domain or host name from of the TCP connection, because a connection is only defined by its target IP address and not the host name and there might be several names for a single target IP address. But while there is no general way to get the target name from all TCP connections it is possible with some protocols on top of HTTP:

  • In case of HTTP you might look at the HTTP Host header which contains the target host name and is set by nearly all HTTP stacks (required with HTTP/1.1).
  • With SSL you might try to extract the host name from the initial ClientHello message in the SSL handshake, in case the client uses SNI (server name indication). All modern browsers use SNI, but older browser like IE8 not and also not older Java, Python, Perl, Ruby ... applications.
Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
0

You may use the following code snippet which will give local domain name -

try {
      InetAddress me = InetAddress.getLocalHost();
      String dottedQuad = me.getHostAddress();
      System.out.println("My address is " + dottedQuad);
 } catch (UnknownHostException e) {
      System.out.println("I'm sorry. I don't know my own address.");
 }
Razib
  • 10,965
  • 11
  • 53
  • 80