source: mainline/uspace/srv/net/loopip/loopip.c@ 3061bc1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3061bc1 was 1d6dd2a, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Remove unnecessary includes from <stdio.h>.

  • Property mode set to 100644
File size: 6.7 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 loopip
30 * @{
31 */
32/**
33 * @file
34 * @brief Loopback IP link provider
35 */
36
37#include <adt/prodcons.h>
38#include <async.h>
39#include <errno.h>
40#include <str_error.h>
41#include <inet/iplink_srv.h>
42#include <inet/addr.h>
43#include <io/log.h>
44#include <loc.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <str.h>
48#include <task.h>
49
50#define NAME "loopip"
51
52static errno_t loopip_open(iplink_srv_t *srv);
53static errno_t loopip_close(iplink_srv_t *srv);
54static errno_t loopip_send(iplink_srv_t *srv, iplink_sdu_t *sdu);
55static errno_t loopip_send6(iplink_srv_t *srv, iplink_sdu6_t *sdu);
56static errno_t loopip_get_mtu(iplink_srv_t *srv, size_t *mtu);
57static errno_t loopip_get_mac48(iplink_srv_t *srv, addr48_t *mac);
58static errno_t loopip_addr_add(iplink_srv_t *srv, inet_addr_t *addr);
59static errno_t loopip_addr_remove(iplink_srv_t *srv, inet_addr_t *addr);
60
61static void loopip_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
62
63static iplink_ops_t loopip_iplink_ops = {
64 .open = loopip_open,
65 .close = loopip_close,
66 .send = loopip_send,
67 .send6 = loopip_send6,
68 .get_mtu = loopip_get_mtu,
69 .get_mac48 = loopip_get_mac48,
70 .addr_add = loopip_addr_add,
71 .addr_remove = loopip_addr_remove
72};
73
74static iplink_srv_t loopip_iplink;
75static prodcons_t loopip_rcv_queue;
76
77typedef struct {
78 link_t link;
79
80 /* XXX Version should be part of SDU */
81 ip_ver_t ver;
82 iplink_recv_sdu_t sdu;
83} rqueue_entry_t;
84
85static errno_t loopip_recv_fibril(void *arg)
86{
87 while (true) {
88 log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip_recv_fibril(): Wait for one item");
89 link_t *link = prodcons_consume(&loopip_rcv_queue);
90 rqueue_entry_t *rqe =
91 list_get_instance(link, rqueue_entry_t, link);
92
93 (void) iplink_ev_recv(&loopip_iplink, &rqe->sdu, rqe->ver);
94
95 free(rqe->sdu.data);
96 free(rqe);
97 }
98
99 return 0;
100}
101
102static errno_t loopip_init(void)
103{
104 async_set_fallback_port_handler(loopip_client_conn, NULL);
105
106 errno_t rc = loc_server_register(NAME);
107 if (rc != EOK) {
108 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server.");
109 return rc;
110 }
111
112 iplink_srv_init(&loopip_iplink);
113 loopip_iplink.ops = &loopip_iplink_ops;
114 loopip_iplink.arg = NULL;
115
116 prodcons_initialize(&loopip_rcv_queue);
117
118 const char *svc_name = "net/loopback";
119 service_id_t sid;
120 rc = loc_service_register(svc_name, &sid);
121 if (rc != EOK) {
122 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service %s.",
123 svc_name);
124 return rc;
125 }
126
127 category_id_t iplink_cat;
128 rc = loc_category_get_id("iplink", &iplink_cat, IPC_FLAG_BLOCKING);
129 if (rc != EOK) {
130 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed resolving category 'iplink'.");
131 return rc;
132 }
133
134 rc = loc_service_add_to_cat(sid, iplink_cat);
135 if (rc != EOK) {
136 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding %s to category.",
137 svc_name);
138 return rc;
139 }
140
141 fid_t fid = fibril_create(loopip_recv_fibril, NULL);
142 if (fid == 0)
143 return ENOMEM;
144
145 fibril_add_ready(fid);
146
147 return EOK;
148}
149
150static void loopip_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
151{
152 log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip_client_conn()");
153 iplink_conn(iid, icall, &loopip_iplink);
154}
155
156static errno_t loopip_open(iplink_srv_t *srv)
157{
158 log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip_open()");
159 return EOK;
160}
161
162static errno_t loopip_close(iplink_srv_t *srv)
163{
164 log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip_close()");
165 return EOK;
166}
167
168static errno_t loopip_send(iplink_srv_t *srv, iplink_sdu_t *sdu)
169{
170 log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip_send()");
171
172 rqueue_entry_t *rqe = calloc(1, sizeof(rqueue_entry_t));
173 if (rqe == NULL)
174 return ENOMEM;
175
176 /*
177 * Clone SDU
178 */
179 rqe->ver = ip_v4;
180 rqe->sdu.data = malloc(sdu->size);
181 if (rqe->sdu.data == NULL) {
182 free(rqe);
183 return ENOMEM;
184 }
185
186 memcpy(rqe->sdu.data, sdu->data, sdu->size);
187 rqe->sdu.size = sdu->size;
188
189 /*
190 * Insert to receive queue
191 */
192 prodcons_produce(&loopip_rcv_queue, &rqe->link);
193
194 return EOK;
195}
196
197static errno_t loopip_send6(iplink_srv_t *srv, iplink_sdu6_t *sdu)
198{
199 log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip6_send()");
200
201 rqueue_entry_t *rqe = calloc(1, sizeof(rqueue_entry_t));
202 if (rqe == NULL)
203 return ENOMEM;
204
205 /*
206 * Clone SDU
207 */
208 rqe->ver = ip_v6;
209 rqe->sdu.data = malloc(sdu->size);
210 if (rqe->sdu.data == NULL) {
211 free(rqe);
212 return ENOMEM;
213 }
214
215 memcpy(rqe->sdu.data, sdu->data, sdu->size);
216 rqe->sdu.size = sdu->size;
217
218 /*
219 * Insert to receive queue
220 */
221 prodcons_produce(&loopip_rcv_queue, &rqe->link);
222
223 return EOK;
224}
225
226static errno_t loopip_get_mtu(iplink_srv_t *srv, size_t *mtu)
227{
228 log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip_get_mtu()");
229 *mtu = 1500;
230 return EOK;
231}
232
233static errno_t loopip_get_mac48(iplink_srv_t *src, addr48_t *mac)
234{
235 log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip_get_mac48()");
236 return ENOTSUP;
237}
238
239static errno_t loopip_addr_add(iplink_srv_t *srv, inet_addr_t *addr)
240{
241 return EOK;
242}
243
244static errno_t loopip_addr_remove(iplink_srv_t *srv, inet_addr_t *addr)
245{
246 return EOK;
247}
248
249int main(int argc, char *argv[])
250{
251 printf("%s: HelenOS loopback IP link provider\n", NAME);
252
253 errno_t rc = log_init(NAME);
254 if (rc != EOK) {
255 printf("%s: Failed to initialize logging: %s.\n", NAME, str_error(rc));
256 return rc;
257 }
258
259 rc = loopip_init();
260 if (rc != EOK) {
261 printf("%s: Failed to initialize loopip: %s.\n", NAME, str_error(rc));
262 return rc;
263 }
264
265 printf("%s: Accepting connections.\n", NAME);
266 task_retval(0);
267 async_manager();
268
269 /* Not reached */
270 return 0;
271}
272
273/** @}
274 */
Note: See TracBrowser for help on using the repository browser.