Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / native_client / src / trusted / service_runtime / name_service / name_service.h
1 /*
2  * Copyright (c) 2011 The Native Client Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6
7 #ifndef NATIVE_CLIENT_SRC_TRUSTED_SERVICE_RUNTIME_NAME_SERVICE_NAME_SERVICE_H_
8 #define NATIVE_CLIENT_SRC_TRUSTED_SERVICE_RUNTIME_NAME_SERVICE_NAME_SERVICE_H_
9
10 #include "native_client/src/include/nacl_base.h"
11 #include "native_client/src/include/portability.h"
12
13 /* Get NACL_NAME_SERVICE_CONNECTION_MAX. */
14 #include "native_client/src/public/name_service.h"
15
16 #include "native_client/src/trusted/desc/nacl_desc_base.h"
17
18 #include "native_client/src/trusted/simple_service/nacl_simple_service.h"
19 #include "native_client/src/trusted/simple_service/nacl_simple_ltd_service.h"
20
21 #include "native_client/src/trusted/threading/nacl_thread_interface.h"
22
23 EXTERN_C_BEGIN
24
25 /*
26  * NaClStartNameService should only be called once, and also serves
27  * as a module initializer.
28  *
29  * This function creates a bound socket / socket address pair, and
30  * spawns a thread that accepts connections on the bound socket and
31  * services requests on the resultant connections.  A maximum number
32  * of connections is defined, since well-behaving programs should not
33  * need more than one (absent excessive opaque layering/abstractions)
34  * name service lookup connection.
35  *
36  * The socket address NaClDescConnCap is returned, to be entered into
37  * the NaClApp's open file table at a well-known location.
38  */
39
40 struct NaClNameServiceEntry;  /* fwd */
41
42 struct NaClNameService {
43   struct NaClSimpleLtdService base NACL_IS_REFCOUNT_SUBCLASS;
44
45   struct NaClMutex            mu;
46   /*
47    * |mu| protects the service entries hanging off of |head|.
48    */
49   struct NaClNameServiceEntry *head;
50 };
51
52 int NaClNameServiceCtor(struct NaClNameService      *self,
53                         NaClThreadIfFactoryFunction thread_factory_fn,
54                         void                        *thread_factory_data);
55
56 int NaClNameServiceCreateDescEntry(
57     struct NaClNameService  *self,
58     char const              *name,
59     int                     mode,
60     struct NaClDesc         *new_desc);  /* takes ownership of ref */
61
62 typedef int (*NaClNameServiceFactoryFn_t)(void             *factory_state,
63                                           char const       *name,
64                                           int              flags,
65                                           struct NaClDesc  **out);
66
67 int NaClNameServiceCreateFactoryEntry(
68     struct NaClNameService      *self,
69     char const                  *name,
70     /*
71      * If a |name| corresponding to a factory is overwritten,
72      * references to the factory and its state is just dropped/lost
73      * after invoking the factory with a NULL for the |out| argument;
74      * the factory function should decrement reference counts as
75      * appropriate and clean up when this occurs.
76      */
77     NaClNameServiceFactoryFn_t  factory_fn,
78     void                        *factory_state);
79
80 int NaClNameServiceResolveName(
81     struct NaClNameService  *self,
82     char const              *name,
83     int                     flags,
84     struct NaClDesc         **out);
85
86 int NaClNameServiceDeleteName(struct NaClNameService  *nnsp,
87                               char const              *name);
88
89 size_t NaClNameServiceEnumerate(struct NaClNameService  *nnsp,
90                                 char                    *dest,
91                                 size_t                  nbytes);
92
93 struct NaClNameServiceVtbl {
94   struct NaClSimpleServiceVtbl  vbase;
95   /*
96    * The following functions return 0 for success, and non-zero to
97    * indicate the reason for failure.  The set of possible failures is
98    * in src/public/name_service.h.
99    *
100    * They don't really have to be virtual, but this makes it easier to
101    * subclass and modify later, should the need occur.
102    */
103   int                           (*CreateDescEntry)(
104       struct NaClNameService  *self,
105       char const              *name,
106       int                     mode,
107       struct NaClDesc         *new_desc);  /* takes ownership of ref */
108   int                           (*CreateFactoryEntry)(
109       struct NaClNameService      *self,
110       char const                  *name,
111       NaClNameServiceFactoryFn_t  factory_fn,
112       void                        *factory_state);
113   int                           (*ResolveName)(
114       struct NaClNameService  *self,
115       char const              *name,
116       int                     flags,
117       struct NaClDesc         **out);
118   int                           (*DeleteName)(
119       struct NaClNameService  *self,
120       char const              *name);
121 };
122
123 extern struct NaClNameServiceVtbl kNaClNameServiceVtbl;
124
125 void NaClNameServiceLaunch(struct NaClNameService *self);
126
127 EXTERN_C_END
128
129 #endif
130 /* NATIVE_CLIENT_SRC_TRUSTED_SERVICE_RUNTIME_NAME_SERVICE_NAME_SERVICE_H_ */