MCPcopy Create free account
hub / github.com/F-Stack/f-stack / ng_mkpeer

Function ng_mkpeer

freebsd/netgraph/ng_base.c:1540–1596  ·  view source on GitHub ↗

* Make a peer and connect. * We assume that the local node is locked. * The new node probably doesn't need a lock until * it has a hook, because it cannot really have any work until then, * but we should think about it a bit more. * * The problem may come if the other node also fires up * some hardware or a timer or some other source of activation, * also it may already get a command msg v

Source from the content-addressed store, hash-verified

1538 * an unconnected node?
1539 */
1540static int
1541ng_mkpeer(node_p node, const char *name, const char *name2, char *type)
1542{
1543 node_p node2;
1544 hook_p hook1, hook2;
1545 int error;
1546
1547 if ((error = ng_make_node(type, &node2))) {
1548 return (error);
1549 }
1550
1551 if ((error = ng_add_hook(node, name, &hook1))) { /* gives us a ref */
1552 ng_rmnode(node2, NULL, NULL, 0);
1553 return (error);
1554 }
1555
1556 if ((error = ng_add_hook(node2, name2, &hook2))) {
1557 ng_rmnode(node2, NULL, NULL, 0);
1558 ng_destroy_hook(hook1);
1559 NG_HOOK_UNREF(hook1);
1560 return (error);
1561 }
1562
1563 /*
1564 * Actually link the two hooks together.
1565 */
1566 hook1->hk_peer = hook2;
1567 hook2->hk_peer = hook1;
1568
1569 /* Each hook is referenced by the other */
1570 NG_HOOK_REF(hook1);
1571 NG_HOOK_REF(hook2);
1572
1573 /* Give each node the opportunity to veto the pending connection */
1574 if (hook1->hk_node->nd_type->connect) {
1575 error = (*hook1->hk_node->nd_type->connect) (hook1);
1576 }
1577
1578 if ((error == 0) && hook2->hk_node->nd_type->connect) {
1579 error = (*hook2->hk_node->nd_type->connect) (hook2);
1580 }
1581
1582 /*
1583 * drop the references we were holding on the two hooks.
1584 */
1585 if (error) {
1586 ng_destroy_hook(hook2); /* also zaps hook1 */
1587 ng_rmnode(node2, NULL, NULL, 0);
1588 } else {
1589 /* As a last act, allow the hooks to be used */
1590 hook1->hk_flags &= ~HK_INVALID;
1591 hook2->hk_flags &= ~HK_INVALID;
1592 }
1593 NG_HOOK_UNREF(hook1);
1594 NG_HOOK_UNREF(hook2);
1595 return (error);
1596}
1597

Callers 1

ng_generic_msgFunction · 0.70

Calls 4

ng_make_nodeFunction · 0.70
ng_add_hookFunction · 0.70
ng_rmnodeFunction · 0.70
ng_destroy_hookFunction · 0.70

Tested by

no test coverage detected