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