aboutsummaryrefslogtreecommitdiff
path: root/win32
diff options
context:
space:
mode:
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>2010-04-14 02:16:09 +0200
committerNguyễn Thái Ngọc Duy <pclouds@gmail.com>2010-04-20 19:14:19 +0200
commit8aed602855bbc9a5b614515833796830a3437a98 (patch)
tree46c15dd5562874e39925bf8c751c71ab21159106 /win32
parent87a429e345437cb476295f2a09e9690eaa90f9f7 (diff)
downloadbusybox-w32-8aed602855bbc9a5b614515833796830a3437a98.tar.gz
busybox-w32-8aed602855bbc9a5b614515833796830a3437a98.tar.bz2
busybox-w32-8aed602855bbc9a5b614515833796830a3437a98.zip
win32: mingw32/socket.c
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Diffstat (limited to 'win32')
-rw-r--r--win32/socket.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/win32/socket.c b/win32/socket.c
new file mode 100644
index 000000000..4ebe2ef92
--- /dev/null
+++ b/win32/socket.c
@@ -0,0 +1,57 @@
1#include "libbb.h"
2#include "win32.h"
3#include "strbuf.h"
4#include "git.h"
5
6int mingw_socket(int domain, int type, int protocol)
7{
8 int sockfd;
9 SOCKET s = WSASocket(domain, type, protocol, NULL, 0, 0);
10 if (s == INVALID_SOCKET) {
11 /*
12 * WSAGetLastError() values are regular BSD error codes
13 * biased by WSABASEERR.
14 * However, strerror() does not know about networking
15 * specific errors, which are values beginning at 38 or so.
16 * Therefore, we choose to leave the biased error code
17 * in errno so that _if_ someone looks up the code somewhere,
18 * then it is at least the number that are usually listed.
19 */
20 errno = WSAGetLastError();
21 return -1;
22 }
23 /* convert into a file descriptor */
24 if ((sockfd = _open_osfhandle(s, O_RDWR|O_BINARY)) < 0) {
25 closesocket(s);
26 return error("unable to make a socket file descriptor: %s",
27 strerror(errno));
28 }
29 return sockfd;
30}
31#undef connect
32int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz)
33{
34 SOCKET s = (SOCKET)_get_osfhandle(sockfd);
35 return connect(s, sa, sz);
36}
37
38void winsock_init()
39{
40 static int init = 0;
41 if (!init) {
42 WSADATA wsa;
43 if (WSAStartup(MAKEWORD(2,2), &wsa))
44 die("unable to initialize winsock subsystem, error %d",
45 WSAGetLastError());
46 atexit((void(*)(void)) WSACleanup);
47 init = 1;
48 }
49}
50
51/* this is the first function to call into WS_32; initialize it */
52#undef gethostbyname
53struct hostent *mingw_gethostbyname(const char *host)
54{
55 winsock_init();
56 return gethostbyname(host);
57}