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