Mbed library for ENC28J60 Ethernet modules. Full support for TCP/IP and UDP Server, Client and HTTP server (webserver). DHCP and DNS is included.
Dependents: mBuino_ENC28_MQTT Nucleo_Web_ENC28J60 Nucleo_Web_ENC28J60_ADC Serial_over_Ethernet ... more
Library for ENC28J60 Ethernet modules.
Ported to mbed from Norbert Truchsess's UIPEthernet library for Arduino. Thank you Norbert!
- Full support for persistent (streaming) TCP/IP and UDP connections Client and Server each, ARP, ICMP, DHCP and DNS.
- Works with both Mbed OS 2 and Mbed OS 5.
Usage:
- Import the library into your project.
- Add
#include "UipEthernet.h"
tomain.cpp
- Create one instance of the UipEthernet class initialized with the MAC address you'd like to use and SPI pins of the connected Mbed board.
Example programs:
Import programWebSwitch_ENC28J60
HTTP Server serving a simple webpage which enables to remotely turn a digital output on/off. Compile, download, run and type 'IP_address/secret/' (don't forget the last '/') into your web browser and hit ENTER.
Import programHTTPServer_Echo_ENC28J60
A simple HTTP server echoing received requests. Ethernet connection is over an ENC28J60 board. Usage: Type the server's IP address into you web browser and hit <ENTER>.
Import programTcpServer_ENC28J60
Simple TCP/IP Server using the UIPEthernet library for ENC28J60 Ethernet boards.
Import programTcpClient_ENC28J60
Simple TCP/IP Client using the UIPEthernet library for ENC28J60 Ethernet boards.
Import programUdpServer_ENC28J60
Simple UDP Server using the UIPEthernet library for ENC28J60 Ethernet boards.
Import programUdpClient_ENC28J60
Simple UDP Client using the UIPEthernet library for ENC28J60 Ethernet boards.
Import programMQTT_Hello_ENC28J60
MQTT Client example program. Ethernet connection is via an ENC28J60 module.
Diff: Dns.cpp
- Revision:
- 8:4acb22344932
- Parent:
- 7:1bc7e6120801
--- a/Dns.cpp Tue Apr 26 18:37:14 2016 +0000 +++ b/Dns.cpp Fri Jun 30 19:51:28 2017 +0000 @@ -9,7 +9,7 @@ #include <string.h> //#include <stdlib.h> -#include <mbed.h> +#include "mbed.h" #define SOCKET_NONE 255 // Various flags and header field values for a DNS message @@ -44,11 +44,11 @@ // Possible return codes from ProcessResponse -#define SUCCESS 1 -#define TIMED_OUT -1 -#define INVALID_SERVER -2 -#define TRUNCATED -3 -#define INVALID_RESPONSE -4 +#define SUCCESS 1 +#define TIMED_OUT - 1 +#define INVALID_SERVER - 2 +#define TRUNCATED - 3 +#define INVALID_RESPONSE - 4 /** * @brief @@ -56,7 +56,6 @@ * @param * @retval */ - void DNSClient::begin(const IPAddress& aDNSServer) { iDNSServer = aDNSServer; iRequestId = 0; @@ -72,22 +71,22 @@ // See if we've been given a valid IP address const char* p = aIPAddrString; - while(*p && ((*p == '.') || (*p >= '0') && (*p <= '9'))) { + while (*p && ((*p == '.') || ((*p >= '0') && (*p <= '9')))) { p++; } - if(*p == '\0') { + if (*p == '\0') { // It's looking promising, we haven't found any invalid characters p = aIPAddrString; int segment = 0; int segmentValue = 0; - while(*p && (segment < 4)) { - if(*p == '.') { + while (*p && (segment < 4)) { + if (*p == '.') { // We've reached the end of a segment - if(segmentValue > 255) { + if (segmentValue > 255) { // You can't have IP address segments that don't fit in a byte return 0; @@ -109,7 +108,7 @@ // We've reached the end of address, but there'll still be the last // segment to deal with - if((segmentValue > 255) || (segment > 3)) { + if ((segmentValue > 255) || (segment > 3)) { // You can't have IP address segments that don't fit in a byte, // or more than four segments @@ -136,19 +135,19 @@ // See if it's a numeric IP address - if(inet_aton(aHostname, aResult)) { + if (inet_aton(aHostname, aResult)) { // It is, our work here is done return 1; } // Check we've got a valid DNS server to use - if(iDNSServer == INADDR_NONE) { + if (iDNSServer == INADDR_NONE) { return INVALID_SERVER; } // Find a socket to use - if(iUdp.begin(1024 + (millis() & 0xF)) == 1) { + if (iUdp.begin(1024 + (millis() & 0xF)) == 1) { // Try up to three times int retries = 0; @@ -156,20 +155,20 @@ { // Send DNS request ret = iUdp.beginPacket(iDNSServer, DNS_PORT); - if(ret != 0) { + if (ret != 0) { // Now output the request data ret = BuildRequest(aHostname); - if(ret != 0) { + if (ret != 0) { // And finally send the request ret = iUdp.endPacket(); - if(ret != 0) { + if (ret != 0) { // Now wait for a response int wait_retries = 0; ret = TIMED_OUT; - while((wait_retries < 3) && (ret == TIMED_OUT)) { + while ((wait_retries < 3) && (ret == TIMED_OUT)) { ret = ProcessResponse(5000, aResult); wait_retries++; } @@ -242,15 +241,15 @@ uint8_t len; // Run through the name being requested - while(*end) { + while (*end) { // Find out how long this section of the name is end = start; - while(*end && (*end != '.')) { + while (*end && (*end != '.')) { end++; } - if(end - start > 0) { + if (end - start > 0) { // Write out the size of this section len = end - start; @@ -290,8 +289,8 @@ // Wait for a response packet - while(iUdp.parsePacket() <= 0) { - if((millis() - startTime) > aTimeout) + while (iUdp.parsePacket() <= 0) { + if ((millis() - startTime) > aTimeout) return TIMED_OUT; wait(0.050); } @@ -301,14 +300,14 @@ uint8_t header[DNS_HEADER_SIZE]; // Enough space to reuse for the DNS header // Check that it's a response from the right server and the right port - if((iDNSServer != iUdp.remoteIP()) || (iUdp.remotePort() != DNS_PORT)) { + if ((iDNSServer != iUdp.remoteIP()) || (iUdp.remotePort() != DNS_PORT)) { // It's not from who we expected return INVALID_SERVER; } // Read through the rest of the response - if(iUdp.available() < DNS_HEADER_SIZE) { + if (iUdp.available() < DNS_HEADER_SIZE) { return TRUNCATED; } @@ -330,7 +329,7 @@ // Check for any errors in the response (or in our request) // although we don't do anything to get round these - if((header_flags & TRUNCATION_FLAG) || (header_flags & RESP_MASK)) { + if ((header_flags & TRUNCATION_FLAG) || (header_flags & RESP_MASK)) { // Mark the entire packet as read iUdp.flush(); @@ -339,7 +338,7 @@ // And make sure we've got (at least) one answer uint16_t answerCount = htons(*((uint16_t*) &header[6])); - if(answerCount == 0) { + if (answerCount == 0) { // Mark the entire packet as read iUdp.flush(); @@ -347,25 +346,24 @@ } // Skip over any questions - for(uint16_t i = 0; i < htons(*((uint16_t*) &header[4])); i++) { + for (uint16_t i = 0; i < htons(*((uint16_t*) &header[4])); i++) { // Skip over the name uint8_t len; - do - { + do { iUdp.read(&len, sizeof(len)); - if(len > 0) { + if (len > 0) { // Don't need to actually read the data out for the string, just // advance ptr to beyond it - while(len--) { + while (len--) { iUdp.read(); // we don't care about the returned byte } } - } while(len != 0); + } while (len != 0); // Now jump over the type and class - for(int i = 0; i < 4; i++) { + for (int i = 0; i < 4; i++) { iUdp.read(); // we don't care about the returned byte } } @@ -374,22 +372,21 @@ // There might be more than one answer (although we'll just use the first // type A answer) and some authority and additional resource records but // we're going to ignore all of them. - for(uint16_t i = 0; i < answerCount; i++) { + for (uint16_t i = 0; i < answerCount; i++) { // Skip the name uint8_t len; - do - { + do { iUdp.read(&len, sizeof(len)); - if((len & LABEL_COMPRESSION_MASK) == 0) { + if ((len & LABEL_COMPRESSION_MASK) == 0) { // It's just a normal label - if(len > 0) { + if (len > 0) { // And it's got a length // Don't need to actually read the data out for the string, // just advance ptr to beyond it - while(len--) { + while (len--) { iUdp.read(); // we don't care about the returned byte } } @@ -408,7 +405,7 @@ // And set len so that we drop out of the name loop len = 0; } - } while(len != 0); + } while (len != 0); // Check the type and class uint16_t answerType; @@ -417,7 +414,7 @@ iUdp.read((uint8_t*) &answerClass, sizeof(answerClass)); // Ignore the Time-To-Live as we don't do any caching - for(int i = 0; i < TTL_SIZE; i++) { + for (int i = 0; i < TTL_SIZE; i++) { iUdp.read(); // we don't care about the returned byte } @@ -425,8 +422,8 @@ // Don't need header_flags anymore, so we can reuse it here iUdp.read((uint8_t*) &header_flags, sizeof(header_flags)); - if((htons(answerType) == TYPE_A) && (htons(answerClass) == CLASS_IN)) { - if(htons(header_flags) != 4) { + if ((htons(answerType) == TYPE_A) && (htons(answerClass) == CLASS_IN)) { + if (htons(header_flags) != 4) { // It's a weird size // Mark the entire packet as read @@ -440,7 +437,7 @@ else { // This isn't an answer type we're after, move onto the next one - for(uint16_t i = 0; i < htons(header_flags); i++) { + for (uint16_t i = 0; i < htons(header_flags); i++) { iUdp.read(); // we don't care about the returned byte } }