2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
5 * WvIPAliaser handles IP aliasing in the Linux kernel. See wvipaliaser.h.
7 #include "wvipaliaser.h"
8 #include "wvinterface.h"
12 WvIPAliaser::AliasList
WvIPAliaser::all_aliases
;
16 ///////////////////////////////////// WvIPAliaser::Alias
20 WvIPAliaser::Alias::Alias(const WvIPAddr
&_ip
) : ip(_ip
)
23 WvIPNet
nonet(noip
, noip
);
26 for (index
= 0; index
< 256; index
++)
28 WvInterface
i(WvString("lo:wv%s", index
));
30 if (!i
.isup() || i
.ipaddr() == nonet
) // not in use yet!
34 if (WvIPAddr(i
.ipaddr()) != ip
)
36 // no permission, most likely.
43 if (i
.isup() && WvIPNet(i
.ipaddr(),32) == ip
)
45 // a bit weird... this alias already has the right address.
51 // got through all possible names without a free one? Weird!
56 WvIPAliaser::Alias::~Alias()
60 WvInterface
i(WvString("lo:wv%s", index
));
61 // i.setipaddr(WvIPAddr()); // not necessary in recent kernels
68 //////////////////////////////////// WvIPAliaser
72 WvIPAliaser::WvIPAliaser() : interfaces()
78 WvIPAliaser::~WvIPAliaser()
80 // clear the alias list
86 void WvIPAliaser::start_edit()
88 AliasList::Iter
i(aliases
);
91 AliasList::Iter
i_all(all_aliases
);
96 for (i
.rewind(); i
.next(); )
98 assert(i_all
.find(i
.ptr()));
100 // the global alias entry goes down by one
104 // empty out the local list
109 WvIPAliaser::Alias
*WvIPAliaser::ipsearch(WvIPAliaser::AliasList
&l
,
112 AliasList::Iter
i(l
);
114 for (i
.rewind(); i
.next(); )
116 if (i
->ip
== WvIPAddr(ip
))
124 bool WvIPAliaser::add(const WvIPAddr
&ip
)
128 if (WvIPAddr(ip
) == WvIPAddr() || ipsearch(aliases
, ip
))
129 return false; // already done.
131 // If the alias is already a local address, there is no need for an alias.
132 // We have to be careful that we don't find an existing alias as the
133 // local interface. Otherwise, we'll toggle that alias on and off.
134 WvString
ifc(interfaces
.islocal(WvIPAddr(ip
)));
135 if (!!ifc
&& !strchr(ifc
, ':')) // Make sure it is a real interface
138 a
= ipsearch(all_aliases
, ip
);
141 // It's already in the global list, so we add its entry to
142 // our list and increase the link count.
143 aliases
.append(a
, false);
149 // It's not there, so we add a new alias to the global list and
152 aliases
.append(a
, false);
153 all_aliases
.append(a
, true);
160 bool WvIPAliaser::done_edit()
162 bool any_change
=false;
163 AliasList::Iter
i(all_aliases
);
165 i
.rewind(); i
.next();
180 void WvIPAliaser::dump()
183 WvLog
log("local aliases");
184 AliasList::Iter
i(aliases
);
185 for (i
.rewind(); i
.next(); )
188 log("#%s = lo:wv%s: %s (%s links)\n",
189 a
.index
, a
.index
, a
.ip
, a
.link_count
);
195 WvLog
log("global aliases");
196 AliasList::Iter
i(all_aliases
);
197 for (i
.rewind(); i
.next(); )
200 log("#%s = lo:wv%s: %s (%s links)\n",
201 a
.index
, a
.index
, a
.ip
, a
.link_count
);