[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 client stub
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <async.h>
|
---|
| 38 | #include <assert.h>
|
---|
| 39 | #include <errno.h>
|
---|
| 40 | #include <inet/iplink.h>
|
---|
| 41 | #include <ipc/iplink.h>
|
---|
| 42 | #include <ipc/services.h>
|
---|
| 43 | #include <loc.h>
|
---|
| 44 | #include <stdlib.h>
|
---|
| 45 |
|
---|
| 46 | static void iplink_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
|
---|
| 47 |
|
---|
| 48 | int iplink_open(async_sess_t *sess, iplink_ev_ops_t *ev_ops,
|
---|
| 49 | iplink_t **riplink)
|
---|
| 50 | {
|
---|
| 51 | iplink_t *iplink = NULL;
|
---|
| 52 | int rc;
|
---|
| 53 |
|
---|
| 54 | iplink = calloc(1, sizeof(iplink_t));
|
---|
| 55 | if (iplink == NULL)
|
---|
| 56 | return ENOMEM;
|
---|
| 57 |
|
---|
| 58 | iplink->sess = sess;
|
---|
| 59 | iplink->ev_ops = ev_ops;
|
---|
| 60 |
|
---|
| 61 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 62 |
|
---|
| 63 | rc = async_connect_to_me(exch, 0, 0, 0, iplink_cb_conn, iplink);
|
---|
| 64 | async_exchange_end(exch);
|
---|
| 65 |
|
---|
| 66 | if (rc != EOK)
|
---|
| 67 | goto error;
|
---|
| 68 |
|
---|
| 69 | *riplink = iplink;
|
---|
| 70 | return EOK;
|
---|
| 71 |
|
---|
| 72 | error:
|
---|
| 73 | if (iplink != NULL)
|
---|
| 74 | free(iplink);
|
---|
| 75 |
|
---|
| 76 | return rc;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | void iplink_close(iplink_t *iplink)
|
---|
| 80 | {
|
---|
| 81 | /* XXX Synchronize with iplink_cb_conn */
|
---|
| 82 | free(iplink);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | int iplink_send(iplink_t *iplink, iplink_sdu_t *sdu)
|
---|
| 86 | {
|
---|
| 87 | async_exch_t *exch = async_exchange_begin(iplink->sess);
|
---|
| 88 |
|
---|
| 89 | ipc_call_t answer;
|
---|
[ceba4bed] | 90 | aid_t req = async_send_2(exch, IPLINK_SEND, sdu->lsrc.ipv4,
|
---|
| 91 | sdu->ldest.ipv4, &answer);
|
---|
[f834f90d] | 92 | int rc = async_data_write_start(exch, sdu->data, sdu->size);
|
---|
| 93 | async_exchange_end(exch);
|
---|
| 94 |
|
---|
| 95 | if (rc != EOK) {
|
---|
[50b581d] | 96 | async_forget(req);
|
---|
[f834f90d] | 97 | return rc;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | sysarg_t retval;
|
---|
| 101 | async_wait_for(req, &retval);
|
---|
| 102 | if (retval != EOK)
|
---|
| 103 | return retval;
|
---|
| 104 |
|
---|
| 105 | return EOK;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | int iplink_get_mtu(iplink_t *iplink, size_t *rmtu)
|
---|
| 109 | {
|
---|
| 110 | sysarg_t mtu;
|
---|
| 111 | async_exch_t *exch = async_exchange_begin(iplink->sess);
|
---|
| 112 |
|
---|
| 113 | int rc = async_req_0_1(exch, IPLINK_GET_MTU, &mtu);
|
---|
| 114 | async_exchange_end(exch);
|
---|
| 115 |
|
---|
| 116 | if (rc != EOK)
|
---|
| 117 | return rc;
|
---|
| 118 |
|
---|
| 119 | *rmtu = mtu;
|
---|
| 120 | return EOK;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[962f03b] | 123 | int iplink_addr_add(iplink_t *iplink, iplink_addr_t *addr)
|
---|
| 124 | {
|
---|
| 125 | async_exch_t *exch = async_exchange_begin(iplink->sess);
|
---|
| 126 |
|
---|
| 127 | int rc = async_req_1_0(exch, IPLINK_ADDR_ADD, (sysarg_t)addr->ipv4);
|
---|
| 128 | async_exchange_end(exch);
|
---|
| 129 |
|
---|
| 130 | return rc;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | int iplink_addr_remove(iplink_t *iplink, iplink_addr_t *addr)
|
---|
| 134 | {
|
---|
| 135 | async_exch_t *exch = async_exchange_begin(iplink->sess);
|
---|
| 136 |
|
---|
| 137 | int rc = async_req_1_0(exch, IPLINK_ADDR_REMOVE, (sysarg_t)addr->ipv4);
|
---|
| 138 | async_exchange_end(exch);
|
---|
| 139 |
|
---|
| 140 | return rc;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[f834f90d] | 143 | static void iplink_ev_recv(iplink_t *iplink, ipc_callid_t callid,
|
---|
| 144 | ipc_call_t *call)
|
---|
| 145 | {
|
---|
| 146 | int rc;
|
---|
| 147 | iplink_sdu_t sdu;
|
---|
| 148 |
|
---|
[ceba4bed] | 149 | sdu.lsrc.ipv4 = IPC_GET_ARG1(*call);
|
---|
| 150 | sdu.ldest.ipv4 = IPC_GET_ARG2(*call);
|
---|
[f834f90d] | 151 |
|
---|
| 152 | rc = async_data_write_accept(&sdu.data, false, 0, 0, 0, &sdu.size);
|
---|
| 153 | if (rc != EOK) {
|
---|
| 154 | async_answer_0(callid, rc);
|
---|
| 155 | return;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | rc = iplink->ev_ops->recv(iplink, &sdu);
|
---|
[ffa8912] | 159 | free(sdu.data);
|
---|
[f834f90d] | 160 | async_answer_0(callid, rc);
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | static void iplink_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
| 164 | {
|
---|
| 165 | iplink_t *iplink = (iplink_t *)arg;
|
---|
| 166 |
|
---|
| 167 | while (true) {
|
---|
| 168 | ipc_call_t call;
|
---|
| 169 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 170 |
|
---|
| 171 | if (!IPC_GET_IMETHOD(call)) {
|
---|
| 172 | /* TODO: Handle hangup */
|
---|
| 173 | return;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | switch (IPC_GET_IMETHOD(call)) {
|
---|
| 177 | case IPLINK_EV_RECV:
|
---|
| 178 | iplink_ev_recv(iplink, callid, &call);
|
---|
| 179 | break;
|
---|
| 180 | default:
|
---|
| 181 | async_answer_0(callid, ENOTSUP);
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | /** @}
|
---|
| 187 | */
|
---|