diff options
author | Flavio Santes <flavio.santes@intel.com> | 2017-01-29 00:09:56 -0600 |
---|---|---|
committer | Jukka Rissanen <jukka.rissanen@linux.intel.com> | 2017-02-03 15:59:18 +0200 |
commit | 76785f0bcdeaabbef561838d398e93fc670a11f3 (patch) | |
tree | c120e8524be2294b30b289788b1e1203cd8cf358 /samples | |
parent | 3a0518928435b86c18dbf1e046f9ab521b608933 (diff) |
samples/net/dns: Improve network configuration
- Reorganize some CONF variables and add the CONFIG_NET_SAMPLES_xxx
variables to the DNS sample application.
- Remove all the numeric IP addresses
- Use the CONFIG_NET_SAMPLES_xxx variables to define the IP addresses
Change-Id: Ib294d338f163f03ea1b47143b489d916578c532d
Signed-off-by: Flavio Santes <flavio.santes@intel.com>
Diffstat (limited to 'samples')
-rw-r--r-- | samples/net/dns_client/prj_arduino_101.conf | 11 | ||||
-rw-r--r-- | samples/net/dns_client/prj_frdm_k64f.conf | 10 | ||||
-rw-r--r-- | samples/net/dns_client/prj_qemu_x86.conf | 10 | ||||
-rw-r--r-- | samples/net/dns_client/src/config.h | 23 | ||||
-rw-r--r-- | samples/net/dns_client/src/main.c | 158 |
5 files changed, 138 insertions, 74 deletions
diff --git a/samples/net/dns_client/prj_arduino_101.conf b/samples/net/dns_client/prj_arduino_101.conf index 25ff2d9d3..5a5513857 100644 --- a/samples/net/dns_client/prj_arduino_101.conf +++ b/samples/net/dns_client/prj_arduino_101.conf @@ -1,5 +1,4 @@ CONFIG_NETWORKING=y -CONFIG_NET_IPV4=y CONFIG_NET_UDP=y CONFIG_RANDOM_GENERATOR=y CONFIG_TEST_RANDOM_GENERATOR=y @@ -9,10 +8,10 @@ CONFIG_NET_LOG=y CONFIG_INIT_STACKS=y CONFIG_NET_NBUF_RX_COUNT=16 CONFIG_NET_NBUF_TX_COUNT=16 +CONFIG_NET_NBUF_DATA_COUNT=16 CONFIG_NET_IPV6_RA_RDNSS=y CONFIG_NET_IFACE_UNICAST_IPV4_ADDR_COUNT=3 -CONFIG_NET_NBUF_DATA_COUNT=16 # ENC28J60 Ethernet Device CONFIG_ETH_ENC28J60=y @@ -28,11 +27,19 @@ CONFIG_SPI=y CONFIG_PRINTK=y #CONFIG_NET_DEBUG_NET_BUF=y +CONFIG_NET_IPV4=y # Enable IPv6 support CONFIG_NET_IPV6=n + # Enable the DNS resolver CONFIG_DNS_RESOLVER=y # Enable additional buffers CONFIG_DNS_RESOLVER_ADDITIONAL_BUF_CTR=0 # Enable additional queries CONFIG_DNS_RESOLVER_ADDITIONAL_QUERIES=1 + +CONFIG_NET_SAMPLES_IP_ADDRESSES=y +CONFIG_NET_SAMPLES_MY_IPV6_ADDR="2001:db8::1" +CONFIG_NET_SAMPLES_PEER_IPV6_ADDR="2001:db8::2" +CONFIG_NET_SAMPLES_MY_IPV4_ADDR="192.168.1.101" +CONFIG_NET_SAMPLES_PEER_IPV4_ADDR="192.168.1.10" diff --git a/samples/net/dns_client/prj_frdm_k64f.conf b/samples/net/dns_client/prj_frdm_k64f.conf index 6946c4adf..c9925fd03 100644 --- a/samples/net/dns_client/prj_frdm_k64f.conf +++ b/samples/net/dns_client/prj_frdm_k64f.conf @@ -1,5 +1,4 @@ CONFIG_NETWORKING=y -CONFIG_NET_IPV4=y CONFIG_NET_UDP=y CONFIG_RANDOM_GENERATOR=y CONFIG_NET_ARP=y @@ -8,16 +7,18 @@ CONFIG_NET_LOG=y CONFIG_INIT_STACKS=y CONFIG_NET_NBUF_RX_COUNT=16 CONFIG_NET_NBUF_TX_COUNT=16 +CONFIG_NET_NBUF_DATA_COUNT=16 CONFIG_NET_IPV6_RA_RDNSS=y CONFIG_NET_IFACE_UNICAST_IPV4_ADDR_COUNT=3 -CONFIG_NET_NBUF_DATA_COUNT=16 CONFIG_PRINTK=y #CONFIG_NET_DEBUG_NET_BUF=y +CONFIG_NET_IPV4=y # Enable IPv6 support CONFIG_NET_IPV6=n + # Enable the DNS resolver CONFIG_DNS_RESOLVER=y # Enable additional buffers @@ -25,3 +26,8 @@ CONFIG_DNS_RESOLVER_ADDITIONAL_BUF_CTR=0 # Enable additional queries CONFIG_DNS_RESOLVER_ADDITIONAL_QUERIES=1 +CONFIG_NET_SAMPLES_IP_ADDRESSES=y +CONFIG_NET_SAMPLES_MY_IPV6_ADDR="2001:db8::1" +CONFIG_NET_SAMPLES_PEER_IPV6_ADDR="2001:db8::2" +CONFIG_NET_SAMPLES_MY_IPV4_ADDR="192.168.1.101" +CONFIG_NET_SAMPLES_PEER_IPV4_ADDR="192.168.1.10" diff --git a/samples/net/dns_client/prj_qemu_x86.conf b/samples/net/dns_client/prj_qemu_x86.conf index 96d98816e..394104749 100644 --- a/samples/net/dns_client/prj_qemu_x86.conf +++ b/samples/net/dns_client/prj_qemu_x86.conf @@ -1,5 +1,4 @@ CONFIG_NETWORKING=y -CONFIG_NET_IPV4=y CONFIG_NET_UDP=y CONFIG_RANDOM_GENERATOR=y CONFIG_TEST_RANDOM_GENERATOR=y @@ -12,16 +11,23 @@ CONFIG_NET_NBUF_TX_COUNT=16 CONFIG_NET_IPV6_RA_RDNSS=y CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=3 CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT=2 -CONFIG_NET_MAX_CONTEXTS=10 CONFIG_PRINTK=y #CONFIG_NET_DEBUG_NET_BUF=y +CONFIG_NET_IPV4=y # Enable IPv6 support CONFIG_NET_IPV6=n + # Enable the DNS resolver CONFIG_DNS_RESOLVER=y # Enable additional buffers CONFIG_DNS_RESOLVER_ADDITIONAL_BUF_CTR=0 # Enable additional queries CONFIG_DNS_RESOLVER_ADDITIONAL_QUERIES=1 + +CONFIG_NET_SAMPLES_IP_ADDRESSES=y +CONFIG_NET_SAMPLES_MY_IPV6_ADDR="2001:db8::1" +CONFIG_NET_SAMPLES_PEER_IPV6_ADDR="2001:db8::2" +CONFIG_NET_SAMPLES_MY_IPV4_ADDR="192.168.1.101" +CONFIG_NET_SAMPLES_PEER_IPV4_ADDR="192.168.1.10" diff --git a/samples/net/dns_client/src/config.h b/samples/net/dns_client/src/config.h index d4c21799d..1c6d1c02d 100644 --- a/samples/net/dns_client/src/config.h +++ b/samples/net/dns_client/src/config.h @@ -7,18 +7,25 @@ #ifndef _CONFIG_H_ #define _CONFIG_H_ +#ifdef CONFIG_NET_SAMPLES_IP_ADDRESSES #ifdef CONFIG_NET_IPV6 -#define LOCAL_ADDR { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0,\ - 0, 0, 0, 0, 0, 0x1 } } } -#define REMOTE_ADDR { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0,\ - 0, 0, 0, 0, 0, 0x2 } } } +#define LOCAL_ADDR CONFIG_NET_SAMPLES_MY_IPV6_ADDR +#define REMOTE_ADDR CONFIG_NET_SAMPLES_PEER_IPV6_ADDR #else -#define LOCAL_ADDR { { { 192, 0, 2, 1 } } } -#define REMOTE_ADDR { { { 192, 0, 2, 2 } } } +#define LOCAL_ADDR CONFIG_NET_SAMPLES_MY_IPV4_ADDR +#define REMOTE_ADDR CONFIG_NET_SAMPLES_PEER_IPV4_ADDR +#endif +#else +#ifdef CONFIG_NET_IPV6 +#define LOCAL_ADDR "2001:db8::1" +#define REMOTE_ADDR "2001:db8::2" +#else +#define LOCAL_ADDR "192.168.1.101" +#define REMOTE_ADDR "192.168.1.10" +#endif #endif -#define MY_PORT 0 -#define PEER_PORT 5353 +#define REMOTE_PORT 5353 #define APP_SLEEP_MSECS 400 diff --git a/samples/net/dns_client/src/main.c b/samples/net/dns_client/src/main.c index 072395081..f5f7bd8c3 100644 --- a/samples/net/dns_client/src/main.c +++ b/samples/net/dns_client/src/main.c @@ -18,32 +18,13 @@ #define STACK_SIZE 2048 uint8_t stack[STACK_SIZE]; -static struct net_context *net_ctx; - -#ifdef CONFIG_NET_IPV6 - struct in6_addr local_addr = LOCAL_ADDR; - struct sockaddr_in6 local_sock = { - .sin6_family = AF_INET6, - .sin6_port = htons(MY_PORT), - .sin6_addr = LOCAL_ADDR }; - struct sockaddr_in6 remote_sock = { - .sin6_family = AF_INET6, - .sin6_port = htons(PEER_PORT), - .sin6_addr = REMOTE_ADDR }; - struct in6_addr addresses[MAX_ADDRESSES]; +#if CONFIG_NET_IPV6 + static struct in6_addr addresses[MAX_ADDRESSES]; #else - struct in_addr local_addr = LOCAL_ADDR; - struct sockaddr_in local_sock = { - .sin_family = AF_INET, - .sin_port = htons(MY_PORT), - .sin_addr = LOCAL_ADDR }; - struct sockaddr_in remote_sock = { - .sin_family = AF_INET, - .sin_port = htons(PEER_PORT), - .sin_addr = REMOTE_ADDR }; - struct in_addr addresses[MAX_ADDRESSES]; + static struct in_addr addresses[MAX_ADDRESSES]; #endif - +static struct net_context *net_ctx; +static struct sockaddr remote_sock; static char str[128]; static @@ -62,56 +43,34 @@ char *domains[] = {"not_a_real_domain_name", "www.google.com", NULL}; +static +int set_addr(struct sockaddr *sock_addr, const char *addr, uint16_t port); + +static +int set_local_addr(struct net_context **net_ctx, const char *local_addr); + void run_dns(void) { struct dns_context ctx; - void *p; int rc; int d; int i; - dns_init(&ctx); - -#ifdef CONFIG_NET_IPV6 - p = net_if_ipv6_addr_add(net_if_get_default(), &local_addr, - NET_ADDR_MANUAL, 0); -#else - p = net_if_ipv4_addr_add(net_if_get_default(), &local_addr, - NET_ADDR_MANUAL, 0); -#endif - if (p == NULL) { - printk("[%s:%d] Unable to add address to network interface\n", - __func__, __LINE__); + rc = set_local_addr(&net_ctx, LOCAL_ADDR); + if (rc) { return; } -#ifdef CONFIG_NET_IPV6 - rc = net_context_get(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, &net_ctx); -#else - rc = net_context_get(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &net_ctx); -#endif - if (rc != 0) { - printk("[%s:%d] Cannot get network context for IPv4 UDP: %d\n", - __func__, __LINE__, rc); - return; - } - -#ifdef CONFIG_NET_IPV6 - rc = net_context_bind(net_ctx, (struct sockaddr *)&local_sock, - sizeof(struct sockaddr_in6)); -#else - rc = net_context_bind(net_ctx, (struct sockaddr *)&local_sock, - sizeof(struct sockaddr_in)); -#endif - if (rc != 0) { - printk("[%s:%d] Cannot bind: %d\n", - __func__, __LINE__, rc); + rc = set_addr(&remote_sock, REMOTE_ADDR, REMOTE_PORT); + if (rc) { goto lb_exit; } + dns_init(&ctx); + ctx.net_ctx = net_ctx; ctx.timeout = APP_SLEEP_MSECS; - ctx.dns_server = (struct sockaddr *)&remote_sock; + ctx.dns_server = &remote_sock; ctx.elements = MAX_ADDRESSES; #ifdef CONFIG_NET_IPV6 ctx.query_type = DNS_QUERY_TYPE_AAAA; @@ -152,9 +111,88 @@ lb_exit: printk("\nBye!\n"); } - void main(void) { k_thread_spawn(stack, STACK_SIZE, (k_thread_entry_t)run_dns, NULL, NULL, NULL, K_PRIO_COOP(7), 0, 0); } + +static +int set_addr(struct sockaddr *sock_addr, const char *addr, uint16_t port) +{ + void *ptr; + int rc; + +#ifdef CONFIG_NET_IPV6 + net_sin6(sock_addr)->sin6_port = htons(port); + sock_addr->family = AF_INET6; + ptr = &(net_sin6(sock_addr)->sin6_addr); + rc = net_addr_pton(AF_INET6, addr, ptr); +#else + net_sin(sock_addr)->sin_port = htons(port); + sock_addr->family = AF_INET; + ptr = &(net_sin(sock_addr)->sin_addr); + rc = net_addr_pton(AF_INET, addr, ptr); +#endif + + if (rc) { + printk("Invalid IP address: %s\n", addr); + } + + return rc; +} + +static +int set_local_addr(struct net_context **net_ctx, const char *local_addr) +{ +#ifdef CONFIG_NET_IPV6 + socklen_t addr_len = sizeof(struct sockaddr_in6); + sa_family_t family = AF_INET6; + +#else + socklen_t addr_len = sizeof(struct sockaddr_in); + sa_family_t family = AF_INET; +#endif + struct sockaddr local_sock; + void *p; + int rc; + + rc = set_addr(&local_sock, local_addr, 0); + if (rc) { + printk("set_addr (local) error\n"); + return rc; + } + +#ifdef CONFIG_NET_IPV6 + p = net_if_ipv6_addr_add(net_if_get_default(), + &net_sin6(&local_sock)->sin6_addr, + NET_ADDR_MANUAL, 0); +#else + p = net_if_ipv4_addr_add(net_if_get_default(), + &net_sin(&local_sock)->sin_addr, + NET_ADDR_MANUAL, 0); +#endif + + if (!p) { + return -EINVAL; + } + + rc = net_context_get(family, SOCK_DGRAM, IPPROTO_UDP, net_ctx); + if (rc) { + printk("net_context_get error\n"); + return rc; + } + + rc = net_context_bind(*net_ctx, &local_sock, addr_len); + if (rc) { + printk("net_context_bind error\n"); + goto lb_exit; + } + + return 0; + +lb_exit: + net_context_put(*net_ctx); + + return rc; +} |