location-query: Add listing networks by flags master
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 15 Nov 2019 15:58:59 +0000 (15:58 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 15 Nov 2019 15:58:59 +0000 (15:58 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
man/location-query.txt
src/python/database.c
src/python/location-query.in

index ad129ed..957dc01 100644 (file)
@@ -10,6 +10,7 @@ location-query - Query the location database
 `location-query search-as STRING`
 `location-query list-networks-by-as ASN`
 `location-query list-networks-by-cc COUNTRY_CODE`
+`location-query list-networks-by-flags [--anonymous-proxy|--satellite-provider|--anycast]`
 
 == DESCRIPTION
 The `location-query` retrieves information from the location database.
@@ -62,6 +63,11 @@ or countries.
        +
        See above for usage of the '--output-format' parameter.
 
+'list-networks-by-flags [--output-format FORMAT] [--anonymous-proxy|--satellite-provider|--anycast]'::
+       Lists all networks that have a certain flag.
+       +
+       See above for usage of the '--output-format' parameter.
+
 '--help'::
        Shows a short help text on using this program.
 
index 2210666..c31f8cf 100644 (file)
@@ -203,11 +203,12 @@ static PyObject* Database_search_as(DatabaseObject* self, PyObject* args) {
 }
 
 static PyObject* Database_search_networks(DatabaseObject* self, PyObject* args, PyObject* kwargs) {
-       char* kwlist[] = { "country_code", "asn", NULL };
+       char* kwlist[] = { "country_code", "asn", "flags", NULL };
        const char* country_code = NULL;
        unsigned int asn = 0;
+       int flags = 0;
 
-    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|si", kwlist, &country_code, &asn))
+    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|sii", kwlist, &country_code, &asn, &flags))
                return NULL;
 
        struct loc_database_enumerator* enumerator;
@@ -237,6 +238,16 @@ static PyObject* Database_search_networks(DatabaseObject* self, PyObject* args,
                }
        }
 
+       // Set the flags we are searching for
+       if (flags) {
+               r = loc_database_enumerator_set_flag(enumerator, flags);
+
+               if (r) {
+                       PyErr_SetFromErrno(PyExc_SystemError);
+                       return NULL;
+               }
+       }
+
        PyObject* obj = new_database_enumerator(&DatabaseEnumeratorType, enumerator);
        loc_database_enumerator_unref(enumerator);
 
index 72b7c25..e047e03 100644 (file)
@@ -177,6 +177,23 @@ class CLI(object):
                        choices=self.output_formats.keys(), default="list")
                list_networks_by_cc.set_defaults(func=self.handle_list_networks_by_cc)
 
+               # List all networks with flags
+               list_networks_by_flags = subparsers.add_parser("list-networks-by-flags",
+                       help=_("Lists all networks with flags"),
+               )
+               list_networks_by_flags.add_argument("--anonymous-proxy",
+                       action="store_true", help=_("Anonymous Proxies"),
+               )
+               list_networks_by_flags.add_argument("--satellite-provider",
+                       action="store_true", help=_("Satellite Providers"),
+               )
+               list_networks_by_flags.add_argument("--anycast",
+                       action="store_true", help=_("Anycasts"),
+               )
+               list_networks_by_flags.add_argument("--output-format",
+                       choices=self.output_formats.keys(), default="list")
+               list_networks_by_flags.set_defaults(func=self.handle_list_networks_by_flags)
+
                args = parser.parse_args()
 
                # Print usage if no action was given
@@ -300,6 +317,22 @@ class CLI(object):
                                for n in db.search_networks(country_code=country_code):
                                        f.network(n)
 
+       def handle_list_networks_by_flags(self, db, ns):
+               flags = 0
+
+               if ns.anonymous_proxy:
+                       flags |= location.NETWORK_FLAG_ANONYMOUS_PROXY
+
+               if ns.satellite_provider:
+                       flags |= location.NETWORK_FLAG_SATELLITE_PROVIDER
+
+               if ns.anycast:
+                       flags |= location.NETWORK_FLAG_ANYCAST
+
+               with self.__get_output_formatter(ns) as f:
+                       for n in db.search_networks(flags=flags):
+                               f.network(n)
+
 
 def main():
        # Run the command line interface