sources/network/ipaddress.cpp

changeset 7
01e4e9ae323a
parent 5
146825d63b9a
child 8
8b697d30c49f
equal deleted inserted replaced
6:c709bb1a08e4 7:01e4e9ae323a
116 unsigned int parts[4]; 116 unsigned int parts[4];
117 int colonpos = input.find (":"); 117 int colonpos = input.find (":");
118 String addressString = colonpos == -1 ? input : input.mid (0, colonpos); 118 String addressString = colonpos == -1 ? input : input.mid (0, colonpos);
119 IPAddress value; 119 IPAddress value;
120 120
121 // Try scanf the IPv4 host first. If it's not an IP string, it could 121 // Try scanf the IPv4 host first
122 // be a hostname; thus try resolve it.
123 if (sscanf (addressString, "%u.%u.%u.%u", &parts[0], &parts[1], &parts[2], &parts[3])) 122 if (sscanf (addressString, "%u.%u.%u.%u", &parts[0], &parts[1], &parts[2], &parts[3]))
124 { 123 {
125 for (short i = 0; i < 4; ++i) 124 for (short i = 0; i < 4; ++i)
126 value.set_octet (i, parts[i]); 125 value.set_octet (i, parts[i]);
127 } 126 }
128 else 127 else
129 { 128 {
130 // try resolve it 129 // Possibly a hostname, try resolve it
131 return IPAddress::resolve (addressString, ok); 130 value = IPAddress::resolve (addressString, ok);
131
132 if (*ok == false)
133 return value;
132 } 134 }
133 135
134 if (colonpos != -1) 136 if (colonpos != -1)
135 value.port = atoi (input.mid (colonpos + 1, -1)); 137 value.port = input.mid (colonpos + 1, -1).to_int();
136 138
137 return value; 139 return value;
138 } 140 }
139 141
140 // ----------------------------------------------------------------------------- 142 // -----------------------------------------------------------------------------

mercurial