![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Embedded C project:18/12/2014
Dependencies: DS1307 TextLCD mbed
NetServices/api/DNSRequest.h@0:8d87bc453349, 2014-12-18 (annotated)
- Committer:
- ninoderkinderen
- Date:
- Thu Dec 18 09:35:49 2014 +0000
- Revision:
- 0:8d87bc453349
Programma embedded C
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ninoderkinderen | 0:8d87bc453349 | 1 | |
ninoderkinderen | 0:8d87bc453349 | 2 | /* |
ninoderkinderen | 0:8d87bc453349 | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
ninoderkinderen | 0:8d87bc453349 | 4 | |
ninoderkinderen | 0:8d87bc453349 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
ninoderkinderen | 0:8d87bc453349 | 6 | of this software and associated documentation files (the "Software"), to deal |
ninoderkinderen | 0:8d87bc453349 | 7 | in the Software without restriction, including without limitation the rights |
ninoderkinderen | 0:8d87bc453349 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
ninoderkinderen | 0:8d87bc453349 | 9 | copies of the Software, and to permit persons to whom the Software is |
ninoderkinderen | 0:8d87bc453349 | 10 | furnished to do so, subject to the following conditions: |
ninoderkinderen | 0:8d87bc453349 | 11 | |
ninoderkinderen | 0:8d87bc453349 | 12 | The above copyright notice and this permission notice shall be included in |
ninoderkinderen | 0:8d87bc453349 | 13 | all copies or substantial portions of the Software. |
ninoderkinderen | 0:8d87bc453349 | 14 | |
ninoderkinderen | 0:8d87bc453349 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
ninoderkinderen | 0:8d87bc453349 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
ninoderkinderen | 0:8d87bc453349 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
ninoderkinderen | 0:8d87bc453349 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
ninoderkinderen | 0:8d87bc453349 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ninoderkinderen | 0:8d87bc453349 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
ninoderkinderen | 0:8d87bc453349 | 21 | THE SOFTWARE. |
ninoderkinderen | 0:8d87bc453349 | 22 | */ |
ninoderkinderen | 0:8d87bc453349 | 23 | |
ninoderkinderen | 0:8d87bc453349 | 24 | /** \file |
ninoderkinderen | 0:8d87bc453349 | 25 | DNS Request header file |
ninoderkinderen | 0:8d87bc453349 | 26 | */ |
ninoderkinderen | 0:8d87bc453349 | 27 | |
ninoderkinderen | 0:8d87bc453349 | 28 | #ifndef DNSREQUEST_H |
ninoderkinderen | 0:8d87bc453349 | 29 | #define DNSREQUEST_H |
ninoderkinderen | 0:8d87bc453349 | 30 | |
ninoderkinderen | 0:8d87bc453349 | 31 | #include "core/net.h" |
ninoderkinderen | 0:8d87bc453349 | 32 | #include "core/ipaddr.h" |
ninoderkinderen | 0:8d87bc453349 | 33 | #include "core/host.h" |
ninoderkinderen | 0:8d87bc453349 | 34 | //Essentially it is a safe interface to NetDnsRequest |
ninoderkinderen | 0:8d87bc453349 | 35 | |
ninoderkinderen | 0:8d87bc453349 | 36 | ///DNS Request error codes |
ninoderkinderen | 0:8d87bc453349 | 37 | enum DNSRequestErr |
ninoderkinderen | 0:8d87bc453349 | 38 | { |
ninoderkinderen | 0:8d87bc453349 | 39 | __DNS_MIN = -0xFFFF, |
ninoderkinderen | 0:8d87bc453349 | 40 | DNS_SETUP, ///<DNSRequest not properly configured |
ninoderkinderen | 0:8d87bc453349 | 41 | DNS_IF, ///<Interface has problems, does not exist or is not initialized |
ninoderkinderen | 0:8d87bc453349 | 42 | DNS_MEM, ///<Not enough mem |
ninoderkinderen | 0:8d87bc453349 | 43 | DNS_INUSE, ///<Interface / Port is in use |
ninoderkinderen | 0:8d87bc453349 | 44 | DNS_PROCESSING, ///<Request has not completed |
ninoderkinderen | 0:8d87bc453349 | 45 | //... |
ninoderkinderen | 0:8d87bc453349 | 46 | DNS_OK = 0 ///<Success |
ninoderkinderen | 0:8d87bc453349 | 47 | }; |
ninoderkinderen | 0:8d87bc453349 | 48 | |
ninoderkinderen | 0:8d87bc453349 | 49 | ///DNS Request Result Events |
ninoderkinderen | 0:8d87bc453349 | 50 | enum DNSReply |
ninoderkinderen | 0:8d87bc453349 | 51 | { |
ninoderkinderen | 0:8d87bc453349 | 52 | DNS_PRTCL, |
ninoderkinderen | 0:8d87bc453349 | 53 | DNS_NOTFOUND, ///Hostname is unknown |
ninoderkinderen | 0:8d87bc453349 | 54 | DNS_ERROR, ///Problem with DNS Service |
ninoderkinderen | 0:8d87bc453349 | 55 | //... |
ninoderkinderen | 0:8d87bc453349 | 56 | DNS_FOUND, |
ninoderkinderen | 0:8d87bc453349 | 57 | }; |
ninoderkinderen | 0:8d87bc453349 | 58 | |
ninoderkinderen | 0:8d87bc453349 | 59 | class NetDnsRequest; |
ninoderkinderen | 0:8d87bc453349 | 60 | enum NetDnsReply; |
ninoderkinderen | 0:8d87bc453349 | 61 | |
ninoderkinderen | 0:8d87bc453349 | 62 | ///This is a simple DNS Request class |
ninoderkinderen | 0:8d87bc453349 | 63 | /** |
ninoderkinderen | 0:8d87bc453349 | 64 | This class exposes an API to deal with DNS Requests |
ninoderkinderen | 0:8d87bc453349 | 65 | */ |
ninoderkinderen | 0:8d87bc453349 | 66 | class DNSRequest |
ninoderkinderen | 0:8d87bc453349 | 67 | { |
ninoderkinderen | 0:8d87bc453349 | 68 | public: |
ninoderkinderen | 0:8d87bc453349 | 69 | ///Creates a new request |
ninoderkinderen | 0:8d87bc453349 | 70 | DNSRequest(); |
ninoderkinderen | 0:8d87bc453349 | 71 | |
ninoderkinderen | 0:8d87bc453349 | 72 | ///Terminates and closes request |
ninoderkinderen | 0:8d87bc453349 | 73 | ~DNSRequest(); |
ninoderkinderen | 0:8d87bc453349 | 74 | |
ninoderkinderen | 0:8d87bc453349 | 75 | ///Resolves an hostname |
ninoderkinderen | 0:8d87bc453349 | 76 | /** |
ninoderkinderen | 0:8d87bc453349 | 77 | @param hostname : hostname to resolve |
ninoderkinderen | 0:8d87bc453349 | 78 | */ |
ninoderkinderen | 0:8d87bc453349 | 79 | DNSRequestErr resolve(const char* hostname); |
ninoderkinderen | 0:8d87bc453349 | 80 | |
ninoderkinderen | 0:8d87bc453349 | 81 | ///Resolves an hostname |
ninoderkinderen | 0:8d87bc453349 | 82 | /** |
ninoderkinderen | 0:8d87bc453349 | 83 | @param host : hostname to resolve, the result will be stored in the IpAddr field of this object |
ninoderkinderen | 0:8d87bc453349 | 84 | */ |
ninoderkinderen | 0:8d87bc453349 | 85 | DNSRequestErr resolve(Host* pHost); |
ninoderkinderen | 0:8d87bc453349 | 86 | |
ninoderkinderen | 0:8d87bc453349 | 87 | ///Setups callback |
ninoderkinderen | 0:8d87bc453349 | 88 | /** |
ninoderkinderen | 0:8d87bc453349 | 89 | The callback function will be called on result. |
ninoderkinderen | 0:8d87bc453349 | 90 | @param pMethod : callback function |
ninoderkinderen | 0:8d87bc453349 | 91 | */ |
ninoderkinderen | 0:8d87bc453349 | 92 | void setOnReply( void (*pMethod)(DNSReply) ); |
ninoderkinderen | 0:8d87bc453349 | 93 | |
ninoderkinderen | 0:8d87bc453349 | 94 | class CDummy; |
ninoderkinderen | 0:8d87bc453349 | 95 | ///Setups callback |
ninoderkinderen | 0:8d87bc453349 | 96 | /** |
ninoderkinderen | 0:8d87bc453349 | 97 | The callback function will be called on result. |
ninoderkinderen | 0:8d87bc453349 | 98 | @param pItem : instance of class on which to execute the callback method |
ninoderkinderen | 0:8d87bc453349 | 99 | @param pMethod : callback method |
ninoderkinderen | 0:8d87bc453349 | 100 | */ |
ninoderkinderen | 0:8d87bc453349 | 101 | template<class T> |
ninoderkinderen | 0:8d87bc453349 | 102 | void setOnReply( T* pItem, void (T::*pMethod)(DNSReply) ) |
ninoderkinderen | 0:8d87bc453349 | 103 | { |
ninoderkinderen | 0:8d87bc453349 | 104 | m_pCbItem = (CDummy*) pItem; |
ninoderkinderen | 0:8d87bc453349 | 105 | m_pCbMeth = (void (CDummy::*)(DNSReply)) pMethod; |
ninoderkinderen | 0:8d87bc453349 | 106 | } |
ninoderkinderen | 0:8d87bc453349 | 107 | |
ninoderkinderen | 0:8d87bc453349 | 108 | ///Gets IP address once it has been resolved |
ninoderkinderen | 0:8d87bc453349 | 109 | /** |
ninoderkinderen | 0:8d87bc453349 | 110 | @param pIp : pointer to an IpAddr instance in which to store the resolved IP address |
ninoderkinderen | 0:8d87bc453349 | 111 | */ |
ninoderkinderen | 0:8d87bc453349 | 112 | DNSRequestErr getResult(IpAddr* pIp); |
ninoderkinderen | 0:8d87bc453349 | 113 | |
ninoderkinderen | 0:8d87bc453349 | 114 | ///Closes DNS Request before completion |
ninoderkinderen | 0:8d87bc453349 | 115 | DNSRequestErr close(); |
ninoderkinderen | 0:8d87bc453349 | 116 | |
ninoderkinderen | 0:8d87bc453349 | 117 | protected: |
ninoderkinderen | 0:8d87bc453349 | 118 | void onNetDnsReply(NetDnsReply r); |
ninoderkinderen | 0:8d87bc453349 | 119 | DNSRequestErr checkInst(); |
ninoderkinderen | 0:8d87bc453349 | 120 | |
ninoderkinderen | 0:8d87bc453349 | 121 | private: |
ninoderkinderen | 0:8d87bc453349 | 122 | NetDnsRequest* m_pNetDnsRequest; |
ninoderkinderen | 0:8d87bc453349 | 123 | |
ninoderkinderen | 0:8d87bc453349 | 124 | CDummy* m_pCbItem; |
ninoderkinderen | 0:8d87bc453349 | 125 | void (CDummy::*m_pCbMeth)(DNSReply); |
ninoderkinderen | 0:8d87bc453349 | 126 | |
ninoderkinderen | 0:8d87bc453349 | 127 | void (*m_pCb)(DNSReply); |
ninoderkinderen | 0:8d87bc453349 | 128 | |
ninoderkinderen | 0:8d87bc453349 | 129 | }; |
ninoderkinderen | 0:8d87bc453349 | 130 | |
ninoderkinderen | 0:8d87bc453349 | 131 | #endif |