summaryrefslogtreecommitdiffabout
path: root/shared-code/FindIFace.h
authorMichael Krelin <hacker@klever.net>2004-07-05 01:53:09 (UTC)
committer Michael Krelin <hacker@klever.net>2004-07-05 01:53:09 (UTC)
commita1487b3fc0313408525cd5b2f3bc4a462df351f7 (patch) (unidiff)
tree010aec6c2abd5ada2f909ac6798c93bf22508bf5 /shared-code/FindIFace.h
downloadklog-a1487b3fc0313408525cd5b2f3bc4a462df351f7.zip
klog-a1487b3fc0313408525cd5b2f3bc4a462df351f7.tar.gz
klog-a1487b3fc0313408525cd5b2f3bc4a462df351f7.tar.bz2
initial commit into svn repository
git-svn-id: http://svn.klever.net/kin/klog/trunk@1 fe716a7a-6dde-0310-88d9-d003556173a8
Diffstat (limited to 'shared-code/FindIFace.h') (more/less context) (ignore whitespace changes)
-rw-r--r--shared-code/FindIFace.h125
1 files changed, 125 insertions, 0 deletions
diff --git a/shared-code/FindIFace.h b/shared-code/FindIFace.h
new file mode 100644
index 0000000..8dec8c4
--- a/dev/null
+++ b/shared-code/FindIFace.h
@@ -0,0 +1,125 @@
1 #ifndef__FINDIFACE_H
2 #define__FINDIFACE_H
3
4#include "SNMPeer.h"
5#include "SNMPExtDLL.h"
6#include "SNMPOIDs.h"
7
8namespace Klever {
9
10inline BOOL FindIFace(in_addr& target,in_addr& source)
11{
12 DEFINE_OID(ipRouteDest, OIDipRouteDest);
13 DEFINE_OID(ipRouteMask, OIDipRouteMask);
14 DEFINE_OID(ipRouteIfIndex,OIDipRouteIfIndex);
15 DEFINE_OID(ipRouteMetric1,OIDipRouteMetric1);
16 DEFINE_OID(ipAdEntIfIndex,OIDipAdEntIfIndex);
17 DEFINE_OID(ipAdEntAddr, OIDipAdEntAddr);
18 struct _route{
19 int iface;
20 int metric;
21 DWORD nm;
22 }routes[16];
23int nRoutes = 0;
24CSNMPVarBindList vbl;
25 vbl.AddTail(CSNMPVarBind(CASNAny(CASNAny::typeASNOID,ipRouteDest,sizeof(ipRouteDest))));
26 vbl.AddTail(CSNMPVarBind(CASNAny(CASNAny::typeASNOID,ipRouteMask,sizeof(ipRouteMask))));
27 vbl.AddTail(CSNMPVarBind(CASNAny(CASNAny::typeASNOID,ipRouteIfIndex,sizeof(ipRouteIfIndex))));
28 vbl.AddTail(CSNMPVarBind(CASNAny(CASNAny::typeASNOID,ipRouteMetric1,sizeof(ipRouteMetric1))));
29CSNMPExtDLL snmp("INETMIB1");
30 while(nRoutes<(sizeof(routes)/sizeof(routes[0]))){
31 if(
32 snmp.Request(CASNAny::typeASNGetNextRequest,vbl,vbl)
33 && vbl.GetCount() == 4
34 ){
35 POSITION p = vbl.GetHeadPosition();
36 _route r = {-1,-1};
37 in_addr d, m;
38 BOOL bD = FALSE, bM = FALSE;
39 while(p){
40 CSNMPVarBind& vb = vbl.GetNext(p);
41 if(
42 vb.IsName(ipRouteDest,sizeof(ipRouteDest))
43 && vb.value.type==CASNAny::typeASNIP
44 ){
45 d.s_addr=vb.value.value.ip.s_addr; bD = TRUE;
46 }else if(
47 vb.IsName(ipRouteMask,sizeof(ipRouteMask))
48 && vb.value.type==CASNAny::typeASNIP
49 ){
50 m.s_addr=vb.value.value.ip.s_addr; bM = TRUE;
51 }else if(
52 vb.IsName(ipRouteIfIndex,sizeof(ipRouteIfIndex))
53 && vb.value.type==CASNAny::typeASNInteger
54 ){
55 r.iface=vb.value.value.number;
56 }else if(
57 vb.IsName(ipRouteMetric1,sizeof(ipRouteMetric1))
58 && vb.value.type==CASNAny::typeASNInteger
59 ){
60 r.metric=vb.value.value.number;
61 }else
62 break;
63 }
64 if(r.iface<0 || r.metric<0 || (!bD) || (!bM))
65 break;
66 if((target.s_addr&m.s_addr)==(d.s_addr&m.s_addr)){
67 r.nm=htonl(m.s_addr);
68 memmove(&routes[nRoutes++],&r,sizeof(routes[0]));
69 }
70 }else
71 break;
72 }
73 if(!nRoutes)
74 return FALSE;
75int rn = 0;
76 if(nRoutes>1){
77 for(int tmp=1;tmp<nRoutes;tmp++)
78 if(
79 routes[tmp].metric<routes[rn].metric
80 || routes[tmp].nm>routes[rn].nm
81 )
82 rn = tmp;
83 }
84int iface = routes[rn].iface;
85 vbl.RemoveAll();
86 vbl.AddTail(CSNMPVarBind(CASNAny(CASNAny::typeASNOID,ipAdEntAddr,sizeof(ipAdEntAddr))));
87 vbl.AddTail(CSNMPVarBind(CASNAny(CASNAny::typeASNOID,ipAdEntIfIndex,sizeof(ipAdEntIfIndex))));
88 for(;;){
89 if(
90 snmp.Request(CASNAny::typeASNGetNextRequest,vbl,vbl)
91 && vbl.GetCount()==2
92 ){
93 in_addr a; a.s_addr = INADDR_NONE;
94 int ifn = -1;
95 POSITION p = vbl.GetHeadPosition();
96 while(p){
97 CSNMPVarBind& vb = vbl.GetNext(p);
98 if(
99 vb.IsName(ipAdEntAddr,sizeof(ipAdEntAddr))
100 && vb.value.type==CASNAny::typeASNIP
101 ){
102 a.s_addr=vb.value.value.ip.s_addr;
103 }else if(
104 vb.IsName(ipAdEntIfIndex,sizeof(ipAdEntIfIndex))
105 && vb.value.type==CASNAny::typeASNInteger
106 ){
107 ifn = vb.value.value.number;
108 }else
109 break;
110 }
111 if(ifn<0)
112 break;
113 if(ifn==iface){
114 source.s_addr=a.s_addr;
115 return TRUE;
116 }
117 }else
118 break;
119 }
120 return FALSE;
121}
122
123};
124
125 #endif// __FINDIFACE_H