Refactor the post receive method to check the length first.

This patch is in preparation for handling the suffix TLV data. We will
need to use the structure size more than once.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
master
Richard Cochran 2012-06-03 19:56:56 +02:00
parent 018ba27ff8
commit ff44e305b1
1 changed files with 39 additions and 19 deletions

58
msg.c
View File

@ -162,7 +162,7 @@ void msg_get(struct ptp_message *m)
int msg_post_recv(struct ptp_message *m, int cnt) int msg_post_recv(struct ptp_message *m, int cnt)
{ {
int type; int pdulen, type;
if (cnt < sizeof(struct ptp_header)) if (cnt < sizeof(struct ptp_header))
return -1; return -1;
@ -174,43 +174,65 @@ int msg_post_recv(struct ptp_message *m, int cnt)
switch (type) { switch (type) {
case SYNC: case SYNC:
if (cnt < sizeof(struct sync_msg)) pdulen = sizeof(struct sync_msg);
return -1; break;
case DELAY_REQ:
pdulen = sizeof(struct delay_req_msg);
break;
case PDELAY_REQ:
pdulen = sizeof(struct pdelay_req_msg);
break;
case PDELAY_RESP:
pdulen = sizeof(struct pdelay_resp_msg);
break;
case FOLLOW_UP:
pdulen = sizeof(struct follow_up_msg);
break;
case DELAY_RESP:
pdulen = sizeof(struct delay_resp_msg);
break;
case PDELAY_RESP_FOLLOW_UP:
pdulen = sizeof(struct pdelay_resp_fup_msg);
break;
case ANNOUNCE:
pdulen = sizeof(struct announce_msg);
break;
case SIGNALING:
pdulen = sizeof(struct signaling_msg);
break;
case MANAGEMENT:
pdulen = sizeof(struct management_msg);
break;
default:
return -1;
}
if (cnt < pdulen)
return -1;
switch (type) {
case SYNC:
timestamp_post_recv(m, &m->sync.originTimestamp); timestamp_post_recv(m, &m->sync.originTimestamp);
break; break;
case DELAY_REQ: case DELAY_REQ:
if (cnt < sizeof(struct delay_req_msg))
return -1;
break; break;
case PDELAY_REQ: case PDELAY_REQ:
if (cnt < sizeof(struct pdelay_req_msg))
return -1;
break; break;
case PDELAY_RESP: case PDELAY_RESP:
if (cnt < sizeof(struct pdelay_resp_msg))
return -1;
timestamp_post_recv(m, &m->pdelay_resp.requestReceiptTimestamp); timestamp_post_recv(m, &m->pdelay_resp.requestReceiptTimestamp);
port_id_post_recv(&m->pdelay_resp.requestingPortIdentity); port_id_post_recv(&m->pdelay_resp.requestingPortIdentity);
break; break;
case FOLLOW_UP: case FOLLOW_UP:
if (cnt < sizeof(struct follow_up_msg))
return -1;
timestamp_post_recv(m, &m->follow_up.preciseOriginTimestamp); timestamp_post_recv(m, &m->follow_up.preciseOriginTimestamp);
break; break;
case DELAY_RESP: case DELAY_RESP:
if (cnt < sizeof(struct delay_resp_msg))
return -1;
timestamp_post_recv(m, &m->delay_resp.receiveTimestamp); timestamp_post_recv(m, &m->delay_resp.receiveTimestamp);
break; break;
case PDELAY_RESP_FOLLOW_UP: case PDELAY_RESP_FOLLOW_UP:
if (cnt < sizeof(struct pdelay_resp_fup_msg))
return -1;
timestamp_post_recv(m, &m->pdelay_resp_fup.responseOriginTimestamp); timestamp_post_recv(m, &m->pdelay_resp_fup.responseOriginTimestamp);
port_id_post_recv(&m->pdelay_resp_fup.requestingPortIdentity); port_id_post_recv(&m->pdelay_resp_fup.requestingPortIdentity);
break; break;
case ANNOUNCE: case ANNOUNCE:
if (cnt < sizeof(struct announce_msg))
return -1;
clock_gettime(CLOCK_MONOTONIC, &m->ts.host); clock_gettime(CLOCK_MONOTONIC, &m->ts.host);
timestamp_post_recv(m, &m->announce.originTimestamp); timestamp_post_recv(m, &m->announce.originTimestamp);
announce_post_recv(&m->announce); announce_post_recv(&m->announce);
@ -218,8 +240,6 @@ int msg_post_recv(struct ptp_message *m, int cnt)
case SIGNALING: case SIGNALING:
case MANAGEMENT: case MANAGEMENT:
break; break;
default:
return -1;
} }
if (msg_sots_missing(m)) { if (msg_sots_missing(m)) {