ntp: store socket in NTP instance
This is preparation for separate client sockets.
This commit is contained in:
parent
308de81221
commit
9a657cd4a3
5 changed files with 40 additions and 6 deletions
|
@ -51,6 +51,7 @@ typedef struct {
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
IPAddr ip_addr;
|
IPAddr ip_addr;
|
||||||
|
int sock_fd;
|
||||||
} NTP_Local_Address;
|
} NTP_Local_Address;
|
||||||
|
|
||||||
#endif /* GOT_ADDRESSING_H */
|
#endif /* GOT_ADDRESSING_H */
|
||||||
|
|
|
@ -144,6 +144,8 @@ BRD_AddDestination(IPAddr *addr, unsigned short port, int interval)
|
||||||
destinations[n_destinations].addr.ip_addr = *addr;
|
destinations[n_destinations].addr.ip_addr = *addr;
|
||||||
destinations[n_destinations].addr.port = port;
|
destinations[n_destinations].addr.port = port;
|
||||||
destinations[n_destinations].local_addr.ip_addr.family = IPADDR_UNSPEC;
|
destinations[n_destinations].local_addr.ip_addr.family = IPADDR_UNSPEC;
|
||||||
|
destinations[n_destinations].local_addr.sock_fd =
|
||||||
|
NIO_GetServerSocket(&destinations[n_destinations].addr);
|
||||||
destinations[n_destinations].interval = interval;
|
destinations[n_destinations].interval = interval;
|
||||||
|
|
||||||
SCH_AddTimeoutInClass((double) interval, 1.0, 0.0,
|
SCH_AddTimeoutInClass((double) interval, 1.0, 0.0,
|
||||||
|
|
|
@ -64,7 +64,7 @@ typedef enum {
|
||||||
|
|
||||||
struct NCR_Instance_Record {
|
struct NCR_Instance_Record {
|
||||||
NTP_Remote_Address remote_addr; /* Needed for routing transmit packets */
|
NTP_Remote_Address remote_addr; /* Needed for routing transmit packets */
|
||||||
NTP_Local_Address local_addr; /* Local address used when sending packets */
|
NTP_Local_Address local_addr; /* Local address/socket used to send packets */
|
||||||
NTP_Mode mode; /* The source's NTP mode
|
NTP_Mode mode; /* The source's NTP mode
|
||||||
(client/server or symmetric active peer) */
|
(client/server or symmetric active peer) */
|
||||||
OperatingMode opmode; /* Whether we are sampling this source
|
OperatingMode opmode; /* Whether we are sampling this source
|
||||||
|
@ -287,9 +287,11 @@ NCR_GetInstance(NTP_Remote_Address *remote_addr, NTP_Source_Type type, SourcePar
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case NTP_SERVER:
|
case NTP_SERVER:
|
||||||
|
result->local_addr.sock_fd = NIO_GetClientSocket(remote_addr);
|
||||||
result->mode = MODE_CLIENT;
|
result->mode = MODE_CLIENT;
|
||||||
break;
|
break;
|
||||||
case NTP_PEER:
|
case NTP_PEER:
|
||||||
|
result->local_addr.sock_fd = NIO_GetServerSocket(remote_addr);
|
||||||
result->mode = MODE_ACTIVE;
|
result->mode = MODE_ACTIVE;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|
33
ntp_io.c
33
ntp_io.c
|
@ -285,6 +285,28 @@ NIO_Finalise(void)
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
|
int
|
||||||
|
NIO_GetClientSocket(NTP_Remote_Address *remote_addr)
|
||||||
|
{
|
||||||
|
return NIO_GetServerSocket(remote_addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
int
|
||||||
|
NIO_GetServerSocket(NTP_Remote_Address *remote_addr)
|
||||||
|
{
|
||||||
|
switch (remote_addr->ip_addr.family) {
|
||||||
|
case IPADDR_INET4:
|
||||||
|
return sock_fd4;
|
||||||
|
#ifdef HAVE_IPV6
|
||||||
|
case IPADDR_INET6:
|
||||||
|
return sock_fd6;
|
||||||
|
#endif
|
||||||
|
default:
|
||||||
|
return INVALID_SOCK_FD;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
|
@ -351,6 +373,7 @@ read_from_socket(void *anything)
|
||||||
}
|
}
|
||||||
|
|
||||||
local_addr.ip_addr.family = IPADDR_UNSPEC;
|
local_addr.ip_addr.family = IPADDR_UNSPEC;
|
||||||
|
local_addr.sock_fd = sock_fd;
|
||||||
|
|
||||||
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
|
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
|
||||||
#ifdef IP_PKTINFO
|
#ifdef IP_PKTINFO
|
||||||
|
@ -410,7 +433,6 @@ send_packet(void *packet, int packetlen, NTP_Remote_Address *remote_addr, NTP_Lo
|
||||||
struct iovec iov;
|
struct iovec iov;
|
||||||
char cmsgbuf[256];
|
char cmsgbuf[256];
|
||||||
int cmsglen;
|
int cmsglen;
|
||||||
int sock_fd;
|
|
||||||
socklen_t addrlen;
|
socklen_t addrlen;
|
||||||
|
|
||||||
assert(initialised);
|
assert(initialised);
|
||||||
|
@ -422,7 +444,6 @@ send_packet(void *packet, int packetlen, NTP_Remote_Address *remote_addr, NTP_Lo
|
||||||
remote.in4.sin_family = AF_INET;
|
remote.in4.sin_family = AF_INET;
|
||||||
remote.in4.sin_port = htons(remote_addr->port);
|
remote.in4.sin_port = htons(remote_addr->port);
|
||||||
remote.in4.sin_addr.s_addr = htonl(remote_addr->ip_addr.addr.in4);
|
remote.in4.sin_addr.s_addr = htonl(remote_addr->ip_addr.addr.in4);
|
||||||
sock_fd = sock_fd4;
|
|
||||||
break;
|
break;
|
||||||
#ifdef HAVE_IPV6
|
#ifdef HAVE_IPV6
|
||||||
case IPADDR_INET6:
|
case IPADDR_INET6:
|
||||||
|
@ -432,15 +453,17 @@ send_packet(void *packet, int packetlen, NTP_Remote_Address *remote_addr, NTP_Lo
|
||||||
remote.in6.sin6_port = htons(remote_addr->port);
|
remote.in6.sin6_port = htons(remote_addr->port);
|
||||||
memcpy(&remote.in6.sin6_addr.s6_addr, &remote_addr->ip_addr.addr.in6,
|
memcpy(&remote.in6.sin6_addr.s6_addr, &remote_addr->ip_addr.addr.in6,
|
||||||
sizeof (remote.in6.sin6_addr.s6_addr));
|
sizeof (remote.in6.sin6_addr.s6_addr));
|
||||||
sock_fd = sock_fd6;
|
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sock_fd == INVALID_SOCK_FD)
|
if (local_addr->sock_fd == INVALID_SOCK_FD) {
|
||||||
|
DEBUG_LOG(LOGF_NtpIO, "No socket to send to %s:%d",
|
||||||
|
UTI_IPToString(&remote_addr->ip_addr), remote_addr->port);
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
iov.iov_base = packet;
|
iov.iov_base = packet;
|
||||||
iov.iov_len = packetlen;
|
iov.iov_len = packetlen;
|
||||||
|
@ -500,7 +523,7 @@ send_packet(void *packet, int packetlen, NTP_Remote_Address *remote_addr, NTP_Lo
|
||||||
if (!cmsglen)
|
if (!cmsglen)
|
||||||
msg.msg_control = NULL;
|
msg.msg_control = NULL;
|
||||||
|
|
||||||
if (sendmsg(sock_fd, &msg, 0) < 0 &&
|
if (sendmsg(local_addr->sock_fd, &msg, 0) < 0 &&
|
||||||
#ifdef ENETUNREACH
|
#ifdef ENETUNREACH
|
||||||
errno != ENETUNREACH &&
|
errno != ENETUNREACH &&
|
||||||
#endif
|
#endif
|
||||||
|
|
6
ntp_io.h
6
ntp_io.h
|
@ -37,6 +37,12 @@ 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);
|
||||||
|
|
||||||
|
/* Function to obtain a socket for sending client packets */
|
||||||
|
extern int NIO_GetClientSocket(NTP_Remote_Address *remote_addr);
|
||||||
|
|
||||||
|
/* Function to obtain a socket for sending server/peer packets */
|
||||||
|
extern int NIO_GetServerSocket(NTP_Remote_Address *remote_addr);
|
||||||
|
|
||||||
/* Function to transmit a packet */
|
/* Function to transmit a packet */
|
||||||
extern void NIO_SendNormalPacket(NTP_Packet *packet, NTP_Remote_Address *remote_addr, NTP_Local_Address *local_addr);
|
extern void NIO_SendNormalPacket(NTP_Packet *packet, NTP_Remote_Address *remote_addr, NTP_Local_Address *local_addr);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue