void Session::handleLIST(const std::string& arg) { Path path = m_root / m_path; // @fixme don't allow going below root. if (arg.size() > 0) { if (arg != "-aL" && arg != "-la") { path = getAbsolutePath(arg); } } // Check if we're trying to go below root. if (String::startsWith(m_root.str(), path.str())) path = m_root; Path::Type type = path.type(); if (type == Path::PT_INVALID) { sendReply(450, "Requested file action not taken."); return; } sendReply(150, "File status okay; about to open data connection."); Time::BrokenDown time_ref; TCPSocket* data = openDataConnection(); if (type == Path::PT_FILE) { sendFileInfo(path, data, time_ref); } else { Directory dir(path); const char* entry = NULL; while ((entry = dir.readEntry(Directory::RD_FULL_NAME))) { sendFileInfo(entry, data, time_ref); } } closeDataConnection(data); }
CSendFileDlg::CSendFileDlg(TCPSessionBase *tcp) : FileSession(tcp) { transfer_size = 0; stop_by_myself = false; createWindow(); if (tcpSession->isSender()) { show(); Gtk::FileSelection dialog(_("Choose the file which you want to send")); dialog.set_transient_for(*this); int result = dialog.run(); if (result == Gtk::RESPONSE_OK) { filePath = dialog.get_filename(); sendFileInfo(filePath.c_str()); } else tcpSession->destroy(); } }
int main(int argc, char** argv) { /* The port number */ int port = -1; /* The file descriptor representing the connection to the client */ int connfd = -1; /* The number of bytes sent in one shot */ int numSent = 0; /* The total number of bytes sent */ off_t totalNumSent = 0; /* The size of the file name */ int fileNameSize = -1; /* The structures representing the server address */ sockaddr_in serverAddr; /* Stores the size of the client's address */ socklen_t servLen = sizeof(serverAddr); /* Make sure the user has provided the port number to * listen on */ if(argc < 4) { /* Report an error */ fprintf(stderr, "USAGE: %s <SERVER IP> <SERVER PORT #> <FILE NAME>", argv[0]); exit(-1); } /* Get the port number */ port = atoi(argv[2]); /* Get the file name */ const char* fileName = argv[3]; /* Make sure that the port is within a valid range */ if(port < 0 || port > 65535) { fprintf(stderr, "Invalid port number\n"); exit(-1); } /* Connect to the server */ if((connfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket"); exit(-1); } /* Set the structure to all zeros */ memset(&serverAddr, 0, sizeof(serverAddr)); /* Set the server family */ serverAddr.sin_family = AF_INET; /* Convert the port number to network representation */ serverAddr.sin_port = htons(port); /** * Convert the IP from the presentation format (i.e. string) * to the format in the serverAddr structure. */ if(!inet_pton(AF_INET, argv[1], &serverAddr.sin_addr)) { perror("inet_pton"); exit(-1); } /* Lets connect to the client. This call will return a socket used * used for communications between the server and the client. */ if(connect(connfd, (sockaddr*)&serverAddr, sizeof(sockaddr))<0) { perror("connect"); exit(-1); } /* Send the file name */ sendFileInfo(connfd, fileName); /* Get the file size */ int fileSize = getFileSize(fileName); /* Did we successfully obtain the file size ? */ if(fileSize < 0) { perror("stat"); exit(-1); } /* Send the file size */ if(tcp_send_size(connfd, fileSize) < 0) { perror("tcp_send_size"); exit(-1); } /* Open the file */ int fd = open(fileName, O_RDONLY); /* Make sure the file was successfully opened */ if(fd < 0) { perror("fopen"); } /* Keep sending until the whole file is sent */ while(totalNumSent < fileSize) { /* Send the file to the server */ if((numSent = sendfile(connfd, fd, &totalNumSent, fileSize - totalNumSent)) < 0) { perror("sendfile"); exit(-1); } /* Update the total number of bytes sent */ totalNumSent += numSent; } /* Close the connection socket */ close(connfd); /* Close the file */ close(fd); return 0; }