#include #include #include #include #include #include #include #include #include #include #include #include #define SERVER_PORT 21 #define SERVER_ADDR "94.132.140.155" int sockfd, sockDatafd; int getAnswerCode(char * answer) { char temp[4]; int result; temp[0] = answer[0]; temp[1] = answer[1]; temp[2] = answer[2]; temp[3] = '\0'; result = atoi(temp); return result; } void parseURL(char * url,char * userName,char * password, char * host, char *filepath, char *filename){ char userNameAndPass[50]; userNameAndPass[0]=0; char hostAndPath[200]; char urlcopy[256]; int i, divisor, x; //shift(url,'@'); //strcpy(urlcopy,url); sscanf(url,"ftp://%[a-zA-Z0-9:]@%[a-zA-Z0-9./:?&-]",userNameAndPass,hostAndPath); //printf("host= %s\n",hostAndPath); //printf("%s\n",userNameAndPass); if(strlen(userNameAndPass)==0){ userName="anonymous"; password="anonymous"; } else{ sscanf(userNameAndPass,"%[a-zA-Z0-9]:%[^@/:]",userName,password); } for(i= 0; i divisor) { filepath[x] = hostAndPath[i]; x++; } } filepath[x] = '\0'; for(i = strlen(filepath)-1; i > 0; i--) { if (filepath[i] == '/') break; } divisor = i; x= 0; for(i = divisor+1; i < strlen(filepath); i++) { filename[x] = filepath[i]; x++; } filename[x] = '\0'; } int readFromSocket(int socket_id, char message[]) { char temp[2]; char inBuf[256]; int i, finish, j; char resultBuf[256]; i = 0; finish = 0 ; strcpy(inBuf,""); strcpy(resultBuf,""); strcpy(temp,""); while(finish == 0) { strcpy(temp,""); strcpy(inBuf,""); j= 0; while(temp[0] != '\n') { read(socket_id, temp, sizeof(char)); //strcat(inBuf,temp); inBuf[j] = temp[0]; i++; j++; } inBuf[j] = '\0'; //strcat(inBuf,"\0"); if (inBuf[3] == ' ') { finish = 1; } strcat(resultBuf,inBuf); } strcpy(message, resultBuf); return i; } int readDataFromSocket(int socket_id, int fdlocal) { char temp[2]; char inBuf[256]; int i; i = 0; strcpy(inBuf,""); strcpy(temp,""); while(read(socket_id, temp, sizeof(char)) != 0) { //strcat(inBuf,temp); write(fdlocal, temp, sizeof(char)); i++; } write(fdlocal, '\0', sizeof(char)); return i; } void getIpString(char inputString[], char ipString[], int *portNumber) { char portString1[50], portString2[50], tempIpString[60]; int found, i, stringPos, startPos, blocksFound, portNumber1, portNumber2; found = 0; i = 0; startPos = 0; blocksFound = 0; stringPos = 0; strcpy(tempIpString,"\0"); strcpy(portString1,"\0"); strcpy(portString2,"\0"); while(found == 0 && startPos < strlen(inputString)) { if (inputString[startPos] == '(') found = 1; startPos++; } //printf("passou. startPos: %d\n", startPos); for(i = startPos;i 3) { printf("Incorrect number of arguments\n Usage: download ftp://[:@]/\n"); return; } else { parseURL(argv[1], userName, userPassword, hostPath, filePath, fileName); if(argc == 2) { verbose = 0; } else { if(strcmp(argv[2],"--verbose") == 0) verbose = 1; else verbose = 0; } } /*printf("username: %s\n",userName); printf("password: %s\n",userPassword); printf("hostpath: %s\n",hostPath); printf("filepath: %s\n",filePath); printf("filename: %s\n",fileName);*/ // GET IP FROM URL if ((hostnameObject=gethostbyname(hostPath)) == NULL) { herror("gethostbyname"); exit(1); } strcpy(connectionIP,inet_ntoa(*((struct in_addr *)hostnameObject->h_addr))); //printf("IP: %s\n", connectionIP); /*server address handling*/ bzero((char*)&server_addr,sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr(connectionIP); /*32 bit Internet address network byte ordered*/ server_addr.sin_port = htons(SERVER_PORT); /*server TCP port must be network byte ordered */ /*open an TCP socket*/ if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0) { perror("socket()"); exit(0); } /*connect to the server*/ if(connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0){ perror("connect()"); exit(0); } //READ WELCOME MESSAGE bytesRead = readFromSocket(sockfd, inBuf); if(verbose == 1) printf("READ (%d BYTES): %s\n",bytesRead, inBuf); answerCode = getAnswerCode(inBuf); if (answerCode != 220) { printf("Error: BAD WELCOME MESSAGE\nAplication will now quit.\n"); return -1; } //SEND USERNAME strcpy(buf,"USER "); strcat(buf,userName); strcat(buf,"\n"); bytesWritten = write(sockfd, buf, strlen(buf)); if(verbose == 1) printf("Bytes escritos %d\nWROTE: %s \n", bytesWritten, buf); bytesRead = readFromSocket(sockfd,inBuf); if(verbose == 1) printf("READ (%d BYTES): %s\n",bytesRead, inBuf); answerCode = getAnswerCode(inBuf); if (answerCode != 331) { printf("Error: BAD USER RESPONSE\nAplication will now quit.\n"); return -1; } //SEND PASSWORD strcpy(buf,"PASS "); strcat(buf,userPassword); strcat(buf,"\n"); bytesWritten = write(sockfd, buf, strlen(buf)); if(verbose == 1) printf("Bytes escritos %d\nWROTE: %s \n", bytesWritten, buf); bytesRead = readFromSocket(sockfd,inBuf); if(verbose == 1) printf("READ (%d BYTES): %s\n",bytesRead, inBuf); answerCode = getAnswerCode(inBuf); if (answerCode != 230) { printf("Error: LOGIN FAILED\nAplication will now quit.\n"); return -1; } //ENTER PASSIVE MODE strcpy(buf,"PASV\n"); bytesWritten = write(sockfd, buf, strlen(buf)); if(verbose == 1) printf("Bytes escritos %d\nWROTE: %s \n", bytesWritten, buf); bytesRead = readFromSocket(sockfd,inBuf); if(verbose == 1) printf("READ (%d BYTES): %s\n",bytesRead, inBuf); answerCode = getAnswerCode(inBuf); if (answerCode != 227) { printf("Error: COULDN'T ENTER PASSIVE MODE\nAplication will now quit.\n"); return -1; } //PARSE PASSIVE MODE IP //printf("TESTE DA FUNCAO\n"); getIpString(inBuf, passiveIP, &passivePort); //printf("PassiveIP: %s\nPassivePort: %d\n",passiveIP, passivePort); //SECOND SOCKET CONFIGURATION & OPENING /*Data Socket Configuration*/ bzero((char*)&server_addr_data,sizeof(server_addr_data)); server_addr_data.sin_family = AF_INET; server_addr_data.sin_addr.s_addr = inet_addr(passiveIP); /*32 bit Internet address network byte ordered*/ server_addr_data.sin_port = htons(passivePort); /*server TCP port must be network byte ordered */ /*open an TCP socket*/ if ((sockDatafd = socket(AF_INET,SOCK_STREAM,0)) < 0) { perror("socket()"); exit(0); } /*connect to the server*/ if(connect(sockDatafd, (struct sockaddr *)&server_addr_data, sizeof(server_addr_data)) < 0){ perror("connect()"); exit(0); } // ASK FOR FILE //strcpy(buf,"RETR folder1\\potato2.txt\n"); strcpy(buf,"RETR "); strcat(buf,filePath); strcat(buf,"\n"); bytesWritten = write(sockfd, buf, strlen(buf)); if(verbose == 1) printf("Bytes escritos %d\nWROTE: %s \n", bytesWritten, buf); bytesRead = readFromSocket(sockfd,inBuf); if(verbose == 1) printf("READ (%d BYTES): %s\n",bytesRead, inBuf); answerCode = getAnswerCode(inBuf); if (answerCode != 150) { printf("Error: COULDN'T OPEN CONNECTION FOR THE FILE\nAplication will now quit.\n"); return -1; } destinationFile = open(fileName, O_WRONLY | O_CREAT); //RECEIVE FILE ON 2ND SOCKET bytesRead = readDataFromSocket(sockDatafd,destinationFile); printf("DOWNLOADED %d BYTES OF DATA\n",bytesRead); // READ FROM FIRST SOCKET THAT THE TRANSFER WAS COMPLETED bytesRead = readFromSocket(sockfd,inBuf); if(verbose == 1) printf("READ (%d BYTES): %s\n",bytesRead, inBuf); answerCode = getAnswerCode(inBuf); if (answerCode != 226) { printf("Error: TRANSFER COULDN'T BE COMPLETED\nAplication will now quit.\n"); return -1; } //QUIT FROM SERVER //strcpy(buf,"RETR folder1\\potato2.txt\n"); strcpy(buf,"QUIT "); strcat(buf,filePath); strcat(buf,"\n"); bytesWritten = write(sockfd, buf, strlen(buf)); if(verbose == 1) printf("Bytes escritos %d\nWROTE: %s \n", bytesWritten, buf); bytesRead = readFromSocket(sockfd,inBuf); if(verbose == 1) printf("READ (%d BYTES): %s\n",bytesRead, inBuf); answerCode = getAnswerCode(inBuf); if (answerCode != 221) { printf("Error: COULDN'T CLOSE CONNECTION\nAplication will now quit.\n"); return -1; } close(sockDatafd); close(sockfd); // CHANGE FILE PERMISSIONS chmod(fileName, S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR); exit(0); }