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;
95 return nixio__pstatus(L, !close(sockfd));
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) {
110 * string representation
112 static int nixio_sock__tostring(lua_State *L) {
113 lua_pushfstring(L, "nixio socket %d", nixio__checksockfd(L));
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);
125 if (!strcmp(what, "rdwr") || !strcmp(what, "both")) {
127 } else if (!strcmp(what, "rd") || !strcmp(what, "read")) {
129 } else if (!strcmp(what, "wr") || !strcmp(what, "write")) {
132 return luaL_argerror(L, 2, "supported values: both, read, write");
135 return nixio__pstatus(L, !shutdown(sockfd, how));
139 static const luaL_reg R[] = {
140 {"socket", nixio_socket},
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},
153 void nixio_open_socket(lua_State *L) {
154 luaL_register(L, NULL, R);
156 lua_pushvalue(L, -2);
157 luaL_register(L, NULL, M);