Good morning,
On 22 Oct 2020, at 14:39, Gisle Vanem gisle.vanem@gmail.com wrote:
I've changed the subject since it's now off-topic (solved that part).
And a CRT is what?
A "C Run-time".
// Set country code
loc_network_set_country_code(network4, "ZZ");
I assume you meant to set the country code for network5?
Oh, yes. Silly me.
But other item. I've come a long way integrating 'libloc' support in my program. Basically I do:
struct in6_addr addr; struct loc_network *net; struct loc_as *as = NULL; ... loc_database_lookup (libloc_data.db, &addr, &net); int family = loc_network_address_family (net); const char *country = loc_network_get_country_code (net); const char *AS_name = NULL;
uint32 AS_num = loc_network_get_asn (net); is_anycast = loc_network_has_flag (net, LOC_NETWORK_FLAG_ANYCAST); is_anon_proxy = loc_network_has_flag (net, LOC_NETWORK_FLAG_ANONYMOUS_PROXY); sat_provider = loc_network_has_flag (net, LOC_NETWORK_FLAG_SATELLITE_PROVIDER);
if (AS_num) { int r = loc_as_new (libloc_data.ctx, &as, AS_num); if (r == 0) AS_name = loc_as_get_name (as); } printf ("ASN: %lu, AF: %d, country: %s, name: %s (%d,%d,%d)", AS_num, family, country, AS_name, is_anycast, is_anon_proxy, sat_provider); if (as) loc_as_unref (as); loc_network_unref (net);
Cool, this doesn’t look too bad.
First thing to know is that we use reference counting for all objects.
So every pointer that refers to an object has to decrement the counter when it no longer needs it. The object will be automatically cleaned up after nothing needs it any more. Therefore the loc_*_unref() calls are always needed. If you don’t call them, you will leak memory.
The next thing is that networks and AS are independent of each other. You can have a network giving you an ASN, but there might be no AS object in the database for it. That is because we might not have a name for that AS (ARIN doesn’t publicly share them) and therefore we do not need to waste memory on an empty object.
In your code, you are creating a new AS which is not what you want.
You want to call loc_database_get_as() with the AS number that you got from loc_network_get_asn(). That function with then either return an AS or NULL.
loc_as_get_name will give you the name. So your code might look like this:
struct loc_network* network = loc_database_lookup_from_string(db, "81.3.27.38"); if (network) { uint32_t asn = loc_network_get_asn(network);
if (asn) { struct loc_as* a = loc_database_get_as(db, asn); if (a) { name = loc_as_get_name(a); } }
loc_network_unref(network); }
// Copy the name to somewhere ...
// Cleanup if (a) loc_as_unref(a);
Everything except 'AS_name' is okay and prints sensible values (I've checked all ret-vals). Since there isn't any 'loc_get_asn_by_number()' function, what should I do to get a non-NULL 'AS_name' from 'AS_num'?
PS. do I really need this 'loc_network_unref()'? Or does 'loc_database_lookup()' handle it?
I hope this answers your questions :)
Best, -Michael