Changeset 4437
- Timestamp:
- 04/21/09 17:26:45 (4 years ago)
- Location:
- luci/trunk/libs/nixio
- Files:
-
- 27 added
- 17 modified
-
docsrc (added)
-
docsrc/nixio.bin.lua (added)
-
docsrc/nixio.bit.lua (added)
-
docsrc/nixio.crypto.lua (added)
-
docsrc/nixio.CryptoHash.lua (added)
-
docsrc/nixio.File.lua (added)
-
docsrc/nixio.fs.lua (added)
-
docsrc/nixio.lua (added)
-
docsrc/nixio.Socket.lua (added)
-
docsrc/nixio.TLSContext.lua (added)
-
docsrc/nixio.TLSSocket.lua (added)
-
docsrc/nixio.UnifiedIO.lua (added)
-
docsrc/README.lua (added)
-
LICENSE (added)
-
lua/bit.lua (added)
-
lua/nixio/fs.lua (added)
-
lua/nixio/util.lua (modified) (6 diffs)
-
Makefile (modified) (6 diffs)
-
NOTICE (added)
-
README (modified) (1 diff)
-
src/address.c (modified) (5 diffs)
-
src/axtls-compat.c (added)
-
src/axtls-compat.h (added)
-
src/binary.c (added)
-
src/bind.c (modified) (9 diffs)
-
src/bit.c (added)
-
src/file.c (modified) (7 diffs)
-
src/fs.c (added)
-
src/io.c (modified) (7 diffs)
-
src/mingw-compat.c (added)
-
src/mingw-compat.h (added)
-
src/nixio-tls.h (modified) (2 diffs)
-
src/nixio.c (modified) (11 diffs)
-
src/nixio.h (modified) (4 diffs)
-
src/poll.c (modified) (5 diffs)
-
src/process.c (modified) (5 diffs)
-
src/socket.c (modified) (5 diffs)
-
src/sockopt.c (modified) (15 diffs)
-
src/splice.c (modified) (2 diffs)
-
src/syslog.c (modified) (4 diffs)
-
src/tls-crypto.c (added)
-
src/tls-socket.c (modified) (3 diffs)
-
src/user.c (added)
-
standalone.mk (added)
Legend:
- Unmodified
- Added
- Removed
-
luci/trunk/libs/nixio/lua/nixio/util.lua
r4301 r4437 19 19 module "nixio.util" 20 20 21 local BUFFERSIZE = 8096 21 local BUFFERSIZE = nixio.const.buffersize 22 local ZIOBLKSIZE = 65536 22 23 local socket = nixio.meta_socket 23 24 local tls_socket = nixio.meta_tls_socket 24 25 local file = nixio.meta_file 25 26 27 function consume(iter) 28 local tbl = {} 29 for obj in iter do 30 tbl[#tbl+1] = obj 31 end 32 return tbl 33 end 34 26 35 local meta = {} 27 36 … … 39 48 40 49 function meta.readall(self, len) 41 local block, code, msg = self:read(len )50 local block, code, msg = self:read(len or BUFFERSIZE) 42 51 43 52 if not block then 44 return "", code, msg, len53 return nil, code, msg, "" 45 54 elseif #block == 0 then 46 return "", nil, nil, len55 return "", nil, nil, "" 47 56 end 48 57 49 58 local data, total = {block}, #block 50 59 51 while len > total do52 block, code, msg = self:read(len - total)60 while not len or len > total do 61 block, code, msg = self:read(len and (len - total) or BUFFERSIZE) 53 62 54 63 if not block then 55 return data, code, msg, len - #data64 return nil, code, msg, table.concat(data) 56 65 elseif #block == 0 then 57 return data, nil, nil, len - #data66 break 58 67 end 59 68 … … 61 70 end 62 71 63 return (#data > 1 and table.concat(data) or data[1]), nil, nil, 0 72 local data = #data > 1 and table.concat(data) or data[1] 73 return data, nil, nil, data 64 74 end 65 75 meta.recvall = meta.readall 66 76 67 77 function meta.writeall(self, data) 68 local total, block = 069 78 local sent, code, msg = self:write(data) 70 79 71 80 if not sent then 72 return total, code, msg, data 73 end 74 75 while sent < #data do 76 block, total = data:sub(sent + 1), total + sent 77 sent, code, msg = self:write(block) 78 81 return nil, code, msg, 0 82 end 83 84 local total = sent 85 86 while total < #data do 87 sent, code, msg = self:write(data, total) 88 79 89 if not sent then 80 return total, code, msg, block 81 end 90 return nil, code, msg, total 91 end 92 93 total = total + sent 82 94 end 83 95 84 return total + sent, nil, nil, ""96 return total, nil, nil, total 85 97 end 86 98 meta.sendall = meta.writeall … … 106 118 return line 107 119 elseif #buffer < limit + bpos then 108 local newblock, code = self:read(limit + bpos - #buffer)120 local newblock, code, msg = self:read(limit + bpos - #buffer) 109 121 if not newblock then 110 return nil, code 122 return nil, code, msg 111 123 elseif #newblock == 0 then 112 124 return nil … … 136 148 137 149 if not block then 138 return nil, code 150 return nil, code, msg 139 151 elseif #block == 0 then 140 152 return nil … … 163 175 end 164 176 177 function meta.copy(self, fdout, size) 178 local source = self:blocksource(nil, size) 179 local sink = fdout:sink() 180 local sent, chunk, code, msg = 0 181 182 repeat 183 chunk, code, msg = source() 184 sink(chunk, code, msg) 185 sent = chunk and (sent + #chunk) or sent 186 until not chunk 187 return not code and sent or nil, code, msg, sent 188 end 189 190 function meta.copyz(self, fd, size) 191 local sent, lsent, code, msg = 0 192 if self:is_file() then 193 if nixio.sendfile and fd:is_socket() and self:stat("type") == "reg" then 194 repeat 195 lsent, code, msg = nixio.sendfile(fd, self, size or ZIOBLKSIZE) 196 if lsent then 197 sent = sent + lsent 198 size = size and (size - lsent) 199 end 200 until (not lsent or lsent == 0 or (size and size == 0)) 201 if lsent or (not lsent and sent == 0 and 202 code ~= nixio.const.ENOSYS and code ~= nixio.const.EINVAL) then 203 return lsent and sent, code, msg, sent 204 end 205 end 206 end 207 208 return self:copy(fd, size) 209 end 210 165 211 function tls_socket.close(self) 166 212 return self.socket:close() -
luci/trunk/libs/nixio/Makefile
r4334 r4437 1 ifneq (,$(wildcard ../../build/config.mk)) 1 2 include ../../build/config.mk 2 3 include ../../build/module.mk 3 4 include ../../build/gccconfig.mk 5 else 6 include standalone.mk 7 endif 4 8 5 9 AXTLS_VERSION = 1.2.1 … … 7 11 AXTLS_FILE = $(AXTLS_DIR)-$(AXTLS_VERSION).tar.gz 8 12 NIXIO_TLS ?= openssl 9 NIXIO_LDFLAGS = 13 NIXIO_LDFLAGS = -lcrypt 14 NIXIO_SO = nixio.so 10 15 11 16 NIXIO_OBJ = src/nixio.o src/socket.o src/sockopt.o src/bind.o src/address.o \ 12 17 src/poll.o src/io.o src/file.o src/splice.o src/process.o src/syslog.o \ 13 src/tls-context.o src/tls-socket.o 18 src/bit.o src/binary.o src/fs.o src/user.o \ 19 src/tls-crypto.o src/tls-context.o src/tls-socket.o 14 20 15 21 ifeq ($(NIXIO_TLS),axtls) 16 TLS_CFLAGS = -IaxTLS/ssl -IaxTLS/crypto -IaxTLS/config -include src/ openssl-compat.h17 TLS_DEPENDS = src/ openssl-compat.o18 NIXIO_OBJ += src/ openssl-compat.o src/libaxtls.a22 TLS_CFLAGS = -IaxTLS/ssl -IaxTLS/crypto -IaxTLS/config -include src/axtls-compat.h 23 TLS_DEPENDS = src/axtls-compat.o 24 NIXIO_OBJ += src/axtls-compat.o src/libaxtls.a 19 25 endif 20 26 … … 28 34 endif 29 35 36 ifneq (,$(findstring MINGW,$(OS))$(findstring mingw,$(OS))$(findstring Windows,$(OS))) 37 NIXIO_CROSS_CC:=$(shell which i586-mingw32msvc-cc) 38 ifneq (,$(NIXIO_CROSS_CC)) 39 CC:=$(NIXIO_CROSS_CC) 40 endif 41 NIXIO_OBJ += src/mingw-compat.o 42 NIXIO_LDFLAGS_POST:=-llua -lssl -lcrypto -lws2_32 -lgdi32 43 FPIC:= 44 EXTRA_CFLAGS += -D_WIN32_WINNT=0x0501 45 LUA_CFLAGS:= 46 NIXIO_SO:=nixio.dll 47 NIXIO_LDFLAGS:= 48 endif 49 30 50 31 51 %.o: %.c 32 52 $(COMPILE) $(NIXIO_CFLAGS) $(LUA_CFLAGS) $(FPIC) -c -o $@ $< 53 54 src/tls-crypto.o: $(TLS_DEPENDS) src/tls-crypto.c 55 $(COMPILE) $(NIXIO_CFLAGS) $(LUA_CFLAGS) $(FPIC) $(TLS_CFLAGS) -c -o $@ src/tls-crypto.c 33 56 34 57 src/tls-context.o: $(TLS_DEPENDS) src/tls-context.c … … 38 61 $(COMPILE) $(NIXIO_CFLAGS) $(LUA_CFLAGS) $(FPIC) $(TLS_CFLAGS) -c -o $@ src/tls-socket.c 39 62 40 src/ openssl-compat.o: src/libaxtls.a src/openssl-compat.c41 $(COMPILE) $(NIXIO_CFLAGS) $(LUA_CFLAGS) $(FPIC) $(TLS_CFLAGS) -c -o $@ src/ openssl-compat.c63 src/axtls-compat.o: src/libaxtls.a src/axtls-compat.c 64 $(COMPILE) $(NIXIO_CFLAGS) $(LUA_CFLAGS) $(FPIC) $(TLS_CFLAGS) -c -o $@ src/axtls-compat.c 42 65 mkdir -p dist 43 66 cp -pR axtls-root/* dist/ … … 45 68 46 69 compile: $(NIXIO_OBJ) 47 $(LINK) $(SHLIB_FLAGS) $(NIXIO_LDFLAGS) -o src/ nixio.so $(NIXIO_OBJ)70 $(LINK) $(SHLIB_FLAGS) $(NIXIO_LDFLAGS) -o src/$(NIXIO_SO) $(NIXIO_OBJ) $(NIXIO_LDFLAGS_POST) 48 71 mkdir -p dist$(LUA_LIBRARYDIR) 49 cp src/ nixio.so dist$(LUA_LIBRARYDIR)/nixio.so72 cp src/$(NIXIO_SO) dist$(LUA_LIBRARYDIR)/$(NIXIO_SO) 50 73 51 74 $(AXTLS_DIR)/.prepared: … … 56 79 57 80 src/libaxtls.a: $(AXTLS_DIR)/.prepared 58 $(MAKE) -C $(AXTLS_DIR) CC=$(CC) CFLAGS="$(CFLAGS) $(EXTRA_CFLAGS) $(FPIC) '-Dalloca(size)=__builtin_alloca(size)'-Wall -pedantic -I../config -I../ssl -I../crypto" LDFLAGS="$(LDFLAGS)" OS="$(OS)" clean all81 $(MAKE) -C $(AXTLS_DIR) CC=$(CC) CFLAGS="$(CFLAGS) $(EXTRA_CFLAGS) $(FPIC) -Wall -pedantic -I../config -I../ssl -I../crypto" LDFLAGS="$(LDFLAGS)" OS="$(OS)" clean all 59 82 cp -p $(AXTLS_DIR)/_stage/libaxtls.a src 60 83 61 84 clean: luaclean 62 rm -f src/*.o src/*.so src/*.a 85 rm -f src/*.o src/*.so src/*.a src/*.dll 63 86 rm -f $(AXTLS_DIR)/.prepared 87 88 install: build 89 cp -pR dist$(LUA_MODULEDIR)/* $(LUA_MODULEDIR) 90 cp -pR dist$(LUA_LIBRARYDIR)/* $(LUA_LIBRARYDIR) -
luci/trunk/libs/nixio/README
r4287 r4437 1 1 Building: 2 Use GNU Make. 3 make or gmake depending on your system. 4 5 Special make flags: 2 6 3 With axTLS (standard): 4 make 5 6 With OpenSSL: 7 make NIXIO_TLS=openssl 7 OS Override Target OS [Linux|FreeBSD|SunOS|Windows] 8 NIXIO_TLS TLS-Library [*openssl|axtls] 9 NIXIO_CROSS_CC MinGW CC (Windows) `which i586-mingw32msvc-cc` 10 LUA_CFLAGS Lua CFLAGS `pkg-config --cflags lua5.1` 11 LUA_TARGET Lua compile target [*source|strip|compile] 12 LUA_MODULEDIR Install LUA_PATH "/usr/share/lua/5.1" 13 LUA_LIBRARYDIR Install LUA_CPATH "/usr/lib/lua/5.1" -
luci/trunk/libs/nixio/src/address.c
r4322 r4437 19 19 #include "nixio.h" 20 20 #include <sys/types.h> 21 #include <sys/socket.h> 22 #include <arpa/inet.h> 23 #include <netinet/in.h> 21 #include <errno.h> 24 22 #include <string.h> 25 #include <netdb.h>26 23 27 24 #ifndef NI_MAXHOST 28 25 #define NI_MAXHOST 1025 29 26 #endif 27 28 /** 29 * address pushing helper 30 */ 31 int nixio__addr_parse(nixio_addr *addr, struct sockaddr *saddr) { 32 void *baddr; 33 34 addr->family = saddr->sa_family; 35 if (saddr->sa_family == AF_INET) { 36 struct sockaddr_in *inetaddr = (struct sockaddr_in*)saddr; 37 addr->port = ntohs(inetaddr->sin_port); 38 baddr = &inetaddr->sin_addr; 39 } else if (saddr->sa_family == AF_INET6) { 40 struct sockaddr_in6 *inet6addr = (struct sockaddr_in6*)saddr; 41 addr->port = ntohs(inet6addr->sin6_port); 42 baddr = &inet6addr->sin6_addr; 43 } else { 44 errno = EAFNOSUPPORT; 45 return -1; 46 } 47 48 if (!inet_ntop(saddr->sa_family, baddr, addr->host, sizeof(addr->host))) { 49 return -1; 50 } 51 52 return 0; 53 } 54 55 /** 56 * address pulling helper 57 */ 58 int nixio__addr_write(nixio_addr *addr, struct sockaddr *saddr) { 59 if (addr->family == AF_UNSPEC) { 60 if (strchr(addr->host, ':')) { 61 addr->family = AF_INET6; 62 } else { 63 addr->family = AF_INET; 64 } 65 } 66 if (addr->family == AF_INET) { 67 struct sockaddr_in *inetaddr = (struct sockaddr_in *)saddr; 68 memset(inetaddr, 0, sizeof(struct sockaddr_in)); 69 70 if (inet_pton(AF_INET, addr->host, &inetaddr->sin_addr) < 1) { 71 return -1; 72 } 73 74 inetaddr->sin_family = AF_INET; 75 inetaddr->sin_port = htons((uint16_t)addr->port); 76 return 0; 77 } else if (addr->family == AF_INET6) { 78 struct sockaddr_in6 *inet6addr = (struct sockaddr_in6 *)saddr; 79 memset(inet6addr, 0, sizeof(struct sockaddr_in6)); 80 81 if (inet_pton(AF_INET6, addr->host, &inet6addr->sin6_addr) < 1) { 82 return -1; 83 } 84 85 inet6addr->sin6_family = AF_INET6; 86 inet6addr->sin6_port = htons((uint16_t)addr->port); 87 return 0; 88 } else { 89 errno = EAFNOSUPPORT; 90 return -1; 91 } 92 } 30 93 31 94 … … 71 134 for (rp = result; rp != NULL; rp = rp->ai_next) { 72 135 /* avoid duplicate results */ 136 #ifndef __WINNT__ 73 137 if (!port && rp->ai_socktype != SOCK_STREAM) { 74 138 continue; 75 139 } 140 #endif 76 141 77 142 if (rp->ai_family == AF_INET || rp->ai_family == AF_INET6) { … … 102 167 } 103 168 104 char ip[INET6_ADDRSTRLEN]; 105 void *binaddr = NULL; 106 uint16_t binport = 0; 107 108 if (rp->ai_family == AF_INET) { 109 struct sockaddr_in *v4addr = (struct sockaddr_in*)rp->ai_addr; 110 binport = v4addr->sin_port; 111 binaddr = (void *)&v4addr->sin_addr; 112 } else if (rp->ai_family == AF_INET6) { 113 struct sockaddr_in6 *v6addr = (struct sockaddr_in6*)rp->ai_addr; 114 binport = v6addr->sin6_port; 115 binaddr = (void *)&v6addr->sin6_addr; 116 } 117 118 if (!inet_ntop(rp->ai_family, binaddr, ip, sizeof(ip))) { 169 nixio_addr addr; 170 if (nixio__addr_parse(&addr, rp->ai_addr)) { 119 171 freeaddrinfo(result); 120 return nixio__perror (L);172 return nixio__perror_s(L); 121 173 } 122 174 123 175 if (port) { 124 lua_pushinteger(L, ntohs(binport));176 lua_pushinteger(L, addr.port); 125 177 lua_setfield(L, -2, "port"); 126 178 } 127 179 128 lua_pushstring(L, ip);180 lua_pushstring(L, addr.host); 129 181 lua_setfield(L, -2, "address"); 130 182 lua_rawseti(L, -2, i++); … … 141 193 */ 142 194 static int nixio_getnameinfo(lua_State *L) { 143 const char *ip = luaL_check lstring(L, 1, NULL);144 const char *family = luaL_opt lstring(L, 2, "inet", NULL);195 const char *ip = luaL_checkstring(L, 1); 196 const char *family = luaL_optstring(L, 2, NULL); 145 197 char host[NI_MAXHOST]; 146 198 147 struct sockaddr *addr = NULL; 148 socklen_t alen = 0; 149 int res; 150 151 if (!strcmp(family, "inet")) { 152 struct sockaddr_in inetaddr; 153 memset(&inetaddr, 0, sizeof(inetaddr)); 154 inetaddr.sin_family = AF_INET; 155 if (inet_pton(AF_INET, ip, &inetaddr.sin_addr) < 1) { 156 return luaL_argerror(L, 1, "invalid address"); 157 } 158 alen = sizeof(inetaddr); 159 addr = (struct sockaddr *)&inetaddr; 199 struct sockaddr_storage saddr; 200 nixio_addr addr; 201 memset(&addr, 0, sizeof(addr)); 202 strncpy(addr.host, ip, sizeof(addr.host) - 1); 203 204 if (!family) { 205 addr.family = AF_UNSPEC; 206 } else if (!strcmp(family, "inet")) { 207 addr.family = AF_INET; 160 208 } else if (!strcmp(family, "inet6")) { 161 struct sockaddr_in6 inet6addr; 162 memset(&inet6addr, 0, sizeof(inet6addr)); 163 inet6addr.sin6_family = AF_INET6; 164 if (inet_pton(AF_INET6, ip, &inet6addr.sin6_addr) < 1) { 165 return luaL_argerror(L, 1, "invalid address"); 166 } 167 alen = sizeof(inet6addr); 168 addr = (struct sockaddr *)&inet6addr; 209 addr.family = AF_INET6; 169 210 } else { 170 211 return luaL_argerror(L, 2, "supported values: inet, inet6"); 171 212 } 172 213 173 res = getnameinfo(addr, alen, host, sizeof(host), NULL, 0, NI_NAMEREQD); 214 nixio__addr_write(&addr, (struct sockaddr *)&saddr); 215 216 int res = getnameinfo((struct sockaddr *)&saddr, sizeof(saddr), 217 host, sizeof(host), NULL, 0, NI_NAMEREQD); 174 218 if (res) { 175 219 lua_pushnil(L); … … 184 228 185 229 /** 186 * getsockname() / getpeername() helper187 */ 188 static int nixio_sock_ _getname(lua_State *L, int sock) {230 * getsockname() 231 */ 232 static int nixio_sock_getsockname(lua_State *L) { 189 233 int sockfd = nixio__checksockfd(L); 190 struct sockaddr_storage addr; 191 socklen_t addrlen = sizeof(addr); 192 char ipaddr[INET6_ADDRSTRLEN]; 193 void *binaddr; 194 uint16_t port; 195 196 if (sock) { 197 if (getsockname(sockfd, (struct sockaddr*)&addr, &addrlen)) { 198 return nixio__perror(L); 199 } 200 } else { 201 if (getpeername(sockfd, (struct sockaddr*)&addr, &addrlen)) { 202 return nixio__perror(L); 203 } 204 } 205 206 if (addr.ss_family == AF_INET) { 207 struct sockaddr_in *inetaddr = (struct sockaddr_in*)&addr; 208 port = inetaddr->sin_port; 209 binaddr = &inetaddr->sin_addr; 210 } else if (addr.ss_family == AF_INET6) { 211 struct sockaddr_in6 *inet6addr = (struct sockaddr_in6*)&addr; 212 port = inet6addr->sin6_port; 213 binaddr = &inet6addr->sin6_addr; 214 } else { 215 return luaL_error(L, "unknown address family"); 216 } 217 218 if (!inet_ntop(addr.ss_family, binaddr, ipaddr, sizeof(ipaddr))) { 219 return nixio__perror(L); 220 } 221 222 lua_pushstring(L, ipaddr); 223 lua_pushinteger(L, ntohs(port)); 234 struct sockaddr_storage saddr; 235 socklen_t addrlen = sizeof(saddr); 236 nixio_addr addr; 237 238 if (getsockname(sockfd, (struct sockaddr*)&saddr, &addrlen) || 239 nixio__addr_parse(&addr, (struct sockaddr*)&saddr)) { 240 return nixio__perror_s(L); 241 } 242 243 lua_pushstring(L, addr.host); 244 lua_pushnumber(L, addr.port); 224 245 return 2; 225 246 } 226 247 227 248 /** 228 * getsockname()229 */230 static int nixio_sock_getsockname(lua_State *L) {231 return nixio_sock__getname(L, 1);232 }233 234 /**235 249 * getpeername() 236 250 */ 237 251 static int nixio_sock_getpeername(lua_State *L) { 238 return nixio_sock__getname(L, 0); 252 int sockfd = nixio__checksockfd(L); 253 struct sockaddr_storage saddr; 254 socklen_t addrlen = sizeof(saddr); 255 nixio_addr addr; 256 257 if (getpeername(sockfd, (struct sockaddr*)&saddr, &addrlen) || 258 nixio__addr_parse(&addr, (struct sockaddr*)&saddr)) { 259 return nixio__perror_s(L); 260 } 261 262 lua_pushstring(L, addr.host); 263 lua_pushnumber(L, addr.port); 264 return 2; 239 265 } 240 266 -
luci/trunk/libs/nixio/src/bind.c
r4322 r4437 19 19 #include "nixio.h" 20 20 #include <sys/types.h> 21 #include <sys/socket.h>22 #include <arpa/inet.h>23 #include <netinet/in.h>24 #include <sys/un.h>25 21 #include <string.h> 26 22 #include <unistd.h> 27 #include <netdb.h>28 23 #include <errno.h> 24 29 25 30 26 /** … … 88 84 89 85 if (do_bind) { 86 int one = 1; 87 setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, 88 (char*)&one, sizeof(one)); 90 89 status = bind(sock->fd, rp->ai_addr, rp->ai_addrlen); 91 90 } else { … … 104 103 105 104 do { 105 #ifndef __WINNT__ 106 106 clstat = close(sock->fd); 107 #else 108 clstat = closesocket(sock->fd); 109 #endif 107 110 } while (clstat == -1 && errno == EINTR); 108 111 } … … 112 115 /* on failure */ 113 116 if (status) { 114 return nixio__perror (L);117 return nixio__perror_s(L); 115 118 } 116 119 … … 184 187 185 188 freeaddrinfo(result); 189 #ifndef __WINNT__ 186 190 } else if (sock->domain == AF_UNIX) { 187 191 size_t pathlen; … … 201 205 } while (status == -1 && errno == EINTR); 202 206 } 207 #endif 203 208 } else { 204 209 return luaL_error(L, "not supported"); 205 210 } 206 return nixio__pstatus (L, !status);211 return nixio__pstatus_s(L, !status); 207 212 } 208 213 … … 226 231 static int nixio_sock_listen(lua_State *L) { 227 232 int sockfd = nixio__checksockfd(L); 228 lua_Integerbacklog = luaL_checkinteger(L, 2);229 return nixio__pstatus (L, !listen(sockfd, backlog));233 int backlog = luaL_checkinteger(L, 2); 234 return nixio__pstatus_s(L, !listen(sockfd, backlog)); 230 235 } 231 236 … … 235 240 static int nixio_sock_accept(lua_State *L) { 236 241 nixio_sock *sock = nixio__checksock(L); 237 struct sockaddr_storage addr; 238 socklen_t addrlen = sizeof(addr); 239 char ipaddr[INET6_ADDRSTRLEN]; 240 void *binaddr; 241 uint16_t port; 242 struct sockaddr_storage saddr; 243 nixio_addr addr; 244 socklen_t saddrlen = sizeof(saddr); 242 245 int newfd; 243 246 244 247 do { 245 newfd = accept(sock->fd, (struct sockaddr *)& addr, &addrlen);248 newfd = accept(sock->fd, (struct sockaddr *)&saddr, &saddrlen); 246 249 } while (newfd == -1 && errno == EINTR); 247 250 if (newfd < 0) { 248 return nixio__perror (L);251 return nixio__perror_s(L); 249 252 } 250 253 … … 257 260 clsock->fd = newfd; 258 261 259 if (addr.ss_family == AF_INET) { 260 struct sockaddr_in *inetaddr = (struct sockaddr_in*)&addr; 261 port = inetaddr->sin_port; 262 binaddr = &inetaddr->sin_addr; 263 } else if (addr.ss_family == AF_INET6) { 264 struct sockaddr_in6 *inet6addr = (struct sockaddr_in6*)&addr; 265 port = inet6addr->sin6_port; 266 binaddr = &inet6addr->sin6_addr; 267 } else { 268 return luaL_error(L, "unknown address family"); 269 } 270 271 if (!inet_ntop(addr.ss_family, binaddr, ipaddr, sizeof(ipaddr))) { 272 return nixio__perror(L); 273 } 274 275 lua_pushstring(L, ipaddr); 276 lua_pushinteger(L, ntohs(port)); 277 return 3; 262 if (!nixio__addr_parse(&addr, (struct sockaddr *)&saddr)) { 263 lua_pushstring(L, addr.host); 264 lua_pushnumber(L, addr.port); 265 return 3; 266 } else { 267 return 1; 268 } 278 269 } 279 270 -
luci/trunk/libs/nixio/src/file.c
r4322 r4437 31 31 static int nixio_open(lua_State *L) { 32 32 const char *filename = luaL_checklstring(L, 1, NULL); 33 int flags = luaL_optint(L, 2, O_RDONLY); 34 int mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; 33 int flags; 34 35 if (lua_isnoneornil(L, 2)) { 36 flags = O_RDONLY; 37 } else if (lua_isnumber(L, 2)) { 38 flags = lua_tointeger(L, 2); 39 } else if (lua_isstring(L, 2)) { 40 const char *str = lua_tostring(L, 2); 41 if (!strcmp(str, "r")) { 42 flags = O_RDONLY; 43 } else if (!strcmp(str, "r+")) { 44 flags = O_RDWR; 45 } else if (!strcmp(str, "w")) { 46 flags = O_WRONLY | O_CREAT | O_TRUNC; 47 } else if (!strcmp(str, "w+")) { 48 flags = O_RDWR | O_CREAT | O_TRUNC; 49 } else if (!strcmp(str, "a")) { 50 flags = O_WRONLY | O_CREAT | O_APPEND; 51 } else if (!strcmp(str, "a+")) { 52 flags = O_RDWR | O_CREAT | O_APPEND; 53 } else { 54 return luaL_argerror(L, 2, "supported values: r, r+, w, w+, a, a+"); 55 } 56 } else { 57 return luaL_argerror(L, 2, "open flags or string expected"); 58 } 59 35 60 int fd; 36 61 37 62 do { 38 fd = open(filename, flags, mode);63 fd = open(filename, flags, nixio__check_mode(L, 3, 0666)); 39 64 } while (fd == -1 && errno == EINTR); 40 65 if (fd == -1) { … … 67 92 mode |= O_EXCL; 68 93 } else if (!strcmp(flag, "nonblock") || !strcmp(flag, "ndelay")) { 94 #ifndef __WINNT__ 69 95 mode |= O_NONBLOCK; 96 #endif 70 97 } else if (!strcmp(flag, "sync")) { 98 #ifndef __WINNT__ 71 99 mode |= O_SYNC; 100 #endif 72 101 } else if (!strcmp(flag, "trunc")) { 73 102 mode |= O_TRUNC; … … 142 171 const char *data = luaL_checklstring(L, 2, &len); 143 172 173 if (lua_gettop(L) > 2) { 174 int offset = luaL_optint(L, 3, 0); 175 if (offset) { 176 if (offset < len) { 177 data += offset; 178 len -= offset; 179 } else { 180 len = 0; 181 } 182 } 183 184 unsigned int wlen = luaL_optint(L, 4, len); 185 if (wlen < len) { 186 len = wlen; 187 } 188 } 189 144 190 do { 145 191 sent = write(fd, data, len); … … 156 202 int fd = nixio__checkfd(L, 1); 157 203 char buffer[NIXIO_BUFFERSIZE]; 158 int req = luaL_checkinteger(L, 2);204 uint req = luaL_checkinteger(L, 2); 159 205 int readc; 160 206 … … 209 255 } 210 256 257 static int nixio_file_stat(lua_State *L) { 258 nixio_stat_t buf; 259 if (fstat(nixio__checkfd(L, 1), &buf)) { 260 return nixio__perror(L); 261 } else { 262 nixio__push_stat(L, &buf); 263 if (lua_isstring(L, 2)) { 264 lua_getfield(L, -1, lua_tostring(L, 2)); 265 } 266 return 1; 267 } 268 } 269 211 270 static int nixio_file_sync(lua_State *L) { 212 271 int fd = nixio__checkfd(L, 1); 213 #ifndef BSD 214 int meta = lua_toboolean(L, 2); 215 return nixio__pstatus(L, (meta) ? !fsync(fd) : !fdatasync(fd)); 272 int stat; 273 #if (!defined BSD && !defined __WINNT__) 274 int dataonly = lua_toboolean(L, 2); 275 do { 276 stat = (dataonly) ? fdatasync(fd) : fsync(fd); 277 } while (stat == -1 && errno == EINTR); 278 return nixio__pstatus(L, !stat); 216 279 #else 217 return nixio__pstatus(L, !fsync(fd)); 280 do { 281 stat = fsync(fd); 282 } while (stat == -1 && errno == EINTR); 283 return nixio__pstatus(L, !stat); 218 284 #endif 219 285 } … … 283 349 {"tell", nixio_file_tell}, 284 350 {"seek", nixio_file_seek}, 351 {"stat", nixio_file_stat}, 285 352 {"sync", nixio_file_sync}, 286 353 {"lock", nixio_file_lock}, … … 307 374 lua_pushvalue(L, -1); 308 375 lua_setfield(L, -2, "__index"); 376 377 int *uin = lua_newuserdata(L, sizeof(int)); 378 int *uout = lua_newuserdata(L, sizeof(int)); 379 int *uerr = lua_newuserdata(L, sizeof(int)); 380 381 if (!uin || !uout || !uerr) { 382 luaL_error(L, "out of memory"); 383 } 384 385 *uin = STDIN_FILENO; 386 *uout = STDOUT_FILENO; 387 *uerr = STDERR_FILENO; 388 389 for (int i = -4; i < -1; i++) { 390 lua_pushvalue(L, -4); 391 lua_setmetatable(L, i); 392 } 393 394 lua_setfield(L, -5, "stderr"); 395 lua_setfield(L, -4, "stdout"); 396 lua_setfield(L, -3, "stdin"); 309 397 lua_setfield(L, -2, "meta_file"); 310 398 } -
luci/trunk/libs/nixio/src/io.c
r4322 r4437 19 19 #include "nixio.h" 20 20 #include <errno.h> 21 #include <string.h> 21 22 #include <stdlib.h> 22 23 #include <stdio.h> 23 24 #include <sys/types.h> 24 #include <sys/socket.h>25 #include <arpa/inet.h>26 #include <netinet/in.h>27 25 28 26 … … 34 32 struct sockaddr *addr = NULL; 35 33 socklen_t alen = 0; 34 int argoff = 2; 36 35 37 36 if (to) { 38 const char *address = luaL_checklstring(L, 3, NULL);39 uint16_t port = (uint16_t)luaL_checkinteger(L, 4);37 argoff += 2; 38 const char *address = luaL_checkstring(L, 3); 40 39 struct sockaddr_storage addrstor; 41 40 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"); 41 42 nixio_addr naddr; 43 memset(&naddr, 0, sizeof(naddr)); 44 strncpy(naddr.host, address, sizeof(naddr.host) - 1); 45 naddr.port = (uint16_t)luaL_checkinteger(L, 4); 46 naddr.family = sock->domain; 47 48 if (nixio__addr_write(&naddr, addr)) { 49 return nixio__perror_s(L); 58 50 } 59 51 } … … 62 54 ssize_t sent; 63 55 const char *data = luaL_checklstring(L, 2, &len); 56 57 if (lua_gettop(L) > argoff) { 58 int offset = luaL_optint(L, argoff + 1, 0); 59 if (offset) { 60 if (offset < len) { 61 data += offset; 62 len -= offset; 63 } else { 64 len = 0; 65 } 66 } 67 68 unsigned int wlen = luaL_optint(L, argoff + 2, len); 69 if (wlen < len) { 70 len = wlen; 71 } 72 } 73 64 74 do { 65 75 sent = sendto(sock->fd, data, len, 0, addr, alen); … … 69 79 return 1; 70 80 } else { 71 return nixio__perror (L);81 return nixio__perror_s(L); 72 82 } 73 83 } … … 95 105 char buffer[NIXIO_BUFFERSIZE]; 96 106 struct sockaddr_storage addrobj; 97 int req = luaL_checkinteger(L, 2);107 uint req = luaL_checkinteger(L, 2); 98 108 int readc; 99 109 … … 112 122 } while (readc == -1 && errno == EINTR); 113 123 124 #ifdef __WINNT__ 114 125 if (readc < 0) { 115 return nixio__perror(L); 126 int e = WSAGetLastError(); 127 if (e == WSAECONNRESET || e == WSAECONNABORTED || e == WSAESHUTDOWN) { 128 readc = 0; 129 } 130 } 131 #endif 132 133 if (readc < 0) { 134 return nixio__perror_s(L); 116 135 } else { 117 136 lua_pushlstring(L, buffer, readc); … … 120 139 return 1; 121 140 } 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; 141 nixio_addr naddr; 142 if (!nixio__addr_parse(&naddr, (struct sockaddr *)&addrobj)) { 143 lua_pushstring(L, naddr.host); 144 lua_pushnumber(L, naddr.port); 145 return 3; 134 146 } else { 135 return luaL_error(L, "unknown address family");147 return 1; 136 148 } 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 149 } 147 150 } -
luci/trunk/libs/nixio/src/nixio-tls.h
r4311 r4437 3 3 4 4 #include "nixio.h" 5 #include <sys/types.h> 5 6 6 7 #ifndef WITHOUT_OPENSSL 7 8 #include <openssl/ssl.h> 9 #include <openssl/md5.h> 10 #include <openssl/sha.h> 8 11 #endif 9 12 … … 21 24 } nixio_tls_sock; 22 25 26 #define NIXIO_CRYPTO_HASH_META "nixio.crypto.hash" 27 #define NIXIO_DIGEST_SIZE 64 28 #define NIXIO_CRYPTO_BLOCK_SIZE 64 29 30 #define NIXIO_HASH_NONE 0 31 #define NIXIO_HASH_MD5 0x01 32 #define NIXIO_HASH_SHA1 0x02 33 34 #define NIXIO_HMAC_BIT 0x40 35 36 typedef int(*nixio_hash_initcb)(void *); 37 typedef int(*nixio_hash_updatecb)(void *, const void *, unsigned long); 38 typedef int(*nixio_hash_finalcb)(unsigned char *, void *); 39 40 typedef struct nixio_hash_obj { 41 uint type; 42 unsigned char digest[NIXIO_DIGEST_SIZE]; 43 size_t digest_size; 44 unsigned char key[NIXIO_CRYPTO_BLOCK_SIZE]; 45 size_t key_size; 46 size_t block_size; 47 void *ctx; 48 nixio_hash_initcb init; 49 nixio_hash_updatecb update; 50 nixio_hash_finalcb final; 51 } nixio_hash; 52 23 53 #endif /* NIXIO_TLS_H_ */ -
luci/trunk/libs/nixio/src/nixio.c
r4334 r4437 28 28 /* pushes nil, error number and errstring on the stack */ 29 29 int nixio__perror(lua_State *L) { 30 if (errno == EAGAIN ) {30 if (errno == EAGAIN || errno == EWOULDBLOCK) { 31 31 lua_pushboolean(L, 0); 32 32 } else { … … 86 86 } 87 87 88 /* An empty iterator */ 89 int nixio__nulliter(lua_State *L) { 90 lua_pushnil(L); 91 return 1; 92 } 93 94 static int nixio_errno(lua_State *L) { 95 lua_pushinteger(L, errno); 96 return 1; 97 } 98 88 99 static int nixio_strerror(lua_State *L) { 89 100 lua_pushstring(L, strerror(luaL_checkinteger(L, 1))); … … 93 104 /* object table */ 94 105 static const luaL_reg R[] = { 106 {"errno", nixio_errno}, 95 107 {"strerror", nixio_strerror}, 96 108 {NULL, NULL} … … 98 110 99 111 /* entry point */ 100 LUALIB_API int luaopen_nixio(lua_State *L) {112 NIXIO_API int luaopen_nixio(lua_State *L) { 101 113 /* create metatable */ 102 114 luaL_newmetatable(L, NIXIO_META); … … 114 126 115 127 /* register methods */ 128 #ifdef __WINNT__ 129 nixio_open__mingw(L); 130 #endif 116 131 nixio_open_file(L); 117 132 nixio_open_socket(L); … … 124 139 nixio_open_process(L); 125 140 nixio_open_syslog(L); 141 nixio_open_bit(L); 142 nixio_open_bin(L); 143 nixio_open_fs(L); 144 nixio_open_user(L); 145 nixio_open_tls_crypto(L); 126 146 nixio_open_tls_context(L); 127 147 nixio_open_tls_socket(L); … … 132 152 133 153 /* some constants */ 134 lua_createtable(L, 0, 49); 154 lua_newtable(L); 155 156 lua_pushliteral(L, NIXIO_SEP); 157 lua_setfield(L, -2, "sep"); 158 159 lua_pushliteral(L, NIXIO_PATHSEP); 160 lua_setfield(L, -2, "pathsep"); 161 162 lua_pushinteger(L, NIXIO_BUFFERSIZE); 163 lua_setfield(L, -2, "buffersize"); 135 164 136 165 NIXIO_PUSH_CONSTANT(EACCES); … … 138 167 NIXIO_PUSH_CONSTANT(ENOSYS); 139 168 NIXIO_PUSH_CONSTANT(EINVAL); 140 NIXIO_PUSH_CONSTANT(EWOULDBLOCK);141 169 NIXIO_PUSH_CONSTANT(EAGAIN); 142 170 NIXIO_PUSH_CONSTANT(ENOMEM); … … 153 181 NIXIO_PUSH_CONSTANT(EPERM); 154 182 NIXIO_PUSH_CONSTANT(EEXIST); 155 NIXIO_PUSH_CONSTANT(ELOOP);156 183 NIXIO_PUSH_CONSTANT(EMFILE); 157 184 NIXIO_PUSH_CONSTANT(ENAMETOOLONG); 158 185 NIXIO_PUSH_CONSTANT(ENFILE); 159 186 NIXIO_PUSH_CONSTANT(ENODEV); 187 NIXIO_PUSH_CONSTANT(EXDEV); 160 188 NIXIO_PUSH_CONSTANT(ENOTDIR); 161 189 NIXIO_PUSH_CONSTANT(ENXIO); 190 NIXIO_PUSH_CONSTANT(EROFS); 191 NIXIO_PUSH_CONSTANT(EBUSY); 192 NIXIO_PUSH_CONSTANT(ESRCH); 193 NIXIO_PUSH_CONSTANT(SIGINT); 194 NIXIO_PUSH_CONSTANT(SIGTERM); 195 NIXIO_PUSH_CONSTANT(SIGSEGV); 196 197 #ifndef __WINNT__ 198 NIXIO_PUSH_CONSTANT(EWOULDBLOCK); 199 NIXIO_PUSH_CONSTANT(ELOOP); 162 200 NIXIO_PUSH_CONSTANT(EOVERFLOW); 163 NIXIO_PUSH_CONSTANT(EROFS);164 201 NIXIO_PUSH_CONSTANT(ETXTBSY); 165 202 NIXIO_PUSH_CONSTANT(EAFNOSUPPORT); … … 167 204 NIXIO_PUSH_CONSTANT(EPROTONOSUPPORT); 168 205 NIXIO_PUSH_CONSTANT(ENOPROTOOPT); 169 NIXIO_PUSH_CONSTANT(EBUSY); 170 NIXIO_PUSH_CONSTANT(ESRCH); 206 NIXIO_PUSH_CONSTANT(EADDRINUSE); 207 NIXIO_PUSH_CONSTANT(ENETDOWN); 208 NIXIO_PUSH_CONSTANT(ENETUNREACH); 209 171 210 NIXIO_PUSH_CONSTANT(SIGALRM); 172 NIXIO_PUSH_CONSTANT(SIGINT);173 NIXIO_PUSH_CONSTANT(SIGTERM);174 211 NIXIO_PUSH_CONSTANT(SIGKILL); 175 212 NIXIO_PUSH_CONSTANT(SIGHUP); 176 213 NIXIO_PUSH_CONSTANT(SIGSTOP); 177 214 NIXIO_PUSH_CONSTANT(SIGCONT); 178 NIXIO_PUSH_CONSTANT(SIGSEGV);179 215 NIXIO_PUSH_CONSTANT(SIGCHLD); 180 216 NIXIO_PUSH_CONSTANT(SIGQUIT); … … 183 219 NIXIO_PUSH_CONSTANT(SIGIO); 184 220 NIXIO_PUSH_CONSTANT(SIGURG); 185 221 NIXIO_PUSH_CONSTANT(SIGPIPE); 222 223 lua_pushvalue(L, -1); 224 lua_setfield(L, -3, "const_sock"); 225 226 signal(SIGPIPE, SIG_IGN); 227 #endif /* !__WINNT__ */ 186 228 lua_setfield(L, -2, "const"); 187 229 -
luci/trunk/libs/nixio/src/nixio.h
r4334 r4437 2 2 #define NIXIO_H_ 3 3 4 #define NIXIO_OOM "out of memory" 5 4 6 #define NIXIO_META "nixio.socket" 5 7 #define NIXIO_FILE_META "nixio.file" 6 #define NIXIO_BUFFERSIZE 8096 8 #define NIXIO_GLOB_META "nixio.glob" 9 #define NIXIO_DIR_META "nixio.dir" 7 10 #define _FILE_OFFSET_BITS 64 8 11 … … 18 21 #include <lauxlib.h> 19 22 23 #define NIXIO_BUFFERSIZE 8192 24 20 25 typedef struct nixio_socket { 21 26 int fd; … … 25 30 } nixio_sock; 26 31 32 typedef struct nixio_address { 33 int family; 34 char host[128]; 35 int port; 36 } nixio_addr; 37 27 38 int nixio__perror(lua_State *L); 28 39 int nixio__pstatus(lua_State *L, int condition); 40 41 #ifndef __WINNT__ 42 43 #define NIXIO_API extern 44 45 #include <sys/types.h> 46 #include <sys/socket.h> 47 #include <arpa/inet.h> 48 #include <netinet/in.h> 49 #include <netinet/tcp.h> 50 #include <net/if.h> 51 #include <sys/un.h> 52 #include <netdb.h> 53 #include <poll.h> 54 #include <sys/stat.h> 55 #include <errno.h> 56 57 #define NIXIO_SEP "/" 58 #define NIXIO_PATHSEP ":" 59 60 #define nixio__perror_s nixio__perror 61 #define nixio__pstatus_s nixio__pstatus 62 63 int nixio__check_group(lua_State *L, int idx); 64 int nixio__check_user(lua_State *L, int idx); 65 66 typedef struct stat nixio_stat_t; 67 68 #else /* __WINNT__ */ 69 70 #define NIXIO_API extern __declspec(dllexport) 71 #define NIXIO_SEP "\\" 72 #define NIXIO_PATHSEP ";" 73 #include "mingw-compat.h" 74 75 typedef struct _stati64 nixio_stat_t; 76 77 #endif 78 29 79 nixio_sock* nixio__checksock(lua_State *L); 30 80 int nixio__checksockfd(lua_State *L); 31 81 int nixio__checkfd(lua_State *L, int ud); 32 82 int nixio__tofd(lua_State *L, int ud); 83 int nixio__nulliter(lua_State *L); 84 85 int nixio__addr_parse(nixio_addr *addr, struct sockaddr *saddr); 86 int nixio__addr_write(nixio_addr *addr, struct sockaddr *saddr); 87 88 int nixio__check_mode(lua_State *L, int idx, int def); 89 int nixio__mode_write(int mode, char *modestr); 90 91 int nixio__push_stat(lua_State *L, nixio_stat_t *buf); 33 92 34 93 /* Module functions */ … … 43 102 void nixio_open_process(lua_State *L); 44 103 void nixio_open_syslog(lua_State *L); 104 void nixio_open_bit(lua_State *L); 105 void nixio_open_bin(lua_State *L); 106 void nixio_open_fs(lua_State *L); 107 void nixio_open_user(lua_State *L); 108 void nixio_open_tls_crypto(lua_State *L); 45 109 void nixio_open_tls_context(lua_State *L); 46 110 void nixio_open_tls_socket(lua_State *L); -
luci/trunk/libs/nixio/src/poll.c
r4334 r4437 18 18 19 19 #include "nixio.h" 20 #include <poll.h>21 20 #include <time.h> 22 21 #include <errno.h> 23 22 #include <string.h> 24 23 #include <stdlib.h> 25 #include "nixio.h"26 24 27 25 … … 72 70 lua_newtable(L); 73 71 nixio_poll_flags__r(L, &flags, POLLIN, "in"); 74 nixio_poll_flags__r(L, &flags, POLLPRI, "pri");75 72 nixio_poll_flags__r(L, &flags, POLLOUT, "out"); 76 73 nixio_poll_flags__r(L, &flags, POLLERR, "err"); 74 #ifndef __WINNT__ 75 nixio_poll_flags__r(L, &flags, POLLPRI, "pri"); 77 76 nixio_poll_flags__r(L, &flags, POLLHUP, "hup"); 78 77 nixio_poll_flags__r(L, &flags, POLLNVAL, "nval"); 78 #endif 79 79 } else { 80 80 flags = 0; … … 84 84 if (!strcmp(flag, "in")) { 85 85 flags |= POLLIN; 86 } else if (!strcmp(flag, "pri")) {87 flags |= POLLPRI;88 86 } else if (!strcmp(flag, "out")) { 89 87 flags |= POLLOUT; 90 88 } else if (!strcmp(flag, "err")) { 91 89 flags |= POLLERR; 90 } else if (!strcmp(flag, "pri")) { 91 #ifndef __WINNT__ 92 flags |= POLLPRI; 93 #endif 92 94 } else if (!strcmp(flag, "hup")) { 95 #ifndef __WINNT__ 93 96 flags |= POLLHUP; 97 #endif 94 98 } else if (!strcmp(flag, "nval")) { 99 #ifndef __WINNT__ 95 100 flags |= POLLNVAL; 101 #endif 96 102 } else { 97 103 return luaL_argerror(L, i, … … 115 121 /* we are being abused as sleep() replacement... */ 116 122 if (lua_isnoneornil(L, 1) || len < 1) { 117 return nixio__pstatus(L, !poll(NULL, 0, timeout)); 123 if (!poll(NULL, 0, timeout)) { 124 lua_pushinteger(L, 0); 125 return 1; 126 } else { 127 return nixio__perror(L); 128 } 118 129 } 119 130 120 131 luaL_checktype(L, 1, LUA_TTABLE); 121 132 struct pollfd *fds = calloc(len, sizeof(struct pollfd)); 133 if (!fds) { 134 return luaL_error(L, NIXIO_OOM); 135 } 122 136 123 137 for (i = 0; i < len; i++) { … … 146 160 status = poll(fds, (nfds_t)len, timeout); 147 161 148 if (status < 1) { 162 if (status == 0) { 163 free(fds); 164 lua_pushboolean(L, 0); 165 return 1; 166 } else if (status < 0) { 149 167 free(fds); 150 168 return nixio__perror(L); -
luci/trunk/libs/nixio/src/process.c
r4326 r4437 18 18 19 19 #include "nixio.h" 20 #include <pwd.h> 21 #include <grp.h> 20 #include <stdlib.h> 22 21 #include <unistd.h> 23 22 #include <errno.h> 24 23 #include <string.h> 25 #include <sys/ wait.h>24 #include <sys/stat.h> 26 25 #include <sys/types.h> 27 26 #include <signal.h> 27 28 #define NIXIO_EXECVE 0x01 29 #define NIXIO_EXECV 0x02 30 #define NIXIO_EXECVP 0x03 31 32 int nixio__exec(lua_State *L, int m) { 33 const char *path = luaL_checkstring(L, 1); 34 const char *arg; 35 int argn, i; 36 37 if (m == NIXIO_EXECVE) { 38 luaL_checktype(L, 2, LUA_TTABLE); 39 argn = lua_objlen(L, 2) + 1; 40 } else { 41 argn = lua_gettop(L); 42 } 43 44 char **args = lua_newuserdata(L, sizeof(char*) * (argn + 1)); 45 args[argn] = NULL; 46 args[0] = (char *)path; 47 48 if (m == NIXIO_EXECVE) { 49 for (i = 1; i < argn; i++) { 50 lua_rawgeti(L, 2, i); 51 arg = lua_tostring(L, -1); 52 luaL_argcheck(L, arg, 2, "invalid argument"); 53 args[i] = (char *)arg; 54 } 55 56 if (lua_isnoneornil(L, 3)) { 57 execv(path, args); 58 } else { 59 luaL_checktype(L, 3, LUA_TTABLE); 60 argn = 0; 61 lua_pushnil(L); 62 while (lua_next(L, 3)) { 63 if (!lua_checkstack(L, 1)) { 64 lua_settop(L, 0); 65 return luaL_error(L, "stack overflow"); 66 } 67 68 if (!lua_type(L, -2) != LUA_TSTRING || !lua_isstring(L, -1)) { 69 return luaL_argerror(L, 3, "invalid environment"); 70 } 71 72 lua_pushfstring(L, "%s=%s", 73 lua_tostring(L, -2), lua_tostring(L, -1)); 74 75 lua_insert(L, 4); 76 lua_pop(L, 1); 77 argn++; 78 } 79 80 char **env = lua_newuserdata(L, sizeof(char*) * (argn + 1)); 81 env[argn] = NULL; 82 83 for (i = 1; i < argn; i++) { 84 env[i-1] = (char *)lua_tostring(L, -i); 85 } 86 87 execve(path, args, env); 88 } 89 } else { 90 for (i = 2; i <= argn; i++) { 91 arg = luaL_checkstring(L, i); 92 args[i-1] = (char *)arg; 93 } 94 95 if (m == NIXIO_EXECV) { 96 execv(path, args); 97 } else { 98 execvp(path, args); 99 } 100 } 101 102 return nixio__perror(L); 103 } 104 105 #ifndef __WINNT__ 106 #include <sys/utsname.h> 107 #include <sys/times.h> 108 #include <sys/wait.h> 109 #include <pwd.h> 110 #include <grp.h> 28 111 29 112 static int nixio_fork(lua_State *L) { … … 37 120 } 38 121 39 static int nixio_signal(lua_State *L) { 40 int sig = luaL_checkinteger(L, 1); 41 const char *val = luaL_checkstring(L, 2); 42 43 if (!strcmp(val, "ign") || !strcmp(val, "ignore")) { 44 return nixio__pstatus(L, signal(sig, SIG_IGN) != SIG_ERR); 45 } else if (!strcmp(val, "dfl") || !strcmp(val, "default")) { 46 return nixio__pstatus(L, signal(sig, SIG_DFL) != SIG_ERR); 47 } else { 48 return luaL_argerror(L, 2, "supported values: ign, dfl"); 122 static int nixio_kill(lua_State *L) { 123 return nixio__pstatus(L, !kill(luaL_checkint(L, 1), luaL_checkint(L, 2))); 124 } 125 126 static int nixio_getppid(lua_State *L) { 127 lua_pushinteger(L, getppid()); 128 return 1; 129 } 130 131 static int nixio_getuid(lua_State *L) { 132 lua_pushinteger(L, getuid()); 133 return 1; 134 } 135 136 static int nixio_getgid(lua_State *L) { 137 lua_pushinteger(L, getgid()); 138 return 1; 139 } 140 141 static int nixio_setgid(lua_State *L) { 142 return nixio__pstatus(L, !setgid(nixio__check_group(L, 1))); 143 } 144 145 static int nixio_setuid(lua_State *L) { 146 return nixio__pstatus(L, !setuid(nixio__check_user(L, 1))); 147 } 148 149 static int nixio_nice(lua_State *L) { 150 int nval = luaL_checkint(L, 1); 151 152 errno = 0; 153 nval = nice(nval); 154 155 if (nval == -1 && errno) { 156 return nixio__perror(L); 157 } else { 158 lua_pushinteger(L, nval); 159 return 1; 160 } 161 } 162 163 static int nixio_setsid(lua_State *L) { 164 pid_t pid = setsid(); 165 166 if (pid == -1) { 167 return nixio__perror(L); 168 } else { 169 lua_pushinteger(L, pid); 170 return 1; 49 171 } 50 172 } … … 98 220 } 99 221 100 static int nixio_kill(lua_State *L) { 101 return nixio__pstatus(L, !kill(luaL_checkint(L, 1), luaL_checkint(L, 2))); 222 static int nixio_times(lua_State *L) { 223 struct tms buf; 224 if (times(&buf) == -1) { 225 return nixio__perror(L); 226 } else { 227 lua_createtable(L, 0, 4); 228 lua_pushnumber(L, buf.tms_cstime); 229 lua_setfield(L, -2, "cstime"); 230 231 lua_pushnumber(L, buf.tms_cutime); 232 lua_setfield(L, -2, "cutime"); 233 234 lua_pushnumber(L, buf.tms_stime); 235 lua_setfield(L, -2, "stime"); 236 237 lua_pushnumber(L, buf.tms_utime); 238 lua_setfield(L, -2, "utime"); 239 240 return 1; 241 } 242 } 243 244 static int nixio_uname(lua_State *L) { 245 struct utsname buf; 246 if (uname(&buf)) { 247 return nixio__perror(L); 248 } 249 250 lua_createtable(L, 0, 5); 251 252 lua_pushstring(L, buf.machine); 253 lua_setfield(L, -2, "machine"); 254 255 lua_pushstring(L, buf.version); 256 lua_setfield(L, -2, "version"); 257 258 lua_pushstring(L, buf.release); 259 lua_setfield(L, -2, "release"); 260 261 lua_pushstring(L, buf.nodename); 262 lua_setfield(L, -2, "nodename"); 263 264 lua_pushstring(L, buf.sysname); 265 lua_setfield(L, -2, "sysname"); 266 267 return 1; 268 } 269 270 #endif /* !__WINNT__ */ 271 272 static int nixio_chdir(lua_State *L) { 273 return nixio__pstatus(L, !chdir(luaL_checkstring(L, 1))); 274 } 275 276 static int nixio_signal(lua_State *L) { 277 int sig = luaL_checkinteger(L, 1); 278 const char *val = luaL_checkstring(L, 2); 279 280 if (!strcmp(val, "ign") || !strcmp(val, "ignore")) { 281 return nixio__pstatus(L, signal(sig, SIG_IGN) != SIG_ERR); 282 } else if (!strcmp(val, "dfl") || !strcmp(val, "default")) { 283 return nixio__pstatus(L, signal(sig, SIG_DFL) != SIG_ERR); 284 } else { 285 return luaL_argerror(L, 2, "supported values: ign, dfl"); 286 } 102 287 } 103 288 … … 107 292 } 108 293 109 static int nixio_getppid(lua_State *L) { 110 lua_pushinteger(L, getppid()); 111 return 1; 112 } 113 114 static int nixio_getuid(lua_State *L) { 115 lua_pushinteger(L, getuid()); 116 return 1; 117 } 118 119 static int nixio_getgid(lua_State *L) { 120 lua_pushinteger(L, getgid()); 121 return 1; 122 } 123 124 static int nixio_setgid(lua_State *L) { 125 gid_t gid; 126 if (lua_isstring(L, 1)) { 127 struct group *g = getgrnam(lua_tostring(L, 1)); 128 gid = (!g) ? -1 : g->gr_gid; 129 } else if (lua_isnumber(L, 1)) { 130 gid = lua_tointeger(L, 1); 131 } else { 132 return luaL_argerror(L, 1, "supported values: <groupname>, <gid>"); 133 } 134 135 return nixio__pstatus(L, !setgid(gid)); 136 } 137 138 static int nixio_setuid(lua_State *L) { 139 uid_t uid; 140 if (lua_isstring(L, 1)) { 141 struct passwd *p = getpwnam(lua_tostring(L, 1)); 142 uid = (!p) ? -1 : p->pw_uid; 143 } else if (lua_isnumber(L, 1)) { 144 uid = lua_tointeger(L, 1); 145 } else { 146 return luaL_argerror(L, 1, "supported values: <username>, <uid>"); 147 } 148 149 return nixio__pstatus(L, !setuid(uid)); 150 } 151 152 static int nixio_nice(lua_State *L) { 153 int nval = luaL_checkint(L, 1); 154 155 errno = 0; 156 nval = nice(nval); 157 158 if (nval == -1 && errno) { 159 return nixio__perror(L); 160 } else { 161 lua_pushinteger(L, nval); 162 return 1; 163 } 164 } 165 166 static int nixio_setsid(lua_State *L) { 167 pid_t pid = setsid(); 168 169 if (pid == -1) { 170 return nixio__perror(L); 171 } else { 172 lua_pushinteger(L, pid); 173 return 1; 174 } 175 } 176 177 static int nixio_chdir(lua_State *L) { 178 return nixio__pstatus(L, !chdir(luaL_checkstring(L, 1))); 179 } 294 static int nixio_getenv(lua_State *L) { 295 const char *key = luaL_optstring(L, 1, NULL); 296 if (key) { 297 const char *val = getenv(key); 298 if (val) { 299 lua_pushstring(L, val); 300 } else { 301 lua_pushnil(L); 302 } 303 } else { 304 lua_newtable(L); 305 extern char **environ; 306 for (char **c = environ; *c; c++) { 307 const char *delim = strchr(*c, '='); 308 if (!delim) { 309 return luaL_error(L, "invalid environment"); 310 } 311 lua_pushlstring(L, *c, delim-*c); 312 lua_pushstring(L, delim + 1); 313 lua_rawset(L, -3); 314 } 315 } 316 return 1; 317 } 318 319 static int nixio_setenv(lua_State *L) { 320 const char *key = luaL_checkstring(L, 1); 321 const char *val = luaL_optstring(L, 2, NULL); 322 return nixio__pstatus(L, (val) ? !setenv(key, val, 1) : !unsetenv(key)); 323 } 324 325 static int nixio_exec(lua_State *L) { 326 return nixio__exec(L, NIXIO_EXECV); 327 } 328 329 static int nixio_execp(lua_State *L) { 330 return nixio__exec(L, NIXIO_EXECVP); 331 } 332 333 static int nixio_exece(lua_State *L) { 334 return nixio__exec(L, NIXIO_EXECVE); 335 } 336 337 static int nixio_getcwd(lua_State *L) { 338 char path[PATH_MAX]; 339 340 if (getcwd(path, sizeof(path))) { 341 lua_pushstring(L, path); 342 return 1; 343 } else { 344 return nixio__perror(L); 345 } 346 } 347 348 static int nixio_umask(lua_State *L) { 349 char mask[9]; 350 lua_pushinteger(L, 351 nixio__mode_write(umask(nixio__check_mode(L, 1, -1)), mask)); 352 lua_pushlstring(L, mask, 9); 353 return 2; 354 } 355 356 #ifdef __linux__ 357 358 #include <sys/sysinfo.h> 359 360 static int nixio_sysinfo(lua_State *L) { 361 struct sysinfo info; 362 if (sysinfo(&info)) { 363 return nixio__perror(L); 364 } 365 366 lua_createtable(L, 0, 12); 367 368 lua_pushnumber(L, info.bufferram); 369 lua_setfield(L, -2, "bufferram"); 370 371 lua_pushnumber(L, info.freehigh); 372 lua_setfield(L, -2, "freehigh"); 373 374 lua_pushnumber(L, info.freeram); 375 lua_setfield(L, -2, "freeram"); 376 377 lua_pushnumber(L, info.freeswap); 378 lua_setfield(L, -2, "freeswap"); 379 380 lua_createtable(L, 0, 3); 381 for (int i=0; i<3; i++) { 382 lua_pushnumber(L, info.loads[i] / 65536.); 383 lua_rawseti(L, -2, i+1); 384 } 385 lua_setfield(L, -2, "loads"); 386 387 lua_pushnumber(L, info.mem_unit); 388 lua_setfield(L, -2, "mem_unit"); 389 390 lua_pushnumber(L, info.procs); 391 lua_setfield(L, -2, "procs"); 392 393 lua_pushnumber(L, info.sharedram); 394 lua_setfield(L, -2, "sharedram"); 395 396 lua_pushnumber(L, info.totalhigh); 397 lua_setfield(L, -2, "totalhigh"); 398 399 lua_pushnumber(L, info.totalram); 400 lua_setfield(L, -2, "totalram"); 401 402 lua_pushnumber(L, info.totalswap); 403 lua_setfield(L, -2, "totalswap"); 404 405 lua_pushnumber(L, info.uptime); 406 lua_setfield(L, -2, "uptime"); 407 408 return 1; 409 } 410 411 #endif 180 412 181 413 182 414 /* module table */ 183 415 static const luaL_reg R[] = { 416 #ifdef __linux__ 417 {"sysinfo", nixio_sysinfo}, 418 #endif 419 #ifndef __WINNT__ 184 420 {"fork", nixio_fork}, 185 {"wait", nixio_wait},186 421 {"kill", nixio_kill}, 187 422 {"nice", nixio_nice}, 188 {"chdir", nixio_chdir},189 {"getpid", nixio_getpid},190 423 {"getppid", nixio_getppid}, 191 424 {"getuid", nixio_getuid}, … … 194 427 {"setgid", nixio_setgid}, 195 428 {"setsid", nixio_setsid}, 429 {"wait", nixio_wait}, 430 {"waitpid", nixio_wait}, 431 {"times", nixio_times}, 432 {"uname", nixio_uname}, 433 #endif 434 {"chdir", nixio_chdir}, 196 435 {"signal", nixio_signal}, 436 {"getpid", nixio_getpid}, 437 {"getenv", nixio_getenv}, 438 {"setenv", nixio_setenv}, 439 {"putenv", nixio_setenv}, 440 {"exec", nixio_exec}, 441 {"execp", nixio_execp}, 442 {"exece", nixio_exece}, 443 {"getcwd", nixio_getcwd}, 444 {"umask", nixio_umask}, 197 445 {NULL, NULL} 198 446 }; -
luci/trunk/libs/nixio/src/socket.c
r4287 r4437 18 18 19 19 #include "nixio.h" 20 #include <sys/socket.h>21 #include <netinet/in.h>22 20 #include <unistd.h> 23 21 #include <string.h> 24 22 #include <errno.h> 25 #include "nixio.h"26 23 27 24 … … 80 77 81 78 if (sock->fd < 0) { 82 return nixio__perror (L);79 return nixio__perror_s(L); 83 80 } 84 81 … … 96 93 97 94 do { 95 #ifndef __WINNT__ 98 96 res = close(sockfd); 97 #else 98 res = closesocket(sockfd); 99 #endif 99 100 } while (res == -1 && errno == EINTR); 100 101 101 return nixio__pstatus (L, !res);102 return nixio__pstatus_s(L, !res); 102 103 } 103 104 … … 110 111 if (sock && sock->fd != -1) { 111 112 do { 113 #ifndef __WINNT__ 112 114 res = close(sock->fd); 115 #else 116 res = closesocket(sock->fd); 117 #endif 113 118 } while (res == -1 && errno == EINTR); 114 119 } … … 142 147 } 143 148 144 return nixio__pstatus (L, !shutdown(sockfd, how));149 return nixio__pstatus_s(L, !shutdown(sockfd, how)); 145 150 } 146 151 -
luci/trunk/libs/nixio/src/sockopt.c
r4328 r4437 18 18 19 19 #include "nixio.h" 20 20 21 #include <sys/types.h> 21 #include <sys/socket.h>22 #include <netinet/in.h>23 #include <netinet/tcp.h>24 #include <net/if.h>25 22 #include <sys/time.h> 26 23 #include <string.h> 27 24 #include <fcntl.h> 28 25 #include <errno.h> 29 #include "nixio.h" 30 26 27 #ifndef IPV6_ADD_MEMBERSHIP 28 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP 29 #endif 30 31 #ifndef IPV6_DROP_MEMBERSHIP 32 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP 33 #endif 34 35 static int nixio_sock_fileno(lua_State *L) { 36 lua_pushinteger(L, nixio__checkfd(L, 1)); 37 return 1; 38 } 31 39 32 40 /** … … 37 45 luaL_checkany(L, 2); 38 46 int set = lua_toboolean(L, 2); 47 48 #ifndef __WINNT__ 49 39 50 int flags = fcntl(fd, F_GETFL); 40 51 … … 50 61 51 62 return nixio__pstatus(L, !fcntl(fd, F_SETFL, flags)); 63 64 #else /* __WINNT__ */ 65 66 lua_getmetatable(L, 1); 67 luaL_getmetatable(L, NIXIO_META); 68 if (lua_equal(L, -1, -2)) { /* Socket */ 69 unsigned long val = !set; 70 return nixio__pstatus_s(L, !ioctlsocket(fd, FIONBIO, &val)); 71 } else { /* File */ 72 WSASetLastError(WSAENOTSOCK); 73 return nixio__perror_s(L); 74 } 75 76 #endif /* __WINNT__ */ 52 77 } 53 78 … … 56 81 socklen_t optlen = sizeof(value); 57 82 if (!set) { 58 if (!getsockopt(fd, level, opt, &value, &optlen)) {83 if (!getsockopt(fd, level, opt, (char *)&value, &optlen)) { 59 84 lua_pushinteger(L, value); 60 85 return 1; … … 62 87 } else { 63 88 value = luaL_checkinteger(L, set); 64 if (!setsockopt(fd, level, opt, &value, optlen)) {65 lua_pushboolean(L, 1); 66 return 1; 67 } 68 } 69 return nixio__perror (L);89 if (!setsockopt(fd, level, opt, (char *)&value, optlen)) { 90 lua_pushboolean(L, 1); 91 return 1; 92 } 93 } 94 return nixio__perror_s(L); 70 95 } 71 96 … … 74 99 socklen_t optlen = sizeof(value); 75 100 if (!set) { 76 if (!getsockopt(fd, level, opt, &value, &optlen)) {101 if (!getsockopt(fd, level, opt, (char *)&value, &optlen)) { 77 102 lua_pushinteger(L, value.l_onoff ? value.l_linger : 0); 78 103 return 1; … … 81 106 value.l_linger = luaL_checkinteger(L, set); 82 107 value.l_onoff = value.l_linger ? 1 : 0; 83 if (!setsockopt(fd, level, opt, &value, optlen)) {84 lua_pushboolean(L, 1); 85 return 1; 86 } 87 } 88 return nixio__perror (L);108 if (!setsockopt(fd, level, opt, (char *)&value, optlen)) { 109 lua_pushboolean(L, 1); 110 return 1; 111 } 112 } 113 return nixio__perror_s(L); 89 114 } 90 115 … … 93 118 socklen_t optlen = sizeof(value); 94 119 if (!set) { 95 if (!getsockopt(fd, level, opt, &value, &optlen)) {120 if (!getsockopt(fd, level, opt, (char *)&value, &optlen)) { 96 121 lua_pushinteger(L, value.tv_sec); 97 122 lua_pushinteger(L, value.tv_usec); … … 101 126 value.tv_sec = luaL_checkinteger(L, set); 102 127 value.tv_usec = luaL_optinteger(L, set + 1, 0); 103 if (!setsockopt(fd, level, opt, &value, optlen)) {104 lua_pushboolean(L, 1); 105 return 1; 106 } 107 } 108 return nixio__perror (L);128 if (!setsockopt(fd, level, opt, (char *)&value, optlen)) { 129 lua_pushboolean(L, 1); 130 return 1; 131 } 132 } 133 return nixio__perror_s(L); 109 134 } 110 135 … … 115 140 socklen_t optlen = IFNAMSIZ; 116 141 char ifname[IFNAMSIZ]; 117 if (!getsockopt(fd, level, opt, ifname, &optlen)) {142 if (!getsockopt(fd, level, opt, (char *)ifname, &optlen)) { 118 143 lua_pushlstring(L, ifname, optlen); 119 144 return 1; … … 123 148 const char *value = luaL_checklstring(L, set, &valuelen); 124 149 luaL_argcheck(L, valuelen <= IFNAMSIZ, set, "invalid interface name"); 125 if (!setsockopt(fd, level, opt, value, valuelen)) {126 lua_pushboolean(L, 1); 127 return 1; 128 } 129 } 130 return nixio__perror (L);150 if (!setsockopt(fd, level, opt, (char *)value, valuelen)) { 151 lua_pushboolean(L, 1); 152 return 1; 153 } 154 } 155 return nixio__perror_s(L); 131 156 } 132 157 133 158 #endif /* SO_BINDTODEVICE */ 159 160 static int nixio__gso_mreq4(lua_State *L, int fd, int level, int opt, int set) { 161 struct ip_mreq value; 162 socklen_t optlen = sizeof(value); 163 if (!set) { 164 char buf[INET_ADDRSTRLEN]; 165 if (!getsockopt(fd, level, opt, (char *)&value, &optlen)) { 166 if (!inet_ntop(AF_INET, &value.imr_multiaddr, buf, sizeof(buf))) { 167 return nixio__perror_s(L); 168 } 169 lua_pushstring(L, buf); 170 if (!inet_ntop(AF_INET, &value.imr_interface, buf, sizeof(buf))) { 171 return nixio__perror_s(L); 172 } 173 lua_pushstring(L, buf); 174 return 2; 175 } 176 } else { 177 const char *maddr = luaL_checkstring(L, set); 178 const char *iface = luaL_optstring(L, set + 1, "0.0.0.0"); 179 if (inet_pton(AF_INET, maddr, &value.imr_multiaddr) < 1) { 180 return nixio__perror_s(L); 181 } 182 if (inet_pton(AF_INET, iface, &value.imr_interface) < 1) { 183 return nixio__perror_s(L); 184 } 185 if (!setsockopt(fd, level, opt, (char *)&value, optlen)) { 186 lua_pushboolean(L, 1); 187 return 1; 188 } 189 } 190 return nixio__perror_s(L); 191 } 192 193 static int nixio__gso_mreq6(lua_State *L, int fd, int level, int opt, int set) { 194 struct ipv6_mreq val; 195 socklen_t optlen = sizeof(val); 196 if (!set) { 197 char buf[INET_ADDRSTRLEN]; 198 if (!getsockopt(fd, level, opt, (char *)&val, &optlen)) { 199 if (!inet_ntop(AF_INET6, &val.ipv6mr_multiaddr, buf, sizeof(buf))) { 200 return nixio__perror_s(L); 201 } 202 lua_pushstring(L, buf); 203 lua_pushnumber(L, val.ipv6mr_interface); 204 return 2; 205 } 206 } else { 207 const char *maddr = luaL_checkstring(L, set); 208 if (inet_pton(AF_INET6, maddr, &val.ipv6mr_multiaddr) < 1) { 209 return nixio__perror_s(L); 210 } 211 val.ipv6mr_interface = luaL_optlong(L, set + 1, 0); 212 if (!setsockopt(fd, level, opt, (char *)&val, optlen)) { 213 lua_pushboolean(L, 1); 214 return 1; 215 } 216 } 217 return nixio__perror_s(L); 218 } 134 219 135 220 /** … … 184 269 } 185 270 } else if (!strcmp(level, "tcp")) { 186 if (sock->type != SOCK_STREAM) {187 return luaL_error(L, "not a TCP socket");188 }189 271 if (!strcmp(option, "cork")) { 190 272 #ifdef TCP_CORK … … 198 280 return luaL_argerror(L, 3, "supported values: cork, nodelay"); 199 281 } 200 } else { 201 return luaL_argerror(L, 2, "supported values: socket, tcp"); 282 } else if (!strcmp(level, "ip")) { 283 if (!strcmp(option, "mtu")) { 284 #ifdef IP_MTU 285 return nixio__gso_int(L, sock->fd, IPPROTO_IP, IP_MTU, set); 286 #else 287 return nixio__pstatus(L, !(errno = ENOPROTOOPT)); 288 #endif 289 } else if (!strcmp(option, "hdrincl")) { 290 return nixio__gso_int(L, sock->fd, IPPROTO_IP, IP_HDRINCL, 291 set); 292 } else if (!strcmp(option, "multicast_loop")) { 293 return nixio__gso_int(L, sock->fd, IPPROTO_IP, IP_MULTICAST_LOOP, 294 set); 295 } else if (!strcmp(option, "multicast_ttl")) { 296 return nixio__gso_int(L, sock->fd, IPPROTO_IP, IP_MULTICAST_TTL, 297 set); 298 } else if (!strcmp(option, "multicast_if")) { 299 return nixio__gso_mreq4(L, sock->fd, IPPROTO_IP, IP_MULTICAST_IF, 300 set); 301 } else if (!strcmp(option, "add_membership")) { 302 return nixio__gso_mreq4(L, sock->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, 303 set); 304 } else if (!strcmp(option, "drop_membership")) { 305 return nixio__gso_mreq4(L, sock->fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, 306 set); 307 } else { 308 return luaL_argerror(L, 3, 309 "supported values: hdrincl, mtu, multicast_loop, " 310 "multicast_ttl, multicast_if, add_membership, drop_membership"); 311 } 312 } else if (!strcmp(level, "ipv6")) { 313 if (!strcmp(option, "mtu")) { 314 #ifdef IPV6_MTU 315 return nixio__gso_int(L, sock->fd, IPPROTO_IPV6, IPV6_MTU, set); 316 #else 317 return nixio__pstatus(L, !(errno = ENOPROTOOPT)); 318 #endif 319 } else if (!strcmp(option, "v6only")) { 320 #ifdef IPV6_V6ONLY 321 return nixio__gso_int(L, sock->fd, IPPROTO_IPV6, IPV6_V6ONLY, set); 322 #else 323 return nixio__pstatus(L, !(errno = ENOPROTOOPT)); 324 #endif 325 } else if (!strcmp(option, "multicast_loop")) { 326 return nixio__gso_int(L, sock->fd, IPPROTO_IPV6, 327 IPV6_MULTICAST_LOOP, set); 328 } else if (!strcmp(option, "multicast_hops")) { 329 return nixio__gso_int(L, sock->fd, IPPROTO_IPV6, 330 IPV6_MULTICAST_HOPS, set); 331 } else if (!strcmp(option, "multicast_if")) { 332 return nixio__gso_mreq6(L, sock->fd, IPPROTO_IPV6, 333 IPV6_MULTICAST_IF, set); 334 } else if (!strcmp(option, "add_membership")) { 335 return nixio__gso_mreq6(L, sock->fd, IPPROTO_IPV6, 336 IPV6_ADD_MEMBERSHIP, set); 337 } else if (!strcmp(option, "drop_membership")) { 338 return nixio__gso_mreq6(L, sock->fd, IPPROTO_IPV6, 339 IPV6_DROP_MEMBERSHIP, set); 340 } else { 341 return luaL_argerror(L, 3, 342 "supported values: v6only, mtu, multicast_loop, multicast_hops," 343 " multicast_if, add_membership, drop_membership"); 344 } 345 } else { 346 return luaL_argerror(L, 2, "supported values: socket, tcp, ip, ipv6"); 202 347 } 203 348 } … … 222 367 {"getsockopt", nixio_sock_getsockopt}, 223 368 {"setsockopt", nixio_sock_setsockopt}, 369 {"getopt", nixio_sock_getsockopt}, 370 {"setopt", nixio_sock_setsockopt}, 371 {"fileno", nixio_sock_fileno}, 224 372 {NULL, NULL} 225 373 }; … … 233 381 lua_pushcfunction(L, nixio_sock_setblocking); 234 382 lua_setfield(L, -2, "setblocking"); 383 lua_pushcfunction(L, nixio_sock_fileno); 384 lua_setfield(L, -2, "fileno"); 235 385 lua_pop(L, 1); 236 386 } -
luci/trunk/libs/nixio/src/splice.c
r4323 r4437 27 27 #include <unistd.h> 28 28 #include <sys/param.h> 29 30 31 #ifndef __WINNT__ 29 32 30 33 #ifndef BSD … … 151 154 }; 152 155 156 153 157 void nixio_open_splice(lua_State *L) { 154 158 luaL_register(L, NULL, R); 155 159 } 160 161 #else /* __WINNT__ */ 162 163 void nixio_open_splice(lua_State *L) { 164 } 165 166 #endif /* !__WINNT__ */ -
luci/trunk/libs/nixio/src/syslog.c
r4337 r4437 19 19 #include "nixio.h" 20 20 #include <string.h> 21 22 #ifndef __WINNT__ 21 23 #include <syslog.h> 22 24 … … 58 60 } 59 61 60 static int nixio__syslogmas g(lua_State *L, int dolog) {62 static int nixio__syslogmask(lua_State *L, int dolog) { 61 63 int priority; 62 64 … … 93 95 94 96 static int nixio_setlogmask(lua_State *L) { 95 return nixio__syslogmas g(L, 0);97 return nixio__syslogmask(L, 0); 96 98 } 97 99 98 100 static int nixio_syslog(lua_State *L) { 99 return nixio__syslogmas g(L, 1);101 return nixio__syslogmask(L, 1); 100 102 } 101 103 … … 112 114 luaL_register(L, NULL, R); 113 115 } 116 117 #else /* __WINNT__ */ 118 119 void nixio_open_syslog(lua_State *L) { 120 } 121 122 #endif /* __WINNT__ */ -
luci/trunk/libs/nixio/src/tls-socket.c
r4311 r4437 1 /*1 /* 2 2 * nixio - Linux I/O library for lua 3 3 * … … 66 66 SSL *sock = nixio__checktlssock(L); 67 67 nixio_tls__check_connected(L); 68 int req = luaL_checkinteger(L, 2);68 uint req = luaL_checkinteger(L, 2); 69 69 70 70 luaL_argcheck(L, req >= 0, 2, "out of range"); … … 173 173 ssize_t sent; 174 174 const char *data = luaL_checklstring(L, 2, &len); 175 176 if (lua_gettop(L) > 2) { 177 int offset = luaL_optint(L, 3, 0); 178 if (offset) { 179 if (offset < len) { 180 data += offset; 181 len -= offset; 182 } else { 183 len = 0; 184 } 185 } 186 187 unsigned int wlen = luaL_optint(L, 4, len); 188 if (wlen < len) { 189 len = wlen; 190 } 191 } 192 175 193 sent = SSL_write(sock, data, len); 176 194 if (sent > 0) {
