Permalink
Browse files

Proposed fix for issue 243, adding DNS to the Ethernet library. Uses …

…a slightly modified version of the agreed API as the host/port parameters have been moved from the Client constructor to the Client::connect methods. This means it's possible for errors to be returned if the DNS lookup fails and also reduces the RAM footprint of the Client class as it no longer needs to store the host/port for later use in Client::connect.
  • Loading branch information...
1 parent a42dc0b commit a310cb8e4dad410af717e3fe9502fc3f98127892 @amcewen committed Jan 25, 2011
@@ -10,16 +10,32 @@ extern "C" {
#include "Ethernet.h"
#include "Client.h"
#include "Server.h"
+#include "Dns.h"
uint16_t Client::_srcport = 1024;
+Client::Client() : _sock(MAX_SOCK_NUM) {
+}
+
Client::Client(uint8_t sock) : _sock(sock) {
}
-Client::Client(IPAddress& ip, uint16_t port) : _ip(ip), _port(port), _sock(MAX_SOCK_NUM) {
+int Client::connect(const char* host, uint16_t port) {
+ // Look up the host first
+ int ret = 0;
+ DNSClient dns;
+ IPAddress remote_addr;
+
+ dns.begin(Ethernet.dnsServerIP());
+ ret = dns.getHostByName(host, remote_addr);
+ if (ret == 1) {
+ return connect(remote_addr, port);
+ } else {
+ return ret;
+ }
}
-uint8_t Client::connect() {
+int Client::connect(IPAddress ip, uint16_t port) {
if (_sock != MAX_SOCK_NUM)
return 0;
@@ -38,7 +54,7 @@ uint8_t Client::connect() {
if (_srcport == 0) _srcport = 1024;
socket(_sock, SnMR::TCP, _srcport, 0);
- if (!::connect(_sock, _ip.raw_address(), _port)) {
+ if (!::connect(_sock, ip.raw_address(), port)) {
_sock = MAX_SOCK_NUM;
return 0;
}
@@ -8,10 +8,10 @@ class Client : public Stream {
public:
Client();
Client(uint8_t sock);
- Client(IPAddress& ip, uint16_t port);
uint8_t status();
- uint8_t connect();
+ int connect(IPAddress ip, uint16_t port);
+ int connect(const char *host, uint16_t port);
virtual void write(uint8_t);
virtual void write(const char *str);
virtual void write(const uint8_t *buf, size_t size);
@@ -31,8 +31,6 @@ class Client : public Stream {
private:
static uint16_t _srcport;
uint8_t _sock;
- IPAddress _ip;
- uint16_t _port;
};
#endif
Oops, something went wrong. Retry.

0 comments on commit a310cb8

Please sign in to comment.