Skip to content

Commit 6c826ef

Browse files
Cristib05jhedberg
authored andcommitted
net: context: Add support for setting hop limit from ancillary data
Add support to parse over msghdr and set pkt hop limit, if needed, when sendmsg() is used. Signed-off-by: Cristian Bulacu <[email protected]>
1 parent 05f8edf commit 6c826ef

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

subsys/net/ip/net_context.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2332,6 +2332,30 @@ static void set_pkt_txtime(struct net_pkt *pkt, const struct msghdr *msghdr)
23322332
}
23332333
}
23342334

2335+
static void set_pkt_hoplimit(struct net_pkt *pkt, const struct msghdr *msg_hdr)
2336+
{
2337+
struct cmsghdr *cmsg;
2338+
2339+
for (cmsg = CMSG_FIRSTHDR(msg_hdr); cmsg != NULL;
2340+
cmsg = CMSG_NXTHDR(msg_hdr, cmsg)) {
2341+
if (net_pkt_family(pkt) == AF_INET6) {
2342+
if (cmsg->cmsg_len == CMSG_LEN(sizeof(int)) &&
2343+
cmsg->cmsg_level == IPPROTO_IPV6 &&
2344+
cmsg->cmsg_type == IPV6_HOPLIMIT) {
2345+
net_pkt_set_ipv6_hop_limit(pkt, *(uint8_t *)CMSG_DATA(cmsg));
2346+
break;
2347+
}
2348+
} else {
2349+
if (cmsg->cmsg_len == CMSG_LEN(sizeof(int)) &&
2350+
cmsg->cmsg_level == IPPROTO_IP &&
2351+
cmsg->cmsg_type == IP_TTL) {
2352+
net_pkt_set_ipv4_ttl(pkt, *(uint8_t *)CMSG_DATA(cmsg));
2353+
break;
2354+
}
2355+
}
2356+
}
2357+
}
2358+
23352359
static int context_sendto(struct net_context *context,
23362360
const void *buf,
23372361
size_t len,
@@ -2650,6 +2674,9 @@ static int context_sendto(struct net_context *context,
26502674
set_pkt_txtime(pkt, msghdr);
26512675
}
26522676
}
2677+
2678+
set_pkt_hoplimit(pkt, msghdr);
2679+
26532680
}
26542681

26552682
skip_alloc:

0 commit comments

Comments
 (0)