* Send a message to a node using control socket node "cs". * Returns -1 if error and sets errno appropriately, otherwise zero. */
| 201 | * Returns -1 if error and sets errno appropriately, otherwise zero. |
| 202 | */ |
| 203 | static int |
| 204 | NgDeliverMsg(int cs, const char *path, |
| 205 | const struct ng_mesg *hdr, const void *args, size_t arglen) |
| 206 | { |
| 207 | u_char sgbuf[NG_PATHSIZ + NGSA_OVERHEAD]; |
| 208 | struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf; |
| 209 | u_char *buf = NULL; |
| 210 | struct ng_mesg *msg; |
| 211 | int errnosv = 0; |
| 212 | int rtn = 0; |
| 213 | |
| 214 | /* Sanity check */ |
| 215 | if (args == NULL) |
| 216 | arglen = 0; |
| 217 | |
| 218 | /* Get buffer */ |
| 219 | if ((buf = malloc(sizeof(*msg) + arglen)) == NULL) { |
| 220 | errnosv = errno; |
| 221 | if (_gNgDebugLevel >= 1) |
| 222 | NGLOG("malloc"); |
| 223 | rtn = -1; |
| 224 | goto done; |
| 225 | } |
| 226 | msg = (struct ng_mesg *) buf; |
| 227 | |
| 228 | /* Finalize message */ |
| 229 | *msg = *hdr; |
| 230 | msg->header.arglen = arglen; |
| 231 | memcpy(msg->data, args, arglen); |
| 232 | |
| 233 | /* Prepare socket address */ |
| 234 | sg->sg_family = AF_NETGRAPH; |
| 235 | /* XXX handle overflow */ |
| 236 | #if __GNUC__ >= 13 |
| 237 | #pragma GCC diagnostic push |
| 238 | #pragma GCC diagnostic ignored "-Wstringop-overflow" |
| 239 | #endif |
| 240 | strncpy(sg->sg_data, path, NG_PATHSIZ); |
| 241 | #if __GNUC__ >= 13 |
| 242 | #pragma GCC diagnostic pop |
| 243 | #endif |
| 244 | sg->sg_len = strlen(sg->sg_data) + 1 + NGSA_OVERHEAD; |
| 245 | |
| 246 | /* Debugging */ |
| 247 | if (_gNgDebugLevel >= 2) { |
| 248 | NGLOGX("SENDING %s:", |
| 249 | (msg->header.flags & NGF_RESP) ? "RESPONSE" : "MESSAGE"); |
| 250 | _NgDebugSockaddr(sg); |
| 251 | _NgDebugMsg(msg, sg->sg_data); |
| 252 | } |
| 253 | |
| 254 | /* Send it */ |
| 255 | if (sendto(cs, msg, sizeof(*msg) + arglen, |
| 256 | 0, (struct sockaddr *) sg, sg->sg_len) < 0) { |
| 257 | errnosv = errno; |
| 258 | if (_gNgDebugLevel >= 1) |
| 259 | NGLOG("sendto(%s)", sg->sg_data); |
| 260 | rtn = -1; |
no test coverage detected