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 #define LOG_TAG "sniffer"
18
19 #include "sniffer.h"
20
21 #include "osi/include/log.h"
22
23 #include "model/setup/device_boutique.h"
24
25 using std::vector;
26
27 namespace test_vendor_lib {
28
29 bool Sniffer::registered_ = DeviceBoutique::Register(LOG_TAG, &Sniffer::Create);
30
Sniffer()31 Sniffer::Sniffer() {}
32
Initialize(const vector<std::string> & args)33 void Sniffer::Initialize(const vector<std::string>& args) {
34 if (args.size() < 2) return;
35
36 if (Address::FromString(args[1], device_to_sniff_)) {
37 properties_.SetAddress(device_to_sniff_);
38 }
39
40 if (args.size() < 3) return;
41 }
42
TimerTick()43 void Sniffer::TimerTick() {}
44
IncomingPacket(packets::LinkLayerPacketView packet)45 void Sniffer::IncomingPacket(packets::LinkLayerPacketView packet) {
46 Address source = packet.GetSourceAddress();
47 Address dest = packet.GetDestinationAddress();
48 bool match_source = device_to_sniff_ == source;
49 bool match_dest = device_to_sniff_ == dest;
50 if (!match_source && !match_dest) {
51 return;
52 }
53 LOG_INFO(LOG_TAG, "%s %s -> %s (Type %d)", (match_source ? (match_dest ? "<->" : "<--") : "-->"),
54 source.ToString().c_str(), dest.ToString().c_str(), static_cast<int>(packet.GetType()));
55 }
56
57 } // namespace test_vendor_lib
58