mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Antony Pavlov <antonynpavlov@gmail.com>
To: barebox@lists.infradead.org
Subject: [RFC 06/12] WIP: net/dns: convert to picotcp
Date: Wed, 15 Jul 2015 23:13:44 +0300	[thread overview]
Message-ID: <1436991230-14251-7-git-send-email-antonynpavlov@gmail.com> (raw)
In-Reply-To: <1436991230-14251-1-git-send-email-antonynpavlov@gmail.com>

Signed-off-by: Antony Pavlov <antonynpavlov@gmail.com>
---
 net/dns.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 94 insertions(+), 7 deletions(-)

diff --git a/net/dns.c b/net/dns.c
index 0e16ea2..2a35e3b 100644
--- a/net/dns.c
+++ b/net/dns.c
@@ -29,6 +29,10 @@
 #include <environment.h>
 #include <linux/err.h>
 
+#include <pico_socket.h>
+#include <pico_ipv4.h>
+#include <poller.h>
+
 #define DNS_PORT 53
 
 /* http://en.wikipedia.org/wiki/List_of_DNS_record_types */
@@ -59,15 +63,24 @@ static uint64_t dns_timer_start;
 static int dns_state;
 static IPaddr_t dns_ip;
 
+static struct pico_socket *sock;
+static uint8_t *pkt;
+
 static int dns_send(char *name)
 {
 	int ret;
 	struct header *header;
 	enum dns_query_type qtype = DNS_A_RECORD;
-	unsigned char *packet = net_udp_get_payload(dns_con);
+	unsigned char *packet;
 	unsigned char *p, *s, *fullname, *dotptr;
 	const unsigned char *domain;
 
+	if (IS_ENABLED(CONFIG_NET_LEGACY)) {
+		packet = net_udp_get_payload(dns_con);
+	} else if (IS_ENABLED(CONFIG_NET_PICOTCP)) {
+		packet = pkt;
+	}
+
 	/* Prepare DNS packet header */
 	header           = (struct header *)packet;
 	header->tid      = 1;
@@ -109,7 +122,16 @@ static int dns_send(char *name)
 	*p++ = 0;
 	*p++ = 1;				/* Class: inet, 0x0001 */
 
-	ret = net_udp_send(dns_con, p - packet);
+	if (IS_ENABLED(CONFIG_NET_LEGACY)) {
+		ret = net_udp_send(dns_con, p - packet);
+	} else if (IS_ENABLED(CONFIG_NET_PICOTCP)) {
+		uint16_t remote_port;
+
+		remote_port = htons(DNS_PORT);
+		pico_socket_send(sock, pkt, p - packet);
+		/* FIXME */
+		ret = 0;
+	}
 
 	free(fullname);
 
@@ -199,6 +221,44 @@ static void dns_handler(void *ctx, char *packet, unsigned len)
 		net_eth_to_udplen(packet));
 }
 
