source: mainline/uspace/srv/net/nconfsrv/iplink.c@ 368ee04

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

Do not forget to add ncs link to list.

  • Property mode set to 100644
File size: 5.6 KB
Line 
1/*
2 * Copyright (c) 2013 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 nconfsrv
30 * @{
31 */
32/**
33 * @file
34 * @brief
35 */
36
37#include <stdbool.h>
38#include <errno.h>
39#include <fibril_synch.h>
40#include <inet/dhcp.h>
41#include <inet/inetcfg.h>
42#include <io/log.h>
43#include <loc.h>
44#include <stdlib.h>
45#include <str.h>
46
47#include "iplink.h"
48#include "nconfsrv.h"
49
50static int ncs_link_add(service_id_t);
51
52static LIST_INITIALIZE(ncs_links);
53static FIBRIL_MUTEX_INITIALIZE(ncs_links_lock);
54
55static int ncs_link_check_new(void)
56{
57 bool already_known;
58 category_id_t iplink_cat;
59 service_id_t *svcs;
60 size_t count, i;
61 int rc;
62
63 fibril_mutex_lock(&ncs_links_lock);
64
65 rc = loc_category_get_id("iplink", &iplink_cat, IPC_FLAG_BLOCKING);
66 if (rc != EOK) {
67 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed resolving category 'iplink'.");
68 fibril_mutex_unlock(&ncs_links_lock);
69 return ENOENT;
70 }
71
72 rc = loc_category_get_svcs(iplink_cat, &svcs, &count);
73 if (rc != EOK) {
74 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting list of IP links.");
75 fibril_mutex_unlock(&ncs_links_lock);
76 return EIO;
77 }
78
79 for (i = 0; i < count; i++) {
80 already_known = false;
81
82 list_foreach(ncs_links, link_list, ncs_link_t, ilink) {
83 if (ilink->svc_id == svcs[i]) {
84 already_known = true;
85 break;
86 }
87 }
88
89 if (!already_known) {
90 log_msg(LOG_DEFAULT, LVL_NOTE, "Found IP link '%lu'",
91 (unsigned long) svcs[i]);
92 rc = ncs_link_add(svcs[i]);
93 if (rc != EOK)
94 log_msg(LOG_DEFAULT, LVL_ERROR, "Could not add IP link.");
95 }
96 }
97
98 fibril_mutex_unlock(&ncs_links_lock);
99 return EOK;
100}
101
102static ncs_link_t *ncs_link_new(void)
103{
104 ncs_link_t *nlink = calloc(1, sizeof(ncs_link_t));
105
106 if (nlink == NULL) {
107 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating link structure. "
108 "Out of memory.");
109 return NULL;
110 }
111
112 link_initialize(&nlink->link_list);
113
114 return nlink;
115}
116
117static void ncs_link_delete(ncs_link_t *nlink)
118{
119 if (nlink->svc_name != NULL)
120 free(nlink->svc_name);
121
122 free(nlink);
123}
124
125static int ncs_link_add(service_id_t sid)
126{
127 ncs_link_t *nlink;
128 int rc;
129
130 assert(fibril_mutex_is_locked(&ncs_links_lock));
131
132 log_msg(LOG_DEFAULT, LVL_DEBUG, "ncs_link_add()");
133 nlink = ncs_link_new();
134 if (nlink == NULL)
135 return ENOMEM;
136
137 nlink->svc_id = sid;
138
139 rc = loc_service_get_name(sid, &nlink->svc_name);
140 if (rc != EOK) {
141 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting service name.");
142 goto error;
143 }
144
145 log_msg(LOG_DEFAULT, LVL_NOTE, "Configure link %s", nlink->svc_name);
146 rc = inetcfg_link_add(sid);
147 if (rc != EOK) {
148 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed configuring link "
149 "'%s'.\n", nlink->svc_name);
150 goto error;
151 }
152
153 if (str_lcmp(nlink->svc_name, "net/eth", str_length("net/eth")) == 0) {
154 rc = dhcp_link_add(sid);
155 if (rc != EOK) {
156 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed configuring DHCP on "
157 " link '%s'.\n", nlink->svc_name);
158 goto error;
159 }
160 }
161
162 list_append(&nlink->link_list, &ncs_links);
163
164 return EOK;
165
166error:
167 ncs_link_delete(nlink);
168 return rc;
169}
170
171static void ncs_link_cat_change_cb(void)
172{
173 (void) ncs_link_check_new();
174}
175
176int ncs_link_discovery_start(void)
177{
178 int rc;
179
180 rc = loc_register_cat_change_cb(ncs_link_cat_change_cb);
181 if (rc != EOK) {
182 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering callback for IP link "
183 "discovery (%d).", rc);
184 return rc;
185 }
186
187 return ncs_link_check_new();
188}
189
190ncs_link_t *ncs_link_get_by_id(sysarg_t link_id)
191{
192 fibril_mutex_lock(&ncs_links_lock);
193
194 list_foreach(ncs_links, link_list, ncs_link_t, nlink) {
195 if (nlink->svc_id == link_id) {
196 fibril_mutex_unlock(&ncs_links_lock);
197 return nlink;
198 }
199 }
200
201 fibril_mutex_unlock(&ncs_links_lock);
202 return NULL;
203}
204
205/** Get IDs of all links. */
206int ncs_link_get_id_list(sysarg_t **rid_list, size_t *rcount)
207{
208 sysarg_t *id_list;
209 size_t count, i;
210
211 fibril_mutex_lock(&ncs_links_lock);
212 count = list_count(&ncs_links);
213
214 id_list = calloc(count, sizeof(sysarg_t));
215 if (id_list == NULL) {
216 fibril_mutex_unlock(&ncs_links_lock);
217 return ENOMEM;
218 }
219
220 i = 0;
221 list_foreach(ncs_links, link_list, ncs_link_t, nlink) {
222 id_list[i++] = nlink->svc_id;
223 log_msg(LOG_DEFAULT, LVL_NOTE, "add link to list");
224 }
225
226 fibril_mutex_unlock(&ncs_links_lock);
227
228 log_msg(LOG_DEFAULT, LVL_NOTE, "return %zu links", count);
229 *rid_list = id_list;
230 *rcount = count;
231
232 return EOK;
233}
234
235/** @}
236 */
Note: See TracBrowser for help on using the repository browser.