@@ -55,64 +55,46 @@ void DNSClient::begin(const IPAddress& aDNSServer)
55
55
}
56
56
57
57
58
- int DNSClient::inet_aton (const char * aIPAddrString , IPAddress& aResult )
58
+ int DNSClient::inet_aton (const char * addr , IPAddress& result )
59
59
{
60
- // See if we've been given a valid IP address
61
- const char * p =aIPAddrString;
62
- while (*p &&
63
- ( (*p == ' .' ) || (*p >= ' 0' ) || (*p <= ' 9' ) ))
64
- {
65
- p++;
66
- }
60
+ // TODO: add support for "a", "a.b", "a.b.c" formats
61
+
62
+ uint16_t acc = 0 ; // Accumulator
63
+ uint8_t dots = 0 ;
67
64
68
- if (*p == ' \0 ' )
65
+ while (*address )
69
66
{
70
- // It's looking promising, we haven't found any invalid characters
71
- p = aIPAddrString;
72
- int segment =0 ;
73
- int segmentValue =0 ;
74
- while (*p && (segment < 4 ))
67
+ char c = *address++;
68
+ if (c >= ' 0' && c <= ' 9' )
75
69
{
76
- if (*p == ' .' )
77
- {
78
- // We've reached the end of a segment
79
- if (segmentValue > 255 )
80
- {
81
- // You can't have IP address segments that don't fit in a byte
82
- return 0 ;
83
- }
84
- else
85
- {
86
- aResult[segment] = (byte)segmentValue;
87
- segment++;
88
- segmentValue = 0 ;
89
- }
70
+ acc = acc * 10 + (c - ' 0' );
71
+ if (acc > 255 ) {
72
+ // Value out of [0..255] range
73
+ return 0 ;
90
74
}
91
- else
92
- {
93
- // Next digit
94
- segmentValue = (segmentValue*10 )+(*p - ' 0' );
95
- }
96
- p++;
97
75
}
98
- // We've reached the end of address, but there'll still be the last
99
- // segment to deal with
100
- if ((segmentValue > 255 ) || (segment > 3 ))
76
+ else if (c == ' .' )
101
77
{
102
- // You can't have IP address segments that don't fit in a byte,
103
- // or more than four segments
104
- return 0 ;
78
+ if (dots == 3 ) {
79
+ // Too much dots (there must be 3 dots)
80
+ return 0 ;
81
+ }
82
+ result[dots++] = acc;
83
+ acc = 0 ;
105
84
}
106
85
else
107
86
{
108
- aResult[segment] = (byte)segmentValue;
109
- return 1 ;
87
+ // Invalid char
88
+ return 0 ;
110
89
}
111
90
}
112
- else
113
- {
91
+
92
+ if (dots != 3 ) {
93
+ // Too few dots (there must be 3 dots)
114
94
return 0 ;
115
95
}
96
+ result[3 ] = acc;
97
+ return 1 ;
116
98
}
117
99
118
100
int DNSClient::getHostByName (const char * aHostname, IPAddress& aResult)
0 commit comments