Get/set ipv4/ipv6 address, up/down interface
[platform/core/security/vasum.git] / client / vasum-client.h
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 the public API for Vasum Client
24  *
25  * @par Example usage:
26  * @code
27 #include <stdio.h>
28 #include "client/vasum-client.h"
29
30 int main(int argc, char** argv)
31 {
32     VsmStatus status;
33     VsmClient client;
34     VsmArrayString values = NULL;
35     int ret = 0;
36
37     status = vsm_start_glib_loop(); // start glib loop (if not started any yet)
38     if (VSMCLIENT_SUCCESS != status) {
39         // error!
40         return 1;
41     }
42
43     client = vsm_client_create(); // create client handle
44     if (NULL == client) {
45         // error!
46         ret = 1;
47         goto finish;
48     }
49
50     status = vsm_connect(client); // connect to dbus
51     if (VSMCLIENT_SUCCESS != status) {
52         // error!
53         ret = 1;
54         goto finish;
55     }
56
57     status = vsm_get_zone_ids(client, &values);
58     if (VSMCLIENT_SUCCESS != status) {
59         // error!
60         ret = 1;
61         goto finish;
62     }
63
64     // print array
65     for (VsmArrayString iValues = values; *iValues; iValues++) {
66         printf("%s\n", *iValues);
67     }
68
69 finish:
70     vsm_array_string_free(values); // free memory
71     vsm_client_free(client); // destroy client handle
72     vsm_stop_glib_loop(); // stop the glib loop (use only with vsm_start_glib_loop)
73     return ret;
74 }
75  @endcode
76  */
77
78 #ifndef VASUM_CLIENT_H
79 #define VASUM_CLIENT_H
80
81 #include <stdint.h>
82 #include <sys/stat.h>
83 #include <netinet/ip.h>
84 #include <linux/if_link.h>
85
86 #ifdef __cplusplus
87 extern "C"
88 {
89 #endif
90
91 /**
92  * vasum-server's client pointer.
93  */
94 typedef void* VsmClient;
95
96 /**
97  * NULL-terminated string type.
98  *
99  * @sa vsm_array_string_free
100  */
101 typedef char* VsmString;
102
103 /**
104  * NULL-terminated array of strings type.
105  *
106  * @sa vsm_string_free
107  */
108 typedef VsmString* VsmArrayString;
109
110 /**
111  * Completion status of communication function.
112  */
113 typedef enum {
114     VSMCLIENT_CUSTOM_ERROR,     /**< User specified error */
115     VSMCLIENT_IO_ERROR,         /**< Input/Output error */
116     VSMCLIENT_OPERATION_FAILED, /**< Operation failed */
117     VSMCLIENT_INVALID_ARGUMENT, /**< Invalid argument */
118     VSMCLIENT_OTHER_ERROR,      /**< Other error */
119     VSMCLIENT_SUCCESS           /**< Success */
120 } VsmStatus;
121
122 /**
123  * Subscription id
124  */
125 typedef unsigned int VsmSubscriptionId;
126
127 /**
128  * States of zone
129  */
130 typedef enum {
131     STOPPED,
132     STARTING,
133     RUNNING,
134     STOPPING,
135     ABORTING,
136     FREEZING,
137     FROZEN,
138     THAWED,
139     LOCKED,
140     MAX_STATE,
141     ACTIVATING = 128
142 } VsmZoneState;
143
144 /**
145  * Zone information structure
146  */
147 typedef struct {
148     char* id;
149     int terminal;
150     VsmZoneState state;
151     char *rootfs_path;
152 } VsmZoneStructure;
153
154 /**
155  * Zone information
156  */
157 typedef VsmZoneStructure* VsmZone;
158
159 /**
160  * Netowrk device type
161  */
162 typedef enum {
163     VSMNETDEV_VETH,
164     VSMNETDEV_PHYS,
165     VSMNETDEV_MACVLAN
166 } VsmNetdevType;
167
168 /**
169  * Network device information structure
170  */
171 typedef struct {
172     char* name;
173     VsmNetdevType type;
174 } VsmNetdevStructure;
175
176 /**
177  * Network device information
178  */
179 typedef VsmNetdevStructure* VsmNetdev;
180
181 /**
182  * File type
183  */
184 typedef enum {
185     VSMFILE_DIRECTORY,
186     VSMFILE_FIFO,
187     VSMFILE_REGULAR
188 } VsmFileType;
189
190 /**
191  * Start glib loop.
192  *
193  * Do not call this function if an application creates a glib loop itself.
194  * Otherwise, call it before any other function from this library.
195  *
196  * @return status of this function call
197  */
198 VsmStatus vsm_start_glib_loop();
199
200 /**
201  * Stop glib loop.
202  *
203  * Call only if vsm_start_glib_loop() was called.
204  *
205  * @return status of this function call
206  */
207 VsmStatus vsm_stop_glib_loop();
208
209 /**
210  * Create a new vasum-server's client.
211  *
212  * @return Created client pointer or NULL on failure.
213  */
214 VsmClient vsm_client_create();
215
216 /**
217  * Release client resources.
218  *
219  * @param[in] client vasum-server's client
220  */
221 void vsm_client_free(VsmClient client);
222
223 /**
224  * Get status code of last vasum-server communication.
225  *
226  * @param[in] client vasum-server's client
227  * @return status of this function call
228  */
229 VsmStatus vsm_get_status(VsmClient client);
230
231 /**
232  * Get status message of the last vasum-server communication.
233  *
234  * @param[in] client vasum-server's client
235  * @return last status message from vasum-server communication
236  */
237 const char* vsm_get_status_message(VsmClient client);
238
239 /**
240  * Connect client to the vasum-server.
241  *
242  * @param[in] client vasum-server's client
243  * @return status of this function call
244  */
245 VsmStatus vsm_connect(VsmClient client);
246
247 /**
248  * Connect client to the vasum-server via custom address.
249  *
250  * @param[in] client vasum-server's client
251  * @param[in] address dbus address
252  * @return status of this function call
253  */
254 VsmStatus vsm_connect_custom(VsmClient client, const char* address);
255
256 /**
257  * Release VsmArrayString.
258  *
259  * @param[in] astring VsmArrayString
260  */
261 void vsm_array_string_free(VsmArrayString astring);
262
263 /**
264  * Release VsmString.
265  *
266  * @param string VsmString
267  */
268 void vsm_string_free(VsmString string);
269
270 /**
271  * Release VsmZone
272  *
273  * @param zone VsmZone
274  */
275 void vsm_zone_free(VsmZone zone);
276
277 /**
278  * Release VsmNetdev
279  *
280  * @param netdev VsmNetdev
281  */
282 void vsm_netdev_free(VsmNetdev netdev);
283
284 /**
285  * @name Host API
286  *
287  * Functions using org.tizen.vasum.host.manager D-Bus interface.
288  *
289  * @{
290  */
291
292 /**
293  * Zone's D-Bus state change callback function signature.
294  *
295  * @param[in] zoneId affected zone id
296  * @param[in] dbusAddress new D-Bus address
297  * @param data custom user's data pointer passed to vsm_add_state_callback() function
298  */
299 typedef void (*VsmZoneDbusStateCallback)(const char* zoneId,
300                                              const char* dbusAddress,
301                                              void* data);
302
303 /**
304  * Get dbus address of each zone.
305  *
306  * @param[in] client vasum-server's client
307  * @param[out] keys array of zones name
308  * @param[out] values array of zones dbus address
309  * @return status of this function call
310  * @post keys[i] corresponds to values[i]
311  * @remark Use vsm_array_string_free() to free memory occupied by @p keys and @p values.
312  */
313 VsmStatus vsm_get_zone_dbuses(VsmClient client, VsmArrayString* keys, VsmArrayString* values);
314
315 /**
316  * Get zones name.
317  *
318  * @param[in] client vasum-server's client
319  * @param[out] array array of zones name
320  * @return status of this function call
321  * @remark Use vsm_array_string_free() to free memory occupied by @p array.
322  */
323 VsmStatus vsm_get_zone_ids(VsmClient client, VsmArrayString* array);
324
325 /**
326  * Get active (foreground) zone name.
327  *
328  * @param[in] client vasum-server's client
329  * @param[out] id active zone name
330  * @return status of this function call
331  * @remark Use @p vsm_string_free() to free memory occupied by @p id.
332  */
333 VsmStatus vsm_get_active_zone_id(VsmClient client, VsmString* id);
334
335 /**
336  * Get zone name of process with given pid.
337  *
338  * @param[in] client vasum-server's client
339  * @param[in] pid process id
340  * @param[out] id active zone name
341  * @return status of this function call
342  * @remark Use @p vsm_string_free() to free memory occupied by @p id.
343  */
344 VsmStatus vsm_lookup_zone_by_pid(VsmClient client, int pid, VsmString* id);
345
346 /**
347  * Get zone informations of zone with given id.
348  *
349  * @param[in] client vasum-server's client
350  * @param[in] id zone name
351  * @param[out] zone zone informations
352  * @return status of this function call
353  * @remark Use @p vsm_zone_free() to free memory occupied by @p zone
354  */
355 VsmStatus vsm_lookup_zone_by_id(VsmClient client, const char* id, VsmZone* zone);
356
357 /**
358  * Get zone name with given terminal.
359  *
360  * @param[in] client vasum-server's client
361  * @param[in] terminal terminal id
362  * @param[out] id zone name with given terminal
363  * @return status of this function call
364  * @remark Use @p vsm_string_free() to free memory occupied by @p id.
365  */
366 VsmStatus vsm_lookup_zone_by_terminal_id(VsmClient client, int terminal, VsmString* id);
367
368 /**
369  * Set active (foreground) zone.
370  *
371  * @param[in] client vasum-server's client
372  * @param[in] id zone name
373  * @return status of this function call
374  */
375 VsmStatus vsm_set_active_zone(VsmClient client, const char* id);
376
377 /**
378  * Create and add zone
379  *
380  * @param[in] client vasum-server's client
381  * @param[in] id zone id
382  * @param[in] tname template name, NULL is equivalent to "default"
383  * @return status of this function call
384  */
385 VsmStatus vsm_create_zone(VsmClient client, const char* id, const char* tname);
386
387 /**
388  * Remove zone
389  *
390  * @param[in] client vasum-server's client
391  * @param[in] id zone id
392  * @param[in] force if 0 data will be kept, otherwise data will be lost
393  * @return status of this function call
394  */
395 VsmStatus vsm_destroy_zone(VsmClient client, const char* id, int force);
396
397 /**
398  * Shutdown zone
399  *
400  * @param[in] client vasum-server's client
401  * @param[in] id zone name
402  * @return status of this function call
403  */
404 VsmStatus vsm_shutdown_zone(VsmClient client, const char* id);
405
406 /**
407  * Start zone
408  *
409  * @param[in] client vasum-server's client
410  * @param[in] id zone name
411  * @return status of this function call
412  */
413 VsmStatus vsm_start_zone(VsmClient client, const char* id);
414
415 /**
416  * Lock zone
417  *
418  * @param[in] client vasum-server's client
419  * @param[in] id zone name
420  * @return status of this function call
421  */
422 VsmStatus vsm_lock_zone(VsmClient client, const char* id);
423
424 /**
425  * Unlock zone
426  *
427  * @param[in] client vasum-server's client
428  * @param[in] id zone name
429  * @return status of this function call
430  */
431 VsmStatus vsm_unlock_zone(VsmClient client, const char* id);
432
433 /**
434  * Register dbus state change callback function.
435  *
436  * @note The callback function will be invoked on a different thread.
437  *
438  * @param[in] client vasum-server's client
439  * @param[in] zoneDbusStateCallback callback function
440  * @param[in] data some extra data that will be passed to callback function
441  * @param[out] subscriptionId subscription identifier that can be used to unsubscribe signal,
442  *                      pointer can be NULL.
443  * @return status of this function call
444  */
445 VsmStatus vsm_add_state_callback(VsmClient client,
446                                  VsmZoneDbusStateCallback zoneDbusStateCallback,
447                                  void* data,
448                                  VsmSubscriptionId* subscriptionId);
449
450 /**
451  * Unregister dbus state change callback function.
452  *
453  * @param[in] client vasum-server's client
454  * @param[in] subscriptionId subscription identifier returned by vsm_add_state_callback
455  * @return status of this function call
456  */
457 VsmStatus vsm_del_state_callback(VsmClient client, VsmSubscriptionId subscriptionId);
458
459 /**
460  * Grant access to device
461  *
462  * @param[in] client vasum-server's client
463  * @param[in] zone zone name
464  * @param[in] device device path
465  * @param[in] flags access flags
466  * @return status of this function call
467  */
468 VsmStatus vsm_grant_device(VsmClient client,
469                            const char* zone,
470                            const char* device,
471                            uint32_t flags);
472
473 /**
474  * Revoke access to device
475  *
476  * @param[in] client vasum-server's client
477  * @param[in] zone zone name
478  * @param[in] device device path
479  * @return status of this function call
480  */
481 VsmStatus vsm_revoke_device(VsmClient client, const char* zone, const char* device);
482
483 /**
484  * Get array of netdev from given zone
485  *
486  * @param[in] client vasum-server's client
487  * @param[in] zone zone name
488  * @param[out] netdevIds array of netdev id
489  * @return status of this function call
490  * @remark Use vsm_array_string_free() to free memory occupied by @p netdevIds.
491  */
492 VsmStatus vsm_zone_get_netdevs(VsmClient client, const char* zone, VsmArrayString* netdevIds);
493
494 /**
495  * Get ipv4 address for given netdevId
496  *
497  * @param[in] client vasum-server's client
498  * @param[in] zone zone name
499  * @param[in] netdevId netdev id
500  * @param[out] addr ipv4 address
501  * @return status of this function call
502  */
503 VsmStatus vsm_netdev_get_ipv4_addr(VsmClient client,
504                                    const char* zone,
505                                    const char* netdevId,
506                                    struct in_addr *addr);
507
508 /**
509  * Get ipv6 address for given netdevId
510  *
511  * @param[in] client vasum-server's client
512  * @param[in] zone zone name
513  * @param[in] netdevId netdev id
514  * @param[out] addr ipv6 address
515  * @return status of this function call
516  */
517 VsmStatus vsm_netdev_get_ipv6_addr(VsmClient client,
518                                    const char* zone,
519                                    const char* netdevId,
520                                    struct in6_addr *addr);
521
522 /**
523  * Set ipv4 address for given netdevId
524  *
525  * @param[in] client vasum-server's client
526  * @param[in] zone zone name
527  * @param[in] netdevId netdev id
528  * @param[in] addr ipv4 address
529  * @param[in] prefix bit-length of the network prefix
530  * @return status of this function call
531  */
532 VsmStatus vsm_netdev_set_ipv4_addr(VsmClient client,
533                                    const char* zone,
534                                    const char* netdevId,
535                                    struct in_addr *addr,
536                                    int prefix);
537
538 /**
539  * Set ipv6 address for given netdevId
540  *
541  * @param[in] client vasum-server's client
542  * @param[in] zone zone name
543  * @param[in] netdevId netdev id
544  * @param[in] addr ipv6 address
545  * @param[in] prefix bit-length of the network prefix
546  * @return status of this function call
547  */
548 VsmStatus vsm_netdev_set_ipv6_addr(VsmClient client,
549                                    const char* zone,
550                                    const char* netdevId,
551                                    struct in6_addr *addr,
552                                    int prefix);
553
554 /**
555  * Turn up a network device in the zone
556  *
557  * @param[in] client vasum-server's client
558  * @param[in] zone zone name
559  * @param[in] netdevId netdev id
560  * @return status of this function call
561  */
562 VsmStatus vsm_netdev_up(VsmClient client,
563                         const char* zone,
564                         const char* netdevId);
565
566 /**
567  * Turn down a network device in the zone
568  *
569  * @param[in] client vasum-server's client
570  * @param[in] zone zone name
571  * @param[in] netdevId netdev id
572  * @return status of this function call
573  */
574 VsmStatus vsm_netdev_down(VsmClient client,
575                           const char* zone,
576                           const char* netdevId);
577
578
579 /**
580  * Create veth netdev in zone
581  *
582  * @param[in] client vasum-server's client
583  * @param[in] zone zone name
584  * @param[in] zoneDev in host network device id
585  * @param[in] hostDev in zone network device id
586  * @return status of this function call
587  */
588 VsmStatus vsm_create_netdev_veth(VsmClient client,
589                                  const char* zone,
590                                  const char* zoneDev,
591                                  const char* hostDev);
592 /**
593  * Create macvlab in zone
594  *
595  * @param[in] client vasum-server's client
596  * @param[in] zone zone name
597  * @param[in] zoneDev in host network device id
598  * @param[in] hostDev in zone network device id
599  * @return status of this function call
600  */
601 VsmStatus vsm_create_netdev_macvlan(VsmClient client,
602                                     const char* zone,
603                                     const char* zoneDev,
604                                     const char* hostDev,
605                                     enum macvlan_mode mode);
606 /**
607  * Create/move phys netdev in/to zone
608  *
609  * @param[in] client vasum-server's client
610  * @param[in] zone zone name
611  * @param[in] devId network device id
612  * @return status of this function call
613  */
614 VsmStatus vsm_create_netdev_phys(VsmClient client, const char* zone, const char* devId);
615
616 /**
617  * Get netdev informations
618  *
619  * @param[in] client vasum-server's client
620  * @param[in] zone zone name
621  * @param[in] netdevId network device id
622  * @param[out] netdev netdev informations
623  * @return status of this function call
624  * @remark Use vsm_netdev_free() to free memory occupied by @p netdev.
625  */
626 VsmStatus vsm_lookup_netdev_by_name(VsmClient client,
627                                     const char* zone,
628                                     const char* netdevId,
629                                     VsmNetdev* netdev);
630
631 /**
632  * Remove netdev from zone
633  *
634  * @param[in] client vasum-server's client
635  * @param[in] zone zone name
636  * @param[in] devId network device id
637  * @return status of this function call
638  */
639 VsmStatus vsm_destroy_netdev(VsmClient client, const char* zone, const char* devId);
640
641 /**
642  * Create file, directory or pipe in zone
643  *
644  * Declare file, directory or pipe that will be created while zone startup
645  *
646  * @param[in] client vasum-server's client
647  * @param[in] type file type
648  * @param[in] zone zone id
649  * @param[in] path path to file
650  * @param[in] flags if O_CREAT bit is set then file will be created in zone,
651  *                  otherwise file will by copied from host;
652  *                  it is meaningful only when O_CREAT is set
653  * @param[in] mode mode of file
654  * @return status of this function call
655  */
656 VsmStatus vsm_declare_file(VsmClient client,
657                            const char* zone,
658                            VsmFileType type,
659                            const char* path,
660                            int32_t flags,
661                            mode_t mode);
662
663 /**
664  * Create mount point in zone
665  *
666  * Declare mount that will be created while zone startup
667  * Parameters are passed to mount system function
668  *
669  * @param[in] client vasum-server's client
670  * @param[in] source device path (path in host)
671  * @param[in] zone zone id
672  * @param[in] target mount point (path in zone)
673  * @param[in] type filesystem type
674  * @param[in] flags mount flags as in mount function
675  * @param[in] data additional data as in mount function
676  * @return status of this function call
677  */
678 VsmStatus vsm_declare_mount(VsmClient client,
679                             const char* source,
680                             const char* zone,
681                             const char* target,
682                             const char* type,
683                             uint64_t flags,
684                             const char* data);
685
686 /**
687  * Create link in zone
688  *
689  * Declare link that will be created while zone startup
690  * Parameters are passed to link system function
691  *
692  * @param[in] client vasum-server's client
693  * @param[in] source path to link source (in host)
694  * @param[in] zone zone id
695  * @param[in] target path to link name (in zone)
696  * @return status of this function call
697  */
698 VsmStatus vsm_declare_link(VsmClient client,
699                            const char *source,
700                            const char* zone,
701                            const char *target);
702
703 /**
704  * Get all declarations
705  *
706  * Gets all declarations of resourcies
707  * (@see ::vsm_declare_link, @see ::vsm_declare_mount, @see ::vsm_declare_linki)
708  *
709  * @param[in] client vasum-server's client
710  * @param[in] zone zone id
711  * @param[out] declarations array of declarations id
712  * @return status of this function call
713  */
714 VsmStatus vsm_list_declarations(VsmClient client,
715                                 const char* zone,
716                                 VsmArrayString* declarations);
717
718 /**
719  * Remove declaration
720  *
721  * Removes given declaration by its id (@see ::vsm_list_declarations)
722  *
723  * @param[in] client vasum-server's client
724  * @param[in] zone zone id
725  * @param[in] declaration declaration id
726  * @return status of this function call
727  */
728 VsmStatus vsm_remove_declaration(VsmClient client,
729                                  const char* zone,
730                                  VsmString declaration);
731
732
733 /** @} Host API */
734
735
736 /**
737  * @name Zone API
738  *
739  * Functions using org.tizen.vasum.zone.manager D-Bus interface.
740  *
741  * @{
742  */
743
744 /**
745  * Notification callback function signature.
746  *
747  * @param[in] zone source zone
748  * @param[in] application sending application name
749  * @param[in] message notification message
750  * @param data custom user's data pointer passed to vsm_add_notification_callback()
751  */
752 typedef void (*VsmNotificationCallback)(const char* zone,
753                                        const char* application,
754                                        const char* message,
755                                        void* data);
756 /**
757  * Send message to active zone.
758  *
759  * @param[in] client vasum-server's client
760  * @param[in] application application name
761  * @param[in] message message
762  * @return status of this function call
763  */
764 VsmStatus vsm_notify_active_zone(VsmClient client, const char* application, const char* message);
765
766 /**
767  * Move file between zones.
768  *
769  * @param[in] client vasum-server's client
770  * @param[in] destZone destination zone id
771  * @param[in] path path to moved file
772  * @return status of this function call
773  */
774 VsmStatus vsm_file_move_request(VsmClient client, const char* destZone, const char* path);
775
776 /**
777  * Register notification callback function.
778  *
779  * @note The callback function will be invoked on a different thread.
780  *
781  * @param[in] client vasum-server's client
782  * @param[in] notificationCallback callback function
783  * @param[in] data some extra data that will be passed to callback function
784  * @param[out] subscriptionId subscription identifier that can be used to unsubscribe signal,
785  *                      pointer can be NULL.
786  * @return status of this function call
787  */
788 VsmStatus vsm_add_notification_callback(VsmClient client,
789                                         VsmNotificationCallback notificationCallback,
790                                         void* data,
791                                         VsmSubscriptionId* subscriptionId);
792
793 /**
794  * Unregister notification callback function.
795  *
796  * @param[in] client vasum-server's client
797  * @param[in] subscriptionId subscription identifier returned by vsm_add_notification_callback
798  * @return status of this function call
799  */
800 VsmStatus vsm_del_notification_callback(VsmClient client, VsmSubscriptionId subscriptionId);
801
802 /** @} Zone API */
803
804 #ifdef __cplusplus
805 }
806 #endif
807
808 #endif /* VASUM_CLIENT_H */