5 // Created by Terrin Eager on 3/17/13.
15 #include "CollectBy.h"
18 //////////////////////
20 void Collection::Init(BJ_COLLECTBY_TYPE collectByList
[])
22 CollectByAbstract
* pLastCollectBy
= nullptr;
24 for (int i
=0; i
<20 && collectByList
[i
] != CBT_NOT_SET
;i
++)
26 m_CollectByList
[i
] = collectByList
[i
];
29 m_pHeaderCollectBy
= Factory(m_CollectByList
[i
]);
30 pLastCollectBy
= m_pHeaderCollectBy
;
31 m_pFirstCollectBy
= pLastCollectBy
->Factory();
35 pLastCollectBy
->pNext
= Factory(m_CollectByList
[i
]);
36 pLastCollectBy
= pLastCollectBy
->pNext
;
41 void Collection::ProcessFrame(CDNSFrame
* pFrame
)
43 m_pFirstCollectBy
->Collect(pFrame
,m_pHeaderCollectBy
->pNext
);
46 void Collection::ExportCollection(BJString sFileName
)
48 FILE* hFile
= fopen(sFileName
.GetBuffer(),"w");
52 printf("file open failed %s\n",sFileName
.GetBuffer());
57 CollectByAbstract
*collectBy
= m_pHeaderCollectBy
;
61 if (sHeader
.GetBufferLength() != 0)
63 sHeader
+= collectBy
->GetTitle();
64 collectBy
= collectBy
->pNext
;
66 fprintf(hFile
, "%s\n",sHeader
.GetBuffer());
68 m_pFirstCollectBy
->Export(hFile
,"");
73 CollectByAbstract
* Collection::Factory(BJ_COLLECTBY_TYPE type
)
80 return new CollectByService();
81 case CBT_REQUEST_RESPONDS
:
82 return new CollectByRequestResponds();
83 case CBT_SAME_DIFF_SUBNET
:
84 return new CollectBySameSubnetDiffSubnet();
85 case CBT_IP_ADDRESS_TYPE
:
86 return new CollectByIPAddressType();
88 return new CollectByPacketCount();
98 void CServiceNode::Export(FILE* hFile
,BJString sPrevColumns
)
102 BJString sTemp
= sPrevColumns
;
103 if (sPrevColumns
.GetBufferLength())
106 pNext
->Export(hFile
,sTemp
);
109 dynamic_cast<CServiceNode
*>(m_rbLeft
)->Export(hFile
,sPrevColumns
);
111 dynamic_cast<CServiceNode
*>(m_rbRight
)->Export(hFile
,sPrevColumns
);
114 void CollectByService::Collect(CDNSFrame
* pFrame
,CollectByAbstract
* nextCollectBy
)
116 for (int dnsItemsIndex
=0; dnsItemsIndex
< pFrame
->GetQuestionCount()+pFrame
->GetAnswerCount();dnsItemsIndex
++)
119 CDNSRecord
* pDNSRecord
= pFrame
->GetDnsRecord(dnsItemsIndex
);
120 if (pDNSRecord
== NULL
)
123 pDNSRecord
->GetDnsRecordName(RecordName
,0,99);
125 if (RecordName
.Contains("_kerberos."))
127 RecordName
= "_kerberos.";
130 pDNSRecord
->GetDnsRecordName(RecordName
, (pDNSRecord
->m_RecType
== 12)?0:1,99);
132 if (pDNSRecord
->m_RecType
== 12)
134 if (RecordName
.Contains(".ip6.arpa."))
135 RecordName
= "*.ip6.arpa.";
136 else if (RecordName
.Contains(".arpa."))
137 RecordName
= "*.arpa.";
139 if (pDNSRecord
->m_RecType
== 1)
141 if (pDNSRecord
->m_RecType
== 28)
143 if (pDNSRecord
->m_RecType
== 255)
145 if (RecordName
.Contains(".ip6.arpa."))
146 RecordName
= "ANY *.ip6.arpa.";
147 else if (RecordName
.Contains(".arpa."))
148 RecordName
= "ANY *.arpa.";
152 if (RecordName
.Contains("_sub."))
154 pDNSRecord
->GetDnsRecordName(RecordName
,2,99); /// skip first label and _sub. label
158 CServiceNode
*pNode
= m_Cache
.FindwithAddRecord(&RecordName
);
159 if ((pNode
->pNext
== NULL
) && nextCollectBy
)
160 pNode
->pNext
= nextCollectBy
->Factory();
162 pNode
->pNext
->Collect(pFrame
,nextCollectBy
?nextCollectBy
->pNext
:NULL
);
171 void CollectByService::Export(FILE* hFile
,BJString sPrevColumns
)
175 CServiceNode
*pNode
= m_Cache
.GetRoot();
178 pNode
->Export(hFile
,sPrevColumns
);
182 // CollectByRequestResponds
183 void CollectByRequestResponds::Collect(CDNSFrame
* pFrame
,CollectByAbstract
* nextCollectBy
)
185 if (pFrame
->IsQueryFrame())
187 if (pRequestNext
== NULL
)
188 pRequestNext
= nextCollectBy
->Factory();
189 pRequestNext
->Collect(pFrame
, nextCollectBy
?nextCollectBy
->pNext
:NULL
);
193 if (pRespondsNext
== NULL
)
194 pRespondsNext
= nextCollectBy
->Factory();
195 pRespondsNext
->Collect(pFrame
, nextCollectBy
?nextCollectBy
->pNext
:NULL
);
199 void CollectByRequestResponds::Export(FILE* hFile
,BJString sPrevColumns
)
203 BJString sTemp
= sPrevColumns
;
204 if (sPrevColumns
.GetBufferLength())
207 pRequestNext
->Export(hFile
,sTemp
);
211 BJString sTemp
= sPrevColumns
;
212 if (sPrevColumns
.GetBufferLength())
215 pRespondsNext
->Export(hFile
,sTemp
);
219 // CollectByIPAddressType
220 void CollectByIPAddressType::Collect(CDNSFrame
* pFrame
,CollectByAbstract
* nextCollectBy
)
222 if (pFrame
->m_SourceIPAddress
.IsIPv4())
224 if (pIPv4Next
== NULL
)
225 pIPv4Next
= nextCollectBy
->Factory();
226 pIPv4Next
->Collect(pFrame
, nextCollectBy
?nextCollectBy
->pNext
:NULL
);
228 if (pFrame
->m_SourceIPAddress
.IsIPv6())
230 if ((pIPv6Next
== NULL
) && nextCollectBy
)
231 pIPv6Next
= nextCollectBy
->Factory();
233 pIPv6Next
->Collect(pFrame
, nextCollectBy
?nextCollectBy
->pNext
:NULL
);
236 void CollectByIPAddressType::Export(FILE* hFile
,BJString sPrevColumns
)
240 BJString sTemp
= sPrevColumns
;
241 if (sPrevColumns
.GetBufferLength())
244 pIPv4Next
->Export(hFile
,sTemp
);
248 BJString sTemp
= sPrevColumns
;
249 if (sPrevColumns
.GetBufferLength())
252 pIPv6Next
->Export(hFile
,sTemp
);
256 // CollectBySameSubnetDiffSubnet:
259 bool CollectBySameSubnetDiffSubnet::bSameSubnet
= true;
261 void CollectBySameSubnetDiffSubnet::Collect(CDNSFrame
* pFrame
,CollectByAbstract
* nextCollectBy
)
265 if (pSameSubnetNext
== NULL
)
266 pSameSubnetNext
= nextCollectBy
->Factory();
267 pSameSubnetNext
->Collect(pFrame
, nextCollectBy
?nextCollectBy
->pNext
:NULL
);
271 if (pDiffSubnetNext
== NULL
)
272 pDiffSubnetNext
= nextCollectBy
->Factory();
273 pDiffSubnetNext
->Collect(pFrame
, nextCollectBy
?nextCollectBy
->pNext
:NULL
);
277 void CollectBySameSubnetDiffSubnet::Export(FILE* hFile
,BJString sPrevColumns
)
281 BJString sTemp
= sPrevColumns
;
282 if (sPrevColumns
.GetBufferLength())
284 sTemp
+= "SameSubnet";
285 pSameSubnetNext
->Export(hFile
,sTemp
);
289 BJString sTemp
= sPrevColumns
;
290 if (sPrevColumns
.GetBufferLength())
292 sTemp
+= "WrongSubnet";
293 pDiffSubnetNext
->Export(hFile
,sTemp
);
297 // CollectByPacketCount
299 // staticCollectByPacketCount
300 BJ_INT64
CollectByPacketCount::nFrameIndex
= 0;
302 void CollectByPacketCount::Collect(CDNSFrame
* ,CollectByAbstract
* )
304 if (nFrameIndex
!= nLastFrameIndex
)
307 nLastFrameIndex
= nFrameIndex
;
310 void CollectByPacketCount::Export(FILE* hFile
,BJString sPrevColumns
)
313 fprintf(hFile
,"%s,%llu\n",sPrevColumns
.GetBuffer(),nFrameCount
);