[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 |
|
---|
| 43 | static void iplink_get_mtu_srv(iplink_conn_t *conn, ipc_callid_t callid,
|
---|
| 44 | ipc_call_t *call)
|
---|
| 45 | {
|
---|
| 46 | int rc;
|
---|
| 47 | size_t mtu;
|
---|
| 48 |
|
---|
| 49 | rc = conn->srv->ops->get_mtu(conn, &mtu);
|
---|
| 50 | async_answer_1(callid, rc, mtu);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | static void iplink_send_srv(iplink_conn_t *conn, ipc_callid_t callid,
|
---|
| 54 | ipc_call_t *call)
|
---|
| 55 | {
|
---|
| 56 | iplink_srv_sdu_t sdu;
|
---|
| 57 | int rc;
|
---|
| 58 |
|
---|
[ceba4bed] | 59 | sdu.lsrc.ipv4 = IPC_GET_ARG1(*call);
|
---|
| 60 | sdu.ldest.ipv4 = IPC_GET_ARG2(*call);
|
---|
[f834f90d] | 61 |
|
---|
| 62 | rc = async_data_write_accept(&sdu.data, false, 0, 0, 0, &sdu.size);
|
---|
| 63 | if (rc != EOK) {
|
---|
| 64 | async_answer_0(callid, rc);
|
---|
| 65 | return;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | rc = conn->srv->ops->send(conn, &sdu);
|
---|
| 69 | free(sdu.data);
|
---|
| 70 | async_answer_0(callid, rc);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | int iplink_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
| 74 | {
|
---|
| 75 | iplink_srv_t *srv = (iplink_srv_t *)arg;
|
---|
| 76 | iplink_conn_t conn;
|
---|
| 77 | int rc;
|
---|
| 78 |
|
---|
| 79 | /* Accept the connection */
|
---|
| 80 | async_answer_0(iid, EOK);
|
---|
| 81 |
|
---|
| 82 | async_sess_t *sess = async_callback_receive(EXCHANGE_SERIALIZE);
|
---|
| 83 | if (sess == NULL)
|
---|
| 84 | return ENOMEM;
|
---|
| 85 |
|
---|
| 86 | conn.srv = srv;
|
---|
| 87 | conn.client_sess = sess;
|
---|
| 88 |
|
---|
| 89 | rc = srv->ops->open(&conn);
|
---|
| 90 | if (rc != EOK)
|
---|
| 91 | return rc;
|
---|
| 92 |
|
---|
| 93 | while (true) {
|
---|
| 94 | ipc_call_t call;
|
---|
| 95 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 96 | sysarg_t method = IPC_GET_IMETHOD(call);
|
---|
| 97 |
|
---|
| 98 | if (!method) {
|
---|
| 99 | /* The other side has hung up */
|
---|
| 100 | async_answer_0(callid, EOK);
|
---|
| 101 | break;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | switch (method) {
|
---|
| 105 | case IPLINK_GET_MTU:
|
---|
| 106 | iplink_get_mtu_srv(&conn, callid, &call);
|
---|
| 107 | break;
|
---|
| 108 | case IPLINK_SEND:
|
---|
| 109 | iplink_send_srv(&conn, callid, &call);
|
---|
| 110 | break;
|
---|
| 111 | default:
|
---|
| 112 | async_answer_0(callid, EINVAL);
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | return srv->ops->close(&conn);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | int iplink_ev_recv(iplink_conn_t *conn, iplink_srv_sdu_t *sdu)
|
---|
| 120 | {
|
---|
| 121 | async_exch_t *exch = async_exchange_begin(conn->client_sess);
|
---|
| 122 |
|
---|
| 123 | ipc_call_t answer;
|
---|
[ceba4bed] | 124 | aid_t req = async_send_2(exch, IPLINK_EV_RECV, sdu->lsrc.ipv4,
|
---|
| 125 | sdu->ldest.ipv4, &answer);
|
---|
[f834f90d] | 126 | int rc = async_data_write_start(exch, sdu->data, sdu->size);
|
---|
| 127 | async_exchange_end(exch);
|
---|
| 128 |
|
---|
| 129 | if (rc != EOK) {
|
---|
| 130 | async_wait_for(req, NULL);
|
---|
| 131 | return rc;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | sysarg_t retval;
|
---|
| 135 | async_wait_for(req, &retval);
|
---|
| 136 | if (retval != EOK)
|
---|
| 137 | return retval;
|
---|
| 138 |
|
---|
| 139 | return EOK;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | /** @}
|
---|
| 143 | */
|
---|