f8fa3e649a58a9a85c40fa7d04c26bb39d6b6ae7
[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
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(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         sock->fd = -1;
92         return nixio__pstatus(L, !close(sockfd));
93 }
94
95 /**
96  * garbage collector
97  */
98 static int nixio_sock__gc(lua_State *L) {
99         nixio_sock *sock = (nixio_sock*)luaL_checkudata(L, 1, NIXIO_META);
100         if (sock && sock->fd != -1) {
101                 close(sock->fd);
102         }
103         return 0;
104 }
105
106 /**
107  * string representation
108  */
109 static int nixio_sock__tostring(lua_State *L) {
110         lua_pushfstring(L, "nixio socket %d", nixio__checksockfd(L));
111         return 1;
112 }
113
114 /**
115  * shutdown a socket
116  */
117 static int nixio_sock_shutdown(lua_State *L) {
118         int sockfd = nixio__checksockfd(L);
119         const char *what = luaL_optlstring(L, 2, "rdwr", NULL);
120         int how;
121
122         if (!strcmp(what, "rdwr") || !strcmp(what, "both")) {
123                 how = SHUT_RDWR;
124         } else if (!strcmp(what, "rd") || !strcmp(what, "read")) {
125                 how = SHUT_RD;
126         } else if (!strcmp(what, "wr") || !strcmp(what, "write")) {
127                 how = SHUT_WR;
128         } else {
129                 return luaL_argerror(L, 2, "supported values: both, read, write");
130         }
131
132         return nixio__pstatus(L, !shutdown(sockfd, how));
133 }
134
135 /* module table */
136 static const luaL_reg R[] = {
137         {"socket",              nixio_socket},
138         {NULL,                  NULL}
139 };
140
141 /* object table */
142 static const luaL_reg M[] = {
143         {"close",               nixio_sock_close},
144         {"shutdown",    nixio_sock_shutdown},
145         {NULL,                  NULL}
146 };
147
148 void nixio_open_socket(lua_State *L) {
149         luaL_getmetatable(L, NIXIO_META);
150         lua_pushcfunction(L, nixio_sock__gc);
151         lua_setfield(L, -2, "__gc");
152         lua_pushcfunction(L, nixio_sock__tostring);
153         lua_setfield(L, -2, "__tostring");
154         lua_pop(L, 1);
155
156         luaL_register(L, NULL, R);
157
158         lua_pushvalue(L, -2);
159         luaL_register(L, NULL, M);
160         lua_pop(L, 1);
161 }