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