source: mainline/uspace/lib/inet/src/iplink.c

Last change on this file was edeee9f, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Fix header guards and doxy groups of stuff moved out of libc

  • Property mode set to 100644
File size: 6.7 KB
Line 
1/*
2 * Copyright (c) 2021 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/** @addtogroup libinet
30 * @{
31 */
32/**
33 * @file
34 * @brief IP link client stub
35 */
36
37#include <async.h>
38#include <assert.h>
39#include <errno.h>
40#include <inet/addr.h>
41#include <inet/eth_addr.h>
42#include <inet/iplink.h>
43#include <ipc/iplink.h>
44#include <ipc/services.h>
45#include <loc.h>
46#include <stdlib.h>
47
48static void iplink_cb_conn(ipc_call_t *icall, void *arg);
49
50errno_t iplink_open(async_sess_t *sess, iplink_ev_ops_t *ev_ops, void *arg,
51 iplink_t **riplink)
52{
53 iplink_t *iplink = calloc(1, sizeof(iplink_t));
54 if (iplink == NULL)
55 return ENOMEM;
56
57 iplink->sess = sess;
58 iplink->ev_ops = ev_ops;
59 iplink->arg = arg;
60
61 async_exch_t *exch = async_exchange_begin(sess);
62
63 port_id_t port;
64 errno_t rc = async_create_callback_port(exch, INTERFACE_IPLINK_CB, 0, 0,
65 iplink_cb_conn, iplink, &port);
66
67 async_exchange_end(exch);
68
69 if (rc != EOK)
70 goto error;
71
72 *riplink = iplink;
73 return EOK;
74
75error:
76 if (iplink != NULL)
77 free(iplink);
78
79 return rc;
80}
81
82void iplink_close(iplink_t *iplink)
83{
84 /* XXX Synchronize with iplink_cb_conn */
85 free(iplink);
86}
87
88errno_t iplink_send(iplink_t *iplink, iplink_sdu_t *sdu)
89{
90 async_exch_t *exch = async_exchange_begin(iplink->sess);
91
92 ipc_call_t answer;
93 aid_t req = async_send_2(exch, IPLINK_SEND, (sysarg_t) sdu->src,
94 (sysarg_t) sdu->dest, &answer);
95
96 errno_t rc = async_data_write_start(exch, sdu->data, sdu->size);
97
98 async_exchange_end(exch);
99
100 if (rc != EOK) {
101 async_forget(req);
102 return rc;
103 }
104
105 errno_t retval;
106 async_wait_for(req, &retval);
107
108 return retval;
109}
110
111errno_t iplink_send6(iplink_t *iplink, iplink_sdu6_t *sdu)
112{
113 async_exch_t *exch = async_exchange_begin(iplink->sess);
114
115 ipc_call_t answer;
116 aid_t req = async_send_0(exch, IPLINK_SEND6, &answer);
117
118 errno_t rc = async_data_write_start(exch, &sdu->dest, sizeof(eth_addr_t));
119 if (rc != EOK) {
120 async_exchange_end(exch);
121 async_forget(req);
122 return rc;
123 }
124
125 rc = async_data_write_start(exch, sdu->data, sdu->size);
126
127 async_exchange_end(exch);
128
129 if (rc != EOK) {
130 async_forget(req);
131 return rc;
132 }
133
134 errno_t retval;
135 async_wait_for(req, &retval);
136
137 return retval;
138}
139
140errno_t iplink_get_mtu(iplink_t *iplink, size_t *rmtu)
141{
142 async_exch_t *exch = async_exchange_begin(iplink->sess);
143
144 sysarg_t mtu;
145 errno_t rc = async_req_0_1(exch, IPLINK_GET_MTU, &mtu);
146
147 async_exchange_end(exch);
148
149 if (rc != EOK)
150 return rc;
151
152 *rmtu = mtu;
153 return EOK;
154}
155
156errno_t iplink_get_mac48(iplink_t *iplink, eth_addr_t *mac)
157{
158 async_exch_t *exch = async_exchange_begin(iplink->sess);
159
160 ipc_call_t answer;
161 aid_t req = async_send_0(exch, IPLINK_GET_MAC48, &answer);
162
163 errno_t rc = async_data_read_start(exch, mac, sizeof(eth_addr_t));
164
165 loc_exchange_end(exch);
166
167 if (rc != EOK) {
168 async_forget(req);
169 return rc;
170 }
171
172 errno_t retval;
173 async_wait_for(req, &retval);
174
175 return retval;
176}
177
178errno_t iplink_set_mac48(iplink_t *iplink, eth_addr_t *mac)
179{
180 async_exch_t *exch = async_exchange_begin(iplink->sess);
181
182 ipc_call_t answer;
183 aid_t req = async_send_0(exch, IPLINK_GET_MAC48, &answer);
184
185 errno_t rc = async_data_read_start(exch, mac, sizeof(eth_addr_t));
186
187 loc_exchange_end(exch);
188
189 if (rc != EOK) {
190 async_forget(req);
191 return rc;
192 }
193
194 errno_t retval;
195 async_wait_for(req, &retval);
196
197 return retval;
198}
199
200errno_t iplink_addr_add(iplink_t *iplink, inet_addr_t *addr)
201{
202 async_exch_t *exch = async_exchange_begin(iplink->sess);
203
204 ipc_call_t answer;
205 aid_t req = async_send_0(exch, IPLINK_ADDR_ADD, &answer);
206
207 errno_t rc = async_data_write_start(exch, addr, sizeof(inet_addr_t));
208 async_exchange_end(exch);
209
210 if (rc != EOK) {
211 async_forget(req);
212 return rc;
213 }
214
215 errno_t retval;
216 async_wait_for(req, &retval);
217
218 return retval;
219}
220
221errno_t iplink_addr_remove(iplink_t *iplink, inet_addr_t *addr)
222{
223 async_exch_t *exch = async_exchange_begin(iplink->sess);
224
225 ipc_call_t answer;
226 aid_t req = async_send_0(exch, IPLINK_ADDR_REMOVE, &answer);
227
228 errno_t rc = async_data_write_start(exch, addr, sizeof(inet_addr_t));
229 async_exchange_end(exch);
230
231 if (rc != EOK) {
232 async_forget(req);
233 return rc;
234 }
235
236 errno_t retval;
237 async_wait_for(req, &retval);
238
239 return retval;
240}
241
242void *iplink_get_userptr(iplink_t *iplink)
243{
244 return iplink->arg;
245}
246
247static void iplink_ev_recv(iplink_t *iplink, ipc_call_t *icall)
248{
249 iplink_recv_sdu_t sdu;
250
251 ip_ver_t ver = ipc_get_arg1(icall);
252
253 errno_t rc = async_data_write_accept(&sdu.data, false, 0, 0, 0,
254 &sdu.size);
255 if (rc != EOK) {
256 async_answer_0(icall, rc);
257 return;
258 }
259
260 rc = iplink->ev_ops->recv(iplink, &sdu, ver);
261 free(sdu.data);
262 async_answer_0(icall, rc);
263}
264
265static void iplink_ev_change_addr(iplink_t *iplink, ipc_call_t *icall)
266{
267 eth_addr_t *addr;
268 size_t size;
269
270 errno_t rc = async_data_write_accept((void **) &addr, false,
271 sizeof(eth_addr_t), sizeof(eth_addr_t), 0, &size);
272 if (rc != EOK) {
273 async_answer_0(icall, rc);
274 return;
275 }
276
277 rc = iplink->ev_ops->change_addr(iplink, addr);
278 free(addr);
279 async_answer_0(icall, EOK);
280}
281
282static void iplink_cb_conn(ipc_call_t *icall, void *arg)
283{
284 iplink_t *iplink = (iplink_t *) arg;
285
286 while (true) {
287 ipc_call_t call;
288 async_get_call(&call);
289
290 if (!ipc_get_imethod(&call)) {
291 async_answer_0(&call, EOK);
292 return;
293 }
294
295 switch (ipc_get_imethod(&call)) {
296 case IPLINK_EV_RECV:
297 iplink_ev_recv(iplink, &call);
298 break;
299 case IPLINK_EV_CHANGE_ADDR:
300 iplink_ev_change_addr(iplink, &call);
301 break;
302 default:
303 async_answer_0(&call, ENOTSUP);
304 }
305 }
306}
307
308/** @}
309 */
Note: See TracBrowser for help on using the repository browser.