source: mainline/uspace/srv/inet/inet.c@ e2e56e67

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

Stub IP/Ethernet server. Implement IP link discovery in inet server.

  • 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/** @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 "inet.h"
50#include "inet_link.h"
51
52#define NAME "inet"
53
54static void inet_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
55
56static FIBRIL_MUTEX_INITIALIZE(client_list_lock);
57static LIST_INITIALIZE(client_list);
58
59static int inet_init(void)
60{
61 service_id_t sid;
62 int rc;
63
64 log_msg(LVL_DEBUG, "inet_init()");
65
66 async_set_client_connection(inet_client_conn);
67
68 rc = loc_server_register(NAME);
69 if (rc != EOK) {
70 log_msg(LVL_ERROR, "Failed registering server (%d).", rc);
71 return EEXIST;
72 }
73
74 rc = loc_service_register(SERVICE_NAME_INET, &sid);
75 if (rc != EOK) {
76 log_msg(LVL_ERROR, "Failed registering service (%d).", rc);
77 return EEXIST;
78 }
79
80 rc = inet_link_discovery_start();
81 if (rc != EOK)
82 return EEXIST;
83
84 return EOK;
85}
86
87static void inet_callback_create(inet_client_t *client, ipc_callid_t callid,
88 ipc_call_t *call)
89{
90 log_msg(LVL_DEBUG, "inet_callback_create()");
91
92 async_sess_t *sess = async_callback_receive(EXCHANGE_SERIALIZE);
93 if (sess == NULL) {
94 async_answer_0(callid, ENOMEM);
95 return;
96 }
97
98 client->sess = sess;
99 async_answer_0(callid, EOK);
100}
101
102static void inet_get_srcaddr(inet_client_t *client, ipc_callid_t callid,
103 ipc_call_t *call)
104{
105 log_msg(LVL_DEBUG, "inet_get_srcaddr()");
106
107 async_answer_0(callid, ENOTSUP);
108}
109
110static void inet_send(inet_client_t *client, ipc_callid_t callid,
111 ipc_call_t *call)
112{
113 inet_dgram_t dgram;
114 uint8_t ttl;
115 int df;
116 int rc;
117
118 log_msg(LVL_DEBUG, "inet_send()");
119
120 dgram.src.ipv4 = IPC_GET_ARG1(*call);
121 dgram.dest.ipv4 = IPC_GET_ARG2(*call);
122 dgram.tos = IPC_GET_ARG3(*call);
123 ttl = IPC_GET_ARG4(*call);
124 df = IPC_GET_ARG5(*call);
125
126 (void)ttl;
127 (void)df;
128
129 rc = async_data_write_accept(&dgram.data, false, 0, 0, 0, &dgram.size);
130 if (rc != EOK) {
131 async_answer_0(callid, rc);
132 return;
133 }
134
135 free(dgram.data);
136 async_answer_0(callid, ENOTSUP);
137}
138
139static void inet_set_proto(inet_client_t *client, ipc_callid_t callid,
140 ipc_call_t *call)
141{
142 sysarg_t proto;
143
144 proto = IPC_GET_ARG1(*call);
145 log_msg(LVL_DEBUG, "inet_set_proto(%lu)", (unsigned long) proto);
146
147 if (proto > UINT8_MAX) {
148 async_answer_0(callid, EINVAL);
149 return;
150 }
151
152 client->protocol = proto;
153 async_answer_0(callid, EOK);
154}
155
156static void inet_client_init(inet_client_t *client)
157{
158 client->sess = NULL;
159
160 fibril_mutex_lock(&client_list_lock);
161 list_append(&client->client_list, &client_list);
162 fibril_mutex_unlock(&client_list_lock);
163}
164
165static void inet_client_fini(inet_client_t *client)
166{
167 async_hangup(client->sess);
168 client->sess = NULL;
169
170 fibril_mutex_lock(&client_list_lock);
171 list_remove(&client->client_list);
172 fibril_mutex_unlock(&client_list_lock);
173}
174
175static void inet_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
176{
177 inet_client_t client;
178
179 log_msg(LVL_DEBUG, "inet_client_conn()");
180
181 /* Accept the connection */
182 async_answer_0(iid, EOK);
183
184 inet_client_init(&client);
185
186 while (true) {
187 ipc_call_t call;
188 ipc_callid_t callid = async_get_call(&call);
189 sysarg_t method = IPC_GET_IMETHOD(call);
190
191 if (!method) {
192 /* The other side has hung up */
193 async_answer_0(callid, EOK);
194 return;
195 }
196
197 switch (method) {
198 case INET_CALLBACK_CREATE:
199 inet_callback_create(&client, callid, &call);
200 break;
201 case INET_GET_SRCADDR:
202 inet_get_srcaddr(&client, callid, &call);
203 break;
204 case INET_SEND:
205 inet_send(&client, callid, &call);
206 break;
207 case INET_SET_PROTO:
208 inet_set_proto(&client, callid, &call);
209 break;
210 default:
211 async_answer_0(callid, EINVAL);
212 }
213 }
214
215 inet_client_fini(&client);
216}
217
218int inet_ev_recv(inet_client_t *client, inet_dgram_t *dgram)
219{
220 async_exch_t *exch = async_exchange_begin(client->sess);
221
222 ipc_call_t answer;
223 aid_t req = async_send_3(exch, INET_EV_RECV, dgram->src.ipv4,
224 dgram->dest.ipv4, dgram->tos, &answer);
225 int rc = async_data_write_start(exch, dgram->data, dgram->size);
226 async_exchange_end(exch);
227
228 if (rc != EOK) {
229 async_wait_for(req, NULL);
230 return rc;
231 }
232
233 sysarg_t retval;
234 async_wait_for(req, &retval);
235 if (retval != EOK)
236 return retval;
237
238 return EOK;
239}
240
241int main(int argc, char *argv[])
242{
243 int rc;
244
245 printf(NAME ": HelenOS Internet Protocol service\n");
246
247 if (log_init(NAME, LVL_DEBUG) != EOK) {
248 printf(NAME ": Failed to initialize logging.\n");
249 return 1;
250 }
251
252 rc = inet_init();
253 if (rc != EOK)
254 return 1;
255
256 printf(NAME ": Accepting connections.\n");
257 task_retval(0);
258 async_manager();
259
260 /* Not reached */
261 return 0;
262}
263
264/** @}
265 */
Note: See TracBrowser for help on using the repository browser.