Replace byte order with explicit length. Add function for byte order for 64 bits.
Signed-off-by: Erez Geva <erez.geva....@siemens.com> --- clock.c | 4 +- msg.c | 51 ++++++------- nsm.c | 2 +- port.c | 5 +- raw.c | 10 +-- tc.c | 8 +- tlv.c | 207 ++++++++++++++++++++++++++-------------------------- transport.c | 9 ++- udp.c | 6 +- udp6.c | 4 +- util.h | 67 +++++++++++++++++ 11 files changed, 221 insertions(+), 152 deletions(-) diff --git a/clock.c b/clock.c index 7005636..5b3b4d0 100644 --- a/clock.c +++ b/clock.c @@ -255,12 +255,12 @@ void clock_send_notification(struct clock *c, struct ptp_message *msg, if (!event_bitmask_get(s->events, event)) continue; /* send event */ - msg->header.sequenceId = htons(s->sequenceId); + msg->header.sequenceId = hton16(s->sequenceId); s->sequenceId++; msg->management.targetPortIdentity.clockIdentity = s->targetPortIdentity.clockIdentity; msg->management.targetPortIdentity.portNumber = - htons(s->targetPortIdentity.portNumber); + hton16(s->targetPortIdentity.portNumber); msg->address = s->addr; port_forward_to(uds, msg); } diff --git a/msg.c b/msg.c index c4516ad..5676add 100644 --- a/msg.c +++ b/msg.c @@ -25,6 +25,7 @@ #include "contain.h" #include "msg.h" #include "print.h" +#include "util.h" #include "tlv.h" int assume_two_step = 0; @@ -61,37 +62,37 @@ static void pool_debug(const char *str, void *addr) static void announce_pre_send(struct announce_msg *m) { - m->currentUtcOffset = htons(m->currentUtcOffset); + m->currentUtcOffset = hton16(m->currentUtcOffset); m->grandmasterClockQuality.offsetScaledLogVariance = - htons(m->grandmasterClockQuality.offsetScaledLogVariance); - m->stepsRemoved = htons(m->stepsRemoved); + hton16(m->grandmasterClockQuality.offsetScaledLogVariance); + m->stepsRemoved = hton16(m->stepsRemoved); } static void announce_post_recv(struct announce_msg *m) { - m->currentUtcOffset = ntohs(m->currentUtcOffset); + m->currentUtcOffset = ntoh16(m->currentUtcOffset); m->grandmasterClockQuality.offsetScaledLogVariance = - ntohs(m->grandmasterClockQuality.offsetScaledLogVariance); - m->stepsRemoved = ntohs(m->stepsRemoved); + ntoh16(m->grandmasterClockQuality.offsetScaledLogVariance); + m->stepsRemoved = ntoh16(m->stepsRemoved); } static int hdr_post_recv(struct ptp_header *m) { if ((m->ver & MAJOR_VERSION_MASK) != PTP_MAJOR_VERSION) return -EPROTO; - m->messageLength = ntohs(m->messageLength); + m->messageLength = ntoh16(m->messageLength); m->correction = net2host64(m->correction); - m->sourcePortIdentity.portNumber = ntohs(m->sourcePortIdentity.portNumber); - m->sequenceId = ntohs(m->sequenceId); + m->sourcePortIdentity.portNumber = ntoh16(m->sourcePortIdentity.portNumber); + m->sequenceId = ntoh16(m->sequenceId); return 0; } static int hdr_pre_send(struct ptp_header *m) { - m->messageLength = htons(m->messageLength); + m->messageLength = hton16(m->messageLength); m->correction = host2net64(m->correction); - m->sourcePortIdentity.portNumber = htons(m->sourcePortIdentity.portNumber); - m->sequenceId = htons(m->sequenceId); + m->sourcePortIdentity.portNumber = hton16(m->sourcePortIdentity.portNumber); + m->sequenceId = hton16(m->sequenceId); return 0; } @@ -171,12 +172,12 @@ static void msg_tlv_recycle(struct ptp_message *msg) static void port_id_post_recv(struct PortIdentity *pid) { - pid->portNumber = ntohs(pid->portNumber); + pid->portNumber = ntoh16(pid->portNumber); } static void port_id_pre_send(struct PortIdentity *pid) { - pid->portNumber = htons(pid->portNumber); + pid->portNumber = hton16(pid->portNumber); } static int suffix_post_recv(struct ptp_message *msg, int len) @@ -195,8 +196,8 @@ static int suffix_post_recv(struct ptp_message *msg, int len) return -ENOMEM; } extra->tlv = (struct TLV *) ptr; - extra->tlv->type = ntohs(extra->tlv->type); - extra->tlv->length = ntohs(extra->tlv->length); + extra->tlv->type = ntoh16(extra->tlv->type); + extra->tlv->length = ntoh16(extra->tlv->length); if (extra->tlv->length % 2) { tlv_extra_recycle(extra); return -EBADMSG; @@ -227,26 +228,26 @@ static void suffix_pre_send(struct ptp_message *msg) TAILQ_FOREACH(extra, &msg->tlv_list, list) { tlv = extra->tlv; tlv_pre_send(tlv, extra); - tlv->type = htons(tlv->type); - tlv->length = htons(tlv->length); + tlv->type = hton16(tlv->type); + tlv->length = hton16(tlv->length); } msg_tlv_recycle(msg); } static void timestamp_post_recv(struct ptp_message *m, struct Timestamp *ts) { - uint32_t lsb = ntohl(ts->seconds_lsb); - uint16_t msb = ntohs(ts->seconds_msb); + uint32_t lsb = ntoh32(ts->seconds_lsb); + uint16_t msb = ntoh16(ts->seconds_msb); m->ts.pdu.sec = ((uint64_t)lsb) | (((uint64_t)msb) << 32); - m->ts.pdu.nsec = ntohl(ts->nanoseconds); + m->ts.pdu.nsec = ntoh32(ts->nanoseconds); } static void timestamp_pre_send(struct Timestamp *ts) { - ts->seconds_lsb = htonl(ts->seconds_lsb); - ts->seconds_msb = htons(ts->seconds_msb); - ts->nanoseconds = htonl(ts->nanoseconds); + ts->seconds_lsb = hton32(ts->seconds_lsb); + ts->seconds_msb = hton16(ts->seconds_msb); + ts->nanoseconds = hton32(ts->nanoseconds); } /* public methods */ @@ -453,7 +454,7 @@ int msg_pre_send(struct ptp_message *m) case DELAY_RESP: timestamp_pre_send(&m->delay_resp.receiveTimestamp); m->delay_resp.requestingPortIdentity.portNumber = - htons(m->delay_resp.requestingPortIdentity.portNumber); + hton16(m->delay_resp.requestingPortIdentity.portNumber); break; case PDELAY_RESP_FOLLOW_UP: timestamp_pre_send(&m->pdelay_resp_fup.responseOriginTimestamp); diff --git a/nsm.c b/nsm.c index 5aa925b..ebed297 100644 --- a/nsm.c +++ b/nsm.c @@ -139,7 +139,7 @@ static void nsm_handle_msg(struct nsm *nsm, struct ptp_message *msg, FILE *fp) return; } if (msg->header.sequenceId != - ntohs(nsm->nsm_delay_req->header.sequenceId)) { + ntoh16(nsm->nsm_delay_req->header.sequenceId)) { return; } if (!msg_unicast(msg)) { diff --git a/port.c b/port.c index cefe780..d405a2c 100644 --- a/port.c +++ b/port.c @@ -1977,9 +1977,8 @@ void process_delay_resp(struct port *p, struct ptp_message *m) return; } TAILQ_FOREACH(req, &p->delay_req, list) { - if (rsp->hdr.sequenceId == ntohs(req->delay_req.hdr.sequenceId)) { + if (rsp->hdr.sequenceId == ntoh16(req->delay_req.hdr.sequenceId)) break; - } } if (!req) { return; @@ -2205,7 +2204,7 @@ static void port_peer_delay(struct port *p) if (!pid_eq(&rsp->pdelay_resp.requestingPortIdentity, &p->portIdentity)) return; - if (rsp->header.sequenceId != ntohs(req->header.sequenceId)) + if (rsp->header.sequenceId != ntoh16(req->header.sequenceId)) return; t1 = req->hwts.ts; diff --git a/raw.c b/raw.c index 0bd15b0..433b6e1 100644 --- a/raw.c +++ b/raw.c @@ -155,7 +155,7 @@ static int open_socket(const char *name, int event, unsigned char *ptp_dst_mac, struct sockaddr_ll addr; int fd, index; - fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); + fd = socket(PF_PACKET, SOCK_RAW, hton16(ETH_P_ALL)); if (fd < 0) { pr_err("socket failed: %m"); goto no_socket; @@ -167,7 +167,7 @@ static int open_socket(const char *name, int event, unsigned char *ptp_dst_mac, memset(&addr, 0, sizeof(addr)); addr.sll_ifindex = index; addr.sll_family = AF_PACKET; - addr.sll_protocol = htons(ETH_P_ALL); + addr.sll_protocol = hton16(ETH_P_ALL); if (bind(fd, (struct sockaddr *) &addr, sizeof(addr))) { pr_err("bind failed: %m"); goto no_option; @@ -287,12 +287,12 @@ static int raw_recv(struct transport *t, int fd, void *buf, int buflen, return cnt; if (raw->vlan) { - if (ETH_P_1588 == ntohs(hdr->type)) { + if (ntoh16(hdr->type) == ETH_P_1588) { pr_notice("raw: disabling VLAN mode"); raw->vlan = 0; } } else { - if (ETH_P_8021Q == ntohs(hdr->type)) { + if (ntoh16(hdr->type) == ETH_P_8021Q) { pr_notice("raw: switching to VLAN mode"); raw->vlan = 1; } @@ -332,7 +332,7 @@ static int raw_send(struct transport *t, struct fdarray *fda, addr_to_mac(&hdr->dst, addr); addr_to_mac(&hdr->src, &raw->src_addr); - hdr->type = htons(ETH_P_1588); + hdr->type = hton16(ETH_P_1588); cnt = send(fd, ptr, len, 0); if (cnt < 1) { diff --git a/tc.c b/tc.c index d9e4853..1849adc 100644 --- a/tc.c +++ b/tc.c @@ -127,7 +127,7 @@ static void tc_complete_request(struct port *q, struct port *p, } #ifdef DEBUG pr_err("stash delay request from %s to %s seqid %hu residence %lu", - q->log_name, p->log_name, ntohs(req->header.sequenceId), + q->log_name, p->log_name, ntoh16(req->header.sequenceId), (unsigned long) tmv_to_nanoseconds(residence)); #endif msg_get(req); @@ -147,7 +147,7 @@ static void tc_complete_response(struct port *q, struct port *p, #ifdef DEBUG pr_err("complete delay response from %s to %s seqid %hu", - q->log_name, p->log_name, ntohs(resp->header.sequenceId)); + q->log_name, p->log_name, ntoh16(resp->header.sequenceId)); #endif TAILQ_FOREACH(txd, &q->tc_transmitted, list) { type = tc_match_delay(portnum(p), resp, txd); @@ -387,8 +387,8 @@ int tc_forward(struct port *q, struct ptp_message *msg) int cnt; if (q->tc_spanning_tree && msg_type(msg) == ANNOUNCE) { - steps_removed = ntohs(msg->announce.stepsRemoved); - msg->announce.stepsRemoved = htons(1 + steps_removed); + steps_removed = ntoh16(msg->announce.stepsRemoved); + msg->announce.stepsRemoved = hton16(1 + steps_removed); } for (p = clock_first_port(q->clock); p; p = LIST_NEXT(p, list)) { diff --git a/tlv.c b/tlv.c index 879bb7e..69630b3 100644 --- a/tlv.c +++ b/tlv.c @@ -25,11 +25,12 @@ #include "port.h" #include "tlv.h" #include "msg.h" +#include "util.h" -#define HTONS(x) (x) = htons(x) -#define HTONL(x) (x) = htonl(x) -#define NTOHS(x) (x) = ntohs(x) -#define NTOHL(x) (x) = ntohl(x) +#define HTON16(x) { (x) = hton16(x); } +#define HTON32(x) { (x) = hton32(x); } +#define NTOH16(x) { (x) = ntoh16(x); } +#define NTOH32(x) { (x) = ntoh32(x); } #define TLV_LENGTH_INVALID(tlv, type) \ (tlv->length < sizeof(struct type) - sizeof(struct TLV)) @@ -41,37 +42,37 @@ static TAILQ_HEAD(tlv_pool, tlv_extra) tlv_pool = static void scaled_ns_n2h(ScaledNs *sns) { - sns->nanoseconds_msb = ntohs(sns->nanoseconds_msb); + sns->nanoseconds_msb = ntoh16(sns->nanoseconds_msb); sns->nanoseconds_lsb = net2host64(sns->nanoseconds_lsb); - sns->fractional_nanoseconds = ntohs(sns->fractional_nanoseconds); + sns->fractional_nanoseconds = ntoh16(sns->fractional_nanoseconds); } static void scaled_ns_h2n(ScaledNs *sns) { - sns->nanoseconds_msb = htons(sns->nanoseconds_msb); + sns->nanoseconds_msb = hton16(sns->nanoseconds_msb); sns->nanoseconds_lsb = host2net64(sns->nanoseconds_lsb); - sns->fractional_nanoseconds = htons(sns->fractional_nanoseconds); + sns->fractional_nanoseconds = hton16(sns->fractional_nanoseconds); } static void timestamp_host2net(struct Timestamp *t) { - HTONL(t->seconds_lsb); - HTONS(t->seconds_msb); - HTONL(t->nanoseconds); + HTON32(t->seconds_lsb); + HTON16(t->seconds_msb); + HTON32(t->nanoseconds); } static void timestamp_net2host(struct Timestamp *t) { - NTOHL(t->seconds_lsb); - NTOHS(t->seconds_msb); - NTOHL(t->nanoseconds); + NTOH32(t->seconds_lsb); + NTOH16(t->seconds_msb); + NTOH32(t->nanoseconds); } static uint16_t flip16(uint16_t *p) { uint16_t v; memcpy(&v, p, sizeof(v)); - v = htons(v); + v = hton16(v); memcpy(p, &v, sizeof(v)); return v; } @@ -239,15 +240,15 @@ static int mgt_post_recv(struct management_tlv *m, uint16_t data_len, if (data_len != sizeof(struct defaultDS)) goto bad_length; dds = (struct defaultDS *) m->data; - dds->numberPorts = ntohs(dds->numberPorts); + dds->numberPorts = ntoh16(dds->numberPorts); dds->clockQuality.offsetScaledLogVariance = - ntohs(dds->clockQuality.offsetScaledLogVariance); + ntoh16(dds->clockQuality.offsetScaledLogVariance); break; case TLV_CURRENT_DATA_SET: if (data_len != sizeof(struct currentDS)) goto bad_length; cds = (struct currentDS *) m->data; - cds->stepsRemoved = ntohs(cds->stepsRemoved); + cds->stepsRemoved = ntoh16(cds->stepsRemoved); cds->offsetFromMaster = net2host64(cds->offsetFromMaster); cds->meanPathDelay = net2host64(cds->meanPathDelay); break; @@ -256,25 +257,25 @@ static int mgt_post_recv(struct management_tlv *m, uint16_t data_len, goto bad_length; pds = (struct parentDS *) m->data; pds->parentPortIdentity.portNumber = - ntohs(pds->parentPortIdentity.portNumber); + ntoh16(pds->parentPortIdentity.portNumber); pds->observedParentOffsetScaledLogVariance = - ntohs(pds->observedParentOffsetScaledLogVariance); + ntoh16(pds->observedParentOffsetScaledLogVariance); pds->observedParentClockPhaseChangeRate = - ntohl(pds->observedParentClockPhaseChangeRate); + ntoh32(pds->observedParentClockPhaseChangeRate); pds->grandmasterClockQuality.offsetScaledLogVariance = - ntohs(pds->grandmasterClockQuality.offsetScaledLogVariance); + ntoh16(pds->grandmasterClockQuality.offsetScaledLogVariance); break; case TLV_TIME_PROPERTIES_DATA_SET: if (data_len != sizeof(struct timePropertiesDS)) goto bad_length; tp = (struct timePropertiesDS *) m->data; - tp->currentUtcOffset = ntohs(tp->currentUtcOffset); + tp->currentUtcOffset = ntoh16(tp->currentUtcOffset); break; case TLV_PORT_DATA_SET: if (data_len != sizeof(struct portDS)) goto bad_length; p = (struct portDS *) m->data; - p->portIdentity.portNumber = ntohs(p->portIdentity.portNumber); + p->portIdentity.portNumber = ntoh16(p->portIdentity.portNumber); p->peerMeanPathDelay = net2host64(p->peerMeanPathDelay); break; case TLV_TIME_STATUS_NP: @@ -283,38 +284,38 @@ static int mgt_post_recv(struct management_tlv *m, uint16_t data_len, tsn = (struct time_status_np *) m->data; tsn->master_offset = net2host64(tsn->master_offset); tsn->ingress_time = net2host64(tsn->ingress_time); - tsn->cumulativeScaledRateOffset = ntohl(tsn->cumulativeScaledRateOffset); - tsn->scaledLastGmPhaseChange = ntohl(tsn->scaledLastGmPhaseChange); - tsn->gmTimeBaseIndicator = ntohs(tsn->gmTimeBaseIndicator); + tsn->cumulativeScaledRateOffset = ntoh32(tsn->cumulativeScaledRateOffset); + tsn->scaledLastGmPhaseChange = ntoh32(tsn->scaledLastGmPhaseChange); + tsn->gmTimeBaseIndicator = ntoh16(tsn->gmTimeBaseIndicator); scaled_ns_n2h(&tsn->lastGmPhaseChange); - tsn->gmPresent = ntohl(tsn->gmPresent); + tsn->gmPresent = ntoh32(tsn->gmPresent); break; case TLV_GRANDMASTER_SETTINGS_NP: if (data_len != sizeof(struct grandmaster_settings_np)) goto bad_length; gsn = (struct grandmaster_settings_np *) m->data; gsn->clockQuality.offsetScaledLogVariance = - ntohs(gsn->clockQuality.offsetScaledLogVariance); - gsn->utc_offset = ntohs(gsn->utc_offset); + ntoh16(gsn->clockQuality.offsetScaledLogVariance); + gsn->utc_offset = ntoh16(gsn->utc_offset); break; case TLV_PORT_DATA_SET_NP: if (data_len != sizeof(struct port_ds_np)) goto bad_length; pdsnp = (struct port_ds_np *) m->data; - pdsnp->neighborPropDelayThresh = ntohl(pdsnp->neighborPropDelayThresh); - pdsnp->asCapable = ntohl(pdsnp->asCapable); + pdsnp->neighborPropDelayThresh = ntoh32(pdsnp->neighborPropDelayThresh); + pdsnp->asCapable = ntoh32(pdsnp->asCapable); break; case TLV_SUBSCRIBE_EVENTS_NP: if (data_len != sizeof(struct subscribe_events_np)) goto bad_length; sen = (struct subscribe_events_np *)m->data; - sen->duration = ntohs(sen->duration); + sen->duration = ntoh16(sen->duration); break; case TLV_PORT_PROPERTIES_NP: if (data_len < sizeof(struct port_properties_np)) goto bad_length; ppn = (struct port_properties_np *)m->data; - ppn->portIdentity.portNumber = ntohs(ppn->portIdentity.portNumber); + ppn->portIdentity.portNumber = ntoh16(ppn->portIdentity.portNumber); extra_len = sizeof(struct port_properties_np); extra_len += ppn->interface.length; break; @@ -323,7 +324,7 @@ static int mgt_post_recv(struct management_tlv *m, uint16_t data_len, goto bad_length; psn = (struct port_stats_np *)m->data; psn->portIdentity.portNumber = - ntohs(psn->portIdentity.portNumber); + ntoh16(psn->portIdentity.portNumber); extra_len = sizeof(struct port_stats_np); break; case TLV_SAVE_IN_NON_VOLATILE_STORAGE: @@ -373,69 +374,69 @@ static void mgt_pre_send(struct management_tlv *m, struct tlv_extra *extra) break; case TLV_DEFAULT_DATA_SET: dds = (struct defaultDS *) m->data; - dds->numberPorts = htons(dds->numberPorts); + dds->numberPorts = hton16(dds->numberPorts); dds->clockQuality.offsetScaledLogVariance = - htons(dds->clockQuality.offsetScaledLogVariance); + hton16(dds->clockQuality.offsetScaledLogVariance); break; case TLV_CURRENT_DATA_SET: cds = (struct currentDS *) m->data; - cds->stepsRemoved = htons(cds->stepsRemoved); + cds->stepsRemoved = hton16(cds->stepsRemoved); cds->offsetFromMaster = host2net64(cds->offsetFromMaster); cds->meanPathDelay = host2net64(cds->meanPathDelay); break; case TLV_PARENT_DATA_SET: pds = (struct parentDS *) m->data; pds->parentPortIdentity.portNumber = - htons(pds->parentPortIdentity.portNumber); + hton16(pds->parentPortIdentity.portNumber); pds->observedParentOffsetScaledLogVariance = - htons(pds->observedParentOffsetScaledLogVariance); + hton16(pds->observedParentOffsetScaledLogVariance); pds->observedParentClockPhaseChangeRate = - htonl(pds->observedParentClockPhaseChangeRate); + hton32(pds->observedParentClockPhaseChangeRate); pds->grandmasterClockQuality.offsetScaledLogVariance = - htons(pds->grandmasterClockQuality.offsetScaledLogVariance); + hton16(pds->grandmasterClockQuality.offsetScaledLogVariance); break; case TLV_TIME_PROPERTIES_DATA_SET: tp = (struct timePropertiesDS *) m->data; - tp->currentUtcOffset = htons(tp->currentUtcOffset); + tp->currentUtcOffset = hton16(tp->currentUtcOffset); break; case TLV_PORT_DATA_SET: p = (struct portDS *) m->data; - p->portIdentity.portNumber = htons(p->portIdentity.portNumber); + p->portIdentity.portNumber = hton16(p->portIdentity.portNumber); p->peerMeanPathDelay = host2net64(p->peerMeanPathDelay); break; case TLV_TIME_STATUS_NP: tsn = (struct time_status_np *) m->data; tsn->master_offset = host2net64(tsn->master_offset); tsn->ingress_time = host2net64(tsn->ingress_time); - tsn->cumulativeScaledRateOffset = htonl(tsn->cumulativeScaledRateOffset); - tsn->scaledLastGmPhaseChange = htonl(tsn->scaledLastGmPhaseChange); - tsn->gmTimeBaseIndicator = htons(tsn->gmTimeBaseIndicator); + tsn->cumulativeScaledRateOffset = hton32(tsn->cumulativeScaledRateOffset); + tsn->scaledLastGmPhaseChange = hton32(tsn->scaledLastGmPhaseChange); + tsn->gmTimeBaseIndicator = hton16(tsn->gmTimeBaseIndicator); scaled_ns_h2n(&tsn->lastGmPhaseChange); - tsn->gmPresent = htonl(tsn->gmPresent); + tsn->gmPresent = hton32(tsn->gmPresent); break; case TLV_GRANDMASTER_SETTINGS_NP: gsn = (struct grandmaster_settings_np *) m->data; gsn->clockQuality.offsetScaledLogVariance = - htons(gsn->clockQuality.offsetScaledLogVariance); - gsn->utc_offset = htons(gsn->utc_offset); + hton16(gsn->clockQuality.offsetScaledLogVariance); + gsn->utc_offset = hton16(gsn->utc_offset); break; case TLV_PORT_DATA_SET_NP: pdsnp = (struct port_ds_np *) m->data; - pdsnp->neighborPropDelayThresh = htonl(pdsnp->neighborPropDelayThresh); - pdsnp->asCapable = htonl(pdsnp->asCapable); + pdsnp->neighborPropDelayThresh = hton32(pdsnp->neighborPropDelayThresh); + pdsnp->asCapable = hton32(pdsnp->asCapable); break; case TLV_SUBSCRIBE_EVENTS_NP: sen = (struct subscribe_events_np *)m->data; - sen->duration = htons(sen->duration); + sen->duration = hton16(sen->duration); break; case TLV_PORT_PROPERTIES_NP: ppn = (struct port_properties_np *)m->data; - ppn->portIdentity.portNumber = htons(ppn->portIdentity.portNumber); + ppn->portIdentity.portNumber = hton16(ppn->portIdentity.portNumber); break; case TLV_PORT_STATS_NP: psn = (struct port_stats_np *)m->data; psn->portIdentity.portNumber = - htons(psn->portIdentity.portNumber); + hton16(psn->portIdentity.portNumber); break; } } @@ -457,8 +458,8 @@ static int nsm_resp_post_recv(struct tlv_extra *extra) } head = (struct nsm_resp_tlv_head *) tlv; paddr = &head->parent_addr; - NTOHS(paddr->networkProtocol); - NTOHS(paddr->addressLength); + NTOH16(paddr->networkProtocol); + NTOH16(paddr->addressLength); switch (paddr->networkProtocol) { case TRANS_UDP_IPV4: @@ -493,20 +494,20 @@ static int nsm_resp_post_recv(struct tlv_extra *extra) * At this point the alignment only 2 bytes worst case. * So we need to be careful with the 64 bit words. */ - NTOHS(pds->parentPortIdentity.portNumber); - NTOHS(pds->observedParentOffsetScaledLogVariance); - NTOHL(pds->observedParentClockPhaseChangeRate); - NTOHS(pds->grandmasterClockQuality.offsetScaledLogVariance); + NTOH16(pds->parentPortIdentity.portNumber); + NTOH16(pds->observedParentOffsetScaledLogVariance); + NTOH32(pds->observedParentClockPhaseChangeRate); + NTOH16(pds->grandmasterClockQuality.offsetScaledLogVariance); - NTOHS(cds->stepsRemoved); + NTOH16(cds->stepsRemoved); net2host64_unaligned(&cds->offsetFromMaster); net2host64_unaligned(&cds->meanPathDelay); - NTOHS(tp->currentUtcOffset); + NTOH16(tp->currentUtcOffset); - NTOHL(extra->foot->lastsync.seconds_lsb); - NTOHS(extra->foot->lastsync.seconds_msb); - NTOHL(extra->foot->lastsync.nanoseconds); + NTOH32(extra->foot->lastsync.seconds_lsb); + NTOH16(extra->foot->lastsync.seconds_msb); + NTOH32(extra->foot->lastsync.nanoseconds); return 0; } @@ -526,23 +527,23 @@ static void nsm_resp_pre_send(struct tlv_extra *extra) cds = &extra->foot->current; tp = &extra->foot->timeprop; - NTOHS(paddr->networkProtocol); - NTOHS(paddr->addressLength); + NTOH16(paddr->networkProtocol); + NTOH16(paddr->addressLength); - HTONS(pds->parentPortIdentity.portNumber); - HTONS(pds->observedParentOffsetScaledLogVariance); - HTONL(pds->observedParentClockPhaseChangeRate); - HTONS(pds->grandmasterClockQuality.offsetScaledLogVariance); + HTON16(pds->parentPortIdentity.portNumber); + HTON16(pds->observedParentOffsetScaledLogVariance); + HTON32(pds->observedParentClockPhaseChangeRate); + HTON16(pds->grandmasterClockQuality.offsetScaledLogVariance); - HTONS(cds->stepsRemoved); + HTON16(cds->stepsRemoved); host2net64_unaligned(&cds->offsetFromMaster); host2net64_unaligned(&cds->meanPathDelay); - HTONS(tp->currentUtcOffset); + HTON16(tp->currentUtcOffset); - HTONL(extra->foot->lastsync.seconds_lsb); - HTONS(extra->foot->lastsync.seconds_msb); - HTONL(extra->foot->lastsync.nanoseconds); + HTON32(extra->foot->lastsync.seconds_lsb); + HTON16(extra->foot->lastsync.seconds_msb); + HTON32(extra->foot->lastsync.nanoseconds); } static int org_post_recv(struct organization_tlv *org) @@ -558,10 +559,10 @@ static int org_post_recv(struct organization_tlv *org) if (org->length + sizeof(struct TLV) != sizeof(struct follow_up_info_tlv)) goto bad_length; f = (struct follow_up_info_tlv *) org; - f->cumulativeScaledRateOffset = ntohl(f->cumulativeScaledRateOffset); - f->gmTimeBaseIndicator = ntohs(f->gmTimeBaseIndicator); + f->cumulativeScaledRateOffset = ntoh32(f->cumulativeScaledRateOffset); + f->gmTimeBaseIndicator = ntoh16(f->gmTimeBaseIndicator); scaled_ns_n2h(&f->lastGmPhaseChange); - f->scaledLastGmPhaseChange = ntohl(f->scaledLastGmPhaseChange); + f->scaledLastGmPhaseChange = ntoh32(f->scaledLastGmPhaseChange); break; case 2: @@ -585,10 +586,10 @@ static void org_pre_send(struct organization_tlv *org) switch (org->subtype[2]) { case 1: f = (struct follow_up_info_tlv *) org; - f->cumulativeScaledRateOffset = htonl(f->cumulativeScaledRateOffset); - f->gmTimeBaseIndicator = htons(f->gmTimeBaseIndicator); + f->cumulativeScaledRateOffset = hton32(f->cumulativeScaledRateOffset); + f->gmTimeBaseIndicator = hton16(f->gmTimeBaseIndicator); scaled_ns_h2n(&f->lastGmPhaseChange); - f->scaledLastGmPhaseChange = htonl(f->scaledLastGmPhaseChange); + f->scaledLastGmPhaseChange = hton32(f->scaledLastGmPhaseChange); break; } } @@ -607,10 +608,10 @@ static int slave_delay_timing_data_post_revc(struct tlv_extra *extra) n_items = tlv_array_count(extra->tlv, base_size, sizeof(*record)); record = slave_delay->record; - NTOHS(slave_delay->sourcePortIdentity.portNumber); + NTOH16(slave_delay->sourcePortIdentity.portNumber); while (n_items) { - NTOHS(record->sequenceId); + NTOH16(record->sequenceId); timestamp_net2host(&record->delayOriginTimestamp); net2host64_unaligned(&record->totalCorrectionField); timestamp_net2host(&record->delayResponseTimestamp); @@ -631,10 +632,10 @@ static void slave_delay_timing_data_pre_send(struct tlv_extra *extra) n_items = tlv_array_count(extra->tlv, base_size, sizeof(*record)); record = slave_delay->record; - HTONS(slave_delay->sourcePortIdentity.portNumber); + HTON16(slave_delay->sourcePortIdentity.portNumber); while (n_items) { - HTONS(record->sequenceId); + HTON16(record->sequenceId); timestamp_host2net(&record->delayOriginTimestamp); host2net64_unaligned(&record->totalCorrectionField); timestamp_host2net(&record->delayResponseTimestamp); @@ -656,13 +657,13 @@ static int slave_rx_sync_timing_data_post_revc(struct tlv_extra *extra) n_items = tlv_array_count(extra->tlv, base_size, sizeof(*record)); record = slave_data->record; - NTOHS(slave_data->sourcePortIdentity.portNumber); + NTOH16(slave_data->sourcePortIdentity.portNumber); while (n_items) { - NTOHS(record->sequenceId); + NTOH16(record->sequenceId); timestamp_net2host(&record->syncOriginTimestamp); net2host64_unaligned(&record->totalCorrectionField); - NTOHL(record->scaledCumulativeRateOffset); + NTOH32(record->scaledCumulativeRateOffset); timestamp_net2host(&record->syncEventIngressTimestamp); n_items--; record++; @@ -681,13 +682,13 @@ static void slave_rx_sync_timing_data_pre_send(struct tlv_extra *extra) n_items = tlv_array_count(extra->tlv, base_size, sizeof(*record)); record = slave_data->record; - HTONS(slave_data->sourcePortIdentity.portNumber); + HTON16(slave_data->sourcePortIdentity.portNumber); while (n_items) { - HTONS(record->sequenceId); + HTON16(record->sequenceId); timestamp_host2net(&record->syncOriginTimestamp); host2net64_unaligned(&record->totalCorrectionField); - HTONL(record->scaledCumulativeRateOffset); + HTON32(record->scaledCumulativeRateOffset); timestamp_host2net(&record->syncEventIngressTimestamp); n_items--; record++; @@ -725,7 +726,7 @@ static int unicast_negotiation_post_recv(struct tlv_extra *extra) if (!unicast_message_type_valid(request->message_type)) { return -EBADMSG; } - NTOHL(request->durationField); + NTOH32(request->durationField); break; case TLV_GRANT_UNICAST_TRANSMISSION: if (TLV_LENGTH_INVALID(tlv, grant_unicast_xmit_tlv)) { @@ -735,7 +736,7 @@ static int unicast_negotiation_post_recv(struct tlv_extra *extra) if (!unicast_message_type_valid(grant->message_type)) { return -EBADMSG; } - NTOHL(grant->durationField); + NTOH32(grant->durationField); break; case TLV_CANCEL_UNICAST_TRANSMISSION: if (TLV_LENGTH_INVALID(tlv, cancel_unicast_xmit_tlv)) { @@ -767,11 +768,11 @@ static void unicast_negotiation_pre_send(struct TLV *tlv) switch (tlv->type) { case TLV_REQUEST_UNICAST_TRANSMISSION: request = (struct request_unicast_xmit_tlv *) tlv; - HTONL(request->durationField); + HTON32(request->durationField); break; case TLV_GRANT_UNICAST_TRANSMISSION: grant = (struct grant_unicast_xmit_tlv *) tlv; - HTONL(grant->durationField); + HTON32(grant->durationField); break; case TLV_CANCEL_UNICAST_TRANSMISSION: case TLV_ACKNOWLEDGE_CANCEL_UNICAST_TRANSMISSION: @@ -819,7 +820,7 @@ int tlv_post_recv(struct tlv_extra *extra) if (TLV_LENGTH_INVALID(tlv, management_tlv)) goto bad_length; mgt = (struct management_tlv *) tlv; - mgt->id = ntohs(mgt->id); + mgt->id = ntoh16(mgt->id); if (tlv->length > sizeof(mgt->id)) result = mgt_post_recv(mgt, tlv->length - sizeof(mgt->id), extra); break; @@ -827,8 +828,8 @@ int tlv_post_recv(struct tlv_extra *extra) if (TLV_LENGTH_INVALID(tlv, management_error_status)) goto bad_length; mes = (struct management_error_status *) tlv; - mes->error = ntohs(mes->error); - mes->id = ntohs(mes->id); + mes->error = ntoh16(mes->error); + mes->id = ntoh16(mes->id); break; case TLV_ORGANIZATION_EXTENSION: if (TLV_LENGTH_INVALID(tlv, organization_tlv)) @@ -893,12 +894,12 @@ void tlv_pre_send(struct TLV *tlv, struct tlv_extra *extra) mgt = (struct management_tlv *) tlv; if (tlv->length > sizeof(mgt->id)) mgt_pre_send(mgt, extra); - mgt->id = htons(mgt->id); + mgt->id = hton16(mgt->id); break; case TLV_MANAGEMENT_ERROR_STATUS: mes = (struct management_error_status *) tlv; - mes->error = htons(mes->error); - mes->id = htons(mes->id); + mes->error = hton16(mes->error); + mes->id = hton16(mes->id); break; case TLV_ORGANIZATION_EXTENSION: org_pre_send((struct organization_tlv *) tlv); diff --git a/transport.c b/transport.c index 9366fbf..956c4a5 100644 --- a/transport.c +++ b/transport.c @@ -25,6 +25,7 @@ #include "udp.h" #include "udp6.h" #include "uds.h" +#include "util.h" int transport_close(struct transport *t, struct fdarray *fda) { @@ -45,7 +46,7 @@ int transport_recv(struct transport *t, int fd, struct ptp_message *msg) int transport_send(struct transport *t, struct fdarray *fda, enum transport_event event, struct ptp_message *msg) { - int len = ntohs(msg->header.messageLength); + int len = ntoh16(msg->header.messageLength); return t->send(t, fda, event, 0, msg, len, NULL, &msg->hwts); } @@ -53,7 +54,7 @@ int transport_send(struct transport *t, struct fdarray *fda, int transport_peer(struct transport *t, struct fdarray *fda, enum transport_event event, struct ptp_message *msg) { - int len = ntohs(msg->header.messageLength); + int len = ntoh16(msg->header.messageLength); return t->send(t, fda, event, 1, msg, len, NULL, &msg->hwts); } @@ -61,7 +62,7 @@ int transport_peer(struct transport *t, struct fdarray *fda, int transport_sendto(struct transport *t, struct fdarray *fda, enum transport_event event, struct ptp_message *msg) { - int len = ntohs(msg->header.messageLength); + int len = ntoh16(msg->header.messageLength); return t->send(t, fda, event, 0, msg, len, &msg->address, &msg->hwts); } @@ -69,7 +70,7 @@ int transport_sendto(struct transport *t, struct fdarray *fda, int transport_txts(struct fdarray *fda, struct ptp_message *msg) { - int cnt, len = ntohs(msg->header.messageLength); + int cnt, len = ntoh16(msg->header.messageLength); struct hw_timestamp *hwts = &msg->hwts; unsigned char pkt[1600]; diff --git a/udp.c b/udp.c index 826bd12..f563d06 100644 --- a/udp.c +++ b/udp.c @@ -98,8 +98,8 @@ static int open_socket(const char *name, struct in_addr mc_addr[2], short port, memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; - addr.sin_addr.s_addr = htonl(INADDR_ANY); - addr.sin_port = htons(port); + addr.sin_addr.s_addr = hton32(INADDR_ANY); + addr.sin_port = hton16(port); fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); if (fd < 0) { @@ -243,7 +243,7 @@ static int udp_send(struct transport *t, struct fdarray *fda, addr = &addr_buf; } - addr->sin.sin_port = htons(event ? EVENT_PORT : GENERAL_PORT); + addr->sin.sin_port = hton16(event ? EVENT_PORT : GENERAL_PORT); /* * Extend the payload by two, for UDP checksum correction. diff --git a/udp6.c b/udp6.c index ba5482e..18ce38d 100644 --- a/udp6.c +++ b/udp6.c @@ -110,7 +110,7 @@ static int open_socket_ipv6(const char *name, struct in6_addr mc_addr[2], short memset(&addr, 0, sizeof(addr)); addr.sin6_family = AF_INET6; addr.sin6_addr = in6addr_any; - addr.sin6_port = htons(port); + addr.sin6_port = hton16(port); fd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP); if (fd < 0) { @@ -264,7 +264,7 @@ static int udp6_send(struct transport *t, struct fdarray *fda, addr = &addr_buf; } - addr->sin6.sin6_port = htons(event ? EVENT_PORT : GENERAL_PORT); + addr->sin6.sin6_port = hton16(event ? EVENT_PORT : GENERAL_PORT); len += 2; /* Extend the payload by two, for UDP checksum corrections. */ diff --git a/util.h b/util.h index 41e33d4..45cbb96 100644 --- a/util.h +++ b/util.h @@ -22,6 +22,7 @@ #include <string.h> #include <time.h> +#include <endian.h> #include "address.h" #include "ddt.h" @@ -455,4 +456,70 @@ void parray_extend(void ***a, ...); */ int rate_limited(int interval, time_t *last); +/** + * Swap host order to network order of 16 bits unsigned integer. + * + * @param val value to swap. + * @return swaped value. + */ +static inline uint16_t hton16(uint16_t val) +{ + return htobe16(val); +} + +/** + * Swap network order to host order of 16 bits unsigned integer. + * + * @param val value to swap. + * @return swaped value. + */ +static inline uint16_t ntoh16(uint16_t val) +{ + return be16toh(val); +} + +/** + * Swap host order to network order of 32 bits unsigned integer. + * + * @param val value to swap. + * @return swaped value. + */ +static inline uint32_t hton32(uint32_t val) +{ + return htobe32(val); +} + +/** + * Swap network order to host order of 32 bits unsigned integer. + * + * @param val value to swap. + * @return swaped value. + */ +static inline uint32_t ntoh32(uint32_t val) +{ + return be32toh(val); +} + +/** + * Swap host order to network order of 64 bits unsigned integer. + * + * @param val value to swap. + * @return swaped value. + */ +static inline uint64_t hton64(uint64_t val) +{ + return htobe64(val); +} + +/** + * Swap network order to host order of 64 bits unsigned integer. + * + * @param val value to swap. + * @return swaped value. + */ +static inline uint64_t ntoh64(uint64_t val) +{ + return be64toh(val); +} + #endif -- 2.20.1 _______________________________________________ Linuxptp-devel mailing list Linuxptp-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/linuxptp-devel