2 * nixio - Linux I/O library for lua
4 * Copyright (C) 2009 Steven Barth <steven@midlink.org>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
24 #include <sys/types.h>
28 * send() / sendto() helper
30 static int nixio_sock__sendto(lua_State *L, int to) {
31 nixio_sock *sock = nixio__checksock(L);
32 struct sockaddr *addr = NULL;
38 const char *address = luaL_checkstring(L, 3);
39 struct sockaddr_storage addrstor;
40 addr = (struct sockaddr*)&addrstor;
43 memset(&naddr, 0, sizeof(naddr));
44 strncpy(naddr.host, address, sizeof(naddr.host) - 1);
45 naddr.port = (uint16_t)luaL_checkinteger(L, 4);
46 naddr.family = sock->domain;
48 if (nixio__addr_write(&naddr, addr)) {
49 return nixio__perror_s(L);
55 const char *data = luaL_checklstring(L, 2, &len);
57 if (lua_gettop(L) > argoff) {
58 int offset = luaL_optint(L, argoff + 1, 0);
68 unsigned int wlen = luaL_optint(L, argoff + 2, len);
75 sent = sendto(sock->fd, data, len, 0, addr, alen);
76 } while(sent == -1 && errno == EINTR);
78 lua_pushinteger(L, sent);
81 return nixio__perror_s(L);
88 static int nixio_sock_send(lua_State *L) {
89 return nixio_sock__sendto(L, 0);
93 * sendto(data, address, port)
95 static int nixio_sock_sendto(lua_State *L) {
96 return nixio_sock__sendto(L, 1);
101 * recv() / recvfrom() helper
103 static int nixio_sock__recvfrom(lua_State *L, int from) {
104 nixio_sock *sock = nixio__checksock(L);
105 char buffer[NIXIO_BUFFERSIZE];
106 struct sockaddr_storage addrobj;
107 uint req = luaL_checkinteger(L, 2);
110 if (from && sock->domain != AF_INET && sock->domain != AF_INET6) {
111 return luaL_argerror(L, 1, "supported families: inet, inet6");
114 struct sockaddr *addr = (from) ? (struct sockaddr*)&addrobj : NULL;
115 socklen_t alen = (from) ? sizeof(addrobj) : 0;
117 /* We limit the readsize to NIXIO_BUFFERSIZE */
118 req = (req > NIXIO_BUFFERSIZE) ? NIXIO_BUFFERSIZE : req;
121 readc = recvfrom(sock->fd, buffer, req, 0, addr, &alen);
122 } while (readc == -1 && errno == EINTR);
126 int e = WSAGetLastError();
127 if (e == WSAECONNRESET || e == WSAECONNABORTED || e == WSAESHUTDOWN) {
134 return nixio__perror_s(L);
136 lua_pushlstring(L, buffer, readc);
142 if (!nixio__addr_parse(&naddr, (struct sockaddr *)&addrobj)) {
143 lua_pushstring(L, naddr.host);
144 lua_pushnumber(L, naddr.port);
156 static int nixio_sock_recv(lua_State *L) {
157 return nixio_sock__recvfrom(L, 0);
163 static int nixio_sock_recvfrom(lua_State *L) {
164 return nixio_sock__recvfrom(L, 1);
169 static const luaL_reg M[] = {
170 {"send", nixio_sock_send},
171 {"sendto", nixio_sock_sendto},
172 {"recv", nixio_sock_recv},
173 {"recvfrom",nixio_sock_recvfrom},
174 {"write", nixio_sock_send},
175 {"read", nixio_sock_recv},
179 void nixio_open_io(lua_State *L) {
180 lua_pushvalue(L, -2);
181 luaL_register(L, NULL, M);