acfa9aeb22b99ba71d2284c58a3d3623934ebf08
[project/luci.git] / libs / nixio / src / 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 <lua.h>
20 #include <lualib.h>
21 #include <lauxlib.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <errno.h>
27 #include "nixio.h"
28
29
30 /**
31  * create new socket
32  */
33 static int nixio_socket(lua_State *L) {
34         const char *domain = luaL_optlstring(L, 1, "", NULL);
35         const char *type   = luaL_optlstring(L, 2, "", NULL);
36         const char *proto  = lua_tolstring(L, 3, NULL);
37
38         nixio_sock *sock = lua_newuserdata(L, sizeof(nixio_sock));
39
40         if (!strcmp(domain, "inet")) {
41                 sock->domain = AF_INET;
42         } else if (!strcmp(domain, "inet6")) {
43                 sock->domain = AF_INET6;
44         } else if (!strcmp(domain, "unix")) {
45                 sock->domain = AF_UNIX;
46         } else if (!strcmp(domain, "packet")) {
47                 sock->domain = AF_PACKET;
48         } else {
49                 return luaL_argerror(L, 1,
50                  "supported values: inet, inet6, unix, packet"
51                 );
52         }
53
54         if (!strcmp(type, "stream")) {
55                 sock->type = SOCK_STREAM;
56         } else if (!strcmp(type, "dgram")) {
57                 sock->type = SOCK_DGRAM;
58         } else if (!strcmp(type, "raw")) {
59                 sock->type = SOCK_RAW;
60         } else {
61                 return luaL_argerror(L, 2, "supported values: stream, dgram, raw");
62         }
63
64         if (!proto) {
65                 sock->protocol = 0;
66         } else if (!strcmp(proto, "icmp")) {
67                 sock->protocol = IPPROTO_ICMP;
68         } else if (!strcmp(proto, "icmpv6")) {
69                 sock->protocol = IPPROTO_ICMPV6;
70         } else {
71                 return luaL_argerror(L, 3, "supported values: [empty], icmp, icmpv6");
72         }
73
74         /* create userdata */
75         luaL_getmetatable(L, NIXIO_META);
76         lua_setmetatable(L, -2);
77
78         sock->fd = socket(sock->domain, sock->type, sock->protocol);
79
80         if (sock->fd < 0) {
81                 return nixio__perror(L);
82         }
83
84         return 1;
85 }
86
87 /**
88  * close a socket
89  */
90 static int nixio_sock_close(lua_State *L) {
91         nixio_sock *sock = nixio__checksock(L);
92         int sockfd = sock->fd;
93         sock->fd = -1;
94         return nixio__pstatus(L, !close(sockfd));
95 }
96
97 /**
98  * garbage collector
99  */
100 static int nixio_sock__gc(lua_State *L) {
101         nixio_sock *sock = (nixio_sock*)luaL_checkudata(L, 1, NIXIO_META);
102         if (sock && sock->fd != -1) {
103                 close(sock->fd);
104         }
105         return 0;
106 }
107
108 /**
109  * string representation
110  */
111 static int nixio_sock__tostring(lua_State *L) {
112         lua_pushfstring(L, "nixio socket %d", nixio__checksockfd(L));
113         return 1;
114 }
115
116 /**
117  * shutdown a socket
118  */
119 static int nixio_sock_shutdown(lua_State *L) {
120         int sockfd = nixio__checksockfd(L);
121         const char *what = luaL_optlstring(L, 2, "rdwr", NULL);
122         int how;
123
124         if (!strcmp(what, "rdwr") || !strcmp(what, "both")) {
125                 how = SHUT_RDWR;
126         } else if (!strcmp(what, "rd") || !strcmp(what, "read")) {
127                 how = SHUT_RD;
128         } else if (!strcmp(what, "wr") || !strcmp(what, "write")) {
129                 how = SHUT_WR;
130         } else {
131                 return luaL_argerror(L, 2, "supported values: both, read, write");
132         }
133
134         return nixio__pstatus(L, !shutdown(sockfd, how));
135 }
136
137 /* module table */
138 static const luaL_reg R[] = {
139         {"socket",              nixio_socket},
140         {NULL,                  NULL}
141 };
142
143 /* object table */
144 static const luaL_reg M[] = {
145         {"close",               nixio_sock_close},
146         {"shutdown",    nixio_sock_shutdown},
147         {NULL,                  NULL}
148 };
149
150 void nixio_open_socket(lua_State *L) {
151         luaL_getmetatable(L, NIXIO_META);
152         lua_pushcfunction(L, nixio_sock__gc);
153         lua_setfield(L, -2, "__gc");
154         lua_pushcfunction(L, nixio_sock__tostring);
155         lua_setfield(L, -2, "__tostring");
156         lua_pop(L, 1);
157
158         luaL_register(L, NULL, R);
159
160         lua_pushvalue(L, -2);
161         luaL_register(L, NULL, M);
162         lua_pop(L, 1);
163 }