Skip to content

Commit 8bf620d

Browse files
feat: Add hop count estimation
1 parent cc4f617 commit 8bf620d

File tree

8 files changed

+37
-11
lines changed

8 files changed

+37
-11
lines changed

include/globvar.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ struct fh_context {
3131
/* -6 */ int use_ipv6;
3232
/* -d */ int daemon;
3333
/* -f */ int skipfw;
34+
/* -g */ int nohopest;
3435
/* -h */ const char *hostname;
3536
/* -i */ const char *iface;
3637
/* -k */ int killproc;

include/ipv4pkt.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
#include <netinet/tcp.h>
2626

2727
int fh_pkt4_parse(void *pkt_data, int pkt_len, struct sockaddr *saddr,
28-
struct sockaddr *daddr, struct tcphdr **tcph_ptr,
29-
int *tcp_payload_len);
28+
struct sockaddr *daddr, uint8_t *ttl,
29+
struct tcphdr **tcph_ptr, int *tcp_payload_len);
3030

3131
int fh_pkt4_make(char *buffer, size_t buffer_size, struct sockaddr *saddr,
3232
struct sockaddr *daddr, uint16_t sport_be, uint16_t dport_be,

include/ipv6pkt.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
#include <netinet/tcp.h>
2626

2727
int fh_pkt6_parse(void *pkt_data, int pkt_len, struct sockaddr *saddr,
28-
struct sockaddr *daddr, struct tcphdr **tcph_ptr,
29-
int *tcp_payload_len);
28+
struct sockaddr *daddr, uint8_t *ttl,
29+
struct tcphdr **tcph_ptr, int *tcp_payload_len);
3030

