luci-lib-nixio: pass exact sockaddr length to getnameinfo()
[project/luci.git] / libs / luci-lib-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 "nixio.h"
20 #include <errno.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <sys/types.h>
25
26
27 /**
28  * send() / sendto() helper
29  */
30 static int nixio_sock__sendto(lua_State *L, int to) {
31         nixio_sock *sock = nixio__checksock(L);
32         struct sockaddr_storage addr_in;
33 #ifndef __WINNT__
34         struct sockaddr_un addr_un;
35 #endif
36         struct sockaddr *addr = NULL;
37         socklen_t alen = 0;
38         int argoff = 2;
39
40         if (to) {
41                 argoff += 2;
42                 if (sock->domain == AF_INET || sock->domain == AF_INET6) {
43                         const char *address = luaL_checkstring(L, 3);
44                         addr = (struct sockaddr*)&addr_in;
45                         alen = sizeof(addr_in);
46
47                         nixio_addr naddr;
48                         memset(&naddr, 0, sizeof(naddr));
49                         strncpy(naddr.host, address, sizeof(naddr.host) - 1);
50                         naddr.port = (uint16_t)luaL_checkinteger(L, 4);
51                         naddr.family = sock->domain;
52
53                         if (nixio__addr_write(&naddr, addr)) {
54                                 return nixio__perror_s(L);
55                         }
56                 }
57
58 #ifndef __WINNT__
59                 else if (sock->domain == AF_UNIX) {
60                         size_t pathlen;
61                         const char *path = luaL_checklstring(L, 3, &pathlen);
62
63                         addr_un.sun_family = AF_UNIX;
64                         luaL_argcheck(L, pathlen <= sizeof(addr_un.sun_path), 3, "out of range");
65                         memcpy(addr_un.sun_path, path, pathlen);
66
67                         addr = (struct sockaddr*)&addr_un;
68                         alen = sizeof(sa_family_t) + pathlen;
69                 }
70 #endif
71         }
72
73         size_t len;
74         ssize_t sent;
75         const char *data = luaL_checklstring(L, 2, &len);
76
77         if (lua_gettop(L) > argoff) {
78                 int offset = luaL_optint(L, argoff + 1, 0);
79                 if (offset) {
80                         if (offset < len) {
81                                 data += offset;
82                                 len -= offset;
83                         } else {
84                                 len = 0;
85                         }
86                 }
87
88                 unsigned int wlen = luaL_optint(L, argoff + 2, len);
89                 if (wlen < len) {
90                         len = wlen;
91                 }
92         }
93
94         do {
95                 sent = sendto(sock->fd, data, len, 0, addr, alen);
96         } while(sent == -1 && errno == EINTR);
97         if (sent >= 0) {
98                 lua_pushinteger(L, sent);
99                 return 1;
100         } else {
101                 return nixio__perror_s(L);
102         }
103 }
104
105 /**
106  * send(data)
107  */
108 static int nixio_sock_send(lua_State *L) {
109         return nixio_sock__sendto(L, 0);
110 }
111
112 /**
113  * sendto(data, address, port)
114  */
115 static int nixio_sock_sendto(lua_State *L) {
116         return nixio_sock__sendto(L, 1);
117 }
118
119
120 /**
121  * recv() / recvfrom() helper
122  */
123 static int nixio_sock__recvfrom(lua_State *L, int from) {
124         nixio_sock *sock = nixio__checksock(L);
125         char buffer[NIXIO_BUFFERSIZE];
126         struct sockaddr_storage addr_in;
127 #ifndef __WINNT__
128         struct sockaddr_un addr_un;
129 #endif
130         struct sockaddr *addr = NULL;
131         socklen_t alen = 0;
132         uint req = luaL_checkinteger(L, 2);
133         int readc;
134
135         if (sock->domain == AF_INET || sock->domain == AF_INET6) {
136                 addr = (from) ? (struct sockaddr*)&addr_in : NULL;
137                 alen = (from) ? sizeof(addr_in) : 0;
138         }
139 #ifndef __WINNT__
140         else if (sock->domain == AF_UNIX) {
141                 addr = (from) ? (struct sockaddr*)&addr_un : NULL;
142                 alen = (from) ? sizeof(addr_un) : 0;
143         }
144 #endif
145
146         /* We limit the readsize to NIXIO_BUFFERSIZE */
147         req = (req > NIXIO_BUFFERSIZE) ? NIXIO_BUFFERSIZE : req;
148
149         do {
150                 readc = recvfrom(sock->fd, buffer, req, 0, addr, &alen);
151         } while (readc == -1 && errno == EINTR);
152
153 #ifdef __WINNT__
154         if (readc < 0) {
155                 int e = WSAGetLastError();
156                 if (e == WSAECONNRESET || e == WSAECONNABORTED || e == WSAESHUTDOWN) {
157                         readc = 0;
158                 }
159         }
160 #endif
161
162         if (readc < 0) {
163                 return nixio__perror_s(L);
164         } else {
165                 lua_pushlstring(L, buffer, readc);
166
167                 if (!from) {
168                         return 1;
169                 }
170                 /* push address. */
171                 if (sock->domain == AF_INET || sock->domain == AF_INET6) {
172                         nixio_addr naddr;
173                         if (!nixio__addr_parse(&naddr, (struct sockaddr *)&addr_in)) {
174                                 lua_pushstring(L, naddr.host);
175                                 lua_pushinteger(L, naddr.port);
176                                 return 3;
177                         } else {
178                                 return 1;
179                         }
180                 }
181 #ifndef __WINNT__
182                 else if (sock->domain == AF_UNIX && alen > sizeof(sa_family_t)) {
183                         /* if first char is non-null then the path is not in the
184                            abstract namespace and alen includes the trailing null */
185                         if (addr_un.sun_path[0])
186                                 --alen;
187                         lua_pushlstring(L, addr_un.sun_path, alen - sizeof(sa_family_t));
188                         return 2;
189                 }
190 #endif
191         }
192         return 1;
193 }
194
195 /**
196  * recv(count)
197  */
198 static int nixio_sock_recv(lua_State *L) {
199         return nixio_sock__recvfrom(L, 0);
200 }
201
202 /**
203  * recvfrom(count)
204  */
205 static int nixio_sock_recvfrom(lua_State *L) {
206         return nixio_sock__recvfrom(L, 1);
207 }
208
209
210 /* module table */
211 static const luaL_reg M[] = {
212         {"send",        nixio_sock_send},
213         {"sendto",      nixio_sock_sendto},
214         {"recv",        nixio_sock_recv},
215         {"recvfrom",nixio_sock_recvfrom},
216         {"write",       nixio_sock_send},
217         {"read",        nixio_sock_recv},
218         {NULL,                  NULL}
219 };
220
221 void nixio_open_io(lua_State *L) {
222         lua_pushvalue(L, -2);
223         luaL_register(L, NULL, M);
224         lua_pop(L, 1);
225 }