Added support for defining a bind address in the listener config.

Also removed some compilation warnings.

See Bugzilla bug 150
This commit is contained in:
Mark Riddoch
2014-02-06 19:04:12 +01:00
parent 7bda588948
commit 1742372dd5
8 changed files with 98 additions and 67 deletions

View File

@ -340,11 +340,13 @@ int error_count = 0;
else if (!strcmp(type, "listener"))
{
char *service;
char *address;
char *port;
char *protocol;
service = config_get_value(obj->parameters, "service");
port = config_get_value(obj->parameters, "port");
address = config_get_value(obj->parameters, "address");
protocol = config_get_value(obj->parameters, "protocol");
if (service && port && protocol)
@ -356,6 +358,7 @@ int error_count = 0;
{
serviceAddProtocol(ptr->element,
protocol,
address,
atoi(port));
}
else
@ -758,8 +761,10 @@ SERVER *server;
char *service;
char *port;
char *protocol;
char *address;
service = config_get_value(obj->parameters, "service");
address = config_get_value(obj->parameters, "address");
port = config_get_value(obj->parameters, "port");
protocol = config_get_value(obj->parameters, "protocol");
@ -777,6 +782,7 @@ SERVER *server;
{
serviceAddProtocol(ptr->element,
protocol,
address,
atoi(port));
serviceStartProtocol(ptr->element,
protocol,

View File

@ -32,6 +32,7 @@
* 01-07-2013 Massimiliano Pinto Removed session->backends
* from gw_read_gwbuff()
* 25-09-2013 Massimiliano Pinto setipaddress uses getaddrinfo
* 06-02-2014 Mark Riddoch Added parse_bindconfig
*
*@endverbatim
*/
@ -40,9 +41,14 @@
#include <dcb.h>
#include <session.h>
#include <skygw_utils.h>
#include <log_manager.h>
SPINLOCK tmplock = SPINLOCK_INIT;
/**
extern int lm_enabled_logfiles_bitmask;
/*
* Set IP address in socket structure in_addr
*
* @param a Pointer to a struct in_addr into which the address is written
@ -184,3 +190,64 @@ int gw_read_gwbuff(DCB *dcb, GWBUF **head, int b) {
return 0;
}
/**
* Parse the bind config data. This is passed in a string as address:port.
*
* The address may be either a . seperated IP address or a hostname to
* lookup. The address 0.0.0.0 is the wildcard address for SOCKADR_ANY.
* The ':' and port may be omitted, in which case the default port is
* used.
*
* @param config The bind address and port seperated by a ':'
* @param def_port The default port to use
* @param addr The sockaddr_in in which the data is written
* @return 0 on failure
*/
int
parse_bindconfig(char *config, unsigned short def_port, struct sockaddr_in *addr)
{
char *port, buf[1024];
short pnum;
struct hostent *hp;
strncpy(buf, config, 1024);
port = strrchr(buf, ':');
if (port)
{
*port = 0;
port++;
pnum = atoi(port);
}
else
{
pnum = def_port;
}
if (!strcmp(buf, "0.0.0.0"))
{
addr->sin_addr.s_addr = htonl(INADDR_ANY);
}
else
{
if (!inet_aton(buf, &addr->sin_addr))
{
if ((hp = gethostbyname(buf)) != NULL)
{
bcopy(hp->h_addr, &(addr->sin_addr.s_addr), hp->h_length);
}
else
{
LOGIF(LE, (skygw_log_write_flush(
LOGFILE_ERROR,
"Error : Failed to lookup host '%s'. ",
buf)));
return 0;
}
}
}
addr->sin_family = AF_INET;
addr->sin_port = htons(pnum);
return 1;
}

View File

@ -135,7 +135,10 @@ GWPROTOCOL *funcs;
}
memcpy(&(port->listener->func), funcs, sizeof(GWPROTOCOL));
port->listener->session = NULL;
sprintf(config_bind, "0.0.0.0:%d", port->port);
if (port->address)
sprintf(config_bind, "%s:%d", port->address, port->port);
else
sprintf(config_bind, "0.0.0.0:%d", port->port);
if (port->listener->func.listen(port->listener, config_bind)) {
port->listener->session = session_alloc(service, port->listener);
@ -335,11 +338,12 @@ SERVICE *ptr;
*
* @param service The service
* @param protocol The name of the protocol module
* @param address The address to listen with
* @param port The port to listen on
* @return TRUE if the protocol/port could be added
*/
int
serviceAddProtocol(SERVICE *service, char *protocol, unsigned short port)
serviceAddProtocol(SERVICE *service, char *protocol, char *address, unsigned short port)
{
SERV_PROTOCOL *proto;
@ -348,6 +352,10 @@ SERV_PROTOCOL *proto;
return 0;
}
proto->protocol = strdup(protocol);
if (address)
proto->address = strdup(address);
else
proto->address = NULL;
proto->port = port;
spinlock_acquire(&service->spin);
proto->next = service->ports;
@ -358,7 +366,7 @@ SERV_PROTOCOL *proto;
}
/**
* Check if a protocol/port pair si part of the service
* Check if a protocol/port pair is part of the service
*
* @param service The service
* @param protocol The name of the protocol module