Use clang-format to format (somewhat) LLVM-style (fixes #44)
This commit is contained in:
314
connection.cpp
314
connection.cpp
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
* connection.cpp
|
||||
*
|
||||
* Copyright (c) 2004-2006,2008 Arnout Engelen
|
||||
@@ -15,224 +15,198 @@
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
||||
*USA.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
#ifdef __APPLE__
|
||||
#include <sys/malloc.h>
|
||||
#include <sys/malloc.h>
|
||||
#else
|
||||
#include <malloc.h>
|
||||
#include <malloc.h>
|
||||
#endif
|
||||
#include "nethogs.h"
|
||||
#include "connection.h"
|
||||
#include "process.h"
|
||||
|
||||
ConnList * connections = NULL;
|
||||
ConnList *connections = NULL;
|
||||
|
||||
void PackList::add (Packet * p)
|
||||
{
|
||||
if (content == NULL)
|
||||
{
|
||||
content = new PackListNode (new Packet (*p));
|
||||
return;
|
||||
}
|
||||
void PackList::add(Packet *p) {
|
||||
if (content == NULL) {
|
||||
content = new PackListNode(new Packet(*p));
|
||||
return;
|
||||
}
|
||||
|
||||
if (content->val->time.tv_sec == p->time.tv_sec)
|
||||
{
|
||||
content->val->len += p->len;
|
||||
return;
|
||||
}
|
||||
if (content->val->time.tv_sec == p->time.tv_sec) {
|
||||
content->val->len += p->len;
|
||||
return;
|
||||
}
|
||||
|
||||
/* store copy of packet, so that original may be freed */
|
||||
content = new PackListNode(new Packet (*p), content);
|
||||
/* store copy of packet, so that original may be freed */
|
||||
content = new PackListNode(new Packet(*p), content);
|
||||
}
|
||||
|
||||
/* sums up the total bytes used and removes 'old' packets */
|
||||
u_int32_t PackList::sumanddel (timeval t)
|
||||
{
|
||||
u_int32_t retval = 0;
|
||||
PackListNode * current = content;
|
||||
PackListNode * previous = NULL;
|
||||
u_int32_t PackList::sumanddel(timeval t) {
|
||||
u_int32_t retval = 0;
|
||||
PackListNode *current = content;
|
||||
PackListNode *previous = NULL;
|
||||
|
||||
while (current != NULL)
|
||||
{
|
||||
//std::cout << "Comparing " << current->val->time.tv_sec << " <= " << t.tv_sec - PERIOD << endl;
|
||||
if (current->val->time.tv_sec <= t.tv_sec - PERIOD)
|
||||
{
|
||||
if (current == content)
|
||||
content = NULL;
|
||||
else if (previous != NULL)
|
||||
previous->next = NULL;
|
||||
delete current;
|
||||
return retval;
|
||||
}
|
||||
retval += current->val->len;
|
||||
previous = current;
|
||||
current = current->next;
|
||||
}
|
||||
return retval;
|
||||
while (current != NULL) {
|
||||
// std::cout << "Comparing " << current->val->time.tv_sec << " <= " <<
|
||||
// t.tv_sec - PERIOD << endl;
|
||||
if (current->val->time.tv_sec <= t.tv_sec - PERIOD) {
|
||||
if (current == content)
|
||||
content = NULL;
|
||||
else if (previous != NULL)
|
||||
previous->next = NULL;
|
||||
delete current;
|
||||
return retval;
|
||||
}
|
||||
retval += current->val->len;
|
||||
previous = current;
|
||||
current = current->next;
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
/* packet may be deleted by caller */
|
||||
Connection::Connection (Packet * packet)
|
||||
{
|
||||
assert (packet != NULL);
|
||||
connections = new ConnList (this, connections);
|
||||
sent_packets = new PackList ();
|
||||
recv_packets = new PackList ();
|
||||
sumSent = 0;
|
||||
sumRecv = 0;
|
||||
if (DEBUG)
|
||||
{
|
||||
std::cout << "New connection, with package len " << packet->len << std::endl;
|
||||
}
|
||||
if (packet->Outgoing())
|
||||
{
|
||||
sumSent += packet->len;
|
||||
sent_packets->add(packet);
|
||||
refpacket = new Packet (*packet);
|
||||
} else {
|
||||
sumRecv += packet->len;
|
||||
recv_packets->add(packet);
|
||||
refpacket = packet->newInverted();
|
||||
}
|
||||
lastpacket = packet->time.tv_sec;
|
||||
if (DEBUG)
|
||||
std::cout << "New reference packet created at " << refpacket << std::endl;
|
||||
Connection::Connection(Packet *packet) {
|
||||
assert(packet != NULL);
|
||||
connections = new ConnList(this, connections);
|
||||
sent_packets = new PackList();
|
||||
recv_packets = new PackList();
|
||||
sumSent = 0;
|
||||
sumRecv = 0;
|
||||
if (DEBUG) {
|
||||
std::cout << "New connection, with package len " << packet->len
|
||||
<< std::endl;
|
||||
}
|
||||
if (packet->Outgoing()) {
|
||||
sumSent += packet->len;
|
||||
sent_packets->add(packet);
|
||||
refpacket = new Packet(*packet);
|
||||
} else {
|
||||
sumRecv += packet->len;
|
||||
recv_packets->add(packet);
|
||||
refpacket = packet->newInverted();
|
||||
}
|
||||
lastpacket = packet->time.tv_sec;
|
||||
if (DEBUG)
|
||||
std::cout << "New reference packet created at " << refpacket << std::endl;
|
||||
}
|
||||
|
||||
Connection::~Connection ()
|
||||
{
|
||||
if (DEBUG)
|
||||
std::cout << "Deleting connection" << std::endl;
|
||||
/* refpacket is not a pointer to one of the packets in the lists
|
||||
* so deleted */
|
||||
delete (refpacket);
|
||||
if (sent_packets != NULL)
|
||||
delete sent_packets;
|
||||
if (recv_packets != NULL)
|
||||
delete recv_packets;
|
||||
Connection::~Connection() {
|
||||
if (DEBUG)
|
||||
std::cout << "Deleting connection" << std::endl;
|
||||
/* refpacket is not a pointer to one of the packets in the lists
|
||||
* so deleted */
|
||||
delete (refpacket);
|
||||
if (sent_packets != NULL)
|
||||
delete sent_packets;
|
||||
if (recv_packets != NULL)
|
||||
delete recv_packets;
|
||||
|
||||
ConnList * curr_conn = connections;
|
||||
ConnList * prev_conn = NULL;
|
||||
while (curr_conn != NULL)
|
||||
{
|
||||
if (curr_conn->getVal() == this)
|
||||
{
|
||||
ConnList * todelete = curr_conn;
|
||||
curr_conn = curr_conn->getNext();
|
||||
if (prev_conn == NULL)
|
||||
{
|
||||
connections = curr_conn;
|
||||
} else {
|
||||
prev_conn->setNext(curr_conn);
|
||||
}
|
||||
delete (todelete);
|
||||
}
|
||||
else
|
||||
{
|
||||
prev_conn = curr_conn;
|
||||
curr_conn = curr_conn->getNext();
|
||||
}
|
||||
}
|
||||
ConnList *curr_conn = connections;
|
||||
ConnList *prev_conn = NULL;
|
||||
while (curr_conn != NULL) {
|
||||
if (curr_conn->getVal() == this) {
|
||||
ConnList *todelete = curr_conn;
|
||||
curr_conn = curr_conn->getNext();
|
||||
if (prev_conn == NULL) {
|
||||
connections = curr_conn;
|
||||
} else {
|
||||
prev_conn->setNext(curr_conn);
|
||||
}
|
||||
delete (todelete);
|
||||
} else {
|
||||
prev_conn = curr_conn;
|
||||
curr_conn = curr_conn->getNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* the packet will be freed by the calling code */
|
||||
void Connection::add (Packet * packet)
|
||||
{
|
||||
lastpacket = packet->time.tv_sec;
|
||||
if (packet->Outgoing())
|
||||
{
|
||||
if (DEBUG)
|
||||
{
|
||||
std::cout << "Outgoing: " << packet->len << std::endl;
|
||||
}
|
||||
sumSent += packet->len;
|
||||
sent_packets->add (packet);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (DEBUG)
|
||||
{
|
||||
std::cout << "Incoming: " << packet->len << std::endl;
|
||||
}
|
||||
sumRecv += packet->len;
|
||||
if (DEBUG)
|
||||
{
|
||||
std::cout << "sumRecv now: " << sumRecv << std::endl;
|
||||
}
|
||||
recv_packets->add (packet);
|
||||
}
|
||||
void Connection::add(Packet *packet) {
|
||||
lastpacket = packet->time.tv_sec;
|
||||
if (packet->Outgoing()) {
|
||||
if (DEBUG) {
|
||||
std::cout << "Outgoing: " << packet->len << std::endl;
|
||||
}
|
||||
sumSent += packet->len;
|
||||
sent_packets->add(packet);
|
||||
} else {
|
||||
if (DEBUG) {
|
||||
std::cout << "Incoming: " << packet->len << std::endl;
|
||||
}
|
||||
sumRecv += packet->len;
|
||||
if (DEBUG) {
|
||||
std::cout << "sumRecv now: " << sumRecv << std::endl;
|
||||
}
|
||||
recv_packets->add(packet);
|
||||
}
|
||||
}
|
||||
|
||||
Connection * findConnectionWithMatchingSource(Packet * packet) {
|
||||
assert(packet->Outgoing());
|
||||
Connection *findConnectionWithMatchingSource(Packet *packet) {
|
||||
assert(packet->Outgoing());
|
||||
|
||||
ConnList * current = connections;
|
||||
while (current != NULL)
|
||||
{
|
||||
/* the reference packet is always outgoing */
|
||||
if (packet->matchSource(current->getVal()->refpacket))
|
||||
{
|
||||
return current->getVal();
|
||||
}
|
||||
ConnList *current = connections;
|
||||
while (current != NULL) {
|
||||
/* the reference packet is always outgoing */
|
||||
if (packet->matchSource(current->getVal()->refpacket)) {
|
||||
return current->getVal();
|
||||
}
|
||||
|
||||
current = current->getNext();
|
||||
}
|
||||
return NULL;
|
||||
current = current->getNext();
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Connection * findConnectionWithMatchingRefpacketOrSource(Packet * packet) {
|
||||
ConnList * current = connections;
|
||||
while (current != NULL)
|
||||
{
|
||||
/* the reference packet is always *outgoing* */
|
||||
if (packet->match(current->getVal()->refpacket))
|
||||
{
|
||||
return current->getVal();
|
||||
}
|
||||
Connection *findConnectionWithMatchingRefpacketOrSource(Packet *packet) {
|
||||
ConnList *current = connections;
|
||||
while (current != NULL) {
|
||||
/* the reference packet is always *outgoing* */
|
||||
if (packet->match(current->getVal()->refpacket)) {
|
||||
return current->getVal();
|
||||
}
|
||||
|
||||
current = current->getNext();
|
||||
}
|
||||
return findConnectionWithMatchingSource(packet);
|
||||
current = current->getNext();
|
||||
}
|
||||
return findConnectionWithMatchingSource(packet);
|
||||
}
|
||||
|
||||
/*
|
||||
/*
|
||||
* finds connection to which this packet belongs.
|
||||
* a packet belongs to a connection if it matches
|
||||
* to its reference packet
|
||||
* to its reference packet
|
||||
*/
|
||||
Connection * findConnection (Packet * packet)
|
||||
{
|
||||
if (packet->Outgoing())
|
||||
return findConnectionWithMatchingRefpacketOrSource(packet);
|
||||
else
|
||||
{
|
||||
Packet * invertedPacket = packet->newInverted();
|
||||
Connection * result = findConnectionWithMatchingRefpacketOrSource(invertedPacket);
|
||||
Connection *findConnection(Packet *packet) {
|
||||
if (packet->Outgoing())
|
||||
return findConnectionWithMatchingRefpacketOrSource(packet);
|
||||
else {
|
||||
Packet *invertedPacket = packet->newInverted();
|
||||
Connection *result =
|
||||
findConnectionWithMatchingRefpacketOrSource(invertedPacket);
|
||||
|
||||
delete invertedPacket;
|
||||
return result;
|
||||
}
|
||||
delete invertedPacket;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Connection::sumanddel
|
||||
*
|
||||
*
|
||||
* sums up the total bytes used
|
||||
* and removes 'old' packets.
|
||||
* and removes 'old' packets.
|
||||
*
|
||||
* Returns sum of sent packages (by address)
|
||||
* sum of recieved packages (by address)
|
||||
*/
|
||||
void Connection::sumanddel (timeval t, u_int32_t * recv, u_int32_t * sent)
|
||||
{
|
||||
(*sent)=(*recv)=0;
|
||||
void Connection::sumanddel(timeval t, u_int32_t *recv, u_int32_t *sent) {
|
||||
(*sent) = (*recv) = 0;
|
||||
|
||||
*sent = sent_packets->sumanddel(t);
|
||||
*recv = recv_packets->sumanddel(t);
|
||||
*sent = sent_packets->sumanddel(t);
|
||||
*recv = recv_packets->sumanddel(t);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user