Create sockets only in selected family with -4 or -6 option

This commit is contained in:
Miroslav Lichvar 2013-05-20 15:34:33 +02:00
parent 51a2d8dfd8
commit 72d0b3c913
7 changed files with 35 additions and 17 deletions

View file

@ -1097,9 +1097,11 @@ supported only on Linux.
This option will lock chronyd into RAM so that it will never be paged This option will lock chronyd into RAM so that it will never be paged
out. This mode is only supported on Linux. out. This mode is only supported on Linux.
@item -4 @item -4
With this option hostnames will be resolved only to IPv4 addresses. With this option hostnames will be resolved only to IPv4 addresses and only
IPv4 sockets will be created.
@item -6 @item -6
With this option hostnames will be resolved only to IPv6 addresses. With this option hostnames will be resolved only to IPv6 addresses and only
IPv6 sockets will be created.
@end table @end table
On systems that support an @file{/etc/rc.local} file for starting On systems that support an @file{/etc/rc.local} file for starting

View file

@ -106,10 +106,10 @@ user. So far, it works only on Linux when compiled with capabilities support.
This option displays \fBchronyd\fR's version number to the terminal and exits This option displays \fBchronyd\fR's version number to the terminal and exits
.TP .TP
.B \-4 .B \-4
Resolve hostnames only to IPv4 addresses. Resolve hostnames only to IPv4 addresses and create only IPv4 sockets.
.TP .TP
.B \-6 .B \-6
Resolve hostnames only to IPv6 addresses. Resolve hostnames only to IPv6 addresses and create only IPv6 sockets.
.SH FILES .SH FILES
\fI@SYSCONFDIR@/chrony.conf\fR \fI@SYSCONFDIR@/chrony.conf\fR

View file

@ -262,7 +262,7 @@ prepare_socket(int family)
/* ================================================== */ /* ================================================== */
void void
CAM_Initialise(void) CAM_Initialise(int family)
{ {
assert(!initialised); assert(!initialised);
initialised = 1; initialised = 1;
@ -278,9 +278,15 @@ CAM_Initialise(void)
free_replies = NULL; free_replies = NULL;
kept_replies.next = NULL; kept_replies.next = NULL;
if (family == IPADDR_UNSPEC || family == IPADDR_INET4)
sock_fd4 = prepare_socket(AF_INET); sock_fd4 = prepare_socket(AF_INET);
else
sock_fd4 = -1;
#ifdef HAVE_IPV6 #ifdef HAVE_IPV6
if (family == IPADDR_UNSPEC || family == IPADDR_INET6)
sock_fd6 = prepare_socket(AF_INET6); sock_fd6 = prepare_socket(AF_INET6);
else
sock_fd6 = -1;
#endif #endif
if (sock_fd4 < 0 if (sock_fd4 < 0

View file

@ -29,7 +29,7 @@
#include "addressing.h" #include "addressing.h"
extern void CAM_Initialise(void); extern void CAM_Initialise(int family);
extern void CAM_Finalise(void); extern void CAM_Finalise(void);

12
main.c
View file

@ -286,7 +286,7 @@ int main
{ {
const char *conf_file = DEFAULT_CONF_FILE; const char *conf_file = DEFAULT_CONF_FILE;
char *user = NULL; char *user = NULL;
int debug = 0, nofork = 0; int debug = 0, nofork = 0, address_family = IPADDR_UNSPEC;
int do_init_rtc = 0, restarted = 0; int do_init_rtc = 0, restarted = 0;
int other_pid; int other_pid;
int lock_memory = 0, sched_priority = 0; int lock_memory = 0, sched_priority = 0;
@ -329,9 +329,9 @@ int main
debug = 1; debug = 1;
nofork = 1; nofork = 1;
} else if (!strcmp("-4", *argv)) { } else if (!strcmp("-4", *argv)) {
DNS_SetAddressFamily(IPADDR_INET4); address_family = IPADDR_INET4;
} else if (!strcmp("-6", *argv)) { } else if (!strcmp("-6", *argv)) {
DNS_SetAddressFamily(IPADDR_INET6); address_family = IPADDR_INET6;
} else { } else {
LOG_FATAL(LOGF_Main, "Unrecognized command line option [%s]", *argv); LOG_FATAL(LOGF_Main, "Unrecognized command line option [%s]", *argv);
} }
@ -354,6 +354,8 @@ int main
LOG(LOGS_INFO, LOGF_Main, "chronyd version %s starting", CHRONY_VERSION); LOG(LOGS_INFO, LOGF_Main, "chronyd version %s starting", CHRONY_VERSION);
DNS_SetAddressFamily(address_family);
CNF_SetRestarted(restarted); CNF_SetRestarted(restarted);
CNF_ReadFile(conf_file); CNF_ReadFile(conf_file);
@ -376,8 +378,8 @@ int main
LCL_Initialise(); LCL_Initialise();
SCH_Initialise(); SCH_Initialise();
SYS_Initialise(); SYS_Initialise();
NIO_Initialise(); NIO_Initialise(address_family);
CAM_Initialise(); CAM_Initialise(address_family);
RTC_Initialise(); RTC_Initialise();
SRC_Initialise(); SRC_Initialise();
RCL_Initialise(); RCL_Initialise();

View file

@ -231,17 +231,25 @@ prepare_socket(int family)
return sock_fd; return sock_fd;
} }
/* ================================================== */
void void
NIO_Initialise(void) NIO_Initialise(int family)
{ {
assert(!initialised); assert(!initialised);
initialised = 1; initialised = 1;
do_size_checks(); do_size_checks();
if (family == IPADDR_UNSPEC || family == IPADDR_INET4)
sock_fd4 = prepare_socket(AF_INET); sock_fd4 = prepare_socket(AF_INET);
else
sock_fd4 = -1;
#ifdef HAVE_IPV6 #ifdef HAVE_IPV6
if (family == IPADDR_UNSPEC || family == IPADDR_INET6)
sock_fd6 = prepare_socket(AF_INET6); sock_fd6 = prepare_socket(AF_INET6);
else
sock_fd6 = -1;
#endif #endif
if (sock_fd4 < 0 if (sock_fd4 < 0

View file

@ -32,7 +32,7 @@
#include "addressing.h" #include "addressing.h"
/* Function to initialise the module. */ /* Function to initialise the module. */
extern void NIO_Initialise(void); extern void NIO_Initialise(int family);
/* Function to finalise the module */ /* Function to finalise the module */
extern void NIO_Finalise(void); extern void NIO_Finalise(void);