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