How can I upload a directory using the FtpPutFile function or all the directory this is my code:
void FileSubmit(path ToUpload)
{
HINTERNET hInternet;
HINTERNET hFtpSession;
hInternet = InternetOpen(NULL,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
if (hInternet == NULL) cout << ("No Internet Connection..\n");
else cout << ("Internet Connection Established\n");
hFtpSession = InternetConnect(hInternet,"host",INTERNET_DEFAULT_FTP_PORT, "user","pass", INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE,0 );
if (!hFtpSession) cout << ("Error in the FTP connection..\n");
else
{
cout <<("FTP Connection Established!\n");
FtpPutFile(hFtpSession, "D://test//*.doc", ToUpload.string().c_str(), FTP_TRANSFER_TYPE_ASCII, INTERNET_FLAG_PASSIVE);
if (!FtpPutFile(hFtpSession, "D://test//*.doc", ToUpload.string().c_str(), FTP_TRANSFER_TYPE_ASCII, INTERNET_FLAG_PASSIVE))
cout <<("File Transfer Failed..\n");
else cout << ("The file was sent..\n");
InternetCloseHandle(hFtpSession);
InternetCloseHandle(hInternet);
}
}
int main()
{
FileSubmit(destination);
return 0;
}
You can't 'upload' directories directly; you would need to create the directory with FtpCreateDirectory() then iterate over all the files in your local directory and call FtpPutFile() on each of them.
If you need a way of getting a list of files in a directory you can use Boost.Filesystem. Look for the directory_iterator and recursive_directory_iterator classes.
Related
Can I launch umdf2 driver using CreateService and StartService APIs in Windows 10? I am looking for any running sample that I could refer.
I have done it with WDM driver previously, but currently I failed to do it with umdf2 driver. Here is the code
WCHAR strPath[MAX_PATH];
GetCurrentDirectory(MAX_PATH, strPath);
std::wstring binaryPath(strPath);
binaryPath += L"\\" + pDeviceName + L".dll";
std::string logPath(binaryPath.begin(), binaryPath.end());
cout << "Load Path : " << logPath << endl;
SC_HANDLE hManager, hService;
hManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (!hManager) {
DWORD err = GetLastError();
if (err == ERROR_ACCESS_DENIED) {
cout << "OPenSCManager Access denied - run administration access" << endl;
} else {
cout << "OPenSCManager Error : " << err << endl;
}
return;
}
hService = CreateService(hManager, pDeviceName.c_str(), pDeviceName.c_str(), SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START,
SERVICE_ERROR_NORMAL, binaryPath.c_str(), NULL, NULL, NULL, NULL, NULL);
if (!hService) {
hService = OpenService(hManager, pDeviceName.c_str(), SERVICE_ALL_ACCESS);
if (!hService) {
CloseServiceHandle(hManager);
return;
}
}
if (!StartService(hService, 0, NULL)) {
DWORD err = GetLastError();
cout << "StartService Error : " << err << endl;
if (err == ERROR_SERVICE_ALREADY_RUNNING) {
cout << "Already running" << endl;
}
}
CloseServiceHandle(hManager);
CloseServiceHandle(hService);
pDeviceName refers to the driver name. Code execution fails with error 2:
StartService Error : 2
I tested this in both Win7 and Win10 and the result is same.
The error code has told us the most of things:
The system cannot find the file specified.
First, check the (pDeviceName).dll is located in Current Directory.
Second, check its dependencies with tools like Dependency Walker, move them to Current Directory or System Directory to make sure the system can also find the dependencies.
Then try to check the "regedit", Open the key HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\pDeviceName, or other similar names. Check the Key value "ImagePath", the path was the first time you have create it. Move dll to the Path or Change the Path to dll.
I want to upload a file to my FTP server using C++ code and I'm able to FTP my server with FileZilla.
When I run my C++ code, it throws me an output "3" error ( GetLastError() function returned this value to the FtpPutFile() function
#pragma comment (lib,"wininet.lib")
#include <windows.h>
#include <wininet.h> //for uploadFile function
#include <iostream>
using namespace std;
int main()
{
HINTERNET hint, hftp;
hint = InternetOpen("FTP", INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, INTERNET_FLAG_ASYNC);
hftp = InternetConnect(hint, "MY IP ADRESS", INTERNET_DEFAULT_FTP_PORT, "MY NAME", "MY PASS", INTERNET_SERVICE_FTP, 0, 0);
if (!FtpPutFile(hftp, "C://Users//Elliot//Desktop//log.txt", "//log.txt", FTP_TRANSFER_TYPE_BINARY, 0))
{
cout << "FAIL !" << endl;
cout << GetLastError() << endl;
}
else {
cout << "file sended !";
};
InternetCloseHandle(hftp);
InternetCloseHandle(hint);
system("PAUSE");
}
things i have tried :
Changing server ( i made new server but still same result )
Controlling firewall
Running as adminstrator
break points ( the ftpputfile is giving the error )
The error message is clear. File is not there, there is nothing to send. You can easily check the file's path using std::ifstream before continuing with internet functions.
Use FtpSetCurrentDirectory to set a target directory. In this example I used "public_html", maybe your server is different.
#include <windows.h>
#include <wininet.h>
#include <iostream>
#include <string>
#include <fstream>
#pragma comment (lib,"wininet.lib")
using namespace std;
int main()
{
string file = "C:\\path.txt";
string site = "www.site.com";
string user = "username";
string pass = "password";
if (!ifstream(file))
{
cout << "no file\n";
return 0;
}
HINTERNET hint = InternetOpen(0, INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0);
HINTERNET hftp = InternetConnect(hint, site.c_str(), INTERNET_DEFAULT_FTP_PORT,
user.c_str(), pass.c_str(), INTERNET_SERVICE_FTP, 0, 0);
if (FtpSetCurrentDirectory(hftp, "public_html"))
{
if (!FtpPutFile(hftp, file.c_str(), "log.txt", FTP_TRANSFER_TYPE_BINARY, 0))
{
cout << "FAIL!" << endl;
cout << GetLastError() << endl;
}
else
{
cout << "file sended !";
}
}
InternetCloseHandle(hftp);
InternetCloseHandle(hint);
return 0;
}
How to send mouse position from client A =to=>SERVER=to=>client B ? Example code below gives me position output every 2 seconds
What is a better/faster way to do this?
NOTICE: using Winsock and cURL gives antivirus malware warning
USAGE: for remote control
Current TEST example of sending mouse position from client A to SERVER to client A:
1.write mouse position
2.store x,y in send.txt file
3.upload sent.txt to server as temp.txt file
4.remove receve.txt if exist //error 80 if not
5.download temp.txt as receve.txt
6.read receve.txt and display coordinates in console
int x,y; //positions
LPCWSTR s=L"C://Documents and Settings//Administrator//Desktop//c++//FTP//send.txt";//location of file for sending
LPCWSTR r=L"C://Documents and Settings//Administrator//Desktop//c++//FTP//receve.txt";//location of received file
POINT cursor_pos;//for cursor position
HINTERNET hInternet;
HINTERNET hFtpSession;
hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (hInternet == NULL)
{
cout << "Error: " << GetLastError();
}
else
{
hFtpSession = InternetConnect(hInternet, L"www.test.net", INTERNET_DEFAULT_FTP_PORT, L"user", L"pass", INTERNET_SERVICE_FTP, 0, 0);
if (hFtpSession == NULL)//not connect
{
cout << "Error: " << GetLastError();
}
else
{
for(;;){
//file input
fstream inp;
inp.open(s);
GetCursorPos(&cursor_pos);
inp<<cursor_pos.x<<" "<<cursor_pos.y<<endl;//write curent position
inp.close();
//UPLOADING
if (!FtpPutFile(hFtpSession, s, L"//public_html//test//temp.txt", FTP_TRANSFER_TYPE_BINARY, 0))
{
cout << "ErrorPutFile: " << GetLastError();
return 0;
}
remove("C://Documents and Settings//Administrator//Desktop//c++//FTP//receve.txt");//error 80 if file exist so remove it
//DOWNLOADING
if(!FtpGetFile(hFtpSession,L"//public_html//test//temp.txt",r,TRUE,FILE_ATTRIBUTE_NORMAL,FTP_TRANSFER_TYPE_BINARY,0))
{
cout <<"ErrorGetFile"<<GetLastError();
return 0;
}//DELETING
if(!FtpDeleteFile(hFtpSession,L"//public_html//test//temp.txt")){
cout <<"ErrorGetFile"<<GetLastError();
return 0;
}
ifstream outp(r);
while(outp>>x>>y){
cout<<"X: "<<x<<" "<<"Y:"<<y<<endl;//read coordinates
}
outp.close();
}
}
}
return 0;
Thank you for your time :)
You might consider SignalR, it's great for those things. See https://pepitosolis.wordpress.com/2013/12/08/signalr-2-0-persistent-connection-another-example-tracking-your-mouse-pointer/ for an example
I'm writing program on C++ to make calls (POST requests) to Java Servlets using WinHTTP. When I request POST via HTTP everything is OK, problem occurs when I request via HTTPS. It sends empty requests to the server but cuts off request body (but it has content)
int sendPostRequest(char *pszPostData, LPCTSTR servletUrl, char* resultBuffer, ofstream &outputFile) {
outputFile << "====================================== SENDING REEQUEST ======================================" << endl;
HINTERNET hSession = WinHttpOpen(
userAgent,
WINHTTP_ACCESS_TYPE_NO_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS,
0);
if (!hSession)
{
_tprintf(_TEXT("Failed to open WinHTTP session: %ld\n"), GetLastError());
outputFile << "Failed to open WinHTTP session: %ld\n" << GetLastError() << endl;
return NULL;
}
else {
_tprintf(_TEXT("Oppening WinHTTP session successful: %ld\n"), GetLastError());
outputFile << "Oppening WinHTTP session successful: %ld\n" << GetLastError() << endl;
}
HINTERNET hConnect = WinHttpConnect(
hSession,
serverIP,
serverPort,
0);
if (!hConnect)
{
_tprintf(_TEXT("Failed to connect to server: %ld\n"), GetLastError());
outputFile << "Failed to connect to server: %ld\n" << GetLastError() << endl;
WinHttpCloseHandle(hSession);
return NULL;
}
else {
_tprintf(_TEXT("Connection to server successful: %ld\n"), GetLastError());
outputFile << "Connection to server successful: %ld\n" << GetLastError() << endl;
}
_tprintf(_TEXT("Post data : %ld\n"), pszPostData);
outputFile << "Post data : %ld\n" << pszPostData << endl;
DWORD dwDataLen = strlen(pszPostData);
HINTERNET hRequest = WinHttpOpenRequest(
hConnect,
_TEXT("POST"),
servletUrl,
NULL,
WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
WINHTTP_FLAG_REFRESH);
if (!hRequest)
{
_tprintf(_TEXT("Failed to open request: %ld\n"), GetLastError());
outputFile << "Failed to open request: %ld\n" << GetLastError() << endl;
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return -1;
}
else {
_tprintf(_TEXT("Opening request successful: %ld\n"), GetLastError());
outputFile << "Opening request successful: %ld\n" << GetLastError() << endl;
}
DWORD dwReqOpts = 0;
DWORD dwSize = sizeof(DWORD);
WinHttpSetOption(
hRequest,
WINHTTP_OPTION_SECURITY_FLAGS,
&dwReqOpts,
sizeof(DWORD));
BOOL done = false;
BOOL rc = WinHttpSendRequest(
hRequest,
contentTypeHeader,
-1,
(LPVOID)pszPostData,
dwDataLen,
dwDataLen,
NULL);
if (rc) {
rc = WinHttpReceiveResponse(hRequest, NULL);
_tprintf(_TEXT("Sending request successful: %ld\n"), GetLastError());
outputFile << "Sending request successful: %ld\n" << GetLastError() << endl;
}
else
{
_tprintf(_TEXT("Send request failed: %ld\n"), GetLastError());
outputFile << "Send request failed: %ld\n" << GetLastError() << endl;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return -1;
}
// Get the status from the server
DWORD dwCode = 0;
if (rc)
{
rc = WinHttpQueryHeaders(
hRequest,
WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
NULL,
&dwCode,
&dwSize,
NULL);
}
if (dwCode != HTTP_STATUS_OK)
{
_tprintf(_TEXT("HTTP Request failed: %ld\n"), dwCode);
outputFile << "HTTP Request failed: %ld\n" << dwCode << endl;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
}
else
{
_tprintf(_TEXT("HTTP Request is ok: %ld\n"), dwCode);
outputFile << "HTTP Request is ok : %ld\n" << dwCode << endl;
// Keep reading from the remote server until there's nothing left to read
DWORD dwBytesToBeRead = 0, dwBytesRead = 0;
//char szBuffer[8192] = { 0 };
//strcpy(resultBuffer, "");
do
{
if (!WinHttpQueryDataAvailable(hRequest, &dwBytesToBeRead))
{
_tprintf(_TEXT("No data available from server? %ld"), GetLastError());
outputFile << "No data available from server? %ld" << GetLastError() << endl;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return -1;
}
if (!WinHttpReadData(
hRequest,
//szBuffer,
resultBuffer,
//sizeof(szBuffer),
RESULT_BUFFER_SIZE,
&dwBytesRead))
{
_tprintf(_TEXT("Failed to read data from server: %ld"), GetLastError());
outputFile << "Failed to read data from server: %ld" << GetLastError() << endl;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return -1;
}
if (dwBytesRead > 0)
{
//szBuffer[dwBytesRead] = 0;
resultBuffer[dwBytesRead] = 0; // NULL-terminated returned buffer
}
} while (dwBytesRead > 0);
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return 0;
}
return -1;
}
Where pszPostData - content of request body, servletUrl - url to Servlet (endpoint), resultBuffer - call result will be written to this buffer, outputFile - file for logs.
So how to make HTTPS calls without cutting of request body?
Regarding WinHttpSendRequest method, at the 2nd paramaet insert: L"content-type:application/x-www-form-urlencoded" and on the 3rd paramer -1 which is according to w3.
It should work with this correction.
Also, check the encoding of your .php file, gave me a trouble once.
I came across this today as I was facing a very similar issue. In WinHttpConnect() I was sending pswzServerName as 'example.com'. Problem is, in Apache, I was forcing a redirect on domain.com to www.domain.com and also http to https. This redirect was causing no POST data to be sent, wrong content-length header, and wrong content-type header because I had specified example.com in WinHttpConnect().
Two solutions worked with the last being the best choice :
Removing my htaccess non-www to www and http to https redirect
OR
just changing `pswzServerName` to 'www.example.com' (including the www.) in WinHttpConnect()
Spent a long time trying to figure out why winhttp wasn't working as this was easily overlooked so hopefully it helps someone else that might be in the same boat.
Even if it is an old question, this might be helpful:
Add WINHTTP_FLAG_SECURE to the WinHttpOpenRequest, and it should work.
...
HINTERNET hRequest = WinHttpOpenRequest(
hConnect,
_TEXT("POST"),
servletUrl,
NULL,
WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
WINHTTP_FLAG_REFRESH+WINHTTP_FLAG_SECURE); /// Here
...
I'm new to Qt and I would like to implement FTP and SFTP support for my software.
As I googled I discovered that there doesn't exist a sftp library for Qt but it should be possible with QNetworkAccessManager.
I tried then to discover on how to build a custom protocol or something like that but didn't figure out how to do it.
Does someone know how I could do that?
Thanks,
Michael
There is no support for SFTP in Qt SDK but Qt Creator implements SFTP.
I have isolated the library that contains SSH and SFTP and I have created a new project named QSsh in Github. The aim of the project is to provide SSH and SFTP support for any Qt Application.
I have written an example on how to upload a file using SFTP. Take a look at examples/SecureUploader/
I hope it might be helpful
You need a custom implementation for each protocol. But we can create a class like QHttp which will do that. There are several protocols that has similar semantic, but not all. So, if you want write it, tell me and I help you.
There's no current SSH wrapper implementation in the Qt SDK. You have 3 choices here:
Roll your own custom SSH/SFTP client implementation using the IETF RFC and standard drafts like RFC4253. This might not be what you're looking for.
Use any of the ssh implementation libraries like openssh/libssh directly or writing your own Qt/C++ wrapper for future-reuse. Any reasonably decent project with ssh needs usually links to a an already established ssh library and uses it programatically. Like Qt Creator does, if you dig inside it long enough you'll find what user Paglian mentioned earlier. Relying on a library is less risky and more future-proof then rolling your own.
Use openssh tooling at the command line interface directly, using QProcess just like you'd use it at the shell. This is is the fastest method if you're working on a proof-of-concept project and don't need any complex ftp operations, as it might get a bit difficult to devise a robust wrapper around the CLI tooling.
I do this using libssh. Very straight forward.
https://api.libssh.org/stable/libssh_tutor_sftp.html
Don't forget to add your sftp server into known hosts in your system.
ssh-keyscan -H mysftpserver.com >> ~/.ssh/known_hosts
Example code:
#include "sftpuploader.h"
#include <QtDebug>
#include <QFileInfo>
#include <libssh/libssh.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <libssh/sftp.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <QFile>
int verify_knownhost(ssh_session session)
{
int state, hlen;
unsigned char *hash = NULL;
char *hexa;
char buf[10];
state = ssh_is_server_known(session);
hlen = ssh_get_pubkey_hash(session, &hash);
if (hlen < 0)
return -1;
switch (state)
{
case SSH_SERVER_KNOWN_OK:
break; /* ok */
case SSH_SERVER_KNOWN_CHANGED:
fprintf(stderr, "Host key for server changed: it is now:\n");
ssh_print_hexa("Public key hash", hash, hlen);
fprintf(stderr, "For security reasons, connection will be stopped\n");
free(hash);
return -1;
case SSH_SERVER_FOUND_OTHER:
fprintf(stderr, "The host key for this server was not found but an other"
"type of key exists.\n");
fprintf(stderr, "An attacker might change the default server key to"
"confuse your client into thinking the key does not exist\n");
free(hash);
return -1;
case SSH_SERVER_FILE_NOT_FOUND:
fprintf(stderr, "Could not find known host file.\n");
fprintf(stderr, "If you accept the host key here, the file will be"
"automatically created.\n");
/* fallback to SSH_SERVER_NOT_KNOWN behavior */
case SSH_SERVER_NOT_KNOWN:
hexa = ssh_get_hexa(hash, hlen);
fprintf(stderr,"The server is unknown. Do you trust the host key?\n");
fprintf(stderr, "Public key hash: %s\n", hexa);
free(hexa);
if (fgets(buf, sizeof(buf), stdin) == NULL)
{
free(hash);
return -1;
}
if (strncasecmp(buf, "yes", 3) != 0)
{
free(hash);
return -1;
}
if (ssh_write_knownhost(session) < 0)
{
fprintf(stderr, "Error %s\n", strerror(errno));
free(hash);
return -1;
}
break;
case SSH_SERVER_ERROR:
fprintf(stderr, "Error %s", ssh_get_error(session));
free(hash);
return -1;
}
free(hash);
return 0;
}
bool upload(const QString &localFile,
const QString &dest,
const QString &host,
const QString &username,
const QString &passwd)
{
bool retVal = false;
QFileInfo info(localFile);
m_localFilename = info.canonicalFilePath();
m_remoteFilename = dest + "/" + info.fileName();
int verbosity = SSH_LOG_PROTOCOL;
int port = 22;
int rc;
sftp_session sftp;
sftp_file file;
int access_type;
int nwritten;
QByteArray dataToWrite;
ssh_session my_ssh_session;
QFile myfile(m_localFilename);
if(!myfile.exists())
{
qDebug() << "SFTPUploader: File doesn't exist " << m_localFilename;
return retVal;
}
my_ssh_session = ssh_new();
if(my_ssh_session == NULL)
{
return retVal;
}
ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, host.toUtf8());
ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
rc = ssh_connect(my_ssh_session);
if (rc != SSH_OK)
{
qDebug() << "SFTPUploader: Error connecting to localhost: " << ssh_get_error(my_ssh_session);
ssh_free(my_ssh_session);
return retVal;
}
else
{
qDebug() << "SFTPUploader: SSH connected";
}
// Verify the server's identity
// For the source code of verify_knowhost(), check previous example
if (verify_knownhost(my_ssh_session) < 0)
{
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
qDebug() << "SFTPUploader: verify_knownhost failed";
return retVal;
}
rc = ssh_userauth_password(my_ssh_session, username.toUtf8(), passwd.toUtf8());
if (rc != SSH_AUTH_SUCCESS)
{
qDebug() << "SFTPUploader: Error authenticating with password: " << ssh_get_error(my_ssh_session);
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
return retVal;
}
else
{
qDebug() << "SFTPUploader: Authentication sucess";
}
sftp = sftp_new(my_ssh_session);
if (sftp == NULL)
{
qDebug() << "SFTPUploader: Error allocating SFTP session:" << ssh_get_error(my_ssh_session);
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
return retVal;
}
rc = sftp_init(sftp);
if (rc != SSH_OK)
{
qDebug() << "SFTPUploader: Error initializing SFTP session:", sftp_get_error(sftp);
sftp_free(sftp);
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
return retVal;
}
access_type = O_WRONLY | O_CREAT | O_TRUNC;
file = sftp_open(sftp, dest.toUtf8(), access_type, S_IRWXU);
if (file == NULL)
{
qDebug() << "SFTPUploader: Can't open file for writing:", ssh_get_error(my_ssh_session);
sftp_free(sftp);
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
return retVal;
}
if(myfile.open(QFile::ReadOnly))
{
dataToWrite = myfile.readAll();
}
nwritten = sftp_write(file, dataToWrite, dataToWrite.size());
if (nwritten != dataToWrite.size())
{
qDebug() << "SFTPUploader: Can't write data to file: ", ssh_get_error(my_ssh_session);
sftp_close(file);
sftp_free(sftp);
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
return retVal;
}
rc = sftp_close(file);
if (rc != SSH_OK)
{
qDebug() << "SFTPUploader: Can't close the written file:" << ssh_get_error(my_ssh_session);
sftp_free(sftp);
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
return retVal;
}
else
{
qDebug() << "SFTPUploader: Success";
retVal = true;
}
return retVal;
}