source: mainline/uspace/srv/net/inetsrv/inetping.c

Last change on this file was fafb8e5, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 6 years ago

Mechanically lowercase IPC_SET_*/IPC_GET_*

  • Property mode set to 100644
File size: 7.4 KB
RevLine 
[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/** @addtogroup inet
31 * @{
32 */
33/**
34 * @file
35 * @brief
36 */
37
38#include <async.h>
39#include <errno.h>
40#include <fibril_synch.h>
41#include <io/log.h>
42#include <ipc/inet.h>
43#include <loc.h>
44#include <stdlib.h>
[8d2dd7f2]45#include <stddef.h>
46#include <stdint.h>
[13be2583]47#include <types/inetping.h>
[6428115]48#include "icmp.h"
[9749e47]49#include "icmpv6.h"
[6428115]50#include "icmp_std.h"
[b4ec1ea]51#include "inetsrv.h"
[6428115]52#include "inetping.h"
53
54static FIBRIL_MUTEX_INITIALIZE(client_list_lock);
55static LIST_INITIALIZE(client_list);
56
57/** Last used session identifier. Protected by @c client_list_lock */
58static uint16_t inetping_ident = 0;
59
[b7fd2a0]60static errno_t inetping_send(inetping_client_t *client, inetping_sdu_t *sdu)
[6428115]61{
[9749e47]62 if (sdu->src.version != sdu->dest.version)
63 return EINVAL;
64
65 switch (sdu->src.version) {
66 case ip_v4:
67 return icmp_ping_send(client->ident, sdu);
68 case ip_v6:
69 return icmpv6_ping_send(client->ident, sdu);
70 default:
71 return EINVAL;
72 }
[6428115]73}
74
[b7fd2a0]75static errno_t inetping_get_srcaddr(inetping_client_t *client,
[9749e47]76 inet_addr_t *remote, inet_addr_t *local)
[6428115]77{
[9749e47]78 return inet_get_srcaddr(remote, ICMP_TOS, local);
[6428115]79}
80
[02a09ed]81static inetping_client_t *inetping_client_find(uint16_t ident)
[6428115]82{
[02a09ed]83 fibril_mutex_lock(&client_list_lock);
[9749e47]84
[feeac0d]85 list_foreach(client_list, client_list, inetping_client_t, client) {
[02a09ed]86 if (client->ident == ident) {
87 fibril_mutex_unlock(&client_list_lock);
88 return client;
89 }
90 }
[9749e47]91
[02a09ed]92 fibril_mutex_unlock(&client_list_lock);
93 return NULL;
94}
[6428115]95
[b7fd2a0]96errno_t inetping_recv(uint16_t ident, inetping_sdu_t *sdu)
[02a09ed]97{
98 inetping_client_t *client = inetping_client_find(ident);
[6428115]99 if (client == NULL) {
[a1a101d]100 log_msg(LOG_DEFAULT, LVL_DEBUG, "Unknown ICMP ident. Dropping.");
[6428115]101 return ENOENT;
102 }
[9749e47]103
[02a09ed]104 async_exch_t *exch = async_exchange_begin(client->sess);
[9749e47]105
[02a09ed]106 ipc_call_t answer;
[9749e47]107 aid_t req = async_send_1(exch, INETPING_EV_RECV, sdu->seq_no, &answer);
108
[b7fd2a0]109 errno_t rc = async_data_write_start(exch, &sdu->src, sizeof(sdu->src));
[9749e47]110 if (rc != EOK) {
111 async_exchange_end(exch);
112 async_forget(req);
113 return rc;
114 }
115
116 rc = async_data_write_start(exch, &sdu->dest, sizeof(sdu->dest));
117 if (rc != EOK) {
118 async_exchange_end(exch);
119 async_forget(req);
120 return rc;
121 }
122
123 rc = async_data_write_start(exch, sdu->data, sdu->size);
124
[6428115]125 async_exchange_end(exch);
[9749e47]126
[6428115]127 if (rc != EOK) {
[50b581d]128 async_forget(req);
[6428115]129 return rc;
130 }
[9749e47]131
[b7fd2a0]132 errno_t retval;
[6428115]133 async_wait_for(req, &retval);
[9749e47]134
[25a179e]135 return retval;
[6428115]136}
137
[984a9ba]138static void inetping_send_srv(inetping_client_t *client, ipc_call_t *icall)
[6428115]139{
[9749e47]140 log_msg(LOG_DEFAULT, LVL_DEBUG, "inetping_send_srv()");
141
[6428115]142 inetping_sdu_t sdu;
[b7fd2a0]143 errno_t rc;
[6428115]144
[fafb8e5]145 sdu.seq_no = ipc_get_arg1(icall);
[6428115]146
[984a9ba]147 ipc_call_t call;
[9749e47]148 size_t size;
[984a9ba]149 if (!async_data_write_receive(&call, &size)) {
150 async_answer_0(&call, EREFUSED);
151 async_answer_0(icall, EREFUSED);
[9749e47]152 return;
153 }
154
155 if (size != sizeof(sdu.src)) {
[984a9ba]156 async_answer_0(&call, EINVAL);
157 async_answer_0(icall, EINVAL);
[9749e47]158 return;
159 }
160
[984a9ba]161 rc = async_data_write_finalize(&call, &sdu.src, size);
[6428115]162 if (rc != EOK) {
[984a9ba]163 async_answer_0(&call, rc);
164 async_answer_0(icall, rc);
[6428115]165 return;
166 }
167
[984a9ba]168 if (!async_data_write_receive(&call, &size)) {
169 async_answer_0(&call, EREFUSED);
170 async_answer_0(icall, EREFUSED);
[9749e47]171 return;
172 }
173
174 if (size != sizeof(sdu.dest)) {
[984a9ba]175 async_answer_0(&call, EINVAL);
176 async_answer_0(icall, EINVAL);
[9749e47]177 return;
178 }
179
[984a9ba]180 rc = async_data_write_finalize(&call, &sdu.dest, size);
[9749e47]181 if (rc != EOK) {
[984a9ba]182 async_answer_0(&call, rc);
183 async_answer_0(icall, rc);
[9749e47]184 return;
185 }
186
187 rc = async_data_write_accept((void **) &sdu.data, false, 0, 0, 0,
188 &sdu.size);
189 if (rc != EOK) {
[984a9ba]190 async_answer_0(icall, rc);
[9749e47]191 return;
192 }
[6428115]193
194 rc = inetping_send(client, &sdu);
195 free(sdu.data);
196
[984a9ba]197 async_answer_0(icall, rc);
[6428115]198}
199
[984a9ba]200static void inetping_get_srcaddr_srv(inetping_client_t *client, ipc_call_t *icall)
[6428115]201{
[a1a101d]202 log_msg(LOG_DEFAULT, LVL_DEBUG, "inetping_get_srcaddr_srv()");
[9749e47]203
[984a9ba]204 ipc_call_t call;
[9749e47]205 size_t size;
206
207 inet_addr_t local;
208 inet_addr_t remote;
209
[984a9ba]210 if (!async_data_write_receive(&call, &size)) {
211 async_answer_0(&call, EREFUSED);
212 async_answer_0(icall, EREFUSED);
[9749e47]213 return;
214 }
215
216 if (size != sizeof(remote)) {
[984a9ba]217 async_answer_0(&call, EINVAL);
218 async_answer_0(icall, EINVAL);
[9749e47]219 return;
220 }
221
[984a9ba]222 errno_t rc = async_data_write_finalize(&call, &remote, size);
[9749e47]223 if (rc != EOK) {
[984a9ba]224 async_answer_0(&call, rc);
225 async_answer_0(icall, rc);
[9749e47]226 return;
227 }
228
229 rc = inetping_get_srcaddr(client, &remote, &local);
230 if (rc != EOK) {
[984a9ba]231 async_answer_0(icall, rc);
[9749e47]232 return;
233 }
234
[984a9ba]235 if (!async_data_read_receive(&call, &size)) {
236 async_answer_0(&call, EREFUSED);
237 async_answer_0(icall, EREFUSED);
[9749e47]238 return;
239 }
240
241 if (size != sizeof(local)) {
[984a9ba]242 async_answer_0(&call, EINVAL);
243 async_answer_0(icall, EINVAL);
[9749e47]244 return;
245 }
246
[984a9ba]247 rc = async_data_read_finalize(&call, &local, size);
[9749e47]248 if (rc != EOK)
[984a9ba]249 async_answer_0(&call, rc);
[9749e47]250
[984a9ba]251 async_answer_0(icall, rc);
[6428115]252}
253
[b7fd2a0]254static errno_t inetping_client_init(inetping_client_t *client)
[6428115]255{
256 async_sess_t *sess = async_callback_receive(EXCHANGE_SERIALIZE);
257 if (sess == NULL)
258 return ENOMEM;
[9749e47]259
[6428115]260 client->sess = sess;
261 link_initialize(&client->client_list);
[9749e47]262
[6428115]263 fibril_mutex_lock(&client_list_lock);
264 client->ident = ++inetping_ident;
265 list_append(&client->client_list, &client_list);
266 fibril_mutex_unlock(&client_list_lock);
[9749e47]267
[6428115]268 return EOK;
269}
270
271static void inetping_client_fini(inetping_client_t *client)
272{
273 async_hangup(client->sess);
274 client->sess = NULL;
[9749e47]275
[6428115]276 fibril_mutex_lock(&client_list_lock);
277 list_remove(&client->client_list);
278 fibril_mutex_unlock(&client_list_lock);
279}
280
[984a9ba]281void inetping_conn(ipc_call_t *icall, void *arg)
[6428115]282{
[a1a101d]283 log_msg(LOG_DEFAULT, LVL_DEBUG, "inetping_conn()");
[9749e47]284
[6428115]285 /* Accept the connection */
[beb83c1]286 async_accept_0(icall);
[9749e47]287
[02a09ed]288 inetping_client_t client;
[b7fd2a0]289 errno_t rc = inetping_client_init(&client);
[6428115]290 if (rc != EOK)
291 return;
[9749e47]292
[6428115]293 while (true) {
294 ipc_call_t call;
[984a9ba]295 async_get_call(&call);
[fafb8e5]296 sysarg_t method = ipc_get_imethod(&call);
[9749e47]297
[6428115]298 if (!method) {
299 /* The other side has hung up */
[984a9ba]300 async_answer_0(&call, EOK);
[6428115]301 break;
302 }
[9749e47]303
[6428115]304 switch (method) {
305 case INETPING_SEND:
[984a9ba]306 inetping_send_srv(&client, &call);
[6428115]307 break;
308 case INETPING_GET_SRCADDR:
[984a9ba]309 inetping_get_srcaddr_srv(&client, &call);
[6428115]310 break;
311 default:
[984a9ba]312 async_answer_0(&call, EINVAL);
[6428115]313 }
314 }
[9749e47]315
[6428115]316 inetping_client_fini(&client);
317}
318
319/** @}
320 */
Note: See TracBrowser for help on using the repository browser.