3131
int fh_pkt6_make(char *buffer, size_t buffer_size, struct sockaddr *saddr,
3232
struct sockaddr *daddr, uint16_t sport_be, uint16_t dport_be,

src/globvar.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ struct fh_context g_ctx = {.exit = 0,
3030
/* -6 */ .use_ipv6 = 0,
3131
/* -d */ .daemon = 0,
3232
/* -f */ .skipfw = 0,
33+
/* -g */ .nohopest = 0,
3334
/* -h */ .hostname = NULL,
3435
/* -i */ .iface = NULL,
3536
/* -k */ .killproc = 0,

src/ipv4pkt.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
#include "logging.h"
3535

3636
int fh_pkt4_parse(void *pkt_data, int pkt_len, struct sockaddr *saddr,
37-
struct sockaddr *daddr, struct tcphdr **tcph_ptr,
38-
int *tcp_payload_len)
37+
struct sockaddr *daddr, uint8_t *ttl,
38+
struct tcphdr **tcph_ptr, int *tcp_payload_len)
3939
{
4040
struct iphdr *iph;
4141
struct tcphdr *tcph;
@@ -83,6 +83,7 @@ int fh_pkt4_parse(void *pkt_data, int pkt_len, struct sockaddr *saddr,
8383
daddr_in->sin_family = AF_INET;
8484
daddr_in->sin_addr.s_addr = iph->daddr;
8585

86+
*ttl = iph->ttl;
8687
*tcph_ptr = tcph;
8788
*tcp_payload_len = pkt_len - iph_len - tcph_len;
8889

src/ipv6pkt.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
#include "logging.h"
3535

3636
int fh_pkt6_parse(void *pkt_data, int pkt_len, struct sockaddr *saddr,
37-
struct sockaddr *daddr, struct tcphdr **tcph_ptr,
38-
int *tcp_payload_len)
37+
struct sockaddr *daddr, uint8_t *ttl,
38+
struct tcphdr **tcph_ptr, int *tcp_payload_len)
3939
{
4040
struct ip6_hdr *ip6h;
4141
struct tcphdr *tcph;
@@ -79,6 +79,7 @@ int fh_pkt6_parse(void *pkt_data, int pkt_len, struct sockaddr *saddr,
7979
daddr_in6->sin6_family = AF_INET6;
8080
memcpy(&daddr_in6->sin6_addr, &ip6h->ip6_dst, sizeof(struct in6_addr));
8181

82+
*ttl = ip6h->ip6_hlim;
8283
*tcph_ptr = tcph;
8384
*tcp_payload_len = pkt_len - ip6h_len - tcph_len;
8485

src/mainfun.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ static void print_usage(const char *name)
5454
" -6 enable IPv6\n"
5555
" -d run as a daemon\n"
5656
" -f skip firewall rules\n"
57+
" -g disable hop count estimation\n"
5758
" -h <hostname> hostname for obfuscation (required)\n"
5859
" -i <interface> network interface name (required)\n"
5960
" -k kill the running process\n"

src/rawsend.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@
3636

3737
static int sockfd = -1;
3838

39+
static int hop_estimate(uint8_t ttl)
40+
{
41+
if (ttl <= 64) {
42+
return 64 - ttl;
43+
} else if (ttl <= 128) {
44+
return 128 - ttl;
45+
} else {
46+
return 255 - ttl;
47+
}
48+
}
49+
3950
static void ipaddr_to_str(struct sockaddr *addr, char ipstr[INET6_ADDRSTRLEN])
4051
{
4152
static const char invalid[] = "INVALID";
@@ -220,7 +231,8 @@ int fh_rawsend_handle(struct sockaddr_ll *sll, uint8_t *pkt_data, int pkt_len)
220231
{
221232
uint32_t ack_new;
222233
uint16_t ethertype;
223-
int res, i, tcp_payload_len;
234+
int res, i, tcp_payload_len, hop;
235+
uint8_t src_ttl;
224236
struct tcphdr *tcph;
225237
char src_ip[INET6_ADDRSTRLEN], dst_ip[INET6_ADDRSTRLEN];
226238
struct sockaddr_storage saddr_store, daddr_store;
@@ -231,14 +243,14 @@ int fh_rawsend_handle(struct sockaddr_ll *sll, uint8_t *pkt_data, int pkt_len)
231243

232244
ethertype = ntohs(sll->sll_protocol);
233245
if (g_ctx.use_ipv4 && ethertype == ETHERTYPE_IP) {
234-
res = fh_pkt4_parse(pkt_data, pkt_len, saddr, daddr, &tcph,
246+
res = fh_pkt4_parse(pkt_data, pkt_len, saddr, daddr, &src_ttl, &tcph,
235247
&tcp_payload_len);
236248
if (res < 0) {
237249
E(T(fh_pkt4_parse));
238250
return -1;
239251
}
240252
} else if (g_ctx.use_ipv6 && ethertype == ETHERTYPE_IPV6) {
241-
res = fh_pkt6_parse(pkt_data, pkt_len, saddr, daddr, &tcph,
253+
res = fh_pkt6_parse(pkt_data, pkt_len, saddr, daddr, &src_ttl, &tcph,
242254
&tcp_payload_len);
243255
if (res < 0) {
244256
E(T(fh_pkt6_parse));
@@ -254,6 +266,15 @@ int fh_rawsend_handle(struct sockaddr_ll *sll, uint8_t *pkt_data, int pkt_len)
254266
ipaddr_to_str(daddr, dst_ip);
255267
}
256268

269+
if (!g_ctx.nohopest) {
270+
hop = hop_estimate(src_ttl);
271+
if (hop <= g_ctx.ttl) {
272+
E_INFO("%s:%u ===LOCAL(?)===> %s:%u", src_ip, ntohs(tcph->source),
273+
dst_ip, ntohs(tcph->dest));
274+
return 0;
275+
}
276+
}
277+
257278
if (tcp_payload_len > 0) {
258279
E_INFO("%s:%u ===PAYLOAD(?)===> %s:%u", src_ip, ntohs(tcph->source),
259280
dst_ip, ntohs(tcph->dest));

0 commit comments

Comments
 (0)