I have a UDP server running on my private network.I have a device which is beyond NAT which sends UDP packets to my server through public IP. I am able to receive the UDP packets, parse them and I can view the pay load, src and dest port. Now I need to send an ACK message to that device so I have captured the ip and port numbers and sending a UDP packet to it. But I could not able to view any UDP packets in that device.
My question is as my Device is beyond NAT the IP address and port number I am getting is NAT's or the actual device's ?
How to send the UDP packet to that device through NAT ?
My question is as my Device is beyond NAT the IP address and port number I am getting is NAT's or the actual device's ?
The IP and Port you identify from the packet are those which the NAT created a mapping for the client. In simple words, it is the IP and Port of the NAT to which the device is connected.
How to send the UDP packet to that device through NAT ?
You need to reply on the IP and port detected by the server from the packet it received from client.
More clearly you need to implement UDP Holepunching.
I guess you should first understand the networking issues regarding connection to devices working under a NAT. Various RFCs are available for NAT Traversal and UDP Punching.
Here are some sources:
http://en.wikipedia.org/wiki/Network_address_translation
http://en.wikipedia.org/wiki/UDP_hole_punching
https://www.rfc-editor.org/rfc/rfc4787
https://www.rfc-editor.org/rfc/rfc5128
Related
I am very new to network programming, so this might be a no brainer. What I was wondering is, I know TCP requires the client to know the IP of the server. But since UDP is connectionless server, is it still required? I mean can I make my server broadcast it's IP address on a specific port ( not necessary in the same LAN , also over internet) and make the clients listen to that port for any incoming requests and find get the IP of the server once a request is received ?
You can try to send packages on specific port over the LAN. But for the Internet no.
Look into using ARP/RARP if that gives you what you need. Coming to your question
Mind you both TCP and UDP require IP addresses binding the ip address to a port is called a socket and there can be TCP and UDP with the same port no.
You can do this according to what you said but then the server needs to know the client addresses
Create a connection from server to client
Send IP address of server to client
Client send data using just received Server IP
to work around this you can use the broadcast address of the network and have the clients listen to it, just check what your broadcast address is.
What you are trying to do is similar to a DHCP server.
I want to know if there is a way to broadcast data through udp sockets for clients behind a NAT. I found many examples where the server is sending data to a multicast address and clients listen on a fixed port. But if there is a NAT between them, the packages need to be sent to different ports, depending on how the NAT has created the port translations right ? So what is the solution here ?
I want to
Recieve UDP message on device when it is connected through wi-fi
Know how to send udp message on device when it is connected to wi-fi
router as the sender on diffrent network
What i have tried
http://code.google.com/p/boxeeremote/wiki/AndroidUDP
What is working
UDP messeges are being recieved on device when it is connected
through 2g/3g data service
UDP messeges are being recieved when sender and reciever are both in
same network ( behind wi-fi router )
Any help/point in right direction is appreciated.
When the device is behind a wi-fi router, it has a private address so it isn't reachable from outside the private network, at least no without some extra work.
You need to do Hole Punching, is what applications like Skype do to receive UDP packets. Applications usually use a STUN server in order to achieve this.
The easiest alternative would be to have a server with a public address and make all the devices connect to the server with tcp/ip protocol. If you can't afford that you need to do Hole Punching.
I want Peer(s) to be able to send DatagramPackets over a WIFI LAN for the sole purpose of discovering other Peers. So, I have each Peer made up of a Client(Sending packets) and a Server (listening for packets).
I have only been able to get this to work by using IPConfig on each machine to get their respective IPv4 Address and then using that as a paramter for the creation of the DatagramPacket:-
InetAddress server = InetAddress.getByName("THE IPv4 ADDRESS OF THE PEER'S SERVER
RUNNING ON THE OTHER MACHINE");
DatagramPacket sendPacket = new DatagramPacket(outBuffer, outBuffer.length,
server, serverPort);
Am I using broadcast (for peer discovery) in the right way here because it seems pretty rubbish to me if I am? I was under the assumption I could have a client on machineA fire off a packet into the LAN abyss and the Server running on machineB would be listening to the entire LAN and detect the packet. At the moment all I'm doing is manually using IPConfig on the machine running the server and typing that address in the DatagramPacket on the machine running the client, which just seems pointless.
I can use multicast just fine but I think broadcast is more what I need for peer discovery.
Help appreciated.
That's not really broadcast, since you're giving the server's address, rather than the broadcast address (255.255.255.255, or whatever your local network broadcast address is (like 192.168.1.255, if your network segment is 192.168.1.0/24).
You also may consider that you could have a firewall blocking incoming UDP packets on your server port. You should look at the documentation for DatagramSocket/DatagramPacket to see if there's a magic incantation for binding the server socket properly, or sending the packet from the client properly.
[Edit: How to find the network broadcast]
The network broadcast address can be found once you know the network address, both of which you can tell from your IP and subnet mask. The subnet mask basically shows how much of the address is the network, and how much is the client. (subnet mask is often written as "netmask") An example:
IP: 192.168.1.101
Netmask: 255.255.255.0
Network Address (CIDR): 192.168.1.0/24
Broadcast Address: 192.168.1.255
or:
IP: 10.13.28.55
Netmask: 255.255.0.0
Network: 10.13.0.0/16
Broadcast Address: 10.13.255.255
So essentially, the network address has a bunch of zeroes at the end, and the broadcast address replaces them with ones.
IPv4 Addresses are 32-bit numbers (easiest to think of them as unsigned. If you do the bitwise-AND of your address and the netmask, you get the network address. If you bitwise-OR that with the bitwise-NOT of the netmask, you get the broadcast address (so (addr & netmask) | ~netmask) would get you the local broadcast).
255.255.255.255 (or 0xFFFFFFFF) is the global broadcast address. IP broadcasts don't cross network segments, so it's safe to send to it, though.
Use the broadcast address 255.255.255.255 for both server and client.
I'm trying to create a DHCP Client using Java.
The client will send Discover, receive a Offer, send a Request, and receive an Ack.
However, when sending the discver and the request UDP DatagramPackets, I need to send them from the IP 0.0.0.0 port 68 because, the point of DHCP is is to receive a usable IP address from the DHCP Server.
However, If I bind my socket to a 0.0.0.0, according to the http://download.oracle.com/javase/6/docs/api/java/net/DatagramSocket.html Java Reference, the kernel will assign a IP address to the socket.
If I force set the IP address of my ethernet card to 0.0.0.0 via the
$ sudo ifconfig en0 0.0.0.0
and send my Packet; I receive a
java.io.IOException: Can't assign requested address
Exception.
Is there a way to send UDP packets from a 0.0.0.0 and set through System.exec() (if required) the IP address received from the DHCP server using Java?
It may be that you won't be able to use this library to do DHCP. The library assumes that you won't want to use 0.0.0.0 as the sender address, even though DHCP specifies that you shouldn't put an ip address there. Just scanning the RFC, it seems to indicate that you "shouldn't fill out the sender field." Is it possible that it'll just be ignored? It may be that since DHCP is all broadcast, it won't matter and you can just fill out anything. Have you tried listening to DHCP requests on Wireshark or another packet sniffer to see if its really 0.0.0.0?
You may have to build your datagrams at a lower level.
I don't think it will matter how your own IP address is set if you're using DatagramSocket, because the library is going to change it if you try and assign 0.0.0.0.
Edit:
I scanned through parts of the dhclient source, and its huge and I don't understand it, but it looks like they are using shell scripts to configure the interfaces using ifconfig. So that may answer that part of the question. So you can just use your ifconfig to change your IP address as you are now.
0.0.0.0 is the ip that represents the whole local network. You can't bind your network card to it, you can only send packets to it's broadcast ip 255.255.255.255