/
cbits.c
51 lines (41 loc) · 971 Bytes
/
cbits.c
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
#ifndef WINDOWS
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/un.h>
#define SAFE_ON_WIN unsafe
#ifndef AF_LOCAL
#define AF_LOCAL AF_UNIX
#endif
#else /* WINDOWS */
#include <winsock2.h>
#include <ws2tcpip.h>
#define sa_family_t short
#define AI_NUMERICSERV 0
struct network_fancy_aaccept {
SOCKET s;
struct sockaddr *addr;
int alen;
};
#define SAFE_ON_WIN safe
#endif
//int getSocketError(void) { return errno; }
int getsockopt_error(int fd) {
int estat, res;
socklen_t len = sizeof(int);
estat = 0;
#ifdef WINDOWS
res = getsockopt(fd, SOL_SOCKET, SO_ERROR, (char*)&estat, &len);
#else
res = getsockopt(fd, SOL_SOCKET, SO_ERROR, &estat, &len);
#endif
return (res == 0) ? estat : errno;
}
#ifdef WINDOWS
int c_nf_async_accept(struct network_fancy_aaccept *nfa) {
SOCKET n = accept(nfa->s, nfa->addr, &nfa->alen);
nfa->s = n;
return (n != INVALID_SOCKET) ? 0 : GetLastError();
}
#endif