xref: /aosp_15_r20/external/webrtc/net/dcsctp/public/text_pcap_packet_observer.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 #include "net/dcsctp/public/text_pcap_packet_observer.h"
11 
12 #include "api/array_view.h"
13 #include "net/dcsctp/public/types.h"
14 #include "rtc_base/logging.h"
15 #include "rtc_base/strings/string_builder.h"
16 
17 namespace dcsctp {
18 
OnSentPacket(dcsctp::TimeMs now,rtc::ArrayView<const uint8_t> payload)19 void TextPcapPacketObserver::OnSentPacket(
20     dcsctp::TimeMs now,
21     rtc::ArrayView<const uint8_t> payload) {
22   PrintPacket("O ", name_, now, payload);
23 }
24 
OnReceivedPacket(dcsctp::TimeMs now,rtc::ArrayView<const uint8_t> payload)25 void TextPcapPacketObserver::OnReceivedPacket(
26     dcsctp::TimeMs now,
27     rtc::ArrayView<const uint8_t> payload) {
28   PrintPacket("I ", name_, now, payload);
29 }
30 
PrintPacket(absl::string_view prefix,absl::string_view socket_name,dcsctp::TimeMs now,rtc::ArrayView<const uint8_t> payload)31 void TextPcapPacketObserver::PrintPacket(
32     absl::string_view prefix,
33     absl::string_view socket_name,
34     dcsctp::TimeMs now,
35     rtc::ArrayView<const uint8_t> payload) {
36   rtc::StringBuilder s;
37   s << "\n" << prefix;
38   int64_t remaining = *now % (24 * 60 * 60 * 1000);
39   int hours = remaining / (60 * 60 * 1000);
40   remaining = remaining % (60 * 60 * 1000);
41   int minutes = remaining / (60 * 1000);
42   remaining = remaining % (60 * 1000);
43   int seconds = remaining / 1000;
44   int ms = remaining % 1000;
45   s.AppendFormat("%02d:%02d:%02d.%03d", hours, minutes, seconds, ms);
46   s << " 0000";
47   for (uint8_t byte : payload) {
48     s.AppendFormat(" %02x", byte);
49   }
50   s << " # SCTP_PACKET " << socket_name;
51   RTC_LOG(LS_VERBOSE) << s.str();
52 }
53 
54 }  // namespace dcsctp
55