nixio: Fixes, use POSIX calls for file i/o
[project/luci.git] / libs / nixio / src / tls-socket.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-tls.h"
20 #include <string.h>
21 #include <stdlib.h>
22
23 static int nixio__tls_sock_perror(lua_State *L, SSL *sock, int code) {
24         lua_pushnil(L);
25         lua_pushinteger(L, SSL_get_error(sock, code));
26         return 2;
27 }
28
29 static int nixio__tls_sock_pstatus(lua_State *L, SSL *sock, int code) {
30         if (code == 1) {
31                 lua_pushboolean(L, 1);
32                 return 1;
33         } else {
34                 return nixio__tls_sock_perror(L, sock, code);
35         }
36 }
37
38 static SSL* nixio__checktlssock(lua_State *L) {
39         if (lua_istable(L, 1)) {
40                 lua_getfield(L, 1, "connection");
41                 lua_replace(L, 1);
42         }
43         nixio_tls_sock *sock = luaL_checkudata(L, 1, NIXIO_TLS_SOCK_META);
44         luaL_argcheck(L, sock->socket, 1, "invalid context");
45         return sock->socket;
46 }
47
48 static int nixio_tls_sock_recv(lua_State *L) {
49         SSL *sock = nixio__checktlssock(L);
50         int req = luaL_checkinteger(L, 2);
51
52         luaL_argcheck(L, req >= 0, 2, "out of range");
53
54         /* We limit the readsize to NIXIO_BUFFERSIZE */
55         req = (req > NIXIO_BUFFERSIZE) ? NIXIO_BUFFERSIZE : req;
56
57 #ifndef WITH_AXTLS
58
59         char buffer[NIXIO_BUFFERSIZE];
60         int readc = SSL_read(sock, buffer, req);
61
62         if (readc < 0) {
63                 return nixio__tls_sock_pstatus(L, sock, readc);
64         } else {
65                 lua_pushlstring(L, buffer, readc);
66                 return 1;
67         }
68
69 #else
70
71         if (!req) {
72                 lua_pushliteral(L, "");
73                 return 1;
74         }
75
76         nixio_tls_sock *t = lua_touserdata(L, 1);
77
78         /* AXTLS doesn't handle buffering for us, so we have to hack around*/
79         if (req < t->pbufsiz) {
80                 lua_pushlstring(L, t->pbufpos, req);
81                 t->pbufpos += req;
82                 t->pbufsiz -= req;
83                 return 1;
84         } else {
85                 char *axbuf;
86                 int axread;
87
88                 /* while handshake pending */
89                 while ((axread = ssl_read(sock, (uint8_t**)&axbuf)) == SSL_OK);
90
91                 if (t->pbufsiz) {
92                         lua_pushlstring(L, t->pbufpos, t->pbufsiz);
93                 }
94
95                 if (axread < 0) {
96                         /* There is an error */
97                         free(t->pbuffer);
98                         t->pbuffer = t->pbufpos = NULL;
99
100                         if (axread != SSL_ERROR_CONN_LOST) {
101                                 t->pbufsiz = 0;
102                                 return nixio__tls_sock_perror(L, sock, axread);
103                         } else {
104                                 if (!t->pbufsiz) {
105                                         lua_pushliteral(L, "");
106                                 } else {
107                                         t->pbufsiz = 0;
108                                 }
109                         }
110                 } else {
111                         int stillwant = req - t->pbufsiz;
112                         if (stillwant < axread) {
113                                 /* we got more data than we need */
114                                 lua_pushlstring(L, axbuf, stillwant);
115                                 if(t->pbufsiz) {
116                                         lua_concat(L, 2);
117                                 }
118
119                                 /* remaining data goes into the buffer */
120                                 t->pbufpos = t->pbuffer;
121                                 t->pbufsiz = axread - stillwant;
122                                 t->pbuffer = realloc(t->pbuffer, t->pbufsiz);
123                                 if (!t->pbuffer) {
124                                         free(t->pbufpos);
125                                         t->pbufpos = NULL;
126                                         t->pbufsiz = 0;
127                                         return luaL_error(L, "out of memory");
128                                 }
129
130                                 t->pbufpos = t->pbuffer;
131                                 memcpy(t->pbufpos, axbuf + stillwant, t->pbufsiz);
132                         } else {
133                                 lua_pushlstring(L, axbuf, axread);
134                                 if(t->pbufsiz) {
135                                         lua_concat(L, 2);
136                                 }
137
138                                 /* free buffer */
139                                 free(t->pbuffer);
140                                 t->pbuffer = t->pbufpos = NULL;
141                                 t->pbufsiz = 0;
142                         }
143                 }
144                 return 1;
145         }
146
147 #endif /* WITH_AXTLS */
148
149 }
150
151 static int nixio_tls_sock_send(lua_State *L) {
152         SSL *sock = nixio__checktlssock(L);
153         size_t len;
154         ssize_t sent;
155         const char *data = luaL_checklstring(L, 2, &len);
156         sent = SSL_write(sock, data, len);
157         if (sent > 0) {
158                 lua_pushinteger(L, sent);
159                 return 1;
160         } else {
161                 return nixio__tls_sock_pstatus(L, sock, len);
162         }
163 }
164
165 static int nixio_tls_sock_accept(lua_State *L) {
166         SSL *sock = nixio__checktlssock(L);
167         return nixio__tls_sock_pstatus(L, sock, SSL_accept(sock));
168 }
169
170 static int nixio_tls_sock_connect(lua_State *L) {
171         SSL *sock = nixio__checktlssock(L);
172         return nixio__tls_sock_pstatus(L, sock, SSL_connect(sock));
173 }
174
175 static int nixio_tls_sock_shutdown(lua_State *L) {
176         SSL *sock = nixio__checktlssock(L);
177         return nixio__tls_sock_pstatus(L, sock, SSL_shutdown(sock));
178 }
179
180 static int nixio_tls_sock__gc(lua_State *L) {
181         nixio_tls_sock *sock = luaL_checkudata(L, 1, NIXIO_TLS_SOCK_META);
182         if (sock->socket) {
183                 SSL_free(sock->socket);
184                 sock->socket = NULL;
185 #ifdef WITH_AXTLS
186                 free(sock->pbuffer);
187 #endif
188         }
189         return 0;
190 }
191
192 static int nixio_tls_sock__tostring(lua_State *L) {
193         SSL *sock = nixio__checktlssock(L);
194         lua_pushfstring(L, "nixio TLS connection: %p", sock);
195         return 1;
196 }
197
198
199 /* ctx function table */
200 static const luaL_reg M[] = {
201         {"recv",                nixio_tls_sock_recv},
202         {"send",                nixio_tls_sock_send},
203         {"read",                nixio_tls_sock_recv},
204         {"write",               nixio_tls_sock_send},
205         {"accept",              nixio_tls_sock_accept},
206         {"connect",     nixio_tls_sock_connect},
207         {"shutdown",    nixio_tls_sock_shutdown},
208         {"__gc",                nixio_tls_sock__gc},
209         {"__tostring",  nixio_tls_sock__tostring},
210         {NULL,                  NULL}
211 };
212
213
214 void nixio_open_tls_socket(lua_State *L) {
215         /* create socket metatable */
216         luaL_newmetatable(L, NIXIO_TLS_SOCK_META);
217         luaL_register(L, NULL, M);
218         lua_pushvalue(L, -1);
219         lua_setfield(L, -2, "__index");
220         lua_setfield(L, -2, "meta_tls_socket");
221 }