source: mainline/uspace/lib/c/generic/iplink.c@ 0dd16778

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0dd16778 was 8d48c7e, checked in by Jiri Svoboda <jiri@…>, 10 years ago

Find the association to deliver the datagram using amap.

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