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

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

iplink_ev_recv() should use ip_ver_t instead of AF.

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