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.
22 #include <sys/socket.h>
23 #include <netinet/in.h>
33 static int nixio_socket(lua_State *L) {
34 const char *domain = luaL_optlstring(L, 1, "", NULL);
35 const char *type = luaL_optlstring(L, 2, "", NULL);
36 const char *proto = lua_tolstring(L, 3, NULL);
38 nixio_sock *sock = lua_newuserdata(L, sizeof(nixio_sock));
40 if (!strcmp(domain, "inet")) {
41 sock->domain = AF_INET;
42 } else if (!strcmp(domain, "inet6")) {
43 sock->domain = AF_INET6;
44 } else if (!strcmp(domain, "unix")) {
45 sock->domain = AF_UNIX;
46 } else if (!strcmp(domain, "packet")) {
47 sock->domain = AF_PACKET;
49 return luaL_argerror(L, 1,
50 "supported values: inet, inet6, unix, packet"
54 if (!strcmp(type, "stream")) {
55 sock->type = SOCK_STREAM;
56 } else if (!strcmp(type, "dgram")) {
57 sock->type = SOCK_DGRAM;
58 } else if (!strcmp(type, "raw")) {
59 sock->type = SOCK_RAW;
61 return luaL_argerror(L, 2, "supported values: stream, dgram, raw");
66 } else if (!strcmp(proto, "icmp")) {
67 sock->protocol = IPPROTO_ICMP;
68 } else if (!strcmp(proto, "icmpv6")) {
69 sock->protocol = IPPROTO_ICMPV6;
71 return luaL_argerror(L, 3, "supported values: [empty], icmp, icmpv6");
75 luaL_getmetatable(L, NIXIO_META);
76 lua_setmetatable(L, -2);
78 sock->fd = socket(sock->domain, sock->type, sock->protocol);
81 return nixio__perror(L);
90 static int nixio_sock_close(lua_State *L) {
91 nixio_sock *sock = nixio__checksock(L);
92 int sockfd = sock->fd;
94 return nixio__pstatus(L, !close(sockfd));
100 static int nixio_sock__gc(lua_State *L) {
101 nixio_sock *sock = (nixio_sock*)luaL_checkudata(L, 1, NIXIO_META);
102 if (sock && sock->fd != -1) {
109 * string representation
111 static int nixio_sock__tostring(lua_State *L) {
112 lua_pushfstring(L, "nixio socket %d", nixio__checksockfd(L));
119 static int nixio_sock_shutdown(lua_State *L) {
120 int sockfd = nixio__checksockfd(L);
121 const char *what = luaL_optlstring(L, 2, "rdwr", NULL);
124 if (!strcmp(what, "rdwr") || !strcmp(what, "both")) {
126 } else if (!strcmp(what, "rd") || !strcmp(what, "read")) {
128 } else if (!strcmp(what, "wr") || !strcmp(what, "write")) {
131 return luaL_argerror(L, 2, "supported values: both, read, write");
134 return nixio__pstatus(L, !shutdown(sockfd, how));
138 static const luaL_reg R[] = {
139 {"socket", nixio_socket},
144 static const luaL_reg M[] = {
145 {"close", nixio_sock_close},
146 {"shutdown", nixio_sock_shutdown},
150 void nixio_open_socket(lua_State *L) {
151 luaL_getmetatable(L, NIXIO_META);
152 lua_pushcfunction(L, nixio_sock__gc);
153 lua_setfield(L, -2, "__gc");
154 lua_pushcfunction(L, nixio_sock__tostring);
155 lua_setfield(L, -2, "__tostring");
158 luaL_register(L, NULL, R);
160 lua_pushvalue(L, -2);
161 luaL_register(L, NULL, M);