[MEDIUM] IPv6 support for syslog

This commit is contained in:
David du Colombier 2011-03-24 12:23:00 +01:00 committed by Willy Tarreau
parent 0bc3493d2c
commit 11bcb6c4f5
4 changed files with 67 additions and 46 deletions

View File

@ -501,4 +501,52 @@ static inline int is_addr(struct sockaddr_storage *addr)
return 0;
}
/* returns port in network byte order */
static inline int get_net_port(struct sockaddr_storage *addr)
{
switch (addr->ss_family) {
case AF_INET:
return ((struct sockaddr_in *)addr)->sin_port;
case AF_INET6:
return ((struct sockaddr_in6 *)addr)->sin6_port;
}
return 0;
}
/* returns port in host byte order */
static inline int get_host_port(struct sockaddr_storage *addr)
{
switch (addr->ss_family) {
case AF_INET:
return ntohs(((struct sockaddr_in *)addr)->sin_port);
case AF_INET6:
return ntohs(((struct sockaddr_in6 *)addr)->sin6_port);
}
return 0;
}
/* set port in host byte order */
static inline int set_net_port(struct sockaddr_storage *addr, int port)
{
switch (addr->ss_family) {
case AF_INET:
((struct sockaddr_in *)addr)->sin_port = port;
case AF_INET6:
((struct sockaddr_in6 *)addr)->sin6_port = port;
}
return 0;
}
/* set port in network byte order */
static inline int set_host_port(struct sockaddr_storage *addr, int port)
{
switch (addr->ss_family) {
case AF_INET:
((struct sockaddr_in *)addr)->sin_port = htons(port);
case AF_INET6:
((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
}
return 0;
}
#endif /* _COMMON_STANDARD_H */

View File

@ -48,11 +48,7 @@
#define LW_RSPHDR 2048 /* response header(s) */
struct logsrv {
union {
struct sockaddr addr;
struct sockaddr_un un; /* AF_UNIX */
struct sockaddr_in in; /* AF_INET */
} u;
struct sockaddr_storage addr;
};
#endif /* _TYPES_LOG_H */

View File

@ -899,26 +899,24 @@ int cfg_parse_global(const char *file, int linenum, char **args, int kwm)
}
if (args[1][0] == '/') {
struct sockaddr_un *sk = str2sun(args[1]);
struct sockaddr_storage *sk = (struct sockaddr_storage *)str2sun(args[1]);
if (!sk) {
Alert("parsing [%s:%d] : Socket path '%s' too long (max %d)\n", file, linenum,
args[1], (int)sizeof(sk->sun_path) - 1);
args[1], (int)sizeof(((struct sockaddr_un *)&sk)->sun_path) - 1);
err_code |= ERR_ALERT | ERR_FATAL;
goto out;
}
logsrv.u.un = *sk;
logsrv.u.addr.sa_family = AF_UNIX;
logsrv.addr = *sk;
} else {
struct sockaddr_storage *sk = str2sa(args[1]);
if (!sk || sk->ss_family != AF_INET) {
if (!sk) {
Alert("parsing [%s:%d] : Unknown host in '%s'\n", file, linenum, args[1]);
err_code |= ERR_ALERT | ERR_FATAL;
goto out;
}
logsrv.u.in = *(struct sockaddr_in *)sk;
logsrv.u.addr.sa_family = AF_INET;
if (!logsrv.u.in.sin_port)
logsrv.u.in.sin_port = htons(SYSLOG_PORT);
logsrv.addr = *sk;
if (!get_host_port(&logsrv.addr))
set_host_port(&logsrv.addr, SYSLOG_PORT);
}
if (global.logfac1 == -1) {
@ -4546,28 +4544,24 @@ stats_error_parsing:
}
if (args[1][0] == '/') {
struct sockaddr_un *sk = str2sun(args[1]);
struct sockaddr_storage *sk = (struct sockaddr_storage *)str2sun(args[1]);
if (!sk) {
Alert("parsing [%s:%d] : Socket path '%s' too long (max %d)\n", file, linenum,
args[1], (int)sizeof(sk->sun_path) - 1);
args[1], (int)sizeof(((struct sockaddr_un *)sk)->sun_path) - 1);
err_code |= ERR_ALERT | ERR_FATAL;
goto out;
}
logsrv.u.un = *sk;
logsrv.u.addr.sa_family = AF_UNIX;
logsrv.addr = *sk;
} else {
struct sockaddr_storage *sk = str2sa(args[1]);
if (!sk || sk->ss_family != AF_INET) {
if (!sk) {
Alert("parsing [%s:%d] : Unknown host in '%s'\n", file, linenum, args[1]);
err_code |= ERR_ALERT | ERR_FATAL;
goto out;
}
logsrv.u.in = *(struct sockaddr_in *)sk;
logsrv.u.addr.sa_family = AF_INET;
if (!logsrv.u.in.sin_port) {
logsrv.u.in.sin_port =
htons(SYSLOG_PORT);
}
logsrv.addr = *sk;
if (!get_host_port(&logsrv.addr))
set_host_port(&logsrv.addr, SYSLOG_PORT);
}
if (curproxy->logfac1 == -1) {

View File

@ -141,22 +141,6 @@ int get_log_facility(const char *fac)
return facility;
}
/*
* Return the length of the address endpoint, suitable for use with sendto().
*/
static inline int logsrv_addrlen(const struct logsrv *logsrv)
{
switch (logsrv->u.addr.sa_family) {
case AF_UNIX:
return sizeof(logsrv->u.un);
case AF_INET:
return sizeof(logsrv->u.in);
default:
break;
}
return -1;
}
/*
* This function sends a syslog message to both log servers of a proxy,
* or to global log servers if the proxy is NULL.
@ -253,11 +237,10 @@ void send_log(struct proxy *p, int level, const char *message, ...)
for (nblogger = 0; nblogger < nbloggers; nblogger++) {
const struct logsrv *logsrv = logsrvs[nblogger];
int proto, *plogfd;
if (logsrv->u.addr.sa_family == AF_UNIX) {
if (logsrv->addr.ss_family == AF_UNIX) {
proto = 0;
plogfd = &logfdunix;
} else {
/* sa_family == AF_INET */
proto = IPPROTO_UDP;
plogfd = &logfdinet;
}
@ -265,7 +248,7 @@ void send_log(struct proxy *p, int level, const char *message, ...)
/* socket already created. */
continue;
}
if ((*plogfd = socket(logsrv->u.addr.sa_family, SOCK_DGRAM,
if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
proto)) < 0) {
Alert("socket for logger #%d failed: %s (errno=%d)\n",
nblogger + 1, strerror(errno), errno);
@ -280,7 +263,7 @@ void send_log(struct proxy *p, int level, const char *message, ...)
/* Send log messages to syslog server. */
for (nblogger = 0; nblogger < nbloggers; nblogger++) {
const struct logsrv *logsrv = logsrvs[nblogger];
int *plogfd = logsrv->u.addr.sa_family == AF_UNIX ?
int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
&logfdunix : &logfdinet;
int sent;
@ -306,7 +289,7 @@ void send_log(struct proxy *p, int level, const char *message, ...)
/* the total syslog message now starts at logptr, for dataptr+data_len-logptr */
sent = sendto(*plogfd, log_ptr, dataptr + data_len - log_ptr,
MSG_DONTWAIT | MSG_NOSIGNAL, &logsrv->u.addr, logsrv_addrlen(logsrv));
MSG_DONTWAIT | MSG_NOSIGNAL, (struct sockaddr *)&logsrv->addr, sizeof(logsrv->addr));
if (sent < 0) {
Alert("sendto logger #%d failed: %s (errno=%d)\n",
nblogger, strerror(errno), errno);