1 /*
2  * Copyright 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "sniffer.h"
18 
19 #include "model/setup/device_boutique.h"
20 #include "os/log.h"
21 
22 using std::vector;
23 
24 namespace test_vendor_lib {
25 
26 bool Sniffer::registered_ = DeviceBoutique::Register("sniffer", &Sniffer::Create);
27 
Sniffer()28 Sniffer::Sniffer() {}
29 
Initialize(const vector<std::string> & args)30 void Sniffer::Initialize(const vector<std::string>& args) {
31   if (args.size() < 2) return;
32 
33   if (Address::FromString(args[1], device_to_sniff_)) {
34     properties_.SetAddress(device_to_sniff_);
35   }
36 
37   if (args.size() < 3) return;
38 }
39 
TimerTick()40 void Sniffer::TimerTick() {}
41 
IncomingPacket(model::packets::LinkLayerPacketView packet)42 void Sniffer::IncomingPacket(model::packets::LinkLayerPacketView packet) {
43   Address source = packet.GetSourceAddress();
44   Address dest = packet.GetDestinationAddress();
45   bool match_source = device_to_sniff_ == source;
46   bool match_dest = device_to_sniff_ == dest;
47   if (!match_source && !match_dest) {
48     return;
49   }
50   LOG_INFO("%s %s -> %s (Type %s)",
51            (match_source ? (match_dest ? "<->" : "<--") : "-->"),
52            source.ToString().c_str(), dest.ToString().c_str(),
53            model::packets::PacketTypeText(packet.GetType()).c_str());
54 }
55 
56 }  // namespace test_vendor_lib
57