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