source: mainline/uspace/srv/inet/inet_link.c@ 0e25780

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

IP links need to be made aware of configured IP addresses.

  • Property mode set to 100644
File size: 6.3 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 iplink_addr_t iaddr;
154 int rc;
155
156 log_msg(LVL_DEBUG, "inet_link_open()");
157 ilink = inet_link_new();
158 if (ilink == NULL)
159 return ENOMEM;
160
161 rc = loc_service_get_name(sid, &ilink->svc_name);
162 if (rc != EOK) {
163 log_msg(LVL_ERROR, "Failed getting service name.");
164 goto error;
165 }
166
167 ilink->sess = loc_service_connect(EXCHANGE_SERIALIZE, sid, 0);
168 if (ilink->sess == NULL) {
169 log_msg(LVL_ERROR, "Failed connecting '%s'", ilink->svc_name);
170 goto error;
171 }
172
173 rc = iplink_open(ilink->sess, &inet_iplink_ev_ops, &ilink->iplink);
174 if (rc != EOK) {
175 log_msg(LVL_ERROR, "Failed opening IP link '%s'",
176 ilink->svc_name);
177 goto error;
178 }
179
180 log_msg(LVL_DEBUG, "Opened IP link '%s'", ilink->svc_name);
181 list_append(&ilink->link_list, &inet_link_list);
182
183 inet_addrobj_t *addr;
184
185 static int first = 1;
186 /* XXX For testing: set static IP address 192.168.0.4/24 */
187 addr = inet_addrobj_new();
188 if (first) {
189 addr->naddr.ipv4 = (127 << 24) + (0 << 16) + (0 << 8) + 1;
190 first = 0;
191 } else {
192 addr->naddr.ipv4 = (192 << 24) + (168 << 16) + (0 << 8) + 4;
193 }
194 addr->naddr.bits = 24;
195 addr->ilink = ilink;
196 inet_addrobj_add(addr);
197
198 iaddr.ipv4 = addr->naddr.ipv4;
199 rc = iplink_addr_add(ilink->iplink, &iaddr);
200 if (rc != EOK) {
201 log_msg(LVL_ERROR, "Failed setting IP address on internet link.");
202 /* XXX Roll back */
203 return rc;
204 }
205
206 return EOK;
207
208error:
209 inet_link_delete(ilink);
210 return rc;
211}
212
213static void inet_link_cat_change_cb(void)
214{
215 (void) inet_link_check_new();
216}
217
218int inet_link_discovery_start(void)
219{
220 int rc;
221
222 rc = loc_register_cat_change_cb(inet_link_cat_change_cb);
223 if (rc != EOK) {
224 log_msg(LVL_ERROR, "Failed registering callback for IP link "
225 "discovery (%d).", rc);
226 return rc;
227 }
228
229 return inet_link_check_new();
230}
231
232/** Send datagram over Internet link */
233int inet_link_send_dgram(inet_link_t *ilink, inet_addr_t *lsrc,
234 inet_addr_t *ldest, inet_dgram_t *dgram, uint8_t proto, uint8_t ttl, int df)
235{
236 iplink_sdu_t sdu;
237 inet_packet_t packet;
238 int rc;
239
240 /* XXX Fragment packet */
241 packet.src = dgram->src;
242 packet.dest = dgram->dest;
243 packet.tos = dgram->tos;
244 packet.proto = proto;
245 packet.ttl = ttl;
246 packet.df = df;
247 packet.data = dgram->data;
248 packet.size = dgram->size;
249
250 sdu.lsrc.ipv4 = lsrc->ipv4;
251 sdu.ldest.ipv4 = ldest->ipv4;
252 rc = inet_pdu_encode(&packet, &sdu.data, &sdu.size);
253 if (rc != EOK)
254 return rc;
255
256 rc = iplink_send(ilink->iplink, &sdu);
257 free(sdu.data);
258
259 return rc;
260}
261
262
263/** @}
264 */
Note: See TracBrowser for help on using the repository browser.