b80e2e3532b3db229dd90a4b6549066c73070837
[project/luci.git] / libs / nixio / src / sockopt.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 <sys/socket.h>
21 #include <netinet/in.h>
22 #include <netinet/tcp.h>
23 #include <sys/time.h>
24 #include <string.h>
25 #include <fcntl.h>
26 #include "nixio.h"
27
28
29 /**
30  * setblocking()
31  */
32 static int nixio_sock_setblocking(lua_State *L) {
33         int fd = nixio__checkfd(L, 1);
34         luaL_checkany(L, 2);
35         int set = lua_toboolean(L, 2);
36         int flags = fcntl(fd, F_GETFL);
37
38         if (flags == -1) {
39                 return nixio__perror(L);
40         }
41
42         if (!set) {
43                 flags |= O_NONBLOCK;
44         } else {
45                 flags &= ~O_NONBLOCK;
46         }
47
48         return nixio__pstatus(L, !fcntl(fd, F_SETFL, flags));
49 }
50
51 static int nixio__gso_int(lua_State *L, int fd, int level, int opt, int set) {
52         int value;
53         socklen_t optlen = sizeof(value);
54         if (!set) {
55                 if (!getsockopt(fd, level, opt, &value, &optlen)) {
56                         lua_pushinteger(L, value);
57                         return 1;
58                 }
59         } else {
60                 value = luaL_checkinteger(L, set);
61                 if (!setsockopt(fd, level, opt, &value, optlen)) {
62                         lua_pushboolean(L, 1);
63                         return 1;
64                 }
65         }
66         return nixio__perror(L);
67 }
68
69 static int nixio__gso_ling(lua_State *L, int fd, int level, int opt, int set) {
70         struct linger value;
71         socklen_t optlen = sizeof(value);
72         if (!set) {
73                 if (!getsockopt(fd, level, opt, &value, &optlen)) {
74                         lua_pushinteger(L, value.l_onoff ? value.l_linger : 0);
75                         return 1;
76                 }
77         } else {
78                 value.l_linger = luaL_checkinteger(L, set);
79                 value.l_onoff = value.l_linger ? 1 : 0;
80                 if (!setsockopt(fd, level, opt, &value, optlen)) {
81                         lua_pushboolean(L, 1);
82                         return 1;
83                 }
84         }
85         return nixio__perror(L);
86 }
87
88 static int nixio__gso_timev(lua_State *L, int fd, int level, int opt, int set) {
89         struct timeval value;
90         socklen_t optlen = sizeof(value);
91         if (!set) {
92                 if (!getsockopt(fd, level, opt, &value, &optlen)) {
93                         lua_pushinteger(L, value.tv_sec);
94                         lua_pushinteger(L, value.tv_usec);
95                         return 2;
96                 }
97         } else {
98                 value.tv_sec  = luaL_checkinteger(L, set);
99                 value.tv_usec = luaL_optinteger(L, set + 1, 0);
100                 if (!setsockopt(fd, level, opt, &value, optlen)) {
101                         lua_pushboolean(L, 1);
102                         return 1;
103                 }
104         }
105         return nixio__perror(L);
106 }
107
108 /**
109  * get/setsockopt() helper
110  */
111 static int nixio__getsetsockopt(lua_State *L, int set) {
112         nixio_sock *sock = nixio__checksock(L);
113         const char *level = luaL_optlstring(L, 2, "", NULL);
114         const char *option = luaL_optlstring(L, 3, "", NULL);
115         set = (set) ? 4 : 0;
116
117         if (!strcmp(level, "socket")) {
118                 if (!strcmp(option, "keepalive")) {
119                         return nixio__gso_int(L, sock->fd, SOL_SOCKET, SO_KEEPALIVE, set);
120                 } else if (!strcmp(option, "reuseaddr")) {
121                         return nixio__gso_int(L, sock->fd, SOL_SOCKET, SO_REUSEADDR, set);
122                 } else if (!strcmp(option, "rcvbuf")) {
123                         return nixio__gso_int(L, sock->fd, SOL_SOCKET, SO_RCVBUF, set);
124                 } else if (!strcmp(option, "sndbuf")) {
125                         return nixio__gso_int(L, sock->fd, SOL_SOCKET, SO_SNDBUF, set);
126                 } else if (!strcmp(option, "priority")) {
127                         return nixio__gso_int(L, sock->fd, SOL_SOCKET, SO_PRIORITY, set);
128                 } else if (!strcmp(option, "broadcast")) {
129                         return nixio__gso_int(L, sock->fd, SOL_SOCKET, SO_BROADCAST, set);
130                 } else if (!strcmp(option, "linger")) {
131                         return nixio__gso_ling(L, sock->fd, SOL_SOCKET, SO_LINGER, set);
132                 } else if (!strcmp(option, "sndtimeo")) {
133                         return nixio__gso_timev(L, sock->fd, SOL_SOCKET, SO_SNDTIMEO, set);
134                 } else if (!strcmp(option, "rcvtimeo")) {
135                         return nixio__gso_timev(L, sock->fd, SOL_SOCKET, SO_RCVTIMEO, set);
136                 } else {
137                         return luaL_argerror(L, 3, "supported values: keepalive, reuseaddr,"
138                          " sndbuf, rcvbuf, priority, broadcast, linger, sndtimeo, rcvtimeo"
139                         );
140                 }
141         } else if (!strcmp(level, "tcp")) {
142                 if (sock->type != SOCK_STREAM) {
143                         return luaL_error(L, "not a TCP socket");
144                 }
145                 if (!strcmp(option, "cork")) {
146                         return nixio__gso_int(L, sock->fd, IPPROTO_TCP, TCP_CORK, set);
147                 } else if (!strcmp(option, "nodelay")) {
148                         return nixio__gso_int(L, sock->fd, IPPROTO_TCP, TCP_NODELAY, set);
149                 } else {
150                         return luaL_argerror(L, 3, "supported values: cork, nodelay");
151                 }
152         } else {
153                 return luaL_argerror(L, 2, "supported values: socket, tcp");
154         }
155 }
156
157 /**
158  * getsockopt()
159  */
160 static int nixio_sock_getsockopt(lua_State *L) {
161         return nixio__getsetsockopt(L, 0);
162 }
163
164 /**
165  * setsockopt()
166  */
167 static int nixio_sock_setsockopt(lua_State *L) {
168         return nixio__getsetsockopt(L, 1);
169 }
170
171 /* module table */
172 static const luaL_reg M[] = {
173         {"setblocking", nixio_sock_setblocking},
174         {"getsockopt",  nixio_sock_getsockopt},
175         {"setsockopt",  nixio_sock_setsockopt},
176         {NULL,                  NULL}
177 };
178
179 void nixio_open_sockopt(lua_State *L) {
180         lua_pushvalue(L, -2);
181         luaL_register(L, NULL, M);
182         lua_pop(L, 1);
183
184         luaL_getmetatable(L, NIXIO_FILE_META);
185         lua_pushcfunction(L, nixio_sock_setblocking);
186         lua_setfield(L, -2, "setblocking");
187         lua_pop(L, 1);
188 }