source: mainline/uspace/srv/net/loopip/loopip.c@ a17356fd

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

iplink IPv6 datagram support
rudimentary IPv6 support (sans fragmentation)

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