Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions src/net/memcache_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ using namespace std;
MemcacheCommand MemcacheCommand::create(const Packet& pkt,
const bpf_u_int32 captureAddress)
{
static ssize_t ether_header_sz = sizeof(struct ether_header);
static ssize_t ip_sz = sizeof(struct ip);
static ssize_t tcphdr_sz = sizeof(struct tcphdr);

const struct ether_header* ethernetHeader;
const struct ip* ipHeader;
const struct tcphdr* tcpHeader;

const Packet::Header* pkthdr = &pkt.getHeader();
const Packet::Data* packet = pkt.getData();

Expand All @@ -46,14 +38,16 @@ MemcacheCommand MemcacheCommand::create(const Packet& pkt,

// must be an IP packet
// TODO add support for dumping localhost
ethernetHeader = (struct ether_header*)packet;
const struct ether_header* ethernetHeader = (struct ether_header*)packet;
ssize_t ethernetHeaderSize = sizeof(struct ether_header); //14 bytes
auto etype = ntohs(ethernetHeader->ether_type);
if (etype != ETHERTYPE_IP) {
return MemcacheCommand();
}

// must be TCP - TODO add support for UDP
ipHeader = (struct ip*)(packet + ether_header_sz);
const struct ip* ipHeader = (struct ip*)(packet + ethernetHeaderSize);
ssize_t ipHeaderSize = ipHeader->ip_hl * 4;
auto itype = ipHeader->ip_p;
if (itype != IPPROTO_TCP) {
return MemcacheCommand();
Expand All @@ -69,15 +63,20 @@ MemcacheCommand MemcacheCommand::create(const Packet& pkt,
// FIXME will remove once we add back the direction parsing
(void)possible_request;

tcpHeader = (struct tcphdr*)(packet + ether_header_sz + ip_sz);
const struct tcphdr* tcpHeader = (struct tcphdr*)(packet + ethernetHeaderSize
+ ipHeaderSize);
ssize_t tcpHeaderSize = tcpHeader->doff * 4;
(void)tcpHeader;
data = (u_char*)(packet + ether_header_sz + ip_sz + tcphdr_sz);
dataLength = pkthdr->len - (ether_header_sz + ip_sz + tcphdr_sz);
data = (u_char*)(packet + ethernetHeaderSize + ipHeaderSize + tcpHeaderSize);
dataLength = pkthdr->len - (ethernetHeaderSize + ipHeaderSize + tcpHeaderSize);
if (dataLength > pkthdr->caplen) {
dataLength = pkthdr->caplen;
}

// TODO revert to detecting request/response and doing the right thing
if (dataLength <= 0) {
return MemcacheCommand();
}
return MemcacheCommand::makeResponse(data, dataLength, sourceAddress);
}

Expand Down