Update ChangeLog for 3.0.717.
[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 <arpa/inet.h> /* for inet_ntop */
13 #include <assert.h>
14 #include <string.h> /* for memcmp */
15 #include <netdb.h> /* for getaddrinfo */
16
17 int addr_equal(const struct addr * const a, const struct addr * const b)
18 {
19 if (a->family != b->family)
20 return 0;
21 if (a->family == IPv4)
22 return (a->ip.v4 == b->ip.v4);
23 else {
24 assert(a->family == IPv6);
25 return (memcmp(&(a->ip.v6), &(b->ip.v6), sizeof(a->ip.v6)) == 0);
26 }
27 }
28
29 static char _addrstrbuf[INET6_ADDRSTRLEN];
30 const char *addr_to_str(const struct addr * const a)
31 {
32 if (a->family == IPv4) {
33 struct in_addr in;
34 in.s_addr = a->ip.v4;
35 return (inet_ntoa(in));
36 } else {
37 assert(a->family == IPv6);
38 inet_ntop(AF_INET6, &(a->ip.v6), _addrstrbuf, sizeof(_addrstrbuf));
39 return (_addrstrbuf);
40 }
41 }
42
43 int str_to_addr(const char *s, struct addr *a)
44 {
45 struct addrinfo hints, *ai;
46 int ret;
47
48 memset(&hints, 0, sizeof(hints));
49 hints.ai_family = AF_UNSPEC;
50 hints.ai_flags = AI_NUMERICHOST;
51
52 if ((ret = getaddrinfo(s, NULL, &hints, &ai)) != 0)
53 return (ret);
54
55 if (ai->ai_family == AF_INET) {
56 a->family = IPv4;
57 a->ip.v4 = ((const struct sockaddr_in *)ai->ai_addr)->sin_addr.s_addr;
58 } else if (ai->ai_family == AF_INET6) {
59 a->family = IPv6;
60 memcpy(&(a->ip.v6),
61 ((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr.s6_addr,
62 sizeof(a->ip.v6));
63 } else {
64 ret = EAI_FAMILY;
65 }
66
67 freeaddrinfo(ai);
68 return (ret);
69 }
70
71 void addr_mask(struct addr *a, const struct addr * const mask)
72 {
73 assert(a->family == mask->family);
74 if (a->family == IPv4)
75 a->ip.v4 &= mask->ip.v4;
76 else {
77 size_t i;
78
79 assert(a->family == IPv6);
80 for (i=0; i<sizeof(a->ip.v6.s6_addr); i++)
81 a->ip.v6.s6_addr[i] &= mask->ip.v6.s6_addr[i];
82 }
83 }
84
85 int addr_inside(const struct addr * const a,
86 const struct addr * const net, const struct addr * const mask)
87 {
88 struct addr masked;
89
90 assert(a->family == net->family);
91 assert(a->family == mask->family);
92
93 masked = *a;
94 addr_mask(&masked, mask);
95 return (addr_equal(&masked, net));
96 }
97
98 /* vim:set ts=3 sw=3 tw=78 et: */