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

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

Automatically start DHCP on ethernet links.

  • Property mode set to 100644
File size: 5.5 KB
RevLine 
[7af0cc5]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>
[bd88bee]40#include <inet/dhcp.h>
[7af0cc5]41#include <inet/inetcfg.h>
42#include <io/log.h>
43#include <loc.h>
44#include <stdlib.h>
[bd88bee]45#include <str.h>
[7af0cc5]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 log_msg(LOG_DEFAULT, LVL_DEBUG, "ncs_link_add()");
131 nlink = ncs_link_new();
132 if (nlink == NULL)
133 return ENOMEM;
134
135 nlink->svc_id = sid;
136
137 rc = loc_service_get_name(sid, &nlink->svc_name);
138 if (rc != EOK) {
139 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting service name.");
140 goto error;
141 }
142
[bd88bee]143 log_msg(LOG_DEFAULT, LVL_NOTE, "Configure link %s", nlink->svc_name);
[7af0cc5]144 rc = inetcfg_link_add(sid);
145 if (rc != EOK) {
146 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed configuring link "
147 "'%s'.\n", nlink->svc_name);
148 goto error;
149 }
150
[bd88bee]151 if (str_lcmp(nlink->svc_name, "net/eth", str_length("net/eth")) == 0) {
152 rc = dhcp_link_add(sid);
153 if (rc != EOK) {
154 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed configuring DHCP on "
155 " link '%s'.\n", nlink->svc_name);
156 goto error;
157 }
158 }
159
[7af0cc5]160 return EOK;
161
162error:
163 ncs_link_delete(nlink);
164 return rc;
165}
166
167static void ncs_link_cat_change_cb(void)
168{
169 (void) ncs_link_check_new();
170}
171
172int ncs_link_discovery_start(void)
173{
174 int rc;
175
176 rc = loc_register_cat_change_cb(ncs_link_cat_change_cb);
177 if (rc != EOK) {
178 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering callback for IP link "
179 "discovery (%d).", rc);
180 return rc;
181 }
182
183 return ncs_link_check_new();
184}
185
186ncs_link_t *ncs_link_get_by_id(sysarg_t link_id)
187{
188 fibril_mutex_lock(&ncs_links_lock);
189
190 list_foreach(ncs_links, link_list, ncs_link_t, nlink) {
191 if (nlink->svc_id == link_id) {
192 fibril_mutex_unlock(&ncs_links_lock);
193 return nlink;
194 }
195 }
196
197 fibril_mutex_unlock(&ncs_links_lock);
198 return NULL;
199}
200
201/** Get IDs of all links. */
202int ncs_link_get_id_list(sysarg_t **rid_list, size_t *rcount)
203{
204 sysarg_t *id_list;
205 size_t count, i;
206
207 fibril_mutex_lock(&ncs_links_lock);
208 count = list_count(&ncs_links);
209
210 id_list = calloc(count, sizeof(sysarg_t));
211 if (id_list == NULL) {
212 fibril_mutex_unlock(&ncs_links_lock);
213 return ENOMEM;
214 }
215
216 i = 0;
217 list_foreach(ncs_links, link_list, ncs_link_t, nlink) {
218 id_list[i++] = nlink->svc_id;
219 log_msg(LOG_DEFAULT, LVL_NOTE, "add link to list");
220 }
221
222 fibril_mutex_unlock(&ncs_links_lock);
223
224 log_msg(LOG_DEFAULT, LVL_NOTE, "return %zu links", count);
225 *rid_list = id_list;
226 *rcount = count;
227
228 return EOK;
229}
230
231/** @}
232 */
Note: See TracBrowser for help on using the repository browser.