Changeset 6193

Show
Ignore:
Timestamp:
05/31/10 14:45:13 (3 years ago)
Author:
Cyrus
Message:

Merge socket code to usock, fix regression from ulog and ubox hash changes

Location:
luci2
Files:
17 modified

Legend:

Unmodified
Added
Removed
  • luci2/cbi2/cbi.h

    r6006 r6193  
    1212#include <lmo.h> 
    1313#include "lang.h" 
     14#include <libubox/hash.h> 
    1415 
    1516#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 
  • luci2/cbi2/luci_cli.c

    r5791 r6193  
    66#include <sys/types.h> 
    77#include <sys/socket.h> 
    8 #include <sys/un.h> 
     8#include <libubox/usock.h> 
    99 
    1010#define SOCK_PATH "/tmp/luci2-socket" 
     
    1616#define MAX_SOCKET_DATA 512 
    1717    int s, len; 
    18     struct sockaddr_un remote; 
    1918    char reply[4096]; 
    2019    if(argc != 2) 
    2120        return 1; 
    22     if((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) 
    23     { 
    24         perror("socket"); 
    25         exit(1); 
    26     } 
     21 
    2722    printf("Trying to connect...\n"); 
    28  
    29     remote.sun_family = AF_UNIX; 
    30     strcpy(remote.sun_path, SOCK_PATH); 
    31     len = strlen(remote.sun_path) + sizeof(remote.sun_family); 
    32     if(connect(s, (struct sockaddr *)&remote, len) == -1) 
     23    if((s = usock(USOCK_UNIX | USOCK_TCP, SOCK_PATH, NULL)) == -1) 
    3324    { 
    3425        perror("connect"); 
  • luci2/cbi2/Makefile

    r6187 r6193  
    33CFLAGS+= --std=gnu99 -Wall -Werror -pedantic -I/usr/include/lua5.1/ 
    44LDFLAGS?= 
    5 INCLUDES?=-I../libubox/  -Ilmo/ 
     5INCLUDES?=-I../libubox/  -Ilmo/ -I../ 
    66BINARY=libcbi.so luci luci_cli uvl uvlc libcbi.a luci_conf widgets/luci/_builtin.so widgets/config/_builtin.so sources/_builtin.so validator/_builtin.so 
    77 
     
    3030 
    3131luci: luci.c 
    32     $(CC) $(CFLAGS) $(LDFLAGS) -g -o $@ $^ -lcbi -I../libubox/ -Ilmo/ -L. 
     32    $(CC) $(CFLAGS) $(LDFLAGS) -g -o $@ $^ -lcbi $(INCLUDES) -L. 
    3333 
    3434luci_conf: luci_conf.c 
    35     $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ -lcbi -I../libubox/ -Ilmo/ -L. 
     35    $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ -lcbi $(INCLUDES) -L. 
    3636 
    3737libcbi.a: lucic.o parser.o json.o cbi_element.o cbi_dump.o cbi_core.o cbi_buffer.o cbi_uci.o source.o session.o u_sock.o validate.o uvl.o lang.o widget.o log.o handler.o  
     
    4343 
    4444luci_cli: luci_cli.o 
    45     $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ 
     45    $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ -lubox 
    4646 
    4747uvl: uvl.o uvl-cli.o 
  • luci2/cbi2/u_sock.c

    r5816 r6193  
    88#include <sys/types.h> 
    99#include <sys/socket.h> 
     10#include <libubox/usock.h> 
    1011 
    1112#include "list.h" 
     
    4647void u_sock_init(struct cbi_ctx *ctx, const char *path) 
    4748{ 
    48     int s, len; 
    49     struct sockaddr_un local; 
    50  
     49    int s; 
    5150    INIT_LIST_HEAD(&ctx->sock_handlers); 
    5251 
    53     if((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) 
     52    path = (path)?(path):(U_SOCK_PATH); 
     53    unlink(path); 
     54    if((s = usock(USOCK_TCP | USOCK_UNIX | USOCK_SERVER, path, NULL)) == -1) 
    5455    { 
    5556        perror("socket"); 
     
    5758    } 
    5859 
    59     local.sun_family = AF_UNIX; 
    60     strcpy(local.sun_path, (path)?(path):(U_SOCK_PATH)); 
    61     unlink(local.sun_path); 
    62     len = strlen(local.sun_path) + sizeof(local.sun_family); 
    63     if(bind(s, (struct sockaddr *)&local, len) == -1) 
    64     { 
    65         close(s); 
    66         perror("bind"); 
    67         exit(1); 
    68     } 
    69  
    70     if(listen(s, 5) == -1) 
    71     { 
    72         perror("listen"); 
    73         exit(1); 
    74     } 
    7560    ctx->u_sock = s; 
    7661    printf("Unix socket loaded %s\n", (path)?(path):(U_SOCK_PATH)); 
  • luci2/cbi2/widgets/luci/content.c

    r6112 r6193  
    2121{ 
    2222    if(e->value) 
    23         e->lmo = sfh_hash(e->value, strlen(e->value)); 
     23        e->lmo = hash_murmur2(e->value, strlen(e->value)); 
    2424    return 0; 
    2525} 
  • luci2/cbi2/widgets/luci/menu.c

    r5781 r6193  
    1313    if(!tag) 
    1414        tag = cbi_prop_find(e, "id"); 
    15     e->lmo = sfh_hash(tag, strlen(tag)); 
     15    e->lmo = hash_murmur2(tag, strlen(tag)); 
    1616    return 0; 
    1717} 
     
    5454        return 1; 
    5555    } 
    56     e->lmo = sfh_hash(e->value, strlen(e->value)); 
     56    e->lmo = hash_murmur2(e->value, strlen(e->value)); 
    5757    return 0; 
    5858} 
  • luci2/cbi2/widgets/luci/option.c

    r5780 r6193  
    1010{ 
    1111    if(e->value) 
    12         e->lmo = sfh_hash(e->value, strlen(e->value)); 
     12        e->lmo = hash_murmur2(e->value, strlen(e->value)); 
    1313    return 0; 
    1414} 
  • luci2/libubox/ulog.h

    r6190 r6193  
    2626#define LOG     log_printf 
    2727#define log_start(name, daemon) \ 
    28     openlog(name, (daemon) ? 0 : (LOG_PERROR | LOG_CONS), LOG_USER) 
    29 #define log_printf(...) syslog(10, __VA_ARGS__) 
     28    openlog(name, (LOG_PERROR | LOG_CONS), LOG_USER) 
     29#define log_printf(...) syslog(LOG_NOTICE, __VA_ARGS__) 
    3030 
    3131#endif 
  • luci2/libubox/usock.c

    r6192 r6193  
    1414    int sock = -1; 
    1515 
    16     if (service && !(type & USOCK_FLAG_UNIX)) { 
     16    if (service && !(type & USOCK_UNIX)) { 
    1717        struct addrinfo *result, *rp; 
    1818 
    1919        struct addrinfo hints = { 
    20             .ai_family = (type & USOCK_FLAG_IPV6ONLY) ? AF_INET6 : 
    21                 (type & USOCK_FLAG_IPV4ONLY) ? AF_INET : AF_UNSPEC, 
     20            .ai_family = (type & USOCK_IPV6ONLY) ? AF_INET6 : 
     21                (type & USOCK_IPV4ONLY) ? AF_INET : AF_UNSPEC, 
    2222            .ai_socktype = ((type & 0xff) == USOCK_TCP) 
    2323                ? SOCK_STREAM : SOCK_DGRAM, 
    2424            .ai_flags = AI_ADDRCONFIG | 
    25                 ((type & USOCK_FLAG_SERVER) ? AI_PASSIVE : 0), 
     25                ((type & USOCK_SERVER) ? AI_PASSIVE : 0), 
    2626        }; 
    2727 
     
    3636            } 
    3737 
    38             if (!(type & USOCK_FLAG_NOCLOEXEC)) { 
     38            if (!(type & USOCK_NOCLOEXEC)) { 
    3939                fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC); 
    4040            } 
    4141 
    42             if (type & USOCK_FLAG_NONBLOCK) { 
     42            if (type & USOCK_NONBLOCK) { 
    4343                fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK); 
    4444            } 
    4545 
    46             if (type & USOCK_FLAG_SERVER) { 
     46            if (type & USOCK_SERVER) { 
    4747                const int one = 1; 
    4848                setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); 
     
    7676        } 
    7777 
    78         if (!(type & USOCK_FLAG_NOCLOEXEC)) { 
     78        if (!(type & USOCK_NOCLOEXEC)) { 
    7979            fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC); 
    8080        } 
    8181 
    82         if (type & USOCK_FLAG_NONBLOCK) { 
     82        if (type & USOCK_NONBLOCK) { 
    8383            fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK); 
    8484        } 
    8585 
    86         if (type & USOCK_FLAG_SERVER) { 
     86        if (type & USOCK_SERVER) { 
    8787            if (bind(sock, (struct sockaddr*)&sun, sizeof(sun)) || 
    8888            ((type & 0xff) == USOCK_TCP && listen(sock, SOMAXCONN))) { 
  • luci2/libubox/usock.h

    r6192 r6193  
    22#define USOCK_H_ 
    33 
    4 #define USOCK_TCP   1 
    5 #define USOCK_UDP   2 
     4#define USOCK_TCP 0 
     5#define USOCK_UDP 1 
    66 
    7 #define USOCK_FLAG_SERVER       0x0100 
    8 #define USOCK_FLAG_NOCLOEXEC    0x0200 
    9 #define USOCK_FLAG_NONBLOCK     0x0400 
    10 #define USOCK_FLAG_IPV6ONLY     0x2000 
    11 #define USOCK_FLAG_IPV4ONLY     0x4000 
    12 #define USOCK_FLAG_UNIX         0x8000 
     7#define USOCK_SERVER        0x0100 
     8#define USOCK_NOCLOEXEC 0x0200 
     9#define USOCK_NONBLOCK      0x0400 
     10#define USOCK_IPV6ONLY      0x2000 
     11#define USOCK_IPV4ONLY      0x4000 
     12#define USOCK_UNIX          0x8000 
    1313 
    1414int usock(int type, const char *host, const char *service); 
  • luci2/libustream/core.c

    r6160 r6193  
    88#include <stdlib.h> 
    99#include <string.h> 
     10#include <libubox/usock.h> 
    1011 
    1112static int ustream_io_timeout = 60; 
     
    5960 
    6061USTREAM_LOCAL int ustream_io_socket (const char *host, const char *service) { 
    61     struct addrinfo *result, *rp; 
    62     int sock = -1, clstat; 
    63  
    64     struct addrinfo hints = { 
    65         .ai_family = AF_UNSPEC, 
    66         .ai_socktype = SOCK_STREAM 
    67     }; 
    68  
    69     if (getaddrinfo(host, service, &hints, &result)) { 
    70         errno = ENXIO; 
    71         return -1; 
    72     } 
    73  
    74     for (rp = result; rp != NULL; rp = rp->ai_next) { 
    75         sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); 
    76         if (sock == -1) { 
    77             continue; 
    78         } 
    79  
    80         if (!connect(sock, rp->ai_addr, rp->ai_addrlen)) { 
    81             break; 
    82         } 
    83  
    84         clstat = close(sock); 
    85         sock = -1; 
    86     } 
    87  
    88     freeaddrinfo(result); 
    89     fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC); 
     62    int sock = usock(USOCK_TCP, host, service); 
    9063 
    9164    struct timeval t = {.tv_sec = ustream_io_timeout}; 
  • luci2/ubus/libubus.c

    r6177 r6193  
    3838#include "ubus_constants.h" 
    3939#include "hash.h" 
     40#include "usock.h" 
    4041 
    4142struct ubus_ctx 
     
    258259int ubus_connect(struct ubus_ctx *ctx) 
    259260{ 
    260     int s, len; 
    261     struct sockaddr_un remote; 
     261    int s; 
    262262    struct pollfd p = {.fd = ctx->s, .events = POLLRDHUP | POLLHUP | POLLERR}; 
    263263    if(ctx->s && !poll(&p, 1, 0)) 
     
    266266        close(ctx->s); 
    267267 
    268     if((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) 
     268    if((s = usock(USOCK_UNIX | USOCK_TCP, UBUS_SOCK_PATH, NULL)) == -1) 
    269269    { 
    270270        perror("socket"); 
     
    272272    } 
    273273 
    274     remote.sun_family = AF_UNIX; 
    275     strcpy(remote.sun_path, UBUS_SOCK_PATH); 
    276     len = strlen(remote.sun_path) + sizeof(remote.sun_family); 
    277  
    278     if(connect(s, (struct sockaddr *)&remote, len) == -1) 
    279     { 
    280         close(s); 
    281         return -2; 
    282     } 
    283274    ctx->s = s; 
    284275    LOG("connected to ubus\n"); 
  • luci2/ubus/ubus.h

    r5836 r6193  
    3131#include "ubus_msg.h" 
    3232#include "hash.h" 
     33#include "usock.h" 
    3334 
    3435struct ubus_client 
  • luci2/ubus/ubus_core.c

    r6011 r6193  
    7171void ubus_init(const char *path) 
    7272{ 
    73     int len, flags; 
     73    int flags; 
    7474    struct epoll_event ev; 
    75     struct sockaddr_un local; 
    7675    INIT_LIST_HEAD(&ubus_clients); 
    77     if((ubusfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) 
     76    path = (path)?(path):(UBUS_SOCK_PATH); 
     77    unlink(path); 
     78    if((ubusfd = usock(USOCK_TCP | USOCK_UNIX | USOCK_SERVER, path, NULL)) == -1) 
    7879    { 
    7980        perror("socket"); 
     
    8182    } 
    8283 
    83     local.sun_family = AF_UNIX; 
    84     strcpy(local.sun_path, (path)?(path):(UBUS_SOCK_PATH)); 
    85     unlink(local.sun_path); 
    86     len = strlen(local.sun_path) + sizeof(local.sun_family); 
    87     if(bind(ubusfd, (struct sockaddr *)&local, len) == -1) 
    88     { 
    89         perror("bind"); 
    90         exit(1); 
    91     } 
    92  
    93     if(listen(ubusfd, 20) == -1) 
    94     { 
    95         perror("listen"); 
    96         exit(1); 
    97     } 
    9884    ubus_epollfd = epoll_create(32); 
    9985    flags = fcntl(ubusfd, F_GETFL, 0); 
  • luci2/ubus/ubus_dispatch.c

    r6177 r6193  
    148148    if(strcmp(ns, u->type)) 
    149149    { 
    150         LOG("dropping, namespace mismatch\n", u->type, ns); 
     150        LOG("dropping, namespace mismatch\n"); //, u->type, ns); 
    151151        return; 
    152152    } 
  • luci2/ubus/ubus_msg.c

    r5827 r6193  
    133133    if(strlen(node) >= UBUS_MAX_NODE) 
    134134    { 
    135         LOG("node too long %s (%d)\n", node, strlen(node)); 
     135        LOG("node too long %s (%d)\n", node, (int)strlen(node)); 
    136136        return 1; 
    137137    } 
  • luci2/zhttpd/tcp.c

    r6139 r6193  
    3333#include <fcntl.h> 
    3434#include <arpa/inet.h> 
     35#include <libubox/usock.h> 
    3536 
    3637#include "zthread.h" 
     
    255256} 
    256257 
    257 /* Create TCP socket */ 
    258 static int zhttpd_tcp_server_socket(const char *host, const char *service) { 
    259     struct addrinfo *result, *rp; 
    260     int sock = -1, clstat; 
    261  
    262     struct addrinfo hints = { 
    263         .ai_family = AF_UNSPEC, 
    264         .ai_socktype = SOCK_STREAM, 
    265         .ai_flags = AI_PASSIVE 
    266     }; 
    267  
    268     if (getaddrinfo(host, service, &hints, &result)) { 
    269         return -1; 
    270     } 
    271  
    272     for (rp = result; rp != NULL; rp = rp->ai_next) { 
    273         sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); 
    274         if (sock == -1) { 
    275             continue; 
    276         } 
    277  
    278         int one = 1; 
    279         setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&one, sizeof(one)); 
    280         if (!bind(sock, rp->ai_addr, rp->ai_addrlen) && !listen(sock, 32)) { 
    281             break; 
    282         } 
    283  
    284         clstat = close(sock); 
    285         sock = -1; 
    286     } 
    287  
    288     freeaddrinfo(result); 
    289     return sock; 
    290 } 
    291  
    292258 
    293259/* Create and spawn listening server zthread */ 
    294260int zhttpd_tcp_server(const char *host, const char *service, 
    295261const char *tlskey, const char *tlscert, int pem) { 
    296     int socket = zhttpd_tcp_server_socket(host, service); 
     262    int socket = usock(USOCK_TCP | USOCK_NONBLOCK, host, service); 
    297263    if (socket == -1) { 
    298264        return ZHTTPD_EIO; 
    299265    } 
    300     fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK); 
    301     fcntl(socket, F_SETFD, fcntl(socket, F_GETFD) | FD_CLOEXEC); 
    302266 
    303267    zhttpd_tcp_t *tcp = calloc(1, sizeof(zhttpd_tcp_t));