source: mainline/uspace/lib/c/generic/inet.c@ 1d24ad3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1d24ad3 was 02a09ed, checked in by Martin Decky <martin@…>, 12 years ago

add basic infrastructure for IPv6 (inactive)
make inet_addr_t a universal address type

  • Property mode set to 100644
File size: 6.0 KB
Line 
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 <net/socket_codes.h>
33#include <inet/inet.h>
34#include <ipc/inet.h>
35#include <ipc/services.h>
36#include <loc.h>
37
38static void inet_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
39
40static async_sess_t *inet_sess = NULL;
41static inet_ev_ops_t *inet_ev_ops = NULL;
42static uint8_t inet_protocol = 0;
43
44static int inet_callback_create(void)
45{
46 async_exch_t *exch = async_exchange_begin(inet_sess);
47
48 ipc_call_t answer;
49 aid_t req = async_send_0(exch, INET_CALLBACK_CREATE, &answer);
50 int rc = async_connect_to_me(exch, 0, 0, 0, inet_cb_conn, NULL);
51 async_exchange_end(exch);
52
53 if (rc != EOK)
54 return rc;
55
56 sysarg_t retval;
57 async_wait_for(req, &retval);
58
59 return retval;
60}
61
62static int inet_set_proto(uint8_t protocol)
63{
64 async_exch_t *exch = async_exchange_begin(inet_sess);
65 int rc = async_req_1_0(exch, INET_SET_PROTO, protocol);
66 async_exchange_end(exch);
67
68 return rc;
69}
70
71int inet_init(uint8_t protocol, inet_ev_ops_t *ev_ops)
72{
73 service_id_t inet_svc;
74 int rc;
75
76 assert(inet_sess == NULL);
77 assert(inet_ev_ops == NULL);
78 assert(inet_protocol == 0);
79
80 rc = loc_service_get_id(SERVICE_NAME_INET, &inet_svc,
81 IPC_FLAG_BLOCKING);
82 if (rc != EOK)
83 return ENOENT;
84
85 inet_sess = loc_service_connect(EXCHANGE_SERIALIZE, inet_svc,
86 IPC_FLAG_BLOCKING);
87 if (inet_sess == NULL)
88 return ENOENT;
89
90 if (inet_set_proto(protocol) != EOK) {
91 async_hangup(inet_sess);
92 inet_sess = NULL;
93 return EIO;
94 }
95
96 if (inet_callback_create() != EOK) {
97 async_hangup(inet_sess);
98 inet_sess = NULL;
99 return EIO;
100 }
101
102 inet_protocol = protocol;
103 inet_ev_ops = ev_ops;
104
105 return EOK;
106}
107
108int inet_send(inet_dgram_t *dgram, uint8_t ttl, inet_df_t df)
109{
110 addr32_t src_v4;
111 addr128_t src_v6;
112 uint16_t src_af = inet_addr_get(&dgram->src, &src_v4, &src_v6);
113
114 addr32_t dest_v4;
115 addr128_t dest_v6;
116 uint16_t dest_af = inet_addr_get(&dgram->dest, &dest_v4, &dest_v6);
117
118 if (src_af != dest_af)
119 return EINVAL;
120
121 async_exch_t *exch;
122 ipc_call_t answer;
123 aid_t req;
124 int rc;
125
126 switch (src_af) {
127 case AF_INET:
128 exch = async_exchange_begin(inet_sess);
129
130 req = async_send_5(exch, INET_SEND, (sysarg_t) src_v4,
131 (sysarg_t) dest_v4, dgram->tos, ttl, df, &answer);
132 rc = async_data_write_start(exch, dgram->data, dgram->size);
133
134 async_exchange_end(exch);
135 break;
136 case AF_INET6:
137 // FIXME TODO
138 return ENOTSUP;
139 default:
140 return EINVAL;
141 }
142
143 if (rc != EOK) {
144 async_forget(req);
145 return rc;
146 }
147
148 sysarg_t retval;
149 async_wait_for(req, &retval);
150
151 return (int) retval;
152}
153
154int inet_get_srcaddr(inet_addr_t *remote, uint8_t tos, inet_addr_t *local)
155{
156 addr32_t remote_v4;
157 addr128_t remote_v6;
158 uint16_t remote_af = inet_addr_get(remote, &remote_v4, &remote_v6);
159
160 async_exch_t *exch;
161 int rc;
162
163 switch (remote_af) {
164 case AF_INET:
165 exch = async_exchange_begin(inet_sess);
166
167 sysarg_t local_v4;
168 rc = async_req_2_1(exch, INET_GET_SRCADDR, (sysarg_t) remote_v4,
169 tos, &local_v4);
170
171 async_exchange_end(exch);
172
173 if (rc != EOK)
174 return rc;
175
176 inet_addr_set(local_v4, local);
177 return EOK;
178 case AF_INET6:
179 // FIXME TODO
180 return ENOTSUP;
181 default:
182 return EINVAL;
183 }
184}
185
186static void inet_ev_recv(ipc_callid_t iid, ipc_call_t *icall)
187{
188 inet_dgram_t dgram;
189
190 dgram.tos = IPC_GET_ARG1(*icall);
191
192 ipc_callid_t callid;
193 size_t size;
194 if (!async_data_write_receive(&callid, &size)) {
195 async_answer_0(callid, EINVAL);
196 async_answer_0(iid, EINVAL);
197 return;
198 }
199
200 if (size != sizeof(inet_addr_t)) {
201 async_answer_0(callid, EINVAL);
202 async_answer_0(iid, EINVAL);
203 return;
204 }
205
206 int rc = async_data_write_finalize(callid, &dgram.src, size);
207 if (rc != EOK) {
208 async_answer_0(callid, rc);
209 async_answer_0(iid, rc);
210 return;
211 }
212
213 if (!async_data_write_receive(&callid, &size)) {
214 async_answer_0(callid, EINVAL);
215 async_answer_0(iid, EINVAL);
216 return;
217 }
218
219 if (size != sizeof(inet_addr_t)) {
220 async_answer_0(callid, EINVAL);
221 async_answer_0(iid, EINVAL);
222 return;
223 }
224
225 rc = async_data_write_finalize(callid, &dgram.dest, size);
226 if (rc != EOK) {
227 async_answer_0(callid, rc);
228 async_answer_0(iid, rc);
229 return;
230 }
231
232 rc = async_data_write_accept(&dgram.data, false, 0, 0, 0, &dgram.size);
233 if (rc != EOK) {
234 async_answer_0(iid, rc);
235 return;
236 }
237
238 rc = inet_ev_ops->recv(&dgram);
239 async_answer_0(iid, rc);
240}
241
242static void inet_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
243{
244 while (true) {
245 ipc_call_t call;
246 ipc_callid_t callid = async_get_call(&call);
247
248 if (!IPC_GET_IMETHOD(call)) {
249 /* TODO: Handle hangup */
250 return;
251 }
252
253 switch (IPC_GET_IMETHOD(call)) {
254 case INET_EV_RECV:
255 inet_ev_recv(callid, &call);
256 break;
257 default:
258 async_answer_0(callid, ENOTSUP);
259 }
260 }
261}
262
263/** @}
264 */
Note: See TracBrowser for help on using the repository browser.