Changeset 6724

Show
Ignore:
Timestamp:
01/14/11 00:26:19 (2 years ago)
Author:
jow
Message:

libs/nixio: fix sendto(), implement support for unix domain sockets (#140)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • luci/trunk/libs/nixio/src/io.c

    r5414 r6724  
    3030static int nixio_sock__sendto(lua_State *L, int to) { 
    3131    nixio_sock *sock = nixio__checksock(L); 
     32    struct sockaddr_storage addr_in; 
     33#ifndef __WINNT__ 
     34    struct sockaddr_un addr_un; 
     35#endif 
    3236    struct sockaddr *addr = NULL; 
    3337    socklen_t alen = 0; 
     
    3640    if (to) { 
    3741        argoff += 2; 
    38         const char *address = luaL_checkstring(L, 3); 
    39         struct sockaddr_storage addrstor; 
    40         addr = (struct sockaddr*)&addrstor; 
    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); 
    50         } 
     42        if (sock->domain == AF_INET || sock->domain == AF_INET6) { 
     43            const char *address = luaL_checkstring(L, 3); 
     44            addr = (struct sockaddr*)&addr_in; 
     45            alen = sizeof(addr_in); 
     46 
     47            nixio_addr naddr; 
     48            memset(&naddr, 0, sizeof(naddr)); 
     49            strncpy(naddr.host, address, sizeof(naddr.host) - 1); 
     50            naddr.port = (uint16_t)luaL_checkinteger(L, 4); 
     51            naddr.family = sock->domain; 
     52 
     53            if (nixio__addr_write(&naddr, addr)) { 
     54                return nixio__perror_s(L); 
     55            } 
     56        } 
     57 
     58#ifndef __WINNT__ 
     59        else if (sock->domain == AF_UNIX) { 
     60            size_t pathlen; 
     61            const char *path = luaL_checklstring(L, 3, &pathlen); 
     62 
     63            addr_un.sun_family = AF_UNIX; 
     64            luaL_argcheck(L, pathlen < sizeof(addr_un.sun_path), 3, "out of range"); 
     65            strncpy(addr_un.sun_path, path, sizeof(addr_un.sun_path)); 
     66 
     67            addr = (struct sockaddr*)&addr_un; 
     68            alen = sizeof(addr_un); 
     69        } 
     70#endif 
    5171    } 
    5272 
     
    104124    nixio_sock *sock = nixio__checksock(L); 
    105125    char buffer[NIXIO_BUFFERSIZE]; 
    106     struct sockaddr_storage addrobj; 
     126    struct sockaddr_storage addr_in; 
     127#ifndef __WINNT__ 
     128    struct sockaddr_un addr_un; 
     129#endif 
     130    struct sockaddr *addr = NULL; 
     131    socklen_t alen = 0; 
    107132    uint req = luaL_checkinteger(L, 2); 
    108133    int readc; 
    109134 
    110     if (from && sock->domain != AF_INET && sock->domain != AF_INET6) { 
    111         return luaL_argerror(L, 1, "supported families: inet, inet6"); 
    112     } 
    113  
    114     struct sockaddr *addr = (from) ? (struct sockaddr*)&addrobj : NULL; 
    115     socklen_t alen = (from) ? sizeof(addrobj) : 0; 
     135    if (sock->domain == AF_INET || sock->domain == AF_INET6) { 
     136        addr = (from) ? (struct sockaddr*)&addr_in : NULL; 
     137        alen = (from) ? sizeof(addr_in) : 0; 
     138    } 
     139#ifndef __WINNT__ 
     140    else if (sock->domain == AF_UNIX) { 
     141        addr = (from) ? (struct sockaddr*)&addr_un : NULL; 
     142        alen = (from) ? sizeof(addr_un) : 0; 
     143    } 
     144#endif 
    116145 
    117146    /* We limit the readsize to NIXIO_BUFFERSIZE */ 
     
    138167        if (!from) { 
    139168            return 1; 
    140         } else { 
     169        } 
     170        /* push address. */ 
     171        if (sock->domain == AF_INET || sock->domain == AF_INET6) { 
    141172            nixio_addr naddr; 
    142             if (!nixio__addr_parse(&naddr, (struct sockaddr *)&addrobj)) { 
     173            if (!nixio__addr_parse(&naddr, (struct sockaddr *)&addr_in)) { 
    143174                lua_pushstring(L, naddr.host); 
    144175                lua_pushinteger(L, naddr.port); 
     
    148179            } 
    149180        } 
    150     } 
     181#ifndef __WINNT__ 
     182        else if (sock->domain == AF_UNIX) { 
     183            lua_pushstring(L, addr_un.sun_path); 
     184            return 2; 
     185        } 
     186#endif 
     187    } 
     188    return 1; 
    151189} 
    152190