Ticket #17608: 17608_refactor_policies_parse_exit_policy_internal.patch

File 17608_refactor_policies_parse_exit_policy_internal.patch, 5.2 KB (added by juce, 4 years ago)

Hi, I'm new to tor development and want to start contributing as much as I can, so any feedback would be great!

  • src/or/policies.c

    diff --git a/src/or/policies.c b/src/or/policies.c
    index 4d9b17c..5841353 100644
    a b policies_parse_exit_policy_reject_private( 
    11241124  "reject *:563,reject *:1214,reject *:4661-4666,"                  \
    11251125  "reject *:6346-6429,reject *:6699,reject *:6881-6999,accept *:*"
    11261126
    1127 /** Parse the exit policy <b>cfg</b> into the linked list *<b>dest</b>.
    1128  *
    1129  * If <b>ipv6_exit</b> is false, prepend "reject *6:*" to the policy.
    1130  *
    1131  * If <b>rejectprivate</b> is true:
    1132  *   - prepend "reject private:*" to the policy.
    1133  *   - prepend entries that reject publicly routable addresses on this exit
    1134  *     relay by calling policies_parse_exit_policy_reject_private
    1135  *
    1136  * If cfg doesn't end in an absolute accept or reject and if
    1137  * <b>add_default_policy</b> is true, add the default exit
    1138  * policy afterwards.
    1139  *
    1140  * Return -1 if we can't parse cfg, else return 0.
    1141  *
    1142  * This function is used to parse the exit policy from our torrc. For
    1143  * the functions used to parse the exit policy from a router descriptor,
    1144  * see router_add_exit_policy.
     1127/**
     1128 * Iterates through *<b>dest</b> and logs a warning with first
     1129 * redundant entry if found
    11451130 */
    1146 static int
    1147 policies_parse_exit_policy_internal(config_line_t *cfg,
    1148                                     smartlist_t **dest,
    1149                                     int ipv6_exit,
    1150                                     int rejectprivate,
    1151                                     const smartlist_t *configured_addresses,
    1152                                     int reject_interface_addresses,
    1153                                     int reject_configured_port_addresses,
    1154                                     int add_default_policy)
    1155 {
    1156   if (!ipv6_exit) {
    1157     append_exit_policy_string(dest, "reject *6:*");
    1158   }
    1159   if (rejectprivate) {
    1160     /* Reject IPv4 and IPv6 reserved private netblocks */
    1161     append_exit_policy_string(dest, "reject private:*");
    1162     /* Reject IPv4 and IPv6 publicly routable addresses on this exit relay */
    1163     policies_parse_exit_policy_reject_private(
    1164                                             dest, ipv6_exit,
    1165                                             configured_addresses,
    1166                                             reject_interface_addresses,
    1167                                             reject_configured_port_addresses);
    1168   }
    1169   if (parse_addr_policy(cfg, dest, -1))
    1170     return -1;
    1171 
    1172   /* Before we add the default policy and final rejects, check to see if
    1173    * there are any lines after accept *:* or reject *:*. These lines have no
    1174    * effect, and are most likely an error. */
     1131static void
     1132policies_log_first_redundant_entry(smartlist_t** dest) {
    11751133  int found_final_effective_entry = 0;
    11761134  int first_redundant_entry = 0;
    11771135  for (int i = 0; i < smartlist_len(*dest); ++i) {
    policies_parse_exit_policy_internal(config_line_t *cfg, 
    12271185             "accept/reject *:* as the last entry in any exit policy.)",
    12281186             line);
    12291187  }
     1188}
     1189
     1190/** Parse the exit policy <b>cfg</b> into the linked list *<b>dest</b>.
     1191 *
     1192 * If <b>ipv6_exit</b> is false, prepend "reject *6:*" to the policy.
     1193 *
     1194 * If <b>rejectprivate</b> is true:
     1195 *   - prepend "reject private:*" to the policy.
     1196 *   - prepend entries that reject publicly routable addresses on this exit
     1197 *     relay by calling policies_parse_exit_policy_reject_private
     1198 *
     1199 * If cfg doesn't end in an absolute accept or reject and if
     1200 * <b>add_default_policy</b> is true, add the default exit
     1201 * policy afterwards.
     1202 *
     1203 * Return -1 if we can't parse cfg, else return 0.
     1204 *
     1205 * This function is used to parse the exit policy from our torrc. For
     1206 * the functions used to parse the exit policy from a router descriptor,
     1207 * see router_add_exit_policy.
     1208 */
     1209static int
     1210policies_parse_exit_policy_internal(config_line_t *cfg,
     1211                                    smartlist_t **dest,
     1212                                    int ipv6_exit,
     1213                                    int rejectprivate,
     1214                                    const smartlist_t *configured_addresses,
     1215                                    int reject_interface_addresses,
     1216                                    int reject_configured_port_addresses,
     1217                                    int add_default_policy)
     1218{
     1219  if (!ipv6_exit) {
     1220    append_exit_policy_string(dest, "reject *6:*");
     1221  }
     1222  if (rejectprivate) {
     1223    /* Reject IPv4 and IPv6 reserved private netblocks */
     1224    append_exit_policy_string(dest, "reject private:*");
     1225    /* Reject IPv4 and IPv6 publicly routable addresses on this exit relay */
     1226    policies_parse_exit_policy_reject_private(
     1227                                            dest, ipv6_exit,
     1228                                            configured_addresses,
     1229                                            reject_interface_addresses,
     1230                                            reject_configured_port_addresses);
     1231  }
     1232  if (parse_addr_policy(cfg, dest, -1))
     1233    return -1;
     1234
     1235  /* Before we add the default policy and final rejects, check to see if
     1236   * there are any lines after accept *:* or reject *:*. These lines have no
     1237   * effect, and are most likely an error. */
     1238   policies_log_first_redundant_entry(dest);
    12301239
    12311240  if (add_default_policy) {
    12321241    append_exit_policy_string(dest, DEFAULT_EXIT_POLICY);