1/*
2 * Copyright 2016, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
8 * See "LICENSE_BSD2.txt" for details.
9 *
10 * @TAG(D61_BSD)
11 */
12
13#ifndef _RPC_INTERFACE_NAME_CLIENT_HELPER_H_
14#define _RPC_INTERFACE_NAME_CLIENT_HELPER_H_
15
16#include <stdio.h>
17#include <stdlib.h>
18#include <stdbool.h>
19#include <string.h>
20
21#include <refos-rpc/name_client.h>
22#include <refos-rpc/rpc.h>
23#include <refos/error.h>
24#include <refos/refos.h>
25
26/*! @file
27    @brief Helper functions for the name server interface.
28
29    This file contains a simple layer of helper functions that make using the nameserv interface
30    much easier, but are too complex to have been generated by the stub generator. RefOS uses a
31    simple hierachical distributed naming scheme where each nameserver may resolves a certain
32    prefix of a path.
33*/
34
35#define NAMESERV_RESOLVED -1
36#define NAMESERV_PATH_MAXLEN 512
37
38/*! @brief Struct containing a mountpoint, which is a completely resolved namespace path. */
39typedef struct nsv_mountpoint {
40    bool success;
41    seL4_CPtr serverAnon; /* Has ownership. */
42    char dspaceName[NAMESERV_PATH_MAXLEN];
43
44    seL4_CPtr nameservRoot; /* No ownership. */
45    char nameservPathPrefix[NAMESERV_PATH_MAXLEN];
46} nsv_mountpoint_t;
47
48/*! @brief Helper function for nsv_resolve_segment_internal() which resolves a single segment.
49           You probably don't want to call this directly; use the nsv_resolve() helper function.
50    @param nameserv The name server to resolve with.
51    @param path The path to resolve.
52    @param resolvedBytes Output containing number of bytes resolved.
53    @return 0 on error, anon capability to next name server on success.
54*/
55static inline seL4_CPtr
56nsv_resolve_segment(seL4_CPtr nameserv, char* path, int* resolvedBytes)
57{
58    refos_err_t errnoRetVal = EINVALID;
59    int tempResolvedBytes = 0;
60
61    seL4_CPtr tcap = nsv_resolve_segment_internal(nameserv, path, &tempResolvedBytes, &errnoRetVal);
62    if (errnoRetVal != ESUCCESS) {
63        REFOS_SET_ERRNO(errnoRetVal);
64        return 0;
65    }
66
67    REFOS_SET_ERRNO(ESUCCESS);
68    if (resolvedBytes) {
69        (*resolvedBytes) = tempResolvedBytes;
70    }
71    return tcap;
72}
73
74/*! @brief Resolve a path completely.
75
76    This function will completely resolve the given path down to the server that actually
77    contains the dataspace. It will search through the namespace hierachy until the leaf node.
78
79    @param path String containing the path to resolve.
80    @return A mountpoint info structure containing the results of the resolve; look in the
81            success flag of the struct to check for any errors.
82*/
83nsv_mountpoint_t nsv_resolve(char* path);
84
85/*! @brief Release a mount point structure.
86    @param m The mountpoint info structure to release. Does NOT actually free the given structure,
87             only releases the associated resources. (Takes ownership)
88*/
89void nsv_mountpoint_release(nsv_mountpoint_t *m);
90
91#endif /* _RPC_INTERFACE_NAME_CLIENT_HELPER_H_ */
92