Fix several bugs and clean up the code

This commit is contained in:
Daniel Adolfsson 2011-09-17 01:10:23 +02:00
parent 370ba70182
commit 62edaf3c6b
9 changed files with 75 additions and 39 deletions

View File

@ -45,9 +45,14 @@ std::map<std::string, strong_ptr<iface> > iface::_map;
std::vector<struct pollfd> iface::_pollfds;
iface::iface() :
_ifd(-1), _pfd(-1)
{
}
iface::~iface()
{
DBG("iface destroyed");
DBG("iface::~iface()");
}
strong_ptr<iface> iface::open_pfd(const std::string& name)
@ -60,7 +65,7 @@ strong_ptr<iface> iface::open_pfd(const std::string& name)
if(it != _map.end())
{
if(it->second->_pfd)
if(it->second->_pfd >= 0)
return it->second;
ifa = it->second;
@ -243,9 +248,9 @@ strong_ptr<iface> iface::open_ifd(const std::string& name)
ifa = new iface();
ifa->_name = name;
/* ifa->_ptr = ifa;
ifa->_ptr = ifa;
_map[name] = ifa;*/
_map[name] = ifa;
}
else
{
@ -332,10 +337,10 @@ ssize_t iface::read_solicit(address& saddr, address& daddr, address& taddr)
if((len = read(_pfd, saddr, msg, sizeof(msg))) < 0)
return -1;
struct ip6_hdr *ip6h =
struct ip6_hdr *ip6h =
(struct ip6_hdr *)(msg + ETH_HLEN);
struct icmp6_hdr *icmph =
struct icmp6_hdr *icmph =
(struct icmp6_hdr *)(msg + ETH_HLEN + sizeof( struct ip6_hdr));
struct nd_neighbor_solicit *ns =
@ -345,6 +350,10 @@ ssize_t iface::read_solicit(address& saddr, address& daddr, address& taddr)
daddr = ip6h->ip6_dst;
saddr = ip6h->ip6_src;
DBG("iface::read_solicit() saddr=%s, daddr=%s, taddr=%s, len=%d",
daddr.to_string().c_str(), saddr.to_string().c_str(),
taddr.to_string().c_str(), len);
return len;
}
@ -428,6 +437,9 @@ ssize_t iface::read_advert(address& saddr, address& taddr)
taddr = ((struct nd_neighbor_solicit *)msg)->nd_ns_target;
DBG("iface::read_advert() saddr=%s, taddr=%s, len=%d",
saddr.to_string().c_str(), taddr.to_string().c_str(), len);
return len;
}
@ -442,7 +454,12 @@ void iface::fixup_pollfds()
for(std::map<std::string, strong_ptr<iface> >::iterator it = _map.begin();
it != _map.end(); it++)
{
_pollfds[i].fd = (i % 2) ? it->second->_pfd : it->second->_ifd;
_pollfds[i].fd = it->second->_ifd;
_pollfds[i].events = POLLIN;
_pollfds[i].revents = 0;
i++;
_pollfds[i].fd = it->second->_pfd;
_pollfds[i].events = POLLIN;
_pollfds[i].revents = 0;
i++;
@ -467,37 +484,38 @@ int iface::poll_all()
return 0;
}
// TODO: Assert _pollfds.size() == _map.size() * 2.
assert(_pollfds.size() == _map.size() * 2);
int len;
if((len = ::poll(&_pollfds[0], _pollfds.size(), 100)) < 0)
if((len = ::poll(&_pollfds[0], _pollfds.size(), 50)) < 0)
return -1;
if(len == 0)
return 0;
std::vector<struct pollfd>::iterator f_it;
std::map<std::string, strong_ptr<iface> >::iterator i_it;
std::map<std::string, strong_ptr<iface> >::iterator i_it = _map.begin();
int i = 0;
for(f_it = _pollfds.begin(), i_it = _map.begin(); f_it != _pollfds.end(); f_it++)
for(std::vector<struct pollfd>::iterator f_it = _pollfds.begin();
f_it != _pollfds.end(); f_it++)
{
bool was_pfd = i++ % 2;
assert(i_it != _map.end());
if(i && !(i % 2))
i_it++;
bool is_pfd = i++ % 2;
if(!(f_it->revents & POLLIN))
continue;
strong_ptr<iface> ifa = i_it->second;
if(was_pfd)
i_it++;
int icmp6_type;
address saddr, daddr, taddr;
if(was_pfd)
if(is_pfd)
{
if(ifa->read_solicit(saddr, daddr, taddr) < 0)
{

View File

@ -72,6 +72,9 @@ private:
// or -1 if there was an error.
int allmulti(int state);
// Constructor.
iface();
public:
// Destructor.

View File

@ -22,9 +22,27 @@ using namespace ndppd;
__NDPPD_NS_BEGIN
const char *log::_level_str[] =
{
"fatal",
"error",
"warning",
"bug",
"notice",
"info",
"debug"
};
void log::puts(int level, const char *str)
{
fprintf(stderr, "(%d) : %s\n", level, str);
const char *ls;
if((level < 0) || (level >= MAX_L))
ls = "unknown";
else
ls = _level_str[level];
fprintf(stderr, "% 7s : %s\n", ls, str);
}
void log::printf(int level, const char *fmt, ...)
@ -37,6 +55,7 @@ void log::printf(int level, const char *fmt, ...)
if(vsnprintf(buf, sizeof(buf), fmt, args) > 0)
{
puts(level, buf);
}
@ -44,5 +63,3 @@ void log::printf(int level, const char *fmt, ...)
}
__NDPPD_NS_END

View File

@ -33,6 +33,9 @@ __NDPPD_NS_BEGIN
class log
{
private:
static const char *_level_str[];
public:
enum
{
@ -42,7 +45,8 @@ public:
L_BUG,
L_NOTICE,
L_INFO,
L_DEBUG
L_DEBUG,
MAX_L
};
static void puts(int level, const char *str);
@ -53,5 +57,3 @@ public:
__NDPPD_NS_END
#endif // __NDPPD_LOG_H

View File

@ -13,7 +13,6 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <iostream>
#include <string>
@ -22,11 +21,6 @@
#include "ndppd.h"
#include "iface.h"
#include "proxy.h"
#include "rule.h"
#include "session.h"
using namespace ndppd;
int main(int argc, char *argv[], char *env[])
@ -70,7 +64,6 @@ int main(int argc, char *argv[], char *env[])
if(!conf::load(config_path))
return -1;
struct timeval t1, t2;
gettimeofday(&t1, 0);
@ -90,6 +83,8 @@ int main(int argc, char *argv[], char *env[])
session::update_all(elapsed_time);
}
ERR("iface::poll_all() failed");
return 0;
}

View File

@ -23,6 +23,8 @@
#define NDPPD_VERSION "0.1-alpha"
#include <assert.h>
#include "log.h"
#include "ptr.h"
#include "conf.h"

View File

@ -38,7 +38,7 @@ strong_ptr<proxy> proxy::create(const strong_ptr<iface>& ifa)
ifa->pr(pr);
DBG("proxy_create() ifa=%x =%x", (iface *)ifa, (proxy *)pr);
DBG("proxy::create() if=%x", ifa->name().c_str());
return pr;
}

