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;