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

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

cstyle (no change in functionality)

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