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
/*
* frankensocket - tiny socket compatibility layer for liveside.
*
* This header keeps the common BSD socket vocabulary visible on POSIX and
* Win32 while naming the unavoidable differences explicitly.
*/
#ifndef FRANKENSOCKET_H
#define FRANKENSOCKET_H
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")
typedef int FrankenSockError;
static int
frankensocket_startup(void)
{
WSADATA wsa;
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
fprintf(stderr, "[liveside] WSAStartup failed\n");
return -1;
}
return 0;
}
static void
frankensocket_cleanup(void)
{
WSACleanup();
}
static void
frankensocket_ignore_sigpipe(void)
{
}
static FrankenSockError
frankensocket_error(void)
{
return WSAGetLastError();
}
static void
frankensocket_perror(const char *msg)
{
fprintf(stderr, "%s: WSA error %d\n", msg, frankensocket_error());
}
static int
frankensocket_close(SOCKET s)
{
return closesocket(s);
}
static long
frankensocket_send(SOCKET s, const void *buf, size_t len)
{
return send(s, (const char *)buf, (int)len, 0);
}
static long
frankensocket_recv(SOCKET s, void *buf, size_t len)
{
return recv(s, (char *)buf, (int)len, 0);
}
#else
#include <errno.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <signal.h>
#include <sys/socket.h>
#include <unistd.h>
typedef int SOCKET;
typedef int FrankenSockError;
#ifndef INVALID_SOCKET
#define INVALID_SOCKET (-1)
#endif
static int
frankensocket_startup(void)
{
return 0;
}
static void
frankensocket_cleanup(void)
{
}
static void
frankensocket_ignore_sigpipe(void)
{
signal(SIGPIPE, SIG_IGN);
}
static FrankenSockError
frankensocket_error(void)
{
return errno;
}
static void
frankensocket_perror(const char *msg)
{
fprintf(stderr, "%s: %s\n", msg, strerror(frankensocket_error()));
}
static int
frankensocket_close(SOCKET s)
{
return close(s);
}
static long
frankensocket_send(SOCKET s, const void *buf, size_t len)
{
return (long)send(s, buf, len, 0);
}
static long
frankensocket_recv(SOCKET s, void *buf, size_t len)
{
return (long)recv(s, buf, len, 0);
}
#endif
#endif /* FRANKENSOCKET_H */