I notice now that I forgot to include help/usage changes.
But more importantly, after playing with this patch a bit more I find
that while it "seems" to work in a single-core VM, in other situations
this can lead to a race between client and server. What happens is that
the client fires off its message and then closes its socket before the
listener can call connect():
} else if (uflag && !kflag) {
/*
* For UDP and not -k, we will use recvfrom()
* initially to wait for a caller, then use
* the regular functions to talk to the caller.
*/
int rv;
char buf[2048];
struct sockaddr_storage z;
len = sizeof(z);
rv = recvfrom(s, buf, sizeof(buf), MSG_PEEK,
(struct sockaddr *)&z, &len);
if (rv == -1)
err(1, "recvfrom");
Here the message from the client was received and we have the remote
address in "z". But now if the client terminates and its socket is
shutdown the following connect fails:
rv = connect(s, (struct sockaddr *)&z, len);
if (rv == -1)
err(1, "connect");
Using "-k" in the listener avoids this, of course, since there no
connect() in that case. Hmm.