Support getting list of ip/mask for one interface, change netdev_set_ip* to netdev_ad...
[platform/core/security/vasum.git] / client / vasum-client-impl.hpp
1 /*
2  *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Mateusz Malicki <m.malicki2@samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  */
18
19
20 /**
21  * @file
22  * @author  Mateusz Malicki (m.malicki2@samsung.com)
23  * @brief   This file contains vasum-server's client definition
24  */
25
26 #ifndef VASUM_CLIENT_IMPL_HPP
27 #define VASUM_CLIENT_IMPL_HPP
28
29 #include "vasum-client.h"
30 #include "ipc/epoll/thread-dispatcher.hpp"
31 #include "ipc/epoll/event-poll.hpp"
32 #include "ipc/client.hpp"
33
34 #include <mutex>
35 #include <memory>
36 #include <functional>
37 #include <linux/if_link.h>
38
39 /**
40  * Zone's D-Bus state change callback function signature.
41  *
42  * @param[in] zoneId affected zone id
43  * @param[in] dbusAddress new D-Bus address
44  * @param data custom user's data pointer passed to vsm_add_state_callback() function
45  */
46 typedef std::function<void (const char *zoneId, const char *dbusAddress, void *data)> VsmZoneDbusStateFunction;
47
48 /**
49  * Zone information structure
50  */
51 typedef struct ZoneStructure {
52     char *id;
53     int terminal;
54     VsmZoneState state;
55     char *rootfs_path;
56 } *Zone;
57
58 /**
59  * Network device information structure
60  */
61 typedef struct NetdevStructure {
62     char *name;
63     VsmNetdevType type;
64 } *Netdev;
65
66 /**
67  * Network interface information structure
68  */
69 typedef struct {
70     int type;
71     int prefix;
72     union {
73         struct in_addr ipv4;
74         struct in6_addr ipv6;
75     } addr;
76 } InetAddr;
77
78
79 /**
80  * vasum's client definition.
81  *
82  * Client uses dbus API.
83  */
84 class Client {
85 public:
86     Client() noexcept;
87     ~Client() noexcept;
88
89     /**
90      * Connect client with system ipc address.
91      *
92      * @return status of this function call
93      */
94     VsmStatus connectSystem() noexcept;
95
96     /**
97      * Connect client.
98      *
99      * @param address ipc socket address
100      * @return status of this function call
101      */
102     VsmStatus connect(const std::string& address) noexcept;
103
104     /**
105      * Disconnect client
106      */
107     VsmStatus disconnect() noexcept;
108
109     /**
110      *  @see ::vsm_get_poll_fd
111      */
112     VsmStatus vsm_get_poll_fd(int* fd) noexcept;
113
114     /**
115      *  @see ::vsm_enter_eventloop
116      */
117     VsmStatus vsm_enter_eventloop(int flags, int timeout) noexcept;
118
119     /**
120      *  @see ::vsm_set_dispatcher_type
121      */
122     VsmStatus vsm_set_dispatcher_type(VsmDispacherType dispacher) noexcept;
123
124     /**
125      *  @see ::vsm_get_dispatcher_type
126      */
127     VsmStatus vsm_get_dispatcher_type(VsmDispacherType* dispacher) noexcept;
128
129     /**
130      *  @see ::vsm_get_status_message
131      */
132     const char* vsm_get_status_message() const noexcept;
133
134     /**
135      *  @see ::vsm_get_status
136      */
137     VsmStatus vsm_get_status() const noexcept;
138
139     /**
140      *  @see ::vsm_get_zone_dbuses
141      */
142     VsmStatus vsm_get_zone_dbuses(VsmArrayString* keys, VsmArrayString* values) noexcept;
143
144     /**
145      *  @see ::vsm_lock_queue
146      */
147     VsmStatus vsm_lock_queue() noexcept;
148
149     /**
150      *  @see ::vsm_unlock_queue
151      */
152     VsmStatus vsm_unlock_queue() noexcept;
153
154     /**
155      *  @see ::vsm_get_zone_ids
156      */
157     VsmStatus vsm_get_zone_ids(VsmArrayString* array) noexcept;
158
159     /**
160      *  @see ::vsm_get_active_zone_id
161      */
162     VsmStatus vsm_get_active_zone_id(VsmString* id) noexcept;
163
164     /**
165      *  @see ::vsm_lookup_zone_by_pid
166      */
167     VsmStatus vsm_lookup_zone_by_pid(int pid, VsmString* id) noexcept;
168
169     /**
170      * @see ::vsm_lookup_zone_by_id
171      */
172     VsmStatus vsm_lookup_zone_by_id(const char* id, Zone* zone) noexcept;
173
174     /**
175      * @see ::vsm_lookup_zone_by_terminal_id
176      */
177     VsmStatus vsm_lookup_zone_by_terminal_id(int terminal, VsmString* id) noexcept;
178
179     /**
180      *  @see ::vsm_set_active_zone
181      */
182     VsmStatus vsm_set_active_zone(const char* id) noexcept;
183
184     /**
185      *  @see ::vsm_create_zone
186      */
187     VsmStatus vsm_create_zone(const char* id, const char* tname) noexcept;
188
189     /**
190      *  @see ::vsm_destroy_zone
191      */
192     VsmStatus vsm_destroy_zone(const char* id) noexcept;
193
194     /**
195      *  @see ::vsm_shutdown_zone
196      */
197     VsmStatus vsm_shutdown_zone(const char* id) noexcept;
198
199     /**
200      *  @see ::vsm_start_zone
201      */
202     VsmStatus vsm_start_zone(const char* id) noexcept;
203
204     /**
205      *  @see ::vsm_lock_zone
206      */
207     VsmStatus vsm_lock_zone(const char* id) noexcept;
208
209     /**
210      *  @see ::vsm_unlock_zone
211      */
212     VsmStatus vsm_unlock_zone(const char* id) noexcept;
213
214     /**
215      *  @see ::vsm_add_state_callback
216      */
217     VsmStatus vsm_add_state_callback(VsmZoneDbusStateFunction zoneDbusStateCallback,
218                                      void* data,
219                                      VsmSubscriptionId* subscriptionId) noexcept;
220
221     /**
222      *  @see ::vsm_del_state_callback
223      */
224     VsmStatus vsm_del_state_callback(VsmSubscriptionId subscriptionId) noexcept;
225
226     /**
227      *  @see ::vsm_del_state_callback
228      */
229     VsmStatus vsm_grant_device(const char* id,
230                                const char* device,
231                                uint32_t flags) noexcept;
232
233     /**
234      *  @see ::vsm_revoke_device
235      */
236     VsmStatus vsm_revoke_device(const char* id, const char* device) noexcept;
237
238     /**
239      *  @see ::vsm_zone_get_netdevs
240      */
241     VsmStatus vsm_zone_get_netdevs(const char* zone, VsmArrayString* netdevIds) noexcept;
242
243     VsmStatus vsm_netdev_get_ip_addr(const char* zone,
244                                      const char* netdevId,
245                                      std::vector<InetAddr>& addrs) noexcept;
246
247     /**
248      *  @see ::vsm_netdev_get_ipv4_addr
249      */
250     VsmStatus vsm_netdev_get_ipv4_addr(const char* zone,
251                                        const char* netdevId,
252                                        struct in_addr *addr) noexcept;
253
254     /**
255      *  @see ::vsm_netdev_get_ipv6_addr
256      */
257     VsmStatus vsm_netdev_get_ipv6_addr(const char* zone,
258                                        const char* netdevId,
259                                        struct in6_addr *addr) noexcept;
260
261     /**
262      *  @see ::vsm_netdev_add_ipv4_addr
263      */
264     VsmStatus vsm_netdev_add_ipv4_addr(const char* zone,
265                                        const char* netdevId,
266                                        struct in_addr *addr,
267                                        int prefix) noexcept;
268
269     /**
270      *  @see ::vsm_netdev_add_ipv6_addr
271      */
272     VsmStatus vsm_netdev_add_ipv6_addr(const char* zone,
273                                        const char* netdevId,
274                                        struct in6_addr *addr,
275                                        int prefix) noexcept;
276
277     /**
278      *  @see ::vsm_netdev_del_ipv4_addr
279      */
280     VsmStatus vsm_netdev_del_ipv4_addr(const char* zone,
281                                       const char* netdevId,
282                                       struct in_addr* addr,
283                                       int prefix) noexcept;
284
285     /**
286      *  @see ::vsm_netdev_del_ipv6_addr
287      */
288     VsmStatus vsm_netdev_del_ipv6_addr(const char* zone,
289                                       const char* netdevId,
290                                       struct in6_addr* addr,
291                                       int prefix) noexcept;
292
293     /**
294      *  @see ::vsm_netdev_up
295      */
296     VsmStatus vsm_netdev_up(const char* zone, const char* netdevId) noexcept;
297
298     /**
299      *  @see ::vsm_netdev_down
300      */
301     VsmStatus vsm_netdev_down(const char* zone, const char* netdevId) noexcept;
302
303     /**
304      *  @see ::vsm_create_netdev_veth
305      */
306     VsmStatus vsm_create_netdev_veth(const char* zone,
307                                      const char* zoneDev,
308                                      const char* hostDev) noexcept;
309
310     /**
311      *  @see ::vsm_create_netdev_macvlan
312      */
313     VsmStatus vsm_create_netdev_macvlan(const char* zone,
314                                         const char* zoneDev,
315                                         const char* hostDev,
316                                         enum macvlan_mode mode) noexcept;
317
318     /**
319      *  @see ::vsm_create_netdev_phys
320      */
321     VsmStatus vsm_create_netdev_phys(const char* zone, const char* devId) noexcept;
322
323     /**
324      *  @see ::vsm_lookup_netdev_by_name
325      */
326     VsmStatus vsm_lookup_netdev_by_name(const char* zone,
327                                         const char* netdevId,
328                                         Netdev* netdev) noexcept;
329
330     /**
331      *  @see ::vsm_destroy_netdev
332      */
333     VsmStatus vsm_destroy_netdev(const char* zone, const char* devId) noexcept;
334
335     /**
336      *  @see ::vsm_declare_file
337      */
338     VsmStatus vsm_declare_file(const char* zone,
339                                VsmFileType type,
340                                const char* path,
341                                int32_t flags,
342                                mode_t mode,
343                                VsmString* id) noexcept;
344
345     /**
346      * @see ::vsm_declare_mount
347      */
348     VsmStatus vsm_declare_mount(const char* source,
349                                 const char* zone,
350                                 const char* target,
351                                 const char* type,
352                                 uint64_t flags,
353                                 const char* data,
354                                 VsmString* id) noexcept;
355     /**
356      * @see ::vsm_declare_link
357      */
358     VsmStatus vsm_declare_link(const char* source,
359                                const char* zone,
360                                const char* target,
361                                VsmString* id) noexcept;
362
363     /**
364      * @see ::vsm_list_declarations
365      */
366     VsmStatus vsm_list_declarations(const char* zone, VsmArrayString* declarations) noexcept;
367
368     /**
369      * @see ::vsm_remove_declaration
370      */
371     VsmStatus vsm_remove_declaration(const char* zone, VsmString declaration) noexcept;
372
373     /**
374      * @see ::vsm_clean_up_zones_root
375      */
376     VsmStatus vsm_clean_up_zones_root() noexcept;
377
378 private:
379     struct Status {
380         Status();
381         Status(VsmStatus status, const std::string& msg = "");
382         VsmStatus mVsmStatus;
383         std::string mMsg;
384     };
385     Status mStatus;
386
387     mutable std::mutex mStatusMutex;
388     std::unique_ptr<ipc::epoll::ThreadDispatcher> mInternalDispatcher;
389     std::unique_ptr<ipc::epoll::EventPoll> mEventPoll;
390     std::unique_ptr<ipc::Client> mClient;
391
392     bool isConnected() const;
393     bool isInternalDispatcherEnabled() const;
394     ipc::epoll::EventPoll& getEventPoll() const;
395     VsmStatus coverException(const std::function<void(void)>& worker) noexcept;
396 };
397
398 #endif /* VASUM_CLIENT_IMPL_HPP */