9 typedef struct Etherpkt Etherpkt;
16 #define ETHERMINTU 60 /* minimum transmit size */
17 #define ETHERMAXTU 1514 /* maximum transmit size */
18 #define ETHERHDRSIZE 14 /* size of an ethernet header */
23 typedef struct Ippkt Ippkt;
26 uchar vihl; /* Version and header length */
27 uchar tos; /* Type of service */
28 uchar length[2]; /* packet length */
29 uchar id[2]; /* Identification */
30 uchar frag[2]; /* Fragment information */
31 uchar ttl; /* Time to live */
32 uchar proto; /* Protocol */
33 uchar cksum[2]; /* Header checksum */
34 uchar src[4]; /* Ip source */
35 uchar dst[4]; /* Ip destination */
40 #define IP_UDPPROTO 17
41 #define IP_MBONEPROTO 4
44 #define IP_ICMPPROTO 1
48 #define NetS(x) (((x)[0]<<8) | (x)[1])
49 #define NetL(x) (((x)[0]<<24) | ((x)[1]<<16) | ((x)[2]<<8) | (x)[3])
67 errstr(buf, sizeof buf);
68 fprint(2, "snoopy: %s %s\n", buf, s);
77 errstr(buf, sizeof buf);
78 fprint(2, "snoopy: %s %s\n", buf, s);
84 print("\t%d(%ld %ld %ld %ld)", p, protoin[p], protopin[p], protoout[p], protopout[p]);
88 main(int argc, char *argv[])
107 samples = atoi(ARGF());
112 fprint(2, "usage: %s device ip-addr [minutes-per-sample]\n", argv0);
116 delta = atoi(argv[2])*60*1000;
119 parseether(target, argv[1]);
121 fmtinstall('E', eipfmt);
122 fmtinstall('I', eipfmt);
124 snprint(buf, sizeof(buf), "%s!-2", argv[0]);
125 fd = dial(buf, 0, 0, &cfd);
127 error("opening ether data");
128 if(write(cfd, "promiscuous", sizeof("promiscuous")-1) <= 0)
136 fd = dial(buf, 0, 0, &cfd);
138 error("opening ether data");
139 if(write(cfd, "promiscuous", sizeof("promiscuous")-1) <= 0)
143 n = read(fd, &e, sizeof(e));
147 n = NetS(&e.d[58]) - ETHERHDRSIZE;
153 if(t == 0x0800 || (t&0xFF00) == 0x1000){
155 len = NetS(ip->length);
159 fprint(2, "%I -> %I %d\n", ip->src, ip->dst, len);
160 if(memcmp(e.s, target, 6) == 0){
164 protopin[ip->proto]++;
165 protoin[ip->proto] += len;
168 if(memcmp(e.d, target, 6) == 0){
172 protopout[ip->proto]++;
173 protoout[ip->proto] += len;
177 if(ts - start >= delta){
178 print("%8.8ld %ld", time(0), ts - start);
180 printproto(IP_MBONEPROTO);
181 printproto(IP_UDPPROTO);
182 printproto(IP_TCPPROTO);
185 memset(protoin, 0, sizeof(protoin));
186 memset(protoout, 0, sizeof(protoout));
187 memset(protopin, 0, sizeof(protopin));
188 memset(protopout, 0, sizeof(protopout));