summaryrefslogtreecommitdiff
path: root/src/or/policies.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2006-12-29 02:49:12 +0000
committerNick Mathewson <nickm@torproject.org>2006-12-29 02:49:12 +0000
commit7cfdac1bf6486671517b736b8188c5b3d0caa7d5 (patch)
treec0ea7f99f7615d667300c614865b88bdf71de598 /src/or/policies.c
parentb59573949efd7427463d96c83ce059f79ab4f413 (diff)
downloadtor-7cfdac1bf6486671517b736b8188c5b3d0caa7d5.tar.gz
tor-7cfdac1bf6486671517b736b8188c5b3d0caa7d5.zip
r11737@Kushana: nickm | 2006-12-28 18:32:13 -0500
Remove some dead code; refactor some duplicated code. svn:r9206
Diffstat (limited to 'src/or/policies.c')
-rw-r--r--src/or/policies.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/or/policies.c b/src/or/policies.c
index 29e3dd002c..dca16046ab 100644
--- a/src/or/policies.c
+++ b/src/or/policies.c
@@ -658,6 +658,69 @@ policy_is_reject_star(addr_policy_t *p)
return 1;
}
+/** Write a single address policy to the buf_len byte buffer at buf. Return
+ * the number of characters written, or -1 on failure. */
+int
+policy_write_item(char *buf, size_t buflen, addr_policy_t *policy)
+{
+ struct in_addr in;
+ size_t written = 0;
+ char addrbuf[INET_NTOA_BUF_LEN];
+ int result;
+
+ in.s_addr = htonl(policy->addr);
+ tor_inet_ntoa(&in, addrbuf, sizeof(addrbuf));
+ /* write accept/reject 1.2.3.4 */
+ result = tor_snprintf(buf, buflen, "%s %s",
+ policy->policy_type == ADDR_POLICY_ACCEPT ? "accept" : "reject",
+ policy->msk == 0 ? "*" : addrbuf);
+ if (result < 0)
+ return -1;
+ written += strlen(buf);
+ /* If the mask is 0xffffffff, we don't need to give it. If the mask is 0,
+ * we already wrote "*". */
+ if (policy->msk != 0xFFFFFFFFu && policy->msk != 0) {
+ int n_bits = addr_mask_get_bits(policy->msk);
+ if (n_bits >= 0) {
+ if (tor_snprintf(buf+written, buflen-written, "/%d", n_bits)<0)
+ return -1;
+ } else {
+ /* Write "/255.255.0.0" */
+ in.s_addr = htonl(policy->msk);
+ tor_inet_ntoa(&in, addrbuf, sizeof(addrbuf));
+ if (tor_snprintf(buf+written, buflen-written, "/%s", addrbuf)<0)
+ return -1;
+ }
+ written += strlen(buf+written);
+ }
+ if (policy->prt_min <= 1 && policy->prt_max == 65535) {
+ /* There is no port set; write ":*" */
+ if (written+4 > buflen)
+ return -1;
+ strlcat(buf+written, ":*", buflen-written);
+ written += 3;
+ } else if (policy->prt_min == policy->prt_max) {
+ /* There is only one port; write ":80". */
+ result = tor_snprintf(buf+written, buflen-written, ":%d", policy->prt_min);
+ if (result<0)
+ return -1;
+ written += result;
+ } else {
+ /* There is a range of ports; write ":79-80". */
+ result = tor_snprintf(buf+written, buflen-written, ":%d-%d",
+ policy->prt_min, policy->prt_max);
+ if (result<0)
+ return -1;
+ written += result;
+ }
+ if (written < buflen)
+ buf[written] = '\0';
+ else
+ return -1;
+
+ return (int)written;
+}
+
int
getinfo_helper_policies(control_connection_t *conn,
const char *question, char **answer)