nixio + io = nix2io
[project/luci.git] / libs / nixio / src / io.c
1 /*
2  * nixio - Linux I/O library for lua
3  *
4  *   Copyright (C) 2009 Steven Barth <steven@midlink.org>
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 #include <lua.h>
20 #include <lualib.h>
21 #include <lauxlib.h>
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <arpa/inet.h>
28 #include "nixio.h"
29
30
31
32 /**
33  * send() / sendto() helper
34  */
35 static int nixio_sock__sendto(lua_State *L, int to) {
36         nixio_sock *sock = nixio__checksock(L);
37         struct sockaddr *addr = NULL;
38         socklen_t alen = 0;
39
40         if (to) {
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");
49                         }
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");
56                         }
57                         inet6addr->sin6_port = htons(port);
58                         alen = sizeof(*inet6addr);
59                 } else {
60                         return luaL_argerror(L, 1, "supported families: inet, inet6");
61                 }
62         }
63
64         size_t len;
65         ssize_t sent;
66         const char *data = luaL_checklstring(L, 2, &len);
67         do {
68                 sent = sendto(sock->fd, data, len, 0, addr, alen);
69         } while(sent == -1 && errno == EINTR);
70         if (len >= 0) {
71                 lua_pushinteger(L, sent);
72                 return 1;
73         } else {
74                 return nixio__perror(L);
75         }
76 }
77
78 /**
79  * send(data)
80  */
81 static int nixio_sock_send(lua_State *L) {
82         return nixio_sock__sendto(L, 0);
83 }
84
85 /**
86  * sendto(data, address, port)
87  */
88 static int nixio_sock_sendto(lua_State *L) {
89         return nixio_sock__sendto(L, 1);
90 }
91
92
93 /**
94  * recv() / recvfrom() helper
95  */
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);
101         int readc;
102
103         if (from && sock->domain != AF_INET && sock->domain != AF_INET6) {
104                 return luaL_argerror(L, 1, "supported families: inet, inet6");
105         }
106
107         struct sockaddr *addr = (from) ? (struct sockaddr*)&addrobj : NULL;
108         socklen_t alen = (from) ? sizeof(addrobj) : 0;
109
110         /* We limit the readsize to NIXIO_BUFFERSIZE */
111         req = (req > NIXIO_BUFFERSIZE) ? NIXIO_BUFFERSIZE : req;
112
113         do {
114                 readc = recvfrom(sock->fd, buffer, req, 0, addr, &alen);
115         } while (readc == -1 && errno == EINTR);
116
117         if (readc < 0) {
118                 return nixio__perror(L);
119         } else {
120                 lua_pushlstring(L, buffer, readc);
121
122                 if (!from) {
123                         return 1;
124                 } else {
125                         char ipaddr[INET6_ADDRSTRLEN];
126                         void *binaddr;
127                         uint16_t port;
128
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;
137                         } else {
138                                 return luaL_error(L, "unknown address family");
139                         }
140
141                         if (!inet_ntop(addrobj.ss_family, binaddr, ipaddr, sizeof(ipaddr))) {
142                                 return nixio__perror(L);
143                         }
144
145                         lua_pushstring(L, ipaddr);
146                         lua_pushinteger(L, ntohs(port));
147
148                         return 3;
149                 }
150         }
151 }
152
153 /**
154  * recv(count)
155  */
156 static int nixio_sock_recv(lua_State *L) {
157         return nixio_sock__recvfrom(L, 0);
158 }
159
160 /**
161  * recvfrom(count)
162  */
163 static int nixio_sock_recvfrom(lua_State *L) {
164         return nixio_sock__recvfrom(L, 1);
165 }
166
167
168 /* module table */
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         {NULL,                  NULL}
175 };
176
177 void nixio_open_io(lua_State *L) {
178         lua_pushvalue(L, -2);
179         luaL_register(L, NULL, M);
180         lua_pop(L, 1);
181 }