From 6435553dea2b496485b6d5a34ffa6608c5fcad89 Mon Sep 17 00:00:00 2001 From: Tomasz Bursztyka Date: Fri, 15 Feb 2019 09:45:09 +0100 Subject: [PATCH] net/context: Add a way to set/get ttl/hop_limit from net_context Since net_context_sendto_new() does not take a net_pkt anymore, the only way to set net_pkt's ttl/hop_limit is to pass it through net_context. Signed-off-by: Tomasz Bursztyka --- include/net/net_context.h | 28 ++++++++++++++++++++++++++++ subsys/net/ip/net_context.c | 5 +++++ 2 files changed, 33 insertions(+) diff --git a/include/net/net_context.h b/include/net/net_context.h index 06d93bc71a..e51833daf7 100644 --- a/include/net/net_context.h +++ b/include/net/net_context.h @@ -293,6 +293,12 @@ struct net_context { /** Network interface assigned to this context */ s8_t iface; + + /** IPv6 hop limit or IPv4 ttl for packets sent via this context. */ + union { + u8_t ipv6_hop_limit; + u8_t ipv4_ttl; + }; }; static inline bool net_context_is_used(struct net_context *context) @@ -491,6 +497,28 @@ static inline void net_context_set_iface(struct net_context *context, context->iface = net_if_get_by_iface(iface); } +static inline u8_t net_context_get_ipv4_ttl(struct net_context *context) +{ + return context->ipv4_ttl; +} + +static inline void net_context_set_ipv4_ttl(struct net_context *context, + u8_t ttl) +{ + context->ipv4_ttl = ttl; +} + +static inline u8_t net_context_get_ipv6_hop_limit(struct net_context *context) +{ + return context->ipv6_hop_limit; +} + +static inline void net_context_set_ipv6_hop_limit(struct net_context *context, + u8_t hop_limit) +{ + context->ipv6_hop_limit = hop_limit; +} + /** * @brief Get network context. * diff --git a/subsys/net/ip/net_context.c b/subsys/net/ip/net_context.c index cf476f5fb5..d06c44491d 100644 --- a/subsys/net/ip/net_context.c +++ b/subsys/net/ip/net_context.c @@ -875,6 +875,8 @@ int net_context_create_ipv4_new(struct net_context *context, } } + net_pkt_set_ipv4_ttl(pkt, net_context_get_ipv4_ttl(context)); + return net_ipv4_create_new(pkt, src, dst); } #endif /* CONFIG_NET_IPV4 */ @@ -921,6 +923,9 @@ int net_context_create_ipv6_new(struct net_context *context, (struct in6_addr *)dst); } + net_pkt_set_ipv6_hop_limit(pkt, + net_context_get_ipv6_hop_limit(context)); + return net_ipv6_create_new(pkt, src, dst); } #endif /* CONFIG_NET_IPV6 */