On Thu, 2017-03-23 at 13:22 +1100, Daurnimator wrote: > On 9 March 2017 at 14:10, Daurnimator <q...@daurnimator.com> wrote: > > When debugging https://github.com/daurnimator/lua-http/issues/73 which > > uses https://github.com/wahern/dns we ran into an issue where modern > > linux kernels return EINVAL if you try and re-use a udp socket. > > The issue seems to occur if you go from a local destination ip to a > > non-local one. > > Did anyone get a chance to look into this issue?
I believe man page is not complete. A disconnect is needed before another connect() eg : #include <stdio.h> #include <stdlib.h> #include <sys/socket.h> #include <netinet/ip.h> #include <arpa/inet.h> #include <errno.h> int main() { int fd = socket(PF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_IP); if (fd == -1) exit(1); if (bind(fd, (struct sockaddr*)&(struct sockaddr_in){.sin_family=AF_INET, .sin_port=htons(57997), .sin_addr=inet_addr("0.0.0.0")}, 16)) exit(2); if (connect(fd, (struct sockaddr*)&(struct sockaddr_in){.sin_family=AF_INET, .sin_port=htons(53), .sin_addr=inet_addr("127.0.0.2")}, 16)) exit(3); if (-1 == sendto(fd, "test", 4, 0, NULL, 0)) exit(4); char buf[200]; if (-1 != recvfrom(fd, buf, 200, 0, 0, 0) && errno != ECONNREFUSED) exit(5); /* okay, try next server... */ /* disconnect */ if (connect(fd, (struct sockaddr*)&(struct sockaddr_in){.sin_family=AF_UNSPEC}, 16)) exit(6); if (connect(fd, (struct sockaddr*)&(struct sockaddr_in){.sin_family=AF_INET, .sin_port=htons(53), .sin_addr=inet_addr("8.8.8.8")}, 16)) exit(7); exit(0); }