Repository navigation
Expand file tree
/
Copy pathluv_tcp.c
More file actions
145 lines (113 loc) · 3.73 KB
/
Copy pathluv_tcp.c
File metadata and controls
145 lines (113 loc) · 3.73 KB
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
5
8000
8
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
#ifndef LIB_LUV_TCP
#define LIB_LUV_TCP
#include "common.h"
static void luv_after_connect(uv_connect_t* req, int status) {
lua_State* L = luv_prepare_callback(req->data);
if (lua_isfunction(L, -1)) {
lua_call(L, 0, 0);
} else {
lua_pop(L, 1);
}
luv_handle_unref(L, req->handle->data);
free(req->data);
free(req);
}
static int luv_tcp_bind(lua_State* L) {
uv_tcp_t* handle = luv_get_tcp(L, 1);
const char* host = luaL_checkstring(L, 2);
int port = luaL_checkint(L, 3);
struct sockaddr_in address = uv_ip4_addr(host, port);
if (uv_tcp_bind(handle, address)) {
luaL_error(L, "Problem binding");
}
return 0;
}
static int luv_tcp_getsockname(lua_State* L) {
uv_tcp_t* handle = luv_get_tcp(L, 1);
int port = 0;
char ip[INET6_ADDRSTRLEN];
int family;
struct sockaddr_storage address;
int addrlen = sizeof(address);
if (uv_tcp_getsockname(handle, (struct sockaddr*)(&address), &addrlen)) {
uv_err_t err = uv_last_error(uv_default_loop());
return luaL_error(L, "tcp_getsockname: %s", uv_strerror(err));
}
family = address.ss_family;
if (family == AF_INET) {
struct sockaddr_in* addrin = (struct sockaddr_in*)&address;
uv_inet_ntop(AF_INET, &(addrin->sin_addr), ip, INET6_ADDRSTRLEN);
port = ntohs(addrin->sin_port);
} else if (family == AF_INET6) {
struct sockaddr_in6* addrin6 = (struct sockaddr_in6*)&address;
uv_inet_ntop(AF_INET6, &(addrin6->sin6_addr), ip, INET6_ADDRSTRLEN);
port = ntohs(addrin6->sin6_port);
}
lua_newtable(L);
lua_pushnumber(L, port);
lua_setfield(L, -2, "port");
lua_pushnumber(L, family);
lua_setfield(L, -2, "family");
lua_pushstring(L, ip);
lua_setfield(L, -2, "address");
return 1;
}
static int luv_tcp_getpeername(lua_State* L) {
uv_tcp_t* handle = luv_get_tcp(L, 1);
int port = 0;
char ip[INET6_ADDRSTRLEN];
int family;
struct sockaddr_storage address;
int addrlen = sizeof(address);
if (uv_tcp_getpeername(handle, (struct sockaddr*)(&address), &addrlen)) {
uv_err_t err = uv_last_error(uv_default_loop());
return luaL_error(L, "tcp_getpeername: %s", uv_strerror(err));
}
family = address.ss_family;
if (family == AF_INET) {
struct sockaddr_in* addrin = (struct sockaddr_in*)&address;
uv_inet_ntop(AF_INET, &(addrin->sin_addr), ip, INET6_ADDRSTRLEN);
port = ntohs(addrin->sin_port);
} else if (family == AF_INET6) {
struct sockaddr_in6* addrin6 = (struct sockaddr_in6*)&address;
uv_inet_ntop(AF_INET6, &(addrin6->sin6_addr), ip, INET6_ADDRSTRLEN);
port = ntohs(addrin6->sin6_port);
}
lua_newtable(L);
lua_pushnumber(L, port);
lua_setfield(L, -2, "port");
lua_pushnumber(L, family);
lua_setfield(L, -2, "family");
lua_pushstring(L, ip);
lua_setfield(L, -2, "address");
return 1;
}
static int luv_tcp_connect(lua_State* L) {
uv_tcp_t* handle = luv_get_tcp(L, 1);
const char* ip_address = luaL_checkstring(L, 2);
int port = luaL_checkint(L, 3);
struct sockaddr_in address = uv_ip4_addr(ip_address, port);
uv_connect_t* req = (uv_connect_t*)malloc(sizeof(uv_connect_t));
luv_req_t* lreq = (luv_req_t*)malloc(sizeof(luv_req_t));
req->data = (void*)lreq;
lreq->lhandle = handle->data;
if (uv_tcp_connect(req, handle, address, luv_after_connect)) {
free(req->data);
free(req);
uv_err_t err = uv_last_error(uv_default_loop());
return luaL_error(L, "tcp_connect: %s", uv_strerror(err));
}
lreq->data_ref = LUA_NOREF;
lua_pushvalue(L, 4);
lreq->callback_ref = luaL_ref(L, LUA_REGISTRYINDEX);
luv_handle_ref(L, handle->data, 1);
return 0;
}
static const luaL_reg luv_tcp_m[] = {
{"bind", luv_tcp_bind},
{"getsockname", luv_tcp_getsockname},
{"getpeername", luv_tcp_getpeername},
{"connect", luv_tcp_connect},
{NULL, NULL}
};
#endif