* Create a socket type node and give it the supplied name. * Return data and control sockets corresponding to the node. * Returns -1 if error and sets errno. */
| 59 | * Returns -1 if error and sets errno. |
| 60 | */ |
| 61 | int |
| 62 | NgMkSockNode(const char *name, int *csp, int *dsp) |
| 63 | { |
| 64 | char namebuf[NG_NODESIZ]; |
| 65 | int cs = -1; /* control socket */ |
| 66 | int ds = -1; /* data socket */ |
| 67 | int errnosv; |
| 68 | |
| 69 | /* Empty name means no name */ |
| 70 | if (name && *name == 0) |
| 71 | name = NULL; |
| 72 | |
| 73 | /* Create control socket; this also creates the netgraph node. |
| 74 | If we get an EAFNOSUPPORT then the socket node type is |
| 75 | not loaded, so load it and try again. */ |
| 76 | if ((cs = socket(AF_NETGRAPH, SOCK_DGRAM, NG_CONTROL)) < 0) { |
| 77 | #ifndef FSTACK |
| 78 | if (errno == EAFNOSUPPORT) { |
| 79 | if (kldload(NG_SOCKET_KLD) < 0) { |
| 80 | errnosv = errno; |
| 81 | if (_gNgDebugLevel >= 1) |
| 82 | NGLOG("can't load %s", NG_SOCKET_KLD); |
| 83 | goto errout; |
| 84 | } |
| 85 | cs = socket(AF_NETGRAPH, SOCK_DGRAM, NG_CONTROL); |
| 86 | if (cs >= 0) |
| 87 | goto gotNode; |
| 88 | } |
| 89 | #endif |
| 90 | errnosv = errno; |
| 91 | if (_gNgDebugLevel >= 1) |
| 92 | NGLOG("socket"); |
| 93 | goto errout; |
| 94 | } |
| 95 | |
| 96 | #ifndef FSTACK |
| 97 | gotNode: |
| 98 | #endif |
| 99 | /* Assign the node the desired name, if any */ |
| 100 | if (name != NULL) { |
| 101 | u_char sbuf[NG_NODESIZ + NGSA_OVERHEAD]; |
| 102 | struct sockaddr_ng *const sg = (struct sockaddr_ng *) sbuf; |
| 103 | |
| 104 | /* Assign name */ |
| 105 | strlcpy(sg->sg_data, name, NG_NODESIZ); |
| 106 | sg->sg_family = AF_NETGRAPH; |
| 107 | sg->sg_len = strlen(sg->sg_data) + 1 + NGSA_OVERHEAD; |
| 108 | if (bind(cs, (struct sockaddr *) sg, sg->sg_len) < 0) { |
| 109 | errnosv = errno; |
| 110 | if (_gNgDebugLevel >= 1) |
| 111 | NGLOG("bind(%s)", sg->sg_data); |
| 112 | goto errout; |
| 113 | } |
| 114 | |
| 115 | /* Save node name */ |
| 116 | strlcpy(namebuf, name, sizeof(namebuf)); |
| 117 | } else if (dsp != NULL) { |
| 118 | union { |