1 | /*
|
---|
2 | * Copyright (c) 2012 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <async.h>
|
---|
30 | #include <assert.h>
|
---|
31 | #include <errno.h>
|
---|
32 | #include <inet/inetcfg.h>
|
---|
33 | #include <ipc/inet.h>
|
---|
34 | #include <ipc/services.h>
|
---|
35 | #include <loc.h>
|
---|
36 | #include <stdlib.h>
|
---|
37 |
|
---|
38 | static async_sess_t *inetcfg_sess = NULL;
|
---|
39 |
|
---|
40 | static int inetcfg_get_ids_once(sysarg_t method, sysarg_t arg1,
|
---|
41 | sysarg_t *id_buf, size_t buf_size, size_t *act_size)
|
---|
42 | {
|
---|
43 | async_exch_t *exch = async_exchange_begin(inetcfg_sess);
|
---|
44 |
|
---|
45 | ipc_call_t answer;
|
---|
46 | aid_t req = async_send_1(exch, method, arg1, &answer);
|
---|
47 | int rc = async_data_read_start(exch, id_buf, buf_size);
|
---|
48 |
|
---|
49 | async_exchange_end(exch);
|
---|
50 |
|
---|
51 | if (rc != EOK) {
|
---|
52 | async_wait_for(req, NULL);
|
---|
53 | return rc;
|
---|
54 | }
|
---|
55 |
|
---|
56 | sysarg_t retval;
|
---|
57 | async_wait_for(req, &retval);
|
---|
58 |
|
---|
59 | if (retval != EOK) {
|
---|
60 | return retval;
|
---|
61 | }
|
---|
62 |
|
---|
63 | *act_size = IPC_GET_ARG1(answer);
|
---|
64 | return EOK;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /** Get list of IDs.
|
---|
68 | *
|
---|
69 | * Returns an allocated array of service IDs.
|
---|
70 | *
|
---|
71 | * @param method IPC method
|
---|
72 | * @param arg1 IPC argument 1
|
---|
73 | * @param data Place to store pointer to array of IDs
|
---|
74 | * @param count Place to store number of IDs
|
---|
75 | * @return EOK on success or negative error code
|
---|
76 | */
|
---|
77 | static int inetcfg_get_ids_internal(sysarg_t method, sysarg_t arg1,
|
---|
78 | sysarg_t **data, size_t *count)
|
---|
79 | {
|
---|
80 | *data = NULL;
|
---|
81 | *count = 0;
|
---|
82 |
|
---|
83 | size_t act_size = 0;
|
---|
84 | int rc = inetcfg_get_ids_once(method, arg1, NULL, 0,
|
---|
85 | &act_size);
|
---|
86 | if (rc != EOK)
|
---|
87 | return rc;
|
---|
88 |
|
---|
89 | size_t alloc_size = act_size;
|
---|
90 | service_id_t *ids = malloc(alloc_size);
|
---|
91 | if (ids == NULL)
|
---|
92 | return ENOMEM;
|
---|
93 |
|
---|
94 | while (true) {
|
---|
95 | rc = inetcfg_get_ids_once(method, arg1, ids, alloc_size,
|
---|
96 | &act_size);
|
---|
97 | if (rc != EOK)
|
---|
98 | return rc;
|
---|
99 |
|
---|
100 | if (act_size <= alloc_size)
|
---|
101 | break;
|
---|
102 |
|
---|
103 | alloc_size = act_size;
|
---|
104 | ids = realloc(ids, alloc_size);
|
---|
105 | if (ids == NULL)
|
---|
106 | return ENOMEM;
|
---|
107 | }
|
---|
108 |
|
---|
109 | *count = act_size / sizeof(sysarg_t);
|
---|
110 | *data = ids;
|
---|
111 | return EOK;
|
---|
112 | }
|
---|
113 |
|
---|
114 | int inetcfg_init(void)
|
---|
115 | {
|
---|
116 | service_id_t inet_svc;
|
---|
117 | int rc;
|
---|
118 |
|
---|
119 | assert(inetcfg_sess == NULL);
|
---|
120 |
|
---|
121 | rc = loc_service_get_id(SERVICE_NAME_INETCFG, &inet_svc,
|
---|
122 | IPC_FLAG_BLOCKING);
|
---|
123 | if (rc != EOK)
|
---|
124 | return ENOENT;
|
---|
125 |
|
---|
126 | inetcfg_sess = loc_service_connect(EXCHANGE_SERIALIZE, inet_svc,
|
---|
127 | IPC_FLAG_BLOCKING);
|
---|
128 | if (inetcfg_sess == NULL)
|
---|
129 | return ENOENT;
|
---|
130 |
|
---|
131 | return EOK;
|
---|
132 | }
|
---|
133 |
|
---|
134 | int inetcfg_addr_create_static(const char *name, inet_naddr_t *naddr,
|
---|
135 | sysarg_t link_id, sysarg_t *addr_id)
|
---|
136 | {
|
---|
137 | async_exch_t *exch = async_exchange_begin(inetcfg_sess);
|
---|
138 |
|
---|
139 | int rc = async_req_3_1(exch, INETCFG_ADDR_CREATE_STATIC, naddr->ipv4,
|
---|
140 | naddr->bits, link_id, addr_id);
|
---|
141 | async_exchange_end(exch);
|
---|
142 |
|
---|
143 | return rc;
|
---|
144 | }
|
---|
145 |
|
---|
146 | int inetcfg_addr_delete(sysarg_t addr_id)
|
---|
147 | {
|
---|
148 | async_exch_t *exch = async_exchange_begin(inetcfg_sess);
|
---|
149 |
|
---|
150 | int rc = async_req_1_0(exch, INETCFG_ADDR_DELETE, addr_id);
|
---|
151 | async_exchange_end(exch);
|
---|
152 |
|
---|
153 | return rc;
|
---|
154 | }
|
---|
155 |
|
---|
156 | int inetcfg_addr_get(sysarg_t addr_id, inet_addr_info_t *ainfo)
|
---|
157 | {
|
---|
158 | sysarg_t ipv4, bits;
|
---|
159 | async_exch_t *exch = async_exchange_begin(inetcfg_sess);
|
---|
160 |
|
---|
161 | int rc = async_req_1_2(exch, INETCFG_ADDR_GET, addr_id,
|
---|
162 | &ipv4, &bits);
|
---|
163 | async_exchange_end(exch);
|
---|
164 |
|
---|
165 | if (rc != EOK)
|
---|
166 | return rc;
|
---|
167 |
|
---|
168 | ainfo->naddr.ipv4 = ipv4;
|
---|
169 | ainfo->naddr.bits = bits;
|
---|
170 | return EOK;
|
---|
171 | }
|
---|
172 |
|
---|
173 | int inetcfg_get_addr_list(sysarg_t **addrs, size_t *count)
|
---|
174 | {
|
---|
175 | return inetcfg_get_ids_internal(INETCFG_GET_ADDR_LIST,
|
---|
176 | 0, addrs, count);
|
---|
177 | }
|
---|
178 |
|
---|
179 | int inetcfg_get_link_list(sysarg_t **links, size_t *count)
|
---|
180 | {
|
---|
181 | return inetcfg_get_ids_internal(INETCFG_GET_LINK_LIST,
|
---|
182 | 0, links, count);
|
---|
183 | }
|
---|
184 |
|
---|
185 | int inetcfg_link_get(sysarg_t link_id, inet_link_info_t *linfo)
|
---|
186 | {
|
---|
187 | async_exch_t *exch = async_exchange_begin(inetcfg_sess);
|
---|
188 |
|
---|
189 | int rc = async_req_1_0(exch, INETCFG_LINK_GET, link_id);
|
---|
190 | async_exchange_end(exch);
|
---|
191 |
|
---|
192 | if (rc != EOK)
|
---|
193 | return rc;
|
---|
194 |
|
---|
195 | linfo->dummy = 0;
|
---|
196 | return EOK;
|
---|
197 | }
|
---|
198 |
|
---|
199 | /** @}
|
---|
200 | */
|
---|