+static int
+ssh_request_reply(AuthenticationConnection *auth, Buffer *request, Buffer *reply)
+{
+ u_int l, len;
+ char buf[1024];
+
+ /* Get the length of the message, and format it in the buffer. */
+ len = buffer_len(request);
+ put_u32(buf, len);
+
+ /* Send the length and then the packet to the agent. */
+ if (atomicio(vwrite, auth->fd, buf, 4) != 4 ||
+ atomicio(vwrite, auth->fd, buffer_ptr(request),
+ buffer_len(request)) != buffer_len(request)) {
+ error("Error writing to authentication socket.");
+ return 0;
+ }
+ /*
+ * Wait for response from the agent. First read the length of the
+ * response packet.
+ */
+ if (atomicio(read, auth->fd, buf, 4) != 4) {
+ error("Error reading response length from authentication socket.");
+ return 0;
+ }
+
+ /* Extract the length, and check it for sanity. */
+ len = get_u32(buf);
+ if (len > 256 * 1024)
+ fatal("Authentication response too long: %u", len);
+
+ /* Read the rest of the response in to the buffer. */
+ buffer_clear(reply);
+ while (len > 0) {
+ l = len;
+ if (l > sizeof(buf))
+ l = sizeof(buf);
+ if (atomicio(read, auth->fd, buf, l) != l) {
+ error("Error reading response from authentication socket.");
+ return 0;
+ }
+ buffer_append(reply, buf, l);
+ len -= l;
+ }
+ return 1;
+}
+