Using getsockopt to check the initial value of SO_DEBUG option - c++

I am trying to understand the nature of getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen).
I am trying to see the initial status of SO_DEBUG and I am referring to this link https://www.mkssoftware.com/docs/man3/getsockopt.3.asp , and I am not sure if I am doing this the correct way because I am getting random values.
//***********************************************Libraries****************************************************************
#include <iostream>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <errno.h>
using namespace std;
//************************************************************************************************************************
int main()
{
int * optval;
int optionDebug = 0;
socklen_t optlen;
int sockFD;
sockFD = socket(AF_INET, SOCK_STREAM ,0);
optlen =sizeof(optval);
int udpFD;
udpFD = socket(AF_INET, SOCK_DGRAM,0);
optionDebug = getsockopt(sockFD, SOL_SOCKET, SO_DEBUG, optval, &optlen) ;
cout<<"My value "<< *optval<<endl;
return 0;
}

To retrieve the value for optval you need to pass the address of a valid variable (not an uninitialized pointer):
ExpectedOptType optval;
// ^^^^^^^^^^^^^^^ Put whatever type (probably a enum) is expected for
// the specific option
optionDebug = getsockopt(sockFD, SOL_SOCKET, SO_DEBUG, &optval, &optlen);
// ^
Read more details about how it works here: getsockopt(2).

Related

How to create or manage Libuv TCP server in a c++ fuction?

