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