source: mainline/uspace/srv/inet/inet_link.c@ fe4310f

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

Distinguish between datagram and packet. Deliver received datagrams to
clients based on protocol number.

  • 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
35 */
36
37#include <bool.h>
38#include <errno.h>
39#include <fibril_synch.h>
40#include <inet/iplink.h>
41#include <io/log.h>
42#include <loc.h>
43#include <stdlib.h>
44
45#include "addrobj.h"
46#include "inet.h"
47#include "inet_link.h"
48#include "pdu.h"
49
50static int inet_link_open(service_id_t sid);
51static int inet_iplink_recv(iplink_t *ilink, iplink_sdu_t *sdu);
52
53static iplink_ev_ops_t inet_iplink_ev_ops = {
54 .recv = inet_iplink_recv
55};
56
57static LIST_INITIALIZE(inet_link_list);
58static FIBRIL_MUTEX_INITIALIZE(inet_discovery_lock);
59
60static int inet_iplink_recv(iplink_t *iplink, iplink_sdu_t *sdu)
61{
62 inet_packet_t packet;
63 int rc;
64
65 log_msg(LVL_DEBUG, "inet_iplink_recv()");
66 rc = inet_pdu_decode(sdu->data, sdu->size, &packet);
67 if (rc != EOK) {
68 log_msg(LVL_DEBUG, "failed decoding PDU");
69 return rc;
70 }
71
72 log_msg(LVL_DEBUG, "call inet_recv_packet()");
73 rc = inet_recv_packet(&packet);
74 log_msg(LVL_DEBUG, "call inet_recv_packet -> %d", rc);
75
76 return rc;
77}
78
79static int inet_link_check_new(void)
80{
81 bool already_known;
82 category_id_t iplink_cat;
83 service_id_t *svcs;
84 size_t count, i;
85 int rc;
86
87 fibril_mutex_lock(&inet_discovery_lock);
88
89 rc = loc_category_get_id("iplink", &iplink_cat, IPC_FLAG_BLOCKING);
90 if (rc != EOK) {
91 log_msg(LVL_ERROR, "Failed resolving category 'iplink'.");
92 fibril_mutex_unlock(&inet_discovery_lock);
93 return ENOENT;
94 }
95
96 rc = loc_category_get_svcs(iplink_cat, &svcs, &count);
97 if (rc != EOK) {
98 log_msg(LVL_ERROR, "Failed getting list of IP links.");
99 fibril_mutex_unlock(&inet_discovery_lock);
100 return EIO;
101 }
102
103 for (i = 0; i < count; i++) {
104 already_known = false;
105
106 list_foreach(inet_link_list, ilink_link) {
107 inet_link_t *ilink = list_get_instance(ilink_link,
108 inet_link_t, link_list);
109 if (ilink->svc_id == svcs[i]) {
110 already_known = true;
111 break;
112 }
113 }
114
115 if (!already_known) {
116 log_msg(LVL_DEBUG, "Found IP link '%lu'",
117 (unsigned long) svcs[i]);
118 rc = inet_link_open(svcs[i]);
119 if (rc != EOK)
120 log_msg(LVL_ERROR, "Could not open IP link.");
121 }
122 }
123
124 fibril_mutex_unlock(&inet_discovery_lock);
125 return EOK;
126}
127
128static inet_link_t *inet_link_new(void)
129{
130 inet_link_t *ilink = calloc(1, sizeof(inet_link_t));
131
132 if (ilink == NULL) {
133 log_msg(LVL_ERROR, "Failed allocating link structure. "
134 "Out of memory.");
135 return NULL;
136 }
137
138 link_initialize(&ilink->link_list);
139
140 return ilink;
141}
142
143static void inet_link_delete(inet_link_t *ilink)
144{
145 if (ilink->svc_name != NULL)
146 free(ilink->svc_name);
147 free(ilink);
148}
149
150static int inet_link_open(service_id_t sid)
151{
152 inet_link_t *ilink;
153 int rc;
154
155 log_msg(LVL_DEBUG, "inet_link_open()");
156 ilink = inet_link_new();
157 if (ilink == NULL)
158 return ENOMEM;
159
160 rc = loc_service_get_name(sid, &ilink->svc_name);
161 if (rc != EOK) {
162 log_msg(LVL_ERROR, "Failed getting service name.");
163 goto error;
164 }
165
166 ilink->sess = loc_service_connect(EXCHANGE_SERIALIZE, sid, 0);
167 if (ilink->sess == NULL) {
168 log_msg(LVL_ERROR, "Failed connecting '%s'", ilink->svc_name);
169 goto error;
170 }
171
172 rc = iplink_open(ilink->sess, &inet_iplink_ev_ops, &ilink->iplink);
173 if (rc != EOK) {
174 log_msg(LVL_ERROR, "Failed opening IP link '%s'",
175 ilink->svc_name);
176 goto error;
177 }
178
179 log_msg(LVL_DEBUG, "Opened IP link '%s'", ilink->svc_name);
180 list_append(&ilink->link_list, &inet_link_list);
181
182 inet_addrobj_t *addr;
183
184 /* XXX For testing: set static IP address 192.168.0.4/24 */
185 addr = inet_addrobj_new();
186 addr->naddr.ipv4 = (192 << 24) + (168 << 16) + (0 << 8) + 4;
187 addr->naddr.bits = 24;
188 addr->ilink = ilink;
189 inet_addrobj_add(addr);
190
191 return EOK;
192
193error:
194 inet_link_delete(ilink);
195 return rc;
196}
197
198static void inet_link_cat_change_cb(void)
199{
200 (void) inet_link_check_new();
201}
202
203int inet_link_discovery_start(void)
204{
205 int rc;
206
207 rc = loc_register_cat_change_cb(inet_link_cat_change_cb);
208 if (rc != EOK) {
209 log_msg(LVL_ERROR, "Failed registering callback for IP link "
210 "discovery (%d).", rc);
211 return rc;
212 }
213
214 return inet_link_check_new();
215}
216
217/** Send datagram over Internet link */
218int inet_link_send_dgram(inet_link_t *ilink, inet_addr_t *lsrc,
219 inet_addr_t *ldest, inet_dgram_t *dgram, uint8_t ttl, int df)
220{
221 iplink_sdu_t sdu;
222 inet_packet_t packet;
223 int rc;
224
225 /* XXX Fragment packet */
226 packet.src = dgram->src;
227 packet.dest = dgram->dest;
228 packet.tos = dgram->tos;
229 packet.proto = 42;
230 packet.ttl = ttl;
231 packet.df = df;
232 packet.data = dgram->data;
233 packet.size = dgram->size;
234
235 sdu.lsrc.ipv4 = lsrc->ipv4;
236 sdu.ldest.ipv4 = ldest->ipv4;
237 rc = inet_pdu_encode(&packet, &sdu.data, &sdu.size);
238 if (rc != EOK)
239 return rc;
240
241 rc = iplink_send(ilink->iplink, &sdu);
242 free(sdu.data);
243
244 return rc;
245}
246
247
248/** @}
249 */
Note: See TracBrowser for help on using the repository browser.