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/inet.h>
|
---|
33 | #include <ipc/inet.h>
|
---|
34 | #include <ipc/services.h>
|
---|
35 | #include <loc.h>
|
---|
36 |
|
---|
37 | static void inet_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
|
---|
38 |
|
---|
39 | static async_sess_t *inet_sess = NULL;
|
---|
40 | static inet_ev_ops_t *inet_ev_ops = NULL;
|
---|
41 | static uint8_t inet_protocol = 0;
|
---|
42 |
|
---|
43 | static int inet_callback_create(void)
|
---|
44 | {
|
---|
45 | async_exch_t *exch = async_exchange_begin(inet_sess);
|
---|
46 |
|
---|
47 | ipc_call_t answer;
|
---|
48 | aid_t req = async_send_0(exch, INET_CALLBACK_CREATE, &answer);
|
---|
49 | int rc = async_connect_to_me(exch, 0, 0, 0, inet_cb_conn, NULL);
|
---|
50 | async_exchange_end(exch);
|
---|
51 |
|
---|
52 | if (rc != EOK)
|
---|
53 | return rc;
|
---|
54 |
|
---|
55 | sysarg_t retval;
|
---|
56 | async_wait_for(req, &retval);
|
---|
57 | if (retval != EOK)
|
---|
58 | return retval;
|
---|
59 |
|
---|
60 | return EOK;
|
---|
61 | }
|
---|
62 |
|
---|
63 | static int inet_set_proto(uint8_t protocol)
|
---|
64 | {
|
---|
65 | int rc;
|
---|
66 |
|
---|
67 | async_exch_t *exch = async_exchange_begin(inet_sess);
|
---|
68 | rc = async_req_1_0(exch, INET_SET_PROTO, protocol);
|
---|
69 | async_exchange_end(exch);
|
---|
70 |
|
---|
71 | return rc;
|
---|
72 | }
|
---|
73 |
|
---|
74 | int inet_init(uint8_t protocol, inet_ev_ops_t *ev_ops)
|
---|
75 | {
|
---|
76 | service_id_t inet_svc;
|
---|
77 | int rc;
|
---|
78 |
|
---|
79 | assert(inet_sess == NULL);
|
---|
80 | assert(inet_ev_ops == NULL);
|
---|
81 | assert(inet_protocol == 0);
|
---|
82 |
|
---|
83 | rc = loc_service_get_id(SERVICE_NAME_INET, &inet_svc,
|
---|
84 | IPC_FLAG_BLOCKING);
|
---|
85 | if (rc != EOK)
|
---|
86 | return ENOENT;
|
---|
87 |
|
---|
88 | inet_sess = loc_service_connect(EXCHANGE_SERIALIZE, inet_svc,
|
---|
89 | IPC_FLAG_BLOCKING);
|
---|
90 | if (inet_sess == NULL)
|
---|
91 | return ENOENT;
|
---|
92 |
|
---|
93 | if (inet_set_proto(protocol) != EOK) {
|
---|
94 | async_hangup(inet_sess);
|
---|
95 | inet_sess = NULL;
|
---|
96 | return EIO;
|
---|
97 | }
|
---|
98 |
|
---|
99 | if (inet_callback_create() != EOK) {
|
---|
100 | async_hangup(inet_sess);
|
---|
101 | inet_sess = NULL;
|
---|
102 | return EIO;
|
---|
103 | }
|
---|
104 |
|
---|
105 | inet_protocol = protocol;
|
---|
106 | inet_ev_ops = ev_ops;
|
---|
107 |
|
---|
108 | return EOK;
|
---|
109 | }
|
---|
110 |
|
---|
111 | int inet_send(inet_dgram_t *dgram, uint8_t ttl, inet_df_t df)
|
---|
112 | {
|
---|
113 | async_exch_t *exch = async_exchange_begin(inet_sess);
|
---|
114 |
|
---|
115 | ipc_call_t answer;
|
---|
116 | aid_t req = async_send_5(exch, INET_SEND, dgram->src.ipv4,
|
---|
117 | dgram->dest.ipv4, dgram->tos, ttl, df, &answer);
|
---|
118 | int rc = async_data_write_start(exch, dgram->data, dgram->size);
|
---|
119 | async_exchange_end(exch);
|
---|
120 |
|
---|
121 | if (rc != EOK) {
|
---|
122 | async_forget(req);
|
---|
123 | return rc;
|
---|
124 | }
|
---|
125 |
|
---|
126 | sysarg_t retval;
|
---|
127 | async_wait_for(req, &retval);
|
---|
128 | if (retval != EOK)
|
---|
129 | return retval;
|
---|
130 |
|
---|
131 | return EOK;
|
---|
132 | }
|
---|
133 |
|
---|
134 | int inet_get_srcaddr(inet_addr_t *remote, uint8_t tos, inet_addr_t *local)
|
---|
135 | {
|
---|
136 | sysarg_t local_addr;
|
---|
137 | async_exch_t *exch = async_exchange_begin(inet_sess);
|
---|
138 |
|
---|
139 | int rc = async_req_2_1(exch, INET_GET_SRCADDR, remote->ipv4,
|
---|
140 | tos, &local_addr);
|
---|
141 | async_exchange_end(exch);
|
---|
142 |
|
---|
143 | if (rc != EOK)
|
---|
144 | return rc;
|
---|
145 |
|
---|
146 | local->ipv4 = local_addr;
|
---|
147 | return EOK;
|
---|
148 | }
|
---|
149 |
|
---|
150 | static void inet_ev_recv(ipc_callid_t callid, ipc_call_t *call)
|
---|
151 | {
|
---|
152 | int rc;
|
---|
153 | inet_dgram_t dgram;
|
---|
154 |
|
---|
155 | dgram.src.ipv4 = IPC_GET_ARG1(*call);
|
---|
156 | dgram.dest.ipv4 = IPC_GET_ARG2(*call);
|
---|
157 | dgram.tos = IPC_GET_ARG3(*call);
|
---|
158 |
|
---|
159 | rc = async_data_write_accept(&dgram.data, false, 0, 0, 0, &dgram.size);
|
---|
160 | if (rc != EOK) {
|
---|
161 | async_answer_0(callid, rc);
|
---|
162 | return;
|
---|
163 | }
|
---|
164 |
|
---|
165 | rc = inet_ev_ops->recv(&dgram);
|
---|
166 | async_answer_0(callid, rc);
|
---|
167 | }
|
---|
168 |
|
---|
169 | static void inet_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
170 | {
|
---|
171 | while (true) {
|
---|
172 | ipc_call_t call;
|
---|
173 | ipc_callid_t callid = async_get_call(&call);
|
---|
174 |
|
---|
175 | if (!IPC_GET_IMETHOD(call)) {
|
---|
176 | /* TODO: Handle hangup */
|
---|
177 | return;
|
---|
178 | }
|
---|
179 |
|
---|
180 | switch (IPC_GET_IMETHOD(call)) {
|
---|
181 | case INET_EV_RECV:
|
---|
182 | inet_ev_recv(callid, &call);
|
---|
183 | break;
|
---|
184 | default:
|
---|
185 | async_answer_0(callid, ENOTSUP);
|
---|
186 | }
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | /** @}
|
---|
191 | */
|
---|