- if (fcntl(st->fd, F_SETFL, fcntl(st->fd, F_GETFL)|O_NONBLOCK)==-1) {
- fatal_perror("udp (%s:%d): fcntl(set O_NONBLOCK)",
- st->loc.file,st->loc.line);
- }
- if (fcntl(st->fd, F_SETFD, FD_CLOEXEC)==-1) {
- fatal_perror("udp (%s:%d): fcntl(set FD_CLOEXEC)",
- st->loc.file,st->loc.line);
+}
+
+bool_t udp_make_socket(struct udpcommon *uc, struct udpsock *us,
+ int failmsgclass)
+{
+ const union iaddr *addr=&us->addr;
+ struct commcommon *cc=&uc->cc;
+ us->fd=-1;
+
+#define FAIL_LG 0, cc->cl.description, &cc->loc, failmsgclass
+#define FAIL(...) do{ \
+ lg_perror(FAIL_LG,errno,__VA_ARGS__); \
+ goto failed; \
+ }while(0)
+
+ FILLZERO(us->experienced);
+ us->fd=socket(addr->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
+ if (us->fd<0) FAIL("socket");
+ setnonblock(us->fd);
+ setcloexec(us->fd);
+#ifdef CONFIG_IPV6
+ if (addr->sa.sa_family==AF_INET6) {
+ int r;
+ int optval=1;
+ socklen_t optlen=sizeof(optval);
+ r=setsockopt(us->fd,IPPROTO_IPV6,IPV6_V6ONLY,&optval,optlen);
+ if (r) FAIL("setsockopt(,IPV6_V6ONLY,&1,)");