2 * Copyright (c) 2013 Jiri Svoboda
3 * Copyright (c) 2013 Martin Decky
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
33 #include <inet/inetping.h>
35 #include <ipc/services.h>
40 static void inetping_cb_conn(ipc_callid_t
, ipc_call_t
*, void *);
41 static void inetping_ev_recv(ipc_callid_t
, ipc_call_t
*);
43 static async_sess_t
*inetping_sess
= NULL
;
44 static inetping_ev_ops_t
*inetping_ev_ops
;
46 errno_t
inetping_init(inetping_ev_ops_t
*ev_ops
)
48 service_id_t inetping_svc
;
51 assert(inetping_sess
== NULL
);
53 inetping_ev_ops
= ev_ops
;
55 rc
= loc_service_get_id(SERVICE_NAME_INET
, &inetping_svc
,
60 inetping_sess
= loc_service_connect(inetping_svc
, INTERFACE_INETPING
,
62 if (inetping_sess
== NULL
)
65 async_exch_t
*exch
= async_exchange_begin(inetping_sess
);
68 rc
= async_create_callback_port(exch
, INTERFACE_INETPING_CB
, 0, 0,
69 inetping_cb_conn
, NULL
, &port
);
71 async_exchange_end(exch
);
74 async_hangup(inetping_sess
);
82 errno_t
inetping_send(inetping_sdu_t
*sdu
)
84 async_exch_t
*exch
= async_exchange_begin(inetping_sess
);
87 aid_t req
= async_send_1(exch
, INETPING_SEND
, sdu
->seq_no
, &answer
);
89 errno_t rc
= async_data_write_start(exch
, &sdu
->src
, sizeof(sdu
->src
));
91 async_exchange_end(exch
);
96 rc
= async_data_write_start(exch
, &sdu
->dest
, sizeof(sdu
->dest
));
98 async_exchange_end(exch
);
103 rc
= async_data_write_start(exch
, sdu
->data
, sdu
->size
);
105 async_exchange_end(exch
);
113 async_wait_for(req
, &retval
);
118 errno_t
inetping_get_srcaddr(const inet_addr_t
*remote
, inet_addr_t
*local
)
120 async_exch_t
*exch
= async_exchange_begin(inetping_sess
);
123 aid_t req
= async_send_0(exch
, INETPING_GET_SRCADDR
, &answer
);
125 errno_t rc
= async_data_write_start(exch
, remote
, sizeof(*remote
));
127 async_exchange_end(exch
);
132 ipc_call_t answer_local
;
133 aid_t req_local
= async_data_read(exch
, local
, sizeof(*local
),
136 async_exchange_end(exch
);
138 errno_t retval_local
;
139 async_wait_for(req_local
, &retval_local
);
141 if (retval_local
!= EOK
) {
147 async_wait_for(req
, &retval
);
152 static void inetping_ev_recv(ipc_callid_t iid
, ipc_call_t
*icall
)
156 sdu
.seq_no
= IPC_GET_ARG1(*icall
);
160 if (!async_data_write_receive(&callid
, &size
)) {
161 async_answer_0(callid
, EREFUSED
);
162 async_answer_0(iid
, EREFUSED
);
166 if (size
!= sizeof(sdu
.src
)) {
167 async_answer_0(callid
, EINVAL
);
168 async_answer_0(iid
, EINVAL
);
172 errno_t rc
= async_data_write_finalize(callid
, &sdu
.src
, size
);
174 async_answer_0(callid
, rc
);
175 async_answer_0(iid
, rc
);
179 if (!async_data_write_receive(&callid
, &size
)) {
180 async_answer_0(callid
, EREFUSED
);
181 async_answer_0(iid
, EREFUSED
);
185 if (size
!= sizeof(sdu
.dest
)) {
186 async_answer_0(callid
, EINVAL
);
187 async_answer_0(iid
, EINVAL
);
191 rc
= async_data_write_finalize(callid
, &sdu
.dest
, size
);
193 async_answer_0(callid
, rc
);
194 async_answer_0(iid
, rc
);
198 rc
= async_data_write_accept(&sdu
.data
, false, 0, 0, 0, &sdu
.size
);
200 async_answer_0(iid
, rc
);
204 rc
= inetping_ev_ops
->recv(&sdu
);
206 async_answer_0(iid
, rc
);
209 static void inetping_cb_conn(ipc_callid_t iid
, ipc_call_t
*icall
, void *arg
)
213 ipc_callid_t callid
= async_get_call(&call
);
215 if (!IPC_GET_IMETHOD(call
)) {
216 /* TODO: Handle hangup */
220 switch (IPC_GET_IMETHOD(call
)) {
221 case INETPING_EV_RECV
:
222 inetping_ev_recv(callid
, &call
);
225 async_answer_0(callid
, ENOTSUP
);