0

I would like to send a UDP datagram from a specified port number WITHOUT binding (ex. when the port is already bound to another socket that I have no control over).

While I do appreciate the OS trying to keep everything nice and clean, and I cherish the bind() functionality, the question is as it is.

How to accomplish this with WSASendTo() or WSASendMsg()?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Vega4
  • 969
  • 1
  • 11
  • 25
  • Calling `WSASendTo` on an unbound UDP socket will implicitly bind it. This behavior is not documented for `WSASendMsg`, but it likely behaves the same way. However, you said you have a socket that is *already bound*. In that case, you should be totally fine to call `WSASendTo` or `WSASendMsg` because, in the call, you will specify the destination, which will override whatever that socket has been previously bound to. Did you try it? Can you also provide us with more details about your *specific* situation? Are you trying to use the same socket across multiple processes? (That won't work.) – Cody Gray - on strike Jan 12 '23 at 08:43
  • I just got to know that that WSASendTo implicitly binds the socket the moment you wrote it. Please do notice that I'm after setting the source port of the datagram and that I may not have control over (neither access to) the other socket which is already bound to port which I want to use. – Vega4 Jan 12 '23 at 08:47
  • Why do you care about the socket's *source* port? Isn't all you care about the *destination* port? – Cody Gray - on strike Jan 12 '23 at 08:49
  • There are many benefits associated with knowing from which port UDP packets originate, including improved performance of NAT traversals through more efficient hole punching, or no need for hole-punching at all if source port is A priori known and can be configured on a NAT, anyone? – Vega4 Jan 12 '23 at 15:20
  • I've tried binding datagram port to source port address but then the API seems to ignore the REUSEADDR option... ehhh. windows? – Vega4 Jan 12 '23 at 15:49
  • according to this https://stackoverflow.com/questions/3062205/setting-the-source-ip-for-a-udp-socket windows does not support ipi_spec_dst for pktinfo to be used by sendmsg thus seems like this CANNOT be done, on Microsoft Windows., without binding the socket. – Vega4 Jan 12 '23 at 16:00
  • @Vega4 `ipi_spec_dst` can't set a port number anyway, only an IP address (and as far as that goes, [Microsoft's documentation](https://learn.microsoft.com/en-us/windows/win32/api/ws2ipdef/ns-ws2ipdef-in_pktinfo) suggests the source IP *can* be set for `WSASendMsg()`). – Remy Lebeau Jan 12 '23 at 23:56

1 Answers1

0

WSASendTo() will use whichever source IP/Port the socket is currently bound to. If the socket is not bound, WSASendTo() will perform an implicit binding first.

WSASendMsg() can use the IN_PKTINFO/IN6_PKTINFO structs to specify a source IP and source interface, but AFAIK the only way to specify a source port is through bind().

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • ok, I will wait a short while and accept this answer, as that is in line what my research suggests as well. – Vega4 Jan 13 '23 at 07:56