Debian Codegraph @

protolib/src/common/protoNet.cpp

raw
1 #include "protoNet.h"
2 #include "protoDebug.h"
3
4 unsigned int ProtoNet::GetInterfaceCount()
5 {
6 return GetInterfaceIndices(NULL, 0);
7 } // end ProtoNet::GetInterfaceCount()
8
9 unsigned int ProtoNet::GetInterfaceIndex(const ProtoAddress& ifAddr)
10 {
11
12 char ifName[256];
13 ifName[255] = '\0';
14 if (GetInterfaceName(ifAddr, ifName, 255))
15 {
16 return GetInterfaceIndex(ifName);
17 }
18 else
19 {
20 return 0;
21 }
22 } // end ProtoNet::GetInterfaceIndex(by address)
23
24 bool ProtoNet::GetInterfaceAddressList(unsigned int ifIndex,
25 ProtoAddress::Type addrType,
26 ProtoAddressList& addrList)
27 {
28 char ifName[256];
29 ifName[255] = '\0';
30 if (GetInterfaceName(ifIndex, ifName, 255))
31 {
32 return GetInterfaceAddressList(ifName, addrType, addrList);
33 }
34 else
35 {
36 PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() error: invalid interface index?!\n");
37 return false;
38 }
39 } // end GetInterfaceAddressList(by index)
40
41 // given addrType, returns addrList with addresses found added
42 // TBD - we may want to move this implementation to "unixNet.cpp" and create a separate
43 // Win32-specific implementation using the Windows "GetAdaptersAddresses()" call which
44 // does.
45
46 bool ProtoNet::GetHostAddressList(ProtoAddress::Type addrType,
47 ProtoAddressList& addrList)
48 {
49 // First determine how many interfaces there are
50 unsigned int ifCount = GetInterfaceCount();
51 if (0 == ifCount)
52 {
53 PLOG(PL_WARN, "ProtoNet::GetHostAddressList() warning: no interfaces?!\n");
54 return true;
55 }
56 // Then allocate a buffer of the appropriate size
57 unsigned int* ifIndices = new unsigned int[ifCount];
58 if (NULL == ifIndices)
59 {
60 PLOG(PL_ERROR, "ProtoNet::GetHostAddressList() new ifIndices[] error: %s\n", GetErrorString());
61 return false;
62 }
63 // Now call with a buffer to get this list of indices
64 ifCount = GetInterfaceIndices(ifIndices, ifCount);
65 for (unsigned int i = 0; i < ifCount; i++)
66 {
67 if (!GetInterfaceAddressList(ifIndices[i], addrType, addrList))
68 {
69 PLOG(PL_DEBUG, "ProtoNet::GetHostAddressList() error: unable to get addresses for iface index %d\n", ifIndices[i]);
70 }
71 }
72 delete[] ifIndices;
73 return true; // all interfaces found & list returned in addrList
74 } // end ProtoNet::GetHostAddressList()
75
76 // given "addrType", searches through interface list, returns first non-loopback address found
77 // TBD - should we _try_ to find a non-link-local addr as well?
78 #ifndef WIN32
79 // FindLocalAddress defined in win32Net.cpp
80 bool ProtoNet::FindLocalAddress(ProtoAddress::Type addrType, ProtoAddress& theAddress)
81 {
82 ProtoAddressList addrList;
83 if (GetHostAddressList(addrType, addrList))
84 {
85 ProtoAddressList::Iterator iterator(addrList);
86 while (iterator.GetNextAddress(theAddress))
87 {
88 if (!theAddress.IsLoopback())
89 return true;
90 }
91 }
92 return false;
93 } // end ProtoNet::FindLocalAddress()
94 #endif
95 bool ProtoNet::GetInterfaceAddress(const char* ifName,
96 ProtoAddress::Type addrType,
97 ProtoAddress& theAddress,
98 unsigned int* ifIndex)
99 {
100 ProtoAddressList addrList;
101 GetInterfaceAddressList(ifName, addrType, addrList, ifIndex);
102 return addrList.GetFirstAddress(theAddress);
103 } // end ProtoNet::GetInterfaceAddress()
104
105 bool ProtoNet::GetInterfaceAddress(unsigned int ifIndex,
106 ProtoAddress::Type addrType,
107 ProtoAddress& theAddress)
108 {
109 ProtoAddressList addrList;
110 GetInterfaceAddressList(ifIndex, addrType, addrList);
111 return addrList.GetFirstAddress(theAddress);
112 } // end ProtoNet::GetInterfaceAddress()
113
114 #ifndef WIN32
115 unsigned int ProtoNet::GetInterfaceAddressMask(unsigned int ifIndex, const ProtoAddress& ifAddr)
116 {
117 #ifndef WIN32
118 char ifName[256];
119 ifName[255] = '\0';
120 if (GetInterfaceName(ifIndex, ifName, 255))
121 {
122 return GetInterfaceAddressMask(ifName, ifAddr);
123 }
124 else
125 {
126 PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressMask() error: invalid interface index?!\n");
127 return 0;
128 }
129 #else
130 PLOG(PL_ERROR,"ProtoNet::GetInterfaceAddressMask() error: function not implemented for WIN32\n");
131 return 0;
132 #endif
133 } // end ProtoNet::GetInterfaceAddressMask()
134
135 bool ProtoNet::AddInterfaceAddress(unsigned int ifIndex, const ProtoAddress& addr, unsigned int maskLen)
136 {
137 #ifndef WIN32
138 char ifName[256];
139 ifName[255] = '\0';
140 if (GetInterfaceName(ifIndex, ifName, 255))
141 {
142 return AddInterfaceAddress(ifName, addr, maskLen);
143 }
144 else
145 {
146 PLOG(PL_ERROR, "ProtoNet::AddInterfaceAddress() error: invalid interface index?!\n");
147 return false;
148 }
149 #else
150 PLOG(PL_ERROR,"ProtoNet::AddInterfaceAddress() error: function not implemented in WIN32\n");
151 return false;
152 #endif
153 } // end ProtoNet::AddInterfaceAddress()
154
155 bool ProtoNet::RemoveInterfaceAddress(unsigned int ifIndex, const ProtoAddress& addr, unsigned int maskLen)
156 {
157 #ifndef WIN32
158 char ifName[256];
159 ifName[255] = '\0';
160 if (GetInterfaceName(ifIndex, ifName, 255))
161 {
162 return RemoveInterfaceAddress(ifName, addr, maskLen);
163 }
164 else
165 {
166 PLOG(PL_ERROR, "ProtoNet::AddInterfaceAddress() error: invalid interface index?!\n");
167 return false;
168 }
169 #else
170 PLOG(PL_ERROR,"ProtoNet::RemoveInterfaceAddress() error: function not implemented in WIN32\n");
171 return false;
172 #endif
173 } // end ProtoNet::RemoveInterfaceAddress()
174 #endif // !WIN32
175
176 ProtoNet::Monitor::Monitor()
177 {
178 // Enable input notification by default
179 StartInputNotification();
180 }
181
182 ProtoNet::Monitor::~Monitor()
183 {
184 Close();
185 }
186
187 ProtoNet::Monitor::Event::Event()
188 : event_type(UNKNOWN_EVENT), iface_index(0)
189 {
190 strcpy(iface_name, "???");
191 iface_name[IFNAME_MAX] = '\0';
192 }
193
194
195 ProtoNet::Monitor::Event::~Event()
196 {
197 }