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