sbin/atm/atm/atm_subr.c
int
compress_prefix_list(struct in_addr *ipp, int ilen)
{
	int		i, j, n;
	struct in_addr	*ip1, *ip2, *m1, *m2;

	/*
	 * Figure out how many pairs there are
	 */
	n = ilen / (sizeof(struct in_addr) * 2);

	/*
	 * Check each pair of address/mask pairs to make sure
	 * none contains the other
	 */
	for (i = 0; i < n; i++) {
		ip1 = &ipp[i*2];
		m1 = &ipp[i*2+1];

		/*
		 * If we've already eliminated this address,
		 * skip the checks
		 */
		if (ip1->s_addr == 0)
			continue;

		/*
		 * Try all possible second members of the pair
		 */
		for (j = i + 1; j < n; j++) {
			ip2 = &ipp[j*2];
			m2 = &ipp[j*2+1];

			/*
			 * If we've already eliminated the second
			 * address, just skip the checks
			 */
			if (ip2->s_addr == 0)
				continue;

			/*
			 * Compare the address/mask pairs
			 */
			if (m1->s_addr == m2->s_addr) {
				/*
				 * Masks are equal
				 */
				if (ip1->s_addr == ip2->s_addr) {
					ip2->s_addr = 0;
					m2->s_addr = 0;
				}
			} else if (ntohl(m1->s_addr) <
					ntohl(m2->s_addr)) {
				/*
				 * m1 is shorter
				 */
				if ((ip2->s_addr & m1->s_addr) ==
						ip1->s_addr) {
					ip2->s_addr = 0;
					m2->s_addr = 0;
				}
			} else {
				/*
				 * m1 is longer
				 */
				if ((ip1->s_addr & m2->s_addr) ==
						ip2->s_addr) {
					ip1->s_addr = 0;
					m1->s_addr = 0;
					break;
				}
			}
		}
	}

	/*
	 * Now pull up the list, eliminating zeroed entries
	 */
	for (i = 0, j = 0; i < n; i++) {
		ip1 = &ipp[i*2];
		m1 = &ipp[i*2+1];
		ip2 = &ipp[j*2];
		m2 = &ipp[j*2+1];
		if (ip1->s_addr != 0) {
			if (i != j) {
				*ip2 = *ip1;
				*m2 = *m1;
			}
			j++;
		}
	}

	return(j * sizeof(struct in_addr) * 2);
}
/*
 * Compress a list of IP network prefixes
 *
 * Arguments:
 *	ipp	pointer to list of IP address/mask pairs
 *	ipc	length of list
 *
 * Returns:
 *	length of compressed list
 *
 */
Compress a list of IP network prefixes
Arguments:
ipp     pointer to list of IP address/mask pairs
ipc     length of list

length of compressed list
Compress a list of IP network prefixes Arguments : ipp pointer to list of IP address / mask pairs ipc length of list length of compressed list
int
compress_prefix_list(struct in_addr *ipp, int ilen)
{
	int		i, j, n;
	struct in_addr	*ip1, *ip2, *m1, *m2;
	n = ilen / (sizeof(struct in_addr) * 2);
	for (i = 0; i < n; i++) {
		ip1 = &ipp[i*2];
		m1 = &ipp[i*2+1];
		if (ip1->s_addr == 0)
			continue;
		for (j = i + 1; j < n; j++) {
			ip2 = &ipp[j*2];
			m2 = &ipp[j*2+1];
			if (ip2->s_addr == 0)
				continue;
			if (m1->s_addr == m2->s_addr) {
				if (ip1->s_addr == ip2->s_addr) {
					ip2->s_addr = 0;
					m2->s_addr = 0;
				}
			} else if (ntohl(m1->s_addr) <
					ntohl(m2->s_addr)) {
				if ((ip2->s_addr & m1->s_addr) ==
						ip1->s_addr) {
					ip2->s_addr = 0;
					m2->s_addr = 0;
				}
			} else {
				if ((ip1->s_addr & m2->s_addr) ==
						ip2->s_addr) {
					ip1->s_addr = 0;
					m1->s_addr = 0;
					break;
				}
			}
		}
	}
	for (i = 0, j = 0; i < n; i++) {
		ip1 = &ipp[i*2];
		m1 = &ipp[i*2+1];
		ip2 = &ipp[j*2];
		m2 = &ipp[j*2+1];
		if (ip1->s_addr != 0) {
			if (i != j) {
				*ip2 = *ip1;
				*m2 = *m1;
			}
			j++;
		}
	}
	return(j * sizeof(struct in_addr) * 2);
}
int compress_prefix_list ( struct in_addr * ipp , int ilen ) { int i , j , n ; struct in_addr * ip1 , * ip2 , * m1 , * m2 ; n = ilen / ( sizeof ( struct in_addr ) * 2 ) ; for ( i = 0 ; i < n ; i ++ ) { ip1 = & ipp [ i * 2 ] ; m1 = & ipp [ i * 2 + 1 ] ; if ( ip1 -> s_addr == 0 ) continue ; for ( j = i + 1 ; j < n ; j ++ ) { ip2 = & ipp [ j * 2 ] ; m2 = & ipp [ j * 2 + 1 ] ; if ( ip2 -> s_addr == 0 ) continue ; if ( m1 -> s_addr == m2 -> s_addr ) { if ( ip1 -> s_addr == ip2 -> s_addr ) { ip2 -> s_addr = 0 ; m2 -> s_addr = 0 ; } } else if ( ntohl ( m1 -> s_addr ) < ntohl ( m2 -> s_addr ) ) { if ( ( ip2 -> s_addr & m1 -> s_addr ) == ip1 -> s_addr ) { ip2 -> s_addr = 0 ; m2 -> s_addr = 0 ; } } else { if ( ( ip1 -> s_addr & m2 -> s_addr ) == ip2 -> s_addr ) { ip1 -> s_addr = 0 ; m1 -> s_addr = 0 ; break ; } } } } for ( i = 0 , j = 0 ; i < n ; i ++ ) { ip1 = & ipp [ i * 2 ] ; m1 = & ipp [ i * 2 + 1 ] ; ip2 = & ipp [ j * 2 ] ; m2 = & ipp [ j * 2 + 1 ] ; if ( ip1 -> s_addr != 0 ) { if ( i != j ) { * ip2 = * ip1 ; * m2 = * m1 ; } j ++ ; } } return ( j * sizeof ( struct in_addr ) * 2 ) ; }
Compress a list of IP network prefixes
 Arguments:
      ipp     pointer to list of IP address/mask pairs
      ipc     length of list
Compress a list of IP network prefixes Arguments : ipp pointer to list of IP address / mask pairs ipc length of list
/*
	 * Figure out how many pairs there are
	 */ /*
	 * Check each pair of address/mask pairs to make sure
	 * none contains the other
	 */ /*
		 * If we've already eliminated this address,
		 * skip the checks
		 */ /*
		 * Try all possible second members of the pair
		 */ /*
			 * If we've already eliminated the second
			 * address, just skip the checks
			 */ /*
			 * Compare the address/mask pairs
			 */ /*
				 * Masks are equal
				 */ /*
				 * m1 is shorter
				 */ /*
				 * m1 is longer
				 */ /*
	 * Now pull up the list, eliminating zeroed entries
	 */