[quagga-dev 7276] rpng_nexthop.h -- IPv6 Address Comparison -- doesn't look right...

Chris Hall chris.hall at highwayman.com
Wed Sep 9 09:49:49 BST 2009


I've been staring at this fragment from rpng_nexthop.h, and it doesn't
look right to me...

static inline int
addr6_cmp(struct in6_addr *A, struct in6_addr *B) {

#define a(i) A->s6_addr32[i]
#define b(i) B->s6_addr32[i]

  if (a(3) > b(3))
    return 1;
  else if ((a(3) == b(3)) && (a(2) > b(2)))
    return 1;
  else if ((a(3) == b(3)) && (a(2) == b(2)) && (a(1) > b(1)))
    return 1;
  else if ((a(3) == b(3)) && (a(2) == b(2)) && (a(1) == b(1)) && (a(0) > b(0)))
    return 1;

  if ((a(3) == b(3)) && (a(2) == b(2)) && (a(1) == b(1)) && (a(0) == b(0)))
    return 0;

  return -1;
}

Which appears to assume that the address is little-endian, in units of
32-bit words...  but I cannot find anything which makes it so.

AFAIKS it doesn't much matter how the addresses sort, but this does look
odd.  To sort in numerical order I would:

static inline int
addr6_cmp_x(struct in6_addr *A, struct in6_addr *B) {

#define ADDR6_CMP(i) if (a(i) != b(i)) \
                        return ntohl(a(i)) < ntohl(b(i)) ? -1 : +1
  ADDR6_CMP(0) ;
  ADDR6_CMP(1) ;
  ADDR6_CMP(2) ;
  ADDR6_CMP(3) ;
  return 0;
}

which also avoids the repeated '==' comparisons.
-- 
Chris Hall               highwayman.com            +44 7970 277 383
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 470 bytes
Desc: not available
URL: <http://lists.quagga.net/pipermail/quagga-dev/attachments/20090909/3cd1cace/attachment-0001.sig>


More information about the Quagga-dev mailing list