I am trying to create a TCP server in a function int create_server(int port_number, char ip_addr_string[IPV4_ADDR_SIZE]) which is called in main.
When I run the c++ code given below: Assertion failed: (w->fd >= 0), function uv__io_poll, file kqueue.c, line 149.
#include <iostream>
#include <vector>
#include <algorithm>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <future>
#include <sys/types.h>
#include <uv.h>
#define IPV4_ADDR_SIZE 15
uv_loop_t* loop;
struct sockaddr_in addr;
std::map <int, uv_tcp_t * > pool;
int create_tunnel(int port_number, char ip_addr_string[IPV4_ADDR_SIZE]){
uv_tcp_t global_server;
uv_tcp_init(loop, &global_server);
uv_ip4_addr("0.0.0.0", port_number, &addr);
uv_tcp_bind(&global_server, (const struct sockaddr *)&addr, 0);
int r = uv_listen((uv_stream_t *)&global_server, 128, NULL);
if(r){
fprintf(stderr, "Listen error: %s \n", uv_strerror(r));
}
else{
fprintf(stdout, "Listening on: %d \n", port_number);
}
pool[rand()] = &global_server;
return r;
}
int main(int argc, const char *argv[]){
int status = 1;
loop = uv_default_loop();
uv_loop_init(loop);
loop->data = &pool;
status = create_tunnel(7011, (char*)"0.0.0.0");
std::cout<< "status: " << status << std::endl;
uv_run(loop, UV_RUN_DEFAULT);
return 0;
I think this error is because the servers created are not tracked by the event_loop when the create_server function ends, but I am not sure.
Any help, please?

__beginthreadex was not declared in this scope

I am new to multi threading. I am trying to make a program that will listen to incoming connections, while being able to send data at the same time.
Here is the code so far. It is not finished yet, because I am stuck trying to figure out why I get this error:
__beginthreadex was not declared in this scope
Same goes for __endthreadex.
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>
#include <windows.h>
#include <process.h>
#pragma comment (lib,"Ws2_32.lib")
#define PORT "27015"
#define BUFLEN 512
using namespace std;
char recvbuf[BUFLEN];
int ires;
int recvbuflen=BUFLEN;
void Thread(void* data, SOCKET *x[2]) {
listen(*x[0],128);
*x[1]=accept(*x[0],NULL,NULL);
}
int main(int argc, char **argv){
SOCKET *SOKETI[2];
HANDLE H;
string X="Ping.";
string Y;
int i;
WSADATA wsa;
i=WSAStartup(MAKEWORD(2,0),&wsa);
struct addrinfo *result=NULL, *ptr=NULL, hints;
ZeroMemory(&hints,sizeof(hints));
hints.ai_family=AF_INET;
hints.ai_socktype=SOCK_STREAM;
hints.ai_protocol=IPPROTO_TCP;
hints.ai_flags=AI_PASSIVE;
i=getaddrinfo(NULL,PORT,&hints,&result);
SOCKET LSock;
LSock=socket(result->ai_family, result->ai_socktype, result->ai_protocol);
i=bind(LSock,result->ai_addr, result->ai_addrlen);
SOCKET CSock=INVALID_SOCKET;
SOKETI[0]=&LSock;
SOKETI[1]=&CSock;
H=(HANDLE)__beginthreadx(&Thread,0,&SOKETI,0,0,0);
while(1) {
//RECIEVER
i=recv(CSock,recvbuf,recvbuflen,0);
Y.append(recvbuf, recvbuf + i);
if (i=sizeof(X)){
H=(HANDLE)__endthreadx(&Thread,0,&SOKETI,0,0,0);
CloseHandle(H);
cout<<"Recieved "<<Y<<endl;
getchar();
return 0;
}
//RECIEVER
}
}
EDIT
I realized that I misspelled the function. Other mistakes done here are another topic.

simple client/server program using named pipes in linux

I am trying to write a program that has two separate process that talk via named pipes. The client which sends a message to a server, and the server which needs to broadcast that message to all clients attached to it. So far, I can get a connection between the two, but I cannot get more than one message to work no matter what I have tried. Below is the code I have written that will allow a connection and transmission of a single message.
server.cpp:
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#define FIFO_FILE_1 "/tmp/client_to_server_fifo"
#define FIFO_FILE_2 "/tmp/server_to_client_fifo"
int main()
{
int client_to_server;
int server_to_client;
char buf[BUFSIZ];
/* create the FIFO (named pipe) */
mkfifo(FIFO_FILE_1, 0666);
mkfifo(FIFO_FILE_2, 0666);
printf("Server ON.\n");
while (1)
{
/* open, read, and display the message from the FIFO */
client_to_server = open(FIFO_FILE_1, O_RDONLY);
server_to_client = open(FIFO_FILE_2, O_WRONLY);
read(client_to_server, buf, BUFSIZ);
if (strcmp("exit",buf)==0)
{
printf("Server OFF.\n");
break;
}
else if (strcmp("",buf)!=0)
{
printf("Received: %s\n", buf);
printf("Sending back...\n");
write(server_to_client,buf,BUFSIZ);
}
/* clean buf from any data */
memset(buf, 0, sizeof(buf));
close(client_to_server);
close(server_to_client);
}
close(client_to_server);
close(server_to_client);
unlink(FIFO_FILE_1);
unlink(FIFO_FILE_2);
return 0;
}
client.cpp:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <wait.h>
#include <string.h>
#define FIFO_FILE_1 "/tmp/client_to_server_fifo"
#define FIFO_FILE_2 "/tmp/server_to_client_fifo"
int main()
{
system("clear");
int client_to_server;
int server_to_client;
char str[140];
printf("Input message to server: ");
scanf("%139[^\r\n]", str);
/* write str to the FIFO */
client_to_server = open(FIFO_FILE_1, O_WRONLY);
server_to_client = open(FIFO_FILE_2, O_RDONLY);
if(write(client_to_server, str, sizeof(str)) < 0){
perror("Write:");//print error
exit(-1);
}
if(read(server_to_client,str,sizeof(str)) < 0){
perror("Read:"); //error check
exit(-1);
}
printf("\n...received from the server: %s\n\n\n",str);
close(client_to_server);
close(server_to_client);
/* remove the FIFO */
return 0;
}
close(client_to_server);
close(server_to_client);
Remove these lines from while loop because when server has done its work for the first time it will close the pipe and you cant be able to proceed further in pipes.

libnids didn't capture xmpp packet

this program can detect http flow and etc....
but it ignores XMPP flow ; i don't know why ?
(I guess this is a port problem , but i don't know where i should fix it )
Below are the relevant sections from main.cpp :
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include "nids.h"
#include <cstdlib>
#include <unistd.h>
#define int_ntoa(x) inet_ntoa(*((struct in_addr *)&x))
// struct tuple4 contains addresses and port numbers of the TCP connections
// the following auxiliary function produces a string looking like
// 10.0.0.1,1024,10.0.0.2,23
char *
adres (struct tuple4 addr)
{
static char buf[256];
strcpy (buf, int_ntoa (addr.saddr));
sprintf (buf + strlen (buf), ",%i,", addr.source);
strcat (buf, int_ntoa (addr.daddr));
sprintf (buf + strlen (buf), ",%i", addr.dest);
return buf;
}
void
tcp_callback (struct tcp_stream *a_tcp, void ** this_time_not_needed)
{
printf("packet captured !\n");
}
int
main ()
{
// here we can alter libnids params, for instance:
// nids_params.n_hosts=256;
struct nids_chksum_ctl nochksumchk;
nochksumchk.netaddr = 0;
nochksumchk.mask = 0;
nochksumchk.action = NIDS_DONT_CHKSUM;
//char fileName[] = "/home/test.pcap";
//nids_params.filename =fileName;
nids_register_chksum_ctl(&nochksumchk, 1);
char myDevice [] = "eth0";
nids_params.device =myDevice;
if (!nids_init ())
{
fprintf(stderr,"%s\n",nids_errbuf);
exit(1);
}
nids_register_tcp ( (void*)tcp_callback);
nids_run ();
return 0;
}
My pcap file has some problem about syncing in tcp connection .
So above snippet code of libnids is correct !

C++ INET noob, what am I doing wrong?

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <cstdio>
using namespace std;
int main(int argc, char *argv[])
{
int port, n,sockfd;
struct sockaddr_in srv_addr;
struct hostent *serv;
char buffer[256];
if(argc<3)
{
cout <<"\n ussage: host port\nexiting\n";
return 0;
}
port=atoi(argv[2]);
sockfd=socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
cout <<"\nsocket creation on"<<port<<" failed\nexiting\ncheck to see if port is in use";
return 0;
}
serv = gethostbyname(argv[1]);
if (serv=NULL)
{
cout <<"\n"<<serv<<" is not a valid host name\nexiting";
return 0;
}
bzero((char *) &srv_addr, sizeof(srv_addr));
srv_addr.sin_family=AF_INET;
bcopy((char *) serv->h_addr,
(char *) &srv_addr.sin_addr.s_addr,
serv->h_length);
srv_addr.sin_port=htons(port);
if (connect(sockfd,&srv_addr,
sizeof (srv_addr))<0)
{
cout <<" \nconnection failed\n";
}
cout <<"\nType message\n";
bzero(buffer, 256);
cin.getline(buffer,256);
n=write(sockfd,buffer,strlen(buffer));
if (n>0)
{
cout <<"\nsocket write error\nexiting\n";
}
n=read(sockfd,buffer,255);
if (n>0)
{
cout <<"\nsocket read error\nexiting\n";
}
cout <<buffer;
return 0;
}
When compiled, I get this error:
fg.c: In function ‘int main(int, char**)’:
fg.c:53:33: error: cannot convert ‘sockaddr_in*’ to ‘const sockaddr*’ for argument ‘2’ to ‘int connect(int, const sockaddr*, socklen_t)’
I cant figure it out. I am a noob to programming. Any help is grateful.
You need explicit cast. Replace
connect(sockfd,&srv_addr, sizeof (srv_addr))
with
connect(sockfd,(sockaddr*)&srv_addr, sizeof (srv_addr))