Go back to using own implementation of smart pointers

This commit is contained in:
Daniel Adolfsson 2012-02-02 20:35:09 +01:00
parent ffb4e6d398
commit ab0ebe09ff
14 changed files with 138 additions and 131 deletions

View File

@ -1,9 +1,9 @@
DEBUG=1 DEBUG=1
ifdef DEBUG ifdef DEBUG
CXXFLAGS ?= -g -std=c++0x -DDEBUG CXXFLAGS ?= -g -DDEBUG
else else
CXXFLAGS ?= -O3 -std=c++0x CXXFLAGS ?= -O3
endif endif
PREFIX ?= /usr/local PREFIX ?= /usr/local
@ -12,7 +12,7 @@ GZIP ?= /bin/gzip
MANDIR ?= ${DESTDIR}${PREFIX}/share/man MANDIR ?= ${DESTDIR}${PREFIX}/share/man
SBINDIR ?= ${DESTDIR}${PREFIX}/sbin SBINDIR ?= ${DESTDIR}${PREFIX}/sbin
LIBS = -lconfuse LIBS =
OBJS = src/logger.o src/ndppd.o src/iface.o src/proxy.o src/address.o \ OBJS = src/logger.o src/ndppd.o src/iface.o src/proxy.o src/address.o \
src/rule.o src/session.o src/conf.o src/rule.o src/session.o src/conf.o

View File

