* Receive a control message and convert the arguments to ASCII */
| 355 | * Receive a control message and convert the arguments to ASCII |
| 356 | */ |
| 357 | int |
| 358 | NgRecvAsciiMsg(int cs, struct ng_mesg *reply, size_t replen, char *path) |
| 359 | { |
| 360 | struct ng_mesg *msg, *ascii; |
| 361 | int bufSize, errnosv; |
| 362 | u_char *buf; |
| 363 | |
| 364 | /* Allocate buffer */ |
| 365 | bufSize = 2 * sizeof(*reply) + replen; |
| 366 | if ((buf = malloc(bufSize)) == NULL) |
| 367 | return (-1); |
| 368 | msg = (struct ng_mesg *)buf; |
| 369 | ascii = (struct ng_mesg *)msg->data; |
| 370 | |
| 371 | /* Get binary message */ |
| 372 | if (NgRecvMsg(cs, msg, bufSize, path) < 0) |
| 373 | goto fail; |
| 374 | memcpy(reply, msg, sizeof(*msg)); |
| 375 | |
| 376 | /* Ask originating node to convert the arguments to ASCII */ |
| 377 | if (NgSendMsg(cs, path, NGM_GENERIC_COOKIE, |
| 378 | NGM_BINARY2ASCII, msg, sizeof(*msg) + msg->header.arglen) < 0) |
| 379 | goto fail; |
| 380 | if (NgRecvMsg(cs, msg, bufSize, NULL) < 0) |
| 381 | goto fail; |
| 382 | |
| 383 | /* Copy result to client buffer */ |
| 384 | if (sizeof(*ascii) + ascii->header.arglen > replen) { |
| 385 | errno = ERANGE; |
| 386 | fail: |
| 387 | errnosv = errno; |
| 388 | free(buf); |
| 389 | errno = errnosv; |
| 390 | return (-1); |
| 391 | } |
| 392 | strncpy(reply->data, ascii->data, ascii->header.arglen); |
| 393 | |
| 394 | /* Done */ |
| 395 | free(buf); |
| 396 | return (0); |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | * Identical to NgRecvAsciiMsg() except buffer is dynamically allocated. |