[Git][NTPsec/ntpsec][master] 3 commits: Added declaration line for unmarshall_ntp_control.
Ian Bruene
gitlab at mg.gitlab.com
Thu Apr 26 14:41:17 UTC 2018
Ian Bruene pushed to branch master at NTPsec / ntpsec
Commits:
f24435f3 by Ian Bruene at 2018-04-26T14:02:26Z
Added declaration line for unmarshall_ntp_control.
- - - - -
a93b9396 by Ian Bruene at 2018-04-26T14:35:43Z
Removed variable names from function declaration.
- - - - -
04084f99 by Ian Bruene at 2018-04-26T14:36:24Z
Refactored unmarshalling functions.
- - - - -
3 changed files:
- include/recvbuff.h
- libntp/recvbuff.c
- ntpd/ntp_control.c
Changes:
=====================================
include/recvbuff.h
=====================================
--- a/include/recvbuff.h
+++ b/include/recvbuff.h
@@ -59,7 +59,7 @@ struct recvbuf {
#endif /* REFCLOCK */
};
-extern void unmarshall_pkt(struct pkt *rpkt, struct recvbuf *rbufp);
+extern void unmarshall_pkt(struct pkt *, struct recvbuf *);
extern void init_recvbuff(unsigned int); /* not really pure */
=====================================
libntp/recvbuff.c
=====================================
--- a/libntp/recvbuff.c
+++ b/libntp/recvbuff.c
@@ -18,6 +18,10 @@ static unsigned long total_recvbufs; /* total recvbufs currently in use */
static unsigned long lowater_adds; /* # of times we have added memory */
static unsigned long buffer_shortfall; /* # of missed free receive buffers
between replenishments */
+/*
+ * Unmarshalling helper
+ */
+extern uint32_t extract_32bits_from_stream(uint8_t *);
static DECL_FIFO_ANCHOR(recvbuf_t) full_recv_fifo;
static recvbuf_t * free_recv_list;
@@ -265,65 +269,33 @@ unmarshall_pkt(struct pkt *rpkt, struct recvbuf *rbufp)
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);
+ rpkt->rootdelay = extract_32bits_from_stream(&rbufp->recv_buffer[4]);
// 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);
+ rpkt->rootdisp = extract_32bits_from_stream(&rbufp->recv_buffer[8]);
// 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);
+ rpkt->refid = extract_32bits_from_stream(&rbufp->recv_buffer[12]);
// 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);
+ rpkt->reftime.l_ui = extract_32bits_from_stream(&rbufp->recv_buffer[16]);
+ rpkt->reftime.l_uf = extract_32bits_from_stream(&rbufp->recv_buffer[20]);
// 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);
+ rpkt->org.l_ui = extract_32bits_from_stream(&rbufp->recv_buffer[24]);
+ rpkt->org.l_uf = extract_32bits_from_stream(&rbufp->recv_buffer[28]);
// 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);
+ rpkt->rec.l_ui = extract_32bits_from_stream(&rbufp->recv_buffer[32]);
+ rpkt->rec.l_uf = extract_32bits_from_stream(&rbufp->recv_buffer[36]);
// 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);
+ rpkt->xmt.l_ui = extract_32bits_from_stream(&rbufp->recv_buffer[40]);
+ rpkt->xmt.l_uf = extract_32bits_from_stream(&rbufp->recv_buffer[44]);
+}
+
+uint32_t
+extract_32bits_from_stream(uint8_t *addr)
+{
+ uint32_t var = 0;
+ var = (uint32_t)*addr << 24;
+ var |= (uint32_t)*(addr + 1) << 16;
+ var |= (uint32_t)*(addr + 2) << 8;
+ var |= (uint32_t)*(addr + 3);
+ var = ntohl(var);
+ return var;
}
=====================================
ntpd/ntp_control.c
=====================================
--- a/ntpd/ntp_control.c
+++ b/ntpd/ntp_control.c
@@ -43,11 +43,13 @@ struct ctl_proc {
#define AUTH 1
void (*handler) (struct recvbuf *, int); /* handle request */
};
-
+
/*
* Request processing routines
*/
+static void unmarshall_ntp_control(struct ntp_control *, struct recvbuf *);
+static uint16_t extract_16bits_from_stream(uint8_t *);
static void ctl_error (uint8_t);
#ifdef REFCLOCK
static unsigned short ctlclkstatus (struct refclockstat *);
@@ -705,24 +707,24 @@ 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);
+ pkt->sequence = extract_16bits_from_stream(&rbufp->recv_buffer[2]);
+ pkt->status = extract_16bits_from_stream(&rbufp->recv_buffer[4]);
+ pkt->associd = extract_16bits_from_stream(&rbufp->recv_buffer[6]);
+ pkt->offset = extract_16bits_from_stream(&rbufp->recv_buffer[8]);
+ pkt->count = extract_16bits_from_stream(&rbufp->recv_buffer[10]);
memcpy(&pkt->data, rbufp->recv_buffer + 12, 480 + MAX_MAC_LEN);
}
+uint16_t
+extract_16bits_from_stream(uint8_t *addr)
+{
+ uint16_t var = 0;
+ var = (uint16_t)*addr << 8;
+ var |= (uint16_t)*(addr + 1);
+ var = ntohs(var);
+ return var;
+}
+
/*
* ctl_error - send an error response for the current request
*/
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/6c7d219b37b625577d2e60f2559e4e48e3ac68de...04084f9923ac5227d0d260ccee495fea11803049
---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/6c7d219b37b625577d2e60f2559e4e48e3ac68de...04084f9923ac5227d0d260ccee495fea11803049
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/27e67986/attachment.html>
More information about the vc
mailing list