Skip to content

Commit

Permalink
Issue 155: bind to multiple interface
Browse files Browse the repository at this point in the history
  • Loading branch information
trondn committed Nov 10, 2010
1 parent 16a809e commit c605b31
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions memcached.c
Original file line number Diff line number Diff line change
Expand Up @@ -3796,13 +3796,16 @@ static void maximize_sndbuf(const int sfd) {

/**
* Create a socket and bind it to a specific port number
* @param interface the interface to bind to
* @param port the port number to bind to
* @param transport the transport protocol (TCP / UDP)
* @param portnumber_file A filepointer to write the port numbers to
* when they are successfully added to the list of ports we
* listen on.
*/
static int server_socket(int port, enum network_transport transport,
static int server_socket(const char *interface,
int port,
enum network_transport transport,
FILE *portnumber_file) {
int sfd;
struct linger ling = {0, 0};
Expand All @@ -3821,7 +3824,7 @@ static int server_socket(int port, enum network_transport transport,
port = 0;
}
snprintf(port_buf, sizeof(port_buf), "%d", port);
error= getaddrinfo(settings.inter, port_buf, &hints, &ai);
error= getaddrinfo(interface, port_buf, &hints, &ai);
if (error != 0) {
if (error != EAI_SYSTEM)
fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(error));
Expand Down Expand Up @@ -3932,6 +3935,30 @@ static int server_socket(int port, enum network_transport transport,
return success == 0;
}

static int server_sockets(int port, enum network_transport transport,
FILE *portnumber_file) {
if (settings.inter == NULL) {
return server_socket(settings.inter, port, transport, portnumber_file);
} else {
// tokenize them and bind to each one of them..
char *b;
int ret = 0;
char *list = strdup(settings.inter);

if (list == NULL) {
fprintf(stderr, "Failed to allocate memory for parsing server interface string\n");
return 1;
}
for (char *p = strtok_r(list, ";,", &b);
p != NULL;
p = strtok_r(NULL, ";,", &b)) {
ret |= server_socket(p, port, transport, portnumber_file);
}
free(list);
return ret;
}
}

static int new_socket_unix(void) {
int sfd;
int flags;
Expand Down Expand Up @@ -4679,7 +4706,7 @@ int main (int argc, char **argv) {
}

errno = 0;
if (settings.port && server_socket(settings.port, tcp_transport,
if (settings.port && server_sockets(settings.port, tcp_transport,
portnumber_file)) {
vperror("failed to listen on TCP port %d", settings.port);
exit(EX_OSERR);
Expand All @@ -4695,7 +4722,7 @@ int main (int argc, char **argv) {

/* create the UDP listening socket and bind it */
errno = 0;
if (settings.udpport && server_socket(settings.udpport, udp_transport,
if (settings.udpport && server_sockets(settings.udpport, udp_transport,
portnumber_file)) {
vperror("failed to listen on UDP port %d", settings.udpport);
exit(EX_OSERR);
Expand Down

0 comments on commit c605b31

Please sign in to comment.