-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsockets-stub.c
83 lines (69 loc) · 1.67 KB
/
sockets-stub.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
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
#ifdef __NT__
#define WIN32
#elif defined __WINDOWS__
#define WIN32
#endif
#ifdef WIN32
#define EXPORTED(type) __declspec ( dllexport ) type cdecl
#include <stddef.h>
#include <winsock2.h>
#include <ws2tcpip.h>
typedef int ssize_t;
#define SCHEME_STATIC
#else
#define EXPORTED(type) type
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <sys/unistd.h>
#include <sys/fcntl.h>
#endif
#ifdef WIN32
extern __declspec(dllimport) int cdecl Sactivate_thread(void);
extern __declspec(dllimport) void cdecl Sdeactivate_thread(void);
#else
#include "scheme.h"
#endif
EXPORTED(int)
get_errno()
{
return errno;
}
#define GUARD(exp) do { \
Sdeactivate_thread(); \
exp; \
Sactivate_thread(); \
} while (0)
/* Blocking Accept */
EXPORTED(int)
accept_block(int fd, struct sockaddr *addr, socklen_t *addrlen) {
int ret;
GUARD(ret = accept(fd, addr, addrlen));
return ret;
}
/* Blocking Connect */
EXPORTED(int)
connect_block(int fd, const struct sockaddr *addr, socklen_t addrlen) {
int ret;
GUARD(ret = connect(fd, addr, addrlen));
return ret;
}
/* Blocking Receive */
EXPORTED(ssize_t)
recvfrom_block(int fd, void *buf, size_t len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen) {
ssize_t ret;
GUARD(ret = recvfrom(fd, buf, len, flags, src_addr, addrlen));
return ret;
}
/* Blocking Send To */
EXPORTED(int)
sendto_block(int fd, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen) {
ssize_t ret;
GUARD(ret = sendto(fd, buf, len, flags, dest_addr, addrlen));
return ret;
}