Fawkes API Fawkes Development Version
addr_comp.h
1
2/***************************************************************************
3 * addr_comp.h - address comparison
4 *
5 * Created: Tue Dec 13 15:17:43 2016
6 * Copyright 2006-2016 Tim Niemueller [www.niemueller.de]
7 *
8 ****************************************************************************/
9
10/* This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version. A runtime exception applies to
14 * this software (see LICENSE.GPL_WRE file mentioned below for details).
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Library General Public License for more details.
20 *
21 * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22 */
23
24#ifndef _NETCOMM_UTILS_ADDR_COMP_H_
25#define _NETCOMM_UTILS_ADDR_COMP_H_
26
27#include <netinet/in.h>
28
29namespace fawkes {
30
31/** Compare two sockaddr structures.
32 * The comparison is based on address family first, and if the same based on
33 * the IP address. Works for AF_INET and AF_INET6.
34 * @param a first compare argument
35 * @param b second compare argument
36 * @return <0 if a is less than b, 0 if they are the same, or >0 otherwise
37 */
38static inline int
39sock_addr_cmp_addr(const struct sockaddr *a, const struct sockaddr *b)
40{
41 if (a->sa_family != b->sa_family) {
42 return a->sa_family - b->sa_family;
43 } else if (a->sa_family == AF_INET) {
44 return (((sockaddr_in *)a)->sin_addr.s_addr - ((sockaddr_in *)b)->sin_addr.s_addr);
45 } else if (a->sa_family == AF_INET6) {
46 return (memcmp((char *)&((sockaddr_in6 *)a)->sin6_addr,
47 (char *)&((sockaddr_in6 *)a)->sin6_addr,
48 sizeof(in6_addr)));
49 }
50}
51
52/** Compare concept comparator for sockaddr.
53 * @author Tim Niemueller
54 */
56{
57 /** Compare sockaddr structures.
58 * uses sock_addr_cmp_addr().
59 * @param a first compare argument
60 * @param b second compare argument
61 * @return true if a < b, false otherwise
62 */
63 bool
64 operator()(const struct sockaddr *&a, const struct sockaddr *&b) const
65 {
66 return (sock_addr_cmp_addr(a, b) < 0);
67 }
68};
69
70} // end namespace fawkes
71
72#endif
Fawkes library namespace.
static int sock_addr_cmp_addr(const struct sockaddr *a, const struct sockaddr *b)
Compare two sockaddr structures.
Definition: addr_comp.h:39
Compare concept comparator for sockaddr.
Definition: addr_comp.h:56
bool operator()(const struct sockaddr *&a, const struct sockaddr *&b) const
Compare sockaddr structures.
Definition: addr_comp.h:64