Replace memcmp() with pid_eq() and cid_eq()

Consistenly use the pid_eq() and cid_eq() helper functions. Move the
functions into utils.h (making them inline functions) because they are
used in multiple source files.

Signed-off-by: Michael Walle <michael@walle.cc>
master
Michael Walle 2018-06-21 14:59:24 +02:00 committed by Richard Cochran
parent 4c5d180ab3
commit 6bc77ccf4b
6 changed files with 38 additions and 25 deletions

13
clock.c
View File

@ -131,11 +131,6 @@ static void handle_state_decision_event(struct clock *c);
static int clock_resize_pollfd(struct clock *c, int new_nports);
static void clock_remove_port(struct clock *c, struct port *p);
static int cid_eq(struct ClockIdentity *a, struct ClockIdentity *b)
{
return 0 == memcmp(a, b, sizeof(*a));
}
static void remove_subscriber(struct clock_subscriber *s)
{
LIST_REMOVE(s, list);
@ -157,8 +152,8 @@ static void clock_update_subscription(struct clock *c, struct ptp_message *req,
}
LIST_FOREACH(s, &c->subscribers, list) {
if (!memcmp(&s->targetPortIdentity, &req->header.sourcePortIdentity,
sizeof(struct PortIdentity))) {
if (pid_eq(&s->targetPortIdentity,
&req->header.sourcePortIdentity)) {
/* Found, update the transport address and event
* mask. */
if (!remove) {
@ -196,8 +191,8 @@ static void clock_get_subscription(struct clock *c, struct ptp_message *req,
struct timespec now;
LIST_FOREACH(s, &c->subscribers, list) {
if (!memcmp(&s->targetPortIdentity, &req->header.sourcePortIdentity,
sizeof(struct PortIdentity))) {
if (pid_eq(&s->targetPortIdentity,
&req->header.sourcePortIdentity)) {
memcpy(bitmask, s->events, EVENT_BITMASK_CNT);
clock_gettime(CLOCK_MONOTONIC, &now);
if (s->expiration < now.tv_sec)

View File

@ -807,9 +807,8 @@ static int check_clock_identity(struct node *node, struct ptp_message *msg)
{
if (!node->clock_identity_set)
return 1;
return !memcmp(&node->clock_identity,
&msg->header.sourcePortIdentity.clockIdentity,
sizeof(struct ClockIdentity));
return cid_eq(&node->clock_identity,
&msg->header.sourcePortIdentity.clockIdentity);
}
static int is_msg_mgt(struct ptp_message *msg)

18
port.c
View File

@ -147,11 +147,6 @@ static int msg_source_equal(struct ptp_message *m1, struct foreign_clock *fc)
return 0 == memcmp(id1, id2, sizeof(*id1));
}
int pid_eq(struct PortIdentity *a, struct PortIdentity *b)
{
return 0 == memcmp(a, b, sizeof(*a));
}
int source_pid_eq(struct ptp_message *m1, struct ptp_message *m2)
{
return pid_eq(&m1->header.sourcePortIdentity,
@ -418,7 +413,7 @@ static int net_sync_resp_append(struct port *p, struct ptp_message *m)
pid = dad->pds.parentPortIdentity.clockIdentity;
paddr = (struct PortAddress *)buf;
if (best && memcmp(&cid, &pid, sizeof(cid))) {
if (best && !cid_eq(&cid, &pid)) {
/* Extract the parent's protocol address. */
paddr->networkProtocol = transport_type(best->trp);
paddr->addressLength =
@ -559,10 +554,11 @@ static int path_trace_ignore(struct port *p, struct ptp_message *m)
cnt = path_length(ptt);
cid = clock_identity(p->clock);
for (i = 0; i < cnt; i++) {
if (0 == memcmp(&ptt->cid[i], &cid, sizeof(cid)))
if (cid_eq(&ptt->cid[i], &cid)) {
return 1;
}
}
}
return 0;
}
@ -681,7 +677,7 @@ static int port_ignore(struct port *p, struct ptp_message *m)
c1 = clock_identity(p->clock);
c2 = m->header.sourcePortIdentity.clockIdentity;
if (0 == memcmp(&c1, &c2, sizeof(c1))) {
if (cid_eq(&c1, &c2)) {
return 1;
}
return 0;
@ -724,7 +720,7 @@ static int port_sync_incapable(struct port *p)
}
cid = clock_identity(p->clock);
pid = clock_parent_identity(p->clock);
if (!memcmp(&cid, &pid.clockIdentity, sizeof(cid))) {
if (cid_eq(&cid, &pid.clockIdentity)) {
/*
* We are the GM, but without gmCapable set.
*/
@ -1854,7 +1850,7 @@ void process_follow_up(struct port *p, struct ptp_message *m)
break;
}
master = clock_parent_identity(p->clock);
if (memcmp(&master, &m->header.sourcePortIdentity, sizeof(master))) {
if (!pid_eq(&master, &m->header.sourcePortIdentity)) {
return;
}
@ -2146,7 +2142,7 @@ void process_sync(struct port *p, struct ptp_message *m)
break;
}
master = clock_parent_identity(p->clock);
if (memcmp(&master, &m->header.sourcePortIdentity, sizeof(master))) {
if (!pid_eq(&master, &m->header.sourcePortIdentity)) {
return;
}

View File

@ -164,7 +164,6 @@ int process_pdelay_req(struct port *p, struct ptp_message *m);
int process_pdelay_resp(struct port *p, struct ptp_message *m);
void process_pdelay_resp_fup(struct port *p, struct ptp_message *m);
void process_sync(struct port *p, struct ptp_message *m);
int pid_eq(struct PortIdentity *a, struct PortIdentity *b);
int source_pid_eq(struct ptp_message *m1, struct ptp_message *m2);
void ts_add(tmv_t *ts, Integer64 correction);

2
tc.c
View File

@ -490,7 +490,7 @@ int tc_ignore(struct port *p, struct ptp_message *m)
c1 = clock_identity(p->clock);
c2 = m->header.sourcePortIdentity.clockIdentity;
if (0 == memcmp(&c1, &c2, sizeof(c1))) {
if (cid_eq(&c1, &c2)) {
return 1;
}
return 0;

24
util.h
View File

@ -59,6 +59,18 @@ char *bin2str_impl(Octet *data, int len, char *buf, int buf_len);
*/
char *cid2str(struct ClockIdentity *id);
/**
* Compare two clock identities for equality.
*
* @param a First clock identity.
* @param b Second clock identity.
* @return 1 if identities are equal, 0 otherwise.
*/
static inline int cid_eq(struct ClockIdentity *a, struct ClockIdentity *b)
{
return memcmp(a, b, sizeof(*a)) == 0;
}
/**
* Counts the number of occurrences of a given character.
* @param str String to evaluate.
@ -80,6 +92,18 @@ char *pid2str(struct PortIdentity *id);
char *portaddr2str(struct PortAddress *addr);
/**
* Compare two port identities for equality.
*
* @param a First port identity.
* @param b Second port identity.
* @return 1 if identities are equal, 0 otherwise.
*/
static inline int pid_eq(struct PortIdentity *a, struct PortIdentity *b)
{
return memcmp(a, b, sizeof(*a)) == 0;
}
/**
* Scan a string containing a MAC address and convert it into binary form.
*