luci-lib-nixio: pass exact sockaddr length to getnameinfo()
[project/luci.git] / libs / luci-lib-nixio / src / splice.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 #ifdef __linux__
20 #define _GNU_SOURCE
21 #endif
22
23 #include "nixio.h"
24 #include <fcntl.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <sys/param.h>
29
30
31 #ifndef __WINNT__
32
33 #ifndef BSD
34 #include <sys/sendfile.h>
35 #else
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/uio.h>
39 #endif
40
41 #ifdef _GNU_SOURCE
42 #ifdef SPLICE_F_MOVE
43
44 /* guess what sucks... */
45 #ifdef __UCLIBC__
46 #include <unistd.h>
47 #include <sys/syscall.h>
48
49 ssize_t splice(int __fdin, __off64_t *__offin, int __fdout,
50         __off64_t *__offout, size_t __len, unsigned int __flags) {
51 #ifdef __NR_splice
52         return syscall(__NR_splice, __fdin, __offin, __fdout, __offout, __len, __flags);
53 #else
54         (void)__fdin;
55         (void)__offin;
56         (void)__fdout;
57         (void)__offout;
58         (void)__len;
59         (void)__flags;
60         errno = ENOSYS;
61         return -1;
62 #endif
63 }
64
65 #undef SPLICE_F_MOVE
66 #undef SPLICE_F_NONBLOCK
67 #undef SPLICE_F_MORE
68
69 #define SPLICE_F_MOVE 1
70 #define SPLICE_F_NONBLOCK 2
71 #define SPLICE_F_MORE 4
72
73 #endif /* __UCLIBC__ */
74
75 /**
76  * splice(fd_in, fd_out, length, flags)
77  */
78 static int nixio_splice(lua_State *L) {
79         int fd_in = nixio__checkfd(L, 1);
80         int fd_out = nixio__checkfd(L, 2);
81         size_t len = luaL_checkinteger(L, 3);
82         int flags = luaL_optinteger(L, 4, 0);
83         long spliced;
84
85         do {
86                 spliced = splice(fd_in, NULL, fd_out, NULL, len, flags);
87         } while (spliced == -1 && errno == EINTR);
88
89         if (spliced < 0) {
90                 return nixio__perror(L);
91         }
92
93         lua_pushinteger(L, spliced);
94         return 1;
95 }
96
97 /**
98  * Translate splice flags to integer
99  */
100 static int nixio_splice_flags(lua_State *L) {
101         const int j = lua_gettop(L);
102         int flags = 0;
103         for (int i=1; i<=j; i++) {
104                 const char *flag = luaL_checkstring(L, i);
105                 if (!strcmp(flag, "move")) {
106                         flags |= SPLICE_F_MOVE;
107                 } else if (!strcmp(flag, "nonblock")) {
108                         flags |= SPLICE_F_NONBLOCK;
109                 } else if (!strcmp(flag, "more")) {
110                         flags |= SPLICE_F_MORE;
111                 } else {
112                         return luaL_argerror(L, i, "supported values: "
113                          "move, nonblock, more");
114                 }
115         }
116         lua_pushinteger(L, flags);
117
118         return 1;
119 }
120
121 #endif /* SPLICE_F_MOVE */
122 #endif /* _GNU_SOURCE */
123
124 /**
125  * sendfile(outfd, infd, length)
126  */
127 static int nixio_sendfile(lua_State *L) {
128         int sock = nixio__checksockfd(L);
129         int infd = nixio__checkfd(L, 2);
130         size_t len = luaL_checkinteger(L, 3);
131         off_t spliced;
132
133 #ifndef BSD
134         do {
135                 spliced = sendfile(sock, infd, NULL, len);
136         } while (spliced == -1 && errno == EINTR);
137
138         if (spliced == -1) {
139                 return nixio__perror(L);
140         }
141 #else
142         int r;
143         const off_t offset = lseek(infd, 0, SEEK_CUR);
144
145         do {
146 #ifdef __DARWIN__
147                 r = sendfile(infd, sock, offset, (off_t *)&len, NULL, 0);
148                 spliced = r;
149 #else
150                 r = sendfile(infd, sock, offset, len, NULL, &spliced, 0);
151 #endif
152         } while (r == -1 && errno == EINTR);
153
154         if (r == -1) {
155                 return nixio__perror(L);
156         }
157 #endif
158
159         lua_pushinteger(L, spliced);
160         return 1;
161 }
162
163 /* module table */
164 static const luaL_reg R[] = {
165 #ifdef _GNU_SOURCE
166 #ifdef SPLICE_F_MOVE
167         {"splice",                      nixio_splice},
168         {"splice_flags",        nixio_splice_flags},
169 #endif
170 #endif
171         {"sendfile",            nixio_sendfile},
172         {NULL,                  NULL}
173 };
174
175
176 void nixio_open_splice(lua_State *L) {
177         luaL_register(L, NULL, R);
178 }
179
180 #else /* __WINNT__ */
181
182 void nixio_open_splice(lua_State *L) {
183 }
184
185 #endif /* !__WINNT__ */