Changeset ebb1489 in mainline for uspace/srv/net/inetsrv/sroute.c
- Timestamp:
- 2024-10-13T08:23:40Z (2 months ago)
- Children:
- 0472cf17
- Parents:
- 2a0c827c (diff), b3b79981 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - git-author:
- boba-buba <120932204+boba-buba@…> (2024-10-13 08:23:40)
- git-committer:
- GitHub <noreply@…> (2024-10-13 08:23:40)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/inetsrv/sroute.c
r2a0c827c rebb1489 1 1 /* 2 * Copyright (c) 20 12Jiri Svoboda2 * Copyright (c) 2024 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 40 40 #include <io/log.h> 41 41 #include <ipc/loc.h> 42 #include <sif.h> 43 #include <stdio.h> 42 44 #include <stdlib.h> 43 45 #include <str.h> … … 210 212 } 211 213 214 /** Load static route from SIF node. 215 * 216 * @param nroute SIF node to load static route from 217 * @return EOK on success or an error code 218 */ 219 static errno_t inet_sroute_load(sif_node_t *nroute) 220 { 221 errno_t rc; 222 const char *sid; 223 const char *sdest; 224 const char *srouter; 225 const char *name; 226 char *endptr; 227 int id; 228 inet_naddr_t dest; 229 inet_addr_t router; 230 inet_sroute_t *sroute; 231 232 sid = sif_node_get_attr(nroute, "id"); 233 if (sid == NULL) 234 return EIO; 235 236 sdest = sif_node_get_attr(nroute, "dest"); 237 if (sdest == NULL) 238 return EIO; 239 240 srouter = sif_node_get_attr(nroute, "router"); 241 if (srouter == NULL) 242 return EIO; 243 244 name = sif_node_get_attr(nroute, "name"); 245 if (name == NULL) 246 return EIO; 247 248 id = strtoul(sid, &endptr, 10); 249 if (*endptr != '\0') 250 return EIO; 251 252 rc = inet_naddr_parse(sdest, &dest, NULL); 253 if (rc != EOK) 254 return EIO; 255 256 rc = inet_addr_parse(srouter, &router, NULL); 257 if (rc != EOK) 258 return EIO; 259 260 sroute = inet_sroute_new(); 261 if (sroute == NULL) 262 return ENOMEM; 263 264 sroute->id = id; 265 sroute->dest = dest; 266 sroute->router = router; 267 sroute->name = str_dup(name); 268 269 if (sroute->name == NULL) { 270 inet_sroute_delete(sroute); 271 return ENOMEM; 272 } 273 274 inet_sroute_add(sroute); 275 return EOK; 276 } 277 278 /** Load static routes from SIF node. 279 * 280 * @param nroutes SIF node to load static routes from 281 * @return EOK on success or an error code 282 */ 283 errno_t inet_sroutes_load(sif_node_t *nroutes) 284 { 285 sif_node_t *nroute; 286 const char *ntype; 287 errno_t rc; 288 289 nroute = sif_node_first_child(nroutes); 290 while (nroute != NULL) { 291 ntype = sif_node_get_type(nroute); 292 if (str_cmp(ntype, "route") != 0) { 293 rc = EIO; 294 goto error; 295 } 296 297 rc = inet_sroute_load(nroute); 298 if (rc != EOK) 299 goto error; 300 301 nroute = sif_node_next_child(nroute); 302 } 303 304 return EOK; 305 error: 306 return rc; 307 } 308 309 /** Save static route to SIF node. 310 * 311 * @param sroute Static route 312 * @param nroute SIF node to save static route to 313 * @return EOK on success or an error code 314 */ 315 static errno_t inet_sroute_save(inet_sroute_t *sroute, sif_node_t *nroute) 316 { 317 char *str = NULL; 318 errno_t rc; 319 int rv; 320 321 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_sroute_save(%p, %p)", 322 sroute, nroute); 323 324 /* id */ 325 326 rv = asprintf(&str, "%zu", sroute->id); 327 if (rv < 0) { 328 str = NULL; 329 rc = ENOMEM; 330 goto error; 331 } 332 333 rc = sif_node_set_attr(nroute, "id", str); 334 if (rc != EOK) 335 goto error; 336 337 free(str); 338 str = NULL; 339 340 /* dest */ 341 342 rc = inet_naddr_format(&sroute->dest, &str); 343 if (rc != EOK) 344 goto error; 345 346 rc = sif_node_set_attr(nroute, "dest", str); 347 if (rc != EOK) 348 goto error; 349 350 free(str); 351 str = NULL; 352 353 /* router */ 354 355 rc = inet_addr_format(&sroute->router, &str); 356 if (rc != EOK) 357 goto error; 358 359 rc = sif_node_set_attr(nroute, "router", str); 360 if (rc != EOK) 361 goto error; 362 363 /* name */ 364 365 rc = sif_node_set_attr(nroute, "name", sroute->name); 366 if (rc != EOK) 367 goto error; 368 369 free(str); 370 371 return rc; 372 error: 373 if (str != NULL) 374 free(str); 375 return rc; 376 } 377 378 /** Save static routes to SIF node. 379 * 380 * @param nroutes SIF node to save static routes to 381 * @return EOK on success or an error code 382 */ 383 errno_t inet_sroutes_save(sif_node_t *nroutes) 384 { 385 sif_node_t *nroute; 386 errno_t rc; 387 388 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_sroutes_save()"); 389 390 fibril_mutex_lock(&sroute_list_lock); 391 392 list_foreach(sroute_list, sroute_list, inet_sroute_t, sroute) { 393 if (sroute->temp == false) { 394 rc = sif_node_append_child(nroutes, "route", &nroute); 395 if (rc != EOK) 396 goto error; 397 398 rc = inet_sroute_save(sroute, nroute); 399 if (rc != EOK) 400 goto error; 401 } 402 } 403 404 fibril_mutex_unlock(&sroute_list_lock); 405 return EOK; 406 error: 407 fibril_mutex_unlock(&sroute_list_lock); 408 return rc; 409 } 410 212 411 /** @} 213 412 */
Note:
See TracChangeset
for help on using the changeset viewer.