0

I have a Linux program that sends & receives UDP packets (it is interacting with various remote machines according to a protocol). I would like to "inject" additional UDP packets from a second program so that the return address is the IP/port used by the main program.

For example, imagine that the main program is sending "ping" style UDP packets to various remote hosts, and they send back "pong" packets. I would like the injector program to send a ping somewhere, and have the main program receive the pong. (The main program wouldn't be expecting that pong, because it didn't send out a ping, but that's OK.)

A complex way to do this would be to have the main program accept injected packets from a local pipe or something and then send them out. But it would be easier if the injector could just set the "from" address on a sendto() packet. (I also don't want the injector to steal packets meant for the main program. The injector would probably be short-lived, but if it bound the main program's port, then theoretically the system could deliver pongs to it instead of the main program?)

Dave M.
  • 1,496
  • 1
  • 12
  • 30
  • Maybe this might help? Once the socket is bound, you should be able to send. It's been a long time since I've done socket-level work in Linux, but I suspect you can simply not bother trying to receive from it? http://stackoverflow.com/questions/4364434/let-two-udp-servers-listen-on-the-same-port – Scott Dudley Dec 06 '14 at 00:25
  • Thanks, that reference helped! It looks like either SO_REUSEADDR or SO_REUSEPORT (added to Linux in Jan 2013) would let me bind the port in both processes, but the intended use case is for load sharing among processes -- my injector would end up stealing some of the packets meant for the main process. Contrary to some answers / comments, these options don't seem to copy all packets to all listeners (the canonical case of this is multicast, where you want all listeners to receive all packets. But that's done with a special address rather than just a port on a regular address.) – Dave M. Dec 06 '14 at 14:32
  • 1
    Are you sure that it would steal packets even if you didn't try to receive on the socket? Anyway, in the worst case, you can always send using the raw IP protocol and build the datagrams yourself. eg. See the sources for: http://nemesis.sourceforge.net – Scott Dudley Dec 06 '14 at 15:01
  • Yeah, I wrote a simple UDP listener that printed out what it got. Run two copies on one port, and then send packets from another port/machine. Only one of the listeners gets each packet. When the sender runs on the same machine & port, sometimes the listener doesn't get anything. I think it's because the packet gets queued back to the sender's socket (and then gets discarded because the sender exits without recv'ing). Anyway, this stuff seems to be murky enough (differences between systems, kernel versions, etc.) that it's not worth trying to do it this way -- I'll work something else out – Dave M. Dec 06 '14 at 16:37
  • The described experiment does not represent what you asked for in your question - having two *UDP listeners* ist different from having one UDP sender/receiver and one *injector*, so why do you draw conclusions from one case for the other? Another question: Does it have to work on a machine with multiple IP addresses? – Armali Jul 14 '15 at 09:31

0 Answers0