[6428115] | 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 | #include <async.h>
|
---|
| 30 | #include <assert.h>
|
---|
| 31 | #include <errno.h>
|
---|
| 32 | #include <inet/inetping.h>
|
---|
| 33 | #include <ipc/inet.h>
|
---|
| 34 | #include <ipc/services.h>
|
---|
| 35 | #include <loc.h>
|
---|
| 36 | #include <stdlib.h>
|
---|
| 37 | #include <str.h>
|
---|
| 38 |
|
---|
| 39 | static void inetping_cb_conn(ipc_callid_t, ipc_call_t *, void *);
|
---|
| 40 | static void inetping_ev_recv(ipc_callid_t, ipc_call_t *);
|
---|
| 41 |
|
---|
| 42 | static async_sess_t *inetping_sess = NULL;
|
---|
| 43 | static inetping_ev_ops_t *inetping_ev_ops;
|
---|
| 44 |
|
---|
| 45 | int inetping_init(inetping_ev_ops_t *ev_ops)
|
---|
| 46 | {
|
---|
| 47 | service_id_t inetping_svc;
|
---|
| 48 | int rc;
|
---|
| 49 |
|
---|
| 50 | assert(inetping_sess == NULL);
|
---|
[77ad86c] | 51 |
|
---|
[6428115] | 52 | inetping_ev_ops = ev_ops;
|
---|
[77ad86c] | 53 |
|
---|
[6428115] | 54 | rc = loc_service_get_id(SERVICE_NAME_INETPING, &inetping_svc,
|
---|
| 55 | IPC_FLAG_BLOCKING);
|
---|
| 56 | if (rc != EOK)
|
---|
| 57 | return ENOENT;
|
---|
[77ad86c] | 58 |
|
---|
[6428115] | 59 | inetping_sess = loc_service_connect(EXCHANGE_SERIALIZE, inetping_svc,
|
---|
| 60 | IPC_FLAG_BLOCKING);
|
---|
| 61 | if (inetping_sess == NULL)
|
---|
| 62 | return ENOENT;
|
---|
[77ad86c] | 63 |
|
---|
[6428115] | 64 | async_exch_t *exch = async_exchange_begin(inetping_sess);
|
---|
| 65 |
|
---|
| 66 | rc = async_connect_to_me(exch, 0, 0, 0, inetping_cb_conn, NULL);
|
---|
| 67 | async_exchange_end(exch);
|
---|
[77ad86c] | 68 |
|
---|
[6428115] | 69 | if (rc != EOK) {
|
---|
| 70 | async_hangup(inetping_sess);
|
---|
| 71 | inetping_sess = NULL;
|
---|
| 72 | return rc;
|
---|
| 73 | }
|
---|
[77ad86c] | 74 |
|
---|
[6428115] | 75 | return EOK;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | int inetping_send(inetping_sdu_t *sdu)
|
---|
| 79 | {
|
---|
| 80 | async_exch_t *exch = async_exchange_begin(inetping_sess);
|
---|
[3e66428] | 81 |
|
---|
[6428115] | 82 | ipc_call_t answer;
|
---|
[3e66428] | 83 | aid_t req = async_send_3(exch, INETPING_SEND, (sysarg_t) sdu->src,
|
---|
| 84 | (sysarg_t) sdu->dest, sdu->seq_no, &answer);
|
---|
[6428115] | 85 | sysarg_t retval = async_data_write_start(exch, sdu->data, sdu->size);
|
---|
[3e66428] | 86 |
|
---|
[6428115] | 87 | async_exchange_end(exch);
|
---|
[3e66428] | 88 |
|
---|
[6428115] | 89 | if (retval != EOK) {
|
---|
[50b581d] | 90 | async_forget(req);
|
---|
[6428115] | 91 | return retval;
|
---|
| 92 | }
|
---|
[3e66428] | 93 |
|
---|
[6428115] | 94 | async_wait_for(req, &retval);
|
---|
| 95 | return retval;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[3e66428] | 98 | int inetping_get_srcaddr(uint32_t remote, uint32_t *local)
|
---|
[6428115] | 99 | {
|
---|
| 100 | async_exch_t *exch = async_exchange_begin(inetping_sess);
|
---|
[3e66428] | 101 |
|
---|
| 102 | sysarg_t local_addr;
|
---|
| 103 | int rc = async_req_1_1(exch, INETPING_GET_SRCADDR, (sysarg_t) remote,
|
---|
[6428115] | 104 | &local_addr);
|
---|
[3e66428] | 105 |
|
---|
[6428115] | 106 | async_exchange_end(exch);
|
---|
[3e66428] | 107 |
|
---|
[6428115] | 108 | if (rc != EOK)
|
---|
| 109 | return rc;
|
---|
[3e66428] | 110 |
|
---|
| 111 | *local = (uint32_t) local_addr;
|
---|
[6428115] | 112 | return EOK;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | static void inetping_ev_recv(ipc_callid_t callid, ipc_call_t *call)
|
---|
| 116 | {
|
---|
| 117 | inetping_sdu_t sdu;
|
---|
[3e66428] | 118 |
|
---|
| 119 | sdu.src = IPC_GET_ARG1(*call);
|
---|
| 120 | sdu.dest = IPC_GET_ARG2(*call);
|
---|
[6428115] | 121 | sdu.seq_no = IPC_GET_ARG3(*call);
|
---|
[3e66428] | 122 |
|
---|
| 123 | int rc = async_data_write_accept(&sdu.data, false, 0, 0, 0, &sdu.size);
|
---|
[6428115] | 124 | if (rc != EOK) {
|
---|
| 125 | async_answer_0(callid, rc);
|
---|
| 126 | return;
|
---|
| 127 | }
|
---|
[3e66428] | 128 |
|
---|
[6428115] | 129 | rc = inetping_ev_ops->recv(&sdu);
|
---|
| 130 | free(sdu.data);
|
---|
| 131 | async_answer_0(callid, rc);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | static void inetping_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
| 135 | {
|
---|
| 136 | while (true) {
|
---|
| 137 | ipc_call_t call;
|
---|
| 138 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 139 |
|
---|
| 140 | if (!IPC_GET_IMETHOD(call)) {
|
---|
| 141 | /* TODO: Handle hangup */
|
---|
| 142 | return;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | switch (IPC_GET_IMETHOD(call)) {
|
---|
| 146 | case INETPING_EV_RECV:
|
---|
| 147 | inetping_ev_recv(callid, &call);
|
---|
| 148 | break;
|
---|
| 149 | default:
|
---|
| 150 | async_answer_0(callid, ENOTSUP);
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | /** @}
|
---|
| 156 | */
|
---|