1 #include <sys/types.h>
2 #ifndef __MINGW32__
3 #include <unistd.h>
4 #include <sys/socket.h>
5 #include <netinet/in.h>
6 #endif
7 #include "windoze.h"
8 #include <errno.h>
9 #include "haveip6.h"
10 #include "socket.h"
11 #include "ndelay.h"
12 
13 #ifndef EAFNOSUPPORT
14 #define EAFNOSUPPORT EINVAL
15 #endif
16 #ifndef EPFNOSUPPORT
17 #define EPFNOSUPPORT EAFNOSUPPORT
18 #endif
19 #ifndef EPROTONOSUPPORT
20 #define EPROTONOSUPPORT EAFNOSUPPORT
21 #endif
22 
socket_udp6b(void)23 int socket_udp6b(void)
24 {
25 #ifdef LIBC_HAS_IP6
26   int s;
27 
28   __winsock_init();
29   if (noipv6) goto compat;
30   s = winsock2errno(socket(PF_INET6,SOCK_DGRAM,0));
31   if (s == -1) {
32     if (errno == EINVAL || errno == EAFNOSUPPORT || errno == EPFNOSUPPORT || errno == EPROTONOSUPPORT) {
33 compat:
34       s=winsock2errno(socket(AF_INET,SOCK_DGRAM,0));
35       noipv6=1;
36       if (s==-1) return -1;
37     } else
38     return -1;
39   }
40 #ifdef IPV6_V6ONLY
41   {
42     int zero=0;
43     winsock2errno(setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&zero,sizeof(zero)));
44   }
45 #endif
46   return s;
47 #else
48   return socket_udp4b();
49 #endif
50 }
51 
socket_udp6(void)52 int socket_udp6(void) {
53   int s=socket_udp6b();
54   if (s!=-1 && ndelay_on(s) == -1) { close(s); return -1; }
55   return s;
56 }
57