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 | #include <async.h>
|
---|
30 | #include <assert.h>
|
---|
31 | #include <errno.h>
|
---|
32 | #include <inet/inetping.h>
|
---|
33 | #include <ipc/inet.h>
|
---|
34 | #include <ipc/services.h>
|
---|
35 | #include <loc.h>
|
---|
36 | #include <stdlib.h>
|
---|
37 | #include <str.h>
|
---|
38 |
|
---|
39 | static void inetping_cb_conn(ipc_callid_t, ipc_call_t *, void *);
|
---|
40 | static void inetping_ev_recv(ipc_callid_t, ipc_call_t *);
|
---|
41 |
|
---|
42 | static async_sess_t *inetping_sess = NULL;
|
---|
43 | static inetping_ev_ops_t *inetping_ev_ops;
|
---|
44 |
|
---|
45 | int inetping_init(inetping_ev_ops_t *ev_ops)
|
---|
46 | {
|
---|
47 | service_id_t inetping_svc;
|
---|
48 | int rc;
|
---|
49 |
|
---|
50 | assert(inetping_sess == NULL);
|
---|
51 |
|
---|
52 | inetping_ev_ops = ev_ops;
|
---|
53 |
|
---|
54 | rc = loc_service_get_id(SERVICE_NAME_INETPING, &inetping_svc,
|
---|
55 | IPC_FLAG_BLOCKING);
|
---|
56 | if (rc != EOK)
|
---|
57 | return ENOENT;
|
---|
58 |
|
---|
59 | inetping_sess = loc_service_connect(EXCHANGE_SERIALIZE, inetping_svc,
|
---|
60 | IPC_FLAG_BLOCKING);
|
---|
61 | if (inetping_sess == NULL)
|
---|
62 | return ENOENT;
|
---|
63 |
|
---|
64 | async_exch_t *exch = async_exchange_begin(inetping_sess);
|
---|
65 |
|
---|
66 | rc = async_connect_to_me(exch, 0, 0, 0, inetping_cb_conn, NULL);
|
---|
67 | async_exchange_end(exch);
|
---|
68 |
|
---|
69 | if (rc != EOK) {
|
---|
70 | async_hangup(inetping_sess);
|
---|
71 | inetping_sess = NULL;
|
---|
72 | return rc;
|
---|
73 | }
|
---|
74 |
|
---|
75 | return EOK;
|
---|
76 | }
|
---|
77 |
|
---|
78 | int inetping_send(inetping_sdu_t *sdu)
|
---|
79 | {
|
---|
80 | async_exch_t *exch = async_exchange_begin(inetping_sess);
|
---|
81 |
|
---|
82 | ipc_call_t answer;
|
---|
83 | aid_t req = async_send_3(exch, INETPING_SEND, (sysarg_t) sdu->src,
|
---|
84 | (sysarg_t) sdu->dest, sdu->seq_no, &answer);
|
---|
85 | sysarg_t retval = async_data_write_start(exch, sdu->data, sdu->size);
|
---|
86 |
|
---|
87 | async_exchange_end(exch);
|
---|
88 |
|
---|
89 | if (retval != EOK) {
|
---|
90 | async_forget(req);
|
---|
91 | return retval;
|
---|
92 | }
|
---|
93 |
|
---|
94 | async_wait_for(req, &retval);
|
---|
95 | return retval;
|
---|
96 | }
|
---|
97 |
|
---|
98 | int inetping_get_srcaddr(uint32_t remote, uint32_t *local)
|
---|
99 | {
|
---|
100 | async_exch_t *exch = async_exchange_begin(inetping_sess);
|
---|
101 |
|
---|
102 | sysarg_t local_addr;
|
---|
103 | int rc = async_req_1_1(exch, INETPING_GET_SRCADDR, (sysarg_t) remote,
|
---|
104 | &local_addr);
|
---|
105 |
|
---|
106 | async_exchange_end(exch);
|
---|
107 |
|
---|
108 | if (rc != EOK)
|
---|
109 | return rc;
|
---|
110 |
|
---|
111 | *local = (uint32_t) local_addr;
|
---|
112 | return EOK;
|
---|
113 | }
|
---|
114 |
|
---|
115 | static void inetping_ev_recv(ipc_callid_t callid, ipc_call_t *call)
|
---|
116 | {
|
---|
117 | inetping_sdu_t sdu;
|
---|
118 |
|
---|
119 | sdu.src = IPC_GET_ARG1(*call);
|
---|
120 | sdu.dest = IPC_GET_ARG2(*call);
|
---|
121 | sdu.seq_no = IPC_GET_ARG3(*call);
|
---|
122 |
|
---|
123 | int rc = async_data_write_accept(&sdu.data, false, 0, 0, 0, &sdu.size);
|
---|
124 | if (rc != EOK) {
|
---|
125 | async_answer_0(callid, rc);
|
---|
126 | return;
|
---|
127 | }
|
---|
128 |
|
---|
129 | rc = inetping_ev_ops->recv(&sdu);
|
---|
130 | free(sdu.data);
|
---|
131 | async_answer_0(callid, rc);
|
---|
132 | }
|
---|
133 |
|
---|
134 | static void inetping_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
135 | {
|
---|
136 | while (true) {
|
---|
137 | ipc_call_t call;
|
---|
138 | ipc_callid_t callid = async_get_call(&call);
|
---|
139 |
|
---|
140 | if (!IPC_GET_IMETHOD(call)) {
|
---|
141 | /* TODO: Handle hangup */
|
---|
142 | return;
|
---|
143 | }
|
---|
144 |
|
---|
145 | switch (IPC_GET_IMETHOD(call)) {
|
---|
146 | case INETPING_EV_RECV:
|
---|
147 | inetping_ev_recv(callid, &call);
|
---|
148 | break;
|
---|
149 | default:
|
---|
150 | async_answer_0(callid, ENOTSUP);
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | /** @}
|
---|
156 | */
|
---|