* Send a message given in ASCII format. We first ask the node to translate * the command into binary, and then we send the binary. */
| 113 | * the command into binary, and then we send the binary. |
| 114 | */ |
| 115 | int |
| 116 | NgSendAsciiMsg(int cs, const char *path, const char *fmt, ...) |
| 117 | { |
| 118 | struct ng_mesg *reply, *binary, *ascii; |
| 119 | char *buf, *cmd, *args; |
| 120 | va_list fmtargs; |
| 121 | int token; |
| 122 | |
| 123 | /* Parse out command and arguments */ |
| 124 | va_start(fmtargs, fmt); |
| 125 | vasprintf(&buf, fmt, fmtargs); |
| 126 | va_end(fmtargs); |
| 127 | if (buf == NULL) |
| 128 | return (-1); |
| 129 | |
| 130 | /* Parse out command, arguments */ |
| 131 | for (cmd = buf; isspace(*cmd); cmd++) |
| 132 | ; |
| 133 | for (args = cmd; *args != '\0' && !isspace(*args); args++) |
| 134 | ; |
| 135 | if (*args != '\0') { |
| 136 | while (isspace(*args)) |
| 137 | *args++ = '\0'; |
| 138 | } |
| 139 | |
| 140 | /* Get a bigger buffer to hold inner message header plus arg string */ |
| 141 | if ((ascii = malloc(sizeof(struct ng_mesg) |
| 142 | + strlen(args) + 1)) == NULL) { |
| 143 | free(buf); |
| 144 | return (-1); |
| 145 | } |
| 146 | memset(ascii, 0, sizeof(*ascii)); |
| 147 | |
| 148 | /* Build inner header (only need cmdstr, arglen, and data fields) */ |
| 149 | strncpy((char *)ascii->header.cmdstr, cmd, |
| 150 | sizeof(ascii->header.cmdstr) - 1); |
| 151 | strcpy(ascii->data, args); |
| 152 | ascii->header.arglen = strlen(ascii->data) + 1; |
| 153 | free(buf); |
| 154 | |
| 155 | /* Send node a request to convert ASCII to binary */ |
| 156 | if (NgSendMsg(cs, path, NGM_GENERIC_COOKIE, NGM_ASCII2BINARY, |
| 157 | (u_char *)ascii, sizeof(*ascii) + ascii->header.arglen) < 0) { |
| 158 | free(ascii); |
| 159 | return (-1); |
| 160 | } |
| 161 | free(ascii); |
| 162 | |
| 163 | /* Get reply */ |
| 164 | if (NgAllocRecvMsg(cs, &reply, NULL) < 0) |
| 165 | return (-1); |
| 166 | |
| 167 | /* Now send binary version */ |
| 168 | binary = (struct ng_mesg *)reply->data; |
| 169 | binary->header.token = atomic_fetch_add(&gMsgId, 1) & INT_MAX; |
| 170 | binary->header.version = NG_VERSION; |
| 171 | if (NgDeliverMsg(cs, |
| 172 | path, binary, binary->data, binary->header.arglen) < 0) { |
no test coverage detected