[Git][NTPsec/ntpsec][master] De-punned struct pkt usage.
Ian Bruene
gitlab at mg.gitlab.com
Thu Apr 26 13:31:38 UTC 2018
Ian Bruene pushed to branch master at NTPsec / ntpsec
Commits:
6c7d219b by Ian Bruene at 2018-04-25T18:40:13Z
De-punned struct pkt usage.
- - - - -
7 changed files:
- include/ntp_fp.h
- include/recvbuff.h
- libntp/recvbuff.c
- ntpd/ntp_control.c
- ntpd/ntp_io.c
- ntpd/ntp_monitor.c
- ntpd/ntp_proto.c
Changes:
=====================================
include/ntp_fp.h
=====================================
--- a/include/ntp_fp.h
+++ b/include/ntp_fp.h
@@ -92,7 +92,7 @@ static inline l_fp lfpinit_u(uint32_t sec, uint32_t frac)
* dispersion values (in local byte order). In network byte order
* it looks like:
*
- * 0 1 2 3
+ * 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Integer Part | Fraction Part |
=====================================
include/recvbuff.h
=====================================
--- a/include/recvbuff.h
+++ b/include/recvbuff.h
@@ -59,6 +59,8 @@ struct recvbuf {
#endif /* REFCLOCK */
};
+extern void unmarshall_pkt(struct pkt *rpkt, struct recvbuf *rbufp);
+
extern void init_recvbuff(unsigned int); /* not really pure */
/* freerecvbuf - make a single recvbuf available for reuse
=====================================
libntp/recvbuff.c
=====================================
--- a/libntp/recvbuff.c
+++ b/libntp/recvbuff.c
@@ -256,3 +256,74 @@ check_gen_fifo_consistency(void *fifo)
REQUIRE(NULL == pf->pptail || pptail == pf->pptail);
}
#endif /* NTP_DEBUG_LISTS */
+
+void
+unmarshall_pkt(struct pkt *rpkt, struct recvbuf *rbufp)
+{
+ rpkt->li_vn_mode = (uint8_t)rbufp->recv_buffer[0];
+ rpkt->stratum = (uint8_t)rbufp->recv_buffer[1];
+ rpkt->ppoll = (uint8_t)rbufp->recv_buffer[2];
+ rpkt->precision = (int8_t)rbufp->recv_buffer[3];
+ // rootdelay
+ rpkt->rootdelay = (u_fp)rbufp->recv_buffer[4] << 24;
+ rpkt->rootdelay |= (u_fp)rbufp->recv_buffer[5] << 16;
+ rpkt->rootdelay |= (u_fp)rbufp->recv_buffer[6] << 8;
+ rpkt->rootdelay |= (u_fp)rbufp->recv_buffer[7];
+ rpkt->rootdelay = ntohl(rpkt->rootdelay);
+ // rootdisp
+ rpkt->rootdisp = (u_fp)rbufp->recv_buffer[8] << 24;
+ rpkt->rootdisp |= (u_fp)rbufp->recv_buffer[9] << 16;
+ rpkt->rootdisp |= (u_fp)rbufp->recv_buffer[10] << 8;
+ rpkt->rootdisp |= (u_fp)rbufp->recv_buffer[11];
+ rpkt->rootdisp = ntohl(rpkt->rootdisp);
+ // refid
+ rpkt->refid = (uint32_t)rbufp->recv_buffer[12] << 24;
+ rpkt->refid |= (uint32_t)rbufp->recv_buffer[13] << 16;
+ rpkt->refid |= (uint32_t)rbufp->recv_buffer[14] << 8;
+ rpkt->refid |= (uint32_t)rbufp->recv_buffer[15];
+ rpkt->refid = ntohl(rpkt->refid);
+ // reftime
+ rpkt->reftime.l_ui = (uint32_t)rbufp->recv_buffer[16] << 24;
+ rpkt->reftime.l_ui |= (uint32_t)rbufp->recv_buffer[17] << 16;
+ rpkt->reftime.l_ui |= (uint32_t)rbufp->recv_buffer[18] << 8;
+ rpkt->reftime.l_ui |= (uint32_t)rbufp->recv_buffer[19];
+ rpkt->reftime.l_ui = ntohl(rpkt->reftime.l_ui);
+ rpkt->reftime.l_uf = (uint32_t)rbufp->recv_buffer[20] << 24;
+ rpkt->reftime.l_uf |= (uint32_t)rbufp->recv_buffer[21] << 16;
+ rpkt->reftime.l_uf |= (uint32_t)rbufp->recv_buffer[22] << 8;
+ rpkt->reftime.l_uf |= (uint32_t)rbufp->recv_buffer[23];
+ rpkt->reftime.l_uf = ntohl(rpkt->reftime.l_uf);
+ // org
+ rpkt->org.l_ui = (uint32_t)rbufp->recv_buffer[24] << 24;
+ rpkt->org.l_ui |= (uint32_t)rbufp->recv_buffer[25] << 16;
+ rpkt->org.l_ui |= (uint32_t)rbufp->recv_buffer[26] << 8;
+ rpkt->org.l_ui |= (uint32_t)rbufp->recv_buffer[27];
+ rpkt->org.l_ui = ntohl(rpkt->org.l_ui);
+ rpkt->org.l_uf = (uint32_t)rbufp->recv_buffer[28] << 24;
+ rpkt->org.l_uf |= (uint32_t)rbufp->recv_buffer[29] << 16;
+ rpkt->org.l_uf |= (uint32_t)rbufp->recv_buffer[30] << 8;
+ rpkt->org.l_uf |= (uint32_t)rbufp->recv_buffer[31];
+ rpkt->org.l_uf = ntohl(rpkt->org.l_uf);
+ // rec
+ rpkt->rec.l_ui = (uint32_t)rbufp->recv_buffer[32] << 24;
+ rpkt->rec.l_ui |= (uint32_t)rbufp->recv_buffer[33] << 16;
+ rpkt->rec.l_ui |= (uint32_t)rbufp->recv_buffer[34] << 8;
+ rpkt->rec.l_ui |= (uint32_t)rbufp->recv_buffer[35];
+ rpkt->rec.l_ui = ntohl(rpkt->rec.l_ui);
+ rpkt->rec.l_uf = (uint32_t)rbufp->recv_buffer[36] << 24;
+ rpkt->rec.l_uf |= (uint32_t)rbufp->recv_buffer[37] << 16;
+ rpkt->rec.l_uf |= (uint32_t)rbufp->recv_buffer[38] << 8;
+ rpkt->rec.l_uf |= (uint32_t)rbufp->recv_buffer[39];
+ rpkt->rec.l_uf = ntohl(rpkt->rec.l_uf);
+ // xmt
+ rpkt->xmt.l_ui = (uint32_t)rbufp->recv_buffer[40] << 24;
+ rpkt->xmt.l_ui |= (uint32_t)rbufp->recv_buffer[41] << 16;
+ rpkt->xmt.l_ui |= (uint32_t)rbufp->recv_buffer[42] << 8;
+ rpkt->xmt.l_ui |= (uint32_t)rbufp->recv_buffer[43];
+ rpkt->xmt.l_ui = ntohl(rpkt->xmt.l_ui);
+ rpkt->xmt.l_uf = (uint32_t)rbufp->recv_buffer[44] << 24;
+ rpkt->xmt.l_uf |= (uint32_t)rbufp->recv_buffer[45] << 16;
+ rpkt->xmt.l_uf |= (uint32_t)rbufp->recv_buffer[46] << 8;
+ rpkt->xmt.l_uf |= (uint32_t)rbufp->recv_buffer[47];
+ rpkt->xmt.l_uf = ntohl(rpkt->xmt.l_uf);
+}
=====================================
ntpd/ntp_control.c
=====================================
--- a/ntpd/ntp_control.c
+++ b/ntpd/ntp_control.c
@@ -703,24 +703,24 @@ init_control(void)
void
unmarshall_ntp_control(struct ntp_control *pkt, struct recvbuf *rbufp)
{
- pkt->li_vn_mode = (uint8_t)rbufp->recv_buffer[0];
- pkt->r_m_e_op = (uint8_t)rbufp->recv_buffer[1];
- pkt->sequence = (uint16_t)rbufp->recv_buffer[2] << 8;
- pkt->sequence |= (uint16_t)rbufp->recv_buffer[3];
- pkt->sequence = ntohs(pkt->sequence);
- pkt->status = (uint16_t)rbufp->recv_buffer[4] << 8;
- pkt->status |= (uint16_t)rbufp->recv_buffer[5];
- pkt->status = ntohs(pkt->status);
- pkt->associd = (uint16_t)rbufp->recv_buffer[6] << 8;
- pkt->associd |= (uint16_t)rbufp->recv_buffer[7];
- pkt->associd = ntohs(pkt->associd);
- pkt->offset = (uint16_t)rbufp->recv_buffer[8] << 8;
- pkt->offset |= (uint16_t)rbufp->recv_buffer[9];
- pkt->offset = ntohs(pkt->offset);
- pkt->count = (uint16_t)rbufp->recv_buffer[10] << 8;
- pkt->count |= (uint16_t)rbufp->recv_buffer[11];
- pkt->count = ntohs(pkt->count);
- memcpy(&pkt->data, rbufp->recv_buffer + 12, 480 + MAX_MAC_LEN);
+ pkt->li_vn_mode = (uint8_t)rbufp->recv_buffer[0];
+ pkt->r_m_e_op = (uint8_t)rbufp->recv_buffer[1];
+ pkt->sequence = (uint16_t)rbufp->recv_buffer[2] << 8;
+ pkt->sequence |= (uint16_t)rbufp->recv_buffer[3];
+ pkt->sequence = ntohs(pkt->sequence);
+ pkt->status = (uint16_t)rbufp->recv_buffer[4] << 8;
+ pkt->status |= (uint16_t)rbufp->recv_buffer[5];
+ pkt->status = ntohs(pkt->status);
+ pkt->associd = (uint16_t)rbufp->recv_buffer[6] << 8;
+ pkt->associd |= (uint16_t)rbufp->recv_buffer[7];
+ pkt->associd = ntohs(pkt->associd);
+ pkt->offset = (uint16_t)rbufp->recv_buffer[8] << 8;
+ pkt->offset |= (uint16_t)rbufp->recv_buffer[9];
+ pkt->offset = ntohs(pkt->offset);
+ pkt->count = (uint16_t)rbufp->recv_buffer[10] << 8;
+ pkt->count |= (uint16_t)rbufp->recv_buffer[11];
+ pkt->count = ntohs(pkt->count);
+ memcpy(&pkt->data, rbufp->recv_buffer + 12, 480 + MAX_MAC_LEN);
}
/*
=====================================
ntpd/ntp_io.c
=====================================
--- a/ntpd/ntp_io.c
+++ b/ntpd/ntp_io.c
@@ -3411,4 +3411,3 @@ init_async_notifications(void)
{
}
#endif
-
=====================================
ntpd/ntp_monitor.c
=====================================
--- a/ntpd/ntp_monitor.c
+++ b/ntpd/ntp_monitor.c
@@ -324,6 +324,7 @@ ntp_monitor(
{
l_fp interval_fp;
struct pkt * pkt;
+ struct pkt pkt_core;
mon_entry * mon;
mon_entry * oldest;
int oldest_age;
@@ -339,12 +340,13 @@ ntp_monitor(
if (mon_enabled == MON_OFF)
return ~(RES_LIMITED | RES_KOD) & flags;
- pkt = &rbufp->recv_pkt;
+ unmarshall_pkt(&pkt_core, rbufp);
+ pkt = &pkt_core;
+
hash = MON_HASH(&rbufp->recv_srcadr);
mode = PKT_MODE(pkt->li_vn_mode);
version = PKT_VERSION(pkt->li_vn_mode);
mon = mon_hash[hash];
-
/*
* We keep track of all traffic for a given IP in one entry,
* otherwise cron'ed ntpdate or similar evades RES_LIMITED.
=====================================
ntpd/ntp_proto.c
=====================================
--- a/ntpd/ntp_proto.c
+++ b/ntpd/ntp_proto.c
@@ -2213,6 +2213,7 @@ fast_xmit(
{
struct pkt xpkt; /* transmit packet structure */
struct pkt *rpkt; /* receive packet structure */
+ struct pkt pkt_core; /* recieve packet copy target */
l_fp xmt_tx, xmt_ty;
size_t sendlen;
@@ -2224,7 +2225,8 @@ fast_xmit(
* control and not strictly specification compliant, but doesn't
* break anything.
*/
- rpkt = &rbufp->recv_pkt;
+ unmarshall_pkt(&pkt_core, rbufp);
+ rpkt = &pkt_core;
/*
* If this is a kiss-o'-death (KoD) packet, show leap
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/6c7d219b37b625577d2e60f2559e4e48e3ac68de
---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/6c7d219b37b625577d2e60f2559e4e48e3ac68de
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20180426/3883bfb7/attachment.html>
More information about the vc
mailing list