| 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 inet
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * @brief
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <async.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <fibril_synch.h>
|
|---|
| 40 | #include <io/log.h>
|
|---|
| 41 | #include <ipc/inet.h>
|
|---|
| 42 | #include <loc.h>
|
|---|
| 43 | #include <stdlib.h>
|
|---|
| 44 | #include <sys/types.h>
|
|---|
| 45 | #include <net/socket_codes.h>
|
|---|
| 46 | #include "icmp.h"
|
|---|
| 47 | #include "icmp_std.h"
|
|---|
| 48 | #include "inetsrv.h"
|
|---|
| 49 | #include "inetping.h"
|
|---|
| 50 |
|
|---|
| 51 | static FIBRIL_MUTEX_INITIALIZE(client_list_lock);
|
|---|
| 52 | static LIST_INITIALIZE(client_list);
|
|---|
| 53 |
|
|---|
| 54 | /** Last used session identifier. Protected by @c client_list_lock */
|
|---|
| 55 | static uint16_t inetping_ident = 0;
|
|---|
| 56 |
|
|---|
| 57 | static int inetping_send(inetping_client_t *client, inetping_sdu_t *sdu)
|
|---|
| 58 | {
|
|---|
| 59 | return icmp_ping_send(client->ident, sdu);
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | static int inetping_get_srcaddr(inetping_client_t *client, addr32_t remote,
|
|---|
| 63 | addr32_t *local)
|
|---|
| 64 | {
|
|---|
| 65 | inet_addr_t remote_addr;
|
|---|
| 66 | inet_addr_set(remote, &remote_addr);
|
|---|
| 67 |
|
|---|
| 68 | inet_addr_t local_addr;
|
|---|
| 69 | int rc = inet_get_srcaddr(&remote_addr, ICMP_TOS, &local_addr);
|
|---|
| 70 | if (rc != EOK)
|
|---|
| 71 | return rc;
|
|---|
| 72 |
|
|---|
| 73 | uint16_t family = inet_addr_get(&local_addr, local, NULL);
|
|---|
| 74 | if (family != AF_INET)
|
|---|
| 75 | return EINVAL;
|
|---|
| 76 |
|
|---|
| 77 | return EOK;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | static inetping_client_t *inetping_client_find(uint16_t ident)
|
|---|
| 81 | {
|
|---|
| 82 | fibril_mutex_lock(&client_list_lock);
|
|---|
| 83 |
|
|---|
| 84 | list_foreach(client_list, link) {
|
|---|
| 85 | inetping_client_t *client = list_get_instance(link,
|
|---|
| 86 | inetping_client_t, client_list);
|
|---|
| 87 |
|
|---|
| 88 | if (client->ident == ident) {
|
|---|
| 89 | fibril_mutex_unlock(&client_list_lock);
|
|---|
| 90 | return client;
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | fibril_mutex_unlock(&client_list_lock);
|
|---|
| 95 | return NULL;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | int inetping_recv(uint16_t ident, inetping_sdu_t *sdu)
|
|---|
| 99 | {
|
|---|
| 100 | inetping_client_t *client = inetping_client_find(ident);
|
|---|
| 101 | if (client == NULL) {
|
|---|
| 102 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Unknown ICMP ident. Dropping.");
|
|---|
| 103 | return ENOENT;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | async_exch_t *exch = async_exchange_begin(client->sess);
|
|---|
| 107 |
|
|---|
| 108 | ipc_call_t answer;
|
|---|
| 109 | aid_t req = async_send_3(exch, INETPING_EV_RECV, (sysarg_t) sdu->src,
|
|---|
| 110 | (sysarg_t) sdu->dest, sdu->seq_no, &answer);
|
|---|
| 111 | int rc = async_data_write_start(exch, sdu->data, sdu->size);
|
|---|
| 112 |
|
|---|
| 113 | async_exchange_end(exch);
|
|---|
| 114 |
|
|---|
| 115 | if (rc != EOK) {
|
|---|
| 116 | async_forget(req);
|
|---|
| 117 | return rc;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | sysarg_t retval;
|
|---|
| 121 | async_wait_for(req, &retval);
|
|---|
| 122 |
|
|---|
| 123 | return (int) retval;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | static void inetping_send_srv(inetping_client_t *client, ipc_callid_t callid,
|
|---|
| 127 | ipc_call_t *call)
|
|---|
| 128 | {
|
|---|
| 129 | inetping_sdu_t sdu;
|
|---|
| 130 | int rc;
|
|---|
| 131 |
|
|---|
| 132 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inetping_send_srv()");
|
|---|
| 133 |
|
|---|
| 134 | rc = async_data_write_accept((void **) &sdu.data, false, 0, 0, 0,
|
|---|
| 135 | &sdu.size);
|
|---|
| 136 | if (rc != EOK) {
|
|---|
| 137 | async_answer_0(callid, rc);
|
|---|
| 138 | return;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | sdu.src = IPC_GET_ARG1(*call);
|
|---|
| 142 | sdu.dest = IPC_GET_ARG2(*call);
|
|---|
| 143 | sdu.seq_no = IPC_GET_ARG3(*call);
|
|---|
| 144 |
|
|---|
| 145 | rc = inetping_send(client, &sdu);
|
|---|
| 146 | free(sdu.data);
|
|---|
| 147 |
|
|---|
| 148 | async_answer_0(callid, rc);
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | static void inetping_get_srcaddr_srv(inetping_client_t *client,
|
|---|
| 152 | ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 153 | {
|
|---|
| 154 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inetping_get_srcaddr_srv()");
|
|---|
| 155 |
|
|---|
| 156 | uint32_t remote = IPC_GET_ARG1(*call);
|
|---|
| 157 | uint32_t local = 0;
|
|---|
| 158 |
|
|---|
| 159 | int rc = inetping_get_srcaddr(client, remote, &local);
|
|---|
| 160 | async_answer_1(callid, rc, (sysarg_t) local);
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | static int inetping_client_init(inetping_client_t *client)
|
|---|
| 164 | {
|
|---|
| 165 | async_sess_t *sess = async_callback_receive(EXCHANGE_SERIALIZE);
|
|---|
| 166 | if (sess == NULL)
|
|---|
| 167 | return ENOMEM;
|
|---|
| 168 |
|
|---|
| 169 | client->sess = sess;
|
|---|
| 170 | link_initialize(&client->client_list);
|
|---|
| 171 |
|
|---|
| 172 | fibril_mutex_lock(&client_list_lock);
|
|---|
| 173 | client->ident = ++inetping_ident;
|
|---|
| 174 | list_append(&client->client_list, &client_list);
|
|---|
| 175 | fibril_mutex_unlock(&client_list_lock);
|
|---|
| 176 |
|
|---|
| 177 | return EOK;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | static void inetping_client_fini(inetping_client_t *client)
|
|---|
| 181 | {
|
|---|
| 182 | async_hangup(client->sess);
|
|---|
| 183 | client->sess = NULL;
|
|---|
| 184 |
|
|---|
| 185 | fibril_mutex_lock(&client_list_lock);
|
|---|
| 186 | list_remove(&client->client_list);
|
|---|
| 187 | fibril_mutex_unlock(&client_list_lock);
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | void inetping_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
|---|
| 191 | {
|
|---|
| 192 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inetping_conn()");
|
|---|
| 193 |
|
|---|
| 194 | /* Accept the connection */
|
|---|
| 195 | async_answer_0(iid, EOK);
|
|---|
| 196 |
|
|---|
| 197 | inetping_client_t client;
|
|---|
| 198 | int rc = inetping_client_init(&client);
|
|---|
| 199 | if (rc != EOK)
|
|---|
| 200 | return;
|
|---|
| 201 |
|
|---|
| 202 | while (true) {
|
|---|
| 203 | ipc_call_t call;
|
|---|
| 204 | ipc_callid_t callid = async_get_call(&call);
|
|---|
| 205 | sysarg_t method = IPC_GET_IMETHOD(call);
|
|---|
| 206 |
|
|---|
| 207 | if (!method) {
|
|---|
| 208 | /* The other side has hung up */
|
|---|
| 209 | async_answer_0(callid, EOK);
|
|---|
| 210 | break;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | switch (method) {
|
|---|
| 214 | case INETPING_SEND:
|
|---|
| 215 | inetping_send_srv(&client, callid, &call);
|
|---|
| 216 | break;
|
|---|
| 217 | case INETPING_GET_SRCADDR:
|
|---|
| 218 | inetping_get_srcaddr_srv(&client, callid, &call);
|
|---|
| 219 | break;
|
|---|
| 220 | default:
|
|---|
| 221 | async_answer_0(callid, EINVAL);
|
|---|
| 222 | }
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | inetping_client_fini(&client);
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | /** @}
|
|---|
| 229 | */
|
|---|