policy_summary_reject(smartlist_t *summary,
                      maskbits_t maskbits,
                      uint16_t prt_min, uint16_t prt_max,
                      sa_family_t family)
{
  tor_assert_nonfatal_once(family == AF_INET || family == AF_INET6);

  int i = policy_summary_split(summary, prt_min, prt_max);

  /* The length of a single address mask */
  int addrbits = (family == AF_INET) ? IPV4_BITS : IPV6_BITS;
  tor_assert_nonfatal_once(addrbits >= maskbits);

  /* We divide IPv6 address counts by (1 << scale) to keep them in a uint64_t
   */
  int scale = ((family == AF_INET) ?
               REJECT_CUTOFF_SCALE_IPV4 :
               REJECT_CUTOFF_SCALE_IPV6);

  tor_assert_nonfatal_once(addrbits >= scale);
  if (maskbits > (addrbits - scale)) {
    tor_assert_nonfatal_once(family == AF_INET6);
    /* The address range is so small, we'd need billions of them to reach the
     * rejection limit. So we ignore this range in the reject count. */
    return;
  }

  uint64_t count = 0;
  if (addrbits - scale - maskbits >= 64) {
    tor_assert_nonfatal_once(family == AF_INET6);
    /* The address range is so large, it's an automatic rejection for all ports
     * in the range. */
    count = UINT64_MAX;
  } else {
    count = (U64_LITERAL(1) << (addrbits - scale - maskbits));
  }
  tor_assert_nonfatal_once(count > 0);
  while (i < smartlist_len(summary) &&
         AT(i)->prt_max <= prt_max) {
    if (AT(i)->reject_count <= UINT64_MAX - count) {
      AT(i)->reject_count += count;
    } else {
      /* IPv4 would require a 4-billion address redundant policy to get here,
       * but IPv6 just needs to have ::/0 */
      if (family == AF_INET) {
        tor_assert_nonfatal_unreached_once();
      }
      /* If we do get here, use saturating arithmetic */
      AT(i)->reject_count = UINT64_MAX;
    }
    i++;
  }
  tor_assert(i < smartlist_len(summary) || prt_max==65535);
}
policy_summary_reject(smartlist_t *summary,
                      maskbits_t maskbits,
                      uint16_t prt_min, uint16_t prt_max,
                      sa_family_t family)
{
  tor_assert_nonfatal_once(family == AF_INET || family == AF_INET6);

  int i = policy_summary_split(summary, prt_min, prt_max);

   
  int addrbits = (family == AF_INET) ? IPV4_BITS : IPV6_BITS;
  tor_assert_nonfatal_once(addrbits >= maskbits);

   
  int scale = ((family == AF_INET) ?
               REJECT_CUTOFF_SCALE_IPV4 :
               REJECT_CUTOFF_SCALE_IPV6);

  tor_assert_nonfatal_once(addrbits >= scale);
  if (maskbits > (addrbits - scale)) {
    tor_assert_nonfatal_once(family == AF_INET6);
     
    return;
  }

  uint64_t count = 0;
  if (addrbits - scale - maskbits >= 64) {
    tor_assert_nonfatal_once(family == AF_INET6);
     
    count = UINT64_MAX;
  } else {
    count = (U64_LITERAL(1) << (addrbits - scale - maskbits));
  }
  tor_assert_nonfatal_once(count > 0);
  while (i < smartlist_len(summary) &&
         AT(i)->prt_max <= prt_max) {
    if (AT(i)->reject_count <= UINT64_MAX - count) {
      AT(i)->reject_count += count;
    } else {
       
      if (family == AF_INET) {
        tor_assert_nonfatal_unreached_once();
      }
       
      AT(i)->reject_count = UINT64_MAX;
    }
    i++;
  }
  tor_assert(i < smartlist_len(summary) || prt_max==65535);
}