-indentation, code cleanup
[oweals/gnunet.git] / src / tun / regex.c
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  */
39 static void
40 iptobinstr (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  */
85 static unsigned int
86 ipv4netmasktoprefixlen (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  */
109 void
110 GNUNET_TUN_ipv4toregexsearch (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  */
131 void
132 GNUNET_TUN_ipv6toregexsearch (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
142 /**
143  * Convert an exit policy to a regular expression.  The exit policy
144  * specifies a set of subnets this peer is willing to serve as an
145  * exit for; the resulting regular expression will match the
146  * IPv4 address strings as returned by 'GNUNET_TUN_ipv4toregexsearch'.
147  *
148  * @param policy exit policy specification
149  * @return regular expression, NULL on error
150  */
151 char *
152 GNUNET_TUN_ipv4policy2regex (const char *policy)
153 {
154   // FIXME: do actual policy parsing here, see #2919
155   return GNUNET_strdup (policy);
156 }
157
158
159 /**
160  * Convert an exit policy to a regular expression.  The exit policy
161  * specifies a set of subnets this peer is willing to serve as an
162  * exit for; the resulting regular expression will match the
163  * IPv4 address strings as returned by 'GNUNET_TUN_ipv4toregexsearch'.
164  *
165  * @param policy exit policy specification
166  * @return regular expression, NULL on error
167  */
168 char *
169 GNUNET_TUN_ipv6policy2regex (const char *policy)
170 {
171   // FIXME: do actual policy parsing here, see #2919
172   return GNUNET_strdup (policy);
173 }
174
175
176 /* end of regex.c */