+/**
+ * \brief Locates what interface should be used to get directly to an address
+ */
+tRoute *_Route_FindInterfaceRoute(int AddressType, void *Address)
+{
+ tRoute *best = NULL, *rt;
+ int addrSize = IPStack_GetAddressSize(AddressType);
+ for( tInterface *iface = gIP_Interfaces; iface; iface = iface->Next )
+ {
+ if( iface->Type != AddressType ) continue;
+
+ // Check if the address matches
+ if( !IPStack_CompareAddress(AddressType, iface->Address, Address, iface->SubnetBits) )
+ continue;
+
+ rt = &iface->Route;
+ if( !rt->Interface )
+ {
+ memcpy(rt->Network, iface->Address, addrSize);
+ memset(rt->NextHop, 0, addrSize);
+ rt->Metric = DEFAUTL_METRIC;
+ rt->SubnetBits = iface->SubnetBits;
+ rt->Interface = iface;
+ }
+
+ if( best ) {
+ // More direct routes are preferred
+ if( best->SubnetBits > rt->SubnetBits ) {
+ LOG("Skipped - less direct (%i < %i)", rt->SubnetBits, best->SubnetBits);
+ continue;
+ }
+ // If equally direct, choose the best metric
+ if( best->SubnetBits == rt->SubnetBits && best->Metric < rt->Metric ) {
+ LOG("Skipped - higher metric (%i > %i)", rt->Metric, best->Metric);
+ continue;
+ }
+ }
+
+ best = rt;
+ }
+
+ return best;
+}
+