Changeset d936b80e0b77ab87046b8dec33cfb9462504f507

Show
Ignore:
Timestamp:
02/06/08 05:29:24 (7 years ago)
Author:
Theo Schlossnagle <jesus@omniti.com>
git-committer:
Theo Schlossnagle <jesus@omniti.com> 1202275764 +0000
git-parent:

[37cf7d8f9e336376dc0ecdb1b80e4776721a950b]

git-author:
Theo Schlossnagle <jesus@omniti.com> 1202275764 +0000
Message:

support unix domain sockets as well

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/noit_listener.c

    r1afde4e rd936b80  
    1212#include <sys/ioctl.h> 
    1313#include <netinet/in.h> 
     14#include <sys/un.h> 
    1415#include <arpa/inet.h> 
    1516 
     
    2829    struct sockaddr_in addr4; 
    2930    struct sockaddr_in6 addr6; 
     31    struct sockaddr_un unix; 
    3032  } s; 
    3133 
     
    6062  int rv, fd; 
    6163  int8_t family; 
     64  int sockaddr_len; 
    6265  socklen_t on; 
    6366  long reuse; 
     
    7174    struct sockaddr_in addr4; 
    7275    struct sockaddr_in6 addr6; 
     76    struct sockaddr_un unix; 
    7377  } s; 
    7478  const char *event_name; 
     
    7882        (event_name = eventer_name_for_callback(handler))?event_name:"??", 
    7983        closure); 
    80   family = AF_INET; 
    81   rv = inet_pton(family, host, &a); 
    82   if(rv != 1) { 
    83     family = AF_INET6; 
     84  if(host[0] == '/') { 
     85    family = AF_UNIX; 
     86  } 
     87  else { 
     88    family = AF_INET; 
    8489    rv = inet_pton(family, host, &a); 
    8590    if(rv != 1) { 
    86       if(!strcmp(host, "*")) { 
    87         family = AF_INET; 
    88         a.addr4.s_addr = INADDR_ANY; 
    89       } else { 
    90         noitL(noit_stderr, "Cannot translate '%s' to IP\n", host); 
    91         return -1; 
     91      family = AF_INET6; 
     92      rv = inet_pton(family, host, &a); 
     93      if(rv != 1) { 
     94        if(!strcmp(host, "*")) { 
     95          family = AF_INET; 
     96          a.addr4.s_addr = INADDR_ANY; 
     97        } else { 
     98          noitL(noit_stderr, "Cannot translate '%s' to IP\n", host); 
     99          return -1; 
     100        } 
    92101      } 
    93102    } 
     
    112121 
    113122  memset(&s, 0, sizeof(s)); 
    114   s.addr6.sin6_family = family; 
    115   s.addr6.sin6_port = htons(port); 
    116   memcpy(&s.addr6.sin6_addr, &a, sizeof(a)); 
    117   if(bind(fd, (struct sockaddr *)&s, 
    118           (family == AF_INET) ?  sizeof(s.addr4) : sizeof(s.addr6)) < 0) { 
    119     noitL(noit_stderr, "bind failed: %s\b", strerror(errno)); 
     123  if(family == AF_UNIX) { 
     124    struct stat sb; 
     125    /* unlink the path iff it is a socket */ 
     126    if(stat(host, &sb) == -1) { 
     127      if(errno != ENOENT) { 
     128        noitL(noit_stderr, "%s: %s\n", host, strerror(errno)); 
     129        close(fd); 
     130        return -1; 
     131      } 
     132    } 
     133    else { 
     134      if(sb.st_mode & S_IFSOCK) 
     135        unlink(host); 
     136      else { 
     137        noitL(noit_stderr, "unlink %s failed: %s\n", host, strerror(errno)); 
     138        close(fd); 
     139        return -1; 
     140      } 
     141    } 
     142    strncpy(s.unix.sun_path, host, sizeof(s.unix.sun_path)-1); 
     143    sockaddr_len = sizeof(s.unix); 
     144  } 
     145  else { 
     146    s.addr6.sin6_family = family; 
     147    s.addr6.sin6_port = htons(port); 
     148    memcpy(&s.addr6.sin6_addr, &a, sizeof(a)); 
     149    sockaddr_len = (family == AF_INET) ?  sizeof(s.addr4) : sizeof(s.addr6); 
     150  } 
     151  if(bind(fd, (struct sockaddr *)&s, sockaddr_len) < 0) { 
     152    noitL(noit_stderr, "bind failed[%s]: %s\n", host, strerror(errno)); 
    120153    close(fd); 
    121154    return -1; 
     
    172205      continue; 
    173206    } 
    174     if(!noit_conf_get_int(listener_configs[i], "port", &portint)) 
    175       portint = 0; 
    176     port = (unsigned short) portint; 
    177     if(portint == 0 || (port != portint)) { 
    178       noitL(noit_stderr, 
    179             "Invalid port [%d] specified in stanza %d\n", port, i+1); 
    180       continue; 
    181     } 
    182207    if(!noit_conf_get_stringbuf(listener_configs[i], 
    183208                                "address", address, sizeof(address))) { 
     
    185210      address[1] = '\0'; 
    186211    } 
     212    if(!noit_conf_get_int(listener_configs[i], "port", &portint)) 
     213      portint = 0; 
     214    port = (unsigned short) portint; 
     215    if(address[0] != '/' && (portint == 0 || (port != portint))) { 
     216      /* UNIX sockets don't require a port (they'll ignore it if specified */ 
     217      noitL(noit_stderr, 
     218            "Invalid port [%d] specified in stanza %d\n", port, i+1); 
     219      continue; 
     220    } 
    187221    if(!noit_conf_get_int(listener_configs[i], "backlog", &backlog)) 
    188222      backlog = 5; 
  • src/sample.conf

    r5229ef1 rd936b80  
    3030    <listener> 
    3131      <type>noit_console</type> 
    32       <address>127.0.0.1</address> 
    33       <port>32321</port> 
     32      <address>/tmp/noit</address> 
    3433    </listener> 
    3534    <listener>