nixio: FreeBSD compatibility #2
[project/luci.git] / libs / nixio / src / io.c
1 /*
2  * nixio - Linux I/O library for lua
3  *
4  *   Copyright (C) 2009 Steven Barth <steven@midlink.org>
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 #include "nixio.h"
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <arpa/inet.h>
26 #include <netinet/in.h>
27
28
29 /**
30  * send() / sendto() helper
31  */
32 static int nixio_sock__sendto(lua_State *L, int to) {
33         nixio_sock *sock = nixio__checksock(L);
34         struct sockaddr *addr = NULL;
35         socklen_t alen = 0;
36
37         if (to) {
38                 const char *address = luaL_checklstring(L, 3, NULL);
39                 uint16_t port = (uint16_t)luaL_checkinteger(L, 4);
40                 struct sockaddr_storage addrstor;
41                 addr = (struct sockaddr*)&addrstor;
42                 if (sock->domain == AF_INET) {
43                         struct sockaddr_in *inetaddr = (struct sockaddr_in *)addr;
44                         if (inet_pton(sock->domain, address, &inetaddr->sin_addr) < 0) {
45                                 return luaL_argerror(L, 3, "invalid address");
46                         }
47                         inetaddr->sin_port = htons(port);
48                         alen = sizeof(*inetaddr);
49                 } else if (sock->domain == AF_INET6) {
50                         struct sockaddr_in6 *inet6addr = (struct sockaddr_in6 *)addr;
51                         if (inet_pton(sock->domain, address, &inet6addr->sin6_addr) < 0) {
52                                 return luaL_argerror(L, 3, "invalid address");
53                         }
54                         inet6addr->sin6_port = htons(port);
55                         alen = sizeof(*inet6addr);
56                 } else {
57                         return luaL_argerror(L, 1, "supported families: inet, inet6");
58                 }
59         }
60
61         size_t len;
62         ssize_t sent;
63         const char *data = luaL_checklstring(L, 2, &len);
64         do {
65                 sent = sendto(sock->fd, data, len, 0, addr, alen);
66         } while(sent == -1 && errno == EINTR);
67         if (sent >= 0) {
68                 lua_pushinteger(L, sent);
69                 return 1;
70         } else {
71                 return nixio__perror(L);
72         }
73 }
74
75 /**
76  * send(data)
77  */
78 static int nixio_sock_send(lua_State *L) {
79         return nixio_sock__sendto(L, 0);
80 }
81
82 /**
83  * sendto(data, address, port)
84  */
85 static int nixio_sock_sendto(lua_State *L) {
86         return nixio_sock__sendto(L, 1);
87 }
88
89
90 /**
91  * recv() / recvfrom() helper
92  */
93 static int nixio_sock__recvfrom(lua_State *L, int from) {
94         nixio_sock *sock = nixio__checksock(L);
95         char buffer[NIXIO_BUFFERSIZE];
96         struct sockaddr_storage addrobj;
97         int req = luaL_checkinteger(L, 2);
98         int readc;
99
100         if (from && sock->domain != AF_INET && sock->domain != AF_INET6) {
101                 return luaL_argerror(L, 1, "supported families: inet, inet6");
102         }
103
104         struct sockaddr *addr = (from) ? (struct sockaddr*)&addrobj : NULL;
105         socklen_t alen = (from) ? sizeof(addrobj) : 0;
106
107         /* We limit the readsize to NIXIO_BUFFERSIZE */
108         req = (req > NIXIO_BUFFERSIZE) ? NIXIO_BUFFERSIZE : req;
109
110         do {
111                 readc = recvfrom(sock->fd, buffer, req, 0, addr, &alen);
112         } while (readc == -1 && errno == EINTR);
113
114         if (readc < 0) {
115                 return nixio__perror(L);
116         } else {
117                 lua_pushlstring(L, buffer, readc);
118
119                 if (!from) {
120                         return 1;
121                 } else {
122                         char ipaddr[INET6_ADDRSTRLEN];
123                         void *binaddr;
124                         uint16_t port;
125
126                         if (addrobj.ss_family == AF_INET) {
127                                 struct sockaddr_in *inetaddr = (struct sockaddr_in*)addr;
128                                 port = inetaddr->sin_port;
129                                 binaddr = &inetaddr->sin_addr;
130                         } else if (addrobj.ss_family == AF_INET6) {
131                                 struct sockaddr_in6 *inet6addr = (struct sockaddr_in6*)addr;
132                                 port = inet6addr->sin6_port;
133                                 binaddr = &inet6addr->sin6_addr;
134                         } else {
135                                 return luaL_error(L, "unknown address family");
136                         }
137
138                         if (!inet_ntop(addrobj.ss_family, binaddr, ipaddr, sizeof(ipaddr))) {
139                                 return nixio__perror(L);
140                         }
141
142                         lua_pushstring(L, ipaddr);
143                         lua_pushinteger(L, ntohs(port));
144
145                         return 3;
146                 }
147         }
148 }
149
150 /**
151  * recv(count)
152  */
153 static int nixio_sock_recv(lua_State *L) {
154         return nixio_sock__recvfrom(L, 0);
155 }
156
157 /**
158  * recvfrom(count)
159  */
160 static int nixio_sock_recvfrom(lua_State *L) {
161         return nixio_sock__recvfrom(L, 1);
162 }
163
164
165 /* module table */
166 static const luaL_reg M[] = {
167         {"send",        nixio_sock_send},
168         {"sendto",      nixio_sock_sendto},
169         {"recv",        nixio_sock_recv},
170         {"recvfrom",nixio_sock_recvfrom},
171         {"write",       nixio_sock_send},
172         {"read",        nixio_sock_recv},
173         {NULL,                  NULL}
174 };
175
176 void nixio_open_io(lua_State *L) {
177         lua_pushvalue(L, -2);
178         luaL_register(L, NULL, M);
179         lua_pop(L, 1);
180 }