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