diff options
Diffstat (limited to 'src/cidr.c')
-rw-r--r-- | src/cidr.c | 62 |
1 files changed, 31 insertions, 31 deletions
@@ -1,4 +1,4 @@ | |||
1 | /* grecs - Gray's Extensible Configuration System | 1 | /* argot - Gray's Extensible Configuration System |
2 | Copyright (C) 2007-2016 Sergey Poznyakoff | 2 | Copyright (C) 2007-2016 Sergey Poznyakoff |
3 | 3 | ||
4 | Grecs is free software; you can redistribute it and/or modify it | 4 | Grecs is free software; you can redistribute it and/or modify it |
@@ -23,7 +23,7 @@ | |||
23 | #include <netinet/in.h> | 23 | #include <netinet/in.h> |
24 | #include <arpa/inet.h> | 24 | #include <arpa/inet.h> |
25 | #include <stdlib.h> | 25 | #include <stdlib.h> |
26 | #include "grecs.h" | 26 | #include "argot.h" |
27 | 27 | ||
28 | static void | 28 | static void |
29 | uint32_to_bytes (unsigned char *bytes, uint32_t u) | 29 | uint32_to_bytes (unsigned char *bytes, uint32_t u) |
@@ -37,7 +37,7 @@ uint32_to_bytes (unsigned char *bytes, uint32_t u) | |||
37 | } | 37 | } |
38 | 38 | ||
39 | int | 39 | int |
40 | grecs_inaddr_to_bytes(int af, void *buf, unsigned char *bytes) | 40 | argot_inaddr_to_bytes(int af, void *buf, unsigned char *bytes) |
41 | { | 41 | { |
42 | uint32_t u; | 42 | uint32_t u; |
43 | 43 | ||
@@ -55,7 +55,7 @@ grecs_inaddr_to_bytes(int af, void *buf, unsigned char *bytes) | |||
55 | } | 55 | } |
56 | 56 | ||
57 | int | 57 | int |
58 | grecs_sockaddr_to_bytes(unsigned char *bytes, struct sockaddr const *sa) | 58 | argot_sockaddr_to_bytes(unsigned char *bytes, struct sockaddr const *sa) |
59 | { | 59 | { |
60 | switch (sa->sa_family) { | 60 | switch (sa->sa_family) { |
61 | case AF_INET: | 61 | case AF_INET: |
@@ -71,19 +71,19 @@ grecs_sockaddr_to_bytes(unsigned char *bytes, struct sockaddr const *sa) | |||
71 | } | 71 | } |
72 | 72 | ||
73 | int | 73 | int |
74 | grecs_sockaddr_to_cidr(struct grecs_cidr *cidr, const struct sockaddr *sa) | 74 | argot_sockaddr_to_cidr(struct argot_cidr *cidr, const struct sockaddr *sa) |
75 | { | 75 | { |
76 | unsigned char address[GRECS_INADDR_BYTES]; | 76 | unsigned char address[ARGOT_INADDR_BYTES]; |
77 | int len; | 77 | int len; |
78 | int i; | 78 | int i; |
79 | 79 | ||
80 | len = grecs_sockaddr_to_bytes(address, sa); | 80 | len = argot_sockaddr_to_bytes(address, sa); |
81 | if (len == 0) | 81 | if (len == 0) |
82 | return -1; | 82 | return -1; |
83 | cidr->family = sa->sa_family; | 83 | cidr->family = sa->sa_family; |
84 | cidr->len = len; | 84 | cidr->len = len; |
85 | memcpy(cidr->address, address, sizeof(cidr->address)); | 85 | memcpy(cidr->address, address, sizeof(cidr->address)); |
86 | for (i = 0; i < GRECS_INADDR_BYTES; i++) | 86 | for (i = 0; i < ARGOT_INADDR_BYTES; i++) |
87 | cidr->netmask[i] = 0xff; | 87 | cidr->netmask[i] = 0xff; |
88 | return 0; | 88 | return 0; |
89 | } | 89 | } |
@@ -96,21 +96,21 @@ masklen_to_netmask(unsigned char *buf, size_t len, size_t masklen) | |||
96 | cnt = masklen / 8; | 96 | cnt = masklen / 8; |
97 | for (i = 0; i < cnt; i++) | 97 | for (i = 0; i < cnt; i++) |
98 | buf[i] = 0xff; | 98 | buf[i] = 0xff; |
99 | if (i == GRECS_INADDR_BYTES) | 99 | if (i == ARGOT_INADDR_BYTES) |
100 | return; | 100 | return; |
101 | cnt = 8 - masklen % 8; | 101 | cnt = 8 - masklen % 8; |
102 | buf[i++] = (0xff >> cnt) << cnt; | 102 | buf[i++] = (0xff >> cnt) << cnt; |
103 | for (; i < GRECS_INADDR_BYTES; i++) | 103 | for (; i < ARGOT_INADDR_BYTES; i++) |
104 | buf[i] = 0; | 104 | buf[i] = 0; |
105 | } | 105 | } |
106 | 106 | ||
107 | int | 107 | int |
108 | grecs_str_to_cidr(struct grecs_cidr *pcidr, const char *str, | 108 | argot_str_to_cidr(struct argot_cidr *pcidr, const char *str, |
109 | grecs_locus_t const *locus) | 109 | argot_locus_t const *locus) |
110 | { | 110 | { |
111 | int rc; | 111 | int rc; |
112 | char ipbuf[41]; | 112 | char ipbuf[41]; |
113 | struct grecs_cidr cidr; | 113 | struct argot_cidr cidr; |
114 | char *p; | 114 | char *p; |
115 | size_t len; | 115 | size_t len; |
116 | union { | 116 | union { |
@@ -125,7 +125,7 @@ grecs_str_to_cidr(struct grecs_cidr *pcidr, const char *str, | |||
125 | len = strlen(str); | 125 | len = strlen(str); |
126 | 126 | ||
127 | if (len > sizeof(ipbuf)) { | 127 | if (len > sizeof(ipbuf)) { |
128 | grecs_error(locus, 0, _("invalid network mask: %s"), | 128 | argot_error(locus, 0, _("invalid network mask: %s"), |
129 | str); | 129 | str); |
130 | return -1; | 130 | return -1; |
131 | } | 131 | } |
@@ -133,30 +133,30 @@ grecs_str_to_cidr(struct grecs_cidr *pcidr, const char *str, | |||
133 | memcpy(ipbuf, str, len); | 133 | memcpy(ipbuf, str, len); |
134 | ipbuf[len] = 0; | 134 | ipbuf[len] = 0; |
135 | 135 | ||
136 | if (grecs_str_is_ipv4(ipbuf)) | 136 | if (argot_str_is_ipv4(ipbuf)) |
137 | cidr.family = AF_INET; | 137 | cidr.family = AF_INET; |
138 | else if (grecs_str_is_ipv6(ipbuf)) | 138 | else if (argot_str_is_ipv6(ipbuf)) |
139 | cidr.family = AF_INET6; | 139 | cidr.family = AF_INET6; |
140 | else { | 140 | else { |
141 | grecs_error(locus, 0, _("unrecognized address family: %s"), | 141 | argot_error(locus, 0, _("unrecognized address family: %s"), |
142 | str); | 142 | str); |
143 | return -1; | 143 | return -1; |
144 | } | 144 | } |
145 | 145 | ||
146 | rc = inet_pton(cidr.family, ipbuf, &inaddr); | 146 | rc = inet_pton(cidr.family, ipbuf, &inaddr); |
147 | if (rc == -1) { | 147 | if (rc == -1) { |
148 | grecs_error(locus, 0, _("unrecognized address family: %s"), | 148 | argot_error(locus, 0, _("unrecognized address family: %s"), |
149 | str); | 149 | str); |
150 | return -1; | 150 | return -1; |
151 | } else if (rc != 1) { | 151 | } else if (rc != 1) { |
152 | grecs_error(locus, 0, _("invalid network address: %s"), | 152 | argot_error(locus, 0, _("invalid network address: %s"), |
153 | str); | 153 | str); |
154 | return -1; | 154 | return -1; |
155 | } | 155 | } |
156 | 156 | ||
157 | cidr.len = grecs_inaddr_to_bytes(cidr.family, &inaddr, cidr.address); | 157 | cidr.len = argot_inaddr_to_bytes(cidr.family, &inaddr, cidr.address); |
158 | if (cidr.len == 0) { | 158 | if (cidr.len == 0) { |
159 | grecs_error(locus, 0, _("unrecognized address family: %s"), | 159 | argot_error(locus, 0, _("unrecognized address family: %s"), |
160 | str); | 160 | str); |
161 | return -1; | 161 | return -1; |
162 | } | 162 | } |
@@ -170,19 +170,19 @@ grecs_str_to_cidr(struct grecs_cidr *pcidr, const char *str, | |||
170 | masklen = strtoul(p, &end, 10); | 170 | masklen = strtoul(p, &end, 10); |
171 | if (*end == 0) | 171 | if (*end == 0) |
172 | masklen_to_netmask(cidr.netmask, cidr.len, masklen); | 172 | masklen_to_netmask(cidr.netmask, cidr.len, masklen); |
173 | else if ((cidr.family == AF_INET && grecs_str_is_ipv4(p)) | 173 | else if ((cidr.family == AF_INET && argot_str_is_ipv4(p)) |
174 | || (cidr.family == AF_INET6 | 174 | || (cidr.family == AF_INET6 |
175 | && grecs_str_is_ipv6(ipbuf))) { | 175 | && argot_str_is_ipv6(ipbuf))) { |
176 | rc = inet_pton(cidr.family, p, &inaddr); | 176 | rc = inet_pton(cidr.family, p, &inaddr); |
177 | if (rc != 1) { | 177 | if (rc != 1) { |
178 | grecs_error(locus, 0, _("invalid network mask: %s"), | 178 | argot_error(locus, 0, _("invalid network mask: %s"), |
179 | str); | 179 | str); |
180 | return -1; | 180 | return -1; |
181 | } | 181 | } |
182 | grecs_inaddr_to_bytes(cidr.family, &inaddr, | 182 | argot_inaddr_to_bytes(cidr.family, &inaddr, |
183 | cidr.netmask); | 183 | cidr.netmask); |
184 | } else { | 184 | } else { |
185 | grecs_error(locus, 0, _("invalid network mask: %s"), | 185 | argot_error(locus, 0, _("invalid network mask: %s"), |
186 | str); | 186 | str); |
187 | return -1; | 187 | return -1; |
188 | } | 188 | } |
@@ -194,7 +194,7 @@ grecs_str_to_cidr(struct grecs_cidr *pcidr, const char *str, | |||
194 | } | 194 | } |
195 | 195 | ||
196 | int | 196 | int |
197 | grecs_cidr_match(struct grecs_cidr *a, struct grecs_cidr *b) | 197 | argot_cidr_match(struct argot_cidr *a, struct argot_cidr *b) |
198 | { | 198 | { |
199 | int i; | 199 | int i; |
200 | 200 | ||
@@ -208,10 +208,10 @@ grecs_cidr_match(struct grecs_cidr *a, struct grecs_cidr *b) | |||
208 | } | 208 | } |
209 | 209 | ||
210 | int | 210 | int |
211 | grecs_sockadd_cidr_match(struct sockaddr *sa, struct grecs_cidr *cidr) | 211 | argot_sockadd_cidr_match(struct sockaddr *sa, struct argot_cidr *cidr) |
212 | { | 212 | { |
213 | struct grecs_cidr t; | 213 | struct argot_cidr t; |
214 | if (grecs_sockaddr_to_cidr(&t, sa)) | 214 | if (argot_sockaddr_to_cidr(&t, sa)) |
215 | return 1; | 215 | return 1; |
216 | return grecs_cidr_match(cidr, &t); | 216 | return argot_cidr_match(cidr, &t); |
217 | } | 217 | } |