diff options
author | Maximilian Szengel <gnunet@maxsz.de> | 2012-09-26 21:22:44 +0000 |
---|---|---|
committer | Maximilian Szengel <gnunet@maxsz.de> | 2012-09-26 21:22:44 +0000 |
commit | c5c3d4a753fac3521e824c8eb0350c2e2587ff83 (patch) | |
tree | 31f9d42c204edc1b98dee7abdbc69c07c70300a2 /src/regex | |
parent | ed40c38220a0c32976a0fda56aec1e7499df858b (diff) | |
download | gnunet-c5c3d4a753fac3521e824c8eb0350c2e2587ff83.tar.gz gnunet-c5c3d4a753fac3521e824c8eb0350c2e2587ff83.zip |
ip/prefix to regex
Diffstat (limited to 'src/regex')
-rw-r--r-- | src/regex/regex.c | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/src/regex/regex.c b/src/regex/regex.c index 26ad38d34..19b55fca3 100644 --- a/src/regex/regex.c +++ b/src/regex/regex.c | |||
@@ -2939,3 +2939,111 @@ GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a, | |||
2939 | iterator_cls); | 2939 | iterator_cls); |
2940 | iterate_edge (a->start, iterator, iterator_cls); | 2940 | iterate_edge (a->start, iterator, iterator_cls); |
2941 | } | 2941 | } |
2942 | |||
2943 | |||
2944 | /** | ||
2945 | * Create a string with binary IP notation for the given 'addr' in 'str'. | ||
2946 | * | ||
2947 | * @param af address family of the given 'addr'. | ||
2948 | * @param addr address that should be converted to a string. | ||
2949 | * struct in_addr * for IPv4 and struct in6_addr * for IPv6. | ||
2950 | * @param str string that will contain binary notation of 'addr'. Expected | ||
2951 | * to be at least 33 bytes long for IPv4 and 129 bytes long for IPv6. | ||
2952 | */ | ||
2953 | static void | ||
2954 | iptobinstr (const int af, const void *addr, char *str) | ||
2955 | { | ||
2956 | unsigned int i; | ||
2957 | |||
2958 | switch (af) | ||
2959 | { | ||
2960 | case AF_INET: | ||
2961 | { | ||
2962 | uint32_t b = htonl (((struct in_addr *) addr)->s_addr); | ||
2963 | |||
2964 | str[32] = '\0'; | ||
2965 | str += 31; | ||
2966 | for (i = 31; i >= 0; i--) | ||
2967 | { | ||
2968 | *str-- = (b & 1) + '0'; | ||
2969 | b >>= 1; | ||
2970 | } | ||
2971 | break; | ||
2972 | } | ||
2973 | case AF_INET6: | ||
2974 | { | ||
2975 | struct in6_addr b = *(struct in6_addr *) addr; | ||
2976 | |||
2977 | str[128] = '\0'; | ||
2978 | str += 127; | ||
2979 | for (i = 127; i >= 0; i--) | ||
2980 | { | ||
2981 | *str-- = (b.s6_addr[i / 8] & 1) + '0'; | ||
2982 | b.s6_addr[i / 8] >>= 1; | ||
2983 | } | ||
2984 | break; | ||
2985 | } | ||
2986 | } | ||
2987 | } | ||
2988 | |||
2989 | |||
2990 | /** | ||
2991 | * Get the ipv4 network prefix from the given 'netmask'. | ||
2992 | * | ||
2993 | * @param netmask netmask for which to get the prefix len. | ||
2994 | * | ||
2995 | * @return length of ipv4 prefix for 'netmask'. | ||
2996 | */ | ||
2997 | static unsigned int | ||
2998 | ipv4netmasktoprefixlen (const char *netmask) | ||
2999 | { | ||
3000 | struct in_addr a; | ||
3001 | unsigned int len; | ||
3002 | uint32_t t; | ||
3003 | |||
3004 | if (1 != inet_pton (AF_INET, netmask, &a)) | ||
3005 | return 0; | ||
3006 | |||
3007 | for (len = 32, t = htonl (~a.s_addr); t & 1; t >>= 1, len--) ; | ||
3008 | |||
3009 | return len; | ||
3010 | } | ||
3011 | |||
3012 | |||
3013 | /** | ||
3014 | * Create a regex in 'rxstr' from the given 'ip' and 'netmask'. | ||
3015 | * | ||
3016 | * @param ip IPv4 representation. | ||
3017 | * @param netmask netmask for the ip. | ||
3018 | * @param rxstr generated regex, must be at least GNUNET_REGEX_IPV4_REGEXLEN | ||
3019 | * bytes long. | ||
3020 | */ | ||
3021 | void | ||
3022 | GNUNET_REGEX_ipv4toregex (const struct in_addr *ip, const char *netmask, | ||
3023 | char *rxstr) | ||
3024 | { | ||
3025 | unsigned int pfxlen; | ||
3026 | |||
3027 | pfxlen = ipv4netmasktoprefixlen (netmask); | ||
3028 | iptobinstr (AF_INET, ip, rxstr); | ||
3029 | rxstr[pfxlen] = '\0'; | ||
3030 | strcat (rxstr, "(0|1)*"); | ||
3031 | } | ||
3032 | |||
3033 | |||
3034 | /** | ||
3035 | * Create a regex in 'rxstr' from the given 'ipv6' and 'prefixlen'. | ||
3036 | * | ||
3037 | * @param ipv6 IPv6 representation. | ||
3038 | * @param prefixlen length of the ipv6 prefix. | ||
3039 | * @param rxstr generated regex, must be at least GNUNET_REGEX_IPV6_REGEXLEN | ||
3040 | * bytes long. | ||
3041 | */ | ||
3042 | void | ||
3043 | GNUNET_REGEX_ipv6toregex (const struct in6_addr *ipv6, | ||
3044 | const unsigned int prefixlen, char *rxstr) | ||
3045 | { | ||
3046 | iptobinstr (AF_INET6, ipv6, rxstr); | ||
3047 | rxstr[prefixlen] = '\0'; | ||
3048 | strcat (rxstr, "(0|1)*"); | ||
3049 | } | ||