Changeset ebb1489 in mainline for uspace/srv/net/inetsrv/addrobj.c
- Timestamp:
- 2024-10-13T08:23:40Z (8 weeks 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/addrobj.c
r2a0c827c rebb1489 1 1 /* 2 * Copyright (c) 202 1Jiri Svoboda2 * Copyright (c) 2024 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 41 41 #include <io/log.h> 42 42 #include <ipc/loc.h> 43 #include <sif.h> 44 #include <stdio.h> 43 45 #include <stdlib.h> 44 46 #include <str.h> … … 211 213 } 212 214 215 /** Count number of non-temporary address objects configured for link. 216 * 217 * @param ilink Inet link 218 * @return Number of address objects configured for this link 219 */ 220 unsigned inet_addrobj_cnt_by_link(inet_link_t *ilink) 221 { 222 unsigned cnt; 223 224 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_addrobj_cnt_by_link()"); 225 226 fibril_mutex_lock(&addr_list_lock); 227 228 cnt = 0; 229 list_foreach(addr_list, addr_list, inet_addrobj_t, naddr) { 230 if (naddr->ilink == ilink && naddr->temp == false) 231 ++cnt; 232 } 233 234 fibril_mutex_unlock(&addr_list_lock); 235 return cnt; 236 } 237 213 238 /** Send datagram from address object */ 214 239 errno_t inet_addrobj_send_dgram(inet_addrobj_t *addr, inet_addr_t *ldest, … … 282 307 } 283 308 309 /** Load address object from SIF node. 310 * 311 * @param anode SIF node to load address object from 312 * @return EOK on success or an error code 313 */ 314 static errno_t inet_addrobj_load(sif_node_t *anode) 315 { 316 errno_t rc; 317 const char *sid; 318 const char *snaddr; 319 const char *slink; 320 const char *name; 321 char *endptr; 322 int id; 323 inet_naddr_t naddr; 324 inet_addrobj_t *addr; 325 inet_link_t *link; 326 327 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_addrobj_load()"); 328 329 sid = sif_node_get_attr(anode, "id"); 330 if (sid == NULL) 331 return EIO; 332 333 snaddr = sif_node_get_attr(anode, "naddr"); 334 if (snaddr == NULL) 335 return EIO; 336 337 slink = sif_node_get_attr(anode, "link"); 338 if (slink == NULL) 339 return EIO; 340 341 name = sif_node_get_attr(anode, "name"); 342 if (name == NULL) 343 return EIO; 344 345 log_msg(LOG_DEFAULT, LVL_NOTE, "inet_addrobj_load(): id='%s' " 346 "naddr='%s' link='%s' name='%s'", sid, snaddr, slink, name); 347 348 id = strtoul(sid, &endptr, 10); 349 if (*endptr != '\0') 350 return EIO; 351 352 rc = inet_naddr_parse(snaddr, &naddr, NULL); 353 if (rc != EOK) 354 return EIO; 355 356 link = inet_link_get_by_svc_name(slink); 357 if (link == NULL) { 358 log_msg(LOG_DEFAULT, LVL_ERROR, "Link '%s' not found", 359 slink); 360 return EIO; 361 } 362 363 addr = inet_addrobj_new(); 364 if (addr == NULL) 365 return ENOMEM; 366 367 addr->id = id; 368 addr->naddr = naddr; 369 addr->ilink = link; 370 addr->name = str_dup(name); 371 372 if (addr->name == NULL) { 373 inet_addrobj_delete(addr); 374 return ENOMEM; 375 } 376 377 inet_addrobj_add(addr); 378 return EOK; 379 } 380 381 /** Load address objects from SIF node. 382 * 383 * @param naddrs SIF node to load address objects from 384 * @return EOK on success or an error code 385 */ 386 errno_t inet_addrobjs_load(sif_node_t *naddrs) 387 { 388 sif_node_t *naddr; 389 const char *ntype; 390 errno_t rc; 391 392 naddr = sif_node_first_child(naddrs); 393 while (naddr != NULL) { 394 ntype = sif_node_get_type(naddr); 395 if (str_cmp(ntype, "address") != 0) { 396 rc = EIO; 397 goto error; 398 } 399 400 rc = inet_addrobj_load(naddr); 401 if (rc != EOK) 402 goto error; 403 404 naddr = sif_node_next_child(naddr); 405 } 406 407 return EOK; 408 error: 409 return rc; 410 411 } 412 413 /** Save address object to SIF node. 414 * 415 * @param addr Address object 416 * @param naddr SIF node to save addres to 417 * @return EOK on success or an error code 418 */ 419 static errno_t inet_addrobj_save(inet_addrobj_t *addr, sif_node_t *naddr) 420 { 421 char *str = NULL; 422 errno_t rc; 423 int rv; 424 425 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_addrobj_save(%p, %p)", 426 addr, naddr); 427 428 /* id */ 429 430 rv = asprintf(&str, "%zu", addr->id); 431 if (rv < 0) { 432 str = NULL; 433 rc = ENOMEM; 434 goto error; 435 } 436 437 rc = sif_node_set_attr(naddr, "id", str); 438 if (rc != EOK) 439 goto error; 440 441 free(str); 442 str = NULL; 443 444 /* dest */ 445 446 rc = inet_naddr_format(&addr->naddr, &str); 447 if (rc != EOK) 448 goto error; 449 450 rc = sif_node_set_attr(naddr, "naddr", str); 451 if (rc != EOK) 452 goto error; 453 454 free(str); 455 str = NULL; 456 457 /* link */ 458 459 rc = sif_node_set_attr(naddr, "link", addr->ilink->svc_name); 460 if (rc != EOK) 461 goto error; 462 463 /* name */ 464 465 rc = sif_node_set_attr(naddr, "name", addr->name); 466 if (rc != EOK) 467 goto error; 468 469 free(str); 470 471 return rc; 472 error: 473 if (str != NULL) 474 free(str); 475 return rc; 476 } 477 478 /** Save address objects to SIF node. 479 * 480 * @param cnode SIF node to save address objects to 481 * @return EOK on success or an error code 482 */ 483 errno_t inet_addrobjs_save(sif_node_t *cnode) 484 { 485 sif_node_t *naddr; 486 errno_t rc; 487 488 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_addrobjs_save()"); 489 490 fibril_mutex_lock(&addr_list_lock); 491 492 list_foreach(addr_list, addr_list, inet_addrobj_t, addr) { 493 if (addr->temp == false) { 494 rc = sif_node_append_child(cnode, "address", &naddr); 495 if (rc != EOK) 496 goto error; 497 498 rc = inet_addrobj_save(addr, naddr); 499 if (rc != EOK) 500 goto error; 501 } 502 } 503 504 fibril_mutex_unlock(&addr_list_lock); 505 return EOK; 506 error: 507 fibril_mutex_unlock(&addr_list_lock); 508 return rc; 509 } 510 284 511 /** @} 285 512 */
Note:
See TracChangeset
for help on using the changeset viewer.