Skip to content

DNS client additions #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
22 changes: 19 additions & 3 deletions libraries/Ethernet/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(uint8_t *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;

Expand All @@ -38,7 +54,7 @@ uint8_t Client::connect() {
if (_srcport == 0) _srcport = 1024;
socket(_sock, SnMR::TCP, _srcport, 0);

if (!::connect(_sock, _ip, _port)) {
if (!::connect(_sock, ip.raw_address(), port)) {
_sock = MAX_SOCK_NUM;
return 0;
}
Expand Down
8 changes: 3 additions & 5 deletions libraries/Ethernet/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ class Client : public Stream {

public:
Client();
Client(uint8_t);
Client(uint8_t *, uint16_t);
Client(uint8_t sock);

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);
Expand All @@ -31,8 +31,6 @@ class Client : public Stream {
private:
static uint16_t _srcport;
uint8_t _sock;
uint8_t *_ip;
uint16_t _port;
};

#endif
Loading