-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathipaddr.cpp
482 lines (369 loc) · 12 KB
/
ipaddr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#include "server.h"
#ifndef NO_REPORT
#include <iomanip>
#endif
#include <sys/socket.h>
#include <sys/param.h>
#include <netdb.h>
#include <unistd.h>
#include <string>
#include <cstring>
// Local types
#define ILLEGAL_NODE nodename_t(0xfffffffful)
class IpInfo : private Noncopyable {
sockaddr_in in;
nodename_t name_;
DataOut* partial;
IpInfo& operator= (IpInfo const&);
public:
IpInfo() : name_(ILLEGAL_NODE), partial(0)
{
in.sin_family = AF_INET;
in.sin_port = htons(0);
in.sin_addr.s_addr = htonl(0);
#if THIS_TARGET != Linux_Target && THIS_TARGET != SunOS_Target
in.sin_len = sizeof(in);
#endif
}
sockaddr_in const* addr() const { return ∈ }
DataOut* partialBuffer() const { return partial; }
void setPartialBuffer(DataOut* ptr) { partial = ptr; }
bool matches(nodename_t nm) const { return name_ == nm; }
nodename_t name() const { return name_; }
void update(nodename_t n, ipaddr_t a)
{
if (!n.isBlank())
name_ = n;
if (a.isValid()) {
in.sin_addr.s_addr = htonl(a.value());
in.sin_port = htons(acnetPort);
}
}
};
// Local prototypes
static void eraseNode(trunknode_t);
static void insertNode(trunknode_t, nodename_t, ipaddr_t);
static IpInfo* returnTrunk(trunk_t);
// Local data
static IpInfo* addrMap[256] = { 0 };
static trunknode_t myNode_;
static ipaddr_t myIp_;
static nodename_t myHostName_;
static int64_t lastNodeTableDownloadTime_ = 0;
std::ostream& operator<<(std::ostream& os, ipaddr_t v)
{
os << (v.a >> 24) << "." << (int) ((uint8_t) (v.a >> 16)) << "." <<
(int) ((uint8_t) (v.a >> 8)) << "." << (int) ((uint8_t) v.a);
return os;
}
void generateKillerMessages()
{
assert(myIp_.isValid());
uint32_t const addr = htonl(myIp_.value());
syslog(LOG_WARNING, "Sending killer messages for node %s", myHostName_.str());
for (size_t trunk = 0; trunk < sizeof(addrMap) / sizeof(*addrMap); ++trunk)
if (addrMap[trunk])
for (size_t node = 0; node < 256; ++node) {
IpInfo const& data = addrMap[trunk][node];
if (data.name() != ILLEGAL_NODE &&
data.addr()->sin_addr.s_addr == addr)
sendKillerMessage(trunknode_t(trunk_t(trunk), node_t(node)));
}
}
bool addrLookup(ipaddr_t a, trunknode_t& tn)
{
for (size_t trunk = 0; trunk < 256; ++trunk) {
IpInfo const* const ptr = addrMap[trunk];
if (ptr)
for (size_t node = 0; node < 256; ++node) {
IpInfo const* const record = ptr + node;
if (record->name() != ILLEGAL_NODE && htonl(a.value()) == record->addr()->sin_addr.s_addr) {
tn = trunknode_t(trunk_t(trunk), node_t(node));
return true;
}
}
}
return false;
}
// Clears out the entry for the given trunk and node.
static void eraseNode(trunknode_t tn)
{
if (addrMap[tn.trunk().raw()])
addrMap[tn.trunk().raw()][tn.node().raw()].update(ILLEGAL_NODE, ipaddr_t());
}
// Returns the IpInfo structure associated with the given trunk and node.
IpInfo* findNodeInfo(trunknode_t tn)
{
IpInfo* const ptr = addrMap[tn.trunk().raw()];
if (ptr) {
IpInfo* const data = ptr + tn.node().raw();
if (data->name() != ILLEGAL_NODE)
return data;
}
return 0;
}
// Writes a report of the IP table to the specified stream.
#ifndef NO_REPORT
void generateIpReport(std::ostream& os)
{
os << "\t\t<div class=\"section\">\n"
"\t\t<h1>IP Table Report</h1>\n" << std::setw(0) << std::hex << std::setfill(' ');
time_t t = (time_t) (lastNodeTableDownloadTime() / 1000);
if (t)
os << "\t\t<p>Last node table download: " << ctime(&t) << "<p>";
else
os << "<p>Waiting for node table download<p>";
os << "\t\t<table = width=\"80%\">\n"
"\t\t\t<colgroup>\n"
"\t\t\t\t<col class=\"label\"/>\n"
"\t\t\t\t<col/>\n"
"\t\t\t</colgroup>\n"
"\t\t\t<thead>\n"
"\t\t\t\t<tr><td>TRUNK</td><td>NODE</td><td>IP Address</td><td>NAME</td></tr>\n"
"\t\t\t</thead>\n"
"\t\t\t<tbody>\n";
bool even = false;
for (size_t trunk = 0; trunk < 256; ++trunk) {
IpInfo const* const base = addrMap[trunk];
if (base)
for (size_t node = 0; node < 256; ++node) {
IpInfo const* const record = base + node;
if (record->name() != ILLEGAL_NODE) {
uint32_t const ip = ntohl(record->addr()->sin_addr.s_addr);
os << "\t\t\t<tr" << (even ? " class=\"even\"" : "") << "><td>" << std::hex << (unsigned) trunk <<
"</td><td>" << (unsigned) node << "</td><td>" << std::dec << (ip >> 24) << '.' << ((ip >> 16) & 0xff) <<
'.' << ((ip >> 8) & 0xff) << '.' << (ip & 0xff) << "</td><td>" << record->name().str() << "</td></tr>\n";
even = !even;
}
}
}
os << "\t\t\t</tbody>\n\t\t</table>\n\t\t</div>\n";
}
#endif
// Get address for specified trunk/node.
sockaddr_in const *getAddr(trunknode_t tn)
{
IpInfo const* const ii = findNodeInfo(tn);
return ii ? ii->addr() : 0;
}
ipaddr_t getIpAddr(char const host[])
{
hostent const* const he = gethostbyname(host);
return he ? ipaddr_t(ntohl(*(uint32_t*) he->h_addr_list[0])) : ipaddr_t();
}
int64_t lastNodeTableDownloadTime()
{
return lastNodeTableDownloadTime_;
}
ipaddr_t myIp()
{
return myIp_;
}
trunknode_t myNode()
{
return myNode_;
}
void setMyHostName(nodename_t newHostName)
{
myHostName_ = newHostName;
}
nodename_t myHostName()
{
return myHostName_;
}
// Returns the partial buffer associated with a node. If the node isn't in the map or the node doesn't have a partial buffer,
// then a NULL pointer is returned.
DataOut* partialBuffer(trunknode_t tn)
{
IpInfo const* const ii = findNodeInfo(tn);
return ii ? ii->partialBuffer() : 0;
}
// Inserts a node into the node table.
static void insertNode(trunknode_t tn, nodename_t name, ipaddr_t a)
{
IpInfo* const ptr = returnTrunk(tn.trunk());
assert(ptr);
ptr[tn.node().raw()].update(name, a);
}
bool isMulticastHandle(taskhandle_t th)
{
ipaddr_t a;
if (nameLookup(nodename_t(th), a))
return a.isMulticast();
return false;
}
bool isMulticastNode(trunknode_t const tn)
{
sockaddr_in const* const addr = getAddr(tn);
return addr ? IN_MULTICAST(ntohl(addr->sin_addr.s_addr)) : false;
}
bool isThisMachine(trunknode_t const tn)
{
IpInfo const* const ptr = findNodeInfo(tn);
return ptr && myIp_ == ipaddr_t(ntohl(ptr->addr()->sin_addr.s_addr));
}
// Lookup the trunknode_t for a given nodename_t (slowly)
bool nameLookup(nodename_t name, trunknode_t& tn)
{
for (size_t trunk = 0; trunk < 256; ++trunk) {
IpInfo const* const ptr = addrMap[trunk];
if (ptr)
for (size_t node = 0; node < 256; ++node) {
IpInfo const* const record = ptr + node;
if (record->name() != ILLEGAL_NODE && record->matches(name)) {
tn = trunknode_t(trunk_t(trunk), node_t(node));
return true;
}
}
}
return false;
}
bool nameLookup(nodename_t name, ipaddr_t& a)
{
for (size_t trunk = 0; trunk < 256; ++trunk) {
IpInfo const* const ptr = addrMap[trunk];
if (ptr)
for (size_t node = 0; node < 256; ++node) {
IpInfo const* const record = ptr + node;
if (record->name() != ILLEGAL_NODE && record->matches(name)) {
a = ipaddr_t(ntohl(record->addr()->sin_addr.s_addr));
return true;
}
}
}
return false;
}
bool nodeLookup(trunknode_t tn, nodename_t& name)
{
IpInfo const* const ii = findNodeInfo(tn);
if (ii) {
name = ii->name();
return true;
}
return false;
}
// Returns a pointer to an array of IpInfo structures associated with the specified trunk.
static IpInfo* returnTrunk(trunk_t t)
{
IpInfo*& location = addrMap[t.raw()];
if (!location)
location = new IpInfo[256];
return location;
}
void setLastNodeTableDownloadTime()
{
lastNodeTableDownloadTime_ = now();
}
void setMyIp(ipaddr_t addr)
{
myIp_ = addr;
syslog(LOG_WARNING, "Forcing address to %s to align with entry for %s", addr.str().c_str(), myHostName_.str());
}
// This function is called when acnetd starts up. It does a DNS lookup of its own name to get its IP address. This way, when
// the IP table gets populated, we can determine our own trunk and node.
void setMyIp()
{
char name[MAXHOSTNAMELEN];
// Fill in our buffer with the current machine's network name.
(void) gethostname(name, sizeof(name));
// Do a DNS lookup of the name. If we succeed, we remember the IP address. Otherwise report an error indicating a
// reduction in service.
myIp_ = getIpAddr(name);
if (!myIp_.isValid())
syslog(LOG_WARNING, "DNS failure, '%s' -- we won't be able to recognize local traffic", hstrerror(h_errno));
// If the hostName wasn't set on the command line then use the os hostname
if (myHostName_.isBlank()) {
// Only use the host part of a fully-qualified hostname
char *p = strchr(name, '.');
if (p)
*p = 0;
// Save the rad50 equivalent of our host name
myHostName_ = nodename_t(ator(name));
}
// Add generic multicast to trunk/node table
ipaddr_t mcAddr = octetsToIp(239, 128, 4, 1);
insertNode(ACNET_MULTICAST, nodename_t(ator("MCAST")), mcAddr);
joinMulticastGroup(sClient, mcAddr);
}
// Associates a partially filled buffer with an ACNET node.
void setPartialBuffer(trunknode_t tn, DataOut* ptr)
{
IpInfo* const ii = findNodeInfo(tn);
if (ii)
ii->setPartialBuffer(ptr);
}
bool trunkExists(trunk_t t)
{
return addrMap[t.raw()];
}
// Update trunk/node lookup to map (used for internal node table entries)
void updateAddr(trunknode_t tn, nodename_t newName, ipaddr_t newAddr)
{
// Don't let an application add an entry for the LOCAL address.
if (tn.isBlank()) {
syslog(LOG_WARNING, "an attempt was made to set invalid address 0x%02x%02x", tn.trunk().raw(), tn.node().raw());
return;
}
// If the IP address matches ours, we might have to update our primary trunk and node.
if (newAddr == myIp_) {
// If the node name is -1, then we're adding an entry that will get overwritten. Since we know the IP addresses
// match, we'll update the name to our hostname.
if (newName == ILLEGAL_NODE)
newName = myHostName_;
// If we don't have our node yet, use the first one that matches the ip addr without
// checking the name since the hostname may not be the same as the ACNET name.
if (myNode_.isBlank())
myNode_ = tn;
// If the hostnames match, then we're updating our primary node. In this case, we store the trunk and node for quick
// look-up. This will be the default trunk and node for connections.
if (newName == myHostName_) {
if (!myNode_.isBlank() && myNode_ != tn)
syslog(LOG_WARNING, "trunk and node, for this machine, was changed from (0x%02x%02x) to (0x%02x%02x)",
myNode_.trunk().raw(), myNode_.node().raw(), tn.trunk().raw(), tn.node().raw());
myNode_ = tn;
//syslog(LOG_WARNING, "myNode = (0x%02x%02x)", myNode_.trunk().raw(), myNode_.node().raw());
}
} else if (newName == ILLEGAL_NODE)
newName = nodename_t(ator("%%%%%%"));
if (newName.isBlank() && !newAddr.isValid())
eraseNode(tn);
else {
IpInfo* const ii = findNodeInfo(tn);
// If the node already exists in our table, we simply update the fields. If it doesn't exist, we need to insert a new
// node into the map.
if (ii) {
if (htonl(newAddr.value()) != ii->addr()->sin_addr.s_addr) {
cancelReqToNode(tn);
endRpyToNode(tn);
}
// Update the fields.
ii->update(newName, newAddr);
} else
insertNode(tn, newName, newAddr);
}
}
bool validFromAddress(char const proto[], trunknode_t const ctn, ipaddr_t const in, ipaddr_t const ip)
{
if (ip == in)
return true;
else {
if (dumpIncoming)
syslog(LOG_WARNING, "Dropping %s from %s == %s? -- client masquerading as 0x%02x%02x",
proto, in.str().c_str(), ip.str().c_str(), ctn.trunk().raw(), ctn.node().raw());
return false;
}
}
bool validToAddress(char const proto[], trunknode_t const src, trunknode_t const dst)
{
if (isThisMachine(dst))
return true;
else {
if (dumpIncoming)
syslog(LOG_WARNING, "Dropping %s -- (from 0x%02x%02x) dst node 0x%02x%02x is not this machine)", proto, src.trunk().raw(), src.node().raw(), dst.trunk().raw(), dst.node().raw());
return false;
}
}
// Local Variables:
// mode:c++
// fill-column:125
// End: