I want to write a Java program in which, if I give destination IP address, I will get the information on next hub to reach that IP.
So how can I achieve this?
Thanks
Bapi
Java program??? You have tracert...
Besides, you should explain what exactly you mean by "hub".
I think you mean next hop instead of "hub".
For the host, the next host can be checked from the host's route table, with traceroute it can be implemented as following stackoverflow thread:
How can I determine the IP of my router/gateway in Java?
The most common method for topology detection in IP networks is to send packets (e.g. ICPM ECHO REQUEST) to destination adresses with very small TTL values. Usually the last valid node you reach with a given DLL will then respond with an ICMP error message, telling you that the packet was lost due to its end of life, thereby revealing the IP address of said device.
This question's answer suggests that the Java Socket API can only set the TTL on multicast sockets. To work around this, you could try to work with raw IP sockets, using a third party library like RockSaw, since there is no raw IP support in the JDK (the bug on SUN's tracker from 2002(!) requesting it was close "Won't fix").
Related
For a small java game I made, I would like to be able to play it with two computers in the same (home) network. I think I will use RMI and am now trying with computer on ipaddress 192.168.2.3.
I know I can search for a registry on this ipaddress on my other computer at 192.168.2.6, but I would like to show a list of all ipaddresses in the network my computer is connected with. Prefferably only if they actually host a game.
Now I tried some questions here on stackoverflow:
How to enumerate IP addresses of all enabled NIC cards from Java?
How can I find all locally bound IP addresses in Java?
How to get a list of IP connected in same network (subnet) using Java
Why does InetAddress.isReachable return false, when I can ping the IP address?
,but I don't think I need all my computer network interfaces and InetAddress.isReachable() always seem to result to false (even though I can ping via cmd and I have firewall turned off) and calling a commandline
"ping -n 1 192.168.2.i" for all i, where 0<=i<=255,
always exits normal, so it results always in true.
What is the best way to get a list of ipaddresses of computers in the same network as the computer the JVM runs on?
With the linked answers, you should be able to filter the available interfaces down to a few possible options (i.e. interfaces that are up, no loopback, have an IPv4 address, etc.).
To discover game hosts, you can do something like the following.
Let the game hosts listen for UDP broadcasts on a specific port.
Let the clients send out a UDP broadcast to the broadcast address of each of the remaining interfaces from above. The broadcast address can be determined by getBroadcast() in class InterfaceAddress.
The host replies, to let the client know it is waiting. When using UDP, the hosts IP is in the received DatagramPacket. When using TCP, the hosts IP can be determined from the Socket.
Then the client can use the address of the host to establish a direct connection and/or set up RMI.
Edit: I found this blog post, which includes code that does more or less what I described.
I want to search the local network for special servers to communicate with. To achieve this I'm running a for-loop pinging all IP-Addresses in the range I give the function.
E.g. findServers("192.168.0.x", 101, 255) pings all addresses between 192.168.0.101 and 192.168.0.255 and, if they are reachable, tries to connect to a specific port to find out whether a server is running.
But I don't want to enter the range manually. Is there any way to get the IP-range in which the DHCP-server assigns addresses to the machines in the network?
The only way to obtain this would be to ask the administrator of the DHCP server. One remote possibility is if the DHCP server exposed an SNMP server as well and that SNMP server provided the information. However, no sysadmin worth his/her salt would expose that information, so it's unlikely.
In short, the answer is about 99.9% likely to be "you can't".
You can look at the subnet for your machine and try to connect to the port in question. This saves you needing to know the specific address range the DHCP will give out.
You don't need to determine if the host is reachable because you would still have to attempt to connect to the port. If you connect to 255 addresses using a thread pool it will take a few seconds.
Instead of using TCP you could use UDP. UDP can send a broad cast to a whole subnet or multi-cast across networks with a single packet. This would allow you to send a request to any number of machines to find out if they have a service available.
There is no such protocol that propagates the available ip address range, provided by the DHCP server.
I know its old but maybe someone Google it. You can get it with PowerShell:
$dhcpserver = "10.17.5.1"
$ScopeList = Get-DhcpServerv4Scope -ComputerName $dhcpserver | Where-Object {$_.name -like "*toip*"}
ForEach($Scope in $ScopeList){
$voip += Get-DhcpServerv4Lease -ComputerName $dhcpserver -ScopeId $Scope.ScopeId
}
This will only work if the scopes for Voips have a pattern like "toip".
I am working on a project for androids/computers have p2p talking, and we are experimenting with hole-punching in order to get through the wifi's firewall. However the wifi needs to UDP packet needs to look like it is coming from the same machine that the initial Packet was sent to. Using netcat (and choosing which port to send from) from the same machine we can send information back into the computer. However if we are coming from a different device we need to appear as though we have the same IP address, and port. I was wondering if you can easily specify the Sending IP and Port of the packet? I was thinking of using DatagramPacket, but didn't find any methods that would work.
You can't do that from within Java. You can do it with Java plus one of the several Jpcaps in existence, but be aware it's a JNI library with all the risks that entails.
This might be one of those "huh, why?" questions, but I figured it would be worth the try.
How would one, from a server-side application, use the clients IP address as the applications IP address to another website? The basic idea is that any work the server side application does, is seen as the client itself doing the work, and not the servers static IP.
I am not sure if changing HTTP headers would work, but I could be wrong. Is there any documentation out there on this?
Thanks,
Kyle
Utterly, utterly impossible. You won't even be able to open a TCP connection because the other website's server will try to handshake with the client, and fail.
An IP address isn't just any old ID, it's the actually address that servers will send any response to. Spoofing it basically only makes sense if you can fit your request into a single IP packet (which rules out TCP and thus HTTP) and are not interested in the response. Even then it can fail because your ISP's routers may have anti-spoofing rules that drop packets with "outside" IP addresses originating from "inside" networks.
Why on earth would a legitimate application want to spoof its IP address?
Changing HTTP headers might cut it, but most likely it won't. Depends on how naive the other server is.
It sounds like you're trying to do something the wrong way, can you give a bit more information as to what exactly the use-case is?
If there's no processing to be done in between, you can do port forwarding on your server's IP firewall, so the client connects to your server but ends up talking to the other server.
If there's more involvement of your server, then the correct thing to do would be to pass the client's IP to the other server as part of the URL (if it's a web app) or elsewhere in the data (if not) so the receiving server can know and correctly log the process without any need for fakery. Of course this would also call for a change in the other app.
Again assuming we're talking about HTTP, another idea that came to my mind would be to redirect your client to the other server. As long as all necessary data is in the URI, you could advise the client's browser to connect to the other server with a URI of your own creation that could carry whatever extra value your server's processing adds to the request.
Decades ago, the designer of internet asked, "how can we prevent Kyle Rozendo from doing such a devious thing?"
If the client is cooperating, you can install some software on client machine, and do the work from there. For example, a signed java applet on your page. [kidding]If the client is not cooperating, install some trojan virus[/kidding]
i'm developing a java application using the jstun library (hxxp://jstun.javawi.de/), and i need to compare my public ip with the one chosen by the kernel (wildcard address - hxxp://java.sun.com/j2se/1.5.0/docs/api/java/net/DatagramSocket.html#DatagramSocket() ) when i create a udp socket.
what i don't understand is, if my local ip on my natted network is in the form of 192.168.1.x, why do i get an ip such as 10.x.x.x ?
is there a particular reason why the two are totally unrelated?
if i wasn't behind a nat, would the kernel bind the socket to my public address?
thanks a lot!
asymmetric
For a sending socket the source IP would be determined by the host routing table according to the destination IP; for receiving socket the destination IP is what you get. The "public" IP your peers see is the source address on the packet from you, which is re-written by NAT to be whatever internal addresses are translated to.
i failed to mention a rather important thing: i'm developing for android, and running my code on the android emulator, which explains the 10.x.x.x address thing..
i should dig deeper into that.. thanks for the help!
for those that are interested: hxxp://developer.android.com/guide/developing/tools/emulator.html#networkaddresses