Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using netcat to send a UDP packet without binding

Tags:

udp

netcat

I am trying to use netcat to simulate a NAT traversal protocol.

I have one instance that is listening for UDP packets on port 6666, as so:

nc -ul 6666

In another terminal window, I am trying to periodically send a UDP packet from port 6666 (to open the return path on my router. this would be in a script that repeats every 20 seconds to re-open the port)

nc -u -p6666 mypinghost.com 4444

The problem is netcat fails on this ping call with the message:

nc: bind failed: Address already in use

Which implies that the listener having bound to port 6666 is blocking another process from sending from that port, or possibly that netcat is trying to bind to 6666 to listen.

Is this just how netcat is written, or can I tickle it some way to let me send a packet without binding to the port to listen?

like image 989
Jim Baldwin Avatar asked Jul 08 '12 22:07

Jim Baldwin


People also ask

Does netcat work with UDP?

By default Netcat uses the TCP protocol for its communications, but it can also UDP using the -u option.

How do you connect to UDP ports netcat?

On one machine, you can tell netcat to listen to a specific port for connections. We can do this by providing the -l parameter and choosing a port: netcat -l 4444.

How do I send a UDP request?

Use nc command for sending and receiving UDP packets through network. Send some human readable sentences through nc command. Capture the UDP packet sent by nc command. Check network packet in Wireshark.

How do I get my UDP port to listen?

Listen for UDP on specific host and port using netcat nc is the command alias for netcat. u Use UDP. That is, listen for UDP on give port, by default it listens for TCP unless we give this option. Port You must specify the port on which nc should listen on.


2 Answers

nc -ul 6666

Listen at UDP port 6666.

nc -u -p6666 mypinghost.com 4444

Using UDP port 6666 as the source port, send to mypinghost:4444.

nc: bind failed: Address already in use

That would be on the second netcat invocation, where 6666 is already in use by the first one.

Which implies that the listener having bound to port 6666 is blocking another process from sending from that port

Correct.

or possibly that netcat is trying to bind to 6666 to listen.

And definitely that. You told it to do that, so it did it.

What you are trying to do is impossible between two processes in the same host. Only one process can use a specific local UDP port at a time, unless you use SO_REUSEADDRESS, which netcat doesn't appear to implement.

As the other poster has suggested, the solution lies in using a single process.

like image 72
user207421 Avatar answered Oct 25 '22 23:10

user207421


I do not believe you can use netcat in that way. I would recommend writing a simple Python script that does both the sending and receiving tasks in one process. That way you can hold that port exclusively and still accomplish both tasks.

like image 42
mhall Avatar answered Oct 26 '22 00:10

mhall