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
|
#include "libbb.h"
int inet_aton(const char *cp, struct in_addr *inp)
{
unsigned long val = inet_addr(cp);
if (val == INADDR_NONE)
return 0;
inp->S_un.S_addr = val;
return 1;
}
void init_winsock(void)
{
WSADATA wsa;
static int initialized = 0;
if (initialized)
return;
if (WSAStartup(MAKEWORD(2,2), &wsa))
bb_error_msg_and_die("WSAStartup failed, error %d", WSAGetLastError());
atexit((void(*)(void)) WSACleanup);
initialized = 1;
}
#undef gethostname
int mingw_gethostname(char *name, int namelen)
{
init_winsock();
return gethostname(name, namelen);
}
#undef gethostbyaddr
struct hostent *mingw_gethostbyaddr(const void *addr, socklen_t len, int type)
{
init_winsock();
return gethostbyaddr(addr, len, type);
}
#undef getaddrinfo
int mingw_getaddrinfo(const char *node, const char *service,
const struct addrinfo *hints, struct addrinfo **res)
{
init_winsock();
return getaddrinfo(node, service, hints, res);
}
int mingw_socket(int domain, int type, int protocol)
{
int sockfd;
SOCKET s;
init_winsock();
s = WSASocket(domain, type, protocol, NULL, 0, 0);
if (s == INVALID_SOCKET) {
/*
* WSAGetLastError() values are regular BSD error codes
* biased by WSABASEERR.
* However, strerror() does not know about networking
* specific errors, which are values beginning at 38 or so.
* Therefore, we choose to leave the biased error code
* in errno so that _if_ someone looks up the code somewhere,
* then it is at least the number that are usually listed.
*/
errno = WSAGetLastError();
return -1;
}
/* convert into a file descriptor */
if ((sockfd = _open_osfhandle((intptr_t)s, O_RDWR|O_BINARY)) < 0) {
closesocket(s);
bb_error_msg("unable to make a socket file descriptor: %s",
strerror(errno));
return -1;
}
return sockfd;
}
#undef connect
int mingw_connect(int sockfd, const struct sockaddr *sa, size_t sz)
{
SOCKET s = (SOCKET)_get_osfhandle(sockfd);
return connect(s, sa, sz);
}
#undef bind
int mingw_bind(int sockfd, struct sockaddr *sa, size_t sz)
{
SOCKET s = (SOCKET)_get_osfhandle(sockfd);
return bind(s, sa, sz);
}
#undef setsockopt
int mingw_setsockopt(int sockfd, int lvl, int optname, void *optval, int optlen)
{
SOCKET s = (SOCKET)_get_osfhandle(sockfd);
return setsockopt(s, lvl, optname, (const char*)optval, optlen);
}
#undef shutdown
int mingw_shutdown(int sockfd, int how)
{
SOCKET s = (SOCKET)_get_osfhandle(sockfd);
return shutdown(s, how);
}
#undef listen
int mingw_listen(int sockfd, int backlog)
{
SOCKET s = (SOCKET)_get_osfhandle(sockfd);
return listen(s, backlog);
}
#undef accept
int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
{
int sockfd2;
SOCKET s1 = (SOCKET)_get_osfhandle(sockfd1);
SOCKET s2 = accept(s1, sa, sz);
/* convert into a file descriptor */
if ((sockfd2 = _open_osfhandle((intptr_t)s2, O_RDWR|O_BINARY)) < 0) {
int err = errno;
closesocket(s2);
bb_error_msg("unable to make a socket file descriptor: %s",
strerror(err));
return -1;
}
return sockfd2;
}
#undef getpeername
int mingw_getpeername(int fd, struct sockaddr *sa, socklen_t *sz)
{
SOCKET sock;
init_winsock();
sock = (SOCKET)_get_osfhandle(fd);
if (sock == INVALID_SOCKET) {
errno = EBADF;
return -1;
}
return getpeername(sock, sa, sz);
}
|