I had some more time yesterday because people on the floor below me had a party until quite late, so I stayed up a bit and implemented the planned IP lookup functions. There are seven new functions for this, which seems a bit inflationary, but it should be pretty easy to get the hang of it.
The main problem is that lookups can take time, sometimes several seconds, so the API to use them has to be nonblocking. As a result, IP lookups are now resources of their own that have to be created and destroyed. You can create them with ip_lookup_create(hostname), which returns a lookup handle and starts resolving the name. There are also variants ipv4_lookup_create and ipv6_lookup_create, which will only look for IPv4/IPv6 addresses - the default way is to look for both.
After creating them, you can regularly query whether the lookups are finished by calling ip_lookup_ready(lookup), which unsurprisingly returns true once the results are ready. The thing about the results is that there can be several IPs for a single hostname - take google.com for example, which has multiple IPv4 adresses associated with it. In order to read the results, you use the functions ip_lookup_has_next() and ip_lookup_next_result(). The first one tells you if there is another IP in the results that you didn't read yet, and the second one reads the next IP. This is similar to the concept of an iterator, and makes it convenient to loop over all results. For example, the following code queries all IPs associated with google.com and shows a message for each one:
lookup = ip_lookup_create("google.com");
while(!ip_lookup_ready(lookup)) {
sleep(1); // Or do something useful, like redraw your screen
}
while(ip_lookup_has_next(lookup)) {
show_message(ip_lookup_next_result(lookup));
}
ip_lookup_destroy(lookup);
Edited by Medo42, 15 July 2011 - 09:49 AM.