util: Add a method to compare binary addresses.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
master
Richard Cochran 2018-04-02 00:07:48 -07:00
parent 40480f3b28
commit bc18131d27
2 changed files with 37 additions and 0 deletions

28
util.c
View File

@ -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;

9
util.h
View File

@ -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;