Skip to content

Commit 75ae6c5

Browse files
committed
Use pointers again
1 parent 8347cd9 commit 75ae6c5

4 files changed

Lines changed: 37 additions & 23 deletions

File tree

src/commparty.cc

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,43 +32,54 @@ CommunicationParty::getIpAddress() {
3232
return mIpAddress;
3333
}
3434

35-
WebSocketParser&
35+
WebSocketParser*
3636
CommunicationParty::getWebSocketParserIncoming() {
37-
return mWsIncoming;
37+
return &mWsIncoming;
3838
}
3939

40-
WebSocketParser&
40+
WebSocketParser*
4141
CommunicationParty::getWebSocketParserOutgoing() {
42-
return mWsOutgoing;
42+
return &mWsOutgoing;
4343
}
4444

45-
static map<string, CommunicationParty> parties;
45+
static map<string, CommunicationParty*> parties;
4646

47-
CommunicationParty
47+
CommunicationParty*
4848
CommunicationParty::newParty(string ipAddress) {
4949
string name;
5050
name = 'A' + parties.size();
5151

52-
CommunicationParty party(name, ipAddress);
52+
CommunicationParty* party = new CommunicationParty(name, ipAddress);
5353
return party;
5454
}
5555

56-
CommunicationParty
56+
CommunicationParty*
5757
CommunicationPartyManager::getParty(string ipAddress) {
58-
map<string, CommunicationParty>::iterator it = parties.find(ipAddress);
58+
map<string, CommunicationParty*>::iterator it = parties.find(ipAddress);
5959

6060
if (it != parties.end()) {
6161
return it->second;
6262
}
6363

64-
CommunicationParty party = CommunicationParty::newParty(ipAddress);
64+
CommunicationParty* party = CommunicationParty::newParty(ipAddress);
6565
parties[ipAddress] = party;
6666
return party;
6767
}
6868

69-
CommunicationParty
69+
CommunicationParty*
7070
CommunicationPartyManager::getParty(const struct in_addr& addr) {
7171
string localHostname = inet_ntoa((struct in_addr) addr);
72-
CommunicationParty party = CommunicationPartyManager::getParty(localHostname);
72+
CommunicationParty* party = CommunicationPartyManager::getParty(localHostname);
7373
return party;
7474
}
75+
76+
void
77+
CommunicationPartyManager::cleanup() {
78+
map<string, CommunicationParty*>::iterator it;
79+
80+
for (it = parties.begin(); it != parties.end(); ++it) {
81+
delete it->second;
82+
}
83+
84+
parties.clear();
85+
}

src/commparty.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ class CommunicationParty {
1818

1919
string getIpAddress();
2020

21-
WebSocketParser& getWebSocketParserIncoming();
22-
WebSocketParser& getWebSocketParserOutgoing();
21+
WebSocketParser* getWebSocketParserIncoming();
22+
WebSocketParser* getWebSocketParserOutgoing();
2323

24-
static CommunicationParty newParty(string ipAddress);
24+
static CommunicationParty* newParty(string ipAddress);
2525

2626
private:
2727
string mName;
@@ -32,8 +32,10 @@ class CommunicationParty {
3232

3333
class CommunicationPartyManager {
3434
public:
35-
static CommunicationParty getParty(string ipAddress);
36-
static CommunicationParty getParty(const struct in_addr& addr);
35+
static void cleanup();
36+
37+
static CommunicationParty* getParty(string ipAddress);
38+
static CommunicationParty* getParty(const struct in_addr& addr);
3739
};
3840

3941
#endif /* __COMMUNICATION_PARTY_H__ */

src/main.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,11 @@ void printPacketInfo(string partyName, bool isIncoming, struct timeval tv) {
175175
printTimestamp(tv);
176176
}
177177

178-
void handleWebsocketNotification(string partyName, bool isIncoming, struct timeval tv, WebSocketParser& ws, const char* data, uint16_t len) {
178+
void handleWebsocketNotification(string partyName, bool isIncoming, struct timeval tv, WebSocketParser* ws, const char* data, uint16_t len) {
179179
WebSocketFrame* frame;
180-
ws.addStreamData(data, len);
180+
ws->addStreamData(data, len);
181181

182-
while ((frame = ws.getNextFrame()) != nullptr) {
182+
while ((frame = ws->getNextFrame()) != nullptr) {
183183
printPacketInfo(partyName, isIncoming, tv);
184184
printf("ws %s\n", frame->getSubject().c_str());
185185

@@ -233,11 +233,11 @@ void handleTcpPacket(struct timeval tv, const RawIpPacket* ip, const RawTcpPacke
233233
bool isIncoming = isIncomingIpPacket(ip);
234234
const char* tcpData = ((const char*) tcp) + tcp->th_off * 4;
235235
const struct in_addr ipAddr = isIncoming ? ip->ip_dst : ip->ip_src;
236-
CommunicationParty party = CommunicationPartyManager::getParty(ipAddr);
237-
string partyName = party.getName();
236+
CommunicationParty* party = CommunicationPartyManager::getParty(ipAddr);
237+
string partyName = party->getName();
238238

239239
if (isWebSocketPort(ntohs(tcp->th_sport)) || isWebSocketPort(ntohs(tcp->th_dport))) {
240-
WebSocketParser parser = isIncoming ? party.getWebSocketParserIncoming() : party.getWebSocketParserOutgoing();
240+
WebSocketParser* parser = isIncoming ? party->getWebSocketParserIncoming() : party->getWebSocketParserOutgoing();
241241
handleWebsocketNotification(partyName, isIncoming, tv, parser, tcpData, tcpDataLen);
242242
} else if (isHttpPort(ntohs(tcp->th_sport)) || isHttpPort(ntohs(tcp->th_dport))){
243243
printPacketInfo(partyName, isIncoming, tv);
@@ -292,5 +292,6 @@ int main(int argc, char** argv) {
292292
handlePcapFile(*it);
293293
}
294294

295+
CommunicationPartyManager::cleanup();
295296
return 0;
296297
}

tests/voipInvite.pcap

45.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)