summaryrefslogtreecommitdiff
path: root/src/l_tcp.c
blob: 158a8ba8072676615ec4d458aaad5d2aea5344c7 (plain) (blame)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 *  gcsd
 *
 *  gcsd the generic command sequencer daemon can be used to serialize 
 *  commands sent over various paralell communication channels to a
 *  single command output. It can be seen as a multiplexer for any
 *  kind of communication between a single resource and various clients
 *  which want to submit commands to it or query information from it.
 *  gcsd is written in C and Lua. The goal is to provide an easy to 
 *  understand high level API based on Lua which can be used to
 *  implement the business logic of the so formed multiplexer daemon.
 *  
 *
 *  Copyright (C) 2009-2010 Markus Grueneis <gimpf@spreadspace.org>
 *                          Christian Pointner <equinox@spreadspace.org>
 *
 *  This file is part of gcsd.
 *
 *  gcsd is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  gcsd is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with gcsd. If not, see <http://www.gnu.org/licenses/>.
 */

#include <lua.h>
#include <lauxlib.h>

#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

#include "l_tcp.h"

#include "datatypes.h"
#include "log.h"

typedef struct {
  socklen_t len_;
  struct sockaddr_storage addr_;
} tcp_endpoint_t;
#define LUA_TCP_UDATA_NAME "tcp_endpoint_t"


static tcp_endpoint_t* newtcpendudata(lua_State *L)
{
  tcp_endpoint_t* end = (tcp_endpoint_t*)lua_newuserdata(L, sizeof(tcp_endpoint_t));
  memset(end, 0, sizeof(end));
  luaL_newmetatable(L, LUA_TCP_UDATA_NAME);
  lua_setmetatable(L, -2);
  return end;
}

static char* tcp_endpoint_to_string(tcp_endpoint_t* e)
{
  size_t addrstr_len = 0;
  char addrstr[INET6_ADDRSTRLEN + 1], portstr[6], *ret;
  char addrport_sep = ':';
  
  switch(e->addr_.ss_family)
  {
  case AF_INET: addrport_sep = ':'; break;
  case AF_INET6: addrport_sep = '.'; break;
  case AF_UNSPEC: return NULL;
  default: asprintf(&ret, "unknown address type"); return ret;
  }

  int errcode  = getnameinfo((struct sockaddr *)&(e->addr_), e->len_, addrstr, sizeof(addrstr), portstr, sizeof(portstr), NI_NUMERICHOST | NI_NUMERICSERV);
  if (errcode != 0) return NULL;
  asprintf(&ret, "%s%c%s", addrstr, addrport_sep ,portstr);
  return ret;
}

static int init_listener(tcp_endpoint_t* end)
{
  int fd = socket(end->addr_.ss_family, SOCK_STREAM, 0);
  if(fd < 0) {
    log_printf(ERROR, "tcp: error on opening socket: %s", strerror(errno));
    return -1;
  }

  int on = 1;
  int ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  if(ret) {
    log_printf(ERROR, "tcp: error on setsockopt(): %s", strerror(errno));
    return -1;
  }
  if(end->addr_.ss_family == AF_INET6) {
    if(setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)))
      log_printf(WARNING, "tcp: failed to set IPV6_V6ONLY socket option: %s", strerror(errno));
  }

  char* ls = tcp_endpoint_to_string(end);
  ret = bind(fd, (struct sockaddr *)&(end->addr_), end->len_);
  if(ret) {
    log_printf(ERROR, "tcp: error on bind(%s): %s", ls ? ls:"", strerror(errno));
    if(ls) free(ls);
    return -1;
  }
  free(ls);

  ret = listen(fd, 0);
  if(ret) {
    log_printf(ERROR, "tcp: error on listen(): %s", strerror(errno));
    if(ls) free(ls);
    return -1;
  }

  return fd;
}

