Finding longest match for each ip address