View File

@ -36,7 +36,8 @@ strong_ptr<rule> rule::create(const strong_ptr<proxy>& pr, const address& addr,
ru->_ifa = ifa;
ru->_addr = addr;
DBG("rule addr set to %s", addr.to_string().c_str());
DBG("rule::create() if=%s, addr=%s",
pr->ifa()->name().c_str(), addr.to_string().c_str());
return ru;
}
@ -48,14 +49,14 @@ strong_ptr<rule> rule::create(const strong_ptr<proxy>& pr, const address& addr)
ru->_pr = pr;
ru->_addr = addr;
DBG("rule addr set to %s", addr.to_string().c_str());
DBG("rule::create() if=%s, addr=%s",
pr->ifa()->name().c_str(), addr.to_string().c_str());
return ru;
}
const address& rule::addr() const
{
DBG("rule addr set to %s", _addr.to_string().c_str());
return _addr;
}

View File

@ -91,12 +91,12 @@ void session::add_iface(const strong_ptr<iface>& ifa)
void session::send_solicit()
{
DBG("sending solicit");
DBG("session::send_solicit() (%d)", _ifaces.size());
for(std::list<strong_ptr<iface> >::iterator it = _ifaces.begin();
it != _ifaces.end(); it++)
{
DBG(" on %s", (*it)->name().c_str());
DBG(" - %s", (*it)->name().c_str());
(*it)->write_solicit(_taddr);
}
}
@ -111,8 +111,6 @@ void session::handle_advert()
_status = VALID;
_ttl = 500;
DBG("handle_advert");
send_advert();
}