BEGIN {
	addr_num=0
}

function get_mask(prefix,    suffix) {
	if (prefix <= 0)
		return 0
	if (prefix >= 32)
		return 0xffffffff

	suffix = 32 - prefix
	return lshift(rshift(0xffffffff, suffix), suffix)
}

function decode_mask(mask,    m1, m2, m3, m4) {
	m1 = rshift(and(mask, 0xff000000), 24)
	m2 = rshift(and(mask, 0x00ff0000), 16)
	m3 = rshift(and(mask, 0x0000ff00),  8)
	m4 =        and(mask, 0x000000ff)
	return sprintf("%d.%d.%d.%d", m1, m2, m3, m4)
}

/inet/ {
	getline ipaddr

	split(ipaddr, a, "/")
	printf "IPADDR%d=%s\n", ++addr_num, a[1]
	if ( length(force_mask) > 0 ) {
		printf "NETMASK%d=%s\n", addr_num, decode_mask(get_mask(force_mask))
	} else if ( a[2] )
		printf "NETMASK%d=%s\n", addr_num, decode_mask(get_mask(a[2]))

}

/peer/ {
	getline peer
	split(peer, p, "/")
	mask = get_mask(p[2])
	printf "PEER_IPADDR%d=%s\n", addr_num, p[1]
}
