Skip to content

Commit e2d9689

Browse files
committed
read ether header and only process IPv4 packets
1 parent db788b0 commit e2d9689

3 files changed

Lines changed: 24 additions & 9 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
*.out
3232
*.app
3333

34+
*.swp
35+
3436
# Typical build workspace directory
3537
build
3638

proto/packet.proto

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ syntax = "proto3";
22

33
package ypcap0;
44

5-
message Packet {
6-
string srcAddress = 1;
7-
string dstAddress = 2;
8-
int32 srcPort = 3;
9-
int32 dstPort = 4;
10-
int32 size = 5;
5+
message IpPacket {
6+
bool v6 = 1;
7+
string srcAddress = 2;
8+
string dstAddress = 3;
9+
uint32 srcPort = 4;
10+
uint32 dstPort = 5;
11+
uint32 size = 6;
1112
}
1213

src/y-pcap-service.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <boost/beast/core/detail/base64.hpp>
2+
#include <net/ethernet.h>
23
#include <netinet/ip.h>
34
#include "packet.pb.h"
45
#include "y-pcap-service.h"
@@ -140,11 +141,22 @@ int YPcap0Service::Process(pcap_t *handle, int cnt) const {
140141

141142
void YPcap0Service::HandlePacket(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes) {
142143
std::cout << "=====================================" << std::endl;
143-
std::cout << "Sniffed a packet with length of " << h->len << std::endl;
144-
ypcap0::Packet pkt;
145-
struct ip *ip_header = (struct ip*)(bytes + 14);
144+
std::cout << "Packet Length: " << h->len << std::endl;
145+
146+
struct ether_header *eth_hdr = (struct ether_header*)bytes;
147+
u_short ether_type = ntohs(eth_hdr->ether_type);
148+
149+
std::cout << "Ether Type: " << ether_type << std::endl;
150+
151+
if (ether_type != ETHERTYPE_IP)
152+
return;
153+
154+
ypcap0::IpPacket pkt;
155+
struct ip *ip_header = (struct ip*)(bytes + sizeof(struct ether_header));
146156
char* base64 = new char[256];
147157

158+
std::cout << "IP Version: " << ip_header->ip_v << std::endl;
159+
148160
char src_ip[INET_ADDRSTRLEN], dst_ip[INET_ADDRSTRLEN];
149161
inet_ntop(AF_INET, &(ip_header->ip_src), src_ip, INET_ADDRSTRLEN);
150162
inet_ntop(AF_INET, &(ip_header->ip_dst), dst_ip, INET_ADDRSTRLEN);

0 commit comments

Comments
 (0)