summaryrefslogtreecommitdiff
path: root/src/win32/win32_socket.h
blob: fb622f546c7759a0c813e4deea7c3253724b3fd3 (plain)
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
#if defined(_WIN32)

#include <stdint.h>

#define _USE_W32_SOCKETS 1

#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable: 4255 4668 4820)
#endif

#include <io.h>
#include <winsock2.h>

#if defined(_MSC_VER)
#pragma comment(lib, "ws2_32.lib")
#endif

#include <unistd.h>

#if defined(_MSC_VER)
#pragma warning(pop)
#endif

/*
 * winsock doesn't feature poll(), so there is a version implemented in terms of select() in win32_socket.c.
 * The following definitions are copied from linux man pages.
 * A poll() macro is defined to call the version in win32_socket.c.
 */
#if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0600)

#define POLLIN      0x0001    /* There is data to read */
#define POLLPRI     0x0002    /* There is urgent data to read */
#define POLLOUT     0x0004    /* Writing now will not block */
#define POLLERR     0x0008    /* Error condition */
#define POLLHUP     0x0010    /* Hung up */
#define POLLNVAL    0x0020    /* Invalid request: fd not open */
struct pollfd {
    SOCKET fd;        /* file descriptor */
    int16_t events;     /* requested events */
    int16_t revents;    /* returned events */
};
#endif
#define poll(x, y, z)     win32_poll(x, y, z)

/* 
 * These wrappers do nothing special except set the global errno variable if an error occurs
 * (winsock doesn't do this by default).
 * They set errno to unix-like values (i.e. WSAEWOULDBLOCK is mapped to EAGAIN),
 * so code outside of this file "shouldn't" have to worry about winsock specific error handling.
 */
#define socket(x, y, z)   win32_socket(x, y, z)
#define connect(x, y, z)  win32_connect(x, y, z)
#define accept(x, y, z)   win32_accept(x, y, z)
#define shutdown(x, y)    win32_shutdown(x, y)
#define read(x, y, z)     win32_read_socket(x, y, z)
#define write(x, y, z)    win32_write_socket(x, y, z)

/* Winsock uses int32_t instead of the usual socklen_t */
typedef int32_t socklen_t;

int32_t win32_poll(struct pollfd *, uint32_t, int32_t);
SOCKET  win32_socket(int32_t, int32_t, int32_t);
int32_t win32_connect(SOCKET, struct sockaddr*, socklen_t);
SOCKET  win32_accept(SOCKET, struct sockaddr*, socklen_t *);
int32_t win32_shutdown(SOCKET, int32_t);
int32_t win32_close_socket(SOCKET fd);

#define strtok_r(x, y, z) win32_strtok_r(x, y, z)
#define strsep(x,y)       win32_strsep(x,y)

char *win32_strtok_r(char *s, const char *delim, char **lasts);
char *win32_strsep(char **stringp, const char *delim);

ssize_t win32_read_socket(SOCKET fd, void *buf, int32_t n);
ssize_t win32_write_socket(SOCKET fd, void *buf, int32_t n);

#endif // defined(_WIN32)