- switch(version) {
- case V4:
- /* find ifc address closest to the gateway to use */
- for(lifc = ifc->lifc; lifc; lifc = lifc->next){
- maskip(gate, lifc->mask, gnet);
- if(ipcmp(gnet, lifc->net) == 0){
- ipmove(local, lifc->local);
- goto out;
- }
- }
- break;
- case V6:
- /* find ifc address with scope matching the destination */
- atyper = v6addrtype(remote);
- deprecated = 0;
- for(lifc = ifc->lifc; lifc; lifc = lifc->next){
- atypel = v6addrtype(lifc->local);
- /* prefer appropriate scope */
- if(atypel > atype && atype < atyper ||
- atypel < atype && atype > atyper){
- ipmove(local, lifc->local);
- deprecated = !v6addrcurr(lifc);
- atype = atypel;
- } else if(atypel == atype){
- /* avoid deprecated addresses */
- if(deprecated && v6addrcurr(lifc)){
- ipmove(local, lifc->local);
- atype = atypel;
- deprecated = 0;
- }
- }
- if(atype == atyper && !deprecated)
- goto out;
- }
- if(atype >= atyper)
- goto out;
- break;
- default:
- panic("findlocalip: version %d", version);
+ b = -1;
+ for(lifc = ifc->lifc; lifc != nil; lifc = lifc->next){
+ if((lifc->type & Rv4) == 0 || ipcmp(lifc->local, IPnoaddr) == 0)
+ continue;
+ a = comprefixlen(lifc->local+IPv4off, remote, IPv4addrlen);
+ if(a > b){
+ b = a;
+ memmove(local, lifc->local+IPv4off, IPv4addrlen);