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.
28 static int nixio_socket(lua_State
*L
) {
29 const char *domain
= luaL_optlstring(L
, 1, "", NULL
);
30 const char *type
= luaL_optlstring(L
, 2, "", NULL
);
31 const char *proto
= lua_tolstring(L
, 3, NULL
);
33 nixio_sock
*sock
= lua_newuserdata(L
, sizeof(nixio_sock
));
35 return luaL_error(L
, "out of memory");
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_s(L
);
88 static int nixio_sock_close(lua_State
*L
) {
89 nixio_sock
*sock
= nixio__checksock(L
);
90 int sockfd
= sock
->fd
;
98 res
= closesocket(sockfd
);
100 } while (res
== -1 && errno
== EINTR
);
102 return nixio__pstatus_s(L
, !res
);
108 static int nixio_sock__gc(lua_State
*L
) {
109 nixio_sock
*sock
= (nixio_sock
*)luaL_checkudata(L
, 1, NIXIO_META
);
111 if (sock
&& sock
->fd
!= -1) {
114 res
= close(sock
->fd
);
116 res
= closesocket(sock
->fd
);
118 } while (res
== -1 && errno
== EINTR
);
124 * string representation
126 static int nixio_sock__tostring(lua_State
*L
) {
127 lua_pushfstring(L
, "nixio socket %d", nixio__checksockfd(L
));
134 static int nixio_sock_shutdown(lua_State
*L
) {
135 int sockfd
= nixio__checksockfd(L
);
136 const char *what
= luaL_optlstring(L
, 2, "rdwr", NULL
);
139 if (!strcmp(what
, "rdwr") || !strcmp(what
, "both")) {
141 } else if (!strcmp(what
, "rd") || !strcmp(what
, "read")) {
143 } else if (!strcmp(what
, "wr") || !strcmp(what
, "write")) {
146 return luaL_argerror(L
, 2, "supported values: both, read, write");
149 return nixio__pstatus_s(L
, !shutdown(sockfd
, how
));
153 static const luaL_reg R
[] = {
154 {"socket", nixio_socket
},
159 static const luaL_reg M
[] = {
160 {"close", nixio_sock_close
},
161 {"shutdown", nixio_sock_shutdown
},
162 {"__gc", nixio_sock__gc
},
163 {"__tostring", nixio_sock__tostring
},
167 void nixio_open_socket(lua_State
*L
) {
168 luaL_register(L
, NULL
, R
);
170 lua_pushvalue(L
, -2);
171 luaL_register(L
, NULL
, M
);