@ -17,7 +17,9 @@
#include <cstdarg> #include <cstdarg>
#include <cstring> #include <cstring>
#include <cctype> #include <cctype>
#include <cstdlib>
#include <memory> #include <memory>
#include <string>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <netinet/ip6.h> #include <netinet/ip6.h>
@ -55,18 +57,17 @@ void conf::value(const std::string &value)
_value = value; _value = value;
} }
std::shared_ptr<conf> conf::load(const std::string &path) ptr<conf> conf::load(const std::string &path)
{ {
try {
std::ifstream ifs; std::ifstream ifs;
ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit); ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
ifs.open(path.c_str(), std::ios::in);
try {
ifs.open(path, std::ios::in);
std::string buf((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>()); std::string buf((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
ifs.close();
const char *c_buf = buf.c_str(); const char *c_buf = buf.c_str();
std::shared_ptr<conf> cf(new conf); ptr<conf> cf(new conf);
if (cf->parse_block(&c_buf)) { if (cf->parse_block(&c_buf)) {
logger l(LOG_DEBUG); logger l(LOG_DEBUG);
@ -79,7 +80,7 @@ std::shared_ptr<conf> conf::load(const std::string &path)
logger::error() << "Failed to load configuration file '" << path << "'"; logger::error() << "Failed to load configuration file '" << path << "'";
} }
return std::shared_ptr<conf>(); return ptr<conf>();
} }
bool conf::is_block() const bool conf::is_block() const
@ -152,10 +153,10 @@ bool conf::parse_block(const char **str)
p++; p++;
} }
std::shared_ptr<conf> cf(new conf); ptr<conf> cf(new conf);
if (cf->parse(&p)) { if (cf->parse(&p)) {
_map.insert(std::pair<std::string, std::shared_ptr<conf> >(ss.str(), cf)); _map.insert(std::pair<std::string, ptr<conf> >(ss.str(), cf));
} }
} }
@ -224,7 +225,7 @@ void conf::dump(logger &l, int level) const
if (_is_block) { if (_is_block) {
l << "{" << logger::endl; l << "{" << logger::endl;
std::multimap<std::string, std::shared_ptr<conf> >::const_iterator it; std::multimap<std::string, ptr<conf> >::const_iterator it;
for (it = _map.begin(); it != _map.end(); it++) { for (it = _map.begin(); it != _map.end(); it++) {
l << pfx << " " << it->first << " "; l << pfx << " " << it->first << " ";
@ -237,20 +238,20 @@ void conf::dump(logger &l, int level) const
l << logger::endl; l << logger::endl;
} }
std::shared_ptr<conf> conf::operator[](const std::string& name) const ptr<conf> conf::operator[](const std::string& name) const
{ {
std::multimap<std::string, std::shared_ptr<conf> >::const_iterator it; std::multimap<std::string, ptr<conf> >::const_iterator it;
if ((it = _map.find(name)) == _map.end()) if ((it = _map.find(name)) == _map.end())
return std::shared_ptr<conf>(); return ptr<conf>();
else else
return it->second; return it->second;
} }
std::vector<std::shared_ptr<conf> > conf::find(const std::string& name) const std::vector<ptr<conf> > conf::find(const std::string& name) const
{ {
std::vector<std::shared_ptr<conf> > vec; std::vector<ptr<conf> > vec;
std::multimap<std::string, std::shared_ptr<conf> >::const_iterator it; std::multimap<std::string, ptr<conf> >::const_iterator it;
for (it = _map.find(name); it != _map.end(); it++) { for (it = _map.find(name); it != _map.end(); it++) {
vec.push_back(it->second); vec.push_back(it->second);
} }

View File

@ -34,7 +34,7 @@ private:
bool _is_block; bool _is_block;
std::multimap<std::string, std::shared_ptr<conf> > _map; std::multimap<std::string, ptr<conf> > _map;
void dump(logger &l, int level) const; void dump(logger &l, int level) const;
@ -55,12 +55,12 @@ public:
void value(const std::string &value); void value(const std::string &value);
static std::shared_ptr<conf> load(const std::string &path); static ptr<conf> load(const std::string &path);
bool is_block() const; bool is_block() const;
std::shared_ptr<conf> operator[](const std::string &name) const; ptr<conf> operator[](const std::string &name) const;
std::vector<std::shared_ptr<conf> > find(const std::string &name) const; std::vector<ptr<conf> > find(const std::string &name) const;
void dump() const; void dump() const;

View File

@ -42,7 +42,7 @@
NDPPD_NS_BEGIN NDPPD_NS_BEGIN
std::map<std::string, std::shared_ptr<iface> > iface::_map; std::map<std::string, ptr<iface> > iface::_map;
std::vector<struct pollfd> iface::_pollfds; std::vector<struct pollfd> iface::_pollfds;
@ -64,13 +64,13 @@ iface::~iface()
} }
} }
std::shared_ptr<iface> iface::open_pfd(const std::string& name) ptr<iface> iface::open_pfd(const std::string& name)
{ {
int fd; int fd;
std::map<std::string, std::shared_ptr<iface> >::iterator it = _map.find(name); std::map<std::string, ptr<iface> >::iterator it = _map.find(name);
std::shared_ptr<iface> ifa; ptr<iface> ifa;
if (it != _map.end()) { if (it != _map.end()) {
if (it->second->_pfd >= 0) if (it->second->_pfd >= 0)
@ -83,13 +83,13 @@ std::shared_ptr<iface> iface::open_pfd(const std::string& name)
} }
if (!ifa) if (!ifa)
return std::shared_ptr<iface>(); return ptr<iface>();
// Create a socket. // Create a socket.
if ((fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IPV6))) < 0) { if ((fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IPV6))) < 0) {
logger::error() << "Unable to create socket"; logger::error() << "Unable to create socket";
return std::shared_ptr<iface>(); return ptr<iface>();
} }
// Bind to the specified interface. // Bind to the specified interface.
@ -103,13 +103,13 @@ std::shared_ptr<iface> iface::open_pfd(const std::string& name)
if (!(lladdr.sll_ifindex = if_nametoindex(name.c_str()))) { if (!(lladdr.sll_ifindex = if_nametoindex(name.c_str()))) {
close(fd); close(fd);
logger::error() << "Failed to bind to interface '" << name << "'"; logger::error() << "Failed to bind to interface '" << name << "'";
return std::shared_ptr<iface>(); return ptr<iface>();
} }
if (bind(fd, (struct sockaddr *)&lladdr, sizeof(struct sockaddr_ll)) < 0) { if (bind(fd, (struct sockaddr *)&lladdr, sizeof(struct sockaddr_ll)) < 0) {
close(fd); close(fd);
logger::error() << "Failed to bind to interface '" << name << "'"; logger::error() << "Failed to bind to interface '" << name << "'";
return std::shared_ptr<iface>(); return ptr<iface>();
} }
// Switch to non-blocking mode. // Switch to non-blocking mode.
@ -119,7 +119,7 @@ std::shared_ptr<iface> iface::open_pfd(const std::string& name)
if (ioctl(fd, FIONBIO, (char *)&on) < 0) { if (ioctl(fd, FIONBIO, (char *)&on) < 0) {
close(fd); close(fd);
logger::error() << "Failed to switch to non-blocking on interface '" << name << "'"; logger::error() << "Failed to switch to non-blocking on interface '" << name << "'";
return std::shared_ptr<iface>(); return ptr<iface>();
} }
// Set up filter. // Set up filter.
@ -154,7 +154,7 @@ std::shared_ptr<iface> iface::open_pfd(const std::string& name)
if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog)) < 0) { if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog)) < 0) {
logger::error() << "Failed to set filter"; logger::error() << "Failed to set filter";
return std::shared_ptr<iface>(); return ptr<iface>();
} }
// Set up an instance of 'iface'. // Set up an instance of 'iface'.
@ -166,11 +166,11 @@ std::shared_ptr<iface> iface::open_pfd(const std::string& name)
return ifa; return ifa;
} }
std::shared_ptr<iface> iface::open_ifd(const std::string& name) ptr<iface> iface::open_ifd(const std::string& name)
{ {
int fd; int fd;
std::map<std::string, std::shared_ptr<iface> >::iterator it = _map.find(name); std::map<std::string, ptr<iface> >::iterator it = _map.find(name);
if ((it != _map.end()) && it->second->_ifd) if ((it != _map.end()) && it->second->_ifd)
return it->second; return it->second;
@ -179,7 +179,7 @@ std::shared_ptr<iface> iface::open_ifd(const std::string& name)
if ((fd = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) { if ((fd = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
logger::error() << "Unable to create socket"; logger::error() << "Unable to create socket";
return std::shared_ptr<iface>(); return ptr<iface>();
} }
// Bind to the specified interface. // Bind to the specified interface.
@ -193,7 +193,7 @@ std::shared_ptr<iface> iface::open_ifd(const std::string& name)
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)) < 0) { if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)) < 0) {
close(fd); close(fd);
logger::error() << "Failed to bind to interface '" << name << "'"; logger::error() << "Failed to bind to interface '" << name << "'";
return std::shared_ptr<iface>(); return ptr<iface>();
} }
// Detect the link-layer address. // Detect the link-layer address.
@ -205,7 +205,7 @@ std::shared_ptr<iface> iface::open_ifd(const std::string& name)
if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) { if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
close(fd); close(fd);
logger::error() << "Failed to detect link-layer address for interface '" << name << "'"; logger::error() << "Failed to detect link-layer address for interface '" << name << "'";
return std::shared_ptr<iface>(); return ptr<iface>();
} }
logger::debug() << "fd=" << fd << ", hwaddr=" << ether_ntoa((const struct ether_addr *)&ifr.ifr_hwaddr.sa_data);; logger::debug() << "fd=" << fd << ", hwaddr=" << ether_ntoa((const struct ether_addr *)&ifr.ifr_hwaddr.sa_data);;
@ -217,13 +217,13 @@ std::shared_ptr<iface> iface::open_ifd(const std::string& name)
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hops, sizeof(hops)) < 0) { if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hops, sizeof(hops)) < 0) {
close(fd); close(fd);
logger::error() << "iface::open_ifd() failed IPV6_MULTICAST_HOPS"; logger::error() << "iface::open_ifd() failed IPV6_MULTICAST_HOPS";
return std::shared_ptr<iface>(); return ptr<iface>();
} }
if (setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &hops, sizeof(hops)) < 0) { if (setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &hops, sizeof(hops)) < 0) {
close(fd); close(fd);
logger::error() << "iface::open_ifd() failed IPV6_UNICAST_HOPS"; logger::error() << "iface::open_ifd() failed IPV6_UNICAST_HOPS";
return std::shared_ptr<iface>(); return ptr<iface>();
} }
// Switch to non-blocking mode. // Switch to non-blocking mode.
@ -233,7 +233,7 @@ std::shared_ptr<iface> iface::open_ifd(const std::string& name)
if (ioctl(fd, FIONBIO, (char *)&on) < 0) { if (ioctl(fd, FIONBIO, (char *)&on) < 0) {
close(fd); close(fd);
logger::error() << "Failed to switch to non-blocking on interface '" << name << "'"; logger::error() << "Failed to switch to non-blocking on interface '" << name << "'";
return std::shared_ptr<iface>(); return ptr<iface>();
} }
// Set up filter. // Set up filter.
@ -244,15 +244,15 @@ std::shared_ptr<iface> iface::open_ifd(const std::string& name)
if (setsockopt(fd, IPPROTO_ICMPV6, ICMP6_FILTER, &filter, sizeof(filter)) < 0) { if (setsockopt(fd, IPPROTO_ICMPV6, ICMP6_FILTER, &filter, sizeof(filter)) < 0) {
logger::error() << "Failed to set filter"; logger::error() << "Failed to set filter";
return std::shared_ptr<iface>(); return ptr<iface>();
} }
// Set up an instance of 'iface'. // Set up an instance of 'iface'.
std::shared_ptr<iface> ifa; ptr<iface> ifa;
if (it == _map.end()) { if (it == _map.end()) {
ifa.reset(new iface()); ifa = new iface();
ifa->_name = name; ifa->_name = name;
ifa->_ptr = ifa; ifa->_ptr = ifa;
@ -451,7 +451,7 @@ void iface::fixup_pollfds()
logger::debug() << "iface::fixup_pollfds() _map.size()=" << _map.size(); logger::debug() << "iface::fixup_pollfds() _map.size()=" << _map.size();
for (std::map<std::string, std::shared_ptr<iface> >::iterator it = _map.begin(); for (std::map<std::string, ptr<iface> >::iterator it = _map.begin();
it != _map.end(); it++) { it != _map.end(); it++) {
_pollfds[i].fd = it->second->_ifd; _pollfds[i].fd = it->second->_ifd;
_pollfds[i].events = POLLIN; _pollfds[i].events = POLLIN;
@ -465,18 +465,18 @@ void iface::fixup_pollfds()
} }
} }
void iface::remove_session(const std::shared_ptr<session>& se) void iface::remove_session(const ptr<session>& se)
{ {
for (std::list<std::weak_ptr<session> >::iterator it = _sessions.begin(); for (std::list<weak_ptr<session> >::iterator it = _sessions.begin();
it != _sessions.end(); it++) { it != _sessions.end(); it++) {
if (it->lock() == se) { if (*it == se) {
_sessions.erase(it); _sessions.erase(it);
break; break;
} }
} }
} }
void iface::add_session(const std::shared_ptr<session>& se) void iface::add_session(const ptr<session>& se)
{ {
_sessions.push_back(se); _sessions.push_back(se);
} }
@ -498,7 +498,7 @@ int iface::poll_all()
if (len == 0) if (len == 0)
return 0; return 0;
std::map<std::string, std::shared_ptr<iface> >::iterator i_it = _map.begin(); std::map<std::string, ptr<iface> >::iterator i_it = _map.begin();
int i = 0; int i = 0;
@ -514,7 +514,7 @@ int iface::poll_all()
if (!(f_it->revents & POLLIN)) if (!(f_it->revents & POLLIN))
continue; continue;
std::shared_ptr<iface> ifa = i_it->second; ptr<iface> ifa = i_it->second;
address saddr, daddr, taddr; address saddr, daddr, taddr;
@ -534,9 +534,9 @@ int iface::poll_all()
continue; continue;
} }
for (std::list<std::weak_ptr<session> >::iterator s_it = ifa->_sessions.begin(); for (std::list<weak_ptr<session> >::iterator s_it = ifa->_sessions.begin();
s_it != ifa->_sessions.end(); s_it++) { s_it != ifa->_sessions.end(); s_it++) {
const std::shared_ptr<session> sess = s_it->lock(); const ptr<session> sess = *s_it;
if ((sess->taddr() == taddr) && (sess->status() == session::WAITING)) { if ((sess->taddr() == taddr) && (sess->status() == session::WAITING)) {
sess->handle_advert(); sess->handle_advert();
break; break;
@ -582,12 +582,12 @@ const std::string& iface::name() const
return _name; return _name;
} }
void iface::pr(const std::shared_ptr<proxy>& pr) void iface::pr(const ptr<proxy>& pr)
{ {
_pr = pr; _pr = pr;
} }
const std::shared_ptr<proxy>& iface::pr() const const ptr<proxy>& iface::pr() const
{ {
return _pr; return _pr;
} }

View File

@ -34,9 +34,9 @@ class iface
{ {
private: private:
// Weak pointer so this object can reference itself. // Weak pointer so this object can reference itself.
std::weak_ptr<iface> _ptr; weak_ptr<iface> _ptr;
static std::map<std::string, std::shared_ptr<iface> > _map; static std::map<std::string, ptr<iface> > _map;
// An array of objects used with ::poll. // An array of objects used with ::poll.
static std::vector<struct pollfd> _pollfds; static std::vector<struct pollfd> _pollfds;
@ -60,9 +60,9 @@ private:
// An array of sessions that are monitoring this interface for // An array of sessions that are monitoring this interface for
// ND_NEIGHBOR_ADVERT messages. // ND_NEIGHBOR_ADVERT messages.
std::list<std::weak_ptr<session> > _sessions; std::list<weak_ptr<session> > _sessions;
std::shared_ptr<proxy> _pr; ptr<proxy> _pr;
// The link-layer address of this interface. // The link-layer address of this interface.
struct ether_addr hwaddr; struct ether_addr hwaddr;
@ -79,9 +79,9 @@ public:
// Destructor. // Destructor.
~iface(); ~iface();
static std::shared_ptr<iface> open_ifd(const std::string& name); static ptr<iface> open_ifd(const std::string& name);
static std::shared_ptr<iface> open_pfd(const std::string& name); static ptr<iface> open_pfd(const std::string& name);
static int poll_all(); static int poll_all();
@ -105,13 +105,13 @@ public:
const std::string& name() const; const std::string& name() const;
// Adds a session to be monitored for ND_NEIGHBOR_ADVERT messages. // Adds a session to be monitored for ND_NEIGHBOR_ADVERT messages.
void add_session(const std::shared_ptr<session>& se); void add_session(const ptr<session>& se);
void remove_session(const std::shared_ptr<session>& se); void remove_session(const ptr<session>& se);
void pr(const std::shared_ptr<proxy>& pr); void pr(const ptr<proxy>& pr);
const std::shared_ptr<proxy>& pr() const; const ptr<proxy>& pr() const;
}; };
NDPPD_NS_END NDPPD_NS_END

View File

@ -16,6 +16,7 @@
#include <cstdio> #include <cstdio>
#include <cstdarg> #include <cstdarg>
#include <cctype> #include <cctype>
#include <cstdlib>
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>

View File

@ -13,6 +13,8 @@
// //
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <cstdlib>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <string> #include <string>
@ -52,24 +54,24 @@ int daemonize()
bool configure(const std::string &path) bool configure(const std::string &path)
{ {
std::shared_ptr<conf> cf; ptr<conf> cf;
if (!(cf = conf::load(path))) if (!(cf = conf::load(path)))
return false; return false;
std::vector<std::shared_ptr<conf> >::const_iterator p_it; std::vector<ptr<conf> >::const_iterator p_it;
std::vector<std::shared_ptr<conf> > proxies(cf->find("proxy")); std::vector<ptr<conf> > proxies(cf->find("proxy"));
for (p_it = proxies.begin(); p_it != proxies.end(); p_it++) { for (p_it = proxies.begin(); p_it != proxies.end(); p_it++) {
std::shared_ptr<conf> pr_cf = *p_it, x_cf; ptr<conf> pr_cf = *p_it, x_cf;
if (pr_cf->value() == "") { if (pr_cf->value() == "") {
logger::error() << "'proxy' section is missing interface name"; logger::error() << "'proxy' section is missing interface name";
return false; return false;
} }
std::shared_ptr<proxy> pr = proxy::open(pr_cf->value()); ptr<proxy> pr = proxy::open(pr_cf->value());
if (!pr) { if (!pr) {
logger::error() << "Configuration failed for proxy '" << pr_cf->value() << "'"; logger::error() << "Configuration failed for proxy '" << pr_cf->value() << "'";
@ -91,12 +93,12 @@ bool configure(const std::string &path)
else else
pr->timeout(x_cf->int_value()); pr->timeout(x_cf->int_value());
std::vector<std::shared_ptr<conf> >::const_iterator r_it; std::vector<ptr<conf> >::const_iterator r_it;
std::vector<std::shared_ptr<conf> > rules(pr_cf->find("rule")); std::vector<ptr<conf> > rules(pr_cf->find("rule"));
for (r_it = rules.begin(); r_it != rules.end(); r_it++) { for (r_it = rules.begin(); r_it != rules.end(); r_it++) {
std::shared_ptr<conf> ru_cf = *r_it; ptr<conf> ru_cf = *r_it;
if (ru_cf->value() == "") { if (ru_cf->value() == "") {
logger::error() << "'rule' is missing an IPv6 address/net"; logger::error() << "'rule' is missing an IPv6 address/net";

View File

@ -25,6 +25,8 @@
#include <assert.h> #include <assert.h>
#include "ptr.h"
#include "logger.h" #include "logger.h"
#include "conf.h" #include "conf.h"
#include "address.h" #include "address.h"

View File

@ -31,9 +31,9 @@ proxy::proxy() :
{ {
} }
std::shared_ptr<proxy> proxy::create(const std::shared_ptr<iface>& ifa) ptr<proxy> proxy::create(const ptr<iface>& ifa)
{ {
std::shared_ptr<proxy> pr(new proxy()); ptr<proxy> pr(new proxy());
pr->_ptr = pr; pr->_ptr = pr;
pr->_ifa = ifa; pr->_ifa = ifa;
@ -44,12 +44,12 @@ std::shared_ptr<proxy> proxy::create(const std::shared_ptr<iface>& ifa)
return pr; return pr;
} }
std::shared_ptr<proxy> proxy::open(const std::string& ifname) ptr<proxy> proxy::open(const std::string& ifname)
{ {
std::shared_ptr<iface> ifa = iface::open_pfd(ifname); ptr<iface> ifa = iface::open_pfd(ifname);
if (!ifa) if (!ifa)
return std::shared_ptr<proxy>(); return ptr<proxy>();
return create(ifa); return create(ifa);
} }
@ -63,7 +63,7 @@ void proxy::handle_solicit(const address& saddr, const address& daddr,
// Let's check this proxy's list of sessions to see if we can // Let's check this proxy's list of sessions to see if we can
// find one with the same target address. // find one with the same target address.
for (std::list<std::shared_ptr<session> >::iterator sit = _sessions.begin(); for (std::list<ptr<session> >::iterator sit = _sessions.begin();
sit != _sessions.end(); sit++) { sit != _sessions.end(); sit++) {
if ((*sit)->taddr() == taddr) { if ((*sit)->taddr() == taddr) {
@ -83,17 +83,17 @@ void proxy::handle_solicit(const address& saddr, const address& daddr,
// Since we couldn't find a session that matched, we'll try to find // Since we couldn't find a session that matched, we'll try to find
// a matching rule instead, and then set up a new session. // a matching rule instead, and then set up a new session.
std::shared_ptr<session> se; ptr<session> se;
for (std::list<std::shared_ptr<rule> >::iterator it = _rules.begin(); for (std::list<ptr<rule> >::iterator it = _rules.begin();
it != _rules.end(); it++) { it != _rules.end(); it++) {
std::shared_ptr<rule> ru = *it; ptr<rule> ru = *it;
logger::debug() << "checking " << ru->addr().to_string() << " against " << taddr; logger::debug() << "checking " << ru->addr().to_string() << " against " << taddr;
if (ru->addr() == taddr) { if (ru->addr() == taddr) {
if (!se) if (!se)
se = session::create(_ptr.lock(), saddr, daddr, taddr); se = session::create(_ptr, saddr, daddr, taddr);
if (!ru->ifa()) { if (!ru->ifa()) {
// This rule doesn't have an interface, and thus we'll consider // This rule doesn't have an interface, and thus we'll consider
@ -113,26 +113,26 @@ void proxy::handle_solicit(const address& saddr, const address& daddr,
} }
} }
std::shared_ptr<rule> proxy::add_rule(const address& addr, const std::shared_ptr<iface>& ifa) ptr<rule> proxy::add_rule(const address& addr, const ptr<iface>& ifa)
{ {
std::shared_ptr<rule> ru(rule::create(_ptr.lock(), addr, ifa)); ptr<rule> ru(rule::create(_ptr, addr, ifa));
_rules.push_back(ru); _rules.push_back(ru);
return ru; return ru;
} }
std::shared_ptr<rule> proxy::add_rule(const address& addr) ptr<rule> proxy::add_rule(const address& addr)
{ {
std::shared_ptr<rule> ru(rule::create(_ptr.lock(), addr)); ptr<rule> ru(rule::create(_ptr, addr));
_rules.push_back(ru); _rules.push_back(ru);
return ru; return ru;
} }
void proxy::remove_session(const std::shared_ptr<session>& se) void proxy::remove_session(const ptr<session>& se)
{ {
_sessions.remove(se); _sessions.remove(se);
} }
const std::shared_ptr<iface>& proxy::ifa() const const ptr<iface>& proxy::ifa() const
{ {
return _ifa; return _ifa;
} }

View File

@ -31,13 +31,13 @@ class rule;
class proxy class proxy
{ {
private: private:
std::weak_ptr<proxy> _ptr; weak_ptr<proxy> _ptr;
std::shared_ptr<iface> _ifa; ptr<iface> _ifa;
std::list<std::shared_ptr<rule> > _rules; std::list<ptr<rule> > _rules;
std::list<std::shared_ptr<session> > _sessions; std::list<ptr<session> > _sessions;
bool _router; bool _router;
@ -46,20 +46,20 @@ private:
proxy(); proxy();
public: public:
static std::shared_ptr<proxy> create(const std::shared_ptr<iface>& ifa); static ptr<proxy> create(const ptr<iface>& ifa);
static std::shared_ptr<proxy> open(const std::string& ifn); static ptr<proxy> open(const std::string& ifn);
void handle_solicit(const address& saddr, const address& daddr, void handle_solicit(const address& saddr, const address& daddr,
const address& taddr); const address& taddr);
void remove_session(const std::shared_ptr<session>& se); void remove_session(const ptr<session>& se);
std::shared_ptr<rule> add_rule(const address& addr, const std::shared_ptr<iface>& ifa); ptr<rule> add_rule(const address& addr, const ptr<iface>& ifa);
std::shared_ptr<rule> add_rule(const address& addr); ptr<rule> add_rule(const address& addr);
const std::shared_ptr<iface>& ifa() const; const ptr<iface>& ifa() const;
bool router() const; bool router() const;

View File

@ -28,9 +28,9 @@ rule::rule()
{ {
} }
std::shared_ptr<rule> rule::create(const std::shared_ptr<proxy>& pr, const address& addr, const std::shared_ptr<iface>& ifa) ptr<rule> rule::create(const ptr<proxy>& pr, const address& addr, const ptr<iface>& ifa)
{ {
std::shared_ptr<rule> ru(new rule()); ptr<rule> ru(new rule());
ru->_ptr = ru; ru->_ptr = ru;
ru->_pr = pr; ru->_pr = pr;
ru->_ifa = ifa; ru->_ifa = ifa;
@ -41,9 +41,9 @@ std::shared_ptr<rule> rule::create(const std::shared_ptr<proxy>& pr, const addre
return ru; return ru;
} }
std::shared_ptr<rule> rule::create(const std::shared_ptr<proxy>& pr, const address& addr) ptr<rule> rule::create(const ptr<proxy>& pr, const address& addr)
{ {
std::shared_ptr<rule> ru(new rule()); ptr<rule> ru(new rule());
ru->_ptr = ru; ru->_ptr = ru;
ru->_pr = pr; ru->_pr = pr;
ru->_addr = addr; ru->_addr = addr;
@ -58,7 +58,7 @@ const address& rule::addr() const
return _addr; return _addr;
} }
std::shared_ptr<iface> rule::ifa() const ptr<iface> rule::ifa() const
{ {
return _ifa; return _ifa;
} }

View File

@ -31,24 +31,24 @@ class proxy;
class rule class rule
{ {
private: private:
std::weak_ptr<rule> _ptr; weak_ptr<rule> _ptr;
std::shared_ptr<proxy> _pr; ptr<proxy> _pr;
std::shared_ptr<iface> _ifa; ptr<iface> _ifa;
address _addr; address _addr;
rule(); rule();
public: public:
static std::shared_ptr<rule> create(const std::shared_ptr<proxy>& pr, const address& addr, const std::shared_ptr<iface>& ifa); static ptr<rule> create(const ptr<proxy>& pr, const address& addr, const ptr<iface>& ifa);
static std::shared_ptr<rule> create(const std::shared_ptr<proxy>& pr, const address& addr); static ptr<rule> create(const ptr<proxy>& pr, const address& addr);
const address& addr() const; const address& addr() const;
std::shared_ptr<iface> ifa() const; ptr<iface> ifa() const;
bool is_static() const; bool is_static() const;

View File

@ -22,13 +22,13 @@
NDPPD_NS_BEGIN NDPPD_NS_BEGIN
std::list<std::weak_ptr<session> > session::_sessions; std::list<weak_ptr<session> > session::_sessions;
void session::update_all(int elapsed_time) void session::update_all(int elapsed_time)
{ {
for (std::list<std::weak_ptr<session> >::iterator it = _sessions.begin(); for (std::list<weak_ptr<session> >::iterator it = _sessions.begin();
it != _sessions.end(); ) { it != _sessions.end(); ) {
std::shared_ptr<session> se = (*it++).lock(); ptr<session> se = (*it++);
if ((se->_ttl -= elapsed_time) >= 0) if ((se->_ttl -= elapsed_time) >= 0)
continue; continue;
@ -50,24 +50,24 @@ session::~session()
{ {
logger::debug() << "session::~session() this=" << logger::format("%x", this); logger::debug() << "session::~session() this=" << logger::format("%x", this);
for (std::list<std::weak_ptr<session> >::iterator it = _sessions.begin(); for (std::list<weak_ptr<session> >::iterator it = _sessions.begin();
it != _sessions.end(); it++) { it != _sessions.end(); it++) {
if (it->lock() == _ptr.lock()) { if (*it == _ptr) {
_sessions.erase(it); _sessions.erase(it);
break; break;
} }
} }
for (std::list<std::shared_ptr<iface> >::iterator it = _ifaces.begin(); for (std::list<ptr<iface> >::iterator it = _ifaces.begin();
it != _ifaces.end(); it++) { it != _ifaces.end(); it++) {
(*it)->remove_session(_ptr.lock()); (*it)->remove_session(_ptr);
} }
} }
std::shared_ptr<session> session::create(const std::shared_ptr<proxy>& pr, const address& saddr, ptr<session> session::create(const ptr<proxy>& pr, const address& saddr,
const address& daddr, const address& taddr) const address& daddr, const address& taddr)
{ {
std::shared_ptr<session> se(new session()); ptr<session> se(new session());
se->_ptr = se; se->_ptr = se;
se->_pr = pr; se->_pr = pr;
@ -78,18 +78,19 @@ std::shared_ptr<session> session::create(const std::shared_ptr<proxy>& pr, const
_sessions.push_back(se); _sessions.push_back(se);
logger::debug() << "session::create() pr=" << logger::format("%x", pr.get()) << ", saddr=" << saddr logger::debug()
<< ", daddr=" << daddr << ", taddr=" << taddr << " =" << logger::format("%x", se.get()); << "session::create() pr=" << logger::format("%x", (proxy *)pr) << ", saddr=" << saddr
<< ", daddr=" << daddr << ", taddr=" << taddr << " =" << logger::format("%x", (session *)se);
return se; return se;
} }
void session::add_iface(const std::shared_ptr<iface>& ifa) void session::add_iface(const ptr<iface>& ifa)
{ {
if (std::find(_ifaces.begin(), _ifaces.end(), ifa) != _ifaces.end()) if (std::find(_ifaces.begin(), _ifaces.end(), ifa) != _ifaces.end())
return; return;
ifa->add_session(_ptr.lock()); ifa->add_session(_ptr);
_ifaces.push_back(ifa); _ifaces.push_back(ifa);
} }
@ -97,7 +98,7 @@ void session::send_solicit()
{ {
logger::debug() << "session::send_solicit() (" << _ifaces.size() << ")"; logger::debug() << "session::send_solicit() (" << _ifaces.size() << ")";
for (std::list<std::shared_ptr<iface> >::iterator it = _ifaces.begin(); for (std::list<ptr<iface> >::iterator it = _ifaces.begin();
it != _ifaces.end(); it++) { it != _ifaces.end(); it++) {
logger::debug() << " - %s" << (*it)->name(); logger::debug() << " - %s" << (*it)->name();
(*it)->write_solicit(_taddr); (*it)->write_solicit(_taddr);

View File

@ -27,15 +27,15 @@ class iface;
class session class session
{ {
private: private:
std::weak_ptr<session> _ptr; weak_ptr<session> _ptr;
std::shared_ptr<proxy> _pr; ptr<proxy> _pr;
address _saddr, _daddr, _taddr; address _saddr, _daddr, _taddr;
// An array of interfaces this session is monitoring for // An array of interfaces this session is monitoring for
// ND_NEIGHBOR_ADVERT on. // ND_NEIGHBOR_ADVERT on.
std::list<std::shared_ptr<iface> > _ifaces; std::list<ptr<iface> > _ifaces;
// The remaining time in miliseconds the object will stay in the // The remaining time in miliseconds the object will stay in the
// interface's session array or cache. // interface's session array or cache.
@ -43,7 +43,7 @@ private:
int _status; int _status;
static std::list<std::weak_ptr<session> > _sessions; static std::list<weak_ptr<session> > _sessions;
public: public:
enum enum
@ -58,10 +58,10 @@ public:
// Destructor. // Destructor.
~session(); ~session();
static std::shared_ptr<session> create(const std::shared_ptr<proxy>& pr, const address& saddr, static ptr<session> create(const ptr<proxy>& pr, const address& saddr,
const address& daddr, const address& taddr); const address& daddr, const address& taddr);
void add_iface(const std::shared_ptr<iface>& ifa); void add_iface(const ptr<iface>& ifa);
const address& taddr() const; const address& taddr() const;