+static void dns_cb(uint16_t ev, struct pico_socket *sock)
+{
+	union pico_address ep;
+	uint16_t remote_port;
+	int len;
+
+	if (ev == PICO_SOCK_EV_ERR) {
+		printf("              >>>>>> PICO_SOCK_EV_ERR <<<<<<< \n");
+		return;
+	}
+
+	/* FIXME: 1500 */
+	len = pico_socket_recvfrom(sock, pkt, 1500, &ep, &remote_port);
+
+	dns_recv((struct header *)pkt, len);
+}
+
+static struct pico_socket *dns_socket_open(uint16_t localport)
+{
+	struct pico_socket *sock;
+	union pico_address local_address;
+
+	sock = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_UDP, dns_cb);
+	if (!sock)
+		return NULL;
+
+	localport = htons(localport);
+
+	/* FIXME: use local_address == PICO_IPV4_INADDR_ANY */
+	memset(&local_address, 0, sizeof(union pico_address));
+	if (pico_socket_bind(sock, &local_address, &localport) < 0) {
+		pico_socket_close(sock);
+		return NULL;
+	}
+
+	return sock;
+}
+
 IPaddr_t resolv(char *host)
 {
 	IPaddr_t ip;
@@ -223,9 +283,26 @@ IPaddr_t resolv(char *host)
 
 	debug("resolving host %s via nameserver %s\n", host, ip_to_string(ip));
 
-	dns_con = net_udp_new(ip, DNS_PORT, dns_handler, NULL);
-	if (IS_ERR(dns_con))
-		return PTR_ERR(dns_con);
+	if (IS_ENABLED(CONFIG_NET_LEGACY)) {
+		dns_con = net_udp_new(ip, DNS_PORT, dns_handler, NULL);
+		if (IS_ERR(dns_con))
+			return PTR_ERR(dns_con);
+	} else if (IS_ENABLED(CONFIG_NET_PICOTCP)) {
+		static union pico_address pico_dns_ip;
+
+		pico_string_to_ipv4(ns, &pico_dns_ip.ip4.addr);
+
+		sock = dns_socket_open(0);
+		if (!sock)
+			return 0;
+
+		if (pico_socket_connect(sock, &pico_dns_ip, htons(DNS_PORT)) < 0)
+			return 0;
+
+		/* FIXME: 2048 */
+		pkt = xzalloc(2048);
+	}
+
 	dns_timer_start = get_time_ns();
 	dns_send(host);
 
@@ -233,7 +310,13 @@ IPaddr_t resolv(char *host)
 		if (ctrlc()) {
 			break;
 		}
-		net_poll();
+
+		if (IS_ENABLED(CONFIG_NET_LEGACY)) {
+			net_poll();
+		} else if (IS_ENABLED(CONFIG_NET_PICOTCP)) {
+			poller_call();
+		}
+
 		if (is_timeout(dns_timer_start, SECOND)) {
 			dns_timer_start = get_time_ns();
 			printf("T ");
@@ -241,7 +324,11 @@ IPaddr_t resolv(char *host)
 		}
 	}
 
-	net_unregister(dns_con);
+	if (IS_ENABLED(CONFIG_NET_LEGACY)) {
+		net_unregister(dns_con);
+	} else if (IS_ENABLED(CONFIG_NET_PICOTCP)) {
+		free(pkt);
+	}
 
 	return dns_ip;
 }
-- 
2.1.4


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2015-07-15 20:14 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-15 20:13 [RFC 00/12] barebox picotcp integration (2015.07.15) Antony Pavlov
2015-07-15 20:13 ` [RFC 01/12] picotcp: add barebox target support Antony Pavlov
2015-07-15 20:13 ` [RFC 02/12] picotcp: switch to Kbuild Antony Pavlov
2015-07-15 20:13 ` [RFC 03/12] net: add initial picotcp support Antony Pavlov
2015-07-15 20:13 ` [RFC 04/12] WIP: fs/nfs.c: convert to picotcp Antony Pavlov
2015-07-16 19:51   ` Sascha Hauer
2015-07-17  7:18     ` Antony Pavlov
2015-07-15 20:13 ` [RFC 05/12] WIP: fs/tftp.c: " Antony Pavlov
2015-07-15 20:13 ` Antony Pavlov [this message]
2015-07-15 20:13 ` [RFC 07/12] net: picotcp: add test_picotcp command Antony Pavlov
2015-07-15 20:13 ` [RFC 08/12] net: picotcp: add ifconfig command Antony Pavlov
2015-07-15 20:13 ` [RFC 09/12] net: picotcp: add ping command Antony Pavlov
2015-07-15 20:13 ` [RFC 10/12] net: picotcp: add route command Antony Pavlov
2015-07-15 20:13 ` [RFC 11/12] sandbox_defconfig: switch to picotcp Antony Pavlov
2015-07-15 20:13 ` [RFC 12/12] WIP: sandbox_defconfig: enable network testing-related stuff Antony Pavlov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1436991230-14251-7-git-send-email-antonynpavlov@gmail.com \
    --to=antonynpavlov@gmail.com \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox