Make nixio compile on OpenSolaris
[project/luci.git] / libs / nixio / src / sockopt.c
index f1a326c..6ec8753 100644 (file)
  *  limitations under the License.
  */
 
-#include <lua.h>
-#include <lualib.h>
-#include <lauxlib.h>
+#include "nixio.h"
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <netinet/tcp.h>
 #include <sys/time.h>
 #include <string.h>
 #include <fcntl.h>
+#include <errno.h>
 #include "nixio.h"
 
+
 /**
  * setblocking()
  */
 static int nixio_sock_setblocking(lua_State *L) {
        int fd = nixio__checkfd(L, 1);
+       luaL_checkany(L, 2);
        int set = lua_toboolean(L, 2);
        int flags = fcntl(fd, F_GETFL);
 
@@ -39,7 +40,7 @@ static int nixio_sock_setblocking(lua_State *L) {
                return nixio__perror(L);
        }
 
-       if (set) {
+       if (!set) {
                flags |= O_NONBLOCK;
        } else {
                flags &= ~O_NONBLOCK;
@@ -124,7 +125,11 @@ static int nixio__getsetsockopt(lua_State *L, int set) {
                } else if (!strcmp(option, "sndbuf")) {
                        return nixio__gso_int(L, sock->fd, SOL_SOCKET, SO_SNDBUF, set);
                } else if (!strcmp(option, "priority")) {
+#ifdef SO_PRIORITY
                        return nixio__gso_int(L, sock->fd, SOL_SOCKET, SO_PRIORITY, set);
+#else
+                       return nixio__pstatus(L, !(errno = ENOPROTOOPT));
+#endif
                } else if (!strcmp(option, "broadcast")) {
                        return nixio__gso_int(L, sock->fd, SOL_SOCKET, SO_BROADCAST, set);
                } else if (!strcmp(option, "linger")) {
@@ -143,9 +148,9 @@ static int nixio__getsetsockopt(lua_State *L, int set) {
                        return luaL_error(L, "not a TCP socket");
                }
                if (!strcmp(option, "cork")) {
-                       return nixio__gso_int(L, sock->fd, SOL_TCP, TCP_CORK, set);
+                       return nixio__gso_int(L, sock->fd, IPPROTO_TCP, TCP_CORK, set);
                } else if (!strcmp(option, "nodelay")) {
-                       return nixio__gso_int(L, sock->fd, SOL_TCP, TCP_NODELAY, set);
+                       return nixio__gso_int(L, sock->fd, IPPROTO_TCP, TCP_NODELAY, set);
                } else {
                        return luaL_argerror(L, 3, "supported values: cork, nodelay");
                }
@@ -181,7 +186,7 @@ void nixio_open_sockopt(lua_State *L) {
        luaL_register(L, NULL, M);
        lua_pop(L, 1);
 
-       luaL_getmetatable(L, LUA_FILEHANDLE);
+       luaL_getmetatable(L, NIXIO_FILE_META);
        lua_pushcfunction(L, nixio_sock_setblocking);
        lua_setfield(L, -2, "setblocking");
        lua_pop(L, 1);