API to list and remove known services
v3 cover API to list and remove known services
Based on past discussions with Daniel Wagner, I've tried to adapt the patches according all the comments I've found in mailing list history, namely, compared to v2:
- consistent use of object path in the API calls
- avoid usage of glib types
- some formatting issues
Roman
[1] https://lore.kernel.org/connman/[email protected]/
[2] https://lore.kernel.org/connman/[email protected]/
[3] https://lore.kernel.org/connman/[email protected]/
PATCH v3 1/3 Add GetKnownServices api to connaman
From: Michael Trimarchi <[email protected]>
Add a way to retrieve all the networks that were configured using connman and not all the ones visible
--- client/commands.c | 14 ++++++++++++ doc/manager-api.txt | 11 +++++++++ src/connman.h | 1 + src/manager.c | 23 +++++++++++++++++++ src/service.c | 54 ++++++++++++++++++++++++++++++++++++++++++++ tools/manager-api.c | 30 ++++++++++++++++++++++++ tools/session-test.h | 1 + 7 files changed, 134 insertions(+)
client/commands.c +14 -0
@@ -420,6 +420,18 @@ static int cmd_services(char *args[], int num, struct connman_option *options) object_properties, path, NULL, NULL); } +static int cmd_known_services (char *args[],int num,struct connman_option *options) +{ +if (num >1 ) +return -E2BIG ; + +return __connmanctl_dbus_method_call (connection, +CONNMAN_SERVICE ,CONNMAN_PATH , +"net.connman.Manager" , +"GetKnownServices" , + services_list,NULL ,NULL ,NULL ); +} +static int cmd_peers (char *args[],int num,struct connman_option *options) {char *peer_name =NULL ;
@@ -2808,6 +2820,8 @@ static const struct { "Display tethering clients", NULL }, {"services" ,"[<service>]" , service_options, cmd_services, "Display services", lookup_service_arg }, + { "known-services", NULL, NULL, cmd_known_services, + "Display known services", NULL }, { "peers", "[peer]", NULL, cmd_peers, "Display peers", lookup_peer_arg }, { "scan", "<technology>", NULL, cmd_scan,
doc/manager-api.txt +11 -0
@@ -39,6 +39,17 @@ Methods dict GetProperties() Possible Errors: [service].Error.InvalidArguments + array{object,dict} GetKnownServices() + + Returns a sorted list of tuples with dictionary of service + properties. Those are all the services, including the ones + that are registered, but not visible or not in use. + + This list will not contain sensitive information + like passphrases etc. + + Possible Errors: [service].Error.InvalidArguments + array{object,dict} GetPeers() [experimental] Returns a sorted list of tuples with peer object path
src/connman.h +1 -0
@@ -721,6 +721,7 @@ int __connman_service_move(struct connman_service *service, struct connman_service *target, bool before);int __connman_service_load_modifiable (struct connman_service *service); +void __connman_known_service_list_struct (DBusMessageIter *iter);void __connman_service_list_struct (DBusMessageIter *iter); int __connman_service_compare(const struct connman_service *a,
src/manager.c +23 -0
@@ -175,6 +175,11 @@ static const struct connman_notifier technology_notifier = { .idle_state = idle_state, }; +static void append_known_service_structs (DBusMessageIter *iter,void *user_data) +{ +__connman_known_service_list_struct (iter); +} +static void append_service_structs (DBusMessageIter *iter,void *user_data) {__connman_service_list_struct (iter);
@@ -195,6 +200,21 @@ static DBusMessage *get_services(DBusConnection *conn,return reply; } +static DBusMessage *get_known_services (DBusConnection *conn, +DBusMessage *msg,void *data) +{ +DBusMessage *reply; + + reply =dbus_message_new_method_return (msg); +if (!reply) +return NULL ; + +__connman_dbus_append_objpath_dict_array (reply, + append_known_service_structs,NULL ); + +return reply; +} +static void append_peer_structs (DBusMessageIter *iter,void *user_data) {__connman_peer_list_struct (iter);
@@ -534,6 +554,9 @@ static const GDBusMethodTable manager_methods[] = { {GDBUS_DEPRECATED_METHOD ("RemoveProvider" , GDBUS_ARGS({ "provider", "o" }), NULL, remove_provider) }, + { GDBUS_METHOD("GetKnownServices", + NULL, GDBUS_ARGS({"knownServices", "a(oa{sv})" }), + get_known_services) }, { GDBUS_METHOD("GetServices", NULL, GDBUS_ARGS({ "services", "a(oa{sv})" }), get_services) },
src/service.c +54 -0
60 lines not on this page · raw message
tools/manager-api.c +30 -0
@@ -64,6 +64,36 @@ static DBusMessage *set_property(DBusConnection *connection,return reply; } +DBusMessage *manager_get_known_services (DBusConnection *connection) +{ +DBusMessage *message, *reply; +DBusError error; + + message =dbus_message_new_method_call (CONNMAN_SERVICE , +CONNMAN_MANAGER_PATH , +CONNMAN_MANAGER_INTERFACE , +"GetKnownServices" ); + +if (!message) +return NULL ; + +dbus_error_init (&error); + + reply =dbus_connection_send_with_reply_and_block (connection, + message,-1 , &error); +if (!reply) { +if (dbus_error_is_set (&error)) +dbus_error_free (&error); + +dbus_message_unref (message); +return NULL ; + } + +dbus_message_unref (message); + +return reply; +} +DBusMessage *manager_get_services (DBusConnection *connection) {DBusMessage *message, *reply;
tools/session-test.h +1 -0
@@ -114,6 +114,7 @@ DBusMessage *session_disconnect(DBusConnection *connection, struct test_session *session);/* manager-api.c */ +DBusMessage *manager_get_known_services (DBusConnection *connection);DBusMessage *manager_get_services (DBusConnection *connection);DBusMessage *manager_get_properties (DBusConnection *connection); DBusMessage *manager_create_session(DBusConnection *connection,
Signature · 1 line
2.49.0
Re: [PATCH v3 1/3] Add GetKnownServices api to connaman
Hi Roman,
Quoted 4 lines · Roman Smrž
From: Michael Trimarchi <[email protected]>
Add a way to retrieve all the networks that were configured using connman and not all the ones visible
Is this still about removing WiFi credentials? Have you considered using iwd where this would be exceedingly trivial to do?
Quoted 2 lines
--- client/commands.c | 14 ++++++++++++
Please separate the client part into its own commit
Quoted 6 lines
doc/manager-api.txt | 11 +++++++++ src/connman.h | 1 + src/manager.c | 23 +++++++++++++++++++ src/service.c | 54 ++++++++++++++++++++++++++++++++++++++++++++ tools/manager-api.c | 30 ++++++++++++++++++++++++ tools/session-test.h | 1 +
Ditto for 'tools/' bits.
Quoted 2 lines
7 files changed, 134 insertions(+)
<snip>
doc/manager-api.txt Methods dict GetProperties() · line 44
Quoted 11 lines
doc/manager-api.txt +3
-0@@ -39,6 +39,17 @@ Methods dict GetProperties() Possible Errors: [service].Error.InvalidArguments + array{object,dict} GetKnownServices() + + Returns a sorted list of tuples with dictionary of service
Sorted how?
Quoted 11 lines
doc/manager-api.txt +8
-0+ properties. Those are all the services, including the ones + that are registered, but not visible or not in use. + + This list will not contain sensitive information + like passphrases etc. + + Possible Errors: [service].Error.InvalidArguments + array{object,dict} GetPeers() [experimental] Returns a sorted list of tuples with peer object path
<snip>
Quoted 16 lines
src/service.c +8
-0@@ -5441,6 +5441,60 @@ static void append_struct(gpointer value, gpointer user_data)append_struct_service (iter, append_dict_properties, service); } +void __connman_known_service_list_struct (DBusMessageIter *iter) +{ +struct connman_service *service; +char **srv_id_parts; +char **services; +unsigned int i; + + services =connman_storage_get_services ();
src/service.c static void append_struct(gpointer value, gpointer user_data) · line 5461
Quoted 10 lines
src/service.c +10
-0+if (!services) +return ; + +for (i =0 ; i <g_strv_length (services); i++) { +struct connman_service *srv; + + service =connman_service_create (); +if (!service) { +connman_error ("connman_service_create() allocation failed" ); +return ;
You can't just return here, you're leaking 'services'
src/service.c static void append_struct(gpointer value, gpointer user_data)
Quoted 7 lines
src/service.c +7
-0+ } + + service->identifier =g_strdup (services[i]); + service->path =g_strdup_printf ("%s/service/%s" ,CONNMAN_PATH , + service->identifier); + srv_id_parts =g_strsplit (services[i],"_" ,-1 ); +> + service->type = __connman_service_string2type(srv_id_parts[0]);
This looks exceedingly horrible. I don't see an equivalent type classification anywhere else. Is there a way to use the information present in struct connman_config_service instead?
src/service.c static void append_struct(gpointer value, gpointer user_data) · line 5480
Quoted 11 lines
src/service.c +11
-0+if (service->type ==CONNMAN_SERVICE_TYPE_WIFI ) { +char *id = srv_id_parts[g_strv_length (srv_id_parts) -1 ]; + service->security =__connman_service_string2security (id); + } + + srv =find_service (service->path); +if (srv) + service->state = srv->state; + +if (service->type ==CONNMAN_SERVICE_TYPE_ETHERNET && srv) + service->name = srv->name;
That looks dangerous since 'name' is dynamically allocated.
src/service.c static void append_struct(gpointer value, gpointer user_data) · line 5484
Quoted 4 lines
src/service.c +4
-0+ +if (service_load (service)) { +connman_error ("service_load() returned error" ); +g_free (service);
This looks completely insufficient given what connman_service_create() does.
src/service.c static void append_struct(gpointer value, gpointer user_data) · line 5497
Quoted 13 lines
src/service.c +13
-0+g_strfreev (srv_id_parts); +g_strfreev (services); +return ; + } + +append_struct_service (iter, append_dict_properties, service); +g_strfreev (srv_id_parts); +g_free (service); + } + +g_strfreev (services); +} +
Regards,
-Denis
PATCH v3 2/3 Add RemoveKnownService to connman
In reply to Roman SmržFrom: Michael Trimarchi <[email protected]>
Add a way to remove stored network in connman storage. This allow to remove network even if they are not visible
--- src/connman.h | 3 +++ src/manager.c | 21 +++++++++++++++++++++ src/service.c | 25 +++++++++++++++++++++++++ tools/manager-api.c | 43 +++++++++++++++++++++++++++++++++++++++++++ tools/session-test.h | 2 ++ 5 files changed, 94 insertions(+)
src/connman.h +3 -0
@@ -790,6 +790,8 @@ int __connman_service_disconnect(struct connman_service *service);void __connman_service_set_active_session (bool enable,GSList *list);void __connman_service_auto_connect (enum connman_service_connect_reason reason);bool __connman_service_remove (struct connman_service *service); +bool __connman_service_remove_known_service (DBusConnection *conn, +DBusMessage *msg,const char *path);void __connman_service_set_hidden_data (struct connman_service *service,gpointer user_data); void __connman_service_return_error(struct connman_service *service,
@@ -847,6 +849,7 @@ void __connman_service_set_split_routing(struct connman_service *service,void __connman_service_split_routing_changed (struct connman_service *service);int __connman_service_get_index (const struct connman_service *service);void __connman_service_set_hidden (struct connman_service *service); +void __connman_service_set_auto_connect (struct connman_service *service,bool autoconnect);void __connman_service_set_hostname (struct connman_service *service,const char *hostname);const char *__connman_service_get_hostname (const struct connman_service *service);
src/manager.c +21 -0
@@ -215,6 +215,24 @@ static DBusMessage *get_known_services(DBusConnection *conn,return reply; } +static DBusMessage *remove_known_service (DBusConnection *conn, +DBusMessage *msg,void *data) +{ +const char *name; + +DBG ("conn %p" , conn); + +dbus_message_get_args (msg,NULL ,DBUS_TYPE_OBJECT_PATH , &name, +DBUS_TYPE_INVALID ); + +DBG ("Service to remove [%s]" , name); + +if (!__connman_service_remove_known_service (conn, msg, name)) +return __connman_error_failed (msg, -ENOENT ); + +return g_dbus_create_reply (msg,DBUS_TYPE_INVALID ); +} +static void append_peer_structs (DBusMessageIter *iter,void *user_data) {__connman_peer_list_struct (iter);
@@ -557,6 +575,9 @@ static const GDBusMethodTable manager_methods[] = { {GDBUS_METHOD ("GetKnownServices" , NULL, GDBUS_ARGS({"knownServices", "a(oa{sv})" }), get_known_services) }, + { GDBUS_METHOD("RemoveKnownService", + GDBUS_ARGS({ "name", "o" }), NULL, + remove_known_service) }, { GDBUS_METHOD("GetServices", NULL, GDBUS_ARGS({ "services", "a(oa{sv})" }), get_services) },
src/service.c +25 -0
@@ -7530,6 +7530,31 @@ bool __connman_service_remove(struct connman_service *service)return true; } +bool __connman_service_remove_known_service (DBusConnection *conn, +DBusMessage *msg,const char *path) +{ +struct connman_service *service; +const char * identifier; + +DBG ("Remove known service [%s]" , path); + + identifier =strrchr (path,'/' ); +if (!identifier) +return false; + + service =g_hash_table_lookup (service_hash, identifier); + +if (service) { +if (service->type !=CONNMAN_SERVICE_TYPE_ETHERNET && + service->type !=CONNMAN_SERVICE_TYPE_GADGET ) { +if (!__connman_service_remove (service)) +return false; + } + } + +return __connman_storage_remove_service (identifier); +} +static DBusMessage *remove_service (DBusConnection *conn,DBusMessage *msg,void *user_data) {
tools/manager-api.c +43 -0
49 lines not on this page · raw message
tools/session-test.h +2 -0
@@ -115,6 +115,8 @@ DBusMessage *session_disconnect(DBusConnection *connection,/* manager-api.c */ DBusMessage *manager_get_known_services (DBusConnection *connection); +DBusMessage *manager_remove_known_service (DBusConnection *connection, +const char *service_path);DBusMessage *manager_get_services (DBusConnection *connection);DBusMessage *manager_get_properties (DBusConnection *connection); DBusMessage *manager_create_session(DBusConnection *connection,
Signature · 1 line
2.49.0
PATCH v3 3/3 client: Add RemoveKnownService client implementation
In reply to Roman SmržFrom: Michael Trimarchi <[email protected]>
Add a way to test the remove network feature
--- client/commands.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+)
client/commands.c +43 -0
@@ -432,6 +432,47 @@ static int cmd_known_services(char *args[], int num, struct connman_option *opti services_list, NULL, NULL, NULL); } +static int remove_known_service_return (DBusMessageIter *iter,int errnum, +const char *error,void *user_data) +{ +char *name = user_data; + +if (!error) +fprintf (stdout,"Removed %s\n" , name); +else +fprintf (stderr,"Error %s: %s\n" , name, error); + +g_free (user_data); + +return 0 ; +} + +static void remove_known_service_append_data (DBusMessageIter *iter,void *user_data) +{ +const char *data = user_data; + +dbus_message_iter_append_basic (iter,DBUS_TYPE_STRING , &data); +} + +static int cmd_remove_known_service (char *args[],int num,struct connman_option *options) +{ +char *data; + +if (num >2 ) +return -E2BIG ; + +if (num <2 ) +return -EINVAL ; + + data =g_strdup_printf ("%s" , args[1 ]); + +return __connmanctl_dbus_method_call (connection, +CONNMAN_SERVICE ,CONNMAN_PATH , +"net.connman.Manager" , +"RemoveKnownService" , + remove_known_service_return, data, remove_known_service_append_data, data); +} +static int cmd_peers (char *args[],int num,struct connman_option *options) {char *peer_name =NULL ;
@@ -2822,6 +2863,8 @@ static const struct { "Display services", lookup_service_arg }, {"known-services" ,NULL ,NULL , cmd_known_services, "Display known services", NULL }, + { "remove-known-service", "[<service>]", NULL, + cmd_remove_known_service, "Remove known service", NULL }, { "peers", "[peer]", NULL, cmd_peers, "Display peers", lookup_peer_arg }, { "scan", "<technology>", NULL, cmd_scan,
Signature · 1 line
2.49.0