1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
#define _GNU_SOURCE
#ifdef __MINGW32__
#include <winsock2.h>
#endif
#include <sys/param.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "iarray.h"
#include "windoze.h"
#include "byte.h"
#include "socket.h"
#include "ip6.h"
#include "haveip6.h"
#include "havesl.h"
#include "havescope.h"
#ifdef __MINGW32__
#include <windows.h>
#include <mswsock.h>
#include <errno.h>
#include <stdio.h>
#include "io_internal.h"
#endif
#include "haveaccept4.h"
int socket_accept6_flags(int s, char ip[16], uint16* port, uint32* scope_id, int flags)
{
#ifdef LIBC_HAS_IP6
struct sockaddr_in6 sa = { 0 };
#else
struct sockaddr_in sa = { 0 };
#endif
socklen_t dummy = sizeof sa;
int fd;
#ifdef __MINGW32__
// Windows uses overlapped I/O instead of non-blocking I/O
io_entry* e=iarray_get(&io_fds,s);
if (e && e->inuse) {
int sa2len;
fd=-1;
if (e->acceptqueued==1) {
errno=EAGAIN;
return -1;
}
if (e->acceptqueued==2) {
incoming:
/* incoming! */
{
struct sockaddr* x,* y;
GetAcceptExSockaddrs(e->inbuf,0,200,200,&x,&sa2len,&y,&dummy);
if ((size_t)dummy>sizeof(sa)) dummy=sizeof(sa);
memcpy(&sa,y,dummy);
}
fd=e->next_accept;
e->next_accept=0;
if (e->nonblock) {
if (io_fd(fd)) {
io_entry* f=iarray_get(&io_fds,fd);
if (f) {
f->nonblock=1;
// printf("setting fd %lu to non-blocking\n",(int)fd);
}
}
}
}
/* no accept queued, queue one now. */
if (e->next_accept==0) {
e->next_accept=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (e->next_accept==(SOCKET)-1)
return winsock2errno(-1);
}
if (AcceptEx(s,e->next_accept,e->inbuf,0,200,200,&e->errorcode,&e->or))
goto incoming;
if (WSAGetLastError() != ERROR_IO_PENDING)
return winsock2errno(-1);
e->acceptqueued=1;
if (fd==-1) {
errno=EAGAIN;
return fd;
}
} else {
#endif
#ifdef HAVE_ACCEPT4
static int noaccept4; // auto initialized to 0
if (noaccept4)
fd=-1;
else {
int flg=0;
if (flags & SOCKET_NONBLOCK) flg += SOCK_NONBLOCK;
if (flags & SOCKET_CLOEXEC) flg += SOCK_CLOEXEC;
if ((fd=accept4(s,(void*) &sa, &dummy, flg))==-1) {
if (errno != ENOSYS)
return -1;
noaccept4=1;
}
}
if (fd==-1) {
#endif
int fl = 0;
fd = accept(s, (struct sockaddr *) &sa, &dummy);
if (fd == -1)
return -1;
#ifndef __MINGW32__
if (flags & SOCKET_NONBLOCK) fl |= O_NDELAY;
if (flags & SOCKET_CLOEXEC) fl |= O_CLOEXEC;
/* On BSD the accepted socket inherits O_NDELAY and O_CLOEXEC, on
* Linux it doesn't. accept4 makes this explicit. So for the
* fallback, make it explicit as well */
#ifdef __linux__
if (fl) {
#endif
if (fcntl(fd,F_SETFL,(fcntl(fd,F_GETFL,0) | fl) &~ (O_NDELAY | O_CLOEXEC)) == -1) {
/* This should never fail! */
close(fd);
return -1;
}
#ifdef __linux__
}
#endif
#endif
#ifdef HAVE_ACCEPT4
}
#endif
#ifdef __MINGW32__
}
#endif
#ifdef LIBC_HAS_IP6
if (noipv6 || sa.sin6_family==AF_INET || sa.sin6_family==PF_INET) {
struct sockaddr_in *sa4=(struct sockaddr_in*)&sa;
if (ip) {
byte_copy(ip,12,V4mappedprefix);
byte_copy(ip+12,4,(char *) &sa4->sin_addr);
}
if (port) uint16_unpack_big((char *) &sa4->sin_port,port);
return fd;
}
if (ip) byte_copy(ip,16,(char *) &sa.sin6_addr);
if (port) uint16_unpack_big((char *) &sa.sin6_port,port);
#ifdef LIBC_HAS_SCOPE_ID
if (scope_id) *scope_id=sa.sin6_scope_id;
#else
if (scope_id) *scope_id=0;
#endif
return fd;
#else
if (ip) {
byte_copy(ip,12,V4mappedprefix);
byte_copy(ip+12,4,(char *) &sa.sin_addr);
}
if (port) uint16_unpack_big((char *) &sa.sin_port,port);
if (scope_id) *scope_id=0;
return fd;
#endif
}
|