diff options
-rw-r--r-- | samples/net/dns_client/src/dns_pack.c | 50 | ||||
-rw-r--r-- | samples/net/dns_client/src/dns_pack.h | 68 | ||||
-rw-r--r-- | samples/net/dns_client/src/dns_utils.c | 2 | ||||
-rw-r--r-- | samples/net/dns_client/src/dns_utils.h | 2 | ||||
-rw-r--r-- | samples/net/dns_client/src/netz.c | 2 |
5 files changed, 82 insertions, 42 deletions
diff --git a/samples/net/dns_client/src/dns_pack.c b/samples/net/dns_client/src/dns_pack.c index bceffaf0b..ab28c5f20 100644 --- a/samples/net/dns_client/src/dns_pack.c +++ b/samples/net/dns_client/src/dns_pack.c @@ -18,6 +18,14 @@ #include <string.h> +static inline size_t dns_strlen(char *str) +{ + if (str == NULL) { + return 0; + } + return strlen(str); +} + int dns_msg_pack_qname(int *len, uint8_t *buf, int size, char *domain_name) { int lb_index; @@ -30,7 +38,7 @@ int dns_msg_pack_qname(int *len, uint8_t *buf, int size, char *domain_name) lb_size = 0; /* traverse the domain name str, including the null-terminator :) */ - for (i = 0; i < strlen(domain_name) + 1; i++) { + for (i = 0; i < dns_strlen(domain_name) + 1; i++) { if (lb_index >= size) { return -ENOMEM; } @@ -104,7 +112,8 @@ int dns_unpack_answer(struct dns_msg_t *dns_msg, int dname_ptr) case DNS_RR_TYPE_AAAA: dns_msg->response_type = DNS_RESPONSE_IP; dns_msg->response_position = dns_msg->answer_offset + 12; - dns_msg->response_length = dns_answer_rdlength(2, answer); + dns_msg->response_length = dns_unpack_answer_rdlength(2, + answer); return 0; @@ -112,7 +121,8 @@ int dns_unpack_answer(struct dns_msg_t *dns_msg, int dname_ptr) dns_msg->response_type = DNS_RESPONSE_CNAME_NO_IP; dns_msg->response_position = dns_msg->answer_offset + 12; - dns_msg->response_length = dns_answer_rdlength(2, answer); + dns_msg->response_length = dns_unpack_answer_rdlength(2, + answer); return 0; @@ -130,6 +140,7 @@ int dns_unpack_response_header(struct dns_msg_t *msg, int src_id) int size; int qdcount; int ancount; + int rcode; dns_header = msg->msg; size = msg->msg_size; @@ -138,7 +149,7 @@ int dns_unpack_response_header(struct dns_msg_t *msg, int src_id) return -ENOMEM; } - if (dns_header_id(dns_header) != src_id) { + if (dns_unpack_header_id(dns_header) != src_id) { return -EINVAL; } @@ -154,16 +165,17 @@ int dns_unpack_response_header(struct dns_msg_t *msg, int src_id) return -EINVAL; } - switch (dns_header_rcode(dns_header)) { + rcode = dns_header_rcode(dns_header); + switch (rcode) { case DNS_HEADER_NOERROR: break; default: - return dns_header_rcode(dns_header); + return rcode; } - qdcount = dns_header_qdcount(dns_header); - ancount = dns_header_ancount(dns_header); + qdcount = dns_unpack_header_qdcount(dns_header); + ancount = dns_unpack_header_ancount(dns_header); if (qdcount < 1 || ancount < 1) { return -EINVAL; } @@ -177,7 +189,7 @@ static int dns_msg_pack_query_header(uint8_t *buf, int size, uint16_t id) return -ENOMEM; } - *(uint16_t *)(buf + 0) = z_swap2(id); + *(uint16_t *)(buf + 0) = sys_cpu_to_be16(id); /* RD = 1, TC = 0, AA = 0, Opcode = 0, QR = 0 <-> 0x01 (1B) * RCode = 0, Z = 0, RA = 0 <-> 0x00 (1B) @@ -185,9 +197,11 @@ static int dns_msg_pack_query_header(uint8_t *buf, int size, uint16_t id) * QDCOUNT = 1 <-> 0x0001 (2B) */ - *(uint32_t *)(buf + 2) = 0x01000001; - *(uint32_t *)(buf + 6) = 0; - *(uint16_t *)(buf + 10) = 0; + *(buf + 2) = 0x01; + *(buf + 3) = 0x00; + *(uint16_t *)(buf + 4) = sys_cpu_to_be16(1); + *(uint32_t *)(buf + 6) = 0; /* ANCOUNT and NSCOUNT = 0 */ + *(uint16_t *)(buf + 10) = 0; /* ARCOUNT = 0 */ return 0; } @@ -219,9 +233,9 @@ int dns_msg_pack_query(struct app_buf_t *buf, char *domain_name, return rc; } /* QType */ - *(uint16_t *)(buf->buf + offset + 0) = z_swap2(qtype); + *(uint16_t *)(buf->buf + offset + 0) = sys_cpu_to_be16(qtype); /* QClass */ - *(uint16_t *)(buf->buf + offset + 2) = z_swap2(DNS_CLASS_IN); + *(uint16_t *)(buf->buf + offset + 2) = sys_cpu_to_be16(DNS_CLASS_IN); buf->length = offset + 4; @@ -243,6 +257,7 @@ int dns_find_null(int *qname_size, uint8_t *buf, int size) int dns_unpack_response_query(struct dns_msg_t *dns_msg) { uint8_t *dns_query; + uint8_t *buf; int remaining_size; int qname_size; int offset; @@ -265,12 +280,13 @@ int dns_unpack_response_query(struct dns_msg_t *dns_msg) return -ENOMEM; } - if (dns_query_qtype(dns_query + qname_size) != DNS_RR_TYPE_A && - dns_query_qtype(dns_query + qname_size) != DNS_RR_TYPE_AAAA) { + buf = dns_query + qname_size; + if (dns_unpack_query_qtype(buf) != DNS_RR_TYPE_A && + dns_unpack_query_qtype(buf) != DNS_RR_TYPE_AAAA) { return -EINVAL; } - if (dns_query_qclass(dns_query + qname_size) != DNS_CLASS_IN) { + if (dns_unpack_query_qclass(buf) != DNS_CLASS_IN) { return -EINVAL; } diff --git a/samples/net/dns_client/src/dns_pack.h b/samples/net/dns_client/src/dns_pack.h index 67b74f339..2eddfc9e5 100644 --- a/samples/net/dns_client/src/dns_pack.h +++ b/samples/net/dns_client/src/dns_pack.h @@ -21,7 +21,9 @@ #include <stddef.h> #include <errno.h> -#include <app_buf.h> +#include <misc/byteorder.h> + +#include "app_buf.h" /** * @brief dns_msg_t @@ -104,22 +106,19 @@ enum dns_header_rcode { */ #define DNS_MSG_HEADER_SIZE 12 -static inline uint32_t z_swap4(uint32_t n) -{ - return ((n&0x000000FF)<<24) + ((n&0x0000FF00)<<8) - + ((n&0x00FF0000)>>8) + ((n&0xFF000000)>>24); -} - -static inline uint16_t z_swap2(uint16_t n) +/** It returns the ID field in the DNS msg header */ +static inline int dns_header_id(uint8_t *header) { - return (n>>8) + ((n & 0x00FF) << 8); + return sys_cpu_to_be16(*(uint16_t *)header); } - -/** It returns the ID field in the DNS msg header */ -static inline int dns_header_id(uint8_t *header) +/* inline unpack routines are used to unpack data from network + * order to cpu. Similar routines without the unpack prefix are + * used for cpu to network order. + */ +static inline int dns_unpack_header_id(uint8_t *header) { - return z_swap2(*(uint16_t *)header); + return sys_be16_to_cpu(*(uint16_t *)header); } /** It returns the QR field in the DNS msg header */ @@ -173,40 +172,61 @@ static inline int dns_header_rcode(uint8_t *header) /** It returns the QDCOUNT field in the DNS msg header */ static inline int dns_header_qdcount(uint8_t *header) { - return z_swap2(*(uint16_t *)(header + 4)); + return sys_cpu_to_be16(*(uint16_t *)(header + 4)); +} + +static inline int dns_unpack_header_qdcount(uint8_t *header) +{ + return sys_be16_to_cpu(*(uint16_t *)(header + 4)); } /** It returns the ANCOUNT field in the DNS msg header */ static inline int dns_header_ancount(uint8_t *header) { - return z_swap2(*(uint16_t *)(header + 6)); + return sys_cpu_to_be16(*(uint16_t *)(header + 6)); +} + +static inline int dns_unpack_header_ancount(uint8_t *header) +{ + return sys_be16_to_cpu(*(uint16_t *)(header + 6)); } /** It returns the NSCOUNT field in the DNS msg header */ static inline int dns_header_nscount(uint8_t *header) { - return z_swap2(*(uint16_t *)(header + 8)); + return sys_cpu_to_be16(*(uint16_t *)(header + 8)); } /** It returns the ARCOUNT field in the DNS msg header */ static inline int dns_header_arcount(uint8_t *header) { - return z_swap2(*(uint16_t *)(header + 10)); + return sys_cpu_to_be16(*(uint16_t *)(header + 10)); } static inline int dns_query_qtype(uint8_t *question) { - return z_swap2(*((uint16_t *)(question + 0))); + return sys_cpu_to_be16(*((uint16_t *)(question + 0))); +} + +static inline int dns_unpack_query_qtype(uint8_t *question) +{ + return sys_be16_to_cpu(*((uint16_t *)(question + 0))); } static inline int dns_query_qclass(uint8_t *question) { - return z_swap2(*((uint16_t *)(question + 2))); + return sys_cpu_to_be16(*((uint16_t *)(question + 2))); +} + +static inline int dns_unpack_query_qclass(uint8_t *question) +{ + return sys_be16_to_cpu(*((uint16_t *)(question + 2))); } static inline int dns_response_type(int dname_size, uint8_t *answer) { /** Future versions must consider byte 0 + * 4.1.3. Resource record format * *(answer + dname_size + 0); */ return *(answer + dname_size + 1); @@ -215,6 +235,7 @@ static inline int dns_response_type(int dname_size, uint8_t *answer) static inline int dns_answer_class(int dname_size, uint8_t *answer) { /** Future versions must consider byte 2 + * 4.1.3. Resource record format * *(answer + dname_size + 2); */ return *(answer + dname_size + 3); @@ -222,12 +243,17 @@ static inline int dns_answer_class(int dname_size, uint8_t *answer) static inline int dns_answer_ttl(int dname_size, uint8_t *answer) { - return z_swap4(*(uint32_t *)(answer + dname_size + 4)); + return sys_cpu_to_be32(*(uint32_t *)(answer + dname_size + 4)); } static inline int dns_answer_rdlength(int dname_size, uint8_t *answer) { - return z_swap2(*(uint16_t *)(answer + dname_size + 8)); + return sys_cpu_to_be16(*(uint16_t *)(answer + dname_size + 8)); +} + +static inline int dns_unpack_answer_rdlength(int dname_size, uint8_t *answer) +{ + return sys_be16_to_cpu(*(uint16_t *)(answer + dname_size + 8)); } /** diff --git a/samples/net/dns_client/src/dns_utils.c b/samples/net/dns_client/src/dns_utils.c index 7ebccdcaa..7441c049d 100644 --- a/samples/net/dns_client/src/dns_utils.c +++ b/samples/net/dns_client/src/dns_utils.c @@ -137,7 +137,6 @@ int dns_print_readable_msg_label(int offset, uint8_t *buf, int size) offset = next; } } - printf("\n"); return 0; } @@ -149,7 +148,6 @@ int print_buf(uint8_t *buf, size_t size) for (i = 0; i < size; i++) { printf("%d ", buf[i]); } - printf("\n"); return 0; } diff --git a/samples/net/dns_client/src/dns_utils.h b/samples/net/dns_client/src/dns_utils.h index 934e1978e..865d990e0 100644 --- a/samples/net/dns_client/src/dns_utils.h +++ b/samples/net/dns_client/src/dns_utils.h @@ -17,7 +17,7 @@ #ifndef _DNS_UTILS_H_ #define _DNS_UTILS_H_ -#include <app_buf.h> +#include "app_buf.h" #include <stdint.h> int dns_print_msg_header(uint8_t *header, int size); diff --git a/samples/net/dns_client/src/netz.c b/samples/net/dns_client/src/netz.c index 329a92f18..b57932b22 100644 --- a/samples/net/dns_client/src/netz.c +++ b/samples/net/dns_client/src/netz.c @@ -14,7 +14,7 @@ * limitations under the License. */ -#include <netz.h> +#include "netz.h" #include <net/net_core.h> #include <net/net_socket.h> |