The connection-oriented semantics of TCP indicate that a socket can only connect to one endpoint at a time. But with UDP, a single open socket can send/receive datagrams to/from any number of endpoints. My understanding is that most operating systems (at least Linux and Windows) will assign an ephemeral port to a UDP socket automatically as soon as sendto
is called.
My question is: when using UDP, what are the best practices, in terms of security, when writing applications that may send datagrams to multiple remote endpoints at a time? Should the socket "close" and reopen each before sending datagrams to a different remote endpoint?
Consider, for example, a DNS server that needs to resolve a query. A DNS server may need to send datagrams to many different remote endpoints when resolving a hostname recursively - i.e. first, it must send/receive datagrams from some root server, then from a TLD server, and so on. Should a DNS server in this case reuse the same socket/ephemeral port when sending/receiving datagrams to all these different servers? Or is it better to close/reopen a socket before sending to a different server? Are there any security implications at play here?