I followed the documentation here https://api.libssh.org/master/libssh_tutor_sftp.html
to write a small program in c to download a remote file, the program is working but downloading a 120MB file takes 12-14 seconds to complete, compared to winscp, filezilla or pscp which only takes 2 seconds to complete.
How to improve this speed?
PS D:\MyDartProjects\fsbackup\libssh_binding\libssh_c_wrapper\x64\Release> Measure-Command {pscp -pw Ins257257 isaque.neves#192.168.133.13:/home/isaque.neves/go1.11.4.linux-amd64.tar.gz ./go1.11.4.linux-amd64.tar.gz }
Days : 0
Hours : 0
Minutes : 0
Seconds : 2
Milliseconds : 575
Ticks : 25755922
TotalDays : 2,98100949074074E-05
TotalHours : 0,000715442277777778
TotalMinutes : 0,0429265366666667
TotalSeconds : 2,5755922
TotalMilliseconds : 2575,5922
PS D:\MyDartProjects\fsbackup\libssh_binding\libssh_c_wrapper\x64\Release> Measure-Command {./libssh_wrapper.exe}
Days : 0
Hours : 0
Minutes : 0
Seconds : 14
Milliseconds : 360
Ticks : 143600026
TotalDays : 0,000166203733796296
TotalHours : 0,00398888961111111
TotalMinutes : 0,239333376666667
TotalSeconds : 14,3600026
TotalMilliseconds : 14360,0026
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_NONSTDC_NO_DEPRECATE
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <stdexcept>
#include <string.h>
#include <exception>
#include <fstream>
#include <libssh/callbacks.h>
#include <libssh/libssh.h>
#include <libssh/sftp.h>
#include "custom_exception.cpp"
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <io.h>
#include <time.h>
#include <chrono>
using std::chrono::high_resolution_clock;
using std::chrono::duration_cast;
using std::chrono::duration;
using std::chrono::milliseconds;
//using namespace std;
using std::string;
using std::cout;
using std::endl;
using std::cin;
// Good chunk size
#define MAX_XFER_BUF_SIZE 16384
int sftpDownloadFileTo(ssh_session session, const char* fullRemotePath, const char* fullLocalPath)
{
int access_type;
sftp_file file;
char buffer[MAX_XFER_BUF_SIZE * 8];//128KB
ssize_t nbytes, nwritten, rc;
int fd;
access_type = O_RDONLY;
sftp_session sftp = sftp_new(session);
if (sftp == NULL)
{
fprintf(stderr, "Error allocating SFTP session: %s\n",
ssh_get_error(session));
return SSH_ERROR;
}
rc = sftp_init(sftp);
if (rc != SSH_OK)
{
fprintf(stderr, "Error initializing SFTP session: %d.\n",
sftp_get_error(sftp));
sftp_free(sftp);
return rc;
}
file = sftp_open(sftp, fullRemotePath, access_type, 0);
if (file == NULL) {
fprintf(stderr, "Can't open file for reading: %s\n", ssh_get_error(session));
return SSH_ERROR;
}
fd = open(fullLocalPath, O_CREAT | O_RDWR, 0777);
if (fd < 0) {
fprintf(stderr, "Can't open file for writing: %s\n",
strerror(errno));
return SSH_ERROR;
}
for (;;) {
nbytes = sftp_read(file, buffer, sizeof(buffer));
if (nbytes == 0) {
break; // EOF
}
else if (nbytes < 0) {
fprintf(stderr, "Error while reading file: %s\n",
ssh_get_error(session));
sftp_close(file);
return SSH_ERROR;
}
nwritten = write(fd, buffer, nbytes);
if (nwritten != nbytes) {
fprintf(stderr, "Error writing: %s\n",
strerror(errno));
sftp_close(file);
return SSH_ERROR;
}
}
rc = sftp_close(file);
if (rc != SSH_OK) {
fprintf(stderr, "Can't close the read file: %s\n",
ssh_get_error(session));
return rc;
}
sftp_free(sftp);
return SSH_OK;
}
int main()
{
ssh_session my_ssh_session;
int rc;
int port = 22;
string password = "Ins257257";
auto host = "192.168.133.13";
auto username = "isaque.neves";
int verbosity = SSH_LOG_PROTOCOL;
// Abra a sessão e defina as opções
my_ssh_session = ssh_new();
if (my_ssh_session == NULL)
exit(-1);
ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, host);
//ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
// Conecte-se ao servidor
rc = ssh_connect(my_ssh_session);
if (rc != SSH_OK)
{
fprintf(stderr, "Error connecting to host: %s\n",
ssh_get_error(my_ssh_session));
exit(-1);
}
// Autenticar-se
rc = ssh_userauth_password(my_ssh_session, username, password.c_str());
if (rc != SSH_AUTH_SUCCESS)
{
fprintf(stderr, "Error authenticating with password: %s\n",
ssh_get_error(my_ssh_session));
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
exit(-1);
}
clock_t tic = clock();
auto t1 = high_resolution_clock::now();
sftpDownloadFileTo(my_ssh_session, "/home/isaque.neves/go1.11.4.linux-amd64.tar.gz", "D:/MyDartProjects/fsbackup/libssh_binding/go1.11.4.linux-amd64.tar.gz");
auto t2 = high_resolution_clock::now();
clock_t toc = clock();
duration<double, std::milli> ms_double = t2 - t1;
std::cout << ms_double.count() << " ms\r\n";
printf("Elapsed: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
std::cout << "End\n";
return 0;
}
https://github.com/insinfo/fsbackup/tree/main/libssh_binding/libssh_c_wrapper
Related
I'm using libssh to download a file from a Debian Linux server on my local Windows machine, the program is running without error but the downloaded file is corrupted. When I download via pscp it works, I don't know if I'm doing something wrong with regards to the buffer or something else.
The sizes of the generated files are identical, when I download it via other tools like FileZilla or WinSCP everything goes well. The file doesn't corrupt anymore. When I run my code then it gets corrupted
I'm running this code on Windows 10 with Visual Studio 2019. After messing with this code a lot I think the problem is with the write file writing function when I open the generated file in a hex editor I see differences in bytes only the end of the file that stays the same.
I ended up posting the Dart version of the code previously, but the C version is almost identical to the Dart version.
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_NONSTDC_NO_DEPRECATE
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <stdexcept>
#include <string.h>
#include <exception>
#include <fstream>
#include <libssh/callbacks.h>
#include <libssh/libssh.h>
#include <libssh/sftp.h>
#include "custom_exception.cpp"
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <io.h>
#include <time.h>
#include <chrono>
using std::chrono::high_resolution_clock;
using std::chrono::duration_cast;
using std::chrono::duration;
using std::chrono::milliseconds;
//using namespace std;
using std::string;
using std::cout;
using std::endl;
using std::cin;
// Good chunk size
#define MAX_XFER_BUF_SIZE 16384
int sftpDownloadFileTo(ssh_session session, const char* fullRemotePath, const char* fullLocalPath)
{
int access_type;
sftp_file file;
char buffer[MAX_XFER_BUF_SIZE * 8];//128KB
ssize_t nbytes, nwritten, rc;
int fd;
access_type = O_RDONLY;
sftp_session sftp = sftp_new(session);
if (sftp == NULL)
{
fprintf(stderr, "Error allocating SFTP session: %s\n",
ssh_get_error(session));
return SSH_ERROR;
}
rc = sftp_init(sftp);
if (rc != SSH_OK)
{
fprintf(stderr, "Error initializing SFTP session: %d.\n",
sftp_get_error(sftp));
sftp_free(sftp);
return rc;
}
file = sftp_open(sftp, fullRemotePath, access_type, 0);
if (file == NULL) {
fprintf(stderr, "Can't open file for reading: %s\n", ssh_get_error(session));
return SSH_ERROR;
}
fd = open(fullLocalPath, O_CREAT | O_RDWR, 0777);
if (fd < 0) {
fprintf(stderr, "Can't open file for writing: %s\n",
strerror(errno));
return SSH_ERROR;
}
for (;;) {
nbytes = sftp_read(file, buffer, sizeof(buffer));
if (nbytes == 0) {
break; // EOF
}
else if (nbytes < 0) {
fprintf(stderr, "Error while reading file: %s\n",
ssh_get_error(session));
sftp_close(file);
return SSH_ERROR;
}
nwritten = write(fd, buffer, nbytes);
if (nwritten != nbytes) {
fprintf(stderr, "Error writing: %s\n",
strerror(errno));
sftp_close(file);
return SSH_ERROR;
}
}
rc = sftp_close(file);
if (rc != SSH_OK) {
fprintf(stderr, "Can't close the read file: %s\n",
ssh_get_error(session));
return rc;
}
sftp_free(sftp);
return SSH_OK;
}
int main()
{
ssh_session my_ssh_session;
int rc;
int port = 22;
string password = "Ins257257";
auto host = "192.168.133.13";
auto username = "isaque.neves";
int verbosity = SSH_LOG_PROTOCOL;
// Abra a sessão e defina as opções
my_ssh_session = ssh_new();
if (my_ssh_session == NULL)
exit(-1);
ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, host);
//ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
// Conecte-se ao servidor
rc = ssh_connect(my_ssh_session);
if (rc != SSH_OK)
{
fprintf(stderr, "Error connecting to host: %s\n",
ssh_get_error(my_ssh_session));
exit(-1);
}
// Autenticar-se
rc = ssh_userauth_password(my_ssh_session, username, password.c_str());
if (rc != SSH_AUTH_SUCCESS)
{
fprintf(stderr, "Error authenticating with password: %s\n",
ssh_get_error(my_ssh_session));
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
exit(-1);
}
clock_t tic = clock();
auto t1 = high_resolution_clock::now();
sftpDownloadFileTo(my_ssh_session, "/home/isaque.neves/go1.11.4.linux-amd64.tar.gz", "D:/MyDartProjects/fsbackup/libssh_binding/go1.11.4.linux-amd64.tar.gz");
auto t2 = high_resolution_clock::now();
clock_t toc = clock();
duration<double, std::milli> ms_double = t2 - t1;
std::cout << ms_double.count() << " ms\r\n";
printf("Elapsed: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
std::cout << "End\n";
return 0;
}
https://github.com/insinfo/fsbackup/tree/main/libssh_binding
fd = open(fullLocalPath, O_CREAT | O_RDWR, 0777);
Here's your problem. Since you are running on Windows we need to adjust here.
fd = open(fullLocalPath, O_CREAT | O_RDWR | O_BINARY, 0777);
otherwise write() will try to adjust line endings.
Stress-ng: How to write an application program in C or Cpp using execv to invoke stress-ng commands for CPU and memory testing in MIPS and return its status if it is success or failure?
Given an executable stress-ng file that has been cross-compiled to MIPS32 version using its toolchain.
Sample stress-ng commands:
stress-ng --vm 8 --vm-bytes 80% -t 1h
stress-ng --cpu 8 --cpu-ops 800000
Perhaps this will suffice:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void)
{
pid_t pid;
int ret;
char *stress_ng = "/usr/bin/stress-ng";
char *argv_new[] = { stress_ng,
"--vm", "8", "--vm-bytes", "80%",
"-t", "2s", "-v", NULL };
char *env_new[] = { NULL };
pid = fork();
if (pid < 0) {
fprintf(stderr, "fork failed: %d (%s)\n",
errno, strerror(errno));
exit(EXIT_FAILURE);
} else if (pid == 0) {
ret = execve(stress_ng, argv_new, env_new);
if (ret < 0) {
fprintf(stderr, "execve failed: %d (%s)\n",
errno, strerror(errno));
exit(EXIT_FAILURE);
}
_exit(ret);
} else {
/* Parent */
int status;
ret = waitpid(pid, &status, 0);
if (ret < 0) {
fprintf(stderr, "waitpid failed: %d (%s)\n",
errno, strerror(errno));
exit(EXIT_FAILURE);
}
ret = WEXITSTATUS(status);
printf("stress-ng returned: %d\n", ret);
}
exit(0);
}
If you want to parse the output from stress-ng, you need to create a pipe between the parent and child and the parent needs to read and parse the output over the pipe, something like the following:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void)
{
pid_t pid;
int ret;
int fds[2];
char *stress_ng = "/usr/bin/stress-ng";
char *argv_new[] = { stress_ng,
"--vm", "8", "--vm-bytes", "80%",
"-t", "2s", "-v", NULL };
char *env_new[] = { NULL };
if (pipe(fds) < 0) {
fprintf(stderr, "pipe failed: %d (%s)\n",
errno, strerror(errno));
exit(EXIT_FAILURE);
}
pid = fork();
if (pid < 0) {
fprintf(stderr, "fork failed: %d (%s)\n",
errno, strerror(errno));
exit(EXIT_FAILURE);
} else if (pid == 0) {
//close(STDERR_FILENO);
close(STDIN_FILENO);
close(fds[0]);
dup2(fds[1], STDOUT_FILENO);
dup2(fds[1], STDERR_FILENO);
ret = execve(stress_ng, argv_new, env_new);
if (ret < 0) {
fprintf(stderr, "execve failed: %d (%s)\n",
errno, strerror(errno));
exit(EXIT_FAILURE);
}
close(fds[1]);
_exit(ret);
} else {
/* Parent */
int status;
FILE *fp;
char buffer[1024];
close(fds[1]);
fp = fdopen(fds[0], "r");
if (!fp) {
fprintf(stderr, "fdopen failed: %d (%s)\n",
errno, strerror(errno));
exit(EXIT_FAILURE);
}
while (fgets(buffer, sizeof(buffer), fp)) {
size_t len = strlen(buffer);
if (len > 0)
buffer[len - 1] = '\0';
if (strstr(buffer, "completed"))
printf("GOT: <%s>\n", buffer);
}
fclose(fp);
close(fds[0]);
ret = waitpid(pid, &status, 0);
if (ret < 0) {
fprintf(stderr, "waitpid failed: %d (%s)\n",
errno, strerror(errno));
exit(EXIT_FAILURE);
}
ret = WEXITSTATUS(status);
printf("stress-ng returned: %d\n", ret);
}
exit(0);
}
Disclaimer: I'm googling my way through this
Using C++
Both ends are Linux
I can see the SSH connection on the far end via TCPDUMP
No build errors or return codes other than 0 (debug says its all good)
TCPDUMP doesn't look like its transferring enough data (it is a 2MB file*)
Connect / Authenticate / Authorize are happening properly as far as I can tell
When i go to the far end looking for the file that was supposedly copied over, it doesn't exist. I've tried updatedb & locate "file", but it's nowhere to be found.
Thank you in advance for any insight possible!
SSH Functions (auth & connect) cleared out for "Minimal, Complete, and Verifiable example" since they seem to be working fine*
#include <cstdio>
#include <string>
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <curl/curl.h>
#include <boost/regex.hpp>
#include <boost/system/error_code.hpp>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <x86_64-linux-gnu/sys/stat.h>
#define LIBSSH_STATIC 1
#include <libssh/libssh.h>
using namespace std;
int sourcePull(void)
{
CURL *curl;
FILE *fp;
CURLcode res;
char *url = "https://raw.githubusercontent.com/firehol/blocklist-ipsets/master/firehol_level1.netset";
char outfilename[FILENAME_MAX] = "fireHOL";
curl = curl_easy_init();
if (curl)
{
fp = fopen(outfilename, "wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
return 0;
}
int sourceParse()
{
//Regex for IP's in sourcePull result
ifstream infile("fireHOL");
string ipAddress;
boost::regex expr1("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");
boost::regex expr2("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\/([0-9]|[1-2][0-9]|3[0-2]))$");
boost::smatch matches;
while (infile >> ipAddress)
{
if (boost::regex_match(ipAddress, matches, expr1))
{
ofstream checkpoint;
checkpoint.open("samp_batch.txt", ios::out | ios::app);
checkpoint << "add -t 86400 -a d -l r -o icewall ip -s " << ipAddress << endl;
checkpoint << "add -t 86400 -a r -l r -o icewall ip -d " << ipAddress << endl;
}
if (boost::regex_match(ipAddress, matches, expr2))
{
ofstream checkpoint;
checkpoint.open("samp_batch.txt", ios::out | ios::app);
checkpoint << "add -t 86400 -a d -l r -o icewall ip -s " << ipAddress << endl;
checkpoint << "add -t 86400 -a r -l r -o icewall ip -d " << ipAddress << endl;
}
}
ofstream terminate;
terminate.open("samp_batch.txt", ios::out | ios::app);
terminate << "EOF";
return 0;
}
int fileSize()
{
int size;
streampos begin, end;
ifstream inputFile("samp_batch", ios::binary);
begin = inputFile.tellg();
inputFile.seekg(0, ios::end);
end = inputFile.tellg();
inputFile.close();
return size;
}
int ssh()
{
ssh_session my_ssh_session;
ssh_scp scp;
int port = 22;
int rc;
int method;
char password[128] = { 0 };
char *banner;
//open session & set options
my_ssh_session = ssh_new();
if (my_ssh_session == NULL)
exit(-1);
ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "XXX.XXX.XXX.XXX");
ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "security");
//connect to server
rc = ssh_connect(my_ssh_session);
if (rc != SSH_OK)
{
fprintf(stderr, "Error connecting to host: %s\n", ssh_get_error(my_ssh_session));
ssh_free(my_ssh_session);
exit(-1);
}
//verify the servers identity
if (verify_knownHost(my_ssh_session) < 0)
{
fprintf(stdout, "unkown host\n");
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
exit(-1);
}
// Try to authenticate
rc = ssh_userauth_none(my_ssh_session, NULL);
if (rc == SSH_AUTH_ERROR) {
error(my_ssh_session);
return rc;
}
method = ssh_auth_list(my_ssh_session);
while (rc != SSH_AUTH_SUCCESS) {
// Try to authenticate with public key first
if (method & SSH_AUTH_METHOD_PUBLICKEY) {
rc = ssh_userauth_autopubkey(my_ssh_session, NULL);
if (rc == SSH_AUTH_ERROR) {
error(my_ssh_session);
return rc;
}
else if (rc == SSH_AUTH_SUCCESS) {
break;
}
}
return rc;
//SCP samp_batch file here
const int length = fileSize();
const char *fileName = "samp_batch";
ifstream inputFile(fileName);
constexpr size_t bufferSize = 1024 * 1024 * 1024;
unique_ptr<char[]> buffer(new char[bufferSize]);
const void* cvp = &buffer;
rc = ssh_scp_push_file(scp, "samp_batch", length, 0777);
if (rc != SSH_OK)
{
fprintf(stderr, "Can't open remote file: %s\n", ssh_get_error(my_ssh_session));
ssh_free(my_ssh_session);
exit(-1);
}
while (inputFile)
{
inputFile.read(buffer.get(), 1024 * 1024 * 1024);
rc = ssh_scp_write(scp, cvp, bufferSize);
if (rc != SSH_OK)
{
fprintf(stderr, "Cant write to remote file: %s\n", ssh_get_error(my_ssh_session));
ssh_free(my_ssh_session);
exit(-1);
}
}
return SSH_OK;
////execute remote command here
//ssh_free(my_ssh_session);
}
int main()
{
sourcePull();
sourceParse();
ssh();
return 0;
}
Solved this issue w/ of course, more reading...
I was not actually initializing the SCP subsystem "within" the SSH system.
By adding in;
scp = ssh_scp_new(my_ssh_session, SSH_SCP_WRITE, "/");
if (scp == NULL)
{
fprintf(stderr, "Error allocating scp session: %s\n", ssh_get_error(my_ssh_session));
return SSH_ERROR;
}
rc = ssh_scp_init(scp);
if (rc != SSH_OK)
{
fprintf(stderr, "Error initializing scp session: %s\n", ssh_get_error(my_ssh_session));
ssh_scp_free(scp);
return rc;
}
above my push/write functions, I was able to get the SCP subsystem to "start". Haven't gotten her functioning yet (SCP Status Code 1d not valid) error now, but i at least know scp is "awake" at this point...
I'm doing an implementation of FTP, now I'm implementing the RETR command, I take the errno code 88 when I try to do "get FILENAME".
I think that the error can be the conversion of unsigned to uint32_t and uint16_t in the port command.
#include <cstring>
#include <cstdarg>
#include <cstdio>
#include <cerrno>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <locale.h>
#include <langinfo.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <iostream>
#include <dirent.h>
#include "common.h"
#include "ClientConnection.h"
ClientConnection::ClientConnection(int s) {
int sock = (int)(s);
char buffer[MAX_BUFF];
control_socket = s;
// Consultar la documentación para conocer el funcionamiento de fdopen.
fd = fdopen(s, "a+");
if (fd == NULL){
std::cout << "Connection closed" << std::endl;
fclose(fd);
close(control_socket);
ok = false;
return ;
}
ok = true;
data_socket = -1;
};
ClientConnection::~ClientConnection() {
fclose(fd);
close(control_socket);
}
int connect_TCP(uint32_t address, uint16_t port) {
struct sockaddr_in sin;
struct hostent
*hent;
int s;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
sin.sin_addr.s_addr = address;
//if(hent = gethostbyname(address))
//memcpy(&sin.sin_addr,hent->h_addr,hent->h_length);
//else if ((sin.sin_addr.s_addr = inet_addr((char*)address)) == INADDR_NONE)
//errexit("No puedo resolver el nombre \"%s\"\n", address);
s = socket(AF_INET, SOCK_STREAM, 0);
if(s < 0){
printf("No se puede crear el socket\n");
return 0;
}
if(connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0){
printf("No se puede conectar con %u\n", address);
return 0;
}
return s;
}
void ClientConnection::stop() {
close(data_socket);
close(control_socket);
parar = true;
}
#define COMMAND(cmd) strcmp(command, cmd)==0
void ClientConnection::WaitForRequests() {
if (!ok) {
return;
}
fprintf(fd, "220 Service ready\n");
while(!parar) {
fscanf(fd, "%s", command);
if (COMMAND("USER")) {
fscanf(fd, "%s", arg);
fprintf(fd, "331 User name ok, need password\n");
}
else if (COMMAND("PWD")) {
}
else if (COMMAND("PASS")) {
char pass[30];
fscanf(fd,"%s",pass);
fprintf(fd,"230 User logged in\n");
}
else if (COMMAND("PORT")) {
unsigned ip[4];
unsigned port[2];
fscanf(fd,"%u,%u,%u,%u,%u,%u",&ip[0],&ip[1],&ip[2],&ip[3],&port[0],&port[1]);
uint32_t aux1;
uint16_t aux2;
aux1 = ip[3] << 24 | ip[2] << 16 | ip[1] << 8 | ip[0];
aux2 = port[1]*256 + port[0];
data_socket = connect_TCP(aux1,aux2);
fprintf(fd,"200 OK\n");
}
else if (COMMAND("PASV")) {
}
else if (COMMAND("CWD")) {
}
else if (COMMAND("STOR") ) { //put
FILE* fp = fopen("filename","w+");
int size_buffer = 512;
char buffer[size_buffer];
int recived_datas;
while(recived_datas == size_buffer){
datos_recibidos = recv(data_socket,buffer,size_buffer,0);
fwrite(buffer,1,recived_datas,fp);
}
close(data_socket);
fclose(fp);
}
else if (COMMAND("SYST")) {
fprintf(fd,"SYSTEM DETAILS\n");
}
else if (COMMAND("TYPE")) {
fprintf(fd,"type 1");
}
else if (COMMAND("RETR")) {
fscanf(fd,"%s",arg);
std::cout << "Argument: " << arg << std::endl;
FILE* fp = fopen(arg,"r+");
int sent_datas;
int size_buffer = 512;
char buffer[size_buffer];
std::cout << "Buffer size = " << size_buffer << std::endl;
do{
sent_datas = fread(buffer,size_buffer,1,fp);
printf("Code %d | %s\n",errno,strerror(errno));
send(data_socket,buffer,sent_datas,0);
printf("Code %d | %s\n",errno,strerror(errno));
}while(sent_datas == size_buffer);
close(data_socket);
fclose(fp);
fprintf(fd,"Transferencia completada");
}
else if (COMMAND("QUIT")) {
}
else if (COMMAND("LIST")) {
}
else {
fprintf(fd, "502 Command not implemented.\n"); fflush(fd);
printf("Comando : %s %s\n", command, arg);
printf("Error interno del servidor\n");
}
}
fclose(fd);
return;
};
Error code 88 is ENOTSOCK meaning that your tried to do a socket operation on "not a socket".
The offending line, I believe is:
send(data_socket,buffer,sent_datas,0);
It looks like in your RETR section you never set data_socket to a valid socket with your connect_TCP function, as you did in PORT. Are you certain that data_socket is a valid fd when you call your RETR function?
I'm trying to write some example of client-server application which uses linux messages. Here is my code:
#include <mqueue.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#define MSG_SIZE 4096
#define MSG_MAX_COUNT 1024
#define MSG_TYPE_TO_UPPER 0
#define MSG_TYPE_EXIT 1
#define MQ_NAME "msg_queue"
namespace {
int str_toupper(char *str)
{
int len = 0;
for(; str[len]; ++len) {
str[len] = toupper(str[len]);
}
return len;
}
}
int main(int argc, char** argv)
{
if(argc != 2) {
fprintf(stderr, "Usage: msg_queue (client|server)\n");
exit(EXIT_FAILURE);
}
struct mq_attr attr; // MQueue attributes
mqd_t mqd; // MQueue descriptor
char buf[MSG_SIZE]; // Msg buffer
unsigned int type; // Msg type(priority)
// Set up MQueue attributes
attr.mq_maxmsg = MSG_MAX_COUNT;
attr.mq_msgsize = MSG_SIZE;
attr.mq_flags = 0;
attr.mq_curmsgs = 0;
mqd = mq_open(MQ_NAME, O_RDWR | O_CREAT, 0664, &attr);
if(mqd == -1) {
fprintf(stderr, "mq_open() failed for \""MQ_NAME"\": %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
if(strcmp(argv[1], "server") == 0) {
while(mq_receive (mqd, buf, MSG_SIZE, &type) != -1) {
if(type == MSG_TYPE_EXIT) {
mq_unlink(MQ_NAME);
mq_close(mqd);
break;
} else if(type == MSG_TYPE_TO_UPPER) {
int len = str_toupper(buf);
if(mq_send (mqd, buf, len, MSG_TYPE_TO_UPPER) == -1) {
fprintf(stderr, "Server: mq_send() failed: %s", strerror(errno));
}
}
}
} else if(strcmp(argv[1], "client") == 0) {
while(1) {
printf("Input a message: <type>(0 - TO_UPPER, 1 - EXIT) <message>\n");
scanf("%u %s", &type, buf);
if(mq_send (mqd, buf, strlen(buf), type) == -1) {
fprintf(stderr, "Client: mq_send() failed: %s", strerror(errno));
}
if(type == MSG_TYPE_TO_UPPER) {
if(mq_receive (mqd, buf, MSG_SIZE, &type) == -1) {
fprintf(stderr, "Client: mq_receive() failed: %s", strerror(errno));
}
printf("\"%s\" received\n", buf);
} else if(type == MSG_TYPE_EXIT) {
mq_unlink(MQ_NAME);
mq_close(mqd);
break;
}
}
} else {
fprintf(stderr, "Usage: msg_queue (client|server)\n");
exit(EXIT_FAILURE);
}
return 0;
}
What is my mistake? It always prints error from the line 47 - fprintf(stderr, "mq_open() failed for \""MQ_NAME"\": %s\n", strerror(errno)); with errno = EINVAL.
I see two issues:
Your message queue name must begin with a / on Linux. See mq_overview(7):
Each message queue is identified by a name of the form
/somename; that is, a null-terminated string of up to NAME_MAX (i.e., 255)
characters consisting of an initial slash, followed by one or more characters,
none of which are slashes.
MSG_MAX_COUNT is most likely above your system limits. It must be less than (or equal to) the /proc/sys/fs/mqueue/max_size. See mq_open(3):
EINVAL: O_CREAT was specified in oflag, and attr was not NULL, but
attr->mq_maxmsg or attr->mq_msqsize was invalid. Both of these fields
must be greater than zero. In a process that is unprivileged (does not
have the CAP_SYS_RESOURCE capability), attr->mq_maxmsg must be less
than or equal to the msg_max limit, and attr->mq_msgsize must be less
than or equal to the msgsize_max limit. In addition, even in a
privileged process, attr->mq_maxmsg cannot exceed the HARD_MAX limit.
(See mq_overview(7) for details of these limits.)
The other limit is probably ok, but you should verify it too.