aboutsummaryrefslogtreecommitdiff
path: root/src/tun
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2013-06-20 08:56:24 +0000
committerChristian Grothoff <christian@grothoff.org>2013-06-20 08:56:24 +0000
commitde9409f80dbfc5cc61a28316b271600e9da95cdc (patch)
tree9c2da386cdd794ce89f3ee09bf591440a422d7d1 /src/tun
parent5f7a6c8f6816a826a2dd93eadc5039733f7db606 (diff)
downloadgnunet-de9409f80dbfc5cc61a28316b271600e9da95cdc.tar.gz
gnunet-de9409f80dbfc5cc61a28316b271600e9da95cdc.zip
adding file that was moved here from regex.c and forgotten in earlier commit
Diffstat (limited to 'src/tun')
-rw-r--r--src/tun/regex.c141
1 files changed, 141 insertions, 0 deletions
diff --git a/src/tun/regex.c b/src/tun/regex.c
new file mode 100644
index 000000000..5ec454637
--- /dev/null
+++ b/src/tun/regex.c
@@ -0,0 +1,141 @@
1/*
2 This file is part of GNUnet
3 (C) 2012, 2013 Christian Grothoff (and other contributing authors)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20/**
21 * @file src/tun/regex.c
22 * @brief functions to convert IP networks to regexes
23 * @author Maximilian Szengel
24 */
25#include "platform.h"
26#include "gnunet_util_lib.h"
27#include "gnunet_tun_lib.h"
28
29
30/**
31 * Create a string with binary IP notation for the given 'addr' in 'str'.
32 *
33 * @param af address family of the given 'addr'.
34 * @param addr address that should be converted to a string.
35 * struct in_addr * for IPv4 and struct in6_addr * for IPv6.
36 * @param str string that will contain binary notation of 'addr'. Expected
37 * to be at least 33 bytes long for IPv4 and 129 bytes long for IPv6.
38 */
39static void
40iptobinstr (const int af, const void *addr, char *str)
41{
42 int i;
43
44 switch (af)
45 {
46 case AF_INET:
47 {
48 uint32_t b = htonl (((struct in_addr *) addr)->s_addr);
49
50 str[32] = '\0';
51 str += 31;
52 for (i = 31; i >= 0; i--)
53 {
54 *str = (b & 1) + '0';
55 str--;
56 b >>= 1;
57 }
58 break;
59 }
60 case AF_INET6:
61 {
62 struct in6_addr b = *(const struct in6_addr *) addr;
63
64 str[128] = '\0';
65 str += 127;
66 for (i = 127; i >= 0; i--)
67 {
68 *str = (b.s6_addr[i / 8] & 1) + '0';
69 str--;
70 b.s6_addr[i / 8] >>= 1;
71 }
72 break;
73 }
74 }
75}
76
77
78/**
79 * Get the ipv4 network prefix from the given 'netmask'.
80 *
81 * @param netmask netmask for which to get the prefix len.
82 *
83 * @return length of ipv4 prefix for 'netmask'.
84 */
85static unsigned int
86ipv4netmasktoprefixlen (const char *netmask)
87{
88 struct in_addr a;
89 unsigned int len;
90 uint32_t t;
91
92 if (1 != inet_pton (AF_INET, netmask, &a))
93 return 0;
94 len = 32;
95 for (t = htonl (~a.s_addr); 0 != t; t >>= 1)
96 len--;
97 return len;
98}
99
100
101/**
102 * Create a regex in 'rxstr' from the given 'ip' and 'netmask'.
103 *
104 * @param ip IPv4 representation.
105 * @param netmask netmask for the ip.
106 * @param rxstr generated regex, must be at least GNUNET_REGEX_IPV4_REGEXLEN
107 * bytes long.
108 */
109void
110GNUNET_TUN_ipv4toregex (const struct in_addr *ip, const char *netmask,
111 char *rxstr)
112{
113 unsigned int pfxlen;
114
115 pfxlen = ipv4netmasktoprefixlen (netmask);
116 iptobinstr (AF_INET, ip, rxstr);
117 rxstr[pfxlen] = '\0';
118 if (pfxlen < 32)
119 strcat (rxstr, "(0|1)+");
120}
121
122
123/**
124 * Create a regex in 'rxstr' from the given 'ipv6' and 'prefixlen'.
125 *
126 * @param ipv6 IPv6 representation.
127 * @param prefixlen length of the ipv6 prefix.
128 * @param rxstr generated regex, must be at least GNUNET_REGEX_IPV6_REGEXLEN
129 * bytes long.
130 */
131void
132GNUNET_TUN_ipv6toregex (const struct in6_addr *ipv6, unsigned int prefixlen,
133 char *rxstr)
134{
135 iptobinstr (AF_INET6, ipv6, rxstr);
136 rxstr[prefixlen] = '\0';
137 if (prefixlen < 128)
138 strcat (rxstr, "(0|1)+");
139}
140
141/* end of regex.c */