22
22
23
23
24
24
// DNS options
25
- #define DNS_BUFFER_SIZE 256
25
+ #define DNS_BUFFER_SIZE 256
26
26
#define DNS_TIMEOUT 5000
27
27
#define DNS_SERVERS_SIZE 5
28
28
@@ -45,111 +45,6 @@ extern "C" int nsapi_dns_add_server(nsapi_addr_t addr)
45
45
return 0 ;
46
46
}
47
47
48
- // IP parsing
49
- static bool dns_parse_ipv4 (const char *host, nsapi_addr_t *addr)
50
- {
51
- int i = 0 ;
52
-
53
- // Check each digit for [0-9.]
54
- for (; host[i]; i++) {
55
- if (!(host[i] >= ' 0' && host[i] <= ' 9' ) && host[i] != ' .' ) {
56
- return false ;
57
- }
58
- }
59
-
60
- // Ending with '.' garuntees host
61
- if (i > 0 && host[i-1 ] == ' .' ) {
62
- return false ;
63
- }
64
-
65
- // Build up the ip address
66
- addr->version = NSAPI_IPv4;
67
- i = 0 ;
68
-
69
- for (int count = 0 ; count < NSAPI_IPv4_BYTES; count++) {
70
- int scanned = sscanf (&host[i], " %hhu" , &addr->bytes [count]);
71
- if (scanned < 1 ) {
72
- return true ;
73
- }
74
-
75
- for (; host[i] != ' .' ; i++) {
76
- if (!host[i]) {
77
- return true ;
78
- }
79
- }
80
-
81
- i++;
82
- }
83
-
84
- return true ;
85
- }
86
-
87
- static int dns_parse_ipv6_chunk (const char *chunk, uint16_t *shorts) {
88
- int count = 0 ;
89
- int i = 0 ;
90
-
91
- for (; count < NSAPI_IPv6_BYTES/2 ; count++) {
92
- int scanned = sscanf (&chunk[i], " %hx" , &shorts[count]);
93
- if (scanned < 1 ) {
94
- return count;
95
- }
96
-
97
- for (; chunk[i] != ' :' ; i++) {
98
- if (!chunk[i]) {
99
- return count+1 ;
100
- }
101
- }
102
-
103
- i++;
104
- }
105
-
106
- return count;
107
- }
108
-
109
- static bool dns_parse_ipv6 (const char *host, nsapi_addr_t *addr)
110
- {
111
- // Check each digit for [0-9a-fA-F:]
112
- for (int i = 0 ; host[i]; i++) {
113
- if (!(host[i] >= ' 0' && host[i] <= ' 9' ) &&
114
- !(host[i] >= ' a' && host[i] <= ' f' ) &&
115
- !(host[i] >= ' A' && host[i] <= ' F' ) &&
116
- host[i] != ' :' ) {
117
- return false ;
118
- }
119
- }
120
-
121
- // Build up address
122
- addr->version = NSAPI_IPv6;
123
-
124
- // Start with zeroed address
125
- uint16_t shorts[NSAPI_IPv6_BYTES/2 ];
126
- memset (shorts, 0 , sizeof shorts);
127
-
128
- int suffix = 0 ;
129
-
130
- // Find double colons and scan suffix
131
- for (int i = 0 ; host[i]; i++) {
132
- if (host[i] == ' :' && host[i+1 ] == ' :' ) {
133
- suffix = dns_parse_ipv6_chunk (&host[i+2 ], shorts);
134
- break ;
135
- }
136
- }
137
-
138
- // Move suffix to end
139
- memmove (&shorts[NSAPI_IPv6_BYTES/2 -suffix], &shorts[0 ],
140
- suffix*sizeof (uint16_t ));
141
-
142
- // Scan prefix
143
- dns_parse_ipv6_chunk (&host[0 ], shorts);
144
-
145
- // Flip bytes
146
- for (int i = 0 ; i < NSAPI_IPv6_BYTES/2 ; i++) {
147
- addr->bytes [2 *i+0 ] = (uint8_t )(shorts[i] >> 8 );
148
- addr->bytes [2 *i+1 ] = (uint8_t )(shorts[i] >> 0 );
149
- }
150
-
151
- return true ;
152
- }
153
48
154
49
// DNS packet parsing
155
50
static void dns_append_byte (uint8_t **p, uint8_t byte)
@@ -305,14 +200,14 @@ static int nsapi_dns_query_multiple(NetworkStack *stack, const char *host,
305
200
}
306
201
307
202
// check for simple ip addresses
308
- if (version == NSAPI_IPv4) {
309
- if (dns_parse_ipv4 (host, addr)) {
310
- return 0 ;
311
- }
312
- } else if (version == NSAPI_IPv6) {
313
- if (dns_parse_ipv6 (host, addr)) {
314
- return 0 ;
203
+ SocketAddress address;
204
+ if (address.set_ip_address (host)) {
205
+ if (address.get_ip_version () != version) {
206
+ return NSAPI_ERROR_DNS_FAILURE;
315
207
}
208
+
209
+ *addr = address.get_addr ();
210
+ return 0 ;
316
211
}
317
212
318
213
// create a udp socket
0 commit comments