Create addr module.
[darkstat] / addr.c
1 /* darkstat 3
2 * copyright (c) 2011 Emil Mikulic.
3 *
4 * addr.c: compound IPv4/IPv6 address
5 *
6 * You may use, modify and redistribute this file under the terms of the
7 * GNU General Public License version 2. (see COPYING.GPL)
8 */
9
10 #include "addr.h"
11
12 #include <assert.h>
13 #include <string.h> /* for memcmp */
14
15 int addr_equal(const struct addr * const a, const struct addr * const b)
16 {
17 if (a->family != b->family)
18 return 0;
19 if (a->family == IPv4)
20 return (a->ip.v4 == b->ip.v4);
21 else {
22 assert(a->family == IPv6);
23 return (memcmp(&(a->ip.v6), &(b->ip.v6), sizeof(a->ip.v6)) == 0);
24 }
25 }
26
27 static char _addrstrbuf[INET6_ADDRSTRLEN];
28 const char *addr_to_str(const struct addr * const a)
29 {
30 if (a->family == IPv4) {
31 struct in_addr in;
32 in.s_addr = a->ip.v4;
33 return (inet_ntoa(in));
34 } else {
35 assert(a->family == IPv6);
36 inet_ntop(AF_INET6, &(a->ip.v6), _addrstrbuf, sizeof(_addrstrbuf));
37 return (_addrstrbuf);
38 }
39 }
40
41 /* vim:set ts=3 sw=3 tw=78 et: */