[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;
|
---|
[a17356fd] | 88 | aid_t req = async_send_2(exch, IPLINK_SEND, (sysarg_t) sdu->src,
|
---|
| 89 | (sysarg_t) sdu->dest, &answer);
|
---|
| 90 |
|
---|
| 91 | int rc = async_data_write_start(exch, sdu->data, sdu->size);
|
---|
| 92 |
|
---|
| 93 | async_exchange_end(exch);
|
---|
[02a09ed] | 94 |
|
---|
| 95 | if (rc != EOK) {
|
---|
| 96 | async_forget(req);
|
---|
| 97 | return rc;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[a17356fd] | 100 | sysarg_t retval;
|
---|
| 101 | async_wait_for(req, &retval);
|
---|
| 102 |
|
---|
| 103 | return (int) retval;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | int iplink_send6(iplink_t *iplink, iplink_sdu6_t *sdu)
|
---|
| 107 | {
|
---|
| 108 | async_exch_t *exch = async_exchange_begin(iplink->sess);
|
---|
| 109 |
|
---|
| 110 | ipc_call_t answer;
|
---|
| 111 | aid_t req = async_send_0(exch, IPLINK_SEND6, &answer);
|
---|
| 112 |
|
---|
| 113 | int rc = async_data_write_start(exch, &sdu->dest, sizeof(addr48_t));
|
---|
[02a09ed] | 114 | if (rc != EOK) {
|
---|
| 115 | async_exchange_end(exch);
|
---|
| 116 | async_forget(req);
|
---|
| 117 | return rc;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | rc = async_data_write_start(exch, sdu->data, sdu->size);
|
---|
[a2e3ee6] | 121 |
|
---|
[f834f90d] | 122 | async_exchange_end(exch);
|
---|
[a2e3ee6] | 123 |
|
---|
[f834f90d] | 124 | if (rc != EOK) {
|
---|
[50b581d] | 125 | async_forget(req);
|
---|
[f834f90d] | 126 | return rc;
|
---|
| 127 | }
|
---|
[a2e3ee6] | 128 |
|
---|
[f834f90d] | 129 | sysarg_t retval;
|
---|
| 130 | async_wait_for(req, &retval);
|
---|
[a2e3ee6] | 131 |
|
---|
[b5cf742a] | 132 | return (int) retval;
|
---|
[f834f90d] | 133 | }
|
---|
| 134 |
|
---|
| 135 | int iplink_get_mtu(iplink_t *iplink, size_t *rmtu)
|
---|
| 136 | {
|
---|
| 137 | async_exch_t *exch = async_exchange_begin(iplink->sess);
|
---|
[a17356fd] | 138 |
|
---|
| 139 | sysarg_t mtu;
|
---|
[f834f90d] | 140 | int rc = async_req_0_1(exch, IPLINK_GET_MTU, &mtu);
|
---|
[a17356fd] | 141 |
|
---|
[f834f90d] | 142 | async_exchange_end(exch);
|
---|
[a17356fd] | 143 |
|
---|
[f834f90d] | 144 | if (rc != EOK)
|
---|
| 145 | return rc;
|
---|
[a17356fd] | 146 |
|
---|
[f834f90d] | 147 | *rmtu = mtu;
|
---|
| 148 | return EOK;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[a17356fd] | 151 | int iplink_get_mac48(iplink_t *iplink, addr48_t *mac)
|
---|
| 152 | {
|
---|
| 153 | async_exch_t *exch = async_exchange_begin(iplink->sess);
|
---|
| 154 |
|
---|
| 155 | ipc_call_t answer;
|
---|
| 156 | aid_t req = async_send_0(exch, IPLINK_GET_MAC48, &answer);
|
---|
| 157 |
|
---|
| 158 | int rc = async_data_read_start(exch, mac, sizeof(addr48_t));
|
---|
| 159 |
|
---|
| 160 | loc_exchange_end(exch);
|
---|
| 161 |
|
---|
| 162 | if (rc != EOK) {
|
---|
| 163 | async_forget(req);
|
---|
| 164 | return rc;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | sysarg_t retval;
|
---|
| 168 | async_wait_for(req, &retval);
|
---|
| 169 |
|
---|
| 170 | return (int) retval;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[c3b25985] | 173 | int iplink_set_mac48(iplink_t *iplink, addr48_t mac)
|
---|
| 174 | {
|
---|
| 175 | async_exch_t *exch = async_exchange_begin(iplink->sess);
|
---|
| 176 |
|
---|
| 177 | ipc_call_t answer;
|
---|
| 178 | aid_t req = async_send_0(exch, IPLINK_GET_MAC48, &answer);
|
---|
| 179 |
|
---|
| 180 | int rc = async_data_read_start(exch, mac, sizeof(addr48_t));
|
---|
| 181 |
|
---|
| 182 | loc_exchange_end(exch);
|
---|
| 183 |
|
---|
| 184 | if (rc != EOK) {
|
---|
| 185 | async_forget(req);
|
---|
| 186 | return rc;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | sysarg_t retval;
|
---|
| 190 | async_wait_for(req, &retval);
|
---|
| 191 |
|
---|
| 192 | return (int) retval;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 |
|
---|
[a2e3ee6] | 196 | int iplink_addr_add(iplink_t *iplink, inet_addr_t *addr)
|
---|
[962f03b] | 197 | {
|
---|
| 198 | async_exch_t *exch = async_exchange_begin(iplink->sess);
|
---|
[02a09ed] | 199 |
|
---|
| 200 | ipc_call_t answer;
|
---|
| 201 | aid_t req = async_send_0(exch, IPLINK_ADDR_ADD, &answer);
|
---|
| 202 |
|
---|
| 203 | int rc = async_data_write_start(exch, addr, sizeof(inet_addr_t));
|
---|
[962f03b] | 204 | async_exchange_end(exch);
|
---|
[a2e3ee6] | 205 |
|
---|
[02a09ed] | 206 | if (rc != EOK) {
|
---|
| 207 | async_forget(req);
|
---|
| 208 | return rc;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | sysarg_t retval;
|
---|
| 212 | async_wait_for(req, &retval);
|
---|
| 213 |
|
---|
| 214 | return (int) retval;
|
---|
[962f03b] | 215 | }
|
---|
| 216 |
|
---|
[a2e3ee6] | 217 | int iplink_addr_remove(iplink_t *iplink, inet_addr_t *addr)
|
---|
[962f03b] | 218 | {
|
---|
| 219 | async_exch_t *exch = async_exchange_begin(iplink->sess);
|
---|
[02a09ed] | 220 |
|
---|
| 221 | ipc_call_t answer;
|
---|
| 222 | aid_t req = async_send_0(exch, IPLINK_ADDR_REMOVE, &answer);
|
---|
| 223 |
|
---|
| 224 | int rc = async_data_write_start(exch, addr, sizeof(inet_addr_t));
|
---|
[962f03b] | 225 | async_exchange_end(exch);
|
---|
[a2e3ee6] | 226 |
|
---|
[02a09ed] | 227 | if (rc != EOK) {
|
---|
| 228 | async_forget(req);
|
---|
| 229 | return rc;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | sysarg_t retval;
|
---|
| 233 | async_wait_for(req, &retval);
|
---|
| 234 |
|
---|
| 235 | return (int) retval;
|
---|
[962f03b] | 236 | }
|
---|
| 237 |
|
---|
[02a09ed] | 238 | static void iplink_ev_recv(iplink_t *iplink, ipc_callid_t iid,
|
---|
| 239 | ipc_call_t *icall)
|
---|
[f834f90d] | 240 | {
|
---|
[02a09ed] | 241 | iplink_recv_sdu_t sdu;
|
---|
[a2e3ee6] | 242 |
|
---|
[417a2ba1] | 243 | ip_ver_t ver = IPC_GET_ARG1(*icall);
|
---|
[a2e3ee6] | 244 |
|
---|
[02a09ed] | 245 | int rc = async_data_write_accept(&sdu.data, false, 0, 0, 0,
|
---|
| 246 | &sdu.size);
|
---|
[f834f90d] | 247 | if (rc != EOK) {
|
---|
[02a09ed] | 248 | async_answer_0(iid, rc);
|
---|
[f834f90d] | 249 | return;
|
---|
| 250 | }
|
---|
[a2e3ee6] | 251 |
|
---|
[417a2ba1] | 252 | rc = iplink->ev_ops->recv(iplink, &sdu, ver);
|
---|
[ffa8912] | 253 | free(sdu.data);
|
---|
[02a09ed] | 254 | async_answer_0(iid, rc);
|
---|
[f834f90d] | 255 | }
|
---|
| 256 |
|
---|
[c3b25985] | 257 | static void iplink_ev_change_addr(iplink_t *iplink, ipc_callid_t iid,
|
---|
| 258 | ipc_call_t *icall)
|
---|
| 259 | {
|
---|
| 260 | addr48_t *addr;
|
---|
| 261 | size_t size;
|
---|
| 262 |
|
---|
| 263 | int rc = async_data_write_accept((void **)&addr, false,
|
---|
| 264 | sizeof(addr48_t), sizeof(addr48_t), 0, &size);
|
---|
| 265 | if (rc != EOK) {
|
---|
| 266 | async_answer_0(iid, rc);
|
---|
| 267 | return;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | rc = iplink->ev_ops->change_addr(iplink, *addr);
|
---|
| 271 | free(addr);
|
---|
| 272 | async_answer_0(iid, EOK);
|
---|
| 273 | }
|
---|
| 274 |
|
---|
[f834f90d] | 275 | static void iplink_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
| 276 | {
|
---|
[b5cf742a] | 277 | iplink_t *iplink = (iplink_t *) arg;
|
---|
| 278 |
|
---|
[f834f90d] | 279 | while (true) {
|
---|
| 280 | ipc_call_t call;
|
---|
| 281 | ipc_callid_t callid = async_get_call(&call);
|
---|
[b5cf742a] | 282 |
|
---|
[f834f90d] | 283 | if (!IPC_GET_IMETHOD(call)) {
|
---|
| 284 | /* TODO: Handle hangup */
|
---|
| 285 | return;
|
---|
| 286 | }
|
---|
[b5cf742a] | 287 |
|
---|
[f834f90d] | 288 | switch (IPC_GET_IMETHOD(call)) {
|
---|
| 289 | case IPLINK_EV_RECV:
|
---|
| 290 | iplink_ev_recv(iplink, callid, &call);
|
---|
| 291 | break;
|
---|
[c3b25985] | 292 | case IPLINK_EV_CHANGE_ADDR:
|
---|
| 293 | iplink_ev_change_addr(iplink, callid, &call);
|
---|
[f834f90d] | 294 | default:
|
---|
| 295 | async_answer_0(callid, ENOTSUP);
|
---|
| 296 | }
|
---|
| 297 | }
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | /** @}
|
---|
| 301 | */
|
---|