5 /* (c) Copyright 1988 by the Massachusetts Institute of Technology. */
6 /* For copying and distribution information, please see the file */
7 /* <mit-copyright.h>. */
10 static char *rcsid_send_file_c = "$Header$";
13 #include <mit-copyright.h>
28 extern CONNECTION conn;
31 extern C_Block session;
35 * (already sent) pathname file_size checksum
36 * <<< (int)code can we send it?
42 * >>> data (last block)
43 * <<< 0 (on final write, close, sync, checksum)
47 * 1 on error (file not found, etc)
51 send_file(pathname, target_path, encrypt)
56 int n, fd, code, n_to_send, i;
58 unsigned char dst[UPDATE_BUFSIZ + 8], *src;
60 des_key_schedule sched;
64 fd = open(pathname, O_RDONLY, 0);
66 com_err(whoami, errno, "unable to open %s for read", pathname);
69 if (fstat(fd, &statb)) {
70 com_err(whoami, errno, "unable to stat %s", pathname);
74 n_to_send = statb.st_size;
76 string_alloc(&data, UPDATE_BUFSIZ);
77 sprintf(STRING_DATA(data), "XFER_00%c %d %d %s",
78 (encrypt ? '3' : '2'), n_to_send,
79 checksum_file(pathname), target_path);
80 code = send_object(conn, (char *)&data, STRING_T);
82 com_err(whoami, code, " sending XFER request");
86 code = receive_object(conn, (char *)&n, INTEGER_T);
88 com_err(whoami, code, " getting reply from XFER request");
93 com_err(whoami, n, " transfer request (XFER) rejected");
98 code = receive_object(conn, (char *)&n, INTEGER_T);
100 com_err(whoami, connection_errno(conn), ": lost connection");
105 com_err(whoami, n, " from remote server: can't update %s",
113 printf("Session key %02x %02x %02x %02x %02x %02x %02x %02x\n",
114 session[0], session[1], session[2], session[3],
115 session[4], session[5], session[6], session[7]);
117 des_key_sched(session, sched);
119 memmove(ivec, session, sizeof(ivec));
121 bcopy(session, ivec, sizeof(ivec));
125 while (n_to_send > 0) {
127 printf("n_to_send = %d\n", n_to_send);
129 n = read(fd, STRING_DATA(data), UPDATE_BUFSIZ);
131 com_err(whoami, errno, " reading %s for transmission", pathname);
135 MAX_STRING_SIZE(data) = n;
137 src = (unsigned char *)STRING_DATA(data);
139 memmove(dst, src, n);
143 memset(dst + n, 0, 7);
145 des_pcbc_encrypt(dst, src, n, sched, ivec, 0);
146 /* save vector to continue chaining */
147 for (i = 0; i < 8; i++)
148 ivec[i] = dst[n - 8 + i] ^ src[n - 8 + i];
149 /* round up to multiple of 8 */
150 data.length = (data.length + 7) & 0xfffffff8;
152 code = send_object(conn, (char *)&data, STRING_T);
154 com_err(whoami, connection_errno(conn), " transmitting file %s",
160 code = receive_object(conn, (char *)&n, INTEGER_T);
162 com_err(whoami, connection_errno(conn),
163 " awaiting ACK remote server during transmission of %s",
169 com_err(whoami, n, " from remote server during transmission of %s",
175 if (statb.st_size == 0) {
176 code = receive_object(conn, (char *)&n, INTEGER_T);
178 com_err(whoami, connection_errno(conn),
179 " awaiting ACK remote server after transmission of %s",
185 com_err(whoami, n, " from remote server after transmission of %s",