]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSMacOSX/BonjourTop/source/CollectBy.cpp
mDNSResponder-1096.0.2.tar.gz
[apple/mdnsresponder.git] / mDNSMacOSX / BonjourTop / source / CollectBy.cpp
1 //
2 // CollectBy.cpp
3 // TestTB
4 //
5 // Created by Terrin Eager on 3/17/13.
6 //
7 //
8
9 #include <iostream>
10 #include "bjtypes.h"
11 #include "DNSFrame.h"
12 #include "bjstring.h"
13 #include "LLRBTree.h"
14
15 #include "CollectBy.h"
16
17
18 //////////////////////
19 // Collection
20 void Collection::Init(BJ_COLLECTBY_TYPE collectByList[])
21 {
22 CollectByAbstract* pLastCollectBy = nullptr;
23
24 for (int i=0; i<20 && collectByList[i] != CBT_NOT_SET;i++)
25 {
26 m_CollectByList[i] = collectByList[i];
27 if (i==0)
28 {
29 m_pHeaderCollectBy = Factory(m_CollectByList[i]);
30 pLastCollectBy = m_pHeaderCollectBy;
31 m_pFirstCollectBy = pLastCollectBy->Factory();
32 }
33 else
34 {
35 pLastCollectBy->pNext = Factory(m_CollectByList[i]);
36 pLastCollectBy = pLastCollectBy->pNext;
37 }
38 }
39 }
40
41 void Collection::ProcessFrame(CDNSFrame* pFrame)
42 {
43 m_pFirstCollectBy->Collect(pFrame,m_pHeaderCollectBy->pNext);
44 }
45
46 void Collection::ExportCollection(BJString sFileName)
47 {
48 FILE* hFile = fopen(sFileName.GetBuffer(),"w");
49
50 if (hFile == NULL)
51 {
52 printf("file open failed %s\n",sFileName.GetBuffer());
53 return;
54 }
55
56 // Export Header Line
57 CollectByAbstract *collectBy = m_pHeaderCollectBy;
58 BJString sHeader;
59 while (collectBy)
60 {
61 if (sHeader.GetBufferLength() != 0)
62 sHeader += ",";
63 sHeader += collectBy->GetTitle();
64 collectBy = collectBy->pNext;
65 }
66 fprintf(hFile, "%s\n",sHeader.GetBuffer());
67
68 m_pFirstCollectBy->Export(hFile,"");
69
70 fclose(hFile);
71 }
72
73 CollectByAbstract* Collection::Factory(BJ_COLLECTBY_TYPE type)
74 {
75 switch (type)
76 {
77 case CBT_NOT_SET:
78 return NULL;
79 case CBT_SERVICE:
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();
87 case CBT_PACKET:
88 return new CollectByPacketCount();
89 default:
90 return NULL;
91 }
92
93 }
94
95 /////////////
96 // CollectByService
97
98 void CServiceNode::Export(FILE* hFile,BJString sPrevColumns)
99 {
100 if (pNext)
101 {
102 BJString sTemp = sPrevColumns;
103 if (sPrevColumns.GetBufferLength())
104 sTemp += ",";
105 sTemp += m_Key;
106 pNext->Export(hFile,sTemp);
107 }
108 if (m_rbLeft)
109 dynamic_cast<CServiceNode*>(m_rbLeft)->Export(hFile,sPrevColumns);
110 if (m_rbRight)
111 dynamic_cast<CServiceNode*>(m_rbRight)->Export(hFile,sPrevColumns);
112 }
113
114 void CollectByService::Collect(CDNSFrame* pFrame,CollectByAbstract* nextCollectBy)
115 {
116 for (int dnsItemsIndex =0; dnsItemsIndex < pFrame->GetQuestionCount()+pFrame->GetAnswerCount();dnsItemsIndex++)
117 {
118 BJString RecordName;
119 CDNSRecord* pDNSRecord = pFrame->GetDnsRecord(dnsItemsIndex);
120 if (pDNSRecord == NULL)
121 continue;
122
123 pDNSRecord->GetDnsRecordName(RecordName,0,99);
124
125 if (RecordName.Contains("_kerberos."))
126 {
127 RecordName = "_kerberos.";
128 }
129 else
130 pDNSRecord->GetDnsRecordName(RecordName, (pDNSRecord->m_RecType == 12)?0:1,99);
131
132 if (pDNSRecord->m_RecType == 12)
133 {
134 if (RecordName.Contains(".ip6.arpa."))
135 RecordName = "*.ip6.arpa.";
136 else if (RecordName.Contains(".arpa."))
137 RecordName = "*.arpa.";
138 }
139 if (pDNSRecord->m_RecType == 1)
140 RecordName = "A";
141 if (pDNSRecord->m_RecType == 28)
142 RecordName = "AAAA";
143 if (pDNSRecord->m_RecType == 255)
144 {
145 if (RecordName.Contains(".ip6.arpa."))
146 RecordName = "ANY *.ip6.arpa.";
147 else if (RecordName.Contains(".arpa."))
148 RecordName = "ANY *.arpa.";
149 else
150 RecordName = "Any";
151 }
152 if (RecordName.Contains("_sub."))
153 {
154 pDNSRecord->GetDnsRecordName(RecordName,2,99); /// skip first label and _sub. label
155 }
156
157
158 CServiceNode *pNode= m_Cache.FindwithAddRecord(&RecordName);
159 if ((pNode->pNext == NULL) && nextCollectBy)
160 pNode->pNext = nextCollectBy->Factory();
161 if (pNode->pNext)
162 pNode->pNext->Collect(pFrame,nextCollectBy?nextCollectBy->pNext:NULL);
163
164 }
165
166 }
167
168
169
170
171 void CollectByService::Export(FILE* hFile,BJString sPrevColumns)
172 {
173
174 // loop thur list
175 CServiceNode *pNode = m_Cache.GetRoot();
176
177 if (pNode)
178 pNode->Export(hFile,sPrevColumns);
179 }
180
181 /////////////
182 // CollectByRequestResponds
183 void CollectByRequestResponds::Collect(CDNSFrame* pFrame,CollectByAbstract* nextCollectBy)
184 {
185 if (pFrame->IsQueryFrame())
186 {
187 if (pRequestNext == NULL)
188 pRequestNext = nextCollectBy->Factory();
189 pRequestNext->Collect(pFrame, nextCollectBy?nextCollectBy->pNext:NULL);
190 }
191 else
192 {
193 if (pRespondsNext == NULL)
194 pRespondsNext = nextCollectBy->Factory();
195 pRespondsNext->Collect(pFrame, nextCollectBy?nextCollectBy->pNext:NULL);
196 }
197 }
198
199 void CollectByRequestResponds::Export(FILE* hFile,BJString sPrevColumns)
200 {
201 if (pRequestNext)
202 {
203 BJString sTemp = sPrevColumns;
204 if (sPrevColumns.GetBufferLength())
205 sTemp += ",";
206 sTemp += "Request";
207 pRequestNext->Export(hFile,sTemp);
208 }
209 if (pRespondsNext)
210 {
211 BJString sTemp = sPrevColumns;
212 if (sPrevColumns.GetBufferLength())
213 sTemp += ",";
214 sTemp += "Responds";
215 pRespondsNext->Export(hFile,sTemp);
216 }
217 }
218 /////////////
219 // CollectByIPAddressType
220 void CollectByIPAddressType::Collect(CDNSFrame* pFrame,CollectByAbstract* nextCollectBy)
221 {
222 if (pFrame->m_SourceIPAddress.IsIPv4())
223 {
224 if (pIPv4Next == NULL)
225 pIPv4Next = nextCollectBy->Factory();
226 pIPv4Next->Collect(pFrame, nextCollectBy?nextCollectBy->pNext:NULL);
227 }
228 if (pFrame->m_SourceIPAddress.IsIPv6())
229 {
230 if ((pIPv6Next == NULL) && nextCollectBy)
231 pIPv6Next = nextCollectBy->Factory();
232 if (pIPv6Next)
233 pIPv6Next->Collect(pFrame, nextCollectBy?nextCollectBy->pNext:NULL);
234 }
235 }
236 void CollectByIPAddressType::Export(FILE* hFile,BJString sPrevColumns)
237 {
238 if (pIPv4Next)
239 {
240 BJString sTemp = sPrevColumns;
241 if (sPrevColumns.GetBufferLength())
242 sTemp += ",";
243 sTemp += "IPv4";
244 pIPv4Next->Export(hFile,sTemp);
245 }
246 if (pIPv6Next)
247 {
248 BJString sTemp = sPrevColumns;
249 if (sPrevColumns.GetBufferLength())
250 sTemp += ",";
251 sTemp += "IPv6";
252 pIPv6Next->Export(hFile,sTemp);
253 }
254 }
255 /////////////
256 // CollectBySameSubnetDiffSubnet:
257
258 // static
259 bool CollectBySameSubnetDiffSubnet::bSameSubnet = true;
260
261 void CollectBySameSubnetDiffSubnet::Collect(CDNSFrame* pFrame,CollectByAbstract* nextCollectBy)
262 {
263 if (bSameSubnet)
264 {
265 if (pSameSubnetNext == NULL)
266 pSameSubnetNext = nextCollectBy->Factory();
267 pSameSubnetNext->Collect(pFrame, nextCollectBy?nextCollectBy->pNext:NULL);
268 }
269 else
270 {
271 if (pDiffSubnetNext == NULL)
272 pDiffSubnetNext = nextCollectBy->Factory();
273 pDiffSubnetNext->Collect(pFrame, nextCollectBy?nextCollectBy->pNext:NULL);
274 }
275
276 }
277 void CollectBySameSubnetDiffSubnet::Export(FILE* hFile,BJString sPrevColumns)
278 {
279 if (pSameSubnetNext)
280 {
281 BJString sTemp = sPrevColumns;
282 if (sPrevColumns.GetBufferLength())
283 sTemp += ",";
284 sTemp += "SameSubnet";
285 pSameSubnetNext->Export(hFile,sTemp);
286 }
287 if (pDiffSubnetNext)
288 {
289 BJString sTemp = sPrevColumns;
290 if (sPrevColumns.GetBufferLength())
291 sTemp += ",";
292 sTemp += "WrongSubnet";
293 pDiffSubnetNext->Export(hFile,sTemp);
294 }
295 }
296 /////////////
297 // CollectByPacketCount
298
299 // staticCollectByPacketCount
300 BJ_INT64 CollectByPacketCount::nFrameIndex = 0;
301
302 void CollectByPacketCount::Collect(CDNSFrame* ,CollectByAbstract* )
303 {
304 if (nFrameIndex != nLastFrameIndex)
305 {
306 nFrameCount++;
307 nLastFrameIndex = nFrameIndex;
308 }
309 }
310 void CollectByPacketCount::Export(FILE* hFile,BJString sPrevColumns)
311 {
312
313 fprintf(hFile,"%s,%llu\n",sPrevColumns.GetBuffer(),nFrameCount);
314 }
315
316