Make nixio compile on OpenSolaris
[project/luci.git] / libs / nixio / src / tls-socket.c
index b26d140..693a2a5 100644 (file)
@@ -22,9 +22,8 @@
 
 static int nixio__tls_sock_perror(lua_State *L, SSL *sock, int code) {
        lua_pushnil(L);
-       lua_pushinteger(L, code);
        lua_pushinteger(L, SSL_get_error(sock, code));
-       return 3;
+       return 2;
 }
 
 static int nixio__tls_sock_pstatus(lua_State *L, SSL *sock, int code) {
@@ -37,6 +36,10 @@ static int nixio__tls_sock_pstatus(lua_State *L, SSL *sock, int code) {
 }
 
 static SSL* nixio__checktlssock(lua_State *L) {
+       if (lua_istable(L, 1)) {
+               lua_getfield(L, 1, "connection");
+               lua_replace(L, 1);
+       }
        nixio_tls_sock *sock = luaL_checkudata(L, 1, NIXIO_TLS_SOCK_META);
        luaL_argcheck(L, sock->socket, 1, "invalid context");
        return sock->socket;
@@ -79,11 +82,11 @@ static int nixio_tls_sock_recv(lua_State *L) {
                t->pbufsiz -= req;
                return 1;
        } else {
-               char *axbuf;
-               int axread;
+               uint8_t *axbuf;
+               size_t axread;
 
                /* while handshake pending */
-               while ((axread = ssl_read(sock, (uint8_t**)&axbuf)) == SSL_OK);
+               while ((axread = ssl_read(sock, &axbuf)) == SSL_OK);
 
                if (t->pbufsiz) {
                        lua_pushlstring(L, t->pbufpos, t->pbufsiz);
@@ -93,20 +96,22 @@ static int nixio_tls_sock_recv(lua_State *L) {
                        /* There is an error */
                        free(t->pbuffer);
                        t->pbuffer = t->pbufpos = NULL;
-                       t->pbufsiz = 0;
 
                        if (axread != SSL_ERROR_CONN_LOST) {
+                               t->pbufsiz = 0;
                                return nixio__tls_sock_perror(L, sock, axread);
                        } else {
                                if (!t->pbufsiz) {
                                        lua_pushliteral(L, "");
+                               } else {
+                                       t->pbufsiz = 0;
                                }
                        }
                } else {
                        int stillwant = req - t->pbufsiz;
                        if (stillwant < axread) {
                                /* we got more data than we need */
-                               lua_pushlstring(L, axbuf, stillwant);
+                               lua_pushlstring(L, (char *)axbuf, stillwant);
                                if(t->pbufsiz) {
                                        lua_concat(L, 2);
                                }
@@ -125,7 +130,7 @@ static int nixio_tls_sock_recv(lua_State *L) {
                                t->pbufpos = t->pbuffer;
                                memcpy(t->pbufpos, axbuf + stillwant, t->pbufsiz);
                        } else {
-                               lua_pushlstring(L, axbuf, axread);
+                               lua_pushlstring(L, (char *)axbuf, axread);
                                if(t->pbufsiz) {
                                        lua_concat(L, 2);
                                }
@@ -186,7 +191,7 @@ static int nixio_tls_sock__gc(lua_State *L) {
 
 static int nixio_tls_sock__tostring(lua_State *L) {
        SSL *sock = nixio__checktlssock(L);
-       lua_pushfstring(L, "nixio TLS socket: %p", sock);
+       lua_pushfstring(L, "nixio TLS connection: %p", sock);
        return 1;
 }
 
@@ -195,6 +200,8 @@ static int nixio_tls_sock__tostring(lua_State *L) {
 static const luaL_reg M[] = {
        {"recv",                nixio_tls_sock_recv},
        {"send",                nixio_tls_sock_send},
+       {"read",                nixio_tls_sock_recv},
+       {"write",               nixio_tls_sock_send},
        {"accept",              nixio_tls_sock_accept},
        {"connect",     nixio_tls_sock_connect},
        {"shutdown",    nixio_tls_sock_shutdown},
@@ -210,5 +217,5 @@ void nixio_open_tls_socket(lua_State *L) {
        luaL_register(L, NULL, M);
        lua_pushvalue(L, -1);
        lua_setfield(L, -2, "__index");
-       lua_setfield(L, -2, "tls_socket_meta");
+       lua_setfield(L, -2, "meta_tls_socket");
 }