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
out. This mode is only supported on Linux.
@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
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
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
.TP
.B \-4
Resolve hostnames only to IPv4 addresses.
Resolve hostnames only to IPv4 addresses and create only IPv4 sockets.
.TP
.B \-6
Resolve hostnames only to IPv6 addresses.
Resolve hostnames only to IPv6 addresses and create only IPv6 sockets.
.SH FILES
\fI@SYSCONFDIR@/chrony.conf\fR

View file

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

View file

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

12
main.c
View file

@ -286,7 +286,7 @@ int main
{
const char *conf_file = DEFAULT_CONF_FILE;
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 other_pid;
int lock_memory = 0, sched_priority = 0;
@ -329,9 +329,9 @@ int main
debug = 1;
nofork = 1;
} else if (!strcmp("-4", *argv)) {
DNS_SetAddressFamily(IPADDR_INET4);
address_family = IPADDR_INET4;
} else if (!strcmp("-6", *argv)) {
DNS_SetAddressFamily(IPADDR_INET6);
address_family = IPADDR_INET6;
} else {
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);
DNS_SetAddressFamily(address_family);
CNF_SetRestarted(restarted);
CNF_ReadFile(conf_file);
@ -376,8 +378,8 @@ int main
LCL_Initialise();
SCH_Initialise();
SYS_Initialise();
NIO_Initialise();
CAM_Initialise();
NIO_Initialise(address_family);
CAM_Initialise(address_family);
RTC_Initialise();
SRC_Initialise();
RCL_Initialise();

View file

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

View file

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