[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 |
|
---|
[984a9ba] | 40 | static void inetping_cb_conn(ipc_call_t *, void *);
|
---|
| 41 | static void inetping_ev_recv(ipc_call_t *);
|
---|
[6428115] | 42 |
|
---|
| 43 | static async_sess_t *inetping_sess = NULL;
|
---|
| 44 | static inetping_ev_ops_t *inetping_ev_ops;
|
---|
| 45 |
|
---|
[b7fd2a0] | 46 | errno_t inetping_init(inetping_ev_ops_t *ev_ops)
|
---|
[6428115] | 47 | {
|
---|
| 48 | service_id_t inetping_svc;
|
---|
[b7fd2a0] | 49 | errno_t rc;
|
---|
[6428115] | 50 |
|
---|
| 51 | assert(inetping_sess == NULL);
|
---|
[9749e47] | 52 |
|
---|
[6428115] | 53 | inetping_ev_ops = ev_ops;
|
---|
[9749e47] | 54 |
|
---|
[f9b2cb4c] | 55 | rc = loc_service_get_id(SERVICE_NAME_INET, &inetping_svc,
|
---|
[5353f50] | 56 | IPC_AUTOSTART);
|
---|
[6428115] | 57 | if (rc != EOK)
|
---|
| 58 | return ENOENT;
|
---|
[9749e47] | 59 |
|
---|
[f9b2cb4c] | 60 | inetping_sess = loc_service_connect(inetping_svc, INTERFACE_INETPING,
|
---|
[5353f50] | 61 | IPC_AUTOSTART);
|
---|
| 62 |
|
---|
[6428115] | 63 | if (inetping_sess == NULL)
|
---|
| 64 | return ENOENT;
|
---|
[9749e47] | 65 |
|
---|
[6428115] | 66 | async_exch_t *exch = async_exchange_begin(inetping_sess);
|
---|
| 67 |
|
---|
[f9b2cb4c] | 68 | port_id_t port;
|
---|
| 69 | rc = async_create_callback_port(exch, INTERFACE_INETPING_CB, 0, 0,
|
---|
| 70 | inetping_cb_conn, NULL, &port);
|
---|
[a35b458] | 71 |
|
---|
[6428115] | 72 | async_exchange_end(exch);
|
---|
[9749e47] | 73 |
|
---|
[6428115] | 74 | if (rc != EOK) {
|
---|
| 75 | async_hangup(inetping_sess);
|
---|
| 76 | inetping_sess = NULL;
|
---|
| 77 | return rc;
|
---|
| 78 | }
|
---|
[9749e47] | 79 |
|
---|
[6428115] | 80 | return EOK;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[b7fd2a0] | 83 | errno_t inetping_send(inetping_sdu_t *sdu)
|
---|
[6428115] | 84 | {
|
---|
| 85 | async_exch_t *exch = async_exchange_begin(inetping_sess);
|
---|
[9749e47] | 86 |
|
---|
[6428115] | 87 | ipc_call_t answer;
|
---|
[9749e47] | 88 | aid_t req = async_send_1(exch, INETPING_SEND, sdu->seq_no, &answer);
|
---|
| 89 |
|
---|
[b7fd2a0] | 90 | errno_t rc = async_data_write_start(exch, &sdu->src, sizeof(sdu->src));
|
---|
[9749e47] | 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(sdu->dest));
|
---|
| 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);
|
---|
| 105 |
|
---|
[6428115] | 106 | async_exchange_end(exch);
|
---|
[9749e47] | 107 |
|
---|
| 108 | if (rc != EOK) {
|
---|
[50b581d] | 109 | async_forget(req);
|
---|
[9749e47] | 110 | return rc;
|
---|
[6428115] | 111 | }
|
---|
[9749e47] | 112 |
|
---|
[b7fd2a0] | 113 | errno_t retval;
|
---|
[6428115] | 114 | async_wait_for(req, &retval);
|
---|
[9749e47] | 115 |
|
---|
[25a179e] | 116 | return retval;
|
---|
[6428115] | 117 | }
|
---|
| 118 |
|
---|
[b7fd2a0] | 119 | errno_t inetping_get_srcaddr(const inet_addr_t *remote, inet_addr_t *local)
|
---|
[6428115] | 120 | {
|
---|
| 121 | async_exch_t *exch = async_exchange_begin(inetping_sess);
|
---|
[9749e47] | 122 |
|
---|
| 123 | ipc_call_t answer;
|
---|
| 124 | aid_t req = async_send_0(exch, INETPING_GET_SRCADDR, &answer);
|
---|
| 125 |
|
---|
[b7fd2a0] | 126 | errno_t rc = async_data_write_start(exch, remote, sizeof(*remote));
|
---|
[9749e47] | 127 | if (rc != EOK) {
|
---|
| 128 | async_exchange_end(exch);
|
---|
| 129 | async_forget(req);
|
---|
[6428115] | 130 | return rc;
|
---|
[9749e47] | 131 | }
|
---|
| 132 |
|
---|
| 133 | ipc_call_t answer_local;
|
---|
| 134 | aid_t req_local = async_data_read(exch, local, sizeof(*local),
|
---|
| 135 | &answer_local);
|
---|
| 136 |
|
---|
| 137 | async_exchange_end(exch);
|
---|
| 138 |
|
---|
[b7fd2a0] | 139 | errno_t retval_local;
|
---|
[9749e47] | 140 | async_wait_for(req_local, &retval_local);
|
---|
| 141 |
|
---|
| 142 | if (retval_local != EOK) {
|
---|
| 143 | async_forget(req);
|
---|
[25a179e] | 144 | return retval_local;
|
---|
[9749e47] | 145 | }
|
---|
| 146 |
|
---|
[b7fd2a0] | 147 | errno_t retval;
|
---|
[9749e47] | 148 | async_wait_for(req, &retval);
|
---|
| 149 |
|
---|
[25a179e] | 150 | return retval;
|
---|
[6428115] | 151 | }
|
---|
| 152 |
|
---|
[984a9ba] | 153 | static void inetping_ev_recv(ipc_call_t *icall)
|
---|
[6428115] | 154 | {
|
---|
| 155 | inetping_sdu_t sdu;
|
---|
[9749e47] | 156 |
|
---|
[fafb8e5] | 157 | sdu.seq_no = ipc_get_arg1(icall);
|
---|
[9749e47] | 158 |
|
---|
[984a9ba] | 159 | ipc_call_t call;
|
---|
[9749e47] | 160 | size_t size;
|
---|
[984a9ba] | 161 | if (!async_data_write_receive(&call, &size)) {
|
---|
| 162 | async_answer_0(&call, EREFUSED);
|
---|
| 163 | async_answer_0(icall, EREFUSED);
|
---|
[9749e47] | 164 | return;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | if (size != sizeof(sdu.src)) {
|
---|
[984a9ba] | 168 | async_answer_0(&call, EINVAL);
|
---|
| 169 | async_answer_0(icall, EINVAL);
|
---|
[9749e47] | 170 | return;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[984a9ba] | 173 | errno_t rc = async_data_write_finalize(&call, &sdu.src, size);
|
---|
[6428115] | 174 | if (rc != EOK) {
|
---|
[984a9ba] | 175 | async_answer_0(&call, rc);
|
---|
| 176 | async_answer_0(icall, rc);
|
---|
[6428115] | 177 | return;
|
---|
| 178 | }
|
---|
[9749e47] | 179 |
|
---|
[984a9ba] | 180 | if (!async_data_write_receive(&call, &size)) {
|
---|
| 181 | async_answer_0(&call, EREFUSED);
|
---|
| 182 | async_answer_0(icall, EREFUSED);
|
---|
[9749e47] | 183 | return;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | if (size != sizeof(sdu.dest)) {
|
---|
[984a9ba] | 187 | async_answer_0(&call, EINVAL);
|
---|
| 188 | async_answer_0(icall, EINVAL);
|
---|
[9749e47] | 189 | return;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[984a9ba] | 192 | rc = async_data_write_finalize(&call, &sdu.dest, size);
|
---|
[9749e47] | 193 | if (rc != EOK) {
|
---|
[984a9ba] | 194 | async_answer_0(&call, rc);
|
---|
| 195 | async_answer_0(icall, rc);
|
---|
[9749e47] | 196 | return;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | rc = async_data_write_accept(&sdu.data, false, 0, 0, 0, &sdu.size);
|
---|
| 200 | if (rc != EOK) {
|
---|
[984a9ba] | 201 | async_answer_0(icall, rc);
|
---|
[9749e47] | 202 | return;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
[6428115] | 205 | rc = inetping_ev_ops->recv(&sdu);
|
---|
| 206 | free(sdu.data);
|
---|
[984a9ba] | 207 | async_answer_0(icall, rc);
|
---|
[6428115] | 208 | }
|
---|
| 209 |
|
---|
[984a9ba] | 210 | static void inetping_cb_conn(ipc_call_t *icall, void *arg)
|
---|
[6428115] | 211 | {
|
---|
| 212 | while (true) {
|
---|
| 213 | ipc_call_t call;
|
---|
[984a9ba] | 214 | async_get_call(&call);
|
---|
[6428115] | 215 |
|
---|
[fafb8e5] | 216 | if (!ipc_get_imethod(&call)) {
|
---|
[889cdb1] | 217 | async_answer_0(&call, EOK);
|
---|
[6428115] | 218 | return;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
[fafb8e5] | 221 | switch (ipc_get_imethod(&call)) {
|
---|
[6428115] | 222 | case INETPING_EV_RECV:
|
---|
[984a9ba] | 223 | inetping_ev_recv(&call);
|
---|
[6428115] | 224 | break;
|
---|
| 225 | default:
|
---|
[984a9ba] | 226 | async_answer_0(&call, ENOTSUP);
|
---|
[6428115] | 227 | }
|
---|
| 228 | }
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | /** @}
|
---|
| 232 | */
|
---|