Skip to content

Commit d275d1e

Browse files
committed
Add TcpAddress class and use it handle ip/port of src and dest
1 parent 9d81603 commit d275d1e

5 files changed

Lines changed: 52 additions & 8 deletions

File tree

src/commparty.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
#endif /* HAVE_CONFIG_H */
44

55
#include "commparty.h"
6+
#include "tcp.h"
67
#include "websocket.h"
78

8-
#include <arpa/inet.h>
99
#include <map>
1010

1111
CommunicationParty::CommunicationParty() : mName(""), mIpAddress("") {
@@ -67,8 +67,8 @@ CommunicationPartyManager::getParty(string ipAddress) {
6767
}
6868

6969
CommunicationParty*
70-
CommunicationPartyManager::getParty(const struct in_addr& addr) {
71-
string localHostname = inet_ntoa((struct in_addr) addr);
70+
CommunicationPartyManager::getParty(const TcpAddress& addr) {
71+
string localHostname = addr.getHostname();
7272
CommunicationParty* party = CommunicationPartyManager::getParty(localHostname);
7373
return party;
7474
}

src/commparty.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
using namespace std;
99

10+
class TcpAddress;
11+
1012
class CommunicationParty {
1113
public:
1214
CommunicationParty();
@@ -35,7 +37,7 @@ class CommunicationPartyManager {
3537
static void cleanup();
3638

3739
static CommunicationParty* getParty(string ipAddress);
38-
static CommunicationParty* getParty(const struct in_addr& addr);
40+
static CommunicationParty* getParty(const TcpAddress& addr);
3941
};
4042

4143
#endif /* __COMMUNICATION_PARTY_H__ */

src/main.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,14 +233,15 @@ void handleTcpPacket(struct timeval tv, const RawIpPacket* ip, const RawTcpPacke
233233

234234
bool isIncoming = isIncomingIpPacket(ip);
235235
const char* tcpData = ((const char*) tcp) + tcp->th_off * 4;
236-
const struct in_addr ipAddr = isIncoming ? ip->ip_dst : ip->ip_src;
237-
CommunicationParty* party = CommunicationPartyManager::getParty(ipAddr);
236+
TcpAddress src(ip->ip_src, ntohs(tcp->th_sport));
237+
TcpAddress dest(ip->ip_dst, ntohs(tcp->th_dport));
238+
CommunicationParty* party = CommunicationPartyManager::getParty(isIncoming ? dest : src);
238239
string partyName = party->getName();
239240

240-
if (isWebSocketPort(ntohs(tcp->th_sport)) || isWebSocketPort(ntohs(tcp->th_dport))) {
241+
if (isWebSocketPort(src.getPort()) || isWebSocketPort(dest.getPort())) {
241242
WebSocketParser* parser = isIncoming ? party->getWebSocketParserIncoming() : party->getWebSocketParserOutgoing();
242243
handleWebsocketNotification(partyName, isIncoming, tv, parser, tcpData, tcpDataLen);
243-
} else if (isHttpPort(ntohs(tcp->th_sport)) || isHttpPort(ntohs(tcp->th_dport))){
244+
} else if (isHttpPort(src.getPort()) || isHttpPort(dest.getPort())) {
244245
printPacketInfo(partyName, isIncoming, tv);
245246

246247
if (isIncoming) {

src/tcp.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include "tcp.h"
66

7+
#include <arpa/inet.h>
8+
79
IPv4::IPv4(const struct in_addr& addr) {
810
mAddress = (const struct in_addr) addr;
911
}
@@ -38,3 +40,22 @@ Netmask::matches(const IPv4& ip) const {
3840
uint32_t networkPacket = ntohl(ip.getAddress().s_addr) & mask;
3941
return networkFilter == networkPacket;
4042
}
43+
44+
TcpAddress::TcpAddress(const struct in_addr& addr, unsigned short port) {
45+
string hostname = inet_ntoa((struct in_addr) addr);
46+
mHostname = hostname;
47+
mPort = port;
48+
}
49+
50+
TcpAddress::~TcpAddress() {
51+
}
52+
53+
string
54+
TcpAddress::getHostname() const {
55+
return mHostname;
56+
}
57+
58+
unsigned short
59+
TcpAddress::getPort() const {
60+
return mPort;
61+
}

src/tcp.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
#include <netinet/in.h>
55

6+
#include <list>
7+
#include <string>
8+
9+
using namespace std;
10+
611
typedef u_int32_t tcp_seq;
712

813
struct nread_ip {
@@ -54,6 +59,8 @@ typedef struct ether_header RawEtherPacket;
5459
typedef struct nread_ip RawIpPacket;
5560
typedef struct nread_tcp RawTcpPacket;
5661

62+
class Buffer;
63+
5764
class IPv4 {
5865
public:
5966
IPv4(const struct in_addr& addr);
@@ -79,4 +86,17 @@ class Netmask {
7986
unsigned short mNetbits;
8087
};
8188

89+
class TcpAddress {
90+
public:
91+
TcpAddress(const struct in_addr& addr, unsigned short port);
92+
virtual ~TcpAddress();
93+
94+
string getHostname() const;
95+
unsigned short getPort() const;
96+
97+
private:
98+
string mHostname;
99+
unsigned short mPort;
100+
};
101+
82102
#endif /* __TCP_H__ */

0 commit comments

Comments
 (0)