@@ -55,64 +55,46 @@ void DNSClient::begin(const IPAddress& aDNSServer)
5555}
5656
5757
58- int DNSClient::inet_aton (const char * aIPAddrString , IPAddress& aResult )
58+ int DNSClient::inet_aton (const char * addr , IPAddress& result )
5959{
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 ;
6764
68- if (*p == ' \0 ' )
65+ while (*address )
6966 {
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' )
7569 {
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 ;
9074 }
91- else
92- {
93- // Next digit
94- segmentValue = (segmentValue*10 )+(*p - ' 0' );
95- }
96- p++;
9775 }
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 == ' .' )
10177 {
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 ;
10584 }
10685 else
10786 {
108- aResult[segment] = (byte)segmentValue;
109- return 1 ;
87+ // Invalid char
88+ return 0 ;
11089 }
11190 }
112- else
113- {
91+
92+ if (dots != 3 ) {
93+ // Too few dots (there must be 3 dots)
11494 return 0 ;
11595 }
96+ result[3 ] = acc;
97+ return 1 ;
11698}
11799
118100int DNSClient::getHostByName (const char * aHostname, IPAddress& aResult)
0 commit comments