Revised to prevent DHCPClient::discover from hanging.
Fork of WIZ820ioInterface by
DNSClient/dnsname.h@9:c1722862c13b, 2014-08-15 (annotated)
- Committer:
- rraab
- Date:
- Fri Aug 15 19:53:25 2014 +0000
- Revision:
- 9:c1722862c13b
- Parent:
- 5:fb15c35d1e28
Fixed DHCPClient::discover calling rand, which hangs in an infinite loop.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 5:fb15c35d1e28 | 1 | // dnsname.h 2013/8/27 |
va009039 | 5:fb15c35d1e28 | 2 | #pragma once |
va009039 | 5:fb15c35d1e28 | 3 | //#include <string> |
va009039 | 5:fb15c35d1e28 | 4 | #include "pico_string.h" |
va009039 | 5:fb15c35d1e28 | 5 | class dnsname { |
va009039 | 5:fb15c35d1e28 | 6 | public: |
va009039 | 5:fb15c35d1e28 | 7 | uint8_t *buf; |
va009039 | 5:fb15c35d1e28 | 8 | pico_string str; |
va009039 | 5:fb15c35d1e28 | 9 | dnsname(uint8_t *s) { |
va009039 | 5:fb15c35d1e28 | 10 | buf = s; |
va009039 | 5:fb15c35d1e28 | 11 | } |
va009039 | 5:fb15c35d1e28 | 12 | int decode(int pos) { |
va009039 | 5:fb15c35d1e28 | 13 | while(1) { |
va009039 | 5:fb15c35d1e28 | 14 | int len = buf[pos++]; |
va009039 | 5:fb15c35d1e28 | 15 | if (len == 0x00) { |
va009039 | 5:fb15c35d1e28 | 16 | break; |
va009039 | 5:fb15c35d1e28 | 17 | } |
va009039 | 5:fb15c35d1e28 | 18 | if ((len&0xc0) == 0xc0) { //compress |
va009039 | 5:fb15c35d1e28 | 19 | int offset = (len&0x3f)<<8|buf[pos]; |
va009039 | 5:fb15c35d1e28 | 20 | decode(offset); |
va009039 | 5:fb15c35d1e28 | 21 | return pos+1; |
va009039 | 5:fb15c35d1e28 | 22 | } |
va009039 | 5:fb15c35d1e28 | 23 | if (!str.empty()) { |
va009039 | 5:fb15c35d1e28 | 24 | str.append("."); |
va009039 | 5:fb15c35d1e28 | 25 | } |
va009039 | 5:fb15c35d1e28 | 26 | str.append((const char*)(buf+pos), len); |
va009039 | 5:fb15c35d1e28 | 27 | pos += len; |
va009039 | 5:fb15c35d1e28 | 28 | } |
va009039 | 5:fb15c35d1e28 | 29 | return pos; |
va009039 | 5:fb15c35d1e28 | 30 | } |
va009039 | 5:fb15c35d1e28 | 31 | |
va009039 | 5:fb15c35d1e28 | 32 | int encode(int pos, char* s) { |
va009039 | 5:fb15c35d1e28 | 33 | while(*s) { |
va009039 | 5:fb15c35d1e28 | 34 | char *f = strchr(s, '.'); |
va009039 | 5:fb15c35d1e28 | 35 | if (f == NULL) { |
va009039 | 5:fb15c35d1e28 | 36 | int len = strlen(s); |
va009039 | 5:fb15c35d1e28 | 37 | buf[pos++] = len; |
va009039 | 5:fb15c35d1e28 | 38 | memcpy(buf+pos, s, len); |
va009039 | 5:fb15c35d1e28 | 39 | pos += len; |
va009039 | 5:fb15c35d1e28 | 40 | break; |
va009039 | 5:fb15c35d1e28 | 41 | } |
va009039 | 5:fb15c35d1e28 | 42 | int len = f - s; |
va009039 | 5:fb15c35d1e28 | 43 | buf[pos++] = len; |
va009039 | 5:fb15c35d1e28 | 44 | memcpy(buf+pos, s, len); |
va009039 | 5:fb15c35d1e28 | 45 | s = f+1; |
va009039 | 5:fb15c35d1e28 | 46 | pos += len; |
va009039 | 5:fb15c35d1e28 | 47 | } |
va009039 | 5:fb15c35d1e28 | 48 | buf[pos++] = 0x00; |
va009039 | 5:fb15c35d1e28 | 49 | return pos; |
va009039 | 5:fb15c35d1e28 | 50 | } |
va009039 | 5:fb15c35d1e28 | 51 | }; |