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