void MatchPacketBuilder::buildIPv6_other(ByteList *msg, const ByteRange &data) const { addEthernet(msg); addIPv6(msg, data.size()); msg->add(data.data(), data.size()); }
void IPAddressV6::setFromBinary(ByteRange bytes) { if (bytes.size() != 16) { throw IPAddressFormatException("Invalid IPv6 binary data: length must " "be 16 bytes, got ", bytes.size()); } memcpy(&addr_.in6Addr_.s6_addr, bytes.data(), sizeof(in6_addr)); }
// public void IPAddressV4::setFromBinary(ByteRange bytes) { if (bytes.size() != 4) { throw IPAddressFormatException("Invalid IPv4 binary data: length must " "be 4 bytes, got ", bytes.size()); } memcpy(&addr_.inAddr_.s_addr, bytes.data(), sizeof(in_addr)); }
// public bool IPAddress::inSubnetWithMask(const IPAddress& subnet, ByteRange mask) const { auto mkByteArray4 = [&]() -> ByteArray4 { ByteArray4 ba{{0}}; std::memcpy(ba.data(), mask.begin(), std::min<size_t>(mask.size(), 4)); return ba; }; if (bitCount() == subnet.bitCount()) { if (isV4()) { return asV4().inSubnetWithMask(subnet.asV4(), mkByteArray4()); } else { ByteArray16 ba{{0}}; std::memcpy(ba.data(), mask.begin(), std::min<size_t>(mask.size(), 16)); return asV6().inSubnetWithMask(subnet.asV6(), ba); } } // an IPv4 address can never belong in a IPv6 subnet unless the IPv6 is a 6to4 // address and vice-versa if (isV6()) { const IPAddressV6& v6addr = asV6(); const IPAddressV4& v4subnet = subnet.asV4(); if (v6addr.is6To4()) { return v6addr.getIPv4For6To4().inSubnetWithMask(v4subnet, mkByteArray4()); } } else if (subnet.isV6()) { const IPAddressV6& v6subnet = subnet.asV6(); const IPAddressV4& v4addr = asV4(); if (v6subnet.is6To4()) { return v4addr.inSubnetWithMask(v6subnet.getIPv4For6To4(), mkByteArray4()); } } return false; }
void MacAddress::setFromBinary(ByteRange value) { if (value.size() != SIZE) { throw invalid_argument(to<string>("MAC address must be 6 bytes " "long, got ", value.size())); } memcpy(bytes_ + 2, value.begin(), SIZE); }
Expected<IPAddress, IPAddressFormatError> IPAddress::tryFromBinary( ByteRange bytes) noexcept { // Check IPv6 first since it's our main protocol. if (bytes.size() == 16) { return IPAddressV6::tryFromBinary(bytes); } else if (bytes.size() == 4) { return IPAddressV4::tryFromBinary(bytes); } else { return makeUnexpected(IPAddressFormatError::UNSUPPORTED_ADDR_FAMILY); } }
// public static IPAddress IPAddress::fromBinary(ByteRange bytes) { if (bytes.size() == 4) { return IPAddress(IPAddressV4::fromBinary(bytes)); } else if (bytes.size() == 16) { return IPAddress(IPAddressV6::fromBinary(bytes)); } else { string hexval = detail::Bytes::toHex(bytes.data(), bytes.size()); throw IPAddressFormatException( sformat("Invalid address with hex value '{}'", hexval)); } }
void MatchPacketBuilder::buildTCPv4(ByteList *msg, const ByteRange &data) const { addEthernet(msg); addIPv4(msg, sizeof(pkt::TCPHdr) + data.size()); pkt::TCPHdr tcp; std::memset(&tcp, 0, sizeof(tcp)); tcp.srcPort = srcPort_; tcp.dstPort = dstPort_; tcp.cksum = pkt::Checksum({&tcp, sizeof(tcp)}, data); msg->add(&tcp, sizeof(tcp)); msg->add(data.data(), data.size()); }
void MatchPacketBuilder::buildICMPv4(ByteList *msg, const ByteRange &data) const { addEthernet(msg); addIPv4(msg, sizeof(pkt::ICMPHdr) + data.size()); pkt::ICMPHdr icmp; std::memset(&icmp, 0, sizeof(icmp)); icmp.type = icmpType_; icmp.code = icmpCode_; icmp.cksum = pkt::Checksum({&icmp, sizeof(icmp)}, data); msg->add(&icmp, sizeof(icmp)); msg->add(data.data(), data.size()); }
bool ZSTDStreamCodec::doCompressStream( ByteRange& input, MutableByteRange& output, StreamCodec::FlushOp flushOp) { if (needReset_) { resetCCtx(); needReset_ = false; } ZSTD_inBuffer in = {input.data(), input.size(), 0}; ZSTD_outBuffer out = {output.data(), output.size(), 0}; SCOPE_EXIT { input.uncheckedAdvance(in.pos); output.uncheckedAdvance(out.pos); }; size_t const rc = zstdThrowIfError(ZSTD_compressStream2( cctx_.get(), &out, &in, zstdTranslateFlush(flushOp))); switch (flushOp) { case StreamCodec::FlushOp::NONE: return false; case StreamCodec::FlushOp::FLUSH: case StreamCodec::FlushOp::END: return rc == 0; default: throw std::invalid_argument("ZSTD: invalid FlushOp"); } }
void writeFileAtomic(StringPiece filename, ByteRange data, mode_t permissions) { iovec iov; iov.iov_base = const_cast<unsigned char*>(data.data()); iov.iov_len = data.size(); auto rc = writeFileAtomicNoThrow(filename, &iov, 1, permissions); checkPosixError(rc, "writeFileAtomic() failed to update ", filename); }
bool RequestForward::validateInput(Validation *context) const { const ByteRange req = request(); const Header *reqHeader = reinterpret_cast<const Header *>(req.data()); if (reqHeader->length() != req.size()) { log_info("RequestForward inner request size is invalid"); return false; } return true; }
RecordInfo validateRecord(ByteRange range, uint32_t fileId) { if (range.size() <= headerSize()) { // records may not be empty return {0, {}}; } const Header* header = reinterpret_cast<const Header*>(range.begin()); range.advance(sizeof(Header)); if (header->magic != Header::kMagic || header->version != 0 || header->hashFunction != 0 || header->flags != 0 || (fileId != 0 && header->fileId != fileId) || header->dataLength > range.size()) { return {0, {}}; } if (headerHash(*header) != header->headerHash) { return {0, {}}; } range.reset(range.begin(), header->dataLength); if (dataHash(range) != header->dataHash) { return {0, {}}; } return {header->fileId, range}; }
void MatchPacketBuilder::buildICMPv6(ByteList *msg, const ByteRange &data) const { const size_t len = sizeof(pkt::ICMPHdr) + data.size(); addEthernet(msg); addIPv6(msg, len); pkt::IPv6PseudoHdr pseudoHdr; pseudoHdr.src = ipv6Src_; pseudoHdr.dst = ipv6Dst_; pseudoHdr.upperLength = UInt32_narrow_cast(len); pseudoHdr.nextHeader = ipProto_; pkt::ICMPHdr icmp; std::memset(&icmp, 0, sizeof(icmp)); icmp.type = icmpType_; icmp.code = icmpCode_; icmp.cksum = pkt::Checksum({&pseudoHdr, sizeof(pseudoHdr)}, {&icmp, sizeof(icmp)}, data); msg->add(&icmp, sizeof(icmp)); msg->add(data.data(), data.size()); }
bool PktFilter::match(ByteRange data, size_t totalLen) const { // Empty filter never matches. if (empty()) return false; struct pcap_pkthdr hdr; hdr.ts.tv_sec = 0; hdr.ts.tv_usec = 0; hdr.caplen = UInt32_narrow_cast(data.size()); hdr.len = UInt32_narrow_cast(totalLen); assert(prog_.bf_insns != nullptr); return pcap_offline_filter(&prog_, &hdr, data.data()) > 0; }
bool ZSTDStreamCodec::doUncompressStream( ByteRange& input, MutableByteRange& output, StreamCodec::FlushOp) { if (needReset_) { resetDCtx(); needReset_ = false; } ZSTD_inBuffer in = {input.data(), input.size(), 0}; ZSTD_outBuffer out = {output.data(), output.size(), 0}; SCOPE_EXIT { input.uncheckedAdvance(in.pos); output.uncheckedAdvance(out.pos); }; size_t const rc = zstdThrowIfError(ZSTD_decompressStream(dctx_.get(), &out, &in)); return rc == 0; }
void Normalize::normalizeQueueGetConfigReplyV2() { // Pad all queues to a multiple of 8. You can only get a queue whose size is // not a multiple of 8 if it contains an experimenter property with an // unusual length. Header *hdr = header(); size_t length = hdr->length(); if (length < sizeof(QueueGetConfigReply)) return; ByteRange data = SafeByteRange(buf_.mutableData(), buf_.size(), sizeof(QueueGetConfigReply)); size_t remaining = data.size(); const UInt8 *ptr = data.data(); ByteList newBuf; while (remaining > 16) { // Read 16-bit queue length from a potentially mis-aligned position. UInt16 queueLen = Big16_unaligned(ptr + 8); if (queueLen > remaining || queueLen < 16) return; // Copy queue header. newBuf.add(ptr, 16); // Iterate over properties and pad out the properties whose sizes are not // multiples of 8. const UInt8 *prop = ptr + 16; size_t propLeft = queueLen - 16; while (propLeft > 4) { UInt16 propSize = Big16_unaligned(prop + 2); if (propSize > propLeft || propSize < 4) return; newBuf.add(prop, propSize); if ((propSize % 8) != 0) { newBuf.addZeros(PadLength(propSize) - propSize); } prop += propSize; propLeft -= propSize; } if (propLeft != 0) { log_debug("normalizeQueueGetConfigReplyV2: propLeft != 0"); return; } ptr += queueLen; assert(prop == ptr); remaining -= queueLen; } if (remaining != 0) { log_debug("normalizeQueueGetConfigReplyV2: remaining != 0"); return; } // When padding the regular `Queue` structure, we may exceed the max // message size of 65535. if (newBuf.size() > 65535 - sizeof(QueueGetConfigReply)) { markInputTooBig("QueueGetConfigReply is too big"); return; } buf_.replace(data.begin(), data.end(), newBuf.data(), newBuf.size()); }
void writeFileAtomic(StringPiece filename, ByteRange data, mode_t permissions) { iovec iov; iov.iov_base = const_cast<unsigned char*>(data.data()); iov.iov_len = data.size(); writeFileAtomic(filename, &iov, 1, permissions); }
IOBuf::IOBuf(WrapBufferOp op, ByteRange br) : IOBuf(op, br.data(), br.size()) { }
IOBuf::IOBuf(CopyBufferOp op, ByteRange br, uint64_t headroom, uint64_t minTailroom) : IOBuf(op, br.data(), br.size(), headroom, minTailroom) { }
void checkBuf(ByteRange buf, boost::mt19937& gen) { checkBuf(buf.data(), buf.size(), gen); }
bool are_equal(const ByteRange& lhs, const ByteRange& rhs) { return lhs.size() == rhs.size() && !memcmp(lhs.begin(), rhs.begin(), rhs.size()); }
ExperimenterBuilder::ExperimenterBuilder(const Experimenter *msg) : msg_{*msg} { ByteRange expData = msg->expData(); setExpData(expData.data(), expData.size()); }
IOBuf::IOBuf(WrapBufferOp op, ByteRange br) : IOBuf(op, br.data(), folly::to<uint32_t>(br.size())) { }