Fix NULL dereferencing
[platform/upstream/connman.git] / client / mesh.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *
6  *  Copyright (C) 2017 Samsung Electronics Co., Ltd.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2 as
10  *  published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  *  02110-1301  USA
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <string.h>
29
30 #include "mesh.h"
31 #include "dbus_helpers.h"
32
33 static void print_mesh_peer(char *path, DBusMessageIter *iter)
34 {
35         char *name = "";
36         char state = ' ';
37         char *str, *property;
38         DBusMessageIter entry, val;
39         int count = 0, favorite = 0;
40
41         while (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
42                 dbus_message_iter_recurse(iter, &entry);
43                 dbus_message_iter_get_basic(&entry, &property);
44
45                 if (strcmp(property, "Name") == 0) {
46                         dbus_message_iter_next(&entry);
47                         dbus_message_iter_recurse(&entry, &val);
48                         dbus_message_iter_get_basic(&val, &name);
49                 } else if (strcmp(property, "State") == 0) {
50                         dbus_message_iter_next(&entry);
51                         dbus_message_iter_recurse(&entry, &val);
52                         dbus_message_iter_get_basic(&val, &str);
53
54                         if (str) {
55                                 if (strcmp(str, "online") == 0)
56                                         state = 'O';
57                                 else if (strcmp(str, "ready") == 0)
58                                         state = 'R';
59                                 else if (!strcmp(str, "association"))
60                                         state = 'a';
61                                 else if (!strcmp(str, "configuration"))
62                                         state = 'c';
63                                 else if (!strcmp(str, "disconnect"))
64                                         state = 'd';
65                         }
66                 } else if (strcmp(property, "Favorite") == 0) {
67                         dbus_message_iter_next(&entry);
68                         dbus_message_iter_recurse(&entry, &val);
69                         dbus_message_iter_get_basic(&val, &favorite);
70                 }
71
72                 dbus_message_iter_next(iter);
73                 count++;
74         }
75
76         str = strrchr(path, '/');
77         if (str)
78                 str++;
79         else
80                 str = path;
81
82         if (count > 0)
83                 fprintf(stdout, "%c%c %-20s %s", favorite != 0 ? 'A' : ' ',
84                                 state, name, str);
85         else
86                 fprintf(stdout, "%s %s", "unchanged", str);
87 }
88
89 static void list_mesh_peer_array(DBusMessageIter *iter)
90 {
91         DBusMessageIter array, dict;
92         char *path = NULL;
93
94         while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRUCT) {
95                 dbus_message_iter_recurse(iter, &array);
96                 if (dbus_message_iter_get_arg_type(&array)
97                                 != DBUS_TYPE_OBJECT_PATH)
98                         return;
99
100                 dbus_message_iter_get_basic(&array, &path);
101
102                 dbus_message_iter_next(&array);
103                 if (dbus_message_iter_get_arg_type(&array)
104                                                 == DBUS_TYPE_ARRAY) {
105                         dbus_message_iter_recurse(&array, &dict);
106                         print_mesh_peer(path, &dict);
107                 }
108
109                 if (dbus_message_iter_has_next(iter))
110                         fprintf(stdout, "\n");
111
112                 dbus_message_iter_next(iter);
113         }
114 }
115
116 void __connmanctl_mesh_peers_list(DBusMessageIter *iter)
117 {
118         DBusMessageIter array;
119         char *path;
120
121         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
122                 return;
123
124         dbus_message_iter_recurse(iter, &array);
125         list_mesh_peer_array(&array);
126
127         dbus_message_iter_next(iter);
128         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
129                 return;
130
131         fprintf(stdout, "\n}, {");
132
133         dbus_message_iter_recurse(iter, &array);
134         while (dbus_message_iter_get_arg_type(&array)
135                                         == DBUS_TYPE_OBJECT_PATH) {
136                 dbus_message_iter_get_basic(&array, &path);
137                 fprintf(stdout, "\n%s %s", "removed", path);
138
139                 dbus_message_iter_next(&array);
140         }
141
142 }
143
144 void __connmanctl_mesh_connected_peers_list(DBusMessageIter *iter)
145 {
146         DBusMessageIter array;
147
148         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
149                 return;
150
151         dbus_message_iter_recurse(iter, &array);
152         __connmanctl_dbus_print(&array, "  ", " = ", "\n");
153         fprintf(stdout, "\n");
154 }
155
156 void __connmanctl_mesh_disconnected_peers_list(DBusMessageIter *iter)
157 {
158         DBusMessageIter array;
159
160         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
161                 return;
162
163         dbus_message_iter_recurse(iter, &array);
164         __connmanctl_dbus_print(&array, "  ", " = ", "\n");
165         fprintf(stdout, "\n");
166 }