[6428115] | 1 | /*
|
---|
[9749e47] | 2 | * Copyright (c) 2013 Jiri Svoboda
|
---|
| 3 | * Copyright (c) 2013 Martin Decky
|
---|
[6428115] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | #include <async.h>
|
---|
| 31 | #include <assert.h>
|
---|
| 32 | #include <errno.h>
|
---|
| 33 | #include <inet/inetping.h>
|
---|
| 34 | #include <ipc/inet.h>
|
---|
| 35 | #include <ipc/services.h>
|
---|
| 36 | #include <loc.h>
|
---|
| 37 | #include <stdlib.h>
|
---|
| 38 | #include <str.h>
|
---|
| 39 |
|
---|
| 40 | static void inetping_cb_conn(ipc_callid_t, ipc_call_t *, void *);
|
---|
| 41 | static void inetping_ev_recv(ipc_callid_t, ipc_call_t *);
|
---|
| 42 |
|
---|
| 43 | static async_sess_t *inetping_sess = NULL;
|
---|
| 44 | static inetping_ev_ops_t *inetping_ev_ops;
|
---|
| 45 |
|
---|
| 46 | int inetping_init(inetping_ev_ops_t *ev_ops)
|
---|
| 47 | {
|
---|
| 48 | service_id_t inetping_svc;
|
---|
| 49 | int rc;
|
---|
| 50 |
|
---|
| 51 | assert(inetping_sess == NULL);
|
---|
[9749e47] | 52 |
|
---|
[6428115] | 53 | inetping_ev_ops = ev_ops;
|
---|
[9749e47] | 54 |
|
---|
[6428115] | 55 | rc = loc_service_get_id(SERVICE_NAME_INETPING, &inetping_svc,
|
---|
| 56 | IPC_FLAG_BLOCKING);
|
---|
| 57 | if (rc != EOK)
|
---|
| 58 | return ENOENT;
|
---|
[9749e47] | 59 |
|
---|
[6428115] | 60 | inetping_sess = loc_service_connect(EXCHANGE_SERIALIZE, inetping_svc,
|
---|
| 61 | IPC_FLAG_BLOCKING);
|
---|
| 62 | if (inetping_sess == NULL)
|
---|
| 63 | return ENOENT;
|
---|
[9749e47] | 64 |
|
---|
[6428115] | 65 | async_exch_t *exch = async_exchange_begin(inetping_sess);
|
---|
| 66 |
|
---|
| 67 | rc = async_connect_to_me(exch, 0, 0, 0, inetping_cb_conn, NULL);
|
---|
| 68 | async_exchange_end(exch);
|
---|
[9749e47] | 69 |
|
---|
[6428115] | 70 | if (rc != EOK) {
|
---|
| 71 | async_hangup(inetping_sess);
|
---|
| 72 | inetping_sess = NULL;
|
---|
| 73 | return rc;
|
---|
| 74 | }
|
---|
[9749e47] | 75 |
|
---|
[6428115] | 76 | return EOK;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | int inetping_send(inetping_sdu_t *sdu)
|
---|
| 80 | {
|
---|
| 81 | async_exch_t *exch = async_exchange_begin(inetping_sess);
|
---|
[9749e47] | 82 |
|
---|
[6428115] | 83 | ipc_call_t answer;
|
---|
[9749e47] | 84 | aid_t req = async_send_1(exch, INETPING_SEND, sdu->seq_no, &answer);
|
---|
| 85 |
|
---|
| 86 | int rc = async_data_write_start(exch, &sdu->src, sizeof(sdu->src));
|
---|
| 87 | if (rc != EOK) {
|
---|
| 88 | async_exchange_end(exch);
|
---|
| 89 | async_forget(req);
|
---|
| 90 | return rc;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | rc = async_data_write_start(exch, &sdu->dest, sizeof(sdu->dest));
|
---|
| 94 | if (rc != EOK) {
|
---|
| 95 | async_exchange_end(exch);
|
---|
| 96 | async_forget(req);
|
---|
| 97 | return rc;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | rc = async_data_write_start(exch, sdu->data, sdu->size);
|
---|
| 101 |
|
---|
[6428115] | 102 | async_exchange_end(exch);
|
---|
[9749e47] | 103 |
|
---|
| 104 | if (rc != EOK) {
|
---|
[50b581d] | 105 | async_forget(req);
|
---|
[9749e47] | 106 | return rc;
|
---|
[6428115] | 107 | }
|
---|
[9749e47] | 108 |
|
---|
| 109 | sysarg_t retval;
|
---|
[6428115] | 110 | async_wait_for(req, &retval);
|
---|
[9749e47] | 111 |
|
---|
| 112 | return (int) retval;
|
---|
[6428115] | 113 | }
|
---|
| 114 |
|
---|
[9749e47] | 115 | int inetping_get_srcaddr(const inet_addr_t *remote, inet_addr_t *local)
|
---|
[6428115] | 116 | {
|
---|
| 117 | async_exch_t *exch = async_exchange_begin(inetping_sess);
|
---|
[9749e47] | 118 |
|
---|
| 119 | ipc_call_t answer;
|
---|
| 120 | aid_t req = async_send_0(exch, INETPING_GET_SRCADDR, &answer);
|
---|
| 121 |
|
---|
| 122 | int rc = async_data_write_start(exch, remote, sizeof(*remote));
|
---|
| 123 | if (rc != EOK) {
|
---|
| 124 | async_exchange_end(exch);
|
---|
| 125 | async_forget(req);
|
---|
[6428115] | 126 | return rc;
|
---|
[9749e47] | 127 | }
|
---|
| 128 |
|
---|
| 129 | ipc_call_t answer_local;
|
---|
| 130 | aid_t req_local = async_data_read(exch, local, sizeof(*local),
|
---|
| 131 | &answer_local);
|
---|
| 132 |
|
---|
| 133 | async_exchange_end(exch);
|
---|
| 134 |
|
---|
| 135 | sysarg_t retval_local;
|
---|
| 136 | async_wait_for(req_local, &retval_local);
|
---|
| 137 |
|
---|
| 138 | if (retval_local != EOK) {
|
---|
| 139 | async_forget(req);
|
---|
| 140 | return (int) retval_local;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | sysarg_t retval;
|
---|
| 144 | async_wait_for(req, &retval);
|
---|
| 145 |
|
---|
| 146 | return (int) retval;
|
---|
[6428115] | 147 | }
|
---|
| 148 |
|
---|
[9749e47] | 149 | static void inetping_ev_recv(ipc_callid_t iid, ipc_call_t *icall)
|
---|
[6428115] | 150 | {
|
---|
| 151 | inetping_sdu_t sdu;
|
---|
[9749e47] | 152 |
|
---|
| 153 | sdu.seq_no = IPC_GET_ARG1(*icall);
|
---|
| 154 |
|
---|
| 155 | ipc_callid_t callid;
|
---|
| 156 | size_t size;
|
---|
| 157 | if (!async_data_write_receive(&callid, &size)) {
|
---|
| 158 | async_answer_0(callid, EREFUSED);
|
---|
| 159 | async_answer_0(iid, EREFUSED);
|
---|
| 160 | return;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | if (size != sizeof(sdu.src)) {
|
---|
| 164 | async_answer_0(callid, EINVAL);
|
---|
| 165 | async_answer_0(iid, EINVAL);
|
---|
| 166 | return;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | int rc = async_data_write_finalize(callid, &sdu.src, size);
|
---|
[6428115] | 170 | if (rc != EOK) {
|
---|
| 171 | async_answer_0(callid, rc);
|
---|
[9749e47] | 172 | async_answer_0(iid, rc);
|
---|
[6428115] | 173 | return;
|
---|
| 174 | }
|
---|
[9749e47] | 175 |
|
---|
| 176 | if (!async_data_write_receive(&callid, &size)) {
|
---|
| 177 | async_answer_0(callid, EREFUSED);
|
---|
| 178 | async_answer_0(iid, EREFUSED);
|
---|
| 179 | return;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | if (size != sizeof(sdu.dest)) {
|
---|
| 183 | async_answer_0(callid, EINVAL);
|
---|
| 184 | async_answer_0(iid, EINVAL);
|
---|
| 185 | return;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | rc = async_data_write_finalize(callid, &sdu.dest, size);
|
---|
| 189 | if (rc != EOK) {
|
---|
| 190 | async_answer_0(callid, rc);
|
---|
| 191 | async_answer_0(iid, rc);
|
---|
| 192 | return;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | rc = async_data_write_accept(&sdu.data, false, 0, 0, 0, &sdu.size);
|
---|
| 196 | if (rc != EOK) {
|
---|
| 197 | async_answer_0(iid, rc);
|
---|
| 198 | return;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[6428115] | 201 | rc = inetping_ev_ops->recv(&sdu);
|
---|
| 202 | free(sdu.data);
|
---|
[9749e47] | 203 | async_answer_0(iid, rc);
|
---|
[6428115] | 204 | }
|
---|
| 205 |
|
---|
| 206 | static void inetping_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
| 207 | {
|
---|
| 208 | while (true) {
|
---|
| 209 | ipc_call_t call;
|
---|
| 210 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 211 |
|
---|
| 212 | if (!IPC_GET_IMETHOD(call)) {
|
---|
| 213 | /* TODO: Handle hangup */
|
---|
| 214 | return;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | switch (IPC_GET_IMETHOD(call)) {
|
---|
| 218 | case INETPING_EV_RECV:
|
---|
| 219 | inetping_ev_recv(callid, &call);
|
---|
| 220 | break;
|
---|
| 221 | default:
|
---|
| 222 | async_answer_0(callid, ENOTSUP);
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | /** @}
|
---|
| 228 | */
|
---|