Java TCP Get Domain Name? - java

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.

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.

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.");
}

Related

Get ip from udp url/address in tracker

In Java, or more generally, if I have a tracker announce url like: udp://tracker.coppersurfer.tk:6969, how can I acquire the ip address to add to the constructor of a DataGramPacket that requires an InetAddress?
Or am I missing something and this information is available somewhere else in a .torrent file?
In the BitTorrent specification I can only find how it is done with http addresses but all torrents I find use udp for their trackers.
It's close to impossible to run a popular public http tracker today. as they take too much resources to run. When a tracker gets to popular, it has to use only UDP.
BitTorrent UDP trackers are specified in BEP15 - UDP Tracker Protocol.
To get the IP-address for the URL, use DNS.
Answer from Get IP address with URL string? (Java) by Victor Stafusa:
Try this:
InetAddress address = InetAddress.getByName(new URL(urlString).getHost());
To get the raw IP:
String ip = address.getHostAddress();

Why do we have to use InetSocketAddress to enter our port number for using ServerSocketChannel

why do we have to create an object of InetSocketAddress?
but for ServerSocket we just use int to enter port number
Example:
try(
ServerSocketChannel listener = ServerSocketChannel.open();
ServerSocket serverSocket = listener.socket()
){
serverSocket.bind(new InetSocketAddress(2266));
//we can't use serverSocket.bind(2266);
}catch (IOException e){
e.printStackTrace();
}
Your question is somewhat unclear.
ServerSocket#bind() establishes the local end of the connection. If your system has more than one network adapter and/or more than one IP address, AND you wanted to connect the socket using a specific local adapter or IP address, then you would provide a complete InetSocketAddress(host,port) with both host and port, where the host part was one of your local IP addresses.
In the default case where you have only one IP address, or have more than one but don't care which one is used as the source, you can omit the host and just specify the port, as in your example.
The API is defined this way. A TCP connection is symmetrical, and is defined by its two endpoints. An endpoint is a pair (host,port). This applies to BOTH ends of the connection. Thus the bind() call takes an InetSocketAddress parameter. For convenience, InetSocketAddress will assume the default host that specifies "use any available interface" if you provide only a port.
The API designers could have added a bind(int port) method to build the InetSocketAddress(port) behind the scenes, but clearly didn't feel it was necessary.
InetSocketAddress creates a socket address where the IP address is the wildcard address and the port number a specified value
wildcard is special IP address which can be used for binding. If you don't want to listen "everything" but maybe spesific IPs, say, 5.5.5.5 for example, you will be binded to that IP address but not others.
But without the IP part to it means any IP will be listened so pretty much no difference for this particular usage.

Exception in thread "main" java.rmi.ConnectException: Connection refused to host: 192.168.1.55; nested exception is:

i run this code in my system and i don't weather to write localaddress(localhost) or remote addres(IP address)...
ref= (RMISIntf)Naming.lookup("rmi://"+"192.168.0.15"+"/RMIServer");
and also i try this code also till i get no response
ref= (RMISIntf)Naming.lookup("rmi://"+"localhost"+":"+8033+"/RMIServer");
//MyRemoteObject ro = (MyRemoteObject) Naming.lookup("//" + "localhost" + ":" + port + "/" + name);
//ref=Naming.rebind("rmi://localhost:8080/RmiServer",RMISIntf);
It is good to test first with the localhost as it is easy and does not require to set rules as is with the remote connection i.e. the external address.
The Java RMI faq says,
The appropriate workaround is to set the system property
java.rmi.server.hostname when starting the server. The value of the
property should be the externally reachable hostname (or IP address)
of the server -- whatever works when specified as the host-part in
Naming.lookup is good enough.
Also just have a look at the properties for remote method invocation here.
You should be able to make it work using any of the two, but keep in mind that for it to work with the external address, you may need to create some rules on your firewall and forwarding on your modem.

Java Socket on Different Machine Does Not Work

I've tried many examples on web and one of them is this:
http://zerioh.tripod.com/ressources/sockets.html
All of the server-client socket examples work fine when they are tested with 127.0.0.1
BUT it never ever EVAR works on two different computers with actual raw real IP address ("could not connect to host" on telnet and "connection timed out" when tested on java client - the server program just waits for connection)
Note:
Firewall is turned off for sure
IP address from ipconfig didn't work
IP address from myipaddress.com (which is totally different for no reason than that from ipconfig) didn't work
What is it that I'm missing?
If I can only figure this out...
Try binding on 0.0.0.0. This tells your socket to accept connections on every IP your local can accept upon.
Based on the comment where the the following snippet of code is mentioned:
requestSocket = new Socket("10.0.0.5", 2004); // ip from ipconfig
it would be better to use the hostname instead of the IP address in the constructor, as the two-parameter Socket constructor with a String argument expects the hostname as the String, and not an IP address. A lookup of the IP address is then performed on the provided hostname.
If you need to pass in an IP address, use the two-parameter constructor that accepts the InetAddress as an argument. You can then provide a raw IP address to the InetAddress.getByAddress method, as shown in the following snippet:
InetAddress addr = InetAddress.getByAddress(new byte[]{10,0,0,5});
You'll need to be careful when specifying arguments via the byte array, as bytes are signed in Java (-127 through +128), and numbers beyond this range (but valid octets of IP addresses) may have to be specified using Integer.byteValue.
Finally, it should be noted that it is important to specify the IP address of the remote machine, as visible to the client. The IP address listed at myipaddress.com may be the address of a proxy, as that is the public IP of your entire network as visible to the host server at myipaddress.com. Therefore, you ought to be specify the IP address of the remote machine that is visible to your machine and not myipaddress.com.

How to fetch all DNS entries from JAVA application?

As of now, I'm using the below code to get DNS name of the given IPAddress. Instead of fetching it for each IPAddress in the network, I want to fetch all the DNS entries (IPAddress - HostName mapping) from the DNS Server in one go. Is it possible? If so, how to do it?
InetAddress addr = InetAddress.getByName(address);
dnsname = addr.getCanonicalHostName().trim();
From a public DNS server, there is no way to pull out all the data it holds. Enumerating all the IP addresses one by one is the only solution.
If you have a special relationship with the DNS server (for instance, it is managed by your employer), you may request from the DNS administrator a right to transfer the whole zone (the DNS request known as AXFR). They may authorize your IP address or gives you a TSIG key to authentify yourself.
Then, you will have to find a way to do a zone transfer (possibly with TSIG authentication) in Java. Using these keywords, I find some code and documentation. Use a code search engine like Google Code Search or Krugle to find examples of use.
[DNS experts will probably scream "Use zone walking on NSEC" but most DNS zones are not signed with NSEC.]

Categories

Resources