Refactor source
- Change coding style - Switch from own implementation of smart pointers to std::smart_ptr and std::weak_ptr
This commit is contained in:
parent
6fda405a59
commit
bc70f587ef
4
Makefile
4
Makefile
@ -1,7 +1,7 @@
|
||||
ifdef DEBUG
|
||||
CXXFLAGS ?= -g -DDEBUG
|
||||
CXXFLAGS ?= -g -std=c++0x -DDEBUG
|
||||
else
|
||||
CXXFLAGS ?= -O3
|
||||
CXXFLAGS ?= -O3 -std=c++0x
|
||||
endif
|
||||
|
||||
PREFIX ?= /usr/local
|
||||
|
@ -32,15 +32,7 @@ __NDPPD_NS_BEGIN
|
||||
|
||||
address::address()
|
||||
{
|
||||
_addr.s6_addr32[0] = 0;
|
||||
_addr.s6_addr32[1] = 0;
|
||||
_addr.s6_addr32[2] = 0;
|
||||
_addr.s6_addr32[3] = 0;
|
||||
|
||||
_mask.s6_addr32[0] = 0xffffffff;
|
||||
_mask.s6_addr32[1] = 0xffffffff;
|
||||
_mask.s6_addr32[2] = 0xffffffff;
|
||||
_mask.s6_addr32[3] = 0xffffffff;
|
||||
reset();
|
||||
}
|
||||
|
||||
address::address(const address& addr)
|
||||
@ -58,34 +50,12 @@ address::address(const address& addr)
|
||||
|
||||
address::address(const std::string& str)
|
||||
{
|
||||
if(!parse_string(str))
|
||||
{
|
||||
_addr.s6_addr32[0] = 0;
|
||||
_addr.s6_addr32[1] = 0;
|
||||
_addr.s6_addr32[2] = 0;
|
||||
_addr.s6_addr32[3] = 0;
|
||||
|
||||
_mask.s6_addr32[0] = 0xffffffff;
|
||||
_mask.s6_addr32[1] = 0xffffffff;
|
||||
_mask.s6_addr32[2] = 0xffffffff;
|
||||
_mask.s6_addr32[3] = 0xffffffff;
|
||||
}
|
||||
parse_string(str);
|
||||
}
|
||||
|
||||
address::address(const char *str)
|
||||
{
|
||||
if(!parse_string(str))
|
||||
{
|
||||
_addr.s6_addr32[0] = 0;
|
||||
_addr.s6_addr32[1] = 0;
|
||||
_addr.s6_addr32[2] = 0;
|
||||
_addr.s6_addr32[3] = 0;
|
||||
|
||||
_mask.s6_addr32[0] = 0xffffffff;
|
||||
_mask.s6_addr32[1] = 0xffffffff;
|
||||
_mask.s6_addr32[2] = 0xffffffff;
|
||||
_mask.s6_addr32[3] = 0xffffffff;
|
||||
}
|
||||
parse_string(str);
|
||||
}
|
||||
|
||||
address::address(const in6_addr& addr)
|
||||
@ -140,15 +110,27 @@ bool address::operator!=(const address& addr) const
|
||||
((_addr.s6_addr32[3] ^ addr._addr.s6_addr32[3]) & _mask.s6_addr32[3]));
|
||||
}
|
||||
|
||||
void address::reset()
|
||||
{
|
||||
_addr.s6_addr32[0] = 0;
|
||||
_addr.s6_addr32[1] = 0;
|
||||
_addr.s6_addr32[2] = 0;
|
||||
_addr.s6_addr32[3] = 0;
|
||||
|
||||
_mask.s6_addr32[0] = 0xffffffff;
|
||||
_mask.s6_addr32[1] = 0xffffffff;
|
||||
_mask.s6_addr32[2] = 0xffffffff;
|
||||
_mask.s6_addr32[3] = 0xffffffff;
|
||||
}
|
||||
|
||||
int address::prefix() const
|
||||
{
|
||||
if(!_mask.s6_addr[0])
|
||||
if (!_mask.s6_addr[0])
|
||||
return 0;
|
||||
|
||||
for(int p = 0; p < 128; p++)
|
||||
{
|
||||
for (int p = 0; p < 128; p++) {
|
||||
int byi = p / 8, bii = 7 - (p % 8);
|
||||
if(!(_mask.s6_addr[byi] & (1 << bii)))
|
||||
if (!(_mask.s6_addr[byi] & (1 << bii)))
|
||||
return p;
|
||||
}
|
||||
|
||||
@ -157,7 +139,7 @@ int address::prefix() const
|
||||
|
||||
void address::prefix(int pf)
|
||||
{
|
||||
if((pf < 0) || (pf > 128))
|
||||
if ((pf < 0) || (pf > 128))
|
||||
return;
|
||||
|
||||
_mask.s6_addr32[0] = 0;
|
||||
@ -165,8 +147,7 @@ void address::prefix(int pf)
|
||||
_mask.s6_addr32[2] = 0;
|
||||
_mask.s6_addr32[3] = 0;
|
||||
|
||||
while(pf--)
|
||||
{
|
||||
while (pf--) {
|
||||
int byi = pf / 8, bii = 7 - (pf % 8);
|
||||
_mask.s6_addr[byi] |= 1 << bii;
|
||||
}
|
||||
@ -176,14 +157,14 @@ const std::string address::to_string() const
|
||||
{
|
||||
char buf[INET6_ADDRSTRLEN + 8];
|
||||
|
||||
if(!inet_ntop(AF_INET6, &_addr, buf, INET6_ADDRSTRLEN))
|
||||
if (!inet_ntop(AF_INET6, &_addr, buf, INET6_ADDRSTRLEN))
|
||||
return "::1";
|
||||
|
||||
// TODO: What to do about invalid ip?
|
||||
|
||||
int p;
|
||||
|
||||
if((p = prefix()) < 128)
|
||||
if ((p = prefix()) < 128)
|
||||
sprintf(buf + strlen(buf), "/%d", p);
|
||||
|
||||
return buf;
|
||||
@ -197,20 +178,21 @@ bool address::parse_string(const std::string& str)
|
||||
sz = 0;
|
||||
b = buf;
|
||||
|
||||
reset();
|
||||
|
||||
const char *p = str.c_str();
|
||||
|
||||
while(*p && isspace(*p))
|
||||
while (*p && isspace(*p))
|
||||
p++;
|
||||
|
||||
while(*p)
|
||||
{
|
||||
if((*p == '/') || isspace(*p))
|
||||
while (*p) {
|
||||
if ((*p == '/') || isspace(*p))
|
||||
break;
|
||||
|
||||
if((*p != ':') && !isxdigit(*p))
|
||||
if ((*p != ':') && !isxdigit(*p))
|
||||
return false;
|
||||
|
||||
if(sz >= (INET6_ADDRSTRLEN - 1))
|
||||
if (sz >= (INET6_ADDRSTRLEN - 1))
|
||||
return false;
|
||||
|
||||
*b++ = *p++;
|
||||
@ -220,14 +202,13 @@ bool address::parse_string(const std::string& str)
|
||||
|
||||
*b = '\0';
|
||||
|
||||
if(inet_pton(AF_INET6, buf, &_addr) <= 0)
|
||||
if (inet_pton(AF_INET6, buf, &_addr) <= 0)
|
||||
return false;
|
||||
|
||||
while(*p && isspace(*p))
|
||||
while (*p && isspace(*p))
|
||||
p++;
|
||||
|
||||
if(*p == '\0')
|
||||
{
|
||||
if (*p == '\0') {
|
||||
_mask.s6_addr32[0] = 0xffffffff;
|
||||
_mask.s6_addr32[1] = 0xffffffff;
|
||||
_mask.s6_addr32[2] = 0xffffffff;
|
||||
@ -235,21 +216,20 @@ bool address::parse_string(const std::string& str)
|
||||
return true;
|
||||
}
|
||||
|
||||
if(*p++ != '/')
|
||||
if (*p++ != '/')
|
||||
return false;
|
||||
|
||||
while(*p && isspace(*p))
|
||||
while (*p && isspace(*p))
|
||||
p++;
|
||||
|
||||
sz = 0;
|
||||
b = buf;
|
||||
|
||||
while(*p)
|
||||
{
|
||||
if(!isdigit(*p))
|
||||
while (*p) {
|
||||
if (!isdigit(*p))
|
||||
return false;
|
||||
|
||||
if(sz > 3)
|
||||
if (sz > 3)
|
||||
return false;
|
||||
|
||||
*b++ = *p++;
|
||||
|
@ -51,6 +51,8 @@ public:
|
||||
|
||||
bool operator!=(const address& addr) const;
|
||||
|
||||
void reset();
|
||||
|
||||
const std::string to_string() const;
|
||||
|
||||
bool parse_string(const std::string& str);
|
||||
|
27
src/conf.cc
27
src/conf.cc
@ -26,7 +26,7 @@ void conf::error_printf(cfg_t *cfg, const char *fmt, va_list ap)
|
||||
{
|
||||
char buf[256];
|
||||
|
||||
if(vsnprintf(buf, sizeof(buf), fmt, ap) <= 0)
|
||||
if (vsnprintf(buf, sizeof(buf), fmt, ap) <= 0)
|
||||
return;
|
||||
|
||||
ERR("[Config] %s", buf);
|
||||
@ -38,7 +38,7 @@ int conf::validate_rule(cfg_t *cfg, cfg_opt_t *opt)
|
||||
|
||||
cfg_t *rule_cfg = cfg_opt_getnsec(opt, cfg_opt_size(opt) - 1);
|
||||
|
||||
if(!rule_cfg)
|
||||
if (!rule_cfg)
|
||||
return -1;
|
||||
|
||||
// TODO: Maybe we should validate IP here?
|
||||
@ -50,18 +50,16 @@ bool conf::setup(cfg_t *cfg)
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i = 0; i < cfg_size(cfg, "proxy"); i++)
|
||||
{
|
||||
for (i = 0; i < cfg_size(cfg, "proxy"); i++) {
|
||||
cfg_t *proxy_cfg = cfg_getnsec(cfg, "proxy", i);
|
||||
|
||||
if(proxy_cfg)
|
||||
{
|
||||
if (proxy_cfg) {
|
||||
cfg_t *rule_cfg;
|
||||
int i2;
|
||||
|
||||
strong_ptr<proxy> pr = proxy::open(cfg_title(proxy_cfg));
|
||||
std::shared_ptr<proxy> pr = proxy::open(cfg_title(proxy_cfg));
|
||||
|
||||
if(pr.is_null())
|
||||
if (!pr)
|
||||
continue;
|
||||
|
||||
pr->router(cfg_getbool(proxy_cfg, "router"));
|
||||
@ -70,20 +68,18 @@ bool conf::setup(cfg_t *cfg)
|
||||
|
||||
pr->timeout(cfg_getint(proxy_cfg, "timeout"));
|
||||
|
||||
for(i2 = 0; i2 < cfg_size(proxy_cfg, "rule"); i2++)
|
||||
{
|
||||
for (i2 = 0; i2 < cfg_size(proxy_cfg, "rule"); i2++) {
|
||||
cfg_t *rule_cfg;
|
||||
|
||||
if(!(rule_cfg = cfg_getnsec(proxy_cfg, "rule", i2)))
|
||||
if (!(rule_cfg = cfg_getnsec(proxy_cfg, "rule", i2)))
|
||||
continue;
|
||||
|
||||
address addr(cfg_title(rule_cfg));
|
||||
|
||||
std::string ifname(cfg_getstr(rule_cfg, "iface"));
|
||||
|
||||
if(ifname.empty())
|
||||
{
|
||||
if(addr.prefix() <= 120)
|
||||
if (ifname.empty()) {
|
||||
if (addr.prefix() <= 120)
|
||||
NCE("Static rule prefix /%d <= 120 - is this what you want?", addr.prefix());
|
||||
|
||||
pr->add_rule(addr);
|
||||
@ -134,8 +130,7 @@ bool conf::load(const std::string& path)
|
||||
|
||||
cfg_set_validate_func(cfg, "proxy|rule", &validate_rule);
|
||||
|
||||
switch(cfg_parse(cfg, path.c_str()))
|
||||
{
|
||||
switch (cfg_parse(cfg, path.c_str())) {
|
||||
case CFG_SUCCESS:
|
||||
break;
|
||||
|
||||
|
193
src/iface.cc
193
src/iface.cc
@ -42,7 +42,7 @@
|
||||
|
||||
__NDPPD_NS_BEGIN
|
||||
|
||||
std::map<std::string, strong_ptr<iface> > iface::_map;
|
||||
std::map<std::string, std::shared_ptr<iface> > iface::_map;
|
||||
|
||||
std::vector<struct pollfd> iface::_pollfds;
|
||||
|
||||
@ -55,46 +55,41 @@ iface::~iface()
|
||||
{
|
||||
DBG("iface::~iface()");
|
||||
|
||||
if(_ifd >= 0)
|
||||
if (_ifd >= 0)
|
||||
close(_ifd);
|
||||
|
||||
if(_pfd >= 0)
|
||||
{
|
||||
if (_pfd >= 0) {
|
||||
allmulti(_prev_allmulti);
|
||||
close(_pfd);
|
||||
}
|
||||
}
|
||||
|
||||
strong_ptr<iface> iface::open_pfd(const std::string& name)
|
||||
std::shared_ptr<iface> iface::open_pfd(const std::string& name)
|
||||
{
|
||||
int fd;
|
||||
|
||||
std::map<std::string, strong_ptr<iface> >::iterator it = _map.find(name);
|
||||
std::map<std::string, std::shared_ptr<iface> >::iterator it = _map.find(name);
|
||||
|
||||
strong_ptr<iface> ifa;
|
||||
std::shared_ptr<iface> ifa;
|
||||
|
||||
if(it != _map.end())
|
||||
{
|
||||
if(it->second->_pfd >= 0)
|
||||
if (it != _map.end()) {
|
||||
if (it->second->_pfd >= 0)
|
||||
return it->second;
|
||||
|
||||
ifa = it->second;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// We need an _ifs, so let's set one up.
|
||||
ifa = open_ifd(name);
|
||||
}
|
||||
|
||||
if(ifa.is_null())
|
||||
return strong_ptr<iface>();
|
||||
if (!ifa)
|
||||
return std::shared_ptr<iface>();
|
||||
|
||||
// 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) {
|
||||
ERR("Unable to create socket");
|
||||
return strong_ptr<iface>();
|
||||
return std::shared_ptr<iface>();
|
||||
}
|
||||
|
||||
// Bind to the specified interface.
|
||||
@ -105,29 +100,26 @@ strong_ptr<iface> iface::open_pfd(const std::string& name)
|
||||
lladdr.sll_family = AF_PACKET;
|
||||
lladdr.sll_protocol = htons(ETH_P_IPV6);
|
||||
|
||||
if(!(lladdr.sll_ifindex = if_nametoindex(name.c_str())))
|
||||
{
|
||||
if (!(lladdr.sll_ifindex = if_nametoindex(name.c_str()))) {
|
||||
close(fd);
|
||||
ERR("Failed to bind to interface '%s'", name.c_str());
|
||||
return strong_ptr<iface>();
|
||||
return std::shared_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);
|
||||
ERR("Failed to bind to interface '%s'", name.c_str());
|
||||
return strong_ptr<iface>();
|
||||
return std::shared_ptr<iface>();
|
||||
}
|
||||
|
||||
// Switch to non-blocking mode.
|
||||
|
||||
int on = 1;
|
||||
|
||||
if(ioctl(fd, FIONBIO, (char *)&on) < 0)
|
||||
{
|
||||
if (ioctl(fd, FIONBIO, (char *)&on) < 0) {
|
||||
close(fd);
|
||||
ERR("Failed to switch to non-blocking on interface '%s'", name.c_str());
|
||||
return strong_ptr<iface>();
|
||||
return std::shared_ptr<iface>();
|
||||
}
|
||||
|
||||
// Set up filter.
|
||||
@ -160,10 +152,9 @@ strong_ptr<iface> iface::open_pfd(const std::string& name)
|
||||
fprog.filter = (struct sock_filter *)filter;
|
||||
fprog.len = 8;
|
||||
|
||||
if(setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog)) < 0)
|
||||
{
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog)) < 0) {
|
||||
ERR("Failed to set filter");
|
||||
return strong_ptr<iface>();
|
||||
return std::shared_ptr<iface>();
|
||||
}
|
||||
|
||||
// Set up an instance of 'iface'.
|
||||
@ -175,21 +166,20 @@ strong_ptr<iface> iface::open_pfd(const std::string& name)
|
||||
return ifa;
|
||||
}
|
||||
|
||||
strong_ptr<iface> iface::open_ifd(const std::string& name)
|
||||
std::shared_ptr<iface> iface::open_ifd(const std::string& name)
|
||||
{
|
||||
int fd;
|
||||
|
||||
std::map<std::string, strong_ptr<iface> >::iterator it = _map.find(name);
|
||||
std::map<std::string, std::shared_ptr<iface> >::iterator it = _map.find(name);
|
||||
|
||||
if((it != _map.end()) && it->second->_ifd)
|
||||
if ((it != _map.end()) && it->second->_ifd)
|
||||
return it->second;
|
||||
|
||||
// Create a socket.
|
||||
|
||||
if((fd = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0)
|
||||
{
|
||||
if ((fd = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
|
||||
ERR("Unable to create socket");
|
||||
return strong_ptr<iface>();
|
||||
return std::shared_ptr<iface>();
|
||||
}
|
||||
|
||||
// Bind to the specified interface.
|
||||
@ -200,11 +190,10 @@ strong_ptr<iface> iface::open_ifd(const std::string& name)
|
||||
strncpy(ifr.ifr_name, name.c_str(), IFNAMSIZ - 1);
|
||||
ifr.ifr_name[IFNAMSIZ - 1] = '\0';
|
||||
|
||||
if(setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)) < 0)
|
||||
{
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)) < 0) {
|
||||
close(fd);
|
||||
ERR("Failed to bind to interface '%s'", name.c_str());
|
||||
return strong_ptr<iface>();
|
||||
return std::shared_ptr<iface>();
|
||||
}
|
||||
|
||||
// Detect the link-layer address.
|
||||
@ -213,11 +202,10 @@ strong_ptr<iface> iface::open_ifd(const std::string& name)
|
||||
strncpy(ifr.ifr_name, name.c_str(), IFNAMSIZ - 1);
|
||||
ifr.ifr_name[IFNAMSIZ - 1] = '\0';
|
||||
|
||||
if(ioctl(fd, SIOCGIFHWADDR, &ifr) < 0)
|
||||
{
|
||||
if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
|
||||
close(fd);
|
||||
ERR("Failed to detect link-layer address for interface '%s'", name.c_str());
|
||||
return strong_ptr<iface>();
|
||||
return std::shared_ptr<iface>();
|
||||
}
|
||||
|
||||
DBG("fd=%d, hwaddr=%s", fd, ether_ntoa((const struct ether_addr *)&ifr.ifr_hwaddr.sa_data));
|
||||
@ -226,29 +214,26 @@ strong_ptr<iface> iface::open_ifd(const std::string& name)
|
||||
|
||||
int hops = 255;
|
||||
|
||||
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);
|
||||
ERR("iface::open_ifd() failed IPV6_MULTICAST_HOPS");
|
||||
return strong_ptr<iface>();
|
||||
return std::shared_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);
|
||||
ERR("iface::open_ifd() failed IPV6_UNICAST_HOPS");
|
||||
return strong_ptr<iface>();
|
||||
return std::shared_ptr<iface>();
|
||||
}
|
||||
|
||||
// Switch to non-blocking mode.
|
||||
|
||||
int on = 1;
|
||||
|
||||
if(ioctl(fd, FIONBIO, (char *)&on) < 0)
|
||||
{
|
||||
if (ioctl(fd, FIONBIO, (char *)&on) < 0) {
|
||||
close(fd);
|
||||
ERR("Failed to switch to non-blocking on interface '%s'", name.c_str());
|
||||
return strong_ptr<iface>();
|
||||
return std::shared_ptr<iface>();
|
||||
}
|
||||
|
||||
// Set up filter.
|
||||
@ -257,27 +242,22 @@ strong_ptr<iface> iface::open_ifd(const std::string& name)
|
||||
ICMP6_FILTER_SETBLOCKALL(&filter);
|
||||
ICMP6_FILTER_SETPASS(ND_NEIGHBOR_ADVERT, &filter);
|
||||
|
||||
if(setsockopt(fd, IPPROTO_ICMPV6, ICMP6_FILTER, &filter, sizeof(filter)) < 0)
|
||||
{
|
||||
if (setsockopt(fd, IPPROTO_ICMPV6, ICMP6_FILTER, &filter, sizeof(filter)) < 0) {
|
||||
ERR("Failed to set filter");
|
||||
return strong_ptr<iface>();
|
||||
return std::shared_ptr<iface>();
|
||||
}
|
||||
|
||||
// Set up an instance of 'iface'.
|
||||
|
||||
strong_ptr<iface> ifa;
|
||||
|
||||
if(it == _map.end())
|
||||
{
|
||||
ifa = new iface();
|
||||
std::shared_ptr<iface> ifa;
|
||||
|
||||
if (it == _map.end()) {
|
||||
ifa.reset(new iface());
|
||||
ifa->_name = name;
|
||||
ifa->_ptr = ifa;
|
||||
|
||||
_map[name] = ifa;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
ifa = it->second;
|
||||
}
|
||||
|
||||
@ -297,7 +277,7 @@ ssize_t iface::read(int fd, struct sockaddr *saddr, uint8_t *msg, size_t size)
|
||||
char cbuf[256];
|
||||
int len;
|
||||
|
||||
if(!msg || (size < 0))
|
||||
if (!msg || (size < 0))
|
||||
return -1;
|
||||
|
||||
iov.iov_len = size;
|
||||
@ -309,10 +289,10 @@ ssize_t iface::read(int fd, struct sockaddr *saddr, uint8_t *msg, size_t size)
|
||||
mhdr.msg_iov = &iov;
|
||||
mhdr.msg_iovlen = 1;
|
||||
|
||||
if((len = recvmsg(fd, &mhdr, 0)) < 0)
|
||||
if ((len = recvmsg(fd, &mhdr, 0)) < 0)
|
||||
return -1;
|
||||
|
||||
if(len < sizeof(struct icmp6_hdr))
|
||||
if (len < sizeof(struct icmp6_hdr))
|
||||
return -1;
|
||||
|
||||
DBG("iface::read() len=%d", len);
|
||||
@ -344,7 +324,7 @@ ssize_t iface::write(int fd, const address& daddr, const uint8_t *msg, size_t si
|
||||
|
||||
int len;
|
||||
|
||||
if((len = sendmsg(fd, &mhdr, 0)) < 0)
|
||||
if ((len = sendmsg(fd, &mhdr, 0)) < 0)
|
||||
return -1;
|
||||
|
||||
return len;
|
||||
@ -356,7 +336,7 @@ ssize_t iface::read_solicit(address& saddr, address& daddr, address& taddr)
|
||||
uint8_t msg[256];
|
||||
ssize_t len;
|
||||
|
||||
if((len = read(_pfd, (struct sockaddr *)&t_saddr, msg, sizeof(msg))) < 0)
|
||||
if ((len = read(_pfd, (struct sockaddr *)&t_saddr, msg, sizeof(msg))) < 0)
|
||||
return -1;
|
||||
|
||||
struct ip6_hdr *ip6h =
|
||||
@ -452,12 +432,12 @@ ssize_t iface::read_advert(address& saddr, address& taddr)
|
||||
uint8_t msg[256];
|
||||
ssize_t len;
|
||||
|
||||
if((len = read(_ifd, (struct sockaddr *)&t_saddr, msg, sizeof(msg))) < 0)
|
||||
if ((len = read(_ifd, (struct sockaddr *)&t_saddr, msg, sizeof(msg))) < 0)
|
||||
return -1;
|
||||
|
||||
saddr = t_saddr.sin6_addr;
|
||||
|
||||
if(((struct icmp6_hdr *)msg)->icmp6_type != ND_NEIGHBOR_ADVERT)
|
||||
if (((struct icmp6_hdr *)msg)->icmp6_type != ND_NEIGHBOR_ADVERT)
|
||||
return -1;
|
||||
|
||||
taddr = ((struct nd_neighbor_solicit *)msg)->nd_ns_target;
|
||||
@ -476,9 +456,8 @@ void iface::fixup_pollfds()
|
||||
|
||||
DBG("iface::fixup_pollfds() _map.size()=%d", _map.size());
|
||||
|
||||
for(std::map<std::string, strong_ptr<iface> >::iterator it = _map.begin();
|
||||
it != _map.end(); it++)
|
||||
{
|
||||
for (std::map<std::string, std::shared_ptr<iface> >::iterator it = _map.begin();
|
||||
it != _map.end(); it++) {
|
||||
_pollfds[i].fd = it->second->_ifd;
|
||||
_pollfds[i].events = POLLIN;
|
||||
_pollfds[i].revents = 0;
|
||||
@ -491,20 +470,25 @@ void iface::fixup_pollfds()
|
||||
}
|
||||
}
|
||||
|
||||
void iface::remove_session(const strong_ptr<session>& se)
|
||||
void iface::remove_session(const std::shared_ptr<session>& se)
|
||||
{
|
||||
_sessions.remove(se);
|
||||
for (std::list<std::weak_ptr<session> >::iterator it = _sessions.begin();
|
||||
it != _sessions.end(); it++) {
|
||||
if (it->lock() == se) {
|
||||
_sessions.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void iface::add_session(const strong_ptr<session>& se)
|
||||
void iface::add_session(const std::shared_ptr<session>& se)
|
||||
{
|
||||
_sessions.push_back(se);
|
||||
}
|
||||
|
||||
int iface::poll_all()
|
||||
{
|
||||
if(_pollfds.size() == 0)
|
||||
{
|
||||
if (_pollfds.size() == 0) {
|
||||
::sleep(1);
|
||||
return 0;
|
||||
}
|
||||
@ -513,60 +497,53 @@ int iface::poll_all()
|
||||
|
||||
int len;
|
||||
|
||||
if((len = ::poll(&_pollfds[0], _pollfds.size(), 50)) < 0)
|
||||
if ((len = ::poll(&_pollfds[0], _pollfds.size(), 50)) < 0)
|
||||
return -1;
|
||||
|
||||
if(len == 0)
|
||||
if (len == 0)
|
||||
return 0;
|
||||
|
||||
std::map<std::string, strong_ptr<iface> >::iterator i_it = _map.begin();
|
||||
std::map<std::string, std::shared_ptr<iface> >::iterator i_it = _map.begin();
|
||||
|
||||
int i = 0;
|
||||
|
||||
for(std::vector<struct pollfd>::iterator f_it = _pollfds.begin();
|
||||
f_it != _pollfds.end(); f_it++)
|
||||
{
|
||||
for (std::vector<struct pollfd>::iterator f_it = _pollfds.begin();
|
||||
f_it != _pollfds.end(); f_it++) {
|
||||
assert(i_it != _map.end());
|
||||
|
||||
if(i && !(i % 2))
|
||||
if (i && !(i % 2))
|
||||
i_it++;
|
||||
|
||||
bool is_pfd = i++ % 2;
|
||||
|
||||
if(!(f_it->revents & POLLIN))
|
||||
if (!(f_it->revents & POLLIN))
|
||||
continue;
|
||||
|
||||
strong_ptr<iface> ifa = i_it->second;
|
||||
std::shared_ptr<iface> ifa = i_it->second;
|
||||
|
||||
address saddr, daddr, taddr;
|
||||
|
||||
if(is_pfd)
|
||||
{
|
||||
if(ifa->read_solicit(saddr, daddr, taddr) < 0)
|
||||
{
|
||||
if (is_pfd) {
|
||||
if (ifa->read_solicit(saddr, daddr, taddr) < 0) {
|
||||
ERR("Failed to read from interface '%s'", ifa->_name.c_str());
|
||||
continue;
|
||||
}
|
||||
|
||||
if(!saddr.is_unicast() || !daddr.is_multicast())
|
||||
if (!saddr.is_unicast() || !daddr.is_multicast())
|
||||
continue;
|
||||
|
||||
ifa->_pr->handle_solicit(saddr, daddr, taddr);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(ifa->read_advert(saddr, taddr) < 0)
|
||||
{
|
||||
} else {
|
||||
if (ifa->read_advert(saddr, taddr) < 0) {
|
||||
ERR("Failed to read from interface '%s'", ifa->_name.c_str());
|
||||
continue;
|
||||
}
|
||||
|
||||
for(std::list<weak_ptr<session> >::iterator s_it = ifa->_sessions.begin();
|
||||
s_it != ifa->_sessions.end(); s_it++)
|
||||
{
|
||||
if(((*s_it)->taddr() == taddr) && ((*s_it)->status() == session::WAITING))
|
||||
{
|
||||
(*s_it)->handle_advert();
|
||||
for (std::list<std::weak_ptr<session> >::iterator s_it = ifa->_sessions.begin();
|
||||
s_it != ifa->_sessions.end(); s_it++) {
|
||||
const std::shared_ptr<session> sess = s_it->lock();
|
||||
if ((sess->taddr() == taddr) && (sess->status() == session::WAITING)) {
|
||||
sess->handle_advert();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -587,20 +564,20 @@ int iface::allmulti(int state)
|
||||
|
||||
strncpy(ifr.ifr_name, _name.c_str(), IFNAMSIZ);
|
||||
|
||||
if(ioctl(_pfd, SIOCGIFFLAGS, &ifr) < 0)
|
||||
if (ioctl(_pfd, SIOCGIFFLAGS, &ifr) < 0)
|
||||
return -1;
|
||||
|
||||
int old_state = !!(ifr.ifr_flags & IFF_ALLMULTI);
|
||||
|
||||
if(state == old_state)
|
||||
if (state == old_state)
|
||||
return old_state;
|
||||
|
||||
if(state)
|
||||
if (state)
|
||||
ifr.ifr_flags |= IFF_ALLMULTI;
|
||||
else
|
||||
ifr.ifr_flags &= ~IFF_ALLMULTI;
|
||||
|
||||
if(ioctl(_pfd, SIOCSIFFLAGS, &ifr) < 0)
|
||||
if (ioctl(_pfd, SIOCSIFFLAGS, &ifr) < 0)
|
||||
return -1;
|
||||
|
||||
return old_state;
|
||||
@ -611,12 +588,12 @@ const std::string& iface::name() const
|
||||
return _name;
|
||||
}
|
||||
|
||||
void iface::pr(const strong_ptr<proxy>& pr)
|
||||
void iface::pr(const std::shared_ptr<proxy>& pr)
|
||||
{
|
||||
_pr = pr;
|
||||
}
|
||||
|
||||
const strong_ptr<proxy>& iface::pr() const
|
||||
const std::shared_ptr<proxy>& iface::pr() const
|
||||
{
|
||||
return _pr;
|
||||
}
|
||||
|
20
src/iface.h
20
src/iface.h
@ -35,9 +35,9 @@ class iface
|
||||
{
|
||||
private:
|
||||
// Weak pointer so this object can reference itself.
|
||||
weak_ptr<iface> _ptr;
|
||||
std::weak_ptr<iface> _ptr;
|
||||
|
||||
static std::map<std::string, strong_ptr<iface> > _map;
|
||||
static std::map<std::string, std::shared_ptr<iface> > _map;
|
||||
|
||||
// An array of objects used with ::poll.
|
||||
static std::vector<struct pollfd> _pollfds;
|
||||
@ -61,9 +61,9 @@ private:
|
||||
|
||||
// An array of sessions that are monitoring this interface for
|
||||
// ND_NEIGHBOR_ADVERT messages.
|
||||
std::list<weak_ptr<session> > _sessions;
|
||||
std::list<std::weak_ptr<session> > _sessions;
|
||||
|
||||
strong_ptr<proxy> _pr;
|
||||
std::shared_ptr<proxy> _pr;
|
||||
|
||||
// The link-layer address of this interface.
|
||||
struct ether_addr hwaddr;
|
||||
@ -80,9 +80,9 @@ public:
|
||||
// Destructor.
|
||||
~iface();
|
||||
|
||||
static strong_ptr<iface> open_ifd(const std::string& name);
|
||||
static std::shared_ptr<iface> open_ifd(const std::string& name);
|
||||
|
||||
static strong_ptr<iface> open_pfd(const std::string& name);
|
||||
static std::shared_ptr<iface> open_pfd(const std::string& name);
|
||||
|
||||
static int poll_all();
|
||||
|
||||
@ -106,13 +106,13 @@ public:
|
||||
const std::string& name() const;
|
||||
|
||||
// Adds a session to be monitored for ND_NEIGHBOR_ADVERT messages.
|
||||
void add_session(const strong_ptr<session>& se);
|
||||
void add_session(const std::shared_ptr<session>& se);
|
||||
|
||||
void remove_session(const strong_ptr<session>& se);
|
||||
void remove_session(const std::shared_ptr<session>& se);
|
||||
|
||||
void pr(const strong_ptr<proxy>& pr);
|
||||
void pr(const std::shared_ptr<proxy>& pr);
|
||||
|
||||
const strong_ptr<proxy>& pr() const;
|
||||
const std::shared_ptr<proxy>& pr() const;
|
||||
};
|
||||
|
||||
__NDPPD_NS_END
|
||||
|
12
src/log.cc
12
src/log.cc
@ -41,12 +41,12 @@ void log::puts(int level, const char *str)
|
||||
{
|
||||
const char *ls;
|
||||
|
||||
if((level < 0) || (level > LOG_DEBUG))
|
||||
if ((level < 0) || (level > LOG_DEBUG))
|
||||
ls = "unknown";
|
||||
else
|
||||
ls = _level_str[level];
|
||||
|
||||
if(_syslog)
|
||||
if (_syslog)
|
||||
::syslog(level, "(%s) %s", ls, str);
|
||||
else
|
||||
fprintf(stderr, "(% 8s) %s\n", ls, str);
|
||||
@ -60,8 +60,7 @@ void log::printf(int level, const char *fmt, ...)
|
||||
|
||||
va_start(args, fmt);
|
||||
|
||||
if(vsnprintf(buf, sizeof(buf), fmt, args) > 0)
|
||||
{
|
||||
if (vsnprintf(buf, sizeof(buf), fmt, args) > 0) {
|
||||
puts(level, buf);
|
||||
}
|
||||
|
||||
@ -70,11 +69,10 @@ void log::printf(int level, const char *fmt, ...)
|
||||
|
||||
void log::syslog(bool sl)
|
||||
{
|
||||
if(sl == _syslog)
|
||||
if (sl == _syslog)
|
||||
return;
|
||||
|
||||
if(_syslog = sl)
|
||||
{
|
||||
if (_syslog = sl) {
|
||||
#ifdef DEBUG
|
||||
setlogmask(LOG_UPTO(LOG_DEBUG));
|
||||
openlog("ndppd", LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER);
|
||||
|
28
src/ndppd.cc
28
src/ndppd.cc
@ -31,15 +31,15 @@ int daemonize()
|
||||
{
|
||||
pid_t pid = fork();
|
||||
|
||||
if(pid < 0)
|
||||
if (pid < 0)
|
||||
return -1;
|
||||
|
||||
if(pid > 0)
|
||||
if (pid > 0)
|
||||
exit(0);
|
||||
|
||||
pid_t sid = setsid();
|
||||
|
||||
if(sid < 0)
|
||||
if (sid < 0)
|
||||
return -1;
|
||||
|
||||
close(STDIN_FILENO);
|
||||
@ -55,8 +55,7 @@ int main(int argc, char *argv[], char *env[])
|
||||
std::string pidfile;
|
||||
bool daemon = false;
|
||||
|
||||
while(1)
|
||||
{
|
||||
while (1) {
|
||||
int c, opt;
|
||||
|
||||
static struct option long_options[] =
|
||||
@ -68,11 +67,10 @@ int main(int argc, char *argv[], char *env[])
|
||||
|
||||
c = getopt_long(argc, argv, "c:dp:", long_options, &opt);
|
||||
|
||||
if(c == -1)
|
||||
if (c == -1)
|
||||
break;
|
||||
|
||||
switch(c)
|
||||
{
|
||||
switch (c) {
|
||||
case 'c':
|
||||
config_path = optarg;
|
||||
break;
|
||||
@ -87,19 +85,16 @@ int main(int argc, char *argv[], char *env[])
|
||||
}
|
||||
}
|
||||
|
||||
if(daemon)
|
||||
{
|
||||
if (daemon) {
|
||||
log::syslog(true);
|
||||
|
||||
if(daemonize() < 0)
|
||||
{
|
||||
if (daemonize() < 0) {
|
||||
ERR("Failed to daemonize process");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if(!pidfile.empty())
|
||||
{
|
||||
if (!pidfile.empty()) {
|
||||
std::ofstream pf;
|
||||
pf.open(pidfile.c_str(), std::ios::out | std::ios::trunc);
|
||||
pf << getpid() << std::endl;
|
||||
@ -110,15 +105,14 @@ int main(int argc, char *argv[], char *env[])
|
||||
|
||||
NFO("Using configuration file '%s'", config_path.c_str());
|
||||
|
||||
if(!conf::load(config_path))
|
||||
if (!conf::load(config_path))
|
||||
return -1;
|
||||
|
||||
struct timeval t1, t2;
|
||||
|
||||
gettimeofday(&t1, 0);
|
||||
|
||||
while(iface::poll_all() >= 0)
|
||||
{
|
||||
while (iface::poll_all() >= 0) {
|
||||
int elapsed_time;
|
||||
gettimeofday(&t2, 0);
|
||||
|
||||
|
@ -17,6 +17,7 @@
|
||||
#define __NDPPD_H
|
||||
|
||||
#include <netinet/ip6.h>
|
||||
#include <memory>
|
||||
|
||||
#define __NDPPD_NS_BEGIN namespace ndppd {
|
||||
#define __NDPPD_NS_END }
|
||||
@ -26,7 +27,6 @@
|
||||
#include <assert.h>
|
||||
|
||||
#include "log.h"
|
||||
#include "ptr.h"
|
||||
#include "conf.h"
|
||||
#include "address.h"
|
||||
|
||||
|
58
src/proxy.cc
58
src/proxy.cc
@ -31,9 +31,9 @@ proxy::proxy() :
|
||||
{
|
||||
}
|
||||
|
||||
strong_ptr<proxy> proxy::create(const strong_ptr<iface>& ifa)
|
||||
std::shared_ptr<proxy> proxy::create(const std::shared_ptr<iface>& ifa)
|
||||
{
|
||||
strong_ptr<proxy> pr(new proxy());
|
||||
std::shared_ptr<proxy> pr(new proxy());
|
||||
pr->_ptr = pr;
|
||||
pr->_ifa = ifa;
|
||||
|
||||
@ -44,12 +44,12 @@ strong_ptr<proxy> proxy::create(const strong_ptr<iface>& ifa)
|
||||
return pr;
|
||||
}
|
||||
|
||||
strong_ptr<proxy> proxy::open(const std::string& ifname)
|
||||
std::shared_ptr<proxy> proxy::open(const std::string& ifname)
|
||||
{
|
||||
strong_ptr<iface> ifa = iface::open_pfd(ifname);
|
||||
std::shared_ptr<iface> ifa = iface::open_pfd(ifname);
|
||||
|
||||
if(ifa.is_null())
|
||||
return strong_ptr<proxy>();
|
||||
if (!ifa)
|
||||
return std::shared_ptr<proxy>();
|
||||
|
||||
return create(ifa);
|
||||
}
|
||||
@ -63,13 +63,11 @@ void proxy::handle_solicit(const address& saddr, const address& daddr,
|
||||
// Let's check this proxy's list of sessions to see if we can
|
||||
// find one with the same target address.
|
||||
|
||||
for(std::list<strong_ptr<session> >::iterator sit = _sessions.begin();
|
||||
sit != _sessions.end(); sit++)
|
||||
{
|
||||
if((*sit)->taddr() == taddr)
|
||||
{
|
||||
switch((*sit)->status())
|
||||
{
|
||||
for (std::list<std::shared_ptr<session> >::iterator sit = _sessions.begin();
|
||||
sit != _sessions.end(); sit++) {
|
||||
|
||||
if ((*sit)->taddr() == taddr) {
|
||||
switch ((*sit)->status()) {
|
||||
case session::WAITING:
|
||||
case session::INVALID:
|
||||
break;
|
||||
@ -85,23 +83,20 @@ void proxy::handle_solicit(const address& saddr, const address& daddr,
|
||||
// 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.
|
||||
|
||||
strong_ptr<session> se;
|
||||
std::shared_ptr<session> se;
|
||||
|
||||
for(std::list<strong_ptr<rule> >::iterator it = _rules.begin();
|
||||
it != _rules.end(); it++)
|
||||
{
|
||||
strong_ptr<rule> ru = *it;
|
||||
for (std::list<std::shared_ptr<rule> >::iterator it = _rules.begin();
|
||||
it != _rules.end(); it++) {
|
||||
std::shared_ptr<rule> ru = *it;
|
||||
|
||||
DBG("comparing %s against %s",
|
||||
ru->addr().to_string().c_str(), taddr.to_string().c_str());
|
||||
|
||||
if(ru->addr() == taddr)
|
||||
{
|
||||
if(se.is_null())
|
||||
se = session::create(_ptr, saddr, daddr, taddr);
|
||||
if (ru->addr() == taddr) {
|
||||
if (!se)
|
||||
se = session::create(_ptr.lock(), saddr, daddr, taddr);
|
||||
|
||||
if(ru->ifa().is_null())
|
||||
{
|
||||
if (!ru->ifa()) {
|
||||
// This rule doesn't have an interface, and thus we'll consider
|
||||
// it "static" and immediately send the response.
|
||||
|
||||
@ -113,33 +108,32 @@ void proxy::handle_solicit(const address& saddr, const address& daddr,
|
||||
}
|
||||
}
|
||||
|
||||
if(se)
|
||||
{
|
||||
if (se) {
|
||||
_sessions.push_back(se);
|
||||
se->send_solicit();
|
||||
}
|
||||
}
|
||||
|
||||
strong_ptr<rule> proxy::add_rule(const address& addr, const strong_ptr<iface>& ifa)
|
||||
std::shared_ptr<rule> proxy::add_rule(const address& addr, const std::shared_ptr<iface>& ifa)
|
||||
{
|
||||
strong_ptr<rule> ru(rule::create(_ptr, addr, ifa));
|
||||
std::shared_ptr<rule> ru(rule::create(_ptr.lock(), addr, ifa));
|
||||
_rules.push_back(ru);
|
||||
return ru;
|
||||
}
|
||||
|
||||
strong_ptr<rule> proxy::add_rule(const address& addr)
|
||||
std::shared_ptr<rule> proxy::add_rule(const address& addr)
|
||||
{
|
||||
strong_ptr<rule> ru(rule::create(_ptr, addr));
|
||||
std::shared_ptr<rule> ru(rule::create(_ptr.lock(), addr));
|
||||
_rules.push_back(ru);
|
||||
return ru;
|
||||
}
|
||||
|
||||
void proxy::remove_session(const strong_ptr<session>& se)
|
||||
void proxy::remove_session(const std::shared_ptr<session>& se)
|
||||
{
|
||||
_sessions.remove(se);
|
||||
}
|
||||
|
||||
const strong_ptr<iface>& proxy::ifa() const
|
||||
const std::shared_ptr<iface>& proxy::ifa() const
|
||||
{
|
||||
return _ifa;
|
||||
}
|
||||
|
20
src/proxy.h
20
src/proxy.h
@ -32,13 +32,13 @@ class rule;
|
||||
class proxy
|
||||
{
|
||||
private:
|
||||
weak_ptr<proxy> _ptr;
|
||||
std::weak_ptr<proxy> _ptr;
|
||||
|
||||
strong_ptr<iface> _ifa;
|
||||
std::shared_ptr<iface> _ifa;
|
||||
|
||||
std::list<strong_ptr<rule> > _rules;
|
||||
std::list<std::shared_ptr<rule> > _rules;
|
||||
|
||||
std::list<strong_ptr<session> > _sessions;
|
||||
std::list<std::shared_ptr<session> > _sessions;
|
||||
|
||||
bool _router;
|
||||
|
||||
@ -47,20 +47,20 @@ private:
|
||||
proxy();
|
||||
|
||||
public:
|
||||
static strong_ptr<proxy> create(const strong_ptr<iface>& ifa);
|
||||
static std::shared_ptr<proxy> create(const std::shared_ptr<iface>& ifa);
|
||||
|
||||
static strong_ptr<proxy> open(const std::string& ifn);
|
||||
static std::shared_ptr<proxy> open(const std::string& ifn);
|
||||
|
||||
void handle_solicit(const address& saddr, const address& daddr,
|
||||
const address& taddr);
|
||||
|
||||
void remove_session(const strong_ptr<session>& se);
|
||||
void remove_session(const std::shared_ptr<session>& se);
|
||||
|
||||
strong_ptr<rule> add_rule(const address& addr, const strong_ptr<iface>& ifa);
|
||||
std::shared_ptr<rule> add_rule(const address& addr, const std::shared_ptr<iface>& ifa);
|
||||
|
||||
strong_ptr<rule> add_rule(const address& addr);
|
||||
std::shared_ptr<rule> add_rule(const address& addr);
|
||||
|
||||
const strong_ptr<iface>& ifa() const;
|
||||
const std::shared_ptr<iface>& ifa() const;
|
||||
|
||||
bool router() const;
|
||||
|
||||
|
243
src/ptr.h
243
src/ptr.h
@ -1,243 +0,0 @@
|
||||
// ndppd - NDP Proxy Daemon
|
||||
// Copyright (C) 2011 Daniel Adolfsson <daniel@priv.nu>
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#ifndef __NDPPD_PTR_H
|
||||
#define __NDPPD_PTR_H
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
__NDPPD_NS_BEGIN
|
||||
|
||||
// This template class simplifies the usage of pointers. It's basically
|
||||
// a reference-counting smart pointer that supports both weak and
|
||||
// strong references.
|
||||
|
||||
template <typename T>
|
||||
class ptr
|
||||
{
|
||||
protected:
|
||||
struct ref
|
||||
{
|
||||
public:
|
||||
T* p;
|
||||
int n_strong;
|
||||
int n_weak;
|
||||
};
|
||||
|
||||
ref *_ref;
|
||||
|
||||
bool _strong;
|
||||
|
||||
void acquire(T* p)
|
||||
{
|
||||
if(_ref)
|
||||
release();
|
||||
|
||||
if(p)
|
||||
{
|
||||
_ref = new ref;
|
||||
_ref->p = p;
|
||||
_ref->n_strong = _strong ? 1 : 0;
|
||||
_ref->n_weak = _strong ? 0 : 1;
|
||||
}
|
||||
}
|
||||
|
||||
void acquire(const ptr<T>& p)
|
||||
{
|
||||
if(_ref)
|
||||
release();
|
||||
|
||||
if(p._ref && p._ref->n_strong)
|
||||
{
|
||||
_ref = p._ref;
|
||||
|
||||
if(_strong)
|
||||
_ref->n_strong++;
|
||||
else
|
||||
_ref->n_weak++;
|
||||
}
|
||||
}
|
||||
|
||||
void release()
|
||||
{
|
||||
if(!_ref)
|
||||
return;
|
||||
|
||||
if(_strong)
|
||||
{
|
||||
// Assert _ref->n_strong > 0.
|
||||
|
||||
if(_ref->n_strong == 1)
|
||||
{
|
||||
delete _ref->p;
|
||||
_ref->p = 0;
|
||||
}
|
||||
|
||||
_ref->n_strong--;
|
||||
}
|
||||
else
|
||||
{
|
||||
_ref->n_weak--;
|
||||
}
|
||||
|
||||
if(!_ref->n_weak && !_ref->n_strong)
|
||||
delete _ref;
|
||||
|
||||
_ref = 0;
|
||||
}
|
||||
|
||||
ptr(bool strong) :
|
||||
_strong(strong), _ref(0)
|
||||
{
|
||||
}
|
||||
|
||||
ptr(bool strong, T* p) :
|
||||
_strong(strong), _ref(0)
|
||||
{
|
||||
if(p)
|
||||
acquire(p);
|
||||
}
|
||||
|
||||
ptr(bool strong, const ptr<T>& p) :
|
||||
_strong(strong), _ref(0)
|
||||
{
|
||||
acquire(p);
|
||||
}
|
||||
|
||||
virtual ~ptr()
|
||||
{
|
||||
if(_ref)
|
||||
release();
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
void operator=(T* p)
|
||||
{
|
||||
acquire(p);
|
||||
}
|
||||
|
||||
void operator=(const ptr<T>& p)
|
||||
{
|
||||
acquire(p);
|
||||
}
|
||||
|
||||
bool operator==(const ptr<T>& other) const
|
||||
{
|
||||
return other._ref == _ref;
|
||||
}
|
||||
|
||||
bool operator!=(const ptr<T>& other) const
|
||||
{
|
||||
return other._ref != _ref;
|
||||
}
|
||||
|
||||
bool is_null() const
|
||||
{
|
||||
return !((_ref != 0) && (_ref->p != 0));
|
||||
}
|
||||
|
||||
T& operator*() const
|
||||
{
|
||||
return *_ref.p;
|
||||
}
|
||||
|
||||
T* operator->() const
|
||||
{
|
||||
return _ref ? _ref->p : 0;
|
||||
}
|
||||
|
||||
operator T*() const
|
||||
{
|
||||
return _ref->p;
|
||||
}
|
||||
|
||||
operator bool() const
|
||||
{
|
||||
return !is_null();
|
||||
}
|
||||
|
||||
bool is_strong() const
|
||||
{
|
||||
return _strong;
|
||||
}
|
||||
|
||||
bool is_weak() const
|
||||
{
|
||||
return !_strong;
|
||||
}
|
||||
|
||||
void reset(T *p = 0)
|
||||
{
|
||||
acquire(p);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class weak_ptr;
|
||||
|
||||
template <typename T>
|
||||
class strong_ptr : public ptr<T>
|
||||
{
|
||||
public:
|
||||
strong_ptr() : ptr<T>(true)
|
||||
{
|
||||
}
|
||||
|
||||
strong_ptr(T* p) : ptr<T>(true, p)
|
||||
{
|
||||
}
|
||||
|
||||
strong_ptr(const ptr<T>& p) : ptr<T>(true, p)
|
||||
{
|
||||
}
|
||||
|
||||
strong_ptr(const strong_ptr<T>& p) : ptr<T>(true, p)
|
||||
{
|
||||
}
|
||||
|
||||
strong_ptr(const weak_ptr<T>& p) : ptr<T>(true, p)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class weak_ptr : public ptr<T>
|
||||
{
|
||||
public:
|
||||
weak_ptr() : ptr<T>(false)
|
||||
{
|
||||
}
|
||||
|
||||
weak_ptr(T* p) : ptr<T>(false, p)
|
||||
{
|
||||
}
|
||||
|
||||
weak_ptr(const ptr<T>& p) : ptr<T>(false, p)
|
||||
{
|
||||
}
|
||||
|
||||
weak_ptr(const strong_ptr<T>& p) : ptr<T>(false, p)
|
||||
{
|
||||
}
|
||||
|
||||
weak_ptr(const weak_ptr<T>& p) : ptr<T>(false, p)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
__NDPPD_NS_END
|
||||
|
||||
#endif // __NDPPD_PTR_H
|
10
src/rule.cc
10
src/rule.cc
@ -28,9 +28,9 @@ rule::rule()
|
||||
{
|
||||
}
|
||||
|
||||
strong_ptr<rule> rule::create(const strong_ptr<proxy>& pr, const address& addr, const strong_ptr<iface>& ifa)
|
||||
std::shared_ptr<rule> rule::create(const std::shared_ptr<proxy>& pr, const address& addr, const std::shared_ptr<iface>& ifa)
|
||||
{
|
||||
strong_ptr<rule> ru(new rule());
|
||||
std::shared_ptr<rule> ru(new rule());
|
||||
ru->_ptr = ru;
|
||||
ru->_pr = pr;
|
||||
ru->_ifa = ifa;
|
||||
@ -42,9 +42,9 @@ strong_ptr<rule> rule::create(const strong_ptr<proxy>& pr, const address& addr,
|
||||
return ru;
|
||||
}
|
||||
|
||||
strong_ptr<rule> rule::create(const strong_ptr<proxy>& pr, const address& addr)
|
||||
std::shared_ptr<rule> rule::create(const std::shared_ptr<proxy>& pr, const address& addr)
|
||||
{
|
||||
strong_ptr<rule> ru(new rule());
|
||||
std::shared_ptr<rule> ru(new rule());
|
||||
ru->_ptr = ru;
|
||||
ru->_pr = pr;
|
||||
ru->_addr = addr;
|
||||
@ -60,7 +60,7 @@ const address& rule::addr() const
|
||||
return _addr;
|
||||
}
|
||||
|
||||
strong_ptr<iface> rule::ifa() const
|
||||
std::shared_ptr<iface> rule::ifa() const
|
||||
{
|
||||
return _ifa;
|
||||
}
|
||||
|
12
src/rule.h
12
src/rule.h
@ -32,24 +32,24 @@ class proxy;
|
||||
class rule
|
||||
{
|
||||
private:
|
||||
weak_ptr<rule> _ptr;
|
||||
std::weak_ptr<rule> _ptr;
|
||||
|
||||
strong_ptr<proxy> _pr;
|
||||
std::shared_ptr<proxy> _pr;
|
||||
|
||||
strong_ptr<iface> _ifa;
|
||||
std::shared_ptr<iface> _ifa;
|
||||
|
||||
address _addr;
|
||||
|
||||
rule();
|
||||
|
||||
public:
|
||||
static strong_ptr<rule> create(const strong_ptr<proxy>& pr, const address& addr, const strong_ptr<iface>& ifa);
|
||||
static std::shared_ptr<rule> create(const std::shared_ptr<proxy>& pr, const address& addr, const std::shared_ptr<iface>& ifa);
|
||||
|
||||
static strong_ptr<rule> create(const strong_ptr<proxy>& pr, const address& addr);
|
||||
static std::shared_ptr<rule> create(const std::shared_ptr<proxy>& pr, const address& addr);
|
||||
|
||||
const address& addr() const;
|
||||
|
||||
strong_ptr<iface> ifa() const;
|
||||
std::shared_ptr<iface> ifa() const;
|
||||
|
||||
bool is_static() const;
|
||||
|
||||
|
@ -22,20 +22,18 @@
|
||||
|
||||
__NDPPD_NS_BEGIN
|
||||
|
||||
std::list<weak_ptr<session> > session::_sessions;
|
||||
std::list<std::weak_ptr<session> > session::_sessions;
|
||||
|
||||
void session::update_all(int elapsed_time)
|
||||
{
|
||||
for(std::list<weak_ptr<session> >::iterator it = _sessions.begin();
|
||||
it != _sessions.end(); )
|
||||
{
|
||||
strong_ptr<session> se = *it++;
|
||||
for (std::list<std::weak_ptr<session> >::iterator it = _sessions.begin();
|
||||
it != _sessions.end(); ) {
|
||||
std::shared_ptr<session> se = (*it++).lock();
|
||||
|
||||
if((se->_ttl -= elapsed_time) >= 0)
|
||||
if ((se->_ttl -= elapsed_time) >= 0)
|
||||
continue;
|
||||
|
||||
switch(se->_status)
|
||||
{
|
||||
switch (se->_status) {
|
||||
case session::WAITING:
|
||||
DBG("session is now invalid");
|
||||
se->_status = session::INVALID;
|
||||
@ -52,19 +50,24 @@ session::~session()
|
||||
{
|
||||
DBG("session::~session() this=%x", this);
|
||||
|
||||
_sessions.remove(_ptr);
|
||||
for (std::list<std::weak_ptr<session> >::iterator it = _sessions.begin();
|
||||
it != _sessions.end(); it++) {
|
||||
if (it->lock() == _ptr.lock()) {
|
||||
_sessions.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for(std::list<strong_ptr<iface> >::iterator it = _ifaces.begin();
|
||||
it != _ifaces.end(); it++)
|
||||
{
|
||||
(*it)->remove_session(_ptr);
|
||||
for (std::list<std::shared_ptr<iface> >::iterator it = _ifaces.begin();
|
||||
it != _ifaces.end(); it++) {
|
||||
(*it)->remove_session(_ptr.lock());
|
||||
}
|
||||
}
|
||||
|
||||
strong_ptr<session> session::create(const strong_ptr<proxy>& pr, const address& saddr,
|
||||
std::shared_ptr<session> session::create(const std::shared_ptr<proxy>& pr, const address& saddr,
|
||||
const address& daddr, const address& taddr)
|
||||
{
|
||||
strong_ptr<session> se(new session());
|
||||
std::shared_ptr<session> se(new session());
|
||||
|
||||
se->_ptr = se;
|
||||
se->_pr = pr;
|
||||
@ -82,12 +85,12 @@ strong_ptr<session> session::create(const strong_ptr<proxy>& pr, const address&
|
||||
return se;
|
||||
}
|
||||
|
||||
void session::add_iface(const strong_ptr<iface>& ifa)
|
||||
void session::add_iface(const std::shared_ptr<iface>& ifa)
|
||||
{
|
||||
if(std::find(_ifaces.begin(), _ifaces.end(), ifa) != _ifaces.end())
|
||||
if (std::find(_ifaces.begin(), _ifaces.end(), ifa) != _ifaces.end())
|
||||
return;
|
||||
|
||||
ifa->add_session(_ptr);
|
||||
ifa->add_session(_ptr.lock());
|
||||
_ifaces.push_back(ifa);
|
||||
}
|
||||
|
||||
@ -95,9 +98,8 @@ void session::send_solicit()
|
||||
{
|
||||
DBG("session::send_solicit() (%d)", _ifaces.size());
|
||||
|
||||
for(std::list<strong_ptr<iface> >::iterator it = _ifaces.begin();
|
||||
it != _ifaces.end(); it++)
|
||||
{
|
||||
for (std::list<std::shared_ptr<iface> >::iterator it = _ifaces.begin();
|
||||
it != _ifaces.end(); it++) {
|
||||
DBG(" - %s", (*it)->name().c_str());
|
||||
(*it)->write_solicit(_taddr);
|
||||
}
|
||||
|
@ -28,15 +28,15 @@ class iface;
|
||||
class session
|
||||
{
|
||||
private:
|
||||
weak_ptr<session> _ptr;
|
||||
std::weak_ptr<session> _ptr;
|
||||
|
||||
strong_ptr<proxy> _pr;
|
||||
std::shared_ptr<proxy> _pr;
|
||||
|
||||
address _saddr, _daddr, _taddr;
|
||||
|
||||
// An array of interfaces this session is monitoring for
|
||||
// ND_NEIGHBOR_ADVERT on.
|
||||
std::list<strong_ptr<iface> > _ifaces;
|
||||
std::list<std::shared_ptr<iface> > _ifaces;
|
||||
|
||||
// The remaining time in miliseconds the object will stay in the
|
||||
// interface's session array or cache.
|
||||
@ -44,7 +44,7 @@ private:
|
||||
|
||||
int _status;
|
||||
|
||||
static std::list<weak_ptr<session> > _sessions;
|
||||
static std::list<std::weak_ptr<session> > _sessions;
|
||||
|
||||
public:
|
||||
enum
|
||||
@ -59,10 +59,10 @@ public:
|
||||
// Destructor.
|
||||
~session();
|
||||
|
||||
static strong_ptr<session> create(const strong_ptr<proxy>& pr, const address& saddr,
|
||||
static std::shared_ptr<session> create(const std::shared_ptr<proxy>& pr, const address& saddr,
|
||||
const address& daddr, const address& taddr);
|
||||
|
||||
void add_iface(const strong_ptr<iface>& ifa);
|
||||
void add_iface(const std::shared_ptr<iface>& ifa);
|
||||
|
||||
const address& taddr() const;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user