I'm not sure why a "normal" application should care what the topology of the network is. Just so long as the packets it needs all arrive/depart successfully it shgould be all good
. Still, if you're going to do this programatically, one way to approach it would be to understand how enterprise class routers and switches figure out topologies. Basically there are a number of protocols that routers and switches understand. Since you're working in layer 2, it seems, the one you're probably most interested is STP (spanning tree protocol, which pretty much every high end switch will support).
Depending on the network switches, you might be able to query their forwarding tables. You might also be able to do this by querying the switches via SNMP.
Of course, if you have a topology that doesn't have switching gear (e.g. a ring) this will obviously not work. In that case, I would follow rununix's suggestion of trying to figure it out from the routing table. I think it'd be messy though.