Commit 6ac81451 authored by Toon Stegen's avatar Toon Stegen

Combine common functions of mDNS and DNS

Functions that are shared by the DNS client and the mDNS client are
combined in pico_dns_common. There is still some overlap between the two
modules, so this should be refactored to make the common part even
bigger.
parent 37fc347a
......@@ -13,6 +13,7 @@
#include "pico_ipv4.h"
#include "pico_ipv6.h"
#include "pico_dns_client.h"
#include "pico_dns_common.h"
#include "pico_tree.h"
#ifdef PICO_SUPPORT_DNS_CLIENT
......@@ -218,14 +219,6 @@ static struct pico_dns_query *pico_dns_client_find_query(uint16_t id)
return NULL;
}
/* determine len of string */
uint16_t pico_dns_client_strlen(const char *url)
{
if (!url)
return 0;
return (uint16_t)strlen(url);
}
/* seek end of string */
static char *pico_dns_client_seek(char *ptr)
{
......@@ -237,42 +230,6 @@ static char *pico_dns_client_seek(char *ptr)
return ptr + 1;
}
/* mirror ip6 */
/* mirror ip address numbers
* f.e. 192.168.0.1 => 1.0.168.192 */
int8_t pico_dns_client_mirror(char *ptr)
{
const unsigned char *addr = NULL;
char *m = ptr;
uint32_t ip = 0;
int8_t i = 0;
if (pico_string_to_ipv4(ptr, &ip) < 0)
return -1;
ptr = m;
addr = (unsigned char *)&ip;
for (i = 3; i >= 0; i--) {
if (addr[i] > 99) {
*ptr++ = (char)('0' + (addr[i] / 100));
*ptr++ = (char)('0' + ((addr[i] % 100) / 10));
*ptr++ = (char)('0' + ((addr[i] % 100) % 10));
} else if(addr[i] > 9) {
*ptr++ = (char)('0' + (addr[i] / 10));
*ptr++ = (char)('0' + (addr[i] % 10));
} else {
*ptr++ = (char)('0' + addr[i]);
}
if(i > 0)
*ptr++ = '.';
}
*ptr = '\0';
return 0;
}
static struct pico_dns_query *pico_dns_client_idcheck(uint16_t id)
{
struct pico_dns_query test = {
......@@ -312,55 +269,6 @@ int pico_dns_client_query_header(struct pico_dns_header *pre)
return 0;
}
int pico_dns_client_query_suffix(struct pico_dns_query_suffix *suf, uint16_t type, uint16_t qclass)
{
suf->qtype = short_be(type);
suf->qclass = short_be(qclass);
return 0;
}
/* replace '.' in the domain name by the label length
* f.e. www.google.be => 3www6google2be0 */
int pico_dns_client_query_domain(char *ptr)
{
char p = 0, *label = NULL;
uint8_t len = 0;
if (!ptr)
return -1;
label = ptr++;
while ((p = *ptr++) != 0) {
if (p == '.') {
*label = (char)len;
label = ptr - 1;
len = 0;
} else {
len++;
}
}
*label = (char)len;
return 0;
}
/* replace the label length in the domain name by '.'
* f.e. 3www6google2be0 => .www.google.be */
int pico_dns_client_answer_domain(char *ptr)
{
char p = 0, *label = NULL;
if (!ptr)
return -1;
label = ptr;
while ((p = *ptr++) != 0) {
ptr += p;
*label = '.';
label = ptr;
}
return 0;
}
static int pico_dns_client_check_header(struct pico_dns_header *pre)
{
if (pre->qr != PICO_DNS_QR_RESPONSE || pre->opcode != PICO_DNS_OPCODE_QUERY || pre->rcode != PICO_DNS_RCODE_NO_ERROR) {
......@@ -631,18 +539,6 @@ static inline char dns_ptr_ip6_nibble_hi(uint8_t byte)
return (char)(nibble - 0xa + 'a');
}
void pico_dns_ipv6_set_ptr(const char *ip, char *dst)
{
struct pico_ip6 ip6 = {.addr = {}};
int i, j = 0;
pico_string_to_ipv6(ip, ip6.addr);
for (i = 15; i >= 0; i--) {
dst[j++] = dns_ptr_ip6_nibble_lo(ip6.addr[i]);
dst[j++] = '.';
dst[j++] = dns_ptr_ip6_nibble_hi(ip6.addr[i]);
dst[j++] = '.';
}
}
#endif
int pico_dns_create_message(struct pico_dns_header **header, struct pico_dns_query_suffix **qsuffix, enum pico_dns_arpa arpa, const char *url, uint16_t *urlen, uint16_t *hdrlen)
......@@ -838,4 +734,5 @@ int pico_dns_client_init(void)
return pico_dns_client_nameserver(&default_ns, PICO_DNS_NS_ADD);
}
#endif /* PICO_SUPPORT_DNS_CLIENT */
......@@ -14,15 +14,7 @@
#define PICO_DNS_NS_ADD 1
#include <stdint.h>
#include "pico_config.h"
/* QTYPE values */
#define PICO_DNS_TYPE_A 1
#define PICO_DNS_TYPE_AAAA 28
#define PICO_DNS_TYPE_PTR 12
#define PICO_DNS_TYPE_ANY 255
/* QCLASS values */
#define PICO_DNS_CLASS_IN 1
#include "pico_dns_common.h"
/* Compression values */
#define PICO_DNS_LABEL 0
......@@ -39,54 +31,10 @@
#define PICO_DNS_IPV4_ADDR_LEN 16
#define PICO_DNS_IPV6_ADDR_LEN 54
#ifdef PICO_SUPPORT_IPV6
#define STRLEN_PTR_IP6 63
#endif
/* Default nameservers + port */
#define PICO_DNS_NS_DEFAULT "208.67.222.222"
#define PICO_DNS_NS_PORT 53
/* flags splitted in 2x uint8 due to endianness */
PACKED_STRUCT_DEF pico_dns_header
{
uint16_t id;
uint8_t rd : 1; /* recursion desired */
uint8_t tc : 1; /* truncation */
uint8_t aa : 1; /* authoritative answer */
uint8_t opcode : 4; /* opcode */
uint8_t qr : 1; /* query */
uint8_t rcode : 4; /* response code */
uint8_t z : 3; /* zero */
uint8_t ra : 1; /* recursion available */
uint16_t qdcount;
uint16_t ancount;
uint16_t nscount;
uint16_t arcount;
};
PACKED_STRUCT_DEF pico_dns_query_suffix
{
uint16_t qtype;
uint16_t qclass;
};
PACKED_STRUCT_DEF pico_dns_answer_suffix
{
uint16_t qtype;
uint16_t qclass;
uint32_t ttl;
uint16_t rdlength;
};
enum pico_dns_arpa
{
PICO_DNS_ARPA4,
PICO_DNS_ARPA6,
PICO_DNS_NO_ARPA,
};
int pico_dns_client_init(void);
/* flag is PICO_DNS_NS_DEL or PICO_DNS_NS_ADD */
int pico_dns_client_nameserver(struct pico_ip4 *ns, uint8_t flag);
......@@ -96,14 +44,8 @@ int pico_dns_client_getname(const char *ip, void (*callback)(char *url, void *ar
int pico_dns_client_getaddr6(const char *url, void (*callback)(char *, void *), void *arg);
int pico_dns_client_getname6(const char *url, void (*callback)(char *, void *), void *arg);
#endif
/* functions used by the mdns module */
uint16_t pico_dns_client_strlen(const char *url);
int pico_dns_client_query_header(struct pico_dns_header *pre);
int pico_dns_client_query_domain(char *ptr);
int pico_dns_client_answer_domain(char *ptr);
int pico_dns_client_query_suffix(struct pico_dns_query_suffix *suf, uint16_t type, uint16_t qclass);
int pico_dns_create_message(struct pico_dns_header **header, struct pico_dns_query_suffix **qsuffix, enum pico_dns_arpa arpa, const char *url, uint16_t *urlen, uint16_t *hdrlen);
int8_t pico_dns_client_mirror(char *ptr);
void pico_dns_ipv6_set_ptr(const char *ip, char *dst);
#endif /* _INCLUDE_PICO_DNS_CLIENT */
/*********************************************************************
PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
See LICENSE and COPYING for usage.
.
Authors: Toon Stegen
*********************************************************************/
#include "pico_config.h"
#include "pico_stack.h"
#include "pico_addressing.h"
#include "pico_socket.h"
#include "pico_ipv4.h"
#include "pico_ipv6.h"
#include "pico_dns_client.h"
#include "pico_tree.h"
/* determine len of string */
uint16_t pico_dns_client_strlen(const char *url)
{
if (!url)
return 0;
return (uint16_t)strlen(url);
}
/* replace '.' in the domain name by the label length
* f.e. www.google.be => 3www6google2be0 */
int pico_dns_client_query_domain(char *ptr)
{
char p = 0, *label = NULL;
uint8_t len = 0;
if (!ptr)
return -1;
label = ptr++;
while ((p = *ptr++) != 0) {
if (p == '.') {
*label = (char)len;
label = ptr - 1;
len = 0;
} else {
len++;
}
}
*label = (char)len;
return 0;
}
/* replace the label length in the domain name by '.'
* f.e. 3www6google2be0 => .www.google.be */
int pico_dns_client_answer_domain(char *ptr)
{
char p = 0, *label = NULL;
if (!ptr)
return -1;
label = ptr;
while ((p = *ptr++) != 0) {
ptr += p;
*label = '.';
label = ptr;
}
return 0;
}
int pico_dns_client_query_suffix(struct pico_dns_query_suffix *suf, uint16_t type, uint16_t qclass)
{
suf->qtype = short_be(type);
suf->qclass = short_be(qclass);
return 0;
}
/* mirror ip address numbers
* f.e. 192.168.0.1 => 1.0.168.192 */
int8_t pico_dns_client_mirror(char *ptr)
{
const unsigned char *addr = NULL;
char *m = ptr;
uint32_t ip = 0;
int8_t i = 0;
if (pico_string_to_ipv4(ptr, &ip) < 0)
return -1;
ptr = m;
addr = (unsigned char *)&ip;
for (i = 3; i >= 0; i--) {
if (addr[i] > 99) {
*ptr++ = (char)('0' + (addr[i] / 100));
*ptr++ = (char)('0' + ((addr[i] % 100) / 10));
*ptr++ = (char)('0' + ((addr[i] % 100) % 10));
} else if(addr[i] > 9) {
*ptr++ = (char)('0' + (addr[i] / 10));
*ptr++ = (char)('0' + (addr[i] % 10));
} else {
*ptr++ = (char)('0' + addr[i]);
}
if(i > 0)
*ptr++ = '.';
}
*ptr = '\0';
return 0;
}
#ifdef PICO_SUPPORT_IPV6
void pico_dns_ipv6_set_ptr(const char *ip, char *dst)
{
struct pico_ip6 ip6 = {.addr = {}};
int i, j = 0;
pico_string_to_ipv6(ip, ip6.addr);
for (i = 15; i >= 0; i--) {
dst[j++] = dns_ptr_ip6_nibble_lo(ip6.addr[i]);
dst[j++] = '.';
dst[j++] = dns_ptr_ip6_nibble_hi(ip6.addr[i]);
dst[j++] = '.';
}
}
#endif
/*********************************************************************
PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
See LICENSE and COPYING for usage.
.
Authors: Toon Stegen
*********************************************************************/
#ifndef INCLUDE_PICO_DNS_COMMON
#define INCLUDE_PICO_DNS_COMMON
#include "pico_config.h"
/* QTYPE values */
#define PICO_DNS_TYPE_A 1
#define PICO_DNS_TYPE_AAAA 28
#define PICO_DNS_TYPE_PTR 12
#define PICO_DNS_TYPE_ANY 255
/* QCLASS values */
#define PICO_DNS_CLASS_IN 1
#ifdef PICO_SUPPORT_IPV6
#define STRLEN_PTR_IP6 63
#endif
/* flags splitted in 2x uint8 due to endianness */
PACKED_STRUCT_DEF pico_dns_header
{
uint16_t id;
uint8_t rd : 1; /* recursion desired */
uint8_t tc : 1; /* truncation */
uint8_t aa : 1; /* authoritative answer */
uint8_t opcode : 4; /* opcode */
uint8_t qr : 1; /* query */
uint8_t rcode : 4; /* response code */
uint8_t z : 3; /* zero */
uint8_t ra : 1; /* recursion available */
uint16_t qdcount;
uint16_t ancount;
uint16_t nscount;
uint16_t arcount;
};
PACKED_STRUCT_DEF pico_dns_query_suffix
{
uint16_t qtype;
uint16_t qclass;
};
PACKED_STRUCT_DEF pico_dns_answer_suffix
{
uint16_t qtype;
uint16_t qclass;
uint32_t ttl;
uint16_t rdlength;
};
enum pico_dns_arpa
{
PICO_DNS_ARPA4,
PICO_DNS_ARPA6,
PICO_DNS_NO_ARPA,
};
uint16_t pico_dns_client_strlen(const char *url);
int pico_dns_client_query_domain(char *ptr);
int pico_dns_client_answer_domain(char *ptr);
int pico_dns_client_query_suffix(struct pico_dns_query_suffix *suf, uint16_t type, uint16_t qclass);
int8_t pico_dns_client_mirror(char *ptr);
void pico_dns_ipv6_set_ptr(const char *ip, char *dst);
#endif /* _INCLUDE_PICO_DNS_COMMON */
......@@ -10,7 +10,7 @@
#include "pico_socket.h"
#include "pico_ipv4.h"
#include "pico_ipv6.h"
#include "pico_dns_client.h"
#include "pico_dns_common.h"
#include "pico_tree.h"
#ifdef PICO_SUPPORT_MDNS
......
OPTIONS+=-DPICO_SUPPORT_DNS_CLIENT
MOD_OBJ+=$(LIBBASE)modules/pico_dns_client.o
MOD_OBJ+=$(LIBBASE)modules/pico_dns_client.o $(LIBBASE)modules/pico_dns_common.o
OPTIONS+=-DPICO_SUPPORT_MDNS
MOD_OBJ+=$(LIBBASE)modules/pico_mdns.o
MOD_OBJ+=$(LIBBASE)modules/pico_mdns.o $(LIBBASE)modules/pico_dns_common.o
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment