source: mainline/uspace/lib/c/generic/iplink_srv.c@ d73d992

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d73d992 was a46e56b, checked in by Jakub Jermar <jakub@…>, 7 years ago

Prefer handle over ID in naming handle variables

  • Property mode set to 100644
File size: 8.4 KB
RevLine 
[f834f90d]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 server stub
35 */
[02a09ed]36
[f834f90d]37#include <errno.h>
38#include <ipc/iplink.h>
39#include <stdlib.h>
[8d2dd7f2]40#include <stddef.h>
[02a09ed]41#include <inet/addr.h>
[f834f90d]42#include <inet/iplink_srv.h>
43
[a46e56b]44static void iplink_get_mtu_srv(iplink_srv_t *srv, cap_call_handle_t chandle,
[f834f90d]45 ipc_call_t *call)
46{
47 size_t mtu;
[b7fd2a0]48 errno_t rc = srv->ops->get_mtu(srv, &mtu);
[a46e56b]49 async_answer_1(chandle, rc, mtu);
[f834f90d]50}
51
[a46e56b]52static void iplink_get_mac48_srv(iplink_srv_t *srv, cap_call_handle_t icall_handle,
[a17356fd]53 ipc_call_t *icall)
54{
55 addr48_t mac;
[b7fd2a0]56 errno_t rc = srv->ops->get_mac48(srv, &mac);
[a17356fd]57 if (rc != EOK) {
[a46e56b]58 async_answer_0(icall_handle, rc);
[a17356fd]59 return;
60 }
[a35b458]61
[a46e56b]62 cap_call_handle_t chandle;
[a17356fd]63 size_t size;
[a46e56b]64 if (!async_data_read_receive(&chandle, &size)) {
65 async_answer_0(chandle, EREFUSED);
66 async_answer_0(icall_handle, EREFUSED);
[a17356fd]67 return;
68 }
[a35b458]69
[a17356fd]70 if (size != sizeof(addr48_t)) {
[a46e56b]71 async_answer_0(chandle, EINVAL);
72 async_answer_0(icall_handle, EINVAL);
[a17356fd]73 return;
74 }
[a35b458]75
[a46e56b]76 rc = async_data_read_finalize(chandle, &mac, size);
[a17356fd]77 if (rc != EOK)
[a46e56b]78 async_answer_0(chandle, rc);
[a35b458]79
[a46e56b]80 async_answer_0(icall_handle, rc);
[a17356fd]81}
82
[a46e56b]83static void iplink_set_mac48_srv(iplink_srv_t *srv, cap_call_handle_t icall_handle,
[c3b25985]84 ipc_call_t *icall)
85{
[b7fd2a0]86 errno_t rc;
[c3b25985]87 size_t size;
88 addr48_t mac;
[a46e56b]89 cap_call_handle_t chandle;
[c3b25985]90
[a46e56b]91 if (!async_data_write_receive(&chandle, &size)) {
92 async_answer_0(chandle, EREFUSED);
93 async_answer_0(icall_handle, EREFUSED);
[c3b25985]94 }
95
96 rc = srv->ops->set_mac48(srv, &mac);
97 if (rc != EOK) {
[a46e56b]98 async_answer_0(icall_handle, rc);
[c3b25985]99 return;
100 }
[a35b458]101
[a46e56b]102 rc = async_data_read_finalize(chandle, &mac, sizeof(addr48_t));
[c3b25985]103 if (rc != EOK)
[a46e56b]104 async_answer_0(chandle, rc);
[a35b458]105
[a46e56b]106 async_answer_0(icall_handle, rc);
[c3b25985]107}
108
[a46e56b]109static void iplink_addr_add_srv(iplink_srv_t *srv, cap_call_handle_t icall_handle,
[02a09ed]110 ipc_call_t *icall)
[962f03b]111{
[a46e56b]112 cap_call_handle_t chandle;
[02a09ed]113 size_t size;
[a46e56b]114 if (!async_data_write_receive(&chandle, &size)) {
115 async_answer_0(chandle, EREFUSED);
116 async_answer_0(icall_handle, EREFUSED);
[02a09ed]117 return;
118 }
[a35b458]119
[02a09ed]120 if (size != sizeof(inet_addr_t)) {
[a46e56b]121 async_answer_0(chandle, EINVAL);
122 async_answer_0(icall_handle, EINVAL);
[02a09ed]123 return;
124 }
[a35b458]125
[02a09ed]126 inet_addr_t addr;
[a46e56b]127 errno_t rc = async_data_write_finalize(chandle, &addr, size);
[02a09ed]128 if (rc != EOK) {
[a46e56b]129 async_answer_0(chandle, rc);
130 async_answer_0(icall_handle, rc);
[02a09ed]131 }
[a35b458]132
[02a09ed]133 rc = srv->ops->addr_add(srv, &addr);
[a46e56b]134 async_answer_0(icall_handle, rc);
[962f03b]135}
136
[a46e56b]137static void iplink_addr_remove_srv(iplink_srv_t *srv, cap_call_handle_t icall_handle,
[02a09ed]138 ipc_call_t *icall)
[962f03b]139{
[a46e56b]140 cap_call_handle_t chandle;
[02a09ed]141 size_t size;
[a46e56b]142 if (!async_data_write_receive(&chandle, &size)) {
143 async_answer_0(chandle, EREFUSED);
144 async_answer_0(icall_handle, EREFUSED);
[02a09ed]145 return;
146 }
[a35b458]147
[02a09ed]148 if (size != sizeof(inet_addr_t)) {
[a46e56b]149 async_answer_0(chandle, EINVAL);
150 async_answer_0(icall_handle, EINVAL);
[02a09ed]151 return;
152 }
[a35b458]153
[02a09ed]154 inet_addr_t addr;
[a46e56b]155 errno_t rc = async_data_write_finalize(chandle, &addr, size);
[02a09ed]156 if (rc != EOK) {
[a46e56b]157 async_answer_0(chandle, rc);
158 async_answer_0(icall_handle, rc);
[02a09ed]159 }
[a35b458]160
[02a09ed]161 rc = srv->ops->addr_remove(srv, &addr);
[a46e56b]162 async_answer_0(icall_handle, rc);
[962f03b]163}
164
[a46e56b]165static void iplink_send_srv(iplink_srv_t *srv, cap_call_handle_t icall_handle,
[02a09ed]166 ipc_call_t *icall)
[f834f90d]167{
[02a09ed]168 iplink_sdu_t sdu;
[a35b458]169
[a17356fd]170 sdu.src = IPC_GET_ARG1(*icall);
171 sdu.dest = IPC_GET_ARG2(*icall);
[a35b458]172
[b7fd2a0]173 errno_t rc = async_data_write_accept(&sdu.data, false, 0, 0, 0,
[a17356fd]174 &sdu.size);
175 if (rc != EOK) {
[a46e56b]176 async_answer_0(icall_handle, rc);
[02a09ed]177 return;
178 }
[a35b458]179
[a17356fd]180 rc = srv->ops->send(srv, &sdu);
181 free(sdu.data);
[a46e56b]182 async_answer_0(icall_handle, rc);
[a17356fd]183}
184
[a46e56b]185static void iplink_send6_srv(iplink_srv_t *srv, cap_call_handle_t icall_handle,
[a17356fd]186 ipc_call_t *icall)
187{
188 iplink_sdu6_t sdu;
[a35b458]189
[a46e56b]190 cap_call_handle_t chandle;
[a17356fd]191 size_t size;
[a46e56b]192 if (!async_data_write_receive(&chandle, &size)) {
193 async_answer_0(chandle, EREFUSED);
194 async_answer_0(icall_handle, EREFUSED);
[f834f90d]195 return;
196 }
[a35b458]197
[a17356fd]198 if (size != sizeof(addr48_t)) {
[a46e56b]199 async_answer_0(chandle, EINVAL);
200 async_answer_0(icall_handle, EINVAL);
[02a09ed]201 return;
202 }
[a35b458]203
[a46e56b]204 errno_t rc = async_data_write_finalize(chandle, &sdu.dest, size);
[02a09ed]205 if (rc != EOK) {
[a46e56b]206 async_answer_0(chandle, rc);
207 async_answer_0(icall_handle, rc);
[02a09ed]208 }
[a35b458]209
[02a09ed]210 rc = async_data_write_accept(&sdu.data, false, 0, 0, 0,
211 &sdu.size);
[a17356fd]212 if (rc != EOK) {
[a46e56b]213 async_answer_0(icall_handle, rc);
[02a09ed]214 return;
[a17356fd]215 }
[a35b458]216
[a17356fd]217 rc = srv->ops->send6(srv, &sdu);
[f834f90d]218 free(sdu.data);
[a46e56b]219 async_answer_0(icall_handle, rc);
[f834f90d]220}
221
[4f64a523]222void iplink_srv_init(iplink_srv_t *srv)
223{
224 fibril_mutex_initialize(&srv->lock);
225 srv->connected = false;
226 srv->ops = NULL;
227 srv->arg = NULL;
228 srv->client_sess = NULL;
229}
230
[a46e56b]231errno_t iplink_conn(cap_call_handle_t icall_handle, ipc_call_t *icall, void *arg)
[f834f90d]232{
[a17356fd]233 iplink_srv_t *srv = (iplink_srv_t *) arg;
[b7fd2a0]234 errno_t rc;
[a35b458]235
[4f64a523]236 fibril_mutex_lock(&srv->lock);
237 if (srv->connected) {
238 fibril_mutex_unlock(&srv->lock);
[a46e56b]239 async_answer_0(icall_handle, EBUSY);
[4f64a523]240 return EBUSY;
241 }
[a35b458]242
[4f64a523]243 srv->connected = true;
244 fibril_mutex_unlock(&srv->lock);
[a35b458]245
[f834f90d]246 /* Accept the connection */
[a46e56b]247 async_answer_0(icall_handle, EOK);
[a35b458]248
[f834f90d]249 async_sess_t *sess = async_callback_receive(EXCHANGE_SERIALIZE);
250 if (sess == NULL)
251 return ENOMEM;
[a35b458]252
[4f64a523]253 srv->client_sess = sess;
[a35b458]254
[4f64a523]255 rc = srv->ops->open(srv);
[f834f90d]256 if (rc != EOK)
257 return rc;
[a35b458]258
[f834f90d]259 while (true) {
260 ipc_call_t call;
[a46e56b]261 cap_call_handle_t chandle = async_get_call(&call);
[f834f90d]262 sysarg_t method = IPC_GET_IMETHOD(call);
[a35b458]263
[f834f90d]264 if (!method) {
265 /* The other side has hung up */
[a2e3ee6]266 fibril_mutex_lock(&srv->lock);
[4802dd7]267 srv->connected = false;
[a2e3ee6]268 fibril_mutex_unlock(&srv->lock);
[a46e56b]269 async_answer_0(chandle, EOK);
[f834f90d]270 break;
271 }
[a35b458]272
[f834f90d]273 switch (method) {
274 case IPLINK_GET_MTU:
[a46e56b]275 iplink_get_mtu_srv(srv, chandle, &call);
[f834f90d]276 break;
[a17356fd]277 case IPLINK_GET_MAC48:
[a46e56b]278 iplink_get_mac48_srv(srv, chandle, &call);
[a17356fd]279 break;
[c3b25985]280 case IPLINK_SET_MAC48:
[a46e56b]281 iplink_set_mac48_srv(srv, chandle, &call);
[c3b25985]282 break;
[f834f90d]283 case IPLINK_SEND:
[a46e56b]284 iplink_send_srv(srv, chandle, &call);
[f834f90d]285 break;
[a17356fd]286 case IPLINK_SEND6:
[a46e56b]287 iplink_send6_srv(srv, chandle, &call);
[a17356fd]288 break;
[962f03b]289 case IPLINK_ADDR_ADD:
[a46e56b]290 iplink_addr_add_srv(srv, chandle, &call);
[962f03b]291 break;
292 case IPLINK_ADDR_REMOVE:
[a46e56b]293 iplink_addr_remove_srv(srv, chandle, &call);
[962f03b]294 break;
[f834f90d]295 default:
[a46e56b]296 async_answer_0(chandle, EINVAL);
[f834f90d]297 }
298 }
[a35b458]299
[4f64a523]300 return srv->ops->close(srv);
[f834f90d]301}
302
[417a2ba1]303/* XXX Version should be part of @a sdu */
[b7fd2a0]304errno_t iplink_ev_recv(iplink_srv_t *srv, iplink_recv_sdu_t *sdu, ip_ver_t ver)
[f834f90d]305{
[4f64a523]306 if (srv->client_sess == NULL)
307 return EIO;
[a35b458]308
[4f64a523]309 async_exch_t *exch = async_exchange_begin(srv->client_sess);
[a35b458]310
[f834f90d]311 ipc_call_t answer;
[417a2ba1]312 aid_t req = async_send_1(exch, IPLINK_EV_RECV, (sysarg_t)ver,
[02a09ed]313 &answer);
[a35b458]314
[b7fd2a0]315 errno_t rc = async_data_write_start(exch, sdu->data, sdu->size);
[f834f90d]316 async_exchange_end(exch);
[a35b458]317
[f834f90d]318 if (rc != EOK) {
[50b581d]319 async_forget(req);
[f834f90d]320 return rc;
321 }
[a35b458]322
[b7fd2a0]323 errno_t retval;
[f834f90d]324 async_wait_for(req, &retval);
325 if (retval != EOK)
326 return retval;
[a35b458]327
[f834f90d]328 return EOK;
329}
330
[b7fd2a0]331errno_t iplink_ev_change_addr(iplink_srv_t *srv, addr48_t *addr)
[c3b25985]332{
333 if (srv->client_sess == NULL)
334 return EIO;
[a35b458]335
[c3b25985]336 async_exch_t *exch = async_exchange_begin(srv->client_sess);
[a35b458]337
[c3b25985]338 ipc_call_t answer;
339 aid_t req = async_send_0(exch, IPLINK_EV_CHANGE_ADDR, &answer);
[a35b458]340
[b7fd2a0]341 errno_t rc = async_data_write_start(exch, addr, sizeof(addr48_t));
[c3b25985]342 async_exchange_end(exch);
[a35b458]343
[c3b25985]344 if (rc != EOK) {
345 async_forget(req);
346 return rc;
347 }
[a35b458]348
[b7fd2a0]349 errno_t retval;
[c3b25985]350 async_wait_for(req, &retval);
351 if (retval != EOK)
352 return retval;
[a35b458]353
[c3b25985]354 return EOK;
355}
356
[f834f90d]357/** @}
358 */
Note: See TracBrowser for help on using the repository browser.