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