source: mainline/uspace/srv/inet/inet.c@ 291c792

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 291c792 was a88a6eac, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Fix 64-bit build - remove some debugging output.

  • Property mode set to 100644
File size: 8.9 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/** @addtogroup inet
30 * @{
31 */
32/**
33 * @file
34 * @brief Internet Protocol service
35 */
36
37#include <adt/list.h>
38#include <async.h>
39#include <errno.h>
40#include <fibril_synch.h>
41#include <io/log.h>
42#include <ipc/inet.h>
43#include <ipc/services.h>
44#include <loc.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <sys/types.h>
48
49#include "addrobj.h"
50#include "inet.h"
51#include "inetcfg.h"
52#include "inet_link.h"
53
54#define NAME "inet"
55
56static void inet_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
57
58static FIBRIL_MUTEX_INITIALIZE(client_list_lock);
59static LIST_INITIALIZE(client_list);
60
61static int inet_init(void)
62{
63 service_id_t sid;
64 int rc;
65
66 log_msg(LVL_DEBUG, "inet_init()");
67
68 async_set_client_connection(inet_client_conn);
69
70 rc = loc_server_register(NAME);
71 if (rc != EOK) {
72 log_msg(LVL_ERROR, "Failed registering server (%d).", rc);
73 return EEXIST;
74 }
75
76 rc = loc_service_register_with_iface(SERVICE_NAME_INET, &sid,
77 INET_PORT_DEFAULT);
78 if (rc != EOK) {
79 log_msg(LVL_ERROR, "Failed registering service (%d).", rc);
80 return EEXIST;
81 }
82
83 rc = loc_service_register_with_iface(SERVICE_NAME_INETCFG, &sid,
84 INET_PORT_CFG);
85 if (rc != EOK) {
86 log_msg(LVL_ERROR, "Failed registering service (%d).", rc);
87 return EEXIST;
88 }
89
90 rc = inet_link_discovery_start();
91 if (rc != EOK)
92 return EEXIST;
93
94 return EOK;
95}
96
97static void inet_callback_create_srv(inet_client_t *client, ipc_callid_t callid,
98 ipc_call_t *call)
99{
100 log_msg(LVL_DEBUG, "inet_callback_create_srv()");
101
102 async_sess_t *sess = async_callback_receive(EXCHANGE_SERIALIZE);
103 if (sess == NULL) {
104 async_answer_0(callid, ENOMEM);
105 return;
106 }
107
108 client->sess = sess;
109 async_answer_0(callid, EOK);
110}
111
112static int inet_route_packet(inet_dgram_t *dgram, uint8_t proto, uint8_t ttl,
113 int df)
114{
115 inet_addrobj_t *addr;
116
117 addr = inet_addrobj_find(&dgram->dest, iaf_net);
118 if (addr != NULL) {
119 /* Destination is directly accessible */
120 return inet_addrobj_send_dgram(addr, dgram, proto, ttl, df);
121 }
122
123 /* TODO: Gateways */
124 log_msg(LVL_DEBUG, "inet_send: No route to destination.");
125 return ENOENT;
126}
127
128static int inet_send(inet_client_t *client, inet_dgram_t *dgram,
129 uint8_t proto, uint8_t ttl, int df)
130{
131 return inet_route_packet(dgram, proto, ttl, df);
132}
133
134static int inet_get_srcaddr(inet_client_t *client, inet_addr_t *remote,
135 uint8_t tos, inet_addr_t *local)
136{
137 inet_addrobj_t *addr;
138
139 addr = inet_addrobj_find(remote, iaf_net);
140 if (addr != NULL) {
141 /* Destination is directly accessible */
142 local->ipv4 = addr->naddr.ipv4;
143 return EOK;
144 }
145
146 return ENOENT;
147}
148
149static void inet_get_srcaddr_srv(inet_client_t *client, ipc_callid_t callid,
150 ipc_call_t *call)
151{
152 inet_addr_t remote;
153 uint8_t tos;
154 inet_addr_t local;
155 int rc;
156
157 log_msg(LVL_DEBUG, "inet_get_srcaddr_srv()");
158
159 remote.ipv4 = IPC_GET_ARG1(*call);
160 tos = IPC_GET_ARG2(*call);
161 local.ipv4 = 0;
162
163 rc = inet_get_srcaddr(client, &remote, tos, &local);
164 async_answer_1(callid, rc, local.ipv4);
165}
166
167static void inet_send_srv(inet_client_t *client, ipc_callid_t callid,
168 ipc_call_t *call)
169{
170 inet_dgram_t dgram;
171 uint8_t ttl;
172 int df;
173 int rc;
174
175 log_msg(LVL_DEBUG, "inet_send_srv()");
176
177 dgram.src.ipv4 = IPC_GET_ARG1(*call);
178 dgram.dest.ipv4 = IPC_GET_ARG2(*call);
179 dgram.tos = IPC_GET_ARG3(*call);
180 ttl = IPC_GET_ARG4(*call);
181 df = IPC_GET_ARG5(*call);
182
183 rc = async_data_write_accept(&dgram.data, false, 0, 0, 0, &dgram.size);
184 if (rc != EOK) {
185 async_answer_0(callid, rc);
186 return;
187 }
188
189 rc = inet_send(client, &dgram, client->protocol, ttl, df);
190
191 free(dgram.data);
192 async_answer_0(callid, rc);
193}
194
195static void inet_set_proto_srv(inet_client_t *client, ipc_callid_t callid,
196 ipc_call_t *call)
197{
198 sysarg_t proto;
199
200 proto = IPC_GET_ARG1(*call);
201 log_msg(LVL_DEBUG, "inet_set_proto_srv(%lu)", (unsigned long) proto);
202
203 if (proto > UINT8_MAX) {
204 async_answer_0(callid, EINVAL);
205 return;
206 }
207
208 client->protocol = proto;
209 async_answer_0(callid, EOK);
210}
211
212static void inet_client_init(inet_client_t *client)
213{
214 client->sess = NULL;
215
216 fibril_mutex_lock(&client_list_lock);
217 list_append(&client->client_list, &client_list);
218 fibril_mutex_unlock(&client_list_lock);
219}
220
221static void inet_client_fini(inet_client_t *client)
222{
223 async_hangup(client->sess);
224 client->sess = NULL;
225
226 fibril_mutex_lock(&client_list_lock);
227 list_remove(&client->client_list);
228 fibril_mutex_unlock(&client_list_lock);
229}
230
231static void inet_default_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
232{
233 inet_client_t client;
234
235 log_msg(LVL_DEBUG, "inet_default_conn()");
236
237 /* Accept the connection */
238 async_answer_0(iid, EOK);
239
240 inet_client_init(&client);
241
242 while (true) {
243 ipc_call_t call;
244 ipc_callid_t callid = async_get_call(&call);
245 sysarg_t method = IPC_GET_IMETHOD(call);
246
247 if (!method) {
248 /* The other side has hung up */
249 async_answer_0(callid, EOK);
250 return;
251 }
252
253 switch (method) {
254 case INET_CALLBACK_CREATE:
255 inet_callback_create_srv(&client, callid, &call);
256 break;
257 case INET_GET_SRCADDR:
258 inet_get_srcaddr_srv(&client, callid, &call);
259 break;
260 case INET_SEND:
261 inet_send_srv(&client, callid, &call);
262 break;
263 case INET_SET_PROTO:
264 inet_set_proto_srv(&client, callid, &call);
265 break;
266 default:
267 async_answer_0(callid, EINVAL);
268 }
269 }
270
271 inet_client_fini(&client);
272}
273
274static void inet_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
275{
276 sysarg_t port;
277
278 port = IPC_GET_ARG1(*icall);
279
280 switch (port) {
281 case INET_PORT_DEFAULT:
282 inet_default_conn(iid, icall, arg);
283 break;
284 case INET_PORT_CFG:
285 inet_cfg_conn(iid, icall, arg);
286 break;
287 default:
288 async_answer_0(iid, ENOTSUP);
289 break;
290 }
291}
292
293static inet_client_t *inet_client_find(uint8_t proto)
294{
295 fibril_mutex_lock(&client_list_lock);
296
297 list_foreach(client_list, link) {
298 inet_client_t *client = list_get_instance(link, inet_client_t,
299 client_list);
300
301 if (client->protocol == proto) {
302 fibril_mutex_unlock(&client_list_lock);
303 return client;
304 }
305 }
306
307 fibril_mutex_unlock(&client_list_lock);
308 return NULL;
309}
310
311int inet_ev_recv(inet_client_t *client, inet_dgram_t *dgram)
312{
313 async_exch_t *exch = async_exchange_begin(client->sess);
314
315 ipc_call_t answer;
316 aid_t req = async_send_3(exch, INET_EV_RECV, dgram->src.ipv4,
317 dgram->dest.ipv4, dgram->tos, &answer);
318 int rc = async_data_write_start(exch, dgram->data, dgram->size);
319 async_exchange_end(exch);
320
321 if (rc != EOK) {
322 async_wait_for(req, NULL);
323 return rc;
324 }
325
326 sysarg_t retval;
327 async_wait_for(req, &retval);
328 if (retval != EOK)
329 return retval;
330
331 return EOK;
332}
333
334static int inet_recv_dgram_local(inet_dgram_t *dgram, uint8_t proto)
335{
336 inet_client_t *client;
337
338 log_msg(LVL_DEBUG, "inet_recv_dgram_local()");
339
340 client = inet_client_find(proto);
341 if (client == NULL) {
342 log_msg(LVL_DEBUG, "No client found for protocol 0x%" PRIx8,
343 proto);
344 return ENOENT;
345 }
346
347 return inet_ev_recv(client, dgram);
348}
349
350int inet_recv_packet(inet_packet_t *packet)
351{
352 inet_addrobj_t *addr;
353 inet_dgram_t dgram;
354
355 addr = inet_addrobj_find(&packet->dest, iaf_addr);
356 if (addr != NULL) {
357 /* Destined for one of the local addresses */
358
359 /* XXX Reassemble packets */
360 dgram.src = packet->src;
361 dgram.dest = packet->dest;
362 dgram.tos = packet->tos;
363 dgram.data = packet->data;
364 dgram.size = packet->size;
365
366 return inet_recv_dgram_local(&dgram, packet->proto);
367 }
368
369 return ENOENT;
370}
371
372int main(int argc, char *argv[])
373{
374 int rc;
375
376 printf(NAME ": HelenOS Internet Protocol service\n");
377
378 if (log_init(NAME, LVL_DEBUG) != EOK) {
379 printf(NAME ": Failed to initialize logging.\n");
380 return 1;
381 }
382
383 rc = inet_init();
384 if (rc != EOK)
385 return 1;
386
387 printf(NAME ": Accepting connections.\n");
388 task_retval(0);
389 async_manager();
390
391 /* Not reached */
392 return 0;
393}
394
395/** @}
396 */
Note: See TracBrowser for help on using the repository browser.