Library to resolve text URLs to IP addresses (IPv4)

Dependents:   NetworkSocketAPI NetworkSocketAPI Nucleo-AWS-IoT-mbed

Committer:
sarahmarshy
Date:
Fri Jul 24 22:43:25 2015 +0000
Revision:
1:5d978992a518
Parent:
0:fff4b9055396
Child:
2:12d08f0f20cf
Compatibility with new SocketInterface

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sarahmarshy 0:fff4b9055396 1 /* Copyright (c) 2013 Henry Leinen (henry[dot]leinen [at] online [dot] de)
sarahmarshy 0:fff4b9055396 2 *
sarahmarshy 0:fff4b9055396 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
sarahmarshy 0:fff4b9055396 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
sarahmarshy 0:fff4b9055396 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
sarahmarshy 0:fff4b9055396 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
sarahmarshy 0:fff4b9055396 7 * furnished to do so, subject to the following conditions:
sarahmarshy 0:fff4b9055396 8 *
sarahmarshy 0:fff4b9055396 9 * The above copyright notice and this permission notice shall be included in all copies or
sarahmarshy 0:fff4b9055396 10 * substantial portions of the Software.
sarahmarshy 0:fff4b9055396 11 *
sarahmarshy 0:fff4b9055396 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
sarahmarshy 0:fff4b9055396 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
sarahmarshy 0:fff4b9055396 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
sarahmarshy 0:fff4b9055396 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
sarahmarshy 0:fff4b9055396 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
sarahmarshy 0:fff4b9055396 17 */
sarahmarshy 0:fff4b9055396 18 #ifndef __DNSQUERY_H__
sarahmarshy 0:fff4b9055396 19 #define __DNSQUERY_H__
sarahmarshy 0:fff4b9055396 20
sarahmarshy 1:5d978992a518 21 #include "SocketInterface.h"
sarahmarshy 1:5d978992a518 22
sarahmarshy 0:fff4b9055396 23 typedef struct {
sarahmarshy 0:fff4b9055396 24 union {
sarahmarshy 0:fff4b9055396 25 struct {
sarahmarshy 0:fff4b9055396 26 char o1, o2, o3, o4;
sarahmarshy 0:fff4b9055396 27 };
sarahmarshy 0:fff4b9055396 28 char o[4];
sarahmarshy 0:fff4b9055396 29 } sin_addr;
sarahmarshy 0:fff4b9055396 30 operator char* () { return (char*)this; }
sarahmarshy 0:fff4b9055396 31 char* string_format() {
sarahmarshy 0:fff4b9055396 32 char ip[7];
sarahmarshy 0:fff4b9055396 33 sprintf(ip,"%d.%d.%d.%d", this->sin_addr.o1, this->sin_addr.o2, this->sin_addr.o3, this->sin_addr.o4);
sarahmarshy 0:fff4b9055396 34 return ip;
sarahmarshy 0:fff4b9055396 35 }
sarahmarshy 0:fff4b9055396 36 } IPADDRESS_t;
sarahmarshy 0:fff4b9055396 37
sarahmarshy 0:fff4b9055396 38 /** Class DnsQuery implements DNS over UDP functionality.
sarahmarshy 0:fff4b9055396 39 * Example as a typical use case :
sarahmarshy 0:fff4b9055396 40 * @code
sarahmarshy 0:fff4b9055396 41 * #include "mbed.h"
sarahmarshy 0:fff4b9055396 42 * #include "DnsQuery.h"
sarahmarshy 0:fff4b9055396 43 *
sarahmarshy 0:fff4b9055396 44 * void main(void)
sarahmarshy 0:fff4b9055396 45 * {
sarahmarshy 0:fff4b9055396 46 * IPADDRESS_t ipAddress; // will receive the ip address of the host
sarahmarshy 0:fff4b9055396 47 * IPADDRESS_t dnsIp = { 192, 168, 178, 1 }; // Ip Address of the DNS server
sarahmarshy 0:fff4b9055396 48 *
sarahmarshy 0:fff4b9055396 49 * DnsQuery dns(Wifi::getInstance(), &dnsIp);
sarahmarshy 0:fff4b9055396 50 * if (dns.gethostbyname("mbed.org", ipAddress)) {
sarahmarshy 0:fff4b9055396 51 * printf("Ip-Address of mbed.org is %d.%d.%d.%d\n", ipAddress.sin_addr.o1, ipAddress.sin_addr.o2, ipAddress.sin_addr.o3, ipAddress.sin_addr.o4);
sarahmarshy 0:fff4b9055396 52 * } else {
sarahmarshy 0:fff4b9055396 53 * printf("Unable to obtain IP-Address\n");
sarahmarshy 0:fff4b9055396 54 * }
sarahmarshy 0:fff4b9055396 55 * }
sarahmarshy 0:fff4b9055396 56 * @endcode
sarahmarshy 0:fff4b9055396 57 */
sarahmarshy 0:fff4b9055396 58 class DnsQuery
sarahmarshy 0:fff4b9055396 59 {
sarahmarshy 0:fff4b9055396 60 public:
sarahmarshy 0:fff4b9055396 61 /** Constructor to instantiate a DnsQuery object.
sarahmarshy 0:fff4b9055396 62 * @param wifi : A valid pointer to a Wifi Object, which can be used to obtain a UDP socket object.
sarahmarshy 0:fff4b9055396 63 * @param dnsip : A valid pointer which holds the IPAddress of the DNS server to query.
sarahmarshy 0:fff4b9055396 64 */
sarahmarshy 1:5d978992a518 65 DnsQuery(SocketInterface* sock);
sarahmarshy 0:fff4b9055396 66
sarahmarshy 0:fff4b9055396 67
sarahmarshy 0:fff4b9055396 68 /** Function gethostbyname implements the functionality to query a domain name server for an IP-Address of a given hostname.
sarahmarshy 0:fff4b9055396 69 * @param hostname : the hostname of interest as a string.
sarahmarshy 0:fff4b9055396 70 * @param ipaddress : a reference to a IPADDRESS_t object which will receive the resolved IP Address of the host in question.
sarahmarshy 0:fff4b9055396 71 * @returns true if successfull, or false otherwise.
sarahmarshy 0:fff4b9055396 72 */
sarahmarshy 1:5d978992a518 73 bool gethostbyname(const char* hostname, IPADDRESS_t &ipaddress);
sarahmarshy 0:fff4b9055396 74 /** Function gethostbyname implements the functionality to query a domain name server for an IP-Address of a given hostname.
sarahmarshy 0:fff4b9055396 75 * @param hostname : the hostname of interest as a string.
sarahmarshy 0:fff4b9055396 76 * @param ipaddress : a reference to a IPADDRESS_t object which will receive the resolved IP Address of the host in question.
sarahmarshy 0:fff4b9055396 77 * @returns true if successfull, or false otherwise.
sarahmarshy 0:fff4b9055396 78 */
sarahmarshy 1:5d978992a518 79 bool getIP(const char* hostname, IPADDRESS_t &ipaddress);
sarahmarshy 0:fff4b9055396 80 protected:
sarahmarshy 0:fff4b9055396 81 bool resolve(char* resp, IPADDRESS_t &ipaddress);
sarahmarshy 0:fff4b9055396 82 bool parseRR(char *resp, int& c, IPADDRESS_t& adr );
sarahmarshy 0:fff4b9055396 83
sarahmarshy 0:fff4b9055396 84 protected:
sarahmarshy 0:fff4b9055396 85 IPADDRESS_t _dnsip;
sarahmarshy 0:fff4b9055396 86 char* _string_ip;
sarahmarshy 1:5d978992a518 87 SocketInterface* socket;
sarahmarshy 1:5d978992a518 88
sarahmarshy 0:fff4b9055396 89 };
sarahmarshy 0:fff4b9055396 90
sarahmarshy 0:fff4b9055396 91 #endif // __DNSQUERY_H__