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.
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <arpa/inet.h>
33 * send() / sendto() helper
35 static int nixio_sock__sendto(lua_State *L, int to) {
36 nixio_sock *sock = nixio__checksock(L);
37 struct sockaddr *addr = NULL;
41 const char *address = luaL_checklstring(L, 2, NULL);
42 uint16_t port = (uint16_t)luaL_checkinteger(L, 3);
43 struct sockaddr_storage addrstor;
44 addr = (struct sockaddr*)&addrstor;
45 if (sock->domain == AF_INET) {
46 struct sockaddr_in *inetaddr = (struct sockaddr_in *)addr;
47 if (inet_pton(sock->domain, address, &inetaddr->sin_addr) < 0) {
48 return luaL_argerror(L, 3, "invalid address");
50 inetaddr->sin_port = htons(port);
51 alen = sizeof(*inetaddr);
52 } else if (sock->domain == AF_INET6) {
53 struct sockaddr_in6 *inet6addr = (struct sockaddr_in6 *)addr;
54 if (inet_pton(sock->domain, address, &inet6addr->sin6_addr) < 0) {
55 return luaL_argerror(L, 3, "invalid address");
57 inet6addr->sin6_port = htons(port);
58 alen = sizeof(*inet6addr);
60 return luaL_argerror(L, 1, "supported families: inet, inet6");
66 const char *data = luaL_checklstring(L, 2, &len);
68 sent = sendto(sock->fd, data, len, 0, addr, alen);
69 } while(sent == -1 && errno == EINTR);
71 lua_pushinteger(L, sent);
74 return nixio__perror(L);
81 static int nixio_sock_send(lua_State *L) {
82 return nixio_sock__sendto(L, 0);
86 * sendto(data, address, port)
88 static int nixio_sock_sendto(lua_State *L) {
89 return nixio_sock__sendto(L, 1);
94 * recv() / recvfrom() helper
96 static int nixio_sock__recvfrom(lua_State *L, int from) {
97 nixio_sock *sock = nixio__checksock(L);
98 char buffer[NIXIO_BUFFERSIZE];
99 struct sockaddr_storage addrobj;
100 int req = luaL_checkinteger(L, 2);
103 if (from && sock->domain != AF_INET && sock->domain != AF_INET6) {
104 return luaL_argerror(L, 1, "supported families: inet, inet6");
107 struct sockaddr *addr = (from) ? (struct sockaddr*)&addrobj : NULL;
108 socklen_t alen = (from) ? sizeof(addrobj) : 0;
110 /* We limit the readsize to NIXIO_BUFFERSIZE */
111 req = (req > NIXIO_BUFFERSIZE) ? NIXIO_BUFFERSIZE : req;
114 readc = recvfrom(sock->fd, buffer, req, 0, addr, &alen);
115 } while (readc == -1 && errno == EINTR);
118 return nixio__perror(L);
120 lua_pushlstring(L, buffer, readc);
125 char ipaddr[INET6_ADDRSTRLEN];
129 if (addrobj.ss_family == AF_INET) {
130 struct sockaddr_in *inetaddr = (struct sockaddr_in*)addr;
131 port = inetaddr->sin_port;
132 binaddr = &inetaddr->sin_addr;
133 } else if (addrobj.ss_family == AF_INET6) {
134 struct sockaddr_in6 *inet6addr = (struct sockaddr_in6*)addr;
135 port = inet6addr->sin6_port;
136 binaddr = &inet6addr->sin6_addr;
138 return luaL_error(L, "unknown address family");
141 if (!inet_ntop(addrobj.ss_family, binaddr, ipaddr, sizeof(ipaddr))) {
142 return nixio__perror(L);
145 lua_pushstring(L, ipaddr);
146 lua_pushinteger(L, ntohs(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},
177 void nixio_open_io(lua_State *L) {
178 lua_pushvalue(L, -2);
179 luaL_register(L, NULL, M);