Get rid of log_log_msg()
[helenos.git] / uspace / srv / net / inetsrv / inetping.c
blob0fd87e6a9d0c3fbb0dd41c60ed9093206287453d
1 /*
2 * Copyright (c) 2012 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /** @addtogroup inet
30 * @{
32 /**
33 * @file
34 * @brief
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>
46 #include "icmp.h"
47 #include "icmp_std.h"
48 #include "inetsrv.h"
49 #include "inetping.h"
51 static FIBRIL_MUTEX_INITIALIZE(client_list_lock);
52 static LIST_INITIALIZE(client_list);
54 /** Last used session identifier. Protected by @c client_list_lock */
55 static uint16_t inetping_ident = 0;
57 static inetping_client_t *inetping_client_find(uint16_t);
59 static int inetping_send(inetping_client_t *client, inetping_sdu_t *sdu)
61 return icmp_ping_send(client->ident, sdu);
64 static int inetping_get_srcaddr(inetping_client_t *client, inet_addr_t *remote,
65 inet_addr_t *local)
67 return inet_get_srcaddr(remote, ICMP_TOS, local);
70 int inetping_recv(uint16_t ident, inetping_sdu_t *sdu)
72 inetping_client_t *client;
73 async_exch_t *exch;
74 ipc_call_t answer;
76 client = inetping_client_find(ident);
77 if (client == NULL) {
78 log_msg(LOG_DEFAULT, LVL_DEBUG, "Unknown ICMP ident. Dropping.");
79 return ENOENT;
82 exch = async_exchange_begin(client->sess);
84 aid_t req = async_send_3(exch, INETPING_EV_RECV, sdu->src.ipv4,
85 sdu->dest.ipv4, sdu->seq_no, &answer);
86 int rc = async_data_write_start(exch, sdu->data, sdu->size);
87 async_exchange_end(exch);
89 if (rc != EOK) {
90 async_forget(req);
91 return rc;
94 sysarg_t retval;
95 async_wait_for(req, &retval);
96 if (retval != EOK) {
97 return retval;
100 return EOK;
103 static void inetping_send_srv(inetping_client_t *client, ipc_callid_t callid,
104 ipc_call_t *call)
106 inetping_sdu_t sdu;
107 int rc;
109 log_msg(LOG_DEFAULT, LVL_DEBUG, "inetping_send_srv()");
111 rc = async_data_write_accept((void **) &sdu.data, false, 0, 0, 0,
112 &sdu.size);
113 if (rc != EOK) {
114 async_answer_0(callid, rc);
115 return;
118 sdu.src.ipv4 = IPC_GET_ARG1(*call);
119 sdu.dest.ipv4 = IPC_GET_ARG2(*call);
120 sdu.seq_no = IPC_GET_ARG3(*call);
122 rc = inetping_send(client, &sdu);
123 free(sdu.data);
125 async_answer_0(callid, rc);
128 static void inetping_get_srcaddr_srv(inetping_client_t *client,
129 ipc_callid_t callid, ipc_call_t *call)
131 inet_addr_t remote;
132 inet_addr_t local;
133 int rc;
135 log_msg(LOG_DEFAULT, LVL_DEBUG, "inetping_get_srcaddr_srv()");
137 remote.ipv4 = IPC_GET_ARG1(*call);
138 local.ipv4 = 0;
140 rc = inetping_get_srcaddr(client, &remote, &local);
141 async_answer_1(callid, rc, local.ipv4);
144 static int inetping_client_init(inetping_client_t *client)
146 async_sess_t *sess = async_callback_receive(EXCHANGE_SERIALIZE);
147 if (sess == NULL)
148 return ENOMEM;
150 client->sess = sess;
151 link_initialize(&client->client_list);
153 fibril_mutex_lock(&client_list_lock);
154 client->ident = ++inetping_ident;
155 list_append(&client->client_list, &client_list);
156 fibril_mutex_unlock(&client_list_lock);
158 return EOK;
161 static void inetping_client_fini(inetping_client_t *client)
163 async_hangup(client->sess);
164 client->sess = NULL;
166 fibril_mutex_lock(&client_list_lock);
167 list_remove(&client->client_list);
168 fibril_mutex_unlock(&client_list_lock);
171 static inetping_client_t *inetping_client_find(uint16_t ident)
173 fibril_mutex_lock(&client_list_lock);
175 list_foreach(client_list, link) {
176 inetping_client_t *client = list_get_instance(link,
177 inetping_client_t, client_list);
179 if (client->ident == ident) {
180 fibril_mutex_unlock(&client_list_lock);
181 return client;
185 fibril_mutex_unlock(&client_list_lock);
186 return NULL;
189 void inetping_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
191 inetping_client_t client;
192 int rc;
194 log_msg(LOG_DEFAULT, LVL_DEBUG, "inetping_conn()");
196 /* Accept the connection */
197 async_answer_0(iid, EOK);
199 rc = inetping_client_init(&client);
200 if (rc != EOK)
201 return;
203 while (true) {
204 ipc_call_t call;
205 ipc_callid_t callid = async_get_call(&call);
206 sysarg_t method = IPC_GET_IMETHOD(call);
208 if (!method) {
209 /* The other side has hung up */
210 async_answer_0(callid, EOK);
211 break;
214 switch (method) {
215 case INETPING_SEND:
216 inetping_send_srv(&client, callid, &call);
217 break;
218 case INETPING_GET_SRCADDR:
219 inetping_get_srcaddr_srv(&client, callid, &call);
220 break;
221 default:
222 async_answer_0(callid, EINVAL);
226 inetping_client_fini(&client);
229 /** @}