static int l_tcp_server(lua_State *L)
{
  const char* addr = luaL_optstring(L, 1, "*");
  if(!strcmp(addr, "*") || !strlen(addr)) addr = NULL;
  const char* port = luaL_checkstring(L, 2);
  int af = luaL_optint(L, 3, 0);
  
  struct addrinfo hints, *res;
  res = NULL;
  memset (&hints, 0, sizeof (hints));
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
  switch(af) {
  case 4: hints.ai_family = AF_INET; break;
  case 6: hints.ai_family = AF_INET6; break;
  default: hints.ai_family = AF_UNSPEC; break;
  }

  int errcode = getaddrinfo(addr, port, &hints, &res);
  if(errcode != 0 || !res) {
    log_printf(ERROR, "tcp: resolver error: %s", errcode ? gai_strerror(errcode):"no address found!");
    lua_pushnil(L);
    lua_pushstring(L, "error at tcp-server initialization");
    return 2;
  }

  lua_newtable(L);
  int idx = 1;
  struct addrinfo* r = res;
  while(r) {
    lua_pushinteger(L, idx++);
    lua_newtable(L);

    lua_pushliteral(L, "local_end");
    tcp_endpoint_t* end = newtcpendudata(L);
    memcpy(&(end->addr_), r->ai_addr, r->ai_addrlen);
    end->len_ = r->ai_addrlen;
    lua_settable(L, -3);

    int fd = init_listener(end);
    if(fd < 0) {
      freeaddrinfo(res);
      lua_pushnil(L);
      lua_pushstring(L, "error at tcp-server initialization");
      return 2;
    }

    lua_pushliteral(L, "fd");
    lua_pushinteger(L, fd);
    lua_settable(L, -3);

    lua_settable(L, -3);
    r = r->ai_next;
  }
  freeaddrinfo(res);

  return 1;
}

static int l_tcp_accept(lua_State *L)
{
  int fd = luaL_checkint(L, 1);
  tcp_endpoint_t* remote_addr = newtcpendudata(L);
  remote_addr->len_ = sizeof(remote_addr->addr_);
  int new_client = accept(fd, (struct sockaddr *)&(remote_addr->addr_), &remote_addr->len_);
  if(new_client == -1) {
    lua_pushnil(L);
    lua_pushstring(L, strerror(errno));
    return 2;
  }
  char* rs = tcp_endpoint_to_string(remote_addr);
  log_printf(INFO, "new client from %s (fd=%d)", rs ? rs:"(null)", new_client);
  if(rs) free(rs);

  lua_pushinteger(L, new_client);
  lua_insert(L, -2);
  return 2;
}

static int l_tcp_client(lua_State *L)
{
      // TODO: implement this
  return 0;
}

static int l_tcp_connect(lua_State *L)
{
      // TODO: implement this
  return 0;
}

static int l_tcp_recv(lua_State *L)
{
  int fd = luaL_checkint(L,1);
  size_t len = luaL_checkint(L,2);
  char* data = malloc(len);
  if(!data) {
    lua_pushnil(L);
    lua_pushstring(L, "bad alloc");
    return 2;
  }
    
  int ret = recv(fd, data, len, 0);
  
  if(ret == -1) {
    lua_pushnil(L);
// FIXXXXXME: strerror is not threadsafe!!!
    lua_pushstring(L, strerror(errno));
    free(data);
    return 2;
  }
  lua_pushlstring(L, data, ret);
  free(data);
  return 1;
}

static int l_tcp_send(lua_State *L)
{
  int fd = luaL_checkint(L,1);
  size_t len = 0;
  const char* data = luaL_checklstring(L, 2, &len);
  
  int ret = send(fd, data, len, 0);
  
  if(ret == -1) {
    lua_pushnil(L);
// FIXXXXXME: strerror is not threadsafe!!!
    lua_pushstring(L, strerror(errno));
    return 2;
  }
  lua_pushinteger(L, ret);
  return 1;
}

static int l_tcp_endtostring(lua_State *L)
{
  tcp_endpoint_t* e = (tcp_endpoint_t*)luaL_checkudata(L, 1, LUA_TCP_UDATA_NAME);
  char* es = tcp_endpoint_to_string(e);
  if(!es)
    luaL_error(L, "bad alloc");

  lua_pushstring(L, es);
  free(es);
  return 1;
}

static const struct luaL_reg tcp_funcs [] = {
  { "server", l_tcp_server },
  { "accept", l_tcp_accept },
  { "client", l_tcp_client },
  { "connect", l_tcp_connect },
  { "recv", l_tcp_recv },
  { "send", l_tcp_send },
  { "endtostring", l_tcp_endtostring },
  { NULL, NULL }
};

LUALIB_API int luaopen_tcp(lua_State *L) 
{
  luaL_register(L, LUA_TCPLIBNAME, tcp_funcs);
  return 1;
}