sbin/atm/atm/atm_subr.c
int
parse_ip_prefix(char *cp, struct in_addr *op)
{
	int		len;
	char		*mp;
	struct in_addr	ip_addr;

	static u_long	masks[33] = {
		0x0,
		0x80000000,
		0xc0000000,
		0xe0000000,
		0xf0000000,
		0xf8000000,
		0xfc000000,
		0xfe000000,
		0xff000000,
		0xff800000,
		0xffc00000,
		0xffe00000,
		0xfff00000,
		0xfff80000,
		0xfffc0000,
		0xfffe0000,
		0xffff0000,
		0xffff8000,
		0xffffc000,
		0xffffe000,
		0xfffff000,
		0xfffff800,
		0xfffffc00,
		0xfffffe00,
		0xffffff00,
		0xffffff80,
		0xffffffc0,
		0xffffffe0,
		0xfffffff0,
		0xfffffff8,
		0xfffffffc,
		0xfffffffe,
		0xffffffff
	};

	/*
	 * Find the slash that marks the start of the mask
	 */
	mp = strchr(cp, '/');
	if (mp) {
		*mp = '\0';
		mp++;
	}

	/*
	 * Convert the IP-address part of the prefix
	 */
	ip_addr.s_addr = inet_addr(cp);
	if (ip_addr.s_addr == INADDR_NONE)
		return(-1);

	/*
	 * Set the default mask length
	 */
	if (IN_CLASSA(ntohl(ip_addr.s_addr)))
		len = 8;
	else if (IN_CLASSB(ntohl(ip_addr.s_addr)))
		len = 16;
	else if (IN_CLASSC(ntohl(ip_addr.s_addr)))
		len = 24;
	else
		return(-1);

	/*
	 * Get the mask length
	 */
	if (mp) {
		len = atoi(mp);
		if (len < 1 || len > 32)
			return(-1);
	}

	/*
	 * Select the mask and copy the IP address into the
	 * result buffer, ANDing it with the mask
	 */
	op[1].s_addr = htonl(masks[len]);
	op[0].s_addr = ip_addr.s_addr & op[1].s_addr;

	return(0);
}
/*
 * Parse an IP prefix designation in the form nnn.nnn.nnn.nnn/mm
 *
 * Arguments:
 *	cp	pointer to prefix designation string
 *	op	pointer to a pair of in_addrs for the result
 *
 * Returns:
 *	0	success
 *	-1	prefix was invalid
 *
 */
Parse an IP prefix designation in the form nnn.nnn.nnn.nnn/mm
Arguments:
cp      pointer to prefix designation string
op      pointer to a pair of in_addrs for the result

0       success
-1      prefix was invalid
Parse an IP prefix designation in the form nnn . nnn . nnn . nnn / mm Arguments : cp pointer to prefix designation string op pointer to a pair of in_addrs for the result 0 success - 1 prefix was invalid
int
parse_ip_prefix(char *cp, struct in_addr *op)
{
	int		len;
	char		*mp;
	struct in_addr	ip_addr;
	static u_long	masks[33] = {
		0x0,
		0x80000000,
		0xc0000000,
		0xe0000000,
		0xf0000000,
		0xf8000000,
		0xfc000000,
		0xfe000000,
		0xff000000,
		0xff800000,
		0xffc00000,
		0xffe00000,
		0xfff00000,
		0xfff80000,
		0xfffc0000,
		0xfffe0000,
		0xffff0000,
		0xffff8000,
		0xffffc000,
		0xffffe000,
		0xfffff000,
		0xfffff800,
		0xfffffc00,
		0xfffffe00,
		0xffffff00,
		0xffffff80,
		0xffffffc0,
		0xffffffe0,
		0xfffffff0,
		0xfffffff8,
		0xfffffffc,
		0xfffffffe,
		0xffffffff
	};
	mp = strchr(cp, '/');
	if (mp) {
		*mp = '\0';
		mp++;
	}
	ip_addr.s_addr = inet_addr(cp);
	if (ip_addr.s_addr == INADDR_NONE)
		return(-1);
	if (IN_CLASSA(ntohl(ip_addr.s_addr)))
		len = 8;
	else if (IN_CLASSB(ntohl(ip_addr.s_addr)))
		len = 16;
	else if (IN_CLASSC(ntohl(ip_addr.s_addr)))
		len = 24;
	else
		return(-1);
	if (mp) {
		len = atoi(mp);
		if (len < 1 || len > 32)
			return(-1);
	}
	op[1].s_addr = htonl(masks[len]);
	op[0].s_addr = ip_addr.s_addr & op[1].s_addr;
	return(0);
}
int parse_ip_prefix ( char * cp , struct in_addr * op ) { int len ; char * mp ; struct in_addr ip_addr ; static u_long masks [ 33 ] = { 0x0 , 0x80000000 , 0xc0000000 , 0xe0000000 , 0xf0000000 , 0xf8000000 , 0xfc000000 , 0xfe000000 , 0xff000000 , 0xff800000 , 0xffc00000 , 0xffe00000 , 0xfff00000 , 0xfff80000 , 0xfffc0000 , 0xfffe0000 , 0xffff0000 , 0xffff8000 , 0xffffc000 , 0xffffe000 , 0xfffff000 , 0xfffff800 , 0xfffffc00 , 0xfffffe00 , 0xffffff00 , 0xffffff80 , 0xffffffc0 , 0xffffffe0 , 0xfffffff0 , 0xfffffff8 , 0xfffffffc , 0xfffffffe , 0xffffffff } ; mp = strchr ( cp , ' ' ) ; if ( mp ) { * mp = ' \0 ' ; mp ++ ; } ip_addr . s_addr = inet_addr ( cp ) ; if ( ip_addr . s_addr == INADDR_NONE ) return ( -1 ) ; if ( IN_CLASSA ( ntohl ( ip_addr . s_addr ) ) ) len = 8 ; else if ( IN_CLASSB ( ntohl ( ip_addr . s_addr ) ) ) len = 16 ; else if ( IN_CLASSC ( ntohl ( ip_addr . s_addr ) ) ) len = 24 ; else return ( -1 ) ; if ( mp ) { len = atoi ( mp ) ; if ( len < 1 || len > 32 ) return ( -1 ) ; } op [ 1 ] . s_addr = htonl ( masks [ len ] ) ; op [ 0 ] . s_addr = ip_addr . s_addr & op [ 1 ] . s_addr ; return ( 0 ) ; }
Parse an IP prefix designation in the form nnn.nnn.nnn.nnn/mm
 Arguments:
      cp      pointer to prefix designation string
      op      pointer to a pair of in_addrs for the result
Parse an IP prefix designation in the form nnn . nnn . nnn . nnn / mm Arguments : cp pointer to prefix designation string op pointer to a pair of in_addrs for the result
/*
	 * Find the slash that marks the start of the mask
	 */ /*
	 * Convert the IP-address part of the prefix
	 */ /*
	 * Set the default mask length
	 */ /*
	 * Get the mask length
	 */ /*
	 * Select the mask and copy the IP address into the
	 * result buffer, ANDing it with the mask
	 */