5 // Created by Terrin Eager on 9/26/12.
9 #ifndef __TestTB__BonjourTop__
10 #define __TestTB__BonjourTop__
21 #include "bjStringtoStringMap.h"
24 #include "CollectBy.h"
28 typedef struct device_count {
31 BJ_UINT64 unknownOS = 0;
32 BJ_UINT64 OSXWithEDNSField = 0;
33 BJ_UINT64 iOSWithEDNSField = 0;
42 BJ_UINT64 m_nFrameCount;
44 BJ_UINT64 m_nQuestionOnlyFrames;
45 BJ_UINT64 m_nAnswerOnlyFrames;
46 BJ_UINT64 m_nQandAFrames;
55 FrameCount() { count = 0;lastFrameNumber = -1;};
56 void Increment(BJ_UINT64 frameNumber)
58 if (frameNumber != lastFrameNumber)
60 lastFrameNumber = frameNumber;
63 BJ_UINT64 GetValue() const {return count;};
64 void Reset() { count = 0;lastFrameNumber = -1;};
65 FrameCount &operator+=(const FrameCount &src) { count += src.count; return *this;};
69 BJ_UINT64 lastFrameNumber;
76 class CIPDeviceNode : public CRBNode<BJIPAddr>
79 CIPDeviceNode(BJIPAddr* pSrc) {m_Key = *pSrc; /* temp deviceOS = '?';lastQUFrame = 0;*/pDeviceNode=NULL;};
80 CIPDeviceNode(){ /*deviceOS = '?';lastQUFrame = 0; */pDeviceNode=NULL;};
82 inline virtual BJ_COMPARE Compare(BJIPAddr* pKey) {return m_Key.Compare(pKey);};
83 inline virtual void CopyNode(CRBNode* pSrc)
85 m_Key.Set(((BJIPAddr*)pSrc)->Getin6_addr());
86 pDeviceNode = ((CIPDeviceNode*)pSrc)->pDeviceNode;
92 CDeviceNode* pDeviceNode;
96 class CIPAddrMap: public CLLRBTree<BJIPAddr,CIPDeviceNode>
99 void GetDeviceOSTypes(CIPDeviceNode* node, CIPAddrMap* pGobalMap, BJ_UINT64& iOS,BJ_UINT64& OSX,BJ_UINT64& unknowOS);
103 class CDeviceNode : public CRBNode<BJString>
106 CDeviceNode(BJString* pSrc) {m_Key = *pSrc; deviceOS = '?'; bOSXWithEDNSField = false; biOSWithEDNSField = false; bDuplicate = false; bIPName = false; bHasFrames = false; nCreateCount++;};
107 CDeviceNode(){deviceOS = '?'; bDuplicate = false; bIPName = false; bHasFrames = false; nCreateCount++;};
108 ~CDeviceNode(){nCreateCount--;};
109 inline virtual BJ_COMPARE Compare(BJString* pKey) {return m_Key.Compare(*pKey);};
110 inline virtual void CopyNode(CRBNode* pSource)
112 m_Key = pSource->m_Key;
113 deviceOS = dynamic_cast<CDeviceNode*>(pSource)->deviceOS;
114 model = dynamic_cast<CDeviceNode*>(pSource)->model;
115 settingService = dynamic_cast<CDeviceNode*>(pSource)->settingService;
116 macAddress = dynamic_cast<CDeviceNode*>(pSource)->macAddress;
117 ipAddressv4 = dynamic_cast<CDeviceNode*>(pSource)->ipAddressv4;
118 ipAddressv6 = dynamic_cast<CDeviceNode*>(pSource)->ipAddressv6;
119 bOSXWithEDNSField = dynamic_cast<CDeviceNode*>(pSource)->bOSXWithEDNSField;
120 biOSWithEDNSField = dynamic_cast<CDeviceNode*>(pSource)->biOSWithEDNSField;
121 bHasFrames = dynamic_cast<CDeviceNode*>(pSource)->bHasFrames;
123 inline virtual void MergeData(CDeviceNode* src)
125 deviceOS = src->deviceOS;
127 settingService = src->settingService;
128 macAddress = src->macAddress;
129 bOSXWithEDNSField = src->bOSXWithEDNSField;
130 biOSWithEDNSField = src->biOSWithEDNSField;
132 frameTotal += src->frameTotal;
133 questionFrame += src->questionFrame;
134 QUFrame += src->QUFrame;
135 answerFrame += src->answerFrame;
136 bHasFrames |= src->bHasFrames;
141 questionFrame.Reset();
146 void Init() {deviceOS = '?';};
149 char GetDeviceOS() {return deviceOS;};
150 void SetDeviceOS(char t,const char* pSettingService)
152 BJString EDNS0 = "EDNS0 Trace";
153 if (pSettingService == NULL)
154 printf("SetDeviceOS: pSettingService is NULL\n");
156 if (settingService != EDNS0 || EDNS0 == pSettingService)
158 settingService = pSettingService;;
161 // if ( t != deviceOS && deviceOS != '?' && (deviceOS != 'b' ))
162 // printf("SetDeviceOS: %s deviceOS not equal %c by %s != %c by %s\n", m_Key.GetBuffer(),deviceOS,settingService.GetBuffer(),t,pSettingService);
164 void SetModel(char* pModel) {model = pModel;};
165 void Export(FILE* file)
167 // fprintf(hFile,"Name,IPAddress,MACAddress,OSType,Model,Method\n");
169 dynamic_cast<CDeviceNode*>(m_rbRight)->Export(file);
171 if (!bDuplicate || frameTotal.GetValue() > 0)
173 fprintf(file,"\"%s\",\"%s\",\"%s\",\"%s\",%c,\"%s\",\"%s\",%llu,%llu,%llu,%llu\n",
175 ipAddressv4.GetString(),
176 ipAddressv6.GetString(),
177 macAddress.GetString(),
179 model.GetBuffer()?model.GetBuffer():" ",
180 (bDuplicate)?"dup":settingService.GetBuffer()?settingService.GetBuffer():" ",
181 frameTotal.GetValue(),
182 questionFrame.GetValue(),
184 answerFrame.GetValue());
188 dynamic_cast<CDeviceNode*>(m_rbLeft)->Export(file);
191 BJMACAddr macAddress;
192 BJIPAddr ipAddressv4;
193 BJIPAddr ipAddressv6;
195 BJString settingService;
196 bool biOSWithEDNSField;
197 bool bOSXWithEDNSField;
202 FrameCount frameTotal;
203 FrameCount questionFrame;
205 FrameCount answerFrame;
206 static int nCreateCount;
212 class CDeviceMap: public CLLRBTree<BJString,CDeviceNode>
215 void GetDeviceOSTypes(CDeviceNode *node, CDeviceMap *pGobalMap, device_count& dev_cnt);
219 class CMACAddrNode: public CRBNode<BJMACAddr>
222 CMACAddrNode(BJMACAddr* pSrc) {m_Key.Set(pSrc->Get()); deviceOS = '?'; };
223 CMACAddrNode(){deviceOS = '?';};
225 inline virtual BJ_COMPARE Compare(BJMACAddr* pKey) {return m_Key.Compare(pKey);};
226 inline virtual void CopyNode(CRBNode* pSource)
228 m_Key.Set( pSource->m_Key.Get());
229 deviceOS = dynamic_cast<CMACAddrNode*>(pSource)->deviceOS;
230 model = dynamic_cast<CMACAddrNode*>(pSource)->model;
231 method = dynamic_cast<CMACAddrNode*>(pSource)->method;
233 void Export(FILE* file)
236 dynamic_cast<CMACAddrNode*>(m_rbRight)->Export(file);
239 fprintf(file,"\"%s\",%c,\"%s\",\"%s\"\n",
242 model.GetBuffer()?model.GetBuffer():" ",
243 method.GetBuffer()?method.GetBuffer():" ");
247 dynamic_cast<CMACAddrNode*>(m_rbLeft)->Export(file);
250 void Init() {deviceOS = '?';};
258 class CMACAddrTree: public CLLRBTree<BJMACAddr,CMACAddrNode>
263 class CMACAddrDeviceNode: public CRBNode<BJMACAddr>
266 CMACAddrDeviceNode(BJMACAddr* pSrc) {m_Key.Set(pSrc->Get()); device = NULL; };
267 CMACAddrDeviceNode(){device = NULL;};
268 ~CMACAddrDeviceNode(){};
269 inline virtual BJ_COMPARE Compare(BJMACAddr* pKey) {return m_Key.Compare(pKey);};
270 inline virtual void CopyNode(CRBNode* pSource)
272 m_Key.Set( pSource->m_Key.Get());
273 device = dynamic_cast<CMACAddrDeviceNode*>(pSource)->device;
275 void Init() {device = NULL;};
281 class CMACDeviceMap: public CLLRBTree<BJMACAddr,CMACAddrDeviceNode>
288 class CStringNode : public CRBNode<BJ_UINT64>
291 CStringNode(){Init();};
292 CStringNode ( BJ_UINT64* Key) { Init(); m_Key = *Key;};
293 inline virtual BJ_COMPARE Compare(BJ_UINT64* pKey)
298 else if (m_Key > *pKey)
303 inline virtual void CopyNode(CRBNode* pSource)
305 CStringNode* pSrc = (CStringNode*) pSource;
307 // m_Key = pSrc->m_Key;
308 m_nBytes = pSrc->m_nBytes;
309 m_nFrames = pSrc->m_nFrames;
310 m_nFramesiOS = pSrc->m_nFramesiOS;
311 m_nFramesOSX = pSrc->m_nFramesOSX;
312 m_nQuestionFrames = pSrc->m_nQuestionFrames;
313 m_nQuestionFramesiOS = pSrc->m_nQuestionFramesiOS;
314 m_nQuestionFramesOSX = pSrc->m_nQuestionFramesOSX;
315 m_nAnswerFrames = pSrc->m_nAnswerFrames;
316 m_nAnswerFramesiOS = pSrc->m_nAnswerFramesiOS;
317 m_nAnswerFramesOSX = pSrc->m_nAnswerFramesOSX;
318 strcpy(m_Value,pSrc->m_Value);
319 m_nDeviceAskingCount = pSrc->m_nDeviceAskingCount;
320 m_nDeviceAskingiOSCount = pSrc->m_nDeviceAskingiOSCount;
321 m_nDeviceAskingOSXCount = pSrc->m_nDeviceAskingOSXCount;
322 m_nDeviceAnsweringCount = pSrc->m_nDeviceAnsweringCount;
323 m_nDeviceAnsweringiOSCount = pSrc->m_nDeviceAnsweringiOSCount;
324 m_nDeviceAnsweringOSXCount = pSrc->m_nDeviceAnsweringOSXCount;
325 m_nDeviceTotalCount = pSrc->m_nDeviceTotalCount;
326 m_nDeviceTotaliOSCount = pSrc->m_nDeviceTotaliOSCount;
327 m_nDeviceTotalOSXCount = pSrc->m_nDeviceTotalOSXCount;
328 m_nWakeFrames = pSrc->m_nWakeFrames;
329 m_nLastWakeFrameIndex = pSrc->m_nLastWakeFrameIndex;
330 m_nGoodbyeFrames = pSrc->m_nGoodbyeFrames;
335 m_nFrames = m_nFramesiOS = m_nFramesOSX = m_nQuestionFrames = m_nQuestionFramesiOS = m_nQuestionFramesOSX = m_nAnswerFrames = m_nAnswerFramesiOS = m_nAnswerFramesOSX = 0;
336 m_nLastFrameIndex = 0;
337 m_nLastQueryFrameIndex = 0;
338 m_nLastRespondsFrameIndex = 0;
339 m_nDeviceAskingCount = m_nDeviceAskingiOSCount = m_nDeviceAskingOSXCount = 0;
340 m_nDeviceAnsweringCount = m_nDeviceAnsweringiOSCount = m_nDeviceAnsweringOSXCount = 0;
341 m_nDeviceTotalCount = m_nDeviceTotaliOSCount = m_nDeviceTotalOSXCount = 0;
343 m_nGoodbyeFrames = 0;
344 m_lastQUFrameTime = 0;
346 inline void Clear() {};
348 void UpdateOSTypeCounts(CDeviceMap* pGobalMap,CIPAddrMap *pIp2NameMap);
350 void Print(bool bCursers,bool bDescendingSort,BJ_UINT32 &nIndex,BJ_UINT32 nStartIndex,BJ_UINT32 nEndIndex);
351 void Export(FILE* hFile);
357 BJ_UINT64 m_nFramesiOS;
358 BJ_UINT64 m_nFramesOSX;
359 BJ_UINT64 m_nQuestionFrames;
360 BJ_UINT64 m_nQuestionFramesiOS;
361 BJ_UINT64 m_nQuestionFramesOSX;
362 BJ_UINT64 m_nAnswerFrames;
363 BJ_UINT64 m_nAnswerFramesiOS;
364 BJ_UINT64 m_nAnswerFramesOSX;
365 BJ_UINT64 m_nLastFrameIndex;
366 BJ_UINT64 m_nLastQueryFrameIndex;
367 BJ_UINT64 m_nLastRespondsFrameIndex;
368 BJ_UINT64 m_nLastWakeFrameIndex;
369 CIPAddrMap m_DeviceAskingTree;
370 BJ_UINT64 m_nDeviceAskingCount;
371 BJ_UINT64 m_nDeviceAskingiOSCount;
372 BJ_UINT64 m_nDeviceAskingOSXCount;
373 CIPAddrMap m_DeviceAnsweringTree;
374 BJ_UINT64 m_nDeviceAnsweringCount;
375 BJ_UINT64 m_nDeviceAnsweringiOSCount;
376 BJ_UINT64 m_nDeviceAnsweringOSXCount;
377 CIPAddrMap m_DeviceTotalTree;
378 BJ_UINT64 m_nDeviceTotalCount;
379 BJ_UINT64 m_nDeviceTotaliOSCount;
380 BJ_UINT64 m_nDeviceTotalOSXCount;
381 BJ_UINT64 m_nWakeFrames;
382 BJ_UINT64 m_lastQUFrameTime;
383 BJ_UINT64 m_nGoodbyeFrames;
386 class CStringTree: public CLLRBTree<BJ_UINT64,CStringNode>
394 class CStringShortNode: public CRBNode<BJ_UINT64>
397 CStringShortNode(BJ_UINT64* key) {Init(); m_Key = *key;};
398 inline virtual BJ_COMPARE Compare(BJ_UINT64* pKey)
403 else if (m_Key > *pKey)
409 inline virtual void CopyNode(CRBNode* pSource)
411 CStringShortNode* pSrc = (CStringShortNode*) pSource;
413 // m_Key = pSrc->m_Key;
414 m_nBytes = pSrc->m_nBytes;
415 m_nFrames = pSrc->m_nFrames;
416 m_nQuestionFrames = pSrc->m_nQuestionFrames;
417 m_nAnswerFrames = pSrc->m_nAnswerFrames;
418 strcpy(m_Value,pSrc->m_Value);
419 m_nDeviceAskingCount = pSrc->m_nDeviceAskingCount;
420 m_nDeviceAnsweringCount = pSrc->m_nDeviceAnsweringCount;
421 m_nDeviceTotalCount = pSrc->m_nDeviceTotalCount;
422 m_nWakeFrames = pSrc->m_nWakeFrames;
423 m_nLastWakeFrameIndex = pSrc->m_nLastWakeFrameIndex;
424 m_nGoodbyeFrames = pSrc->m_nGoodbyeFrames;
429 m_nFrames = m_nQuestionFrames = m_nAnswerFrames = 0;
430 m_nLastFrameIndex = m_nLastQueryFrameIndex = m_nLastRespondsFrameIndex = m_nLastWakeFrameIndex = 0;
431 m_nDeviceAskingCount = m_nDeviceAnsweringCount = m_nDeviceTotalCount = 0;
432 m_nWakeFrames = m_lastQUFrameTime = m_nGoodbyeFrames = 0;
434 inline void Clear(){};
436 void Export(FILE* hFile);
442 BJ_UINT64 m_nQuestionFrames;
443 BJ_UINT64 m_nAnswerFrames;
444 BJ_UINT64 m_nLastFrameIndex;
445 BJ_UINT64 m_nLastQueryFrameIndex;
446 BJ_UINT64 m_nLastRespondsFrameIndex;
447 BJ_UINT64 m_nLastWakeFrameIndex;
448 CIPAddrMap m_DeviceAskingTree;
449 BJ_UINT64 m_nDeviceAskingCount;
450 CIPAddrMap m_DeviceAnsweringTree;
451 BJ_UINT64 m_nDeviceAnsweringCount;
452 CIPAddrMap m_DeviceTotalTree;
453 BJ_UINT64 m_nDeviceTotalCount;
454 BJ_UINT64 m_nWakeFrames;
455 BJ_UINT64 m_lastQUFrameTime;
456 BJ_UINT64 m_nGoodbyeFrames;
459 class CStringShortTree: public CLLRBTree<BJ_UINT64, CStringShortNode>
472 void SetIPAddr(const char*);
476 void PrintResults(int nSortCol, bool bSortAsc);
477 void UpdateOSCounts();
478 void PrintDetailResults(int nSortCol, bool bSortAsc);
479 void ExportResults();
482 void WriteDeviceFile();
483 void WriteVendorFile();
485 void ProcessFrame(BJ_UINT8* pBuffer,BJ_INT32 nLength, BJ_UINT64 frameTime);
486 bool Name2OSType(BJString name,CDeviceNode* device);
488 void UpdateRecord(CStringTree &Cache,CDNSRecord* pDNSRecord,BJString& RecordName,BJString& ServiceName,BJ_UINT32 nBytes,bool bGoodbye);
490 void UpdateShortRecordHelper(BJ_UINT32 cacheType, BJ_UINT32 tracePlatform, BJ_UINT32 traceVersion, char deviceOS, CDNSRecord* pDNSRecord,BJString& RecordName,BJString& ServiceName,BJ_UINT32 nBytes,bool bGoodbye);
492 void UpdateShortRecord(CStringShortTree* Cache,CDNSRecord* pDNSRecord,BJString& RecordName,BJString& ServiceName,BJ_UINT32 nBytes,bool bGoodbye);
494 void GetOSTypeFromQuery(CDNSRecord *pDNSRecord,BJString& ServiceName);
495 void GetOSTypeFromRegistration(CDNSRecord *pDNSRecord,BJString& ServiceName);
497 CStringNode* GetCurrentDisplayRoot(BJString &sTitle);
498 void ExportPtrCache(FILE* hFile, BJString sTitle, CStringNode* pRoot);
499 void ExportShortCache(FILE* hFile, BJString sTitle, map<BJString, CStringShortTree*>* myMap);
500 void ExportShortCacheHelper(FILE* hFile, BJString sTitle, CStringShortNode* pRoot);
502 void WindowSizeChanged();
505 const char* m_pTcpDumpFileName;
506 const char* m_pExportFileName;
507 bool window_size_changed;
508 bool m_bImportExportDeviceMap;
509 BJString m_DeviceFileName;
513 #define NUM_SOCKET_STATUS 6
514 #define HOURS_IN_DAY 24
515 #define MINUTES_IN_HOUR 60
517 CSocketStats m_SocketStatus[NUM_SOCKET_STATUS];
519 CSocketStats m_MinSnapshot[HOURS_IN_DAY][MINUTES_IN_HOUR];
521 BJ_UINT64 m_nFrameCount;
522 BJ_UINT64 m_nTotalBytes;
525 BJ_UINT64 m_MinAnswerCountForTruncatedFrames;
526 BJ_UINT64 m_AvgAnswerCountForTruncatedFrames;
527 BJ_UINT64 m_MaxAnswerCountForTruncatedFrames;
531 BJStringtoStringMap m_Service2AppMap;
533 BJStringtoStringMap m_Service2osRegisterMap;
534 BJStringtoStringMap m_Service2osBrowseMap;
536 enum BJ_DISPLAY_MODE_ENUM {
540 BJ_DISPLAY_SERVICEv6,
544 BJ_INT32 m_SnapshotSeconds;
546 CStringTree m_ServicePtrCache;
547 CStringTree m_ApplPtrCache;
549 CStringTree m_ServicePtrCacheIPv6;
550 CStringTree m_ApplPtrCacheIPv6;
552 map<BJString, CStringShortTree*> m_ServiceBreakdownIPv4OSX;
553 map<BJString, CStringShortTree*> m_ServiceBreakdownIPv4iOS;
554 map<BJString, CStringShortTree*> m_ServiceBreakdownIPv6OSX;
555 map<BJString, CStringShortTree*> m_ServiceBreakdownIPv6iOS;
557 map<BJString, CStringShortTree*> m_AppBreakdownIPv4OSX;
558 map<BJString, CStringShortTree*> m_AppBreakdownIPv4iOS;
559 map<BJString, CStringShortTree*> m_AppBreakdownIPv6OSX;
560 map<BJString, CStringShortTree*> m_AppBreakdownIPv6iOS;
562 CDeviceMap m_DeviceMap;
564 CMACAddrTree m_MacMap;
565 CIPAddrMap m_IPtoNameMap;
566 CMACDeviceMap m_MACtoDevice;
567 BJStringtoStringMap SVRtoDeviceName;
569 Collection m_Collection;
571 BJString interfaceName;
572 BJString filterApplicationName;
577 #endif /* defined(__TestTB__BonjourTop__) */