Imported Upstream version 878.70.2
[platform/upstream/mdnsresponder.git] / mDNSMacOSX / BonjourTop / source / main.cpp
1 //
2 //  main.cpp
3 //  BonjourTop
4 //
5 //  Created by Terrin Eager on 4/24/13.
6 //  Copyright (c) 2013-2014 Apple Inc. All rights reserved.
7 //
8
9 #include <stdio.h>
10 #include <curses.h>
11
12 #include "bjtypes.h"
13 #include "BonjourTop.h"
14
15 #include <signal.h>
16 #include <getopt.h>
17 #include <stdlib.h>
18 #include <sys/types.h>
19
20
21 #define BJ_VERSION_MAJOR 0
22 #define BJ_VERSION_MINOR 23
23 CBonjourTop BjTop;
24
25 static void
26 usage()
27 {
28     printf("bonjourtop usage: bonjourTop (Version: %d.%d)\n",BJ_VERSION_MAJOR,BJ_VERSION_MINOR);
29     printf("\t\t\t [-t tcptrace_filename ]\n");
30     printf("\t\t\t [-i interfaceName]\n");
31     printf("\t\t\t [-m ipaddress/subnetmask]  ie 17.255.45.12/17\n");
32     printf("\t\t\t [-e export_filename]  \n");
33     printf("\t\t\t [-x seconds]  'Snapshot export every x seconds'\n");
34     printf("\t\t\t [-s] 'service information'\n");
35     printf("\t\t\t [-v] 'report the version number'  \n");
36     printf("\t\t\t [-d] filename 'export device map. Adds timestamp and csv extension to the filename'  \n");
37     printf("\t\t\t [-f application] 'filter application for device map (only available with -t -d options)'  \n");
38     printf("While running the follow keys may be used:\n");
39     printf("\t b - sort by Bytes\n");
40     printf("\t p - sort by Packets (default)\n");
41     printf("\t n - sort by Name\n");
42     printf("\t a - Display Application Names (default) \n");
43     printf("\t s - Display Services Names  \n");
44     printf("\t t - Display 24 hour packet per min  \n");
45
46     printf("\t o - flip sort order\n");
47     printf("\t e - export to BonjourTop.csv\n");
48     printf("\t q - quit\n\n");
49 }
50
51 static void
52 handle_window_change(int signal) {
53     switch (signal) {
54         case SIGWINCH:
55             BjTop.WindowSizeChanged();
56             break;
57         default:
58             break;
59     }
60 }
61
62 int main(int argc, char * const *argv)
63 {
64
65     sigset_t sset, oldsset;
66     int c;
67
68     static struct option longopts[] = {
69         {   "trace", required_argument, NULL, 't' },
70         {   "interface", required_argument, NULL, 'i' },
71         {   "ipaddr_subnet", required_argument, NULL, 'm' },
72         {   "export", required_argument, NULL, 'e' },
73         {   "snapshot", required_argument, NULL, 'x' },
74         {   "service", no_argument, NULL, 's' },
75         {   "version", no_argument, NULL, 'v' },
76         {   "devicemap", required_argument, NULL, 'd' },
77         {   "filter", required_argument, NULL, 'f' },
78         {   NULL, 0, NULL, 0 }
79     };
80
81         sigemptyset(&sset);
82
83     /* Block SIGWINCH signals while we are in a relayout. */
84     if(-1 == sigprocmask(SIG_BLOCK, &sset, &oldsset)) {
85         perror("sigprocmask");
86         exit(EXIT_FAILURE);
87     }
88
89     BJ_COLLECTBY_TYPE TypeList[] = {CBT_SERVICE,CBT_REQUEST_RESPONDS,CBT_SAME_DIFF_SUBNET,CBT_IP_ADDRESS_TYPE,CBT_PACKET};
90     BJString sTemp;
91
92     bool bLiveCapture = true;
93     bool bExport = false;
94
95     while ((c = getopt_long(argc, argv, "t:i:m:e:x:svd:f:phb", longopts, NULL)) != -1) {
96         switch (c) {
97             case 't':
98                 BjTop.m_pTcpDumpFileName = optarg; // TCP Dump Filename
99                 bLiveCapture = false;
100                 BjTop.m_bCursers = false;
101                 break;
102             case 'p':
103                 BjTop.m_bCursers = false;
104                 break;
105             case 'e':
106                 bExport = true;
107                 BjTop.m_pExportFileName = optarg;   // Export filename
108                 break;
109             case 'i':
110                 BjTop.interfaceName = optarg;       // Interface name
111                 break;
112             case 'm':
113                 BjTop.SetIPAddr(optarg);            // TODO: verify that the argument is an ip address
114                 break;
115             case 'x':
116                 sTemp = optarg;                     // time in seconds for snapshots
117                 BjTop.m_SnapshotSeconds = sTemp.GetUINT32();
118                 break;
119             case 'd':
120                 BjTop.m_bImportExportDeviceMap = true;
121                 BjTop.m_DeviceFileName = optarg;
122                 break;
123             case 'f':
124                 BjTop.filterApplicationName = optarg;
125                 break;
126             case 's':
127                 BjTop.m_CurrentDisplay = CBonjourTop::BJ_DISPLAY_SERVICE;
128                 break;
129             case 'v':
130                 printf("\nbonjourtop Version: %d.%d\n\n",BJ_VERSION_MAJOR,BJ_VERSION_MINOR);
131                 exit(0);
132                 break;
133             case 'b':
134                 BjTop.m_Collection.Init(TypeList);
135                 bExport = true;
136                 break;
137             case 'h':
138                 usage();
139                 exit(0);
140                 break;
141             default:
142                 usage();
143                 break;
144         }
145     }
146
147     if (BjTop.m_bCursers)
148     {
149         signal(SIGWINCH, handle_window_change);
150         initscr();
151         timeout(0);
152         BjTop.PrintResults(1,false);
153     }
154
155     if (bLiveCapture)
156         BjTop.LiveCapture();
157     else
158         BjTop.CaptureFile();
159
160
161     if (bExport)
162     {
163         BjTop.ExportResults();
164         return 0;
165     }
166
167     if (BjTop.m_bCursers)
168         endwin();
169
170     if (BjTop.m_bImportExportDeviceMap)
171     {
172         BjTop.WriteDeviceFile();
173         BjTop.WriteVendorFile();
174     }
175
176     return 0;
177 }
178
179