From bc18131d277d9b4b32eed8a49bedba0bb39be3b2 Mon Sep 17 00:00:00 2001 From: Richard Cochran Date: Mon, 2 Apr 2018 00:07:48 -0700 Subject: [PATCH] util: Add a method to compare binary addresses. Signed-off-by: Richard Cochran --- util.c | 28 ++++++++++++++++++++++++++++ util.h | 9 +++++++++ 2 files changed, 37 insertions(+) diff --git a/util.c b/util.c index 799147a..73fb37e 100644 --- a/util.c +++ b/util.c @@ -69,6 +69,34 @@ const char *ev_str[] = { "RS_PASSIVE", }; +int addreq(enum transport_type type, struct address *a, struct address *b) +{ + void *bufa, *bufb; + int len; + + switch (type) { + case TRANS_UDP_IPV4: + bufa = &a->sin.sin_addr; + bufb = &b->sin.sin_addr; + len = sizeof(a->sin); + break; + case TRANS_IEEE_802_3: + bufa = &a->sll.sll_addr; + bufb = &b->sll.sll_addr; + len = MAC_LEN; + break; + case TRANS_UDS: + case TRANS_UDP_IPV6: + case TRANS_DEVICENET: + case TRANS_CONTROLNET: + case TRANS_PROFINET: + default: + pr_err("sorry, cannot compare addresses for this transport"); + return 0; + } + return memcmp(bufa, bufb, len) == 0 ? 1 : 0; +} + char *bin2str_impl(Octet *data, int len, char *buf, int buf_len) { int i, offset = 0; diff --git a/util.h b/util.h index 29ba5ca..1ab1b3f 100644 --- a/util.h +++ b/util.h @@ -41,6 +41,15 @@ extern const char *ps_str[]; */ extern const char *ev_str[]; +/** + * Compares two binary addresses for equality. + * @param type One of the enumerated transport types. + * @param a One address to compare. + * @param b The second address to compare. + * @return One if the addresses are identical, zero otherwise. + */ +int addreq(enum transport_type type, struct address *a, struct address *b); + static inline uint16_t align16(uint16_t *p) { uint16_t v;