summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephen Hemminger <stephen.hemminger@vyatta.com>2008-04-29 16:56:56 -0700
committerStephen Hemminger <stephen.hemminger@vyatta.com>2008-04-29 16:56:56 -0700
commita8726835c9064a1f2c6a261b14c637950abdd2bc (patch)
treefc9add560d1919d3f2c050d3da486038fba9e355
parente87f46b4abc10fe3fc59562d6be25fb96bce1970 (diff)
downloadquagga-a8726835c9064a1f2c6a261b14c637950abdd2bc.tar.bz2
quagga-a8726835c9064a1f2c6a261b14c637950abdd2bc.tar.xz
remove watchlink - no longer used
The watchlink daemon is no longer used, so code can be removed. If it is needed for future changes it can be recovered from the source code control system.
-rw-r--r--watchlink/Makefile.am8
-rw-r--r--watchlink/netlink_event.cc335
-rw-r--r--watchlink/netlink_event.hh274
-rw-r--r--watchlink/netlink_linkstatus.cc318
-rw-r--r--watchlink/netlink_linkstatus.hh56
-rw-r--r--watchlink/netlink_listener.cc271
-rw-r--r--watchlink/netlink_listener.hh50
-rw-r--r--watchlink/netlink_send.cc318
-rw-r--r--watchlink/netlink_send.hh39
-rw-r--r--watchlink/netlink_utils.cc59
-rw-r--r--watchlink/netlink_utils.hh20
-rw-r--r--watchlink/rl_str_proc.cc90
-rw-r--r--watchlink/rl_str_proc.hh32
-rw-r--r--watchlink/watchlink.cc293
14 files changed, 0 insertions, 2163 deletions
diff --git a/watchlink/Makefile.am b/watchlink/Makefile.am
deleted file mode 100644
index 798275e5..00000000
--- a/watchlink/Makefile.am
+++ /dev/null
@@ -1,8 +0,0 @@
-## Process this file with automake to produce Makefile.in.
-
-INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib
-DEFS = @DEFS@ -DSYSCONFDIR=\"$(sysconfdir)/\"
-
-sbin_PROGRAMS = watchlink
-
-watchlink_SOURCES = watchlink.cc netlink_utils.cc netlink_send.cc netlink_listener.cc netlink_event.cc netlink_linkstatus.cc rl_str_proc.cc
diff --git a/watchlink/netlink_event.cc b/watchlink/netlink_event.cc
deleted file mode 100644
index 7bda70bb..00000000
--- a/watchlink/netlink_event.cc
+++ /dev/null
@@ -1,335 +0,0 @@
-/*
- * Module: netlink_event.cc
- *
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 as published
- * by the Free Software Foundation.
- */
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <linux/rtnetlink.h>
-#include <net/if.h>
-
-#include <netinet/in.h>
-#include <arpa/inet.h>
-
-#include <syslog.h>
-
-#include <iostream>
-#include <string>
-#include "netlink_event.hh"
-
-using namespace std;
-
-/**
- *
- *
- **/
-NetlinkEvent::NetlinkEvent(int type,
- std::string iface,
- int mtu,
- unsigned char *mac,
- bool enabled,
- bool running,
- IPv4 local,
- IPv4 addr,
- IPv4 broadcast,
- int mask_len,
- int index) :
- _type(type),
- _iface(iface),
- _vif(false),
- _mtu(mtu),
- _enabled(enabled),
- _running(running),
- _local(local),
- _addr(addr),
- _broadcast(broadcast),
- _mask_len(mask_len),
- _index(index)
-{
- memcpy(_mac, mac, 6);
- if (_iface.find(".") != string::npos) {
- _vif = true;
- }
-}
-
-/**
- *
- *
- **/
-NetlinkEvent::~NetlinkEvent()
-{
-}
-
-/**
- *
- *
- **/
-void
-NetlinkEvent::log()
-{
- syslog(LOG_USER | LOG_INFO, "NetlinkEvent::log(): type: %d, iface: %s, mtu: %d, local: %s, addr: %s, bc: %s, mask: %d, index: %d", _type, _iface.c_str(), _mtu, _local.str().c_str(), _addr.str().c_str(), _broadcast.str().c_str(), _mask_len, _index);
-}
-
-/**
- *
- *
- **/
-NetlinkEventManager::NetlinkEventManager(bool debug) :
- _debug(debug)
-{
-
-}
-
-/**
- *
- *
- *
- **/
-NetlinkEventManager::~NetlinkEventManager()
-{
-}
-
-/**
- *
- *
- *
- **/
-void
-NetlinkEventManager::process(unsigned char* pkt, int size)
-{
- if (size <= 0) {
- return;
- }
-
- size_t ps = size_t(size);
-
- const struct nlmsghdr* mh;
- for (mh = reinterpret_cast<const struct nlmsghdr*>(pkt);
- NLMSG_OK(mh, ps);
- mh = NLMSG_NEXT(const_cast<struct nlmsghdr*>(mh), ps)) {
- parse_msg(mh);
- }
-}
-
-/**
- *
- *
- **/
-bool
-NetlinkEventManager::pop(NetlinkEvent &e)
-{
- char buf[20];
- sprintf(buf, "%d", _coll.size());
-
- NLEventIter iter = _coll.begin();
- if (iter != _coll.end()) {
- e = *iter;
- _coll.erase(iter);
- return true;
- }
- return false;
-}
-
-/**
- *
- *
- *
- **/
-void
-NetlinkEventManager::parse_msg(const struct nlmsghdr *nlHdr)
-{
- bool enabled;
- bool running;
- string iface;
- int mtu = -1;
- int index = -1;
- unsigned char mac[6];
- IPv4 addr, local, broadcast;
- int mask_len = -1;
-
- bzero(mac, 6);
-
- struct ifinfomsg* ifInfo = (struct ifinfomsg *)NLMSG_DATA(nlHdr);
-
- //link state flag
- enabled = ifInfo->ifi_flags & IFF_UP;
- running = enabled && (ifInfo->ifi_flags & IFF_RUNNING);
- index = ifInfo->ifi_index;
-
- struct rtattr* rtAttr = (struct rtattr *)IFLA_RTA(ifInfo);
- int rtLen = IFLA_PAYLOAD(nlHdr);
-
- switch (nlHdr->nlmsg_type) {
- case RTM_NEWLINK:
- case RTM_DELLINK:
- case RTM_NEWADDR:
- case RTM_DELADDR:
- for(;RTA_OK(rtAttr,rtLen);rtAttr = RTA_NEXT(rtAttr,rtLen)){
- if (nlHdr->nlmsg_type == RTM_NEWLINK ||
- nlHdr->nlmsg_type == RTM_DELLINK) {
- switch(rtAttr->rta_type) {
- case IFLA_IFNAME:
- iface = string((char*)RTA_DATA(rtAttr));
- break;
- case IFLA_ADDRESS:
- memcpy(mac, RTA_DATA(rtAttr), 6);
- break;
- case IFLA_MTU:
- mtu = *((unsigned int *)RTA_DATA(rtAttr));
- break;
- default:
- break;
- }
- }
- else if (nlHdr->nlmsg_type == RTM_NEWADDR ||
- nlHdr->nlmsg_type == RTM_DELADDR) {
- uint32_t address;
- struct ifaddrmsg *ifAddrs;
- ifAddrs = (struct ifaddrmsg *)NLMSG_DATA(nlHdr);
- mask_len = ifAddrs->ifa_prefixlen;
-
- switch(rtAttr->rta_type) {
- case IFA_LOCAL:
- address = *(uint32_t *)RTA_DATA(rtAttr);
- local = IPv4(address);
- break;
- case IFA_ADDRESS:
- address = *(uint32_t *)RTA_DATA(rtAttr);
- addr = IPv4(address);
- break;
- case IFA_LABEL:
- iface = string((char*)RTA_DATA(rtAttr));
- break;
- case IFA_BROADCAST:
- address = *(uint32_t *)RTA_DATA(rtAttr);
- broadcast = IPv4(address);
- break;
- default:
- break;
- }
- }
- }
- {
- NetlinkEvent e(nlHdr->nlmsg_type,
- iface,
- mtu,
- mac,
- enabled,
- running,
- local,
- addr,
- broadcast,
- mask_len,
- index);
-
- e.set_ifinfomsg(ifInfo);
-
- if (_debug) {
- e.log();
- }
- _coll.push_back(e);
- }
- break;
- case NLMSG_ERROR: {
- struct nlmsgerr *err = (struct nlmsgerr*) NLMSG_DATA(nlHdr);
- //drop down to info as this can be caused on startup by downed interfaces that are requested for dump, or attempting to remove routes on downed interface after startup (if they have already been removed).
- syslog(LOG_INFO,"netlink message of type ERROR received: %s",strerror(-err->error));
- // cerr << "netlink message of type ERROR received: " ;
- // cerr << string(strerror(-err->error)) << endl;
- }
- break;
- case NLMSG_DONE:
- // if (_debug) {
- // cout << "netlink message of type DONE received" << endl;
- // }
- break;
- case NLMSG_NOOP:
- syslog(LOG_INFO,"netlink message of type NOOP received");
- break;
- default:
- syslog(LOG_INFO,"unknown netlink message type received");
- break;
- }
-}
-
-
-/**
- *
- *
- *
- **/
-typedef struct {
- unsigned int iff_flag;
- char *name;
-} iff_flags_name;
-
-
-string
-NetlinkEvent::get_ifinfomsg()
-{
- string ret;
- char buf[40];
-
- sprintf(buf, "%u", _ifinfo.ifi_family);
- ret = "ifi_family: " + string(buf) + ", ";
- sprintf(buf, "%u", _ifinfo.ifi_type);
- ret += "ifi_type: " + string(buf) + ", ";
- sprintf(buf, "%d", _ifinfo.ifi_index);
- ret += "ifi_index: " + string(buf) + ", ";
- sprintf(buf, "%u", _ifinfo.ifi_flags);
- ret += "ifi_flags: " + string(buf) + ", ";
- sprintf(buf, "%u", _ifinfo.ifi_change);
- ret += "ifi_change: " + string(buf);
- return ret;
-}
-
-/*
-string
-NetlinkEvent::operator<<(const ostream &o)
-{
- UNUSED(o);
- return ("");
-}
-*/
-std::ostream & operator <<(std::ostream & Stream, const NetlinkEvent & instance)
-{
- // Stream << ... fields from instance
- const NetlinkEvent foo = instance;
- return Stream;
-}
-
-
-/* from quagga prefix.c */
-/* Apply mask to IPv4 prefix. */
-/* Maskbit. */
-static u_char maskbit[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
- 0xf8, 0xfc, 0xfe, 0xff};
-
-
-uint32_t
-apply_mask_ipv4 (const uint32_t p, const unsigned char m)
-{
- uint32_t val(p);
- u_char *pnt = (u_char *) &val;
- int index;
- int offset;
-
- index = m / 8;
- if (index < 4) {
- offset = m % 8;
-
- pnt[index] &= maskbit[offset];
- index++;
-
- while (index < 4) {
- pnt[index++] = 0;
- }
- }
- return val;
-}
-
-
-
diff --git a/watchlink/netlink_event.hh b/watchlink/netlink_event.hh
deleted file mode 100644
index 76ccefa6..00000000
--- a/watchlink/netlink_event.hh
+++ /dev/null
@@ -1,274 +0,0 @@
-/*
- * Module: netlink_event.hh
- *
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 as published
- * by the Free Software Foundation.
- */
-#ifndef __NETLINK_EVENT_HH__
-#define __NETLINK_EVENT_HH__
-
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <net/if.h>
-
-#include <string>
-#include <vector>
-#include <ostream>
-#include <linux/rtnetlink.h>
-
-uint32_t
-apply_mask_ipv4 (const uint32_t p, const unsigned char m);
-
-
-class Mac
-{
-public:
- Mac() {}
-private:
- unsigned char _addr[6];
-};
-
-class IPv4
-{
-public:
- IPv4() : _addr(-1) {}
- IPv4(uint32_t addr) : _addr(addr) {}
- virtual ~IPv4() {}
-
- uint32_t
- get() {return _addr;}
-
- virtual
- std::string
- str()
- {
- in_addr addr;
- addr.s_addr = _addr;
- char* buf = inet_ntoa(addr);
- return std::string(buf);
- }
-
-protected:
- uint32_t _addr;
-};
-
-
-class IPv4net : protected IPv4
-{
-public:
- IPv4net() : IPv4(-1),_mask_length(32) {}
- IPv4net(uint32_t addr, char mask_len) : IPv4(addr),_mask_length(mask_len) {}
- IPv4net(std::string &net) {
- int pos = net.find("/");
- if (pos == std::string::npos) {
- pos = net.length();
- _mask_length = 32;
- }
- else {
- uint32_t m_val = strtoul(net.substr(pos+1,net.length()-1).c_str(),NULL,10);
- if (m_val > 32) {
- m_val = 32;
- }
- _mask_length = m_val;
- }
- uint32_t a_val = inet_addr(net.substr(0,pos).c_str());
- if (a_val == INADDR_NONE) {
- this->_addr = 0;
- return;
- }
- this->_addr = apply_mask_ipv4(a_val,_mask_length);
- }
-
- uint32_t
- get_addr() {return get();}
-
- char
- get_mask_length() {return _mask_length;}
-
- bool
- contains(IPv4 &addr) {
- //check if addr is within specified addr range
- return (apply_mask_ipv4(addr.get(),_mask_length) == _addr);
- }
-
- bool
- contains(IPv4 addr) {
- //check if addr is within specified addr range
- return (apply_mask_ipv4(addr.get(),_mask_length) == _addr);
- }
-
- std::string
- str()
- {
- in_addr addr;
- addr.s_addr = _addr;
- char *buf = inet_ntoa(addr);
-
- std::string tmp(buf);
-
- char buf2[80];
- sprintf(buf2,"%d",_mask_length);
- tmp += std::string("/") + buf2;
- return tmp;
- }
-
-private:
- char _mask_length;
-};
-
-
-/**
- *
- *
- **/
-class NetlinkEvent
-{
-public:
- NetlinkEvent(int type,
- std::string iface,
- int mtu,
- unsigned char *mac,
- bool enabled,
- bool running,
- IPv4 local,
- IPv4 addr,
- IPv4 broadcast,
- int mask_len,
- int index);
-
- NetlinkEvent() :
- _type(-1),
- _mtu(-1),
- _enabled(false),
- _running(false),
- _mask_len(-1),
- _index(-1) {}
-
- ~NetlinkEvent();
-
-
- std::string
- get_iface() const {return _iface;}
-
- int
- get_mtu() const {return _mtu;}
-
- std::string
- get_mac_str() const {
- char buf[18];
- sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
- _mac[0], _mac[1], _mac[2],
- _mac[3], _mac[4], _mac[5]
- );
- return std::string(buf);
- }
-
- int
- get_type() const {return _type;}
- /*
- unsigned char*
- get_mac() const {
- return _mac;
- }
- */
-
- int
- get_ifi_type() const {return _ifinfo.ifi_type;}
-
- bool
- get_enabled() const {return _enabled;}
-
- bool
- get_running() const {return _running;}
-
- IPv4
- get_local_addr() const {return _local;}
-
- IPv4
- get_addr() const {return _addr;}
-
- IPv4
- get_broadcast() const {return _broadcast;}
-
- int
- get_mask_len() const {return _mask_len;}
-
- bool
- is_link_up() const {return _enabled;}
-
- int
- get_index() const {return _index;}
-
- bool
- is_vif() const {return _vif;}
-
- void
- log();
-
- void
- set_ifinfomsg(ifinfomsg *ifinfo) {_ifinfo = *ifinfo;}
-
- std::string
- get_ifinfomsg();
-
-private:
- int _type;
- std::string _iface;
- bool _vif;
- int _mtu;
- unsigned char _mac[6];
- bool _enabled;
- bool _running;
- IPv4 _local;
- IPv4 _addr;
- IPv4 _broadcast;
- int _mask_len;
- int _index;
-
- //debug
- struct ifinfomsg _ifinfo;
-};
-
-
-
-/**
- *
- *
- **/
-class NetlinkEventManager
-{
-public:
- typedef std::vector<NetlinkEvent> NLEventColl;
- typedef std::vector<NetlinkEvent>::iterator NLEventIter;
-
-public: //methods
- friend std::ostream & operator<< (std::ostream &, const NetlinkEvent &);
-
- NetlinkEventManager(bool debug);
-
- ~NetlinkEventManager();
-
- void
- process(unsigned char *pkt, int size);
-
- bool
- pop(NetlinkEvent &e);
-
-
-
-private: //methods
- void
- parse_msg(const struct nlmsghdr*);
-
-private: //variables
- NLEventColl _coll;
- bool _debug;
-};
-
-
-
-
-#endif // __NETLINK_EVENT_HH__
diff --git a/watchlink/netlink_linkstatus.cc b/watchlink/netlink_linkstatus.cc
deleted file mode 100644
index 9dd26fbd..00000000
--- a/watchlink/netlink_linkstatus.cc
+++ /dev/null
@@ -1,318 +0,0 @@
-/*
- * Module: netlink_linkstatus.cc
- *
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 as published
- * by the Free Software Foundation.
- */
-#include <stdio.h>
-#include <sys/socket.h>
-#include <sys/file.h>
-#include <iostream>
-#include <string>
-#include <syslog.h>
-#include <errno.h>
-
-#include "rl_str_proc.hh"
-#include "netlink_send.hh"
-#include "netlink_event.hh"
-#include "netlink_utils.hh"
-#include "netlink_linkstatus.hh"
-
-
-using namespace std;
-
-/**
- *
- *
- **/
-NetlinkLinkStatus::NetlinkLinkStatus(int send_sock, const string &link_dir, bool debug) :
- _nl_send(debug),
- _send_sock(send_sock),
- _link_dir(link_dir),
- _debug(debug)
-{
- if (_send_sock < 0) {
- syslog(LOG_ERR,"NetlinkListStatus::NetlinkLinkStatus(), send sock is bad value");
- cerr << "NetlinkListStatus::NetlinkLinkStatus(), send sock is bad value" << endl;
- }
-
- if (_link_dir.empty()) {
- syslog(LOG_ERR,"NetlinkListStatus::NetlinkLinkStatus(), no link status directory specified");
- cerr << "NetlinkListStatus::NetlinkLinkStatus(), no link status directory specified" << endl;
- }
-}
-
-/**
- *
- *
- **/
-NetlinkLinkStatus::~NetlinkLinkStatus()
-{
-}
-
-/**
- *
- *
- **/
-void
-NetlinkLinkStatus::process(const NetlinkEvent &event)
-{
- bool state_event = false;
-
- IfaceStateIter iter = _iface_state_coll.find(event.get_index());
- if (iter == _iface_state_coll.end()) {
- if (event.get_type() == RTM_NEWLINK || event.get_type() == RTM_DELLINK) {
- //start maintaining state information here.
- _iface_state_coll.insert(pair<int,bool>(event.get_index(),event.get_running()));
-
- //let's clean up directory here!
- char buf[40];
- sprintf(buf,"%d",event.get_index());
- string file = _link_dir + "/" + buf;
- unlink(file.c_str());
- }
- return;
- }
-
- bool running_old = iter->second;
- bool running_new = event.get_running();
-
- //capture link status on link messages only
- if (event.get_type() == RTM_NEWLINK ||
- event.get_type() == RTM_DELLINK) {
- // _iface_state_coll.insert(pair<int,bool>(event.get_index(),event.get_running()));
- _iface_state_coll[event.get_index()] = event.get_running();
- if (running_old != running_new) {
- state_event = true;
- }
- }
-
- //is this a transition from up->down, or down->up?
- if (state_event) {
- if (running_new) {
- process_going_up(event);
- }
- else {
- process_going_down(event);
- }
- }
- else {
- if (running_old) {
- process_up(event);
- }
- else {
- process_down(event);
- }
- }
-}
-
-/**
- *
- * file is in the format of IFINDEX,IP,MASK
- *
- **/
-int
-NetlinkLinkStatus::process_up(const NetlinkEvent &event)
-{
- if (_debug) {
- cout << "NetlinkLinkStatus::process_up(): " << event.get_iface() << endl;
- }
- //can't think of anything that needs to go here yet.
- return 0;
-}
-
-/**
- *
- *
- **/
-int
-NetlinkLinkStatus::process_going_up(const NetlinkEvent &event)
-{
- if (_debug) {
- cout << "NetlinkLinkStatus::process_going_up(): " << event.get_iface() << endl;
- }
- syslog(LOG_INFO,"Interface is now active: %s",event.get_iface().c_str());
-
- //check for link status file, otherwise return
- char buf[40];
- sprintf(buf,"%d",event.get_index());
- string file = _link_dir + "/" + buf;
- FILE *fp = fopen(file.c_str(), "r");
- if (fp == NULL) {
- syslog(LOG_INFO,"NetlinkLinkStatus::process_going_up(), failed to open state file: %s",
- strerror(errno));
- // cerr << "NetlinkLinkStatus::process_going_up(), failed to open state file" << endl;
- return -1; //means we are still up, ignore...
- }
-
- if (flock(fileno(fp), LOCK_SH)) {
- syslog(LOG_INFO, "NetlinkLinkStatus::process_going_up() failed to acquire lock: %s",
- strerror(errno));
- fclose(fp);
- return -1;
- }
-
- char str[1025];
- while (fgets(str, 1024, fp)) {
- string line(str);
-
- StrProc tokens(line, ",");
- if (tokens.size() != 4) {
- syslog(LOG_INFO,"NetlinkLinkStatus::process_up(), failure to parse link status file, exiting(): %s, size: %d", line.c_str(), tokens.size());
- // cerr << "NetlinkLinkStatus::process_up(), failure to parse link status file, exiting(): " << line << ", size: " << tokens.size() << endl;
- fclose(fp);
- return -1;
- }
-
- int ifindex = strtoul(tokens.get(0).c_str(),NULL,10);
- uint32_t local_addr = strtoul(tokens.get(1).c_str(),NULL,10);
- uint32_t addr = strtoul(tokens.get(2).c_str(),NULL,10);
- int mask_len = strtoul(tokens.get(3).c_str(),NULL,10);
-
-
- bool err = _nl_send.send_set(_send_sock, ifindex, local_addr, addr, mask_len, RTM_NEWADDR);
- if (err) {
- syslog(LOG_INFO,"NetlinkLinkStatus::process_up(), failure in setting interface back to up");
- }
- /*
- bool err = _nl_send.send_set_route(_send_sock, ifindex, local_addr, local_addr, 32, RTM_NEWROUTE, RT_TABLE_LOCAL, RTN_LOCAL, RT_SCOPE_HOST);
- if (err) {
- syslog(LOG_INFO,"NetlinkLinkStatus::process_up(), failure in setting interface back to up");
- }
- //COMPUTE FIRST ADDRESS
- uint32_t first_addr = ipv4_first_addr(local_addr, mask_len);
- err = _nl_send.send_set_route(_send_sock, ifindex, local_addr, first_addr, 32, RTM_NEWROUTE, RT_TABLE_LOCAL, RTN_BROADCAST, RT_SCOPE_LINK);
- if (err) {
- syslog(LOG_INFO,"NetlinkLinkStatus::process_up(), failure in setting interface back to up");
- }
- //COMPUTE LAST ADDRESS
- uint32_t last_addr = ipv4_broadcast_addr(local_addr, mask_len);
- err = _nl_send.send_set_route(_send_sock, ifindex, local_addr, last_addr, 32, RTM_NEWROUTE, RT_TABLE_LOCAL, RTN_BROADCAST, RT_SCOPE_LINK);
- if (err) {
- syslog(LOG_INFO,"NetlinkLinkStatus::process_up(), failure in setting interface back to up");
- }
-
- //reinsert addresses to interface
- err = _nl_send.send_set_route(_send_sock, ifindex, local_addr, first_addr, mask_len, RTM_NEWROUTE, RT_TABLE_MAIN, RTN_UNICAST, RT_SCOPE_LINK);
- if (err) {
- syslog(LOG_INFO,"NetlinkLinkStatus::process_up(), failure in setting interface back to up");
- }
- */
- }
-
- fclose(fp);
-
- //remove file
- unlink(file.c_str());
-
- return 0;
-}
-
-/**
- * send an ip flush command and capture all the rtm_deladdr messages to the file...
- *
- **/
-int
-NetlinkLinkStatus::process_down(const NetlinkEvent &event)
-{
- if (_debug) {
- cout << "NetlinkLinkStatus::process_down(): " << event.get_iface() << endl;
- }
-
- if (event.get_type() != RTM_NEWADDR) {
- return 0;
- }
-
- if (event.get_index() < 0 || event.get_local_addr().get() == 0 || event.get_mask_len() < 1) {
- return 0;
- }
-
- if (_debug) {
- cout << "netlinkLinkStatus::process_down(), processing valid request" << endl;
- }
-
- //append to file...
- char buf[40];
- sprintf(buf,"%d",event.get_index());
- string file = _link_dir + "/" + buf;
- FILE *fp = fopen(file.c_str(), "a");
- if (fp == NULL) {
- syslog(LOG_INFO,"NetlinkLinkStatus::process_down(), failed to open state file");
- // cerr << "NetlinkLinkStatus::process_down(), failed to open state file" << endl;
- return -1;
- }
-
- if (flock(fileno(fp), LOCK_EX)) {
- syslog(LOG_INFO, "NetlinkLinkStatus::process_down() failed to acquire lock: %s",
- strerror(errno));
- fclose(fp);
- return -1;
- }
-
- int ifindex = event.get_index();
- uint32_t local_addr = event.get_local_addr().get();
- int mask_len = event.get_mask_len();
-
- //create file on system
- //CRAJ--NEED TO HAVE THIS BE FROM A COLLECTION??? DEPENDS ON FORMAT OF NETLINK MSG
- sprintf(buf,"%d",ifindex);
- string line = string(buf) + ",";
- sprintf(buf,"%d",local_addr);
- line += string(buf) + ",";
- sprintf(buf,"%d",event.get_addr().get());
- line += string(buf) + ",";
- sprintf(buf,"%d",mask_len);
- line += string(buf) + "\n";
-
- fputs(line.c_str(),fp);
-
- bool err = _nl_send.send_set(_send_sock, ifindex, event.get_local_addr().get(), event.get_addr().get(), mask_len, RTM_DELADDR);
- if (err) {
- syslog(LOG_INFO,"NetlinkLinkStatus::process_down(), failure in setting interface down");
- }
-
- /*
- uint32_t first_addr = ipv4_first_addr(local_addr, mask_len);
- //reinsert addresses to interface
- bool err = _nl_send.send_set_route(_send_sock, ifindex, local_addr, first_addr, mask_len, RTM_DELROUTE, RT_TABLE_MAIN, -1,-1);
- if (err) {
- syslog(LOG_INFO,"NetlinkLinkStatus::process_down(), failure in setting interface down");
- }
-
- uint32_t last_addr = ipv4_broadcast_addr(local_addr, mask_len);
- err = _nl_send.send_set_route(_send_sock, ifindex, local_addr, last_addr, 32, RTM_DELROUTE, RT_TABLE_LOCAL, -1,-1);
- if (err) {
- syslog(LOG_INFO,"NetlinkLinkStatus::process_down(), failure in setting interface down");
- }
-
- err = _nl_send.send_set_route(_send_sock, ifindex, first_addr, first_addr, 32, RTM_DELROUTE, RT_TABLE_LOCAL, -1,-1);
- if (err) {
- syslog(LOG_INFO,"NetlinkLinkStatus::process_down(), failure in setting interface down");
- }
-
- err = _nl_send.send_set_route(_send_sock, ifindex, local_addr, local_addr, 32, RTM_DELROUTE, RT_TABLE_LOCAL, -1,-1);
- if (err) {
- syslog(LOG_INFO,"NetlinkLinkStatus::process_down(), failure in setting interface down");
- }
- */
- fclose(fp);
-
- return 0;
-}
-
-int
-NetlinkLinkStatus::process_going_down(const NetlinkEvent &event)
-{
- if (_debug) {
- cout << "NetlinkLinkStatus::process_going_down(): " << event.get_iface() << "(" << event.get_index() << ")" << endl;
- }
- syslog(LOG_INFO,"Interface is now inactive: %s",event.get_iface().c_str());
-
- //pull interface addresses
- if (_nl_send.send_get(_send_sock, RTM_GETADDR, event.get_index())) {
- return -1;
- }
- return 0;
-}
diff --git a/watchlink/netlink_linkstatus.hh b/watchlink/netlink_linkstatus.hh
deleted file mode 100644
index b4eebbce..00000000
--- a/watchlink/netlink_linkstatus.hh
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * Module: netlink_linkstatus.hh
- *
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 as published
- * by the Free Software Foundation.
- */
-#ifndef __NETLINK_LINKSTATUS_HH__
-#define __NETLINK_LINKSTATUS_HH__
-
-#include <string>
-#include <map>
-#include "netlink_event.hh"
-#include "netlink_send.hh"
-
-class NetlinkLinkStatus
-{
-public:
- typedef std::map<int,bool> IfaceStateColl;
- typedef std::map<int,bool>::iterator IfaceStateIter;
-
-public:
- NetlinkLinkStatus(int send_sock, const std::string &link_dir, bool debug);
- ~NetlinkLinkStatus();
-
- void
- process(const NetlinkEvent &event);
-
-private:
- int
- process_up(const NetlinkEvent &event);
-
- int
- process_down(const NetlinkEvent &event);
-
- int
- process_going_up(const NetlinkEvent &event);
-
- int
- process_going_down(const NetlinkEvent &event);
-
-
-private:
- NetlinkSend _nl_send;
- int _send_sock;
- std::string _link_dir;
- bool _debug;
-
- //keeps track of down messages where we've issued a
- //request for addresses but haven't received msg yet.
- IfaceStateColl _iface_state_coll;
-
-};
-
-#endif //__NETLINK_LINKSTATUS_HH__
diff --git a/watchlink/netlink_listener.cc b/watchlink/netlink_listener.cc
deleted file mode 100644
index be17fc76..00000000
--- a/watchlink/netlink_listener.cc
+++ /dev/null
@@ -1,271 +0,0 @@
-/*
- * Module: netlink_listener.cc
- *
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 as published
- * by the Free Software Foundation.
- */
-#include <errno.h>
-#include <sys/types.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <linux/rtnetlink.h>
-#include <syslog.h>
-
-#include <map>
-#include <vector>
-#include <string>
-#include <iostream>
-
-#include "netlink_event.hh"
-#include "netlink_listener.hh"
-
-
-using namespace std;
-
-
-#define SO_RCV_BUF_SIZE_MAX (256*1024) // Desired socket buffer size
-#define SO_RCV_BUF_SIZE_MIN (48*1024) // Min. socket buffer size
-#define NLSOCK_BYTES (8*1024)
-
-/**
- *
- *
- **/
-NetlinkListener::NetlinkListener(bool debug) :
- _fd(-1),
- _is_multipart_message_read(false),
- _nl_event_mgr(debug)
-{
-
-}
-
-/**
- *
- *
- **/
-NetlinkListener::~NetlinkListener()
-{
- close(_fd);
-}
-
-/**
- *
- *
- **/
-int
-NetlinkListener::init()
-{
- struct sockaddr_nl snl;
- socklen_t snl_len;
-
- if (_fd >= 0) {
- syslog(LOG_ERR,"socket cannot be initialized");
- cerr << "socket cannot be initialized" << endl;
- return _fd;
- }
-
- _fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
- if (_fd < 0) {
- syslog(LOG_ERR,"Could not open netlink socket: %s",strerror(errno));
- cerr << string("Could not open netlink socket: ") << strerror(errno) << endl;
- return _fd;
- }
-
- comm_sock_set_rcvbuf(_fd, SO_RCV_BUF_SIZE_MAX, SO_RCV_BUF_SIZE_MIN);
-
- memset(&snl, 0, sizeof(snl));
- snl.nl_family = AF_NETLINK;
- snl.nl_pid = getpid(); // Let the kernel assign the pid to the socket
- snl.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;//_nl_groups;
- if (bind(_fd, reinterpret_cast<struct sockaddr*>(&snl), sizeof(snl)) < 0) {
- syslog(LOG_ERR,"bind(AF_NETLINK) failed: ",strerror(errno));
- cerr << string("bind(AF_NETLINK) failed: ") << strerror(errno) << endl;
- close(_fd);
- _fd = -1;
- return _fd;
- }
-
- snl_len = sizeof(snl);
- if (getsockname(_fd, reinterpret_cast<struct sockaddr*>(&snl), &snl_len) < 0) {
- syslog(LOG_ERR,"getsockname(AF_NETLINK) failed: ",strerror(errno));
- cerr << string("getsockname(AF_NETLINK) failed: ") << strerror(errno) << endl;
- close(_fd);
- _fd = -1;
- return _fd;
- }
- if (snl_len != sizeof(snl)) {
- syslog(LOG_ERR,"Wrong address length of AF_NETLINK socket: ");
- cerr << string("Wrong address length of AF_NETLINK socket: ") << endl;
- close(_fd);
- _fd = -1;
- return _fd;
- }
- if (snl.nl_family != AF_NETLINK) {
- syslog(LOG_ERR,"wrong address family of AF_NETLINK socket: ");;
- cerr << string("Wrong address family of AF_NETLINK socket: ") << endl;
- close(_fd);
- _fd = -1;
- return _fd;
- }
- return _fd;
-}
-
-/**
- *
- *
- **/
-bool
-NetlinkListener::process(NetlinkEvent &e, multimap<string,IPv4net> filter)
-{
- bool state = process(e);
- //is this a non-ethernet msg?
- if ((e.get_type() == RTM_DELLINK || e.get_type() == RTM_NEWLINK) && e.get_ifi_type() != 1/*ARPHRD_ETHER*/) {
- e = NetlinkEvent();
- state = false;
- }
- else {
- multimap<string,IPv4net>::iterator iter = filter.begin();
- while (iter != filter.end()) {
- if (iter->first == e.get_iface() || iter->first == "*") { //found interface exclusion
- if (iter->second.contains(e.get_local_addr())) {
- e = NetlinkEvent();
- state = false;
- break;
- }
- }
- ++iter;
- }
- }
-
-
- return state;
-}
-
-/**
- *
- *
- **/
-bool
-NetlinkListener::process(NetlinkEvent &e)
-{
- if (_fd <= 0) {
- return false;
- }
-
- vector<uint8_t> message;
- vector<uint8_t> buffer(NLSOCK_BYTES);
- size_t last_mh_off = 0;
- size_t off = 0;
- ssize_t got = -1;
-
- char buf[20];
-
- for ( ; ; ) {
- //don't block on recv
- do {
- got = recv(_fd, &buffer[0], buffer.size(), MSG_DONTWAIT | MSG_PEEK);
- if ((got < 0) && (errno == EINTR))
- continue; // XXX: the receive was interrupted by a signal
- if ((got < 0) || (got < (ssize_t)buffer.size()))
- break; // The buffer is big enough
- buffer.resize(buffer.size() + NLSOCK_BYTES);
- } while (true);
-
- got = recv(_fd, &buffer[0], buffer.size(),
- MSG_DONTWAIT);
- // got = read(_fd, &buffer[0], buffer.size());
- if (got < 0) {
- if (errno == EINTR)
- continue;
- // cerr << "Netlink socket read error: " << endl;
- break;
- }
-
- message.resize(message.size() + got);
- memcpy(&message[off], &buffer[0], got);
- off += got;
-
- if ((off - last_mh_off) < (ssize_t)sizeof(struct nlmsghdr)) {
- syslog(LOG_ERR,"Netlink socket recvfrom failed: message truncated: ");
- cerr << string("Netlink socket recvfrom failed: message truncated: ") << endl;
- break;
- }
-
- //
- // If this is a multipart message, it must be terminated by NLMSG_DONE
- //
- // NEED to revisit the MULTI part message--does not operate correctly right now.
- //
-
- bool is_end_of_message = false;
- size_t new_size = off - last_mh_off;
- const struct nlmsghdr* mh;
- for (mh = reinterpret_cast<const struct nlmsghdr*>(&buffer[last_mh_off]);
- NLMSG_OK(mh, new_size);
- mh = NLMSG_NEXT(const_cast<struct nlmsghdr*>(mh), new_size)) {
- if ((mh->nlmsg_flags & NLM_F_MULTI)
- || _is_multipart_message_read) {
- sprintf(buf, "%d", mh->nlmsg_type);
- is_end_of_message = false;
- if (mh->nlmsg_type == NLMSG_DONE) {
- is_end_of_message = true;
- }
- }
- }
- last_mh_off = reinterpret_cast<size_t>(mh) - reinterpret_cast<size_t>(&buffer[0]);
- if (is_end_of_message) {
- break;
- }
- }
-
- _nl_event_mgr.process(&message[0], off);
- return _nl_event_mgr.pop(e);
-}
-
-/**
- *
- *
- **/
-int
-NetlinkListener::comm_sock_set_rcvbuf(int sock, int desired_bufsize, int min_bufsize)
-{
- int delta = desired_bufsize / 2;
-
- /*
- * Set the socket buffer size. If we can't set it as large as we
- * want, search around to try to find the highest acceptable
- * value. The highest acceptable value being smaller than
- * minsize is a fatal error.
- */
- if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
- &desired_bufsize,
- sizeof(desired_bufsize)) < 0) {
- desired_bufsize -= delta;
- while (1) {
- if (delta > 1)
- delta /= 2;
-
- if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
- &desired_bufsize,
- sizeof(desired_bufsize)) < 0) {
- desired_bufsize -= delta;
- if (desired_bufsize <= 0)
- break;
- } else {
- if (delta < 1024)
- break;
- desired_bufsize += delta;
- }
- }
- if (desired_bufsize < min_bufsize) {
- syslog(LOG_ERR,"Cannot set receiving buffer size of socket");
- cerr << "Cannot set receiving buffer size of socket" << endl;
- return -1;
- }
- }
-
- return (desired_bufsize);
-}
-
diff --git a/watchlink/netlink_listener.hh b/watchlink/netlink_listener.hh
deleted file mode 100644
index b74589a8..00000000
--- a/watchlink/netlink_listener.hh
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * Module: netlink_listener.hh
- *
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 as published
- * by the Free Software Foundation.
- */
-#ifndef __NETLINK_LISTENER_HH__
-#define __NETLINK_LISTENER_HH__
-
-#include "netlink_event.hh"
-#include <map>
-#include <string>
-
-class NetlinkListener
-{
-public: //methods
- NetlinkListener(bool debug);
- ~NetlinkListener();
-
- /*
- * returns socket fd
- */
- int
- init();
-
- bool
- process(NetlinkEvent &e);
-
- bool
- process(NetlinkEvent &e, std::multimap<std::string,IPv4net> filter);
-
- int
- get_sock() {return _fd;}
-
- void
- set_multipart(bool state) {_is_multipart_message_read = state;}
-
-private: //methods
-int
-comm_sock_set_rcvbuf(int sock, int desired_bufsize, int min_bufsize);
-
-private: //vraiables
- int _fd;
- bool _is_multipart_message_read;
- NetlinkEventManager _nl_event_mgr;
-};
-
-#endif //__NETLINK_LISTENER_HH__
diff --git a/watchlink/netlink_send.cc b/watchlink/netlink_send.cc
deleted file mode 100644
index 3824c2ef..00000000
--- a/watchlink/netlink_send.cc
+++ /dev/null
@@ -1,318 +0,0 @@
-/*
- * Module: netlink_send.cc
- *
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 as published
- * by the Free Software Foundation.
- */
-#include <errno.h>
-#include <arpa/inet.h>
-#include <linux/types.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <linux/netlink.h>
-#include <linux/rtnetlink.h>
-#include <syslog.h>
-
-#include <vector>
-#include <string>
-#include <iostream>
-
-#include "netlink_utils.hh"
-#include "netlink_send.hh"
-
-using namespace std;
-
-
-/**
- *
- *
- **/
-NetlinkSend::NetlinkSend(bool debug) : _debug(debug)
-{
-}
-
-
-/**
- *
- *
- **/
-NetlinkSend::~NetlinkSend()
-{
-}
-
-/**
- *
- *
- **/
-int
-NetlinkSend::send_set(int sock, int ifindex, uint32_t local_addr, uint32_t addr, int mask_len, int type)
-{
- int ret;
- struct sockaddr_nl snl;
- struct {
- struct nlmsghdr n;
- struct ifaddrmsg ifa;
- char buf[256];
- } req;
-
- /* Check netlink socket. */
- if (sock < 0) {
- syslog(LOG_ERR,"sock is not active, exiting");
- cerr << "sock is not active, exiting" << endl;
- return -1;
- }
-
- if (_debug) {
- struct in_addr in;
- in.s_addr = local_addr;
- char *lbuf = inet_ntoa(in);
-
- in.s_addr = addr;
- char *buf = inet_ntoa(in);
-
- char sbuf[1024];
- sprintf(sbuf, "NetlinkSend::send_set(): %d, %s/%d, to this address: %s, on interface: %d",type,buf,mask_len,lbuf,ifindex);
- cout << sbuf << endl;
-
- syslog(LOG_INFO,sbuf);
- }
-
- memset(&req, 0, sizeof(req));
-
- req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
- req.n.nlmsg_flags = NLM_F_REQUEST;
- req.n.nlmsg_pid = getpid();
- req.n.nlmsg_type = type;
- req.n.nlmsg_seq = time(NULL);
- req.ifa.ifa_family = AF_INET;
- req.ifa.ifa_index = ifindex;
- req.ifa.ifa_prefixlen = mask_len;
-
- // addr = htonl( addr );
- addattr_l(&req.n, sizeof(req), IFA_LOCAL, &local_addr, sizeof(local_addr) );
-
- in_addr_t broadcast_addr = ipv4_broadcast_addr(local_addr,mask_len);
- addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &broadcast_addr, sizeof(broadcast_addr) );
-
- if (addr != -1 && local_addr != addr) {
- addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &addr, sizeof(addr) );
- }
-
- memset(&snl, 0, sizeof(snl));
- snl.nl_family = AF_NETLINK;
-
- ret = sendto (sock, (void*) &req, sizeof req, 0,
- (struct sockaddr*) &snl, sizeof snl);
- if (ret < 0) {
- syslog(LOG_ERR,"netlink_send failed on send: %d, %d",ret,errno);
- cerr << "netlink_send failed on send: " << ret << ", " << errno << endl;
- return -1;
- }
- return 0;
-}
-
-/**
- *
- *
- **/
-int
-NetlinkSend::send_set_route(int sock, int ifindex, uint32_t local_addr, uint32_t dst_addr, int mask_len, int type, int table, int rtn_type, int rt_scope)
-{
- int ret;
- struct sockaddr_nl snl;
- struct {
- struct nlmsghdr n;
- struct rtmsg rt_message;
- char buf[8192];
- } req;
- req.rt_message.rtm_table = 0;
- req.rt_message.rtm_protocol = 0;
- req.rt_message.rtm_scope = 0;
- req.rt_message.rtm_type = 0;
- req.rt_message.rtm_src_len = 0;
- req.rt_message.rtm_dst_len = 0;
- req.rt_message.rtm_tos = 0;
-
- memset(&snl, 0, sizeof(snl));
- snl.nl_family = AF_NETLINK;
-
- /* Check netlink socket. */
- if (sock < 0) {
- syslog(LOG_ERR,"sock is not active, exiting");
- cerr << "sock is not active, exiting" << endl;
- return -1;
- }
-
- if (_debug) {
- struct in_addr in;
- in.s_addr = local_addr;
- char *lbuf = inet_ntoa(in);
-
- in.s_addr = dst_addr;
- char *buf = inet_ntoa(in);
-
- char sbuf[1024];
- sprintf(sbuf, "NetlinkSend::send_set_route(): %d, %s/%d, to this address: %s, on interface: %d, for this table: ",type,buf,mask_len,lbuf,ifindex,table);
- cout << sbuf << endl;
-
- syslog(LOG_INFO,sbuf);
- }
-
- memset(&req, 0, sizeof(req));
-
- req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
- if (type == RTM_NEWROUTE) {
- req.n.nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE|NLM_F_EXCL;
- }
- else {
- req.n.nlmsg_flags = NLM_F_REQUEST;
- }
- req.n.nlmsg_pid = getpid();
- req.n.nlmsg_type = type;
- req.n.nlmsg_seq = time(NULL);
-
- req.rt_message.rtm_family = AF_INET;
- req.rt_message.rtm_dst_len = mask_len;
- req.rt_message.rtm_table = table;
- if (type == RTM_NEWROUTE) {
- req.rt_message.rtm_protocol = RTPROT_KERNEL;
- if (rt_scope != -1) {
- req.rt_message.rtm_scope = rt_scope;//RT_SCOPE_HOST; //will need to pass this in to get RT_SCOPE_HOST
- }
- if (rtn_type != -1) {
- req.rt_message.rtm_type = rtn_type;//RTN_LOCAL;
- }
- }
- else {
- req.rt_message.rtm_scope = RT_SCOPE_NOWHERE;
- req.rt_message.rtm_type = RTN_UNSPEC;
- }
-
- addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &local_addr, sizeof(local_addr));
- addattr_l(&req.n, sizeof(req), RTA_DST, &dst_addr, sizeof(dst_addr));
- addattr32(&req.n, sizeof(req), RTA_OIF, ifindex);
-
- if (_debug) {
- cout << "NetlinkSend::send_set_route():" << endl;
- cout << " interface: " << ifindex << endl;
- cout << " type: " << string(req.n.nlmsg_type == RTM_NEWROUTE ? string("RTM_NEWROUTE") : string("RTM_DELROUTE")) << endl;
- cout << " flags: " << req.n.nlmsg_flags << endl;
- cout << " protocol: " << int(req.rt_message.rtm_protocol) << endl;
- cout << " scope: " << int(req.rt_message.rtm_scope) << endl;
- cout << " addr(s): " << local_addr << ", " << dst_addr << ", " << mask_len << ", " << int(req.rt_message.rtm_dst_len) << endl;
- cout << endl;
- }
-
- ret = sendto (sock, (void*) &req, sizeof req, 0,
- (struct sockaddr*) &snl, sizeof snl);
- if (ret < 0) {
- syslog(LOG_ERR,"netlink_send_route failed on send: %d, %d",ret,errno);
- cerr << "netlink_send_route failed on send: " << ret << ", " << errno << endl;
- return -1;
- }
- return 0;
-}
-
-/**
- *
- *
- **/
-int
-NetlinkSend::send_get(int sock, int type, int ifindex)
-{
- int ret;
- struct sockaddr_nl snl;
- struct {
- struct nlmsghdr n;
- struct ifaddrmsg ifa;
- char buf[256];
- } req;
-
- /* Check netlink socket. */
- if (sock < 0) {
- syslog(LOG_ERR,"sock is not active, exiting");
- cerr << "sock is not active, exiting" << endl;
- return -1;
- }
-
- if (_debug) {
- char sbuf[1024];
- sprintf(sbuf,"NetlinkSend::send_get(): type: %d, ifindex: %d",type,ifindex);
- cout << sbuf << endl;
- syslog(LOG_INFO,sbuf);
- }
-
- memset(&req, 0, sizeof(req));
-
- req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
- if (ifindex > -1) {
- req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH;
- }
- else {
- req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
- }
- req.n.nlmsg_pid = getpid();
- req.n.nlmsg_type = type;
- req.n.nlmsg_seq = time(NULL);
- req.ifa.ifa_family = AF_INET;
- if (ifindex > -1) {
- req.ifa.ifa_index = ifindex;
- }
-
- /*
- note bug right now in that all interfaces are provided back when a specific index is requested
- */
- memset(&snl, 0, sizeof(snl));
- snl.nl_family = AF_NETLINK;
-
- ret = sendto (sock, (void*) &req, sizeof req, 0,
- (struct sockaddr*) &snl, sizeof snl);
- if (ret < 0) {
- syslog(LOG_ERR,"netlink_send failed on send: %d, %d",ret,errno);
- cerr << "netlink_send failed on send: " << ret << ", " << errno << endl;
- return -1;
- }
- return 0;
-}
-
-/**
- *
- *
- **/
-int
-NetlinkSend::addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
-{
- int len = RTA_LENGTH(alen);
- struct rtattr *rta;
-
- if ((int)NLMSG_ALIGN(n->nlmsg_len) + len > maxlen)
- return -1;
- rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
- rta->rta_type = type;
- rta->rta_len = len;
- memcpy(RTA_DATA(rta), data, alen);
- n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
- return 0;
-}
-
-int
-NetlinkSend::addattr32(struct nlmsghdr *n, int maxlen, int type, int data)
-{
- int len;
- struct rtattr *rta;
-
- len = RTA_LENGTH (4);
-
- if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen)
- return -1;
-
- rta = (struct rtattr *) (((char *) n) + NLMSG_ALIGN (n->nlmsg_len));
- rta->rta_type = type;
- rta->rta_len = len;
- memcpy (RTA_DATA (rta), &data, 4);
- n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + len;
-
- return 0;
-}
diff --git a/watchlink/netlink_send.hh b/watchlink/netlink_send.hh
deleted file mode 100644
index 3e13e5c8..00000000
--- a/watchlink/netlink_send.hh
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * Module: netlink_send.hh
- *
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 as published
- * by the Free Software Foundation.
- */
-#ifndef __NETLINK_SEND_HH__
-#define __NETLINK_SEND_HH__
-
-class NetlinkSend
-{
-public:
- NetlinkSend(bool debug);
- ~NetlinkSend();
-
- int
- send_get(int sock, int type, int ifindex = -1);
-
- int
- send_set(int sock, int ifindex, uint32_t local_addr, uint32_t addr, int mask_len, int type);
-
- int
- send_set_route(int sock, int ifindex, uint32_t local_addr, uint32_t dst_addr, int mask_len, int type, int table, int rtn_type, int rt_scope);
-
-private:
- int
- addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int alen);
-
- int
- addattr32(struct nlmsghdr *n, int maxlen, int type, int data);
-
-private:
- bool _debug;
-
-};
-
-#endif //__NETLINK_SEND_HH__
diff --git a/watchlink/netlink_utils.cc b/watchlink/netlink_utils.cc
deleted file mode 100644
index 18a4b3ff..00000000
--- a/watchlink/netlink_utils.cc
+++ /dev/null
@@ -1,59 +0,0 @@
-#include <errno.h>
-#include <string.h>
-#include <arpa/inet.h>
-#include <linux/types.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <linux/rtnetlink.h>
-#include <syslog.h>
-
-#include "netlink_utils.hh"
-
-
-/* Maskbit. */
-static u_char maskbit[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
- 0xf8, 0xfc, 0xfe, 0xff};
-
-/* Convert masklen into IP address's netmask. */
-void
-masklen2ip (int masklen, struct in_addr *netmask)
-{
- u_char *pnt;
- int bit;
- int offset;
-
- memset (netmask, 0, sizeof (struct in_addr));
- pnt = (unsigned char *) netmask;
-
- offset = masklen / 8;
- bit = masklen % 8;
-
- while (offset--)
- *pnt++ = 0xff;
-
- if (bit)
- *pnt = maskbit[bit];
-}
-
-
-in_addr_t
-ipv4_broadcast_addr (in_addr_t hostaddr, int masklen)
-{
- struct in_addr mask;
-
- masklen2ip (masklen, &mask);
- return (masklen != 32-1) ?
- /* normal case */
- (hostaddr | ~mask.s_addr) :
- /* special case for /31 */
- (hostaddr ^ ~mask.s_addr);
-}
-
-in_addr_t
-ipv4_first_addr (in_addr_t hostaddr, int masklen)
-{
- struct in_addr mask;
- masklen2ip (masklen, &mask);
- return (hostaddr & mask.s_addr);
-}
-
diff --git a/watchlink/netlink_utils.hh b/watchlink/netlink_utils.hh
deleted file mode 100644
index a1be920a..00000000
--- a/watchlink/netlink_utils.hh
+++ /dev/null
@@ -1,20 +0,0 @@
-#ifndef __NETLINK_UTILS_HH__
-#define __NETLINK_UTILS_HH__
-
-#include <arpa/inet.h>
-#include <linux/types.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-
-
-in_addr_t
-ipv4_broadcast_addr (in_addr_t hostaddr, int masklen);
-
-void
-masklen2ip (int masklen, struct in_addr *netmask);
-
-in_addr_t
-ipv4_first_addr (in_addr_t hostaddr, int masklen);
-
-
-#endif //__NETLINK_UTILS_HH__
diff --git a/watchlink/rl_str_proc.cc b/watchlink/rl_str_proc.cc
deleted file mode 100644
index e3c67e82..00000000
--- a/watchlink/rl_str_proc.cc
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * Module: rl_str_proc.cc
- *
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 as published
- * by the Free Software Foundation.
- */
-#include "rl_str_proc.hh"
-
-using namespace std;
-
-/**
- *
- **/
-StrProc::StrProc(const string &in_str, const string &token)
-{
- string tmp = in_str;
-
- //convert tabs to spaces
- uint32_t pos = 0;
- string tabtospace = " ";
- string::iterator iter = tmp.begin();
- while ((pos = tmp.find("\t", pos)) != string::npos) {
- tmp.replace(pos, 1, tabtospace);
- pos += tabtospace.length();
- }
-
- //remove the cr
- pos = tmp.find("\n");
- if (pos != string::npos) {
- tmp.replace(pos, 1, "");
- }
-
- //now handle the case of the multiple length token
- //note that we are using the '~' as a token internally
- uint32_t start = 0, end;
- while ((start = tmp.find(token, start)) != string::npos) {
- tmp.replace(start, token.length(), "~");
- }
-
-
- while ((start = tmp.find_first_not_of("~")) != string::npos) {
- tmp = tmp.substr(start, tmp.length() - start);
- end = tmp.find_first_of("~");
- _str_coll.push_back(tmp.substr(0, end));
- tmp = tmp.substr(end+1, tmp.length() - end-1);
- if (end == string::npos) {
- break;
- }
- }
-}
-
-/**
- *
- **/
-string
-StrProc::get(int i)
-{
- if (uint32_t(i) >= _str_coll.size()) {
- return string("");
- }
- return _str_coll[i];
-}
-
-/**
- *
- **/
-string
-StrProc::get(int start, int end)
-{
- if (uint32_t(start) >= _str_coll.size()) {
- return string("");
- }
-
- string tmp;
- for (int i = start; (i < end) && (uint32_t(i) < _str_coll.size()); ++i) {
- tmp += _str_coll[i] + " ";
- }
- return tmp.substr(0,tmp.length()-1);
-}
-
-/**
- *
- **/
-vector<string>
-StrProc::get()
-{
- return _str_coll;
-}
diff --git a/watchlink/rl_str_proc.hh b/watchlink/rl_str_proc.hh
deleted file mode 100644
index ad7d897f..00000000
--- a/watchlink/rl_str_proc.hh
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * Module: rl_str_proc.hh
- *
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 as published
- * by the Free Software Foundation.
- */
-#ifndef __RL_STR_PROC_HH__
-#define __RL_STR_PROC_HH__
-
-#include <vector>
-#include <string>
-
-class StrProc
-{
-public:
- StrProc(const std::string &in, const std::string &token);
-
- std::string get(int i);
-
- std::string get(int start, int end);
-
- std::vector<std::string> get();
-
- int size() {return _str_coll.size();}
-
-private:
- std::vector<std::string> _str_coll;
-};
-
-#endif //__RL_STR_PROC_HH__
diff --git a/watchlink/watchlink.cc b/watchlink/watchlink.cc
deleted file mode 100644
index 0cc8f232..00000000
--- a/watchlink/watchlink.cc
+++ /dev/null
@@ -1,293 +0,0 @@
-/*
- * Module: watchlink.cc
- *
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 as published
- * by the Free Software Foundation.
- */
-#include <unistd.h>
-#include <iostream>
-#include <stdio.h>
-#include <string>
-#include <map>
-#include <fcntl.h>
-#include <syslog.h>
-#include <errno.h>
-
-#include <linux/types.h>
-#include <sys/socket.h>
-#include <signal.h>
-#include <linux/rtnetlink.h>
-#include "rl_str_proc.hh"
-#include "netlink_send.hh"
-#include "netlink_listener.hh"
-#include "netlink_linkstatus.hh"
-#include "netlink_event.hh"
-
-using namespace std;
-
-
-void print_netlink(NetlinkEvent &nl_event);
-pid_t pid_output (const char *path);
-
-struct option longopts[] =
-{
- { 0 }
-};
-
-multimap<string,IPv4net> g_exclude;
-string g_link_dir = "/var/linkstatus";
-
-/**
- *
- *
- **/
-void
-usage()
-{
- fprintf(stdout, "-s start with sending netlink request\n");
- fprintf(stdout, "-l specify directory for link status. default /var/linkstatus\n");
- fprintf(stdout, "-i specify location of pid directory\n");
- fprintf(stdout, "-p print netlink messages\n");
- fprintf(stdout, "-d run as a daemon\n");
- fprintf(stdout, "-v debug to stdout\n");
- fprintf(stdout, "-h help message\n");
-}
-
-/**
- *
- *
- **/
-multimap<string,IPv4net>
-load_exclusion_file(const string &link_dir)
-{
- multimap<string,IPv4net> coll;
-
- string file = link_dir + "/exclude";
- FILE *fp = fopen(file.c_str(), "r");
- if (fp == NULL) {
- syslog(LOG_INFO,"load_exclusion_file(), failed to open state file");
- // cerr << "load_exclusion_file(), failed to open state file" << endl;
- return coll; //means we are still up, ignore...
- }
-
- char str[1025];
- while (fgets(str, 1024, fp)) {
- string line(str);
-
- //strip out comments
- int pos = line.find("#");
- line = line.substr(0,pos);
-
- StrProc tokens(line, " ");
- if (tokens.size() == 1) {
- string any("0/0");
- coll.insert(pair<string,IPv4net>(tokens.get(0),IPv4net(any)));
- }
- else if (tokens.size() == 2) {
- string net(tokens.get(1));
- coll.insert(pair<string,IPv4net>(tokens.get(0),IPv4net(net)));
- }
- }
- fclose(fp);
- return coll;
-}
-
-/**
- *
- *
- **/
-static void
-sig_user(int signo)
-{
- //reload interface exclusion list
- g_exclude = load_exclusion_file(g_link_dir);
-}
-
-
-/**
- *
- *
- **/
-int
-main(int argc, char* const argv[])
-{
- int ch;
- bool send_request = false;
- bool debug = false;
- bool daemon = false;
- string pid_path;
- bool print_nl_msg = false;
-
- if (debug) {
- cout << "starting..." << endl;
- }
-
- while ((ch = getopt_long(argc, argv, "sl:i:dvhp",longopts,0)) != -1) {
- switch (ch) {
- case 's':
- send_request = true;
- break;
- case 'l':
- g_link_dir = optarg;
- break;
- case 'i':
- pid_path = optarg;
- break;
- case 'd':
- daemon = true;
- break;
- case 'v':
- debug = true;
- break;
- case 'p':
- print_nl_msg = true;
- break;
- case 'h':
- usage();
- exit(0);
- }
- }
-
- NetlinkSend nl_send(debug);
- NetlinkListener nl_listener(debug);
-
- //add check here to ensure only one watchlink process
-
- if (daemon) {
- if (fork() != 0) {
- exit(0);
- }
- }
-
- if (pid_path.empty() == false) {
- pid_output(pid_path.c_str());
- }
-
- //load interface exclusion list
- g_exclude = load_exclusion_file(g_link_dir);
-
- signal(SIGUSR1, sig_user);
-
- int sock = nl_listener.init();
- if (sock <= 0) {
- syslog(LOG_ERR, "watchlink(), netlink listener failed in initialization. exiting..");
- cerr << "watchlink(), netlink listener failed in initialization. exiting.." << endl;
- exit(1);
- }
-
- if (send_request) {
- if (debug) {
- cout << "sending initial netlink request" << endl;
- }
- nl_listener.set_multipart(true);
- if (nl_send.send_get(sock, RTM_GETLINK) != 0) {
- syslog(LOG_ERR,"watchlink(), error sending, exiting..");
- cerr << "watchlink(), error sending. exiting.." << endl;
- exit(1);
- }
- }
-
- NetlinkLinkStatus nl_ls(sock, g_link_dir, debug);
-
- while (true) {
- NetlinkEvent nl_event;
- if (nl_listener.process(nl_event, g_exclude) == true) {
- if (send_request) {
- if (nl_send.send_get(sock, RTM_GETADDR) != 0) {
- syslog(LOG_ERR,"watchlink(), error sending. exiting..");
- cerr << "watchlink(), error sending. exiting.." << endl;
- exit(1);
- }
- send_request = false;
- }
- else {
- nl_listener.set_multipart(false);
- }
-
- nl_ls.process(nl_event);
-
- if (debug) {
- cout << " ifinfomsg: " << nl_event.get_ifinfomsg() << endl;
- }
- if (print_nl_msg) {
- print_netlink(nl_event);
- }
-
- }
- else {
- // cout << "didn't receive a message, sleeping for 1 second" << endl;
- sleep(1);
- }
- }
-
- exit(0);
-}
-
-
-/**
- *
- *
- **/
-void
-print_netlink(NetlinkEvent &nl_event)
-{
- char buf[20];
- sprintf(buf, "%d", nl_event.get_index());
- cout << "results for " << nl_event.get_iface() << "(" << string(buf) << ")" << endl;
- cout << " running: " << string(nl_event.get_running() ? "yes" : "no") << endl;
- cout << " enabled: " << string(nl_event.get_enabled() ? "yes" : "no") << endl;
- if (nl_event.get_type() == RTM_DELLINK ||
- nl_event.get_type() == RTM_NEWLINK) {
- cout << " type: " << string(nl_event.get_type()==RTM_DELLINK?"DELLINK":"NEWLINK") << endl;
- cout << " state: " << string(nl_event.is_link_up()?"UP":"DOWN") << endl;
- sprintf(buf, "%d", nl_event.get_mtu());
- cout << " mtu: " << string(buf) << endl;
- cout << " mac: " << nl_event.get_mac_str() << endl;
- cout << " alternate mac: " << nl_event.get_mac_str() << endl;
- }
- else if (nl_event.get_type() == RTM_DELADDR ||
- nl_event.get_type() == RTM_NEWADDR) {
- cout << " type: " << string(nl_event.get_type()==RTM_DELADDR?"DELADDR":"NEWADDR") << endl;
- cout << " local addr: " << nl_event.get_local_addr().str().c_str() << endl;
- cout << " addr: " << nl_event.get_addr().str().c_str() << endl;
- cout << " broadcast: " << nl_event.get_broadcast().str().c_str() << endl;
- char buf[20];
- sprintf(buf, "%d", nl_event.get_mask_len());
- cout << " mask length: " << string(buf) << endl;
- }
- cout << endl;
-
-}
-
-/**
- *
- *below borrowed from quagga library.
- **/
-#define PIDFILE_MASK 0644
-pid_t
-pid_output (const char *path)
-{
- FILE *fp;
- pid_t pid;
- mode_t oldumask;
-
- pid = getpid();
-
- oldumask = umask(0777 & ~PIDFILE_MASK);
- fp = fopen (path, "w");
- if (fp != NULL)
- {
- fprintf (fp, "%d\n", (int) pid);
- fclose (fp);
- umask(oldumask);
- return pid;
- }
- /* XXX Why do we continue instead of exiting? This seems incompatible
- with the behavior of the fcntl version below. */
- syslog(LOG_ERR,"Can't fopen pid lock file %s, continuing",
- path);
- umask(oldumask);
- return -1;
-}