2 * nixio - Linux I/O library for lua
4 * Copyright (C) 2009 Steven Barth <steven@midlink.org>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <sys/socket.h>
21 #include <netinet/in.h>
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);
36 nixio_sock *sock = lua_newuserdata(L, sizeof(nixio_sock));
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;*/
47 return luaL_argerror(L, 1,
48 "supported values: inet, inet6, unix, packet"
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;
59 return luaL_argerror(L, 2, "supported values: stream, dgram, raw");
64 } else if (!strcmp(proto, "icmp")) {
65 sock->protocol = IPPROTO_ICMP;
66 } else if (!strcmp(proto, "icmpv6")) {
67 sock->protocol = IPPROTO_ICMPV6;
69 return luaL_argerror(L, 3, "supported values: [empty], icmp, icmpv6");
73 luaL_getmetatable(L, NIXIO_META);
74 lua_setmetatable(L, -2);
76 sock->fd = socket(sock->domain, sock->type, sock->protocol);
79 return nixio__perror(L);
88 static int nixio_sock_close(lua_State *L) {
89 nixio_sock *sock = nixio__checksock(L);
90 int sockfd = sock->fd;
92 return nixio__pstatus(L, !close(sockfd));
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) {
107 * string representation
109 static int nixio_sock__tostring(lua_State *L) {
110 lua_pushfstring(L, "nixio socket %d", nixio__checksockfd(L));
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);
122 if (!strcmp(what, "rdwr") || !strcmp(what, "both")) {
124 } else if (!strcmp(what, "rd") || !strcmp(what, "read")) {
126 } else if (!strcmp(what, "wr") || !strcmp(what, "write")) {
129 return luaL_argerror(L, 2, "supported values: both, read, write");
132 return nixio__pstatus(L, !shutdown(sockfd, how));
136 static const luaL_reg R[] = {
137 {"socket", nixio_socket},
142 static const luaL_reg M[] = {
143 {"close", nixio_sock_close},
144 {"shutdown", nixio_sock_shutdown},
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");
156 luaL_register(L, NULL, R);
158 lua_pushvalue(L, -2);
159 luaL_register(L, NULL, M);