diff options
author | Vinicius Costa Gomes <vinicius.gomes@intel.com> | 2017-01-12 13:15:21 -0800 |
---|---|---|
committer | Jukka Rissanen <jukka.rissanen@linux.intel.com> | 2017-01-27 12:35:50 +0200 |
commit | 3a666c54ce8fcc606890cd422e2c2649745a2129 (patch) | |
tree | 572e6d2d644fa86693c1678d52a65d1bd6835683 /samples | |
parent | c36c3c2883e7d209617c953b4c9bb1d51bff9fe4 (diff) |
samples/zoap_server: Also listen on the unicast address
Some applications are not prepared for using multicast address and
specifying the interface, so it may be convenient for the user to be
able to use a unicast address when communicating with the
samples. This aligns this sample with most of the networking samples.
Change-Id: Ic97ea5a346a650751b6e2cbfefad25a3e700278c
Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
Diffstat (limited to 'samples')
-rw-r--r-- | samples/net/zoap_server/src/zoap-server.c | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/samples/net/zoap_server/src/zoap-server.c b/samples/net/zoap_server/src/zoap-server.c index e2db7fc6f..8a00df4a5 100644 --- a/samples/net/zoap_server/src/zoap-server.c +++ b/samples/net/zoap_server/src/zoap-server.c @@ -37,6 +37,9 @@ #define ALL_NODES_LOCAL_COAP_MCAST \ { { { 0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xfd } } } +#define MY_IP6ADDR \ + { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1 } } } + static struct net_context *context; static const uint8_t plain_text_format; @@ -855,11 +858,13 @@ static void udp_receive(struct net_context *context, static bool join_coap_multicast_group(void) { + static struct in6_addr my_addr = MY_IP6ADDR; static struct sockaddr_in6 mcast_addr = { .sin6_family = AF_INET6, .sin6_addr = ALL_NODES_LOCAL_COAP_MCAST, .sin6_port = htons(MY_COAP_PORT) }; struct net_if_mcast_addr *mcast; + struct net_if_addr *ifaddr; struct net_if *iface; iface = net_if_get_default(); @@ -868,6 +873,18 @@ static bool join_coap_multicast_group(void) return false; } +#if defined(CONFIG_NET_SAMPLES_IP_ADDRESSES) + if (net_addr_pton(AF_INET6, + CONFIG_NET_SAMPLES_MY_IPV6_ADDR, + (struct sockaddr *)&my_addr) < 0) { + NET_ERR("Invalid IPv6 address %s", + CONFIG_NET_SAMPLES_MY_IPV6_ADDR); + } +#endif + + ifaddr = net_if_ipv6_addr_add(iface, &my_addr, NET_ADDR_MANUAL, 0); + ifaddr->addr_state = NET_ADDR_PREFERRED; + mcast = net_if_ipv6_maddr_add(iface, &mcast_addr.sin6_addr); if (!mcast) { NET_ERR("Could not add multicast address to interface\n"); @@ -881,7 +898,7 @@ void main(void) { static struct sockaddr_in6 any_addr = { .sin6_family = AF_INET6, - .sin6_addr = ALL_NODES_LOCAL_COAP_MCAST, + .sin6_addr = IN6ADDR_ANY_INIT, .sin6_port = htons(MY_COAP_PORT) }; int r; |