[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 | */
|
---|
| 36 | #include <errno.h>
|
---|
| 37 | #include <ipc/iplink.h>
|
---|
| 38 | #include <stdlib.h>
|
---|
| 39 | #include <sys/types.h>
|
---|
| 40 |
|
---|
| 41 | #include <inet/iplink_srv.h>
|
---|
| 42 |
|
---|
[4f64a523] | 43 | static void iplink_get_mtu_srv(iplink_srv_t *srv, ipc_callid_t callid,
|
---|
[f834f90d] | 44 | ipc_call_t *call)
|
---|
| 45 | {
|
---|
| 46 | int rc;
|
---|
| 47 | size_t mtu;
|
---|
| 48 |
|
---|
[4f64a523] | 49 | rc = srv->ops->get_mtu(srv, &mtu);
|
---|
[f834f90d] | 50 | async_answer_1(callid, rc, mtu);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[962f03b] | 53 | static void iplink_addr_add_srv(iplink_srv_t *srv, ipc_callid_t callid,
|
---|
| 54 | ipc_call_t *call)
|
---|
| 55 | {
|
---|
[a2e3ee6] | 56 | int rc = srv->ops->addr_add(srv, IPC_GET_ARG1(*call));
|
---|
[962f03b] | 57 | async_answer_0(callid, rc);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | static void iplink_addr_remove_srv(iplink_srv_t *srv, ipc_callid_t callid,
|
---|
| 61 | ipc_call_t *call)
|
---|
| 62 | {
|
---|
[a2e3ee6] | 63 | int rc = srv->ops->addr_remove(srv, IPC_GET_ARG1(*call));
|
---|
[962f03b] | 64 | async_answer_0(callid, rc);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[4f64a523] | 67 | static void iplink_send_srv(iplink_srv_t *srv, ipc_callid_t callid,
|
---|
[f834f90d] | 68 | ipc_call_t *call)
|
---|
| 69 | {
|
---|
| 70 | iplink_srv_sdu_t sdu;
|
---|
| 71 | int rc;
|
---|
| 72 |
|
---|
[a2e3ee6] | 73 | sdu.lsrc = IPC_GET_ARG1(*call);
|
---|
| 74 | sdu.ldest = IPC_GET_ARG2(*call);
|
---|
[f834f90d] | 75 |
|
---|
| 76 | rc = async_data_write_accept(&sdu.data, false, 0, 0, 0, &sdu.size);
|
---|
| 77 | if (rc != EOK) {
|
---|
| 78 | async_answer_0(callid, rc);
|
---|
| 79 | return;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[4f64a523] | 82 | rc = srv->ops->send(srv, &sdu);
|
---|
[f834f90d] | 83 | free(sdu.data);
|
---|
| 84 | async_answer_0(callid, rc);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[4f64a523] | 87 | void iplink_srv_init(iplink_srv_t *srv)
|
---|
| 88 | {
|
---|
| 89 | fibril_mutex_initialize(&srv->lock);
|
---|
| 90 | srv->connected = false;
|
---|
| 91 | srv->ops = NULL;
|
---|
| 92 | srv->arg = NULL;
|
---|
| 93 | srv->client_sess = NULL;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[f834f90d] | 96 | int iplink_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
| 97 | {
|
---|
| 98 | iplink_srv_t *srv = (iplink_srv_t *)arg;
|
---|
| 99 | int rc;
|
---|
| 100 |
|
---|
[4f64a523] | 101 | fibril_mutex_lock(&srv->lock);
|
---|
| 102 | if (srv->connected) {
|
---|
| 103 | fibril_mutex_unlock(&srv->lock);
|
---|
| 104 | async_answer_0(iid, EBUSY);
|
---|
| 105 | return EBUSY;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | srv->connected = true;
|
---|
| 109 | fibril_mutex_unlock(&srv->lock);
|
---|
| 110 |
|
---|
[f834f90d] | 111 | /* Accept the connection */
|
---|
| 112 | async_answer_0(iid, EOK);
|
---|
| 113 |
|
---|
| 114 | async_sess_t *sess = async_callback_receive(EXCHANGE_SERIALIZE);
|
---|
| 115 | if (sess == NULL)
|
---|
| 116 | return ENOMEM;
|
---|
| 117 |
|
---|
[4f64a523] | 118 | srv->client_sess = sess;
|
---|
[f834f90d] | 119 |
|
---|
[4f64a523] | 120 | rc = srv->ops->open(srv);
|
---|
[f834f90d] | 121 | if (rc != EOK)
|
---|
| 122 | return rc;
|
---|
| 123 |
|
---|
| 124 | while (true) {
|
---|
| 125 | ipc_call_t call;
|
---|
| 126 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 127 | sysarg_t method = IPC_GET_IMETHOD(call);
|
---|
| 128 |
|
---|
| 129 | if (!method) {
|
---|
| 130 | /* The other side has hung up */
|
---|
[a2e3ee6] | 131 | fibril_mutex_lock(&srv->lock);
|
---|
[4802dd7] | 132 | srv->connected = false;
|
---|
[a2e3ee6] | 133 | fibril_mutex_unlock(&srv->lock);
|
---|
[f834f90d] | 134 | async_answer_0(callid, EOK);
|
---|
| 135 | break;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | switch (method) {
|
---|
| 139 | case IPLINK_GET_MTU:
|
---|
[4f64a523] | 140 | iplink_get_mtu_srv(srv, callid, &call);
|
---|
[f834f90d] | 141 | break;
|
---|
| 142 | case IPLINK_SEND:
|
---|
[4f64a523] | 143 | iplink_send_srv(srv, callid, &call);
|
---|
[f834f90d] | 144 | break;
|
---|
[962f03b] | 145 | case IPLINK_ADDR_ADD:
|
---|
| 146 | iplink_addr_add_srv(srv, callid, &call);
|
---|
| 147 | break;
|
---|
| 148 | case IPLINK_ADDR_REMOVE:
|
---|
| 149 | iplink_addr_remove_srv(srv, callid, &call);
|
---|
| 150 | break;
|
---|
[f834f90d] | 151 | default:
|
---|
| 152 | async_answer_0(callid, EINVAL);
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[4f64a523] | 156 | return srv->ops->close(srv);
|
---|
[f834f90d] | 157 | }
|
---|
| 158 |
|
---|
[4f64a523] | 159 | int iplink_ev_recv(iplink_srv_t *srv, iplink_srv_sdu_t *sdu)
|
---|
[f834f90d] | 160 | {
|
---|
[4f64a523] | 161 | if (srv->client_sess == NULL)
|
---|
| 162 | return EIO;
|
---|
| 163 |
|
---|
| 164 | async_exch_t *exch = async_exchange_begin(srv->client_sess);
|
---|
[f834f90d] | 165 |
|
---|
| 166 | ipc_call_t answer;
|
---|
[a2e3ee6] | 167 | aid_t req = async_send_2(exch, IPLINK_EV_RECV, (sysarg_t) sdu->lsrc,
|
---|
| 168 | (sysarg_t) sdu->ldest, &answer);
|
---|
[f834f90d] | 169 | int rc = async_data_write_start(exch, sdu->data, sdu->size);
|
---|
| 170 | async_exchange_end(exch);
|
---|
| 171 |
|
---|
| 172 | if (rc != EOK) {
|
---|
[50b581d] | 173 | async_forget(req);
|
---|
[f834f90d] | 174 | return rc;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | sysarg_t retval;
|
---|
| 178 | async_wait_for(req, &retval);
|
---|
| 179 | if (retval != EOK)
|
---|
| 180 | return retval;
|
---|
| 181 |
|
---|
| 182 | return EOK;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | /** @}
|
---|
| 186 | */
|
---|