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 return luaL_error(L, "out of memory");
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;*/
50 return luaL_argerror(L, 1,
51 "supported values: inet, inet6, unix, packet"
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;
62 return luaL_argerror(L, 2, "supported values: stream, dgram, raw");
67 } else if (!strcmp(proto, "icmp")) {
68 sock->protocol = IPPROTO_ICMP;
69 } else if (!strcmp(proto, "icmpv6")) {
70 sock->protocol = IPPROTO_ICMPV6;
72 return luaL_argerror(L, 3, "supported values: [empty], icmp, icmpv6");
76 luaL_getmetatable(L, NIXIO_META);
77 lua_setmetatable(L, -2);
79 sock->fd = socket(sock->domain, sock->type, sock->protocol);
82 return nixio__perror(L);
91 static int nixio_sock_close(lua_State *L) {
92 nixio_sock *sock = nixio__checksock(L);
93 int sockfd = sock->fd;
99 } while (res == -1 && errno == EINTR);
101 return nixio__pstatus(L, !res);
107 static int nixio_sock__gc(lua_State *L) {
108 nixio_sock *sock = (nixio_sock*)luaL_checkudata(L, 1, NIXIO_META);
110 if (sock && sock->fd != -1) {
112 res = close(sock->fd);
113 } while (res == -1 && errno == EINTR);
119 * string representation
121 static int nixio_sock__tostring(lua_State *L) {
122 lua_pushfstring(L, "nixio socket %d", nixio__checksockfd(L));
129 static int nixio_sock_shutdown(lua_State *L) {
130 int sockfd = nixio__checksockfd(L);
131 const char *what = luaL_optlstring(L, 2, "rdwr", NULL);
134 if (!strcmp(what, "rdwr") || !strcmp(what, "both")) {
136 } else if (!strcmp(what, "rd") || !strcmp(what, "read")) {
138 } else if (!strcmp(what, "wr") || !strcmp(what, "write")) {
141 return luaL_argerror(L, 2, "supported values: both, read, write");
144 return nixio__pstatus(L, !shutdown(sockfd, how));
148 static const luaL_reg R[] = {
149 {"socket", nixio_socket},
154 static const luaL_reg M[] = {
155 {"close", nixio_sock_close},
156 {"shutdown", nixio_sock_shutdown},
157 {"__gc", nixio_sock__gc},
158 {"__tostring", nixio_sock__tostring},
162 void nixio_open_socket(lua_State *L) {
163 luaL_register(L, NULL, R);
165 lua_pushvalue(L, -2);
166 luaL_register(L, NULL, M);