| 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 |
| 11 | #include "RawTcpSocket.h" |
| 12 |
| 13 | #include <stdint.h> |
| 14 | #include <string.h> |
| 15 |
| 16 | #include <algorithm> |
| 17 | #include <cstddef> |
| 18 | #include <cstdint> |
| 19 | #include <memory> |
| 20 |
| 21 | #include "api/array_view.h" |
| 22 | #include "rtc_base/byte_order.h" |
| 23 | #include "rtc_base/checks.h" |
| 24 | #include "rtc_base/logging.h" |
| 25 | #include "rtc_base/network/sent_packet.h" |
| 26 | #include "rtc_base/time_utils.h" // for TimeMillis |
| 27 |
| 28 | #if defined(WEBRTC_POSIX) |
| 29 | #include <errno.h> |
| 30 | #endif // WEBRTC_POSIX |
| 31 |
| 32 | namespace rtc { |
| 33 |
| 34 | static const size_t kMaxPacketSize = 64 * 1024; |
| 35 |
| 36 | static const size_t kBufSize = kMaxPacketSize + 4; |
| 37 |
| 38 | // RawTcpSocket |
| 39 | // Binds and connects `socket` and creates RawTcpSocket for |
| 40 | // it. Takes ownership of `socket`. Returns null if bind() or |
| 41 | // connect() fail (`socket` is destroyed in that case). |
| 42 | RawTcpSocket* RawTcpSocket::Create(Socket* socket, |
| 43 | const SocketAddress& bind_address, |
| 44 | const SocketAddress& remote_address) { |
| 45 | return new RawTcpSocket( |
| 46 | AsyncTCPSocketBase::ConnectSocket(socket, bind_address, remote_address)); |
| 47 | } |
| 48 |
| 49 | RawTcpSocket::RawTcpSocket(Socket* socket) |
| 50 | : AsyncTCPSocketBase(socket, kBufSize) {} |
| 51 |
| 52 | int RawTcpSocket::Send(const void* pv, |
| 53 | size_t cb, |
| 54 | const rtc::PacketOptions& options) { |
| 55 | if (cb > kBufSize) { |
| 56 | SetError(EMSGSIZE); |
| 57 | return -1; |
| 58 | } |
| 59 |
| 60 | // If we are blocking on send, then silently drop this packet |
| 61 | if (!IsOutBufferEmpty()) |
| 62 | return static_cast<int>(cb); |
| 63 |
| 64 | if (!did_send_mtproto_prologue_) { |
| 65 | did_send_mtproto_prologue_ = true; |
| 66 | uint32_t prologue = 0xeeeeeeee; |
| 67 | AppendToOutBuffer(&prologue, 4); |
| 68 | } |
| 69 |
| 70 | uint32_t pkt_len = (uint32_t)cb; |
| 71 | AppendToOutBuffer(&pkt_len, 4); |
| 72 | AppendToOutBuffer(pv, cb); |
| 73 |
| 74 | int res = FlushOutBuffer(); |
| 75 | if (res <= 0) { |
| 76 | // drop packet if we made no progress |
| 77 | ClearOutBuffer(); |
| 78 | return res; |
| 79 | } |
| 80 |
| 81 | rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis(), |
| 82 | options.info_signaled_after_sent); |
| 83 | CopySocketInformationToPacketInfo(cb, *this, false, &sent_packet.info); |
| 84 | SignalSentPacket(this, sent_packet); |
| 85 |
| 86 | // We claim to have sent the whole thing, even if we only sent partial |
| 87 | return static_cast<int>(cb); |
| 88 | } |
| 89 |
| 90 | size_t RawTcpSocket::ProcessInput(rtc::ArrayView<const uint8_t> data) { |
| 91 | SocketAddress remote_addr(GetRemoteAddress()); |
| 92 |
| 93 | size_t processed_bytes = 0; |
| 94 | while (true) { |
| 95 | size_t bytes_left = data.size() - processed_bytes; |
| 96 | if (bytes_left < 4) |
| 97 | return processed_bytes; |
| 98 |
| 99 | uint32_t pkt_len = rtc::GetLE32(data.data() + processed_bytes); |
| 100 | if (bytes_left < 4 + pkt_len) |
| 101 | return processed_bytes; |
| 102 |
| 103 | rtc::ReceivedPacket received_packet( |
| 104 | data.subview(processed_bytes + 4, pkt_len), remote_addr, |
| 105 | webrtc::Timestamp::Micros(rtc::TimeMicros())); |
| 106 | NotifyPacketReceived(received_packet); |
| 107 | processed_bytes += 4 + pkt_len; |
| 108 | } |
| 109 | } |
| 110 |
| 111 | } // namespace rtc |
| 112 |