Changeset 4437

Show
Ignore:
Timestamp:
04/21/09 17:26:45 (4 years ago)
Author:
Cyrus
Message:

Merge nixio 0.2

Location:
luci/trunk/libs/nixio
Files:
27 added
17 modified

Legend:

Unmodified
Added
Removed
  • luci/trunk/libs/nixio/lua/nixio/util.lua

    r4301 r4437  
    1919module "nixio.util" 
    2020 
    21 local BUFFERSIZE = 8096 
     21local BUFFERSIZE = nixio.const.buffersize 
     22local ZIOBLKSIZE = 65536 
    2223local socket = nixio.meta_socket 
    2324local tls_socket = nixio.meta_tls_socket 
    2425local file = nixio.meta_file 
    2526 
     27function consume(iter) 
     28    local tbl = {} 
     29    for obj in iter do 
     30        tbl[#tbl+1] = obj 
     31    end 
     32    return tbl 
     33end 
     34 
    2635local meta = {} 
    2736 
     
    3948 
    4049function meta.readall(self, len) 
    41     local block, code, msg = self:read(len) 
     50    local block, code, msg = self:read(len or BUFFERSIZE) 
    4251 
    4352    if not block then 
    44         return "", code, msg, len 
     53        return nil, code, msg, "" 
    4554    elseif #block == 0 then 
    46         return "", nil, nil, len 
     55        return "", nil, nil, "" 
    4756    end 
    4857 
    4958    local data, total = {block}, #block 
    5059 
    51     while len > total do 
    52         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) 
    5362 
    5463        if not block then 
    55             return data, code, msg, len - #data 
     64            return nil, code, msg, table.concat(data) 
    5665        elseif #block == 0 then 
    57             return data, nil, nil, len - #data 
     66            break 
    5867        end 
    5968 
     
    6170    end 
    6271 
    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 
    6474end 
    6575meta.recvall = meta.readall 
    6676 
    6777function meta.writeall(self, data) 
    68     local total, block = 0 
    6978    local sent, code, msg = self:write(data) 
    7079 
    7180    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 
    7989        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 
    8294    end 
    8395     
    84     return total + sent, nil, nil, "" 
     96    return total, nil, nil, total 
    8597end 
    8698meta.sendall = meta.writeall 
     
    106118                return line 
    107119            elseif #buffer < limit + bpos then 
    108                 local newblock, code = self:read(limit + bpos - #buffer) 
     120                local newblock, code, msg = self:read(limit + bpos - #buffer) 
    109121                if not newblock then 
    110                     return nil, code 
     122                    return nil, code, msg 
    111123                elseif #newblock == 0 then 
    112124                    return nil 
     
    136148 
    137149        if not block then 
    138             return nil, code 
     150            return nil, code, msg 
    139151        elseif #block == 0 then 
    140152            return nil 
     
    163175end 
    164176 
     177function 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 
     188end 
     189 
     190function 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) 
     209end 
     210 
    165211function tls_socket.close(self) 
    166212    return self.socket:close() 
  • luci/trunk/libs/nixio/Makefile

    r4334 r4437  
     1ifneq (,$(wildcard ../../build/config.mk)) 
    12include ../../build/config.mk 
    23include ../../build/module.mk 
    34include ../../build/gccconfig.mk 
     5else 
     6include standalone.mk 
     7endif 
    48 
    59AXTLS_VERSION = 1.2.1 
     
    711AXTLS_FILE    = $(AXTLS_DIR)-$(AXTLS_VERSION).tar.gz 
    812NIXIO_TLS    ?= openssl 
    9 NIXIO_LDFLAGS = 
     13NIXIO_LDFLAGS = -lcrypt 
     14NIXIO_SO      = nixio.so 
    1015 
    1116NIXIO_OBJ = src/nixio.o src/socket.o src/sockopt.o src/bind.o src/address.o \ 
    1217        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 
    1420 
    1521ifeq ($(NIXIO_TLS),axtls) 
    16     TLS_CFLAGS = -IaxTLS/ssl -IaxTLS/crypto -IaxTLS/config -include src/openssl-compat.h 
    17     TLS_DEPENDS = src/openssl-compat.o 
    18     NIXIO_OBJ += src/openssl-compat.o src/libaxtls.a 
     22    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 
    1925endif 
    2026 
     
    2834endif 
    2935 
     36ifneq (,$(findstring MINGW,$(OS))$(findstring mingw,$(OS))$(findstring Windows,$(OS))) 
     37    NIXIO_CROSS_CC:=$(shell which i586-mingw32msvc-cc) 
     38ifneq (,$(NIXIO_CROSS_CC)) 
     39    CC:=$(NIXIO_CROSS_CC) 
     40endif 
     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:= 
     48endif 
     49 
    3050 
    3151%.o: %.c 
    3252    $(COMPILE) $(NIXIO_CFLAGS) $(LUA_CFLAGS) $(FPIC) -c -o $@ $<  
     53 
     54src/tls-crypto.o: $(TLS_DEPENDS) src/tls-crypto.c 
     55    $(COMPILE) $(NIXIO_CFLAGS) $(LUA_CFLAGS) $(FPIC) $(TLS_CFLAGS) -c -o $@ src/tls-crypto.c 
    3356 
    3457src/tls-context.o: $(TLS_DEPENDS) src/tls-context.c 
     
    3861    $(COMPILE) $(NIXIO_CFLAGS) $(LUA_CFLAGS) $(FPIC) $(TLS_CFLAGS) -c -o $@ src/tls-socket.c 
    3962     
    40 src/openssl-compat.o: src/libaxtls.a src/openssl-compat.c 
    41     $(COMPILE) $(NIXIO_CFLAGS) $(LUA_CFLAGS) $(FPIC) $(TLS_CFLAGS) -c -o $@ src/openssl-compat.c 
     63src/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 
    4265    mkdir -p dist 
    4366    cp -pR axtls-root/* dist/ 
     
    4568 
    4669compile: $(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) 
    4871    mkdir -p dist$(LUA_LIBRARYDIR) 
    49     cp src/nixio.so dist$(LUA_LIBRARYDIR)/nixio.so 
     72    cp src/$(NIXIO_SO) dist$(LUA_LIBRARYDIR)/$(NIXIO_SO) 
    5073 
    5174$(AXTLS_DIR)/.prepared: 
     
    5679 
    5780src/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 all 
     81    $(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 
    5982    cp -p $(AXTLS_DIR)/_stage/libaxtls.a src 
    6083 
    6184clean: luaclean 
    62     rm -f src/*.o src/*.so src/*.a 
     85    rm -f src/*.o src/*.so src/*.a src/*.dll 
    6386    rm -f $(AXTLS_DIR)/.prepared 
     87 
     88install: build 
     89    cp -pR dist$(LUA_MODULEDIR)/* $(LUA_MODULEDIR) 
     90    cp -pR dist$(LUA_LIBRARYDIR)/* $(LUA_LIBRARYDIR) 
  • luci/trunk/libs/nixio/README

    r4287 r4437  
    11Building: 
     2    Use GNU Make. 
     3    make or gmake depending on your system. 
     4     
     5    Special make flags: 
    26 
    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  
    1919#include "nixio.h" 
    2020#include <sys/types.h> 
    21 #include <sys/socket.h> 
    22 #include <arpa/inet.h> 
    23 #include <netinet/in.h> 
     21#include <errno.h> 
    2422#include <string.h> 
    25 #include <netdb.h> 
    2623 
    2724#ifndef NI_MAXHOST 
    2825#define NI_MAXHOST 1025 
    2926#endif 
     27 
     28/** 
     29 * address pushing helper 
     30 */ 
     31int 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 */ 
     58int 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} 
    3093 
    3194 
     
    71134    for (rp = result; rp != NULL; rp = rp->ai_next) { 
    72135        /* avoid duplicate results */ 
     136#ifndef __WINNT__ 
    73137        if (!port && rp->ai_socktype != SOCK_STREAM) { 
    74138            continue; 
    75139        } 
     140#endif 
    76141 
    77142        if (rp->ai_family == AF_INET || rp->ai_family == AF_INET6) { 
     
    102167            } 
    103168 
    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)) { 
    119171                freeaddrinfo(result); 
    120                 return nixio__perror(L); 
     172                return nixio__perror_s(L); 
    121173            } 
    122174 
    123175            if (port) { 
    124                 lua_pushinteger(L, ntohs(binport)); 
     176                lua_pushinteger(L, addr.port); 
    125177                lua_setfield(L, -2, "port"); 
    126178            } 
    127179 
    128             lua_pushstring(L, ip); 
     180            lua_pushstring(L, addr.host); 
    129181            lua_setfield(L, -2, "address"); 
    130182            lua_rawseti(L, -2, i++); 
     
    141193 */ 
    142194static int nixio_getnameinfo(lua_State *L) { 
    143     const char *ip = luaL_checklstring(L, 1, NULL); 
    144     const char *family = luaL_optlstring(L, 2, "inet", NULL); 
     195    const char *ip = luaL_checkstring(L, 1); 
     196    const char *family = luaL_optstring(L, 2, NULL); 
    145197    char host[NI_MAXHOST]; 
    146198 
    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; 
    160208    } 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; 
    169210    } else { 
    170211        return luaL_argerror(L, 2, "supported values: inet, inet6"); 
    171212    } 
    172213 
    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); 
    174218    if (res) { 
    175219        lua_pushnil(L); 
     
    184228 
    185229/** 
    186  * getsockname() / getpeername() helper 
    187  */ 
    188 static int nixio_sock__getname(lua_State *L, int sock) { 
     230 * getsockname() 
     231 */ 
     232static int nixio_sock_getsockname(lua_State *L) { 
    189233    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); 
    224245    return 2; 
    225246} 
    226247 
    227248/** 
    228  * getsockname() 
    229  */ 
    230 static int nixio_sock_getsockname(lua_State *L) { 
    231     return nixio_sock__getname(L, 1); 
    232 } 
    233  
    234 /** 
    235249 * getpeername() 
    236250 */ 
    237251static 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; 
    239265} 
    240266 
  • luci/trunk/libs/nixio/src/bind.c

    r4322 r4437  
    1919#include "nixio.h" 
    2020#include <sys/types.h> 
    21 #include <sys/socket.h> 
    22 #include <arpa/inet.h> 
    23 #include <netinet/in.h> 
    24 #include <sys/un.h> 
    2521#include <string.h> 
    2622#include <unistd.h> 
    27 #include <netdb.h> 
    2823#include <errno.h> 
     24 
    2925 
    3026/** 
     
    8884 
    8985        if (do_bind) { 
     86            int one = 1; 
     87            setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, 
     88             (char*)&one, sizeof(one)); 
    9089            status = bind(sock->fd, rp->ai_addr, rp->ai_addrlen); 
    9190        } else { 
     
    104103 
    105104        do { 
     105#ifndef __WINNT__ 
    106106            clstat = close(sock->fd); 
     107#else 
     108            clstat = closesocket(sock->fd); 
     109#endif 
    107110        } while (clstat == -1 && errno == EINTR); 
    108111    } 
     
    112115    /* on failure */ 
    113116    if (status) { 
    114         return nixio__perror(L); 
     117        return nixio__perror_s(L); 
    115118    } 
    116119 
     
    184187 
    185188        freeaddrinfo(result); 
     189#ifndef __WINNT__ 
    186190    } else if (sock->domain == AF_UNIX) { 
    187191        size_t pathlen; 
     
    201205            } while (status == -1 && errno == EINTR); 
    202206        } 
     207#endif 
    203208    } else { 
    204209        return luaL_error(L, "not supported"); 
    205210    } 
    206     return nixio__pstatus(L, !status); 
     211    return nixio__pstatus_s(L, !status); 
    207212} 
    208213 
     
    226231static int nixio_sock_listen(lua_State *L) { 
    227232    int sockfd = nixio__checksockfd(L); 
    228     lua_Integer backlog = 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)); 
    230235} 
    231236 
     
    235240static int nixio_sock_accept(lua_State *L) { 
    236241    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); 
    242245    int newfd; 
    243246 
    244247    do { 
    245         newfd = accept(sock->fd, (struct sockaddr *)&addr, &addrlen); 
     248        newfd = accept(sock->fd, (struct sockaddr *)&saddr, &saddrlen); 
    246249    } while (newfd == -1 && errno == EINTR); 
    247250    if (newfd < 0) { 
    248         return nixio__perror(L); 
     251        return nixio__perror_s(L); 
    249252    } 
    250253 
     
    257260    clsock->fd = newfd; 
    258261 
    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    } 
    278269} 
    279270 
  • luci/trunk/libs/nixio/src/file.c

    r4322 r4437  
    3131static int nixio_open(lua_State *L) { 
    3232    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 
    3560    int fd; 
    3661 
    3762    do { 
    38         fd = open(filename, flags, mode); 
     63        fd = open(filename, flags, nixio__check_mode(L, 3, 0666)); 
    3964    } while (fd == -1 && errno == EINTR); 
    4065    if (fd == -1) { 
     
    6792            mode |= O_EXCL; 
    6893        } else if (!strcmp(flag, "nonblock") || !strcmp(flag, "ndelay")) { 
     94#ifndef __WINNT__ 
    6995            mode |= O_NONBLOCK; 
     96#endif 
    7097        } else if (!strcmp(flag, "sync")) { 
     98#ifndef __WINNT__ 
    7199            mode |= O_SYNC; 
     100#endif 
    72101        } else if (!strcmp(flag, "trunc")) { 
    73102            mode |= O_TRUNC; 
     
    142171    const char *data = luaL_checklstring(L, 2, &len); 
    143172 
     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 
    144190    do { 
    145191        sent = write(fd, data, len); 
     
    156202    int fd = nixio__checkfd(L, 1); 
    157203    char buffer[NIXIO_BUFFERSIZE]; 
    158     int req = luaL_checkinteger(L, 2); 
     204    uint req = luaL_checkinteger(L, 2); 
    159205    int readc; 
    160206 
     
    209255} 
    210256 
     257static 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 
    211270static int nixio_file_sync(lua_State *L) { 
    212271    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); 
    216279#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); 
    218284#endif 
    219285} 
     
    283349    {"tell",        nixio_file_tell}, 
    284350    {"seek",        nixio_file_seek}, 
     351    {"stat",        nixio_file_stat}, 
    285352    {"sync",        nixio_file_sync}, 
    286353    {"lock",        nixio_file_lock}, 
     
    307374    lua_pushvalue(L, -1); 
    308375    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"); 
    309397    lua_setfield(L, -2, "meta_file"); 
    310398} 
  • luci/trunk/libs/nixio/src/io.c

    r4322 r4437  
    1919#include "nixio.h" 
    2020#include <errno.h> 
     21#include <string.h> 
    2122#include <stdlib.h> 
    2223#include <stdio.h> 
    2324#include <sys/types.h> 
    24 #include <sys/socket.h> 
    25 #include <arpa/inet.h> 
    26 #include <netinet/in.h> 
    2725 
    2826 
     
    3432    struct sockaddr *addr = NULL; 
    3533    socklen_t alen = 0; 
     34    int argoff = 2; 
    3635 
    3736    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); 
    4039        struct sockaddr_storage addrstor; 
    4140        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); 
    5850        } 
    5951    } 
     
    6254    ssize_t sent; 
    6355    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 
    6474    do { 
    6575        sent = sendto(sock->fd, data, len, 0, addr, alen); 
     
    6979        return 1; 
    7080    } else { 
    71         return nixio__perror(L); 
     81        return nixio__perror_s(L); 
    7282    } 
    7383} 
     
    95105    char buffer[NIXIO_BUFFERSIZE]; 
    96106    struct sockaddr_storage addrobj; 
    97     int req = luaL_checkinteger(L, 2); 
     107    uint req = luaL_checkinteger(L, 2); 
    98108    int readc; 
    99109 
     
    112122    } while (readc == -1 && errno == EINTR); 
    113123 
     124#ifdef __WINNT__ 
    114125    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); 
    116135    } else { 
    117136        lua_pushlstring(L, buffer, readc); 
     
    120139            return 1; 
    121140        } 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; 
    134146            } else { 
    135                 return luaL_error(L, "unknown address family"); 
     147                return 1; 
    136148            } 
    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; 
    146149        } 
    147150    } 
  • luci/trunk/libs/nixio/src/nixio-tls.h

    r4311 r4437  
    33 
    44#include "nixio.h" 
     5#include <sys/types.h> 
    56 
    67#ifndef WITHOUT_OPENSSL 
    78#include <openssl/ssl.h> 
     9#include <openssl/md5.h> 
     10#include <openssl/sha.h> 
    811#endif 
    912 
     
    2124} nixio_tls_sock; 
    2225 
     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 
     36typedef int(*nixio_hash_initcb)(void *); 
     37typedef int(*nixio_hash_updatecb)(void *, const void *, unsigned long); 
     38typedef int(*nixio_hash_finalcb)(unsigned char *, void *); 
     39 
     40typedef 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 
    2353#endif /* NIXIO_TLS_H_ */ 
  • luci/trunk/libs/nixio/src/nixio.c

    r4334 r4437  
    2828/* pushes nil, error number and errstring on the stack */ 
    2929int nixio__perror(lua_State *L) { 
    30     if (errno == EAGAIN) { 
     30    if (errno == EAGAIN || errno == EWOULDBLOCK) { 
    3131        lua_pushboolean(L, 0); 
    3232    } else { 
     
    8686} 
    8787 
     88/* An empty iterator */ 
     89int nixio__nulliter(lua_State *L) { 
     90    lua_pushnil(L); 
     91    return 1; 
     92} 
     93 
     94static int nixio_errno(lua_State *L) { 
     95    lua_pushinteger(L, errno); 
     96    return 1; 
     97} 
     98 
    8899static int nixio_strerror(lua_State *L) { 
    89100    lua_pushstring(L, strerror(luaL_checkinteger(L, 1))); 
     
    93104/* object table */ 
    94105static const luaL_reg R[] = { 
     106    {"errno",       nixio_errno}, 
    95107    {"strerror",    nixio_strerror}, 
    96108    {NULL,          NULL} 
     
    98110 
    99111/* entry point */ 
    100 LUALIB_API int luaopen_nixio(lua_State *L) { 
     112NIXIO_API int luaopen_nixio(lua_State *L) { 
    101113    /* create metatable */ 
    102114    luaL_newmetatable(L, NIXIO_META); 
     
    114126 
    115127    /* register methods */ 
     128#ifdef __WINNT__ 
     129    nixio_open__mingw(L); 
     130#endif 
    116131    nixio_open_file(L); 
    117132    nixio_open_socket(L); 
     
    124139    nixio_open_process(L); 
    125140    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); 
    126146    nixio_open_tls_context(L); 
    127147    nixio_open_tls_socket(L); 
     
    132152 
    133153    /* 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"); 
    135164 
    136165    NIXIO_PUSH_CONSTANT(EACCES); 
     
    138167    NIXIO_PUSH_CONSTANT(ENOSYS); 
    139168    NIXIO_PUSH_CONSTANT(EINVAL); 
    140     NIXIO_PUSH_CONSTANT(EWOULDBLOCK); 
    141169    NIXIO_PUSH_CONSTANT(EAGAIN); 
    142170    NIXIO_PUSH_CONSTANT(ENOMEM); 
     
    153181    NIXIO_PUSH_CONSTANT(EPERM); 
    154182    NIXIO_PUSH_CONSTANT(EEXIST); 
    155     NIXIO_PUSH_CONSTANT(ELOOP); 
    156183    NIXIO_PUSH_CONSTANT(EMFILE); 
    157184    NIXIO_PUSH_CONSTANT(ENAMETOOLONG); 
    158185    NIXIO_PUSH_CONSTANT(ENFILE); 
    159186    NIXIO_PUSH_CONSTANT(ENODEV); 
     187    NIXIO_PUSH_CONSTANT(EXDEV); 
    160188    NIXIO_PUSH_CONSTANT(ENOTDIR); 
    161189    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); 
    162200    NIXIO_PUSH_CONSTANT(EOVERFLOW); 
    163     NIXIO_PUSH_CONSTANT(EROFS); 
    164201    NIXIO_PUSH_CONSTANT(ETXTBSY); 
    165202    NIXIO_PUSH_CONSTANT(EAFNOSUPPORT); 
     
    167204    NIXIO_PUSH_CONSTANT(EPROTONOSUPPORT); 
    168205    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 
    171210    NIXIO_PUSH_CONSTANT(SIGALRM); 
    172     NIXIO_PUSH_CONSTANT(SIGINT); 
    173     NIXIO_PUSH_CONSTANT(SIGTERM); 
    174211    NIXIO_PUSH_CONSTANT(SIGKILL); 
    175212    NIXIO_PUSH_CONSTANT(SIGHUP); 
    176213    NIXIO_PUSH_CONSTANT(SIGSTOP); 
    177214    NIXIO_PUSH_CONSTANT(SIGCONT); 
    178     NIXIO_PUSH_CONSTANT(SIGSEGV); 
    179215    NIXIO_PUSH_CONSTANT(SIGCHLD); 
    180216    NIXIO_PUSH_CONSTANT(SIGQUIT); 
     
    183219    NIXIO_PUSH_CONSTANT(SIGIO); 
    184220    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__ */ 
    186228    lua_setfield(L, -2, "const"); 
    187229 
  • luci/trunk/libs/nixio/src/nixio.h

    r4334 r4437  
    22#define NIXIO_H_ 
    33 
     4#define NIXIO_OOM "out of memory" 
     5 
    46#define NIXIO_META "nixio.socket" 
    57#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" 
    710#define _FILE_OFFSET_BITS 64 
    811 
     
    1821#include <lauxlib.h> 
    1922 
     23#define NIXIO_BUFFERSIZE 8192 
     24 
    2025typedef struct nixio_socket { 
    2126    int fd; 
     
    2530} nixio_sock; 
    2631 
     32typedef struct nixio_address { 
     33    int family; 
     34    char host[128]; 
     35    int port; 
     36} nixio_addr; 
     37 
    2738int nixio__perror(lua_State *L); 
    2839int 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 
     63int nixio__check_group(lua_State *L, int idx); 
     64int nixio__check_user(lua_State *L, int idx); 
     65 
     66typedef 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 
     75typedef struct _stati64 nixio_stat_t; 
     76 
     77#endif 
     78 
    2979nixio_sock* nixio__checksock(lua_State *L); 
    3080int nixio__checksockfd(lua_State *L); 
    3181int nixio__checkfd(lua_State *L, int ud); 
    3282int nixio__tofd(lua_State *L, int ud); 
     83int nixio__nulliter(lua_State *L); 
     84 
     85int nixio__addr_parse(nixio_addr *addr, struct sockaddr *saddr); 
     86int nixio__addr_write(nixio_addr *addr, struct sockaddr *saddr); 
     87 
     88int nixio__check_mode(lua_State *L, int idx, int def); 
     89int nixio__mode_write(int mode, char *modestr); 
     90 
     91int nixio__push_stat(lua_State *L, nixio_stat_t *buf); 
    3392 
    3493/* Module functions */ 
     
    43102void nixio_open_process(lua_State *L); 
    44103void nixio_open_syslog(lua_State *L); 
     104void nixio_open_bit(lua_State *L); 
     105void nixio_open_bin(lua_State *L); 
     106void nixio_open_fs(lua_State *L); 
     107void nixio_open_user(lua_State *L); 
     108void nixio_open_tls_crypto(lua_State *L); 
    45109void nixio_open_tls_context(lua_State *L); 
    46110void nixio_open_tls_socket(lua_State *L); 
  • luci/trunk/libs/nixio/src/poll.c

    r4334 r4437  
    1818 
    1919#include "nixio.h" 
    20 #include <poll.h> 
    2120#include <time.h> 
    2221#include <errno.h> 
    2322#include <string.h> 
    2423#include <stdlib.h> 
    25 #include "nixio.h" 
    2624 
    2725 
     
    7270        lua_newtable(L); 
    7371        nixio_poll_flags__r(L, &flags, POLLIN, "in"); 
    74         nixio_poll_flags__r(L, &flags, POLLPRI, "pri"); 
    7572        nixio_poll_flags__r(L, &flags, POLLOUT, "out"); 
    7673        nixio_poll_flags__r(L, &flags, POLLERR, "err"); 
     74#ifndef __WINNT__ 
     75        nixio_poll_flags__r(L, &flags, POLLPRI, "pri"); 
    7776        nixio_poll_flags__r(L, &flags, POLLHUP, "hup"); 
    7877        nixio_poll_flags__r(L, &flags, POLLNVAL, "nval"); 
     78#endif 
    7979     } else { 
    8080        flags = 0; 
     
    8484            if (!strcmp(flag, "in")) { 
    8585                flags |= POLLIN; 
    86             } else if (!strcmp(flag, "pri")) { 
    87                 flags |= POLLPRI; 
    8886            } else if (!strcmp(flag, "out")) { 
    8987                flags |= POLLOUT; 
    9088            } else if (!strcmp(flag, "err")) { 
    9189                flags |= POLLERR; 
     90            } else if (!strcmp(flag, "pri")) { 
     91#ifndef __WINNT__ 
     92                flags |= POLLPRI; 
     93#endif 
    9294            } else if (!strcmp(flag, "hup")) { 
     95#ifndef __WINNT__ 
    9396                flags |= POLLHUP; 
     97#endif 
    9498            } else if (!strcmp(flag, "nval")) { 
     99#ifndef __WINNT__ 
    95100                flags |= POLLNVAL; 
     101#endif 
    96102            } else { 
    97103                return luaL_argerror(L, i, 
     
    115121    /* we are being abused as sleep() replacement... */ 
    116122    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        } 
    118129    } 
    119130 
    120131    luaL_checktype(L, 1, LUA_TTABLE); 
    121132    struct pollfd *fds = calloc(len, sizeof(struct pollfd)); 
     133    if (!fds) { 
     134        return luaL_error(L, NIXIO_OOM); 
     135    } 
    122136 
    123137    for (i = 0; i < len; i++) { 
     
    146160    status = poll(fds, (nfds_t)len, timeout); 
    147161 
    148     if (status < 1) { 
     162    if (status == 0) { 
     163        free(fds); 
     164        lua_pushboolean(L, 0); 
     165        return 1; 
     166    } else if (status < 0) { 
    149167        free(fds); 
    150168        return nixio__perror(L); 
  • luci/trunk/libs/nixio/src/process.c

    r4326 r4437  
    1818 
    1919#include "nixio.h" 
    20 #include <pwd.h> 
    21 #include <grp.h> 
     20#include <stdlib.h> 
    2221#include <unistd.h> 
    2322#include <errno.h> 
    2423#include <string.h> 
    25 #include <sys/wait.h> 
     24#include <sys/stat.h> 
    2625#include <sys/types.h> 
    2726#include <signal.h> 
     27 
     28#define NIXIO_EXECVE    0x01 
     29#define NIXIO_EXECV     0x02 
     30#define NIXIO_EXECVP    0x03 
     31 
     32int 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> 
    28111 
    29112static int nixio_fork(lua_State *L) { 
     
    37120} 
    38121 
    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"); 
     122static int nixio_kill(lua_State *L) { 
     123    return nixio__pstatus(L, !kill(luaL_checkint(L, 1), luaL_checkint(L, 2))); 
     124} 
     125 
     126static int nixio_getppid(lua_State *L) { 
     127    lua_pushinteger(L, getppid()); 
     128    return 1; 
     129} 
     130 
     131static int nixio_getuid(lua_State *L) { 
     132    lua_pushinteger(L, getuid()); 
     133    return 1; 
     134} 
     135 
     136static int nixio_getgid(lua_State *L) { 
     137    lua_pushinteger(L, getgid()); 
     138    return 1; 
     139} 
     140 
     141static int nixio_setgid(lua_State *L) { 
     142    return nixio__pstatus(L, !setgid(nixio__check_group(L, 1))); 
     143} 
     144 
     145static int nixio_setuid(lua_State *L) { 
     146    return nixio__pstatus(L, !setuid(nixio__check_user(L, 1))); 
     147} 
     148 
     149static 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 
     163static 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; 
    49171    } 
    50172} 
     
    98220} 
    99221 
    100 static int nixio_kill(lua_State *L) { 
    101     return nixio__pstatus(L, !kill(luaL_checkint(L, 1), luaL_checkint(L, 2))); 
     222static 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 
     244static 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 
     272static int nixio_chdir(lua_State *L) { 
     273    return nixio__pstatus(L, !chdir(luaL_checkstring(L, 1))); 
     274} 
     275 
     276static 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    } 
    102287} 
    103288 
     
    107292} 
    108293 
    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 } 
     294static 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 
     319static 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 
     325static int nixio_exec(lua_State *L) { 
     326    return nixio__exec(L, NIXIO_EXECV); 
     327} 
     328 
     329static int nixio_execp(lua_State *L) { 
     330    return nixio__exec(L, NIXIO_EXECVP); 
     331} 
     332 
     333static int nixio_exece(lua_State *L) { 
     334    return nixio__exec(L, NIXIO_EXECVE); 
     335} 
     336 
     337static 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 
     348static 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 
     360static 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 
    180412 
    181413 
    182414/* module table */ 
    183415static const luaL_reg R[] = { 
     416#ifdef __linux__ 
     417    {"sysinfo",     nixio_sysinfo}, 
     418#endif 
     419#ifndef __WINNT__ 
    184420    {"fork",        nixio_fork}, 
    185     {"wait",        nixio_wait}, 
    186421    {"kill",        nixio_kill}, 
    187422    {"nice",        nixio_nice}, 
    188     {"chdir",       nixio_chdir}, 
    189     {"getpid",      nixio_getpid}, 
    190423    {"getppid",     nixio_getppid}, 
    191424    {"getuid",      nixio_getuid}, 
     
    194427    {"setgid",      nixio_setgid}, 
    195428    {"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}, 
    196435    {"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}, 
    197445    {NULL,          NULL} 
    198446}; 
  • luci/trunk/libs/nixio/src/socket.c

    r4287 r4437  
    1818 
    1919#include "nixio.h" 
    20 #include <sys/socket.h> 
    21 #include <netinet/in.h> 
    2220#include <unistd.h> 
    2321#include <string.h> 
    2422#include <errno.h> 
    25 #include "nixio.h" 
    2623 
    2724 
     
    8077 
    8178    if (sock->fd < 0) { 
    82         return nixio__perror(L); 
     79        return nixio__perror_s(L); 
    8380    } 
    8481 
     
    9693 
    9794    do { 
     95#ifndef __WINNT__ 
    9896        res = close(sockfd); 
     97#else 
     98        res = closesocket(sockfd); 
     99#endif 
    99100    } while (res == -1 && errno == EINTR); 
    100101 
    101     return nixio__pstatus(L, !res); 
     102    return nixio__pstatus_s(L, !res); 
    102103} 
    103104 
     
    110111    if (sock && sock->fd != -1) { 
    111112        do { 
     113#ifndef __WINNT__ 
    112114            res = close(sock->fd); 
     115#else 
     116            res = closesocket(sock->fd); 
     117#endif 
    113118        } while (res == -1 && errno == EINTR); 
    114119    } 
     
    142147    } 
    143148 
    144     return nixio__pstatus(L, !shutdown(sockfd, how)); 
     149    return nixio__pstatus_s(L, !shutdown(sockfd, how)); 
    145150} 
    146151 
  • luci/trunk/libs/nixio/src/sockopt.c

    r4328 r4437  
    1818 
    1919#include "nixio.h" 
     20 
    2021#include <sys/types.h> 
    21 #include <sys/socket.h> 
    22 #include <netinet/in.h> 
    23 #include <netinet/tcp.h> 
    24 #include <net/if.h> 
    2522#include <sys/time.h> 
    2623#include <string.h> 
    2724#include <fcntl.h> 
    2825#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 
     35static int nixio_sock_fileno(lua_State *L) { 
     36    lua_pushinteger(L, nixio__checkfd(L, 1)); 
     37    return 1; 
     38} 
    3139 
    3240/** 
     
    3745    luaL_checkany(L, 2); 
    3846    int set = lua_toboolean(L, 2); 
     47 
     48#ifndef __WINNT__ 
     49 
    3950    int flags = fcntl(fd, F_GETFL); 
    4051 
     
    5061 
    5162    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__ */ 
    5277} 
    5378 
     
    5681    socklen_t optlen = sizeof(value); 
    5782    if (!set) { 
    58         if (!getsockopt(fd, level, opt, &value, &optlen)) { 
     83        if (!getsockopt(fd, level, opt, (char *)&value, &optlen)) { 
    5984            lua_pushinteger(L, value); 
    6085            return 1; 
     
    6287    } else { 
    6388        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); 
    7095} 
    7196 
     
    7499    socklen_t optlen = sizeof(value); 
    75100    if (!set) { 
    76         if (!getsockopt(fd, level, opt, &value, &optlen)) { 
     101        if (!getsockopt(fd, level, opt, (char *)&value, &optlen)) { 
    77102            lua_pushinteger(L, value.l_onoff ? value.l_linger : 0); 
    78103            return 1; 
     
    81106        value.l_linger = luaL_checkinteger(L, set); 
    82107        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); 
    89114} 
    90115 
     
    93118    socklen_t optlen = sizeof(value); 
    94119    if (!set) { 
    95         if (!getsockopt(fd, level, opt, &value, &optlen)) { 
     120        if (!getsockopt(fd, level, opt, (char *)&value, &optlen)) { 
    96121            lua_pushinteger(L, value.tv_sec); 
    97122            lua_pushinteger(L, value.tv_usec); 
     
    101126        value.tv_sec  = luaL_checkinteger(L, set); 
    102127        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); 
    109134} 
    110135 
     
    115140        socklen_t optlen = IFNAMSIZ; 
    116141        char ifname[IFNAMSIZ]; 
    117         if (!getsockopt(fd, level, opt, ifname, &optlen)) { 
     142        if (!getsockopt(fd, level, opt, (char *)ifname, &optlen)) { 
    118143            lua_pushlstring(L, ifname, optlen); 
    119144            return 1; 
     
    123148        const char *value = luaL_checklstring(L, set, &valuelen); 
    124149        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); 
    131156} 
    132157 
    133158#endif /* SO_BINDTODEVICE */ 
     159 
     160static 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 
     193static 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} 
    134219 
    135220/** 
     
    184269        } 
    185270    } else if (!strcmp(level, "tcp")) { 
    186         if (sock->type != SOCK_STREAM) { 
    187             return luaL_error(L, "not a TCP socket"); 
    188         } 
    189271        if (!strcmp(option, "cork")) { 
    190272#ifdef TCP_CORK 
     
    198280            return luaL_argerror(L, 3, "supported values: cork, nodelay"); 
    199281        } 
    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"); 
    202347    } 
    203348} 
     
    222367    {"getsockopt",  nixio_sock_getsockopt}, 
    223368    {"setsockopt",  nixio_sock_setsockopt}, 
     369    {"getopt",      nixio_sock_getsockopt}, 
     370    {"setopt",      nixio_sock_setsockopt}, 
     371    {"fileno",      nixio_sock_fileno}, 
    224372    {NULL,          NULL} 
    225373}; 
     
    233381    lua_pushcfunction(L, nixio_sock_setblocking); 
    234382    lua_setfield(L, -2, "setblocking"); 
     383    lua_pushcfunction(L, nixio_sock_fileno); 
     384    lua_setfield(L, -2, "fileno"); 
    235385    lua_pop(L, 1); 
    236386} 
  • luci/trunk/libs/nixio/src/splice.c

    r4323 r4437  
    2727#include <unistd.h> 
    2828#include <sys/param.h> 
     29 
     30 
     31#ifndef __WINNT__ 
    2932 
    3033#ifndef BSD 
     
    151154}; 
    152155 
     156 
    153157void nixio_open_splice(lua_State *L) { 
    154158    luaL_register(L, NULL, R); 
    155159} 
     160 
     161#else /* __WINNT__ */ 
     162 
     163void nixio_open_splice(lua_State *L) { 
     164} 
     165 
     166#endif /* !__WINNT__ */ 
  • luci/trunk/libs/nixio/src/syslog.c

    r4337 r4437  
    1919#include "nixio.h" 
    2020#include <string.h> 
     21 
     22#ifndef __WINNT__ 
    2123#include <syslog.h> 
    2224 
     
    5860} 
    5961 
    60 static int nixio__syslogmasg(lua_State *L, int dolog) { 
     62static int nixio__syslogmask(lua_State *L, int dolog) { 
    6163    int priority; 
    6264 
     
    9395 
    9496static int nixio_setlogmask(lua_State *L) { 
    95     return nixio__syslogmasg(L, 0); 
     97    return nixio__syslogmask(L, 0); 
    9698} 
    9799 
    98100static int nixio_syslog(lua_State *L) { 
    99     return nixio__syslogmasg(L, 1); 
     101    return nixio__syslogmask(L, 1); 
    100102} 
    101103 
     
    112114    luaL_register(L, NULL, R); 
    113115} 
     116 
     117#else /* __WINNT__ */ 
     118 
     119void nixio_open_syslog(lua_State *L) { 
     120} 
     121 
     122#endif /* __WINNT__ */ 
  • luci/trunk/libs/nixio/src/tls-socket.c

    r4311 r4437  
    1 /* 
     1 /* 
    22 * nixio - Linux I/O library for lua 
    33 * 
     
    6666    SSL *sock = nixio__checktlssock(L); 
    6767    nixio_tls__check_connected(L); 
    68     int req = luaL_checkinteger(L, 2); 
     68    uint req = luaL_checkinteger(L, 2); 
    6969 
    7070    luaL_argcheck(L, req >= 0, 2, "out of range"); 
     
    173173    ssize_t sent; 
    174174    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 
    175193    sent = SSL_write(sock, data, len); 
    176194    if (sent > 0) {