-
Notifications
You must be signed in to change notification settings - Fork 1
/
lua-socket.c
169 lines (140 loc) · 2.77 KB
/
lua-socket.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <lua.h>
#include <lauxlib.h>
#include <compat-5.2.h>
#include <errno.h>
#if defined(_WIN32) || defined(_WIN64)
#define _WIN32_WINNT 0x0501
#include <winsock2.h>
#include <ws2tcpip.h>
static void
init_winsock() {
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);
}
#define close(fd) closesocket(fd)
#else
#include <netdb.h>
#include <unistd.h>
#define INVALID_SOCKET (-1)
static void
init_winsock() {
}
#endif
#ifndef MSG_WAITALL
#define MSG_WAITALL 0
#endif
#define LOCALBUFFER 8192
static int
lopen(lua_State *L) {
const char * host = luaL_checkstring(L,1);
int port = luaL_checkinteger(L,2);
char port_str[32];
int status;
struct addrinfo ai_hints;
struct addrinfo *ai_list = NULL;
struct addrinfo *ai_ptr = NULL;
sprintf( port_str, "%d", port );
memset( &ai_hints, 0, sizeof( ai_hints ) );
ai_hints.ai_family = AF_UNSPEC;
ai_hints.ai_socktype = SOCK_STREAM;
ai_hints.ai_protocol = IPPROTO_TCP;
status = getaddrinfo( host, port_str, &ai_hints, &ai_list );
if ( status != 0 ) {
return 0;
}
int sock=INVALID_SOCKET;
for ( ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next ) {
sock = socket( ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol );
if ( sock == INVALID_SOCKET ) {
continue;
}
status = connect( sock, ai_ptr->ai_addr, ai_ptr->ai_addrlen );
if ( status != 0 ) {
close(sock);
sock = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo( ai_list );
if (sock != INVALID_SOCKET) {
lua_pushinteger(L, sock);
return 1;
}
return 0;
}
static int
lclose(lua_State *L) {
int sock = luaL_checkinteger(L, 1);
close(sock);
return 0;
}
static int
lread(lua_State *L) {
int fd = luaL_checkinteger(L,1);
int sz = luaL_checkinteger(L,2);
char tmp[LOCALBUFFER];
void * buffer = tmp;
if (sz > LOCALBUFFER) {
buffer = lua_newuserdata(L, sz);
}
char * ptr = buffer;
for (;;) {
int bytes = recv(fd, ptr, sz, MSG_WAITALL);
if (bytes < 0) {
switch (errno) {
case EAGAIN:
case EINTR:
continue;
}
return 0;
}
if (bytes == 0) {
return 0;
}
if (bytes < sz) {
ptr += bytes;
sz -= bytes;
return 0;
}
lua_pushlstring(L, buffer, sz);
return 1;
}
}
static int
lwrite(lua_State *L) {
int sock = luaL_checkinteger(L,1);
size_t sz = 0;
const char *buffer = luaL_checklstring(L, 2, &sz);
for (;;) {
int err = send(sock, buffer, sz, 0);
if (err < 0) {
switch (errno) {
case EAGAIN:
case EINTR:
continue;
}
}
if (err != sz) {
lua_pushboolean(L,0);
return 1;
}
break;
}
lua_pushboolean(L,1);
return 1;
}
int
luaopen_mongo_socket(lua_State *L) {
init_winsock();
luaL_checkversion(L);
luaL_Reg l[] ={
{ "open", lopen },
{ "close", lclose },
{ "read", lread },
{ "write", lwrite },
{ NULL, NULL },
};
luaL_newlib(L,l);
return 1;
}