int response(void) { char ch, *cp, resp, rbuf[BUFSIZ]; if (read(rem, &resp, sizeof(resp)) != sizeof(resp)) lostconn(0); cp = rbuf; switch(resp) { case 0: /* ok */ return (0); default: *cp++ = resp; /* FALLTHROUGH */ case 1: /* error, followed by error msg */ case 2: /* fatal error, "" */ do { if (read(rem, &ch, sizeof(ch)) != sizeof(ch)) lostconn(0); *cp++ = ch; } while (cp < &rbuf[BUFSIZ] && ch != '\n'); if (!iamremote) (void)write(STDERR_FILENO, rbuf, cp - rbuf); ++errs; if (resp == 1) return (-1); exit(1); } /* NOTREACHED */ }
int response(void) { char ch, *cp, resp, rbuf[2048], visbuf[2048]; if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp)) lostconn(0); cp = rbuf; switch (resp) { case 0: /* ok */ return (0); default: *cp++ = resp; /* FALLTHROUGH */ case 1: /* error, followed by error msg */ case 2: /* fatal error, "" */ do { if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch)) lostconn(0); *cp++ = ch; } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n'); if (!iamremote) { cp[-1] = '\0'; (void) snmprintf(visbuf, sizeof(visbuf), NULL, "%s\n", rbuf); (void) atomicio(vwrite, STDERR_FILENO, visbuf, strlen(visbuf)); } ++errs; if (resp == 1) return (-1); exit(1); } /* NOTREACHED */ }
void sink(int argc, char **argv) { static BUF buffer; struct stat stb; enum { YES, NO, DISPLAYED } wrerr; BUF *bp; off_t i; size_t j, count; int amt, exists, first, ofd; mode_t mode, omode, mask; off_t size, statbytes; unsigned long long ull; int setimes, targisdir, wrerrno = 0; char ch, *cp, *np, *targ, *why, *vect[1], buf[2048]; struct timeval tv[2]; #define atime tv[0] #define mtime tv[1] #define SCREWUP(str) { why = str; goto screwup; } setimes = targisdir = 0; mask = umask(0); if (!pflag) (void) umask(mask); if (argc != 1) { run_err("ambiguous target"); exit(1); } targ = *argv; if (targetshouldbedirectory) verifydir(targ); (void) atomicio(vwrite, remout, "", 1); if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode)) targisdir = 1; for (first = 1;; first = 0) { cp = buf; if (atomicio(read, remin, cp, 1) != 1) return; if (*cp++ == '\n') SCREWUP("unexpected <newline>"); do { if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch)) SCREWUP("lost connection"); *cp++ = ch; } while (cp < &buf[sizeof(buf) - 1] && ch != '\n'); *cp = 0; if (verbose_mode) fprintf(stderr, "Sink: %s", buf); if (buf[0] == '\01' || buf[0] == '\02') { if (iamremote == 0) (void) atomicio(vwrite, STDERR_FILENO, buf + 1, strlen(buf + 1)); if (buf[0] == '\02') exit(1); ++errs; continue; } if (buf[0] == 'E') { (void) atomicio(vwrite, remout, "", 1); return; } if (ch == '\n') *--cp = 0; cp = buf; if (*cp == 'T') { setimes++; cp++; if (!isdigit((unsigned char)*cp)) SCREWUP("mtime.sec not present"); ull = strtoull(cp, &cp, 10); if (!cp || *cp++ != ' ') SCREWUP("mtime.sec not delimited"); if ((time_t)ull < 0 || (unsigned long long)(time_t)ull != ull) setimes = 0; /* out of range */ mtime.tv_sec = ull; mtime.tv_usec = strtol(cp, &cp, 10); if (!cp || *cp++ != ' ' || mtime.tv_usec < 0 || mtime.tv_usec > 999999) SCREWUP("mtime.usec not delimited"); if (!isdigit((unsigned char)*cp)) SCREWUP("atime.sec not present"); ull = strtoull(cp, &cp, 10); if (!cp || *cp++ != ' ') SCREWUP("atime.sec not delimited"); if ((time_t)ull < 0 || (unsigned long long)(time_t)ull != ull) setimes = 0; /* out of range */ atime.tv_sec = ull; atime.tv_usec = strtol(cp, &cp, 10); if (!cp || *cp++ != '\0' || atime.tv_usec < 0 || atime.tv_usec > 999999) SCREWUP("atime.usec not delimited"); (void) atomicio(vwrite, remout, "", 1); continue; } if (*cp != 'C' && *cp != 'D') { /* * Check for the case "rcp remote:foo\* local:bar". * In this case, the line "No match." can be returned * by the shell before the rcp command on the remote is * executed so the ^Aerror_message convention isn't * followed. */ if (first) { run_err("%s", cp); exit(1); } SCREWUP("expected control record"); } mode = 0; for (++cp; cp < buf + 5; cp++) { if (*cp < '0' || *cp > '7') SCREWUP("bad mode"); mode = (mode << 3) | (*cp - '0'); } if (*cp++ != ' ') SCREWUP("mode not delimited"); for (size = 0; isdigit((unsigned char)*cp);) size = size * 10 + (*cp++ - '0'); if (*cp++ != ' ') SCREWUP("size not delimited"); if ((strchr(cp, '/') != NULL) || (strcmp(cp, "..") == 0)) { run_err("error: unexpected filename: %s", cp); exit(1); } if (targisdir) { static char *namebuf; static size_t cursize; size_t need; need = strlen(targ) + strlen(cp) + 250; if (need > cursize) { free(namebuf); namebuf = xmalloc(need); cursize = need; } (void) snprintf(namebuf, need, "%s%s%s", targ, strcmp(targ, "/") ? "/" : "", cp); np = namebuf; } else np = targ; curfile = cp; exists = stat(np, &stb) == 0; if (buf[0] == 'D') { int mod_flag = pflag; if (!iamrecursive) SCREWUP("received directory without -r"); if (exists) { if (!S_ISDIR(stb.st_mode)) { errno = ENOTDIR; goto bad; } if (pflag) (void) chmod(np, mode); } else { /* Handle copying from a read-only directory */ mod_flag = 1; if (mkdir(np, mode | S_IRWXU) < 0) goto bad; } vect[0] = xstrdup(np); sink(1, vect); if (setimes) { setimes = 0; if (utimes(vect[0], tv) < 0) run_err("%s: set times: %s", vect[0], strerror(errno)); } if (mod_flag) (void) chmod(vect[0], mode); free(vect[0]); continue; } omode = mode; mode |= S_IWUSR; if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) { bad: run_err("%s: %s", np, strerror(errno)); continue; } (void) atomicio(vwrite, remout, "", 1); if ((bp = allocbuf(&buffer, ofd, COPY_BUFLEN)) == NULL) { (void) close(ofd); continue; } cp = bp->buf; wrerr = NO; statbytes = 0; if (showprogress) start_progress_meter(curfile, size, &statbytes); set_nonblock(remin); for (count = i = 0; i < size; i += bp->cnt) { amt = bp->cnt; if (i + amt > size) amt = size - i; count += amt; do { j = atomicio6(read, remin, cp, amt, scpio, &statbytes); if (j == 0) { run_err("%s", j != EPIPE ? strerror(errno) : "dropped connection"); exit(1); } amt -= j; cp += j; } while (amt > 0); if (count == bp->cnt) { /* Keep reading so we stay sync'd up. */ if (wrerr == NO) { if (atomicio(vwrite, ofd, bp->buf, count) != count) { wrerr = YES; wrerrno = errno; } } count = 0; cp = bp->buf; } } unset_nonblock(remin); if (showprogress) stop_progress_meter(); if (count != 0 && wrerr == NO && atomicio(vwrite, ofd, bp->buf, count) != count) { wrerr = YES; wrerrno = errno; } if (wrerr == NO && (!exists || S_ISREG(stb.st_mode)) && ftruncate(ofd, size) != 0) { run_err("%s: truncate: %s", np, strerror(errno)); wrerr = DISPLAYED; } if (pflag) { if (exists || omode != mode) #ifdef HAVE_FCHMOD if (fchmod(ofd, omode)) { #else /* HAVE_FCHMOD */ if (chmod(np, omode)) { #endif /* HAVE_FCHMOD */ run_err("%s: set mode: %s", np, strerror(errno)); wrerr = DISPLAYED; } } else { if (!exists && omode != mode) #ifdef HAVE_FCHMOD if (fchmod(ofd, omode & ~mask)) { #else /* HAVE_FCHMOD */ if (chmod(np, omode & ~mask)) { #endif /* HAVE_FCHMOD */ run_err("%s: set mode: %s", np, strerror(errno)); wrerr = DISPLAYED; } } if (close(ofd) == -1) { wrerr = YES; wrerrno = errno; } (void) response(); if (setimes && wrerr == NO) { setimes = 0; if (utimes(np, tv) < 0) { run_err("%s: set times: %s", np, strerror(errno)); wrerr = DISPLAYED; } } switch (wrerr) { case YES: run_err("%s: %s", np, strerror(wrerrno)); break; case NO: (void) atomicio(vwrite, remout, "", 1); break; case DISPLAYED: break; } } screwup: run_err("protocol error: %s", why); exit(1); } int response(void) { char ch, *cp, resp, rbuf[2048]; if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp)) lostconn(0); cp = rbuf; switch (resp) { case 0: /* ok */ return (0); default: *cp++ = resp; /* FALLTHROUGH */ case 1: /* error, followed by error msg */ case 2: /* fatal error, "" */ do { if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch)) lostconn(0); *cp++ = ch; } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n'); if (!iamremote) (void) atomicio(vwrite, STDERR_FILENO, rbuf, cp - rbuf); ++errs; if (resp == 1) return (-1); exit(1); } /* NOTREACHED */ } void usage(void) { (void) fprintf(stderr, "usage: scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n" " [-l limit] [-o ssh_option] [-P port] [-S program]\n" " [[user@]host1:]file1 ... [[user@]host2:]file2\n"); exit(1); }
int main(int argc, char* argv[]) { int sockfd, portno, n; struct hostent* server; struct sockaddr_in serv_addr; char buffer[256]; char str[256]; char *toke; char filename[256]; char getStatus[256]; if(argc != 3) { fprintf(stderr, "Usage: %s <hostname> <port>\n", argv[0]); return 1; } server = gethostbyname(argv[1]); if(!server) { fprintf(stderr, "ERROR: no such host: %s\n", argv[1]); return 2; } portno = atoi(argv[2]); sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(sockfd < 0) syserr("can't open socket"); memset(&serv_addr, 0, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr = *((struct in_addr*)server->h_addr); serv_addr.sin_port = htons(portno); if(connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) syserr("can't connect to server"); printf("Connection to server at %s:%s established. Now awaiting command...\n", argv[1], argv[2]); DIR *dir; struct dirent *ent; for(;;){ printf("%s:%s> ",argv[1],argv[2]); memset(buffer,0,sizeof(buffer)); fgets(buffer, sizeof(buffer), stdin); n=strlen(buffer); if(n>0 && buffer[n-1] == '\n') buffer[n-1]='\0'; strcpy(str, buffer); toke = strtok(str, " "); //input conditions if(strcmp(toke,"ls-local")== 0){ printf("File at the client:\n"); dir = opendir("."); if(dir != NULL){ while((ent = readdir(dir)) != NULL){ if(strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) printf("%s\n",ent->d_name); } } closedir(dir); } else{ if(strlen(buffer)>1) n = send(sockfd, buffer, sizeof(buffer), 0); if(n < 0) lostconn(); else if(strcmp(toke, "exit")==0 || strcmp(toke,"quit")==0 || strcmp(buffer,"stop")==0){ printf("Connection to the server %s:%s terminated. Bye now!\n", argv[1],argv[2]); close(sockfd); exit(0); } else if(strcmp(toke,"get")==0){ toke = strtok(NULL," "); strcpy(filename,toke); memset(buffer,0,sizeof(buffer)); n = recv(sockfd, buffer ,sizeof(buffer),0); if(n<0) lostconn(); strcpy(getStatus,buffer); if(strcmp(getStatus,"succesful")==0){ FILE *f = fopen(filename,"wb"); uint32_t sizeIn; n = recv(sockfd,&sizeIn,sizeof(uint32_t),0); if(n<0) lostconn(); uint32_t filesize = ntohl(sizeIn); int bytes_read,bytes_toRead, bytes_written; bytes_toRead = filesize; printf("Receiving %d bytes from server...\n",bytes_toRead); while(bytes_toRead > 0){ memset(buffer,0,sizeof(buffer)); bytes_read = read(sockfd,buffer,sizeof(buffer)); if(bytes_read <0) syserr("error reading file"); if(bytes_toRead< sizeof(buffer)){ bytes_written = fwrite(buffer, bytes_toRead,1,f); } else{ bytes_written = fwrite(buffer,sizeof(buffer),1,f); } if(bytes_written <0) syserr("error writing"); bytes_toRead -= bytes_read; } fclose(f); } else{ memset(buffer,0,sizeof(buffer)); n = recv(sockfd,buffer,sizeof(buffer),0); if(n<0) lostconn(); printf("%s\n",buffer); } printf("Retrieve file '%s' from server: %s\n",filename,getStatus); } else if(strcmp(toke,"put")==0){ toke = strtok(NULL, " " ); int f = open(toke,0); int filesize; struct stat st; stat(toke, &st); filesize = st.st_size; if(f == -1){ printf("Invalid file/format. Please use put <filename>\n"); } else{ uint32_t un = htonl((uint32_t)filesize); n = send(sockfd,&un,sizeof(uint32_t),0); if(n<0) lostconn(); int bytes_sent,bytes_read,bytes_remaining; bytes_remaining = filesize; printf("Transferring %d bytes to server... \n",bytes_remaining); while(bytes_remaining > 0 ){ memset(buffer,0,sizeof(buffer)); if(bytes_remaining < sizeof(buffer)){ bytes_read = read(f,buffer,bytes_remaining); if(bytes_read<0) syserr("error reading file"); bytes_sent = send(sockfd, buffer,bytes_remaining,0); if(bytes_sent<0) syserr("error sending file"); } else{ bytes_read = read(f,buffer,sizeof(buffer)); if(bytes_read<0) syserr("error reading file"); bytes_sent = send(sockfd, buffer,sizeof(buffer),0); if(bytes_sent<0) syserr("error sending file"); if(bytes_sent < sizeof(buffer)){ bytes_read = read(f,buffer, sizeof(buffer)-bytes_read); if(bytes_read<0) syserr("error reading file"); bytes_sent = send(sockfd,buffer,sizeof(buffer)-bytes_read,0); if(bytes_sent<0) syserr("error sending file"); } } bytes_remaining -= bytes_sent; } printf("Upload '%s' to remote server: successful\n",toke); } close(f); } else if(strcmp(toke,"ls-remote")==0){ printf("Files at the server(%s):\n",argv[1]); memset(buffer,0,sizeof(buffer)); uint32_t numIn; n = recv(sockfd, &numIn,sizeof(uint32_t),0); if(n<0) lostconn(); uint32_t numFiles = ntohl(numIn); while(numFiles > 0){ n = recv(sockfd, buffer, sizeof(buffer), 0); if(n<0) lostconn(); printf("%s\n", buffer); numFiles--; } } else{ memset(buffer,0,sizeof(buffer)); n = recv(sockfd, buffer,sizeof(buffer), 0); if(n<0) lostconn(); printf("%s\n", buffer); } } } close(sockfd); return 0; }