`location list-networks-by-as ASN`
`location list-networks-by-cc COUNTRY_CODE`
`location list-networks-by-flags [--anonymous-proxy|--satellite-provider|--anycast]`
+`location list-countries [--show-name] [--show-continent]`
== DESCRIPTION
`location` retrieves information from the location database.
+
See above for usage of the '--family' and '--output-format' parameters.
+'list-countries [--show-name] [--show-continent]'::
+ Lists all countries known to the database.
+ +
+ With the optional parameters '--show-name' and '--show-continent', the name and
+ continent code will be printed, too.
+
'--help'::
Shows a short help text on using this program.
// Index of the AS we are looking at
unsigned int as_index;
+ // Index of the country we are looking at
+ unsigned int country_index;
+
// Network state
struct in6_addr network_address;
struct loc_node_stack network_stack[MAX_STACK_DEPTH];
return 0;
}
+
+LOC_EXPORT int loc_database_enumerator_next_country(
+ struct loc_database_enumerator* enumerator, struct loc_country** country) {
+ *country = NULL;
+
+ // Do not do anything if not in country mode
+ if (enumerator->mode != LOC_DB_ENUMERATE_COUNTRIES)
+ return 0;
+
+ struct loc_database* db = enumerator->db;
+
+ while (enumerator->country_index < db->countries_count) {
+ // Fetch the next country
+ int r = loc_database_fetch_country(db, country, enumerator->country_index++);
+ if (r)
+ return r;
+
+ // We do not filter here, so it always is a match
+ return 0;
+ }
+
+ // Reset the index
+ enumerator->country_index = 0;
+
+ // We have searched through all of them
+ return 0;
+}
# Database Enumerator
loc_database_enumerator_new;
loc_database_enumerator_next_as;
+ loc_database_enumerator_next_country;
loc_database_enumerator_next_network;
loc_database_enumerator_ref;
loc_database_enumerator_set_asn;
struct loc_country** country, const char* code);
enum loc_database_enumerator_mode {
- LOC_DB_ENUMERATE_NETWORKS = 1,
- LOC_DB_ENUMERATE_ASES = 2,
+ LOC_DB_ENUMERATE_NETWORKS = 1,
+ LOC_DB_ENUMERATE_ASES = 2,
+ LOC_DB_ENUMERATE_COUNTRIES = 3,
};
struct loc_database_enumerator;
struct loc_database_enumerator* enumerator, struct loc_as** as);
int loc_database_enumerator_next_network(
struct loc_database_enumerator* enumerator, struct loc_network** network);
+int loc_database_enumerator_next_country(
+ struct loc_database_enumerator* enumerator, struct loc_country** country);
#endif
return obj;
}
+static PyObject* Database_countries(DatabaseObject* self) {
+ return Database_iterate_all(self, LOC_DB_ENUMERATE_COUNTRIES);
+}
+
static struct PyMethodDef Database_methods[] = {
{
"get_as",
NULL,
NULL,
},
+ {
+ "countries",
+ (getter)Database_countries,
+ NULL,
+ NULL,
+ NULL,
+ },
{
"created_at",
(getter)Database_get_created_at,
return obj;
}
+ // Enumerate all countries
+ struct loc_country* country = NULL;
+
+ r = loc_database_enumerator_next_country(self->enumerator, &country);
+ if (r) {
+ PyErr_SetFromErrno(PyExc_ValueError);
+ return NULL;
+ }
+
+ if (country) {
+ PyObject* obj = new_country(&CountryType, country);
+ loc_country_unref(country);
+
+ return obj;
+ }
+
// Nothing found, that means the end
PyErr_SetNone(PyExc_StopIteration);
return NULL;
choices=location.export.formats.keys(), default="list")
list_networks_by_flags.set_defaults(func=self.handle_list_networks_by_flags)
+ # List countries
+ list_countries = subparsers.add_parser("list-countries",
+ help=_("Lists all countries"),
+ )
+ list_countries.add_argument("--show-name",
+ action="store_true", help=_("Show the name of the country"),
+ )
+ list_countries.add_argument("--show-continent",
+ action="store_true", help=_("Show the continent"),
+ )
+ list_countries.set_defaults(func=self.handle_list_countries)
+
# Export
export = subparsers.add_parser("export",
help=_("Exports data in many formats to load it into packet filters"),
return cls
+ def handle_list_countries(self, db, ns):
+ for country in db.countries:
+ line = [
+ country.code,
+ ]
+
+ if ns.show_continent:
+ line.append(country.continent_code)
+
+ if ns.show_name:
+ line.append(country.name)
+
+ # Format the output
+ line = " ".join(line)
+
+ # Print the output
+ print(line)
+
def handle_list_networks_by_as(self, db, ns):
writer = self.__get_output_formatter(ns)