Changeset 700

Show
Ignore:
Timestamp:
02/18/08 09:22:42 (9 months ago)
Author:
dormando
Message:

IPv6 support patch by Brian Aker <brian@tangent.org>

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/server/memcached.c

    r699 r700  
    6464 */ 
    6565static void drive_machine(conn *c); 
    66 static int new_socket(const bool is_udp); 
     66static int new_socket(struct addrinfo *ai); 
    6767static int server_socket(const int port, const bool is_udp); 
    6868static int try_read_command(conn *c); 
     
    168168    settings.port = 11211; 
    169169    settings.udpport = 0; 
    170     settings.interf.s_addr = htonl(INADDR_ANY); 
     170    /* By default this string should be NULL for getaddrinfo() */ 
     171    settings.inter = NULL; 
    171172    settings.maxbytes = 64 * 1024 * 1024; /* default is 64MB */ 
    172173    settings.maxconns = 1024;         /* to limit connections-related memory to about 5MB */ 
     
    20522053    int sfd, flags = 1; 
    20532054    socklen_t addrlen; 
    2054     struct sockaddr addr; 
     2055    struct sockaddr_storage addr; 
    20552056    int res; 
    20562057 
     
    20622063        case conn_listening: 
    20632064            addrlen = sizeof(addr); 
    2064             if ((sfd = accept(c->sfd, &addr, &addrlen)) == -1) { 
     2065            if ((sfd = accept(c->sfd, (struct sockaddr *)&addr, &addrlen)) == -1) { 
    20652066                if (errno == EAGAIN || errno == EWOULDBLOCK) { 
    20662067                    /* these are transient, so don't log anything */ 
     
    22932294} 
    22942295 
    2295 static int new_socket(const bool is_udp) { 
     2296static int new_socket(struct addrinfo *ai) { 
    22962297    int sfd; 
    22972298    int flags; 
    22982299 
    2299     if ((sfd = socket(AF_INET, is_udp ? SOCK_DGRAM : SOCK_STREAM, 0)) == -1) { 
     2300    if ((sfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) == -1) { 
    23002301        perror("socket()"); 
    23012302        return -1; 
     
    23502351    int sfd; 
    23512352    struct linger ling = {0, 0}; 
    2352     struct sockaddr_in addr; 
     2353    struct addrinfo *ai; 
     2354    struct addrinfo hints; 
     2355    char port_buf[NI_MAXSERV]; 
     2356    int error; 
     2357 
    23532358    int flags =1; 
    23542359 
    2355     if ((sfd = new_socket(is_udp)) == -1) { 
     2360    /* 
     2361     * the memset call clears nonstandard fields in some impementations 
     2362     * that otherwise mess things up. 
     2363     */ 
     2364    memset(&hints, 0, sizeof (hints)); 
     2365    hints.ai_flags = AI_PASSIVE|AI_ADDRCONFIG; 
     2366    if (is_udp) 
     2367    { 
     2368        hints.ai_protocol = IPPROTO_UDP; 
     2369        hints.ai_socktype = SOCK_DGRAM; 
     2370        hints.ai_family = AF_INET; /* This left here because of issues with OSX 10.5 */ 
     2371    } else { 
     2372        hints.ai_family = AF_UNSPEC; 
     2373        hints.ai_protocol = IPPROTO_TCP; 
     2374        hints.ai_socktype = SOCK_STREAM; 
     2375    } 
     2376 
     2377    snprintf(port_buf, NI_MAXSERV, "%d", port); 
     2378    error= getaddrinfo(settings.inter, port_buf, &hints, &ai); 
     2379    if (error != 0) { 
     2380      if (error != EAI_SYSTEM) 
     2381        fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(error)); 
     2382      else 
     2383        perror("getaddrinfo()"); 
     2384 
     2385      return -1; 
     2386    } 
     2387 
     2388    if ((sfd = new_socket(ai)) == -1) { 
     2389        freeaddrinfo(ai); 
    23562390        return -1; 
    23572391    } 
     
    23662400    } 
    23672401 
    2368     /* 
    2369      * the memset call clears nonstandard fields in some impementations 
    2370      * that otherwise mess things up. 
    2371      */ 
    2372     memset(&addr, 0, sizeof(addr)); 
    2373  
    2374     addr.sin_family = AF_INET; 
    2375     addr.sin_port = htons(port); 
    2376     addr.sin_addr = settings.interf; 
    2377     if (bind(sfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) { 
     2402    if (bind(sfd, ai->ai_addr, ai->ai_addrlen) == -1) { 
    23782403        perror("bind()"); 
    23792404        close(sfd); 
     2405        freeaddrinfo(ai); 
    23802406        return -1; 
    23812407    } 
     
    23832409        perror("listen()"); 
    23842410        close(sfd); 
     2411        freeaddrinfo(ai); 
    23852412        return -1; 
    23862413    } 
     2414 
     2415    freeaddrinfo(ai); 
     2416 
    23872417    return sfd; 
    23882418} 
     
    26852715int main (int argc, char **argv) { 
    26862716    int c; 
    2687     struct in_addr addr; 
    26882717    bool lock_memory = false; 
    26892718    bool daemonize = false; 
     
    27462775            break; 
    27472776        case 'l': 
    2748             if (inet_pton(AF_INET, optarg, &addr) <= 0) { 
    2749                 fprintf(stderr, "Illegal address: %s\n", optarg); 
    2750                 return 1; 
    2751             } else { 
    2752                 settings.interf = addr; 
    2753             } 
     2777            settings.inter= strdup(optarg); 
    27542778            break; 
    27552779        case 'd': 
     
    29893013    if (daemonize) 
    29903014        remove_pidfile(pid_file); 
     3015    /* Clean up strdup() call for bind() address */ 
     3016    if (settings.inter) 
     3017      free(settings.inter); 
     3018 
    29913019    return 0; 
    29923020} 
  • trunk/server/memcached.h

    r651 r700  
    1111#include <netinet/in.h> 
    1212#include <event.h> 
     13#include <netdb.h> 
    1314 
    1415#define DATA_BUFFER_SIZE 2048 
     
    8485    int port; 
    8586    int udpport; 
    86     struct in_addr interf
     87    char *inter
    8788    int verbose; 
    8889    rel_time_t oldest_live; /* ignore existing items older than this */