How come when a thread exits the parent process also exits? When I run the server all is well. It sits and listens on the socket. When a client connects the server threads to serve it. When they talk back and forth the client exits and the server quits as well. I"m using pthread.h for the threading. Here they are!
First the client:
#include <netinet/in.h>
#include <netdb.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#define CLIENT_CONNECTED 0
#define CLIENT_NOT_CONNECTED 1
#define PORT 9999
#define MAX_CLIENTS 100
using namespace std;
struct client {
int socket;
int state;
pthread_t tid;
};
int
connectToServer (char *address )
{
struct hostent *hostinfo;
struct sockaddr_in name;
int s;
int rc = 0;
if ( ( s = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
{
cerr<<"Client could not declare socket"<<"\n";
exit( 1 );
}
name.sin_family = AF_INET;
name.sin_port = htons ( ( unsigned short int ) PORT );
name.sin_addr.s_addr = htonl( INADDR_ANY );
hostinfo = gethostbyname ( address );
if ( hostinfo == NULL )
{
cerr<<"Host unknown"<<"\n";
exit( 1 );
}
name.sin_addr = *( struct in_addr * ) hostinfo->h_addr;
if ( connect( s, ( const sockaddr * ) &name, sizeof( name ) ) < 0 )
{
cerr<<"Could not connect to host"<<"\n";
exit( 1 );
}
else
{
/* if( fcntl( s, F_SETFL, O_NONBLOCK ) == -1 )
{
perror( "fcntl" );
exit( 1 );
} */
char readbuf[1024];
char message[ ] = "START";
rc = send( s, message, strlen(message), 0 );
cout<<"RC on send() was "<<rc<<"\n";
if ( rc > 0 )
{
cout<<"using recv...\n";
while( ( rc = recv( s, readbuf, 1, 0 ) ) > 0 )
{
readbuf[ rc ] = '\0';
cout<<"Server responds with: "<<readbuf<<"\n";
}
return true;
}
else
{
return false;
}
}
}
void
killsignal( int param )
{
fprintf( stderr, "Disconnecting." );
exit( 1 );
}
int
main ( )
{
signal( SIGKILL, killsignal );
signal( SIGINT, killsignal );
char address[] = "localhost";
connectToServer( address );
}
And the server:
#include <netinet/in.h>
#include <netdb.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#define CLIENT_CONNECTED 0
#define CLIENT_NOT_CONNECTED 1
#define PORT 9999
#define MAX_CLIENTS 100
using namespace std;
struct client {
int socket;
int state;
pthread_t tid;
};
int
sendClient( char *message, int *socket )
{
int rc = 0;
cout<<message<<"\n";
rc = send( *socket, message, strlen(message), 0 );
cout<<"send RC is: "<<rc<<"\n";
}
void
strtochrstr( char **to, string from )
{
int len = from.size( );
*to = ( char * )malloc( ( len + 1 ) * sizeof( char ) );
if ( to == NULL ){
cout<<"out of memory!\n";
exit( 1 );
}
*to[ 0 ] = '\0';
strcpy( *to, from.c_str( ) );
}
int
createSocket ( int *s )
{
struct sockaddr_in name;
if ( ( *s = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
{
cerr<<"Server: Socket"<<"\n";
}
name.sin_family = AF_INET;
name.sin_port = htons( (unsigned short int) PORT );
name.sin_addr.s_addr = htonl( INADDR_ANY );
if ( bind( *s, ( struct sockaddr * ) &name, sizeof( name ) ) < 0 )
{
cerr<<"Could not bind to socket."<<"\n";
exit( 1 );
}
return *s;
}
void *
serveClient( void *clientState )
{
int c;
int rc = 0;
char readbuf[ 2 ];
char message[ ] = "Message to client";//( char * ) malloc( 300 * sizeof( char ) );
struct client *mystate = ( struct client * ) clientState;
/* Set socket tot NONBLOCKING */
if( fcntl( mystate->socket , F_SETFL, O_NONBLOCK ) == -1 )
{
perror( "fcntl" );
exit( 1 );
}
while ( true )
{
while ( ( rc = recv( mystate->socket, readbuf, 1 , 0 ) ) > 0 )
{
readbuf[ 1 ] = '\0';
cout<<readbuf<<"\n";
}
sendClient( message, &( mystate->socket ) );
}
}
int
listenUp ( int *s )
{
int i = 0;
int error = 0;
pthread_t clientIds[MAX_CLIENTS];
struct client clients[MAX_CLIENTS];
struct sockaddr_in fsaun[MAX_CLIENTS];
int fromlen[MAX_CLIENTS];
while ( i++ < MAX_CLIENTS )
clients[i].state = CLIENT_NOT_CONNECTED;
if ( listen( *s, 10 ) < 0 )
{
cerr<<"Could not listen on socket"<<"\n";
exit( 1 );
}
while ( true )
{
while ( ( clients[i++].state == CLIENT_CONNECTED && i < MAX_CLIENTS ) );
if ( ( clients[i].socket = accept(
*s,
( sockaddr * ) &fsaun[i],
( socklen_t * ) &fromlen[i] ) ) < 0 )
{
cerr<<"Could not accept connection "<<i<<"\n";
}
else
{
error = pthread_create(
&clients[i].tid,
NULL,
serveClient,
(void *)&clients[i]
);
}
i = 0;
}
}
void
killsignal( int param )
{
fprintf( stderr, "Disconnecting.\n" );
}
void
intsignal( int param )
{
fprintf( stderr, "Write error.\n" );
}
int
main ( )
{
signal( SIGKILL, killsignal );
signal( SIGINT, intsignal );
int mySock = createSocket( &mySock );
listenUp( &mySock );
}
Your server is continously sending data to the client.
When your client exits, it hasn't read all the data there is to read on the socket.
This condition will generate a TCP RST, the default behavior on *nixes when a TCP RST is received is to deliver the SIGPIPE signal to the process. The default behavior of the SIGPIPE signal is to exit the program.
For TCP servers its common to just ignore the SIGPIPE signal, with SIGPIPE ignored write()/send() will return an error when the mentioned condition(a client exits or closes a socket with pending data) and set errno to EPIPE
Add this in main() of your server:
signal(SIGPIPE,SIG_IGN);
Some more info can be found here
exit terminates the process so including all threads you spawned inside that same process. You should just return from your thread-functions without calling exit.
exit() kills the whole process, which is going to kill all of the threads that comprise it. You use the words "thread" and "process" interchangeably, which suggests there might be some confusion between the two in your mind.
Multiple threads can execute within a single process, but if the process dies, all of its threads die.
Related
#include <sys/socket.h> // for socket(), bind(), listen(), accept()
#include <netinet/in.h> // for PF_INET, SOCK_STREAM, IPPROTO_TCP
#include <stdio.h> // for printf(), perror()
#include <unistd.h> // for read(), write(), close()
#include <string.h> // for bzero()
#define DEMO_PORT 9723
int main( void )
{
//-----------------------------------------
// create an unnamed socket for the server
//-----------------------------------------
int sock = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP );
if ( sock < 0 )
{
perror( "opening stream socket" );
return -1;
}
printf( "\nsock=%d \n", sock );
//----------------------------------------
// construct a name for the server socket
//----------------------------------------
struct sockaddr_in self;
socklen_t nlen = sizeof self;
bzero( &self, sizeof self );
self.sin_family = AF_INET;
self.sin_port = htons( DEMO_PORT );
self.sin_addr.s_addr = htonl( INADDR_ANY );
if ( bind( sock, (sockaddr *)&self, nlen ) < 0 )
{
perror( "bind failed" );
return -1;
}
printf( "bind succeeded port: %d\n",DEMO_PORT);
//---------------------------------------------------------
// now create a connection queue and wait for some clients
//---------------------------------------------------------
if ( listen( sock, 5 ) < 0 )
{
perror( "listen failed" );
return -1;
}
printf( "listen succeeded \n" );
//---------------------------------------------------
// main loop to process clients' connection-requests
//---------------------------------------------------
while ( 1 )
{
sockaddr_in peer;
socklen_t plen = sizeof peer;
bzero( &peer, plen );
printf( "server waiting \n" );
int client = accept( sock, (sockaddr *)&peer, &plen );
if ( client < 0 ) { perror( "accept" ); break; }
//---------------------------------------------
// we can now read from or write to the client
//---------------------------------------------
char ch;
int index = 0;
char get[1024];
if ( read( client, &ch, 1 ) < 0 )
{
perror( "read" );
}
bool go = true;
while(go){
if(ch != '/' && ch != '\r'){
printf("read: %hhd\n", ch);
get[index] = ch;
printf( "got stuff: %s\n", get );
index++;
read(index, &ch, 1);
} else {
go = false;
index = 0;
close( client );
printf( "server responded, connection closed" );
}
}
//if ( write( client, &ch, 1 ) < 0 ) { perror( "write" ); break; }
}
close( sock );
printf( "\n" );
}
the while(go) look never works and it buffers incorrectly. I do not see the pattern within the buffer or how it works.
What is the correct way to do this? to except a certain amount of bytes, store them in a string, then terminate the read if it reaches a newline character?
You are passing index into the second read call where you should be passing client.
This may not be your only problem but I think it is one of them.
I'm sending from a .Net application 1404 float values which make up for 5616 bytes through an udp socket. I get no exceptions off this operation.
However, the program receiving those data, is a C++ application, and when receiving such amount of data I get a 10040 message too long error.
Apparently 1480bytes is the longest size possible for messages in Wsock2.
What would be the easiest, cleanest way to fix this?
Thanks!
EDIT: Posting some code:
This is my socket J_Receive class:
#include "J_Receive.h"
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#if defined (WIN32) && !defined(__CYGWIN__)
#include <winsock.h>
#else
#include <unistd.h>
#include <sys/uio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/time.h>
#endif
#include <string.h>
#include <iostream>
using namespace sockets;
J_Recibir::J_Recibir( void )
{
_port = 0;
_initialized = false;
_buffer = 0L;
}
J_Recibir::~J_Recibir( void )
{
#if defined (WIN32) && !defined(__CYGWIN__)
closesocket( _so);
#else
close( _so );
#endif
}
bool J_Recibir::init( void )
{
#if defined(WIN32) && !defined(__CYGWIN__)
WORD version = MAKEWORD(1,1);
WSADATA wsaData;
// First, we start up Winsock
WSAStartup(version, &wsaData);
#endif
if( _port == 0 )
{
fprintf( stderr, "Receiver::init() - port not defined\n" );
return false;
}
if( (_so = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 )
{
perror( "Socket" );
return false;
}
/*int buffsize = 50000;
setsockopt( _so, SOL_SOCKET, SO_RCVBUF, (const char*)&buffsize, sizeof(buffsize));*/
#if defined (WIN32) && !defined(__CYGWIN__)
// const BOOL on = TRUE;
// setsockopt( _so, SOL_SOCKET, SO_REUSEADDR, (const char*) &on, sizeof(int));
#else
int on = 1;
setsockopt( _so, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
#endif
// struct sockaddr_in saddr;
saddr.sin_family = AF_INET;
saddr.sin_port = htons( _port );
#if defined (WIN32) && !defined(__CYGWIN__)
saddr.sin_addr.s_addr = htonl(INADDR_ANY);
#else
saddr.sin_addr.s_addr = 0;
#endif
if( bind( _so, (struct sockaddr *)&saddr, sizeof( saddr )) < 0 )
{
perror( "bind" );
return false;
}
u_long iMode = 1; // 1 para No bloqueante, 0 para bloqueante
ioctlsocket(_so, FIONBIO, &iMode);
_initialized = true;
return _initialized;
}
void J_Recibir::setPort( const short port )
{
_port = port;
}
void J_Recibir::setBuffer( void *buffer, const unsigned int size )
{
_buffer = buffer;
_buffer_size = size;
}
int J_Recibir::sync( void )
{
if(!_initialized) init();
if( _buffer == 0L )
{
fprintf( stderr, "Receiver::sync() - No buffer\n" );
return -1;
}
#if defined(__linux) || defined(__FreeBSD__) || defined( __APPLE__ )
socklen_t
#else
int
#endif
size = sizeof( struct sockaddr_in );
fd_set fdset;
FD_ZERO( &fdset );
FD_SET( _so, &fdset );
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
#if defined (WIN32) && !defined(__CYGWIN__)
// saddr.sin_port = htons( _port );
recvfrom( _so, (char *)_buffer, _buffer_size, 0, (sockaddr*)&saddr, &size );
// recvfrom(sock_Receive, szMessage, 256, 0, (sockaddr*)&addr_Cli, &clilen)
int err = WSAGetLastError ();
if (err!=0){
fprintf( stderr, "Receiver::sync() - error %d\n",err );
perror("Error: ");
}
while( select( _so+1, &fdset, 0L, 0L, &tv ) )
{
if( FD_ISSET( _so, &fdset ) )
{
recvfrom( _so, (char *)_buffer, _buffer_size, 0, (sockaddr*)&saddr, &size );
}
}
#else
recvfrom( _so, (caddr_t)_buffer, _buffer_size, 0, 0, &size );
while( select( _so+1, &fdset, 0L, 0L, &tv ) )
{
if( FD_ISSET( _so, &fdset ) )
{
recvfrom( _so, (caddr_t)_buffer, _buffer_size, 0, 0, &size );
}
}
#endif
if (err!=0) return -1;
else return 0;
}
And this is how I call the receive function:
sockets::J_Receiver receiverGUI = new sockets::J_Recibir();
receiverGUI->setPort(4020);
nDatosGUI = 1404;
float* datosGUI = new datosGUI[nDatosGUI ];
receiverGUI->setBuffer((void *)datosGUI, sizeof(float)*nDatosGUI);
WSAEMSGSIZE usually means that the buffer you provided to recvfrom() was smaller than the incoming datagram. Check or post your recvfrom() code to make sure you are using a sufficiently large and correctly declared buffer. Because IPv4 packets can (theoretically) be up to 64 kilobytes in size, it is safest to always use a buffer that large.
Reading from the MSDN documentation for error WSAEMSGSIZE (10040):
Message too long.
A message sent on a datagram socket was larger than the internal message buffer or some other network limit, or the buffer used to receive a datagram was smaller than the datagram itself.
This probably means that your receive buffer is too small, and you need to enlarge it. This is done with the setsockopt function and the SO_RCVBUF option.
10040 is telling you to use a larger buffer when you call recvfrom(). It does NOT mean for you to increase the size of the socket's internal receive buffer.
Since you already know how many floats you are expecting to receive, simply declare a buffer large enough to receive them all, eg:
float buffer[1404];
int ret = recvfrom(..., (char*)&buffer[0], sizeof(buffer), ...);
Winsock most certainly does NOT have a 1480 byte limit on messages.
I have the following code which by default connects to interface "eth0" which is a 1G NIC, but I would like to connect using "eth5", which is a 10G NIC.
class TCPClientSocket {
protected:
int socket_file_descriptor_;
public:
TCPClientSocket ( )
: socket_file_descriptor_ ( -1 )
{
/* socket creation */
socket_file_descriptor_ = socket ( AF_INET, SOCK_STREAM, 0 );
if ( socket_file_descriptor_ < 0 ) { exit(1); }
}
void Connect ( const std::string & _ors_ip_, const int _ors_port_ ) {
struct sockaddr_in ors_Addr_ ;
bzero ( &ors_Addr_, sizeof ( ors_Addr_ ) ) ;
ors_Addr_.sin_family = AF_INET;
ors_Addr_.sin_port = htons ( _ors_port_ );
inet_pton ( AF_INET, _ors_ip_.c_str(), &(ors_Addr_.sin_addr) );
if ( connect ( socket_file_descriptor_, (struct sockaddr *) &ors_Addr_, sizeof(struct sockaddr_in) ) < 0 ) {
fprintf ( stderr, "connect() failed on %s:%d\n", _ors_ip_.c_str( ), _ors_port_ );
close ( socket_file_descriptor_ );
socket_file_descriptor_ = -1;
}
}
inline int WriteN ( const unsigned int _len_, const void * _src_ ) const {
if ( socket_file_descriptor_ != -1 ) {
return write ( socket_file_descriptor_, _src_, _len_ );
}
return -1;
}
inline int ReadN ( const unsigned int _len_, void * _dest_ ) const {
if ( socket_file_descriptor_ != -1 ) {
return read ( socket_file_descriptor_, _dest_, _len_ );
}
return -1;
}
inline bool IsOpen ( ) const { return ( socket_file_descriptor_ != -1 ) ; }
inline int socket_file_descriptor() const { return socket_file_descriptor_; }
void Close ( ) {
if ( socket_file_descriptor_ != -1 ) {
shutdown ( socket_file_descriptor_, SHUT_RDWR );
close ( socket_file_descriptor_ );
socket_file_descriptor_ = -1;
}
}
};
According to the information here you can use setsockopt() to achieve this as follows:
char* interface = "eth5";
setsockopt( socket_file_descriptor_, SOL_SOCKET, SO_BINDTODEVICE, interface, 4 );
The final parameter, 4, represents the number of bytes in the interface variable.
I have an error during compiling:
raja#raja-desktop:~/socket$ g++ Socket.cpp
Socket.cpp: In member function ‘int Socket::recv(std::string&) const’:
Socket.cpp:135: error: ‘cout’ is not a member of ‘std’
Source of Socket.cpp:
// Implementation of the Socket class.
#include "Socket.h"
#include "string.h"
#include <string.h>
#include <errno.h>
#include <fcntl.h>
Socket::Socket() :
m_sock ( -1 )
{
memset ( &m_addr,
0,
sizeof ( m_addr ) );
}
Socket::~Socket()
{
if ( is_valid() )
::close ( m_sock );
}
bool Socket::create()
{
m_sock = socket ( AF_INET,
SOCK_STREAM,
0 );
if ( ! is_valid() )
return false;
// TIME_WAIT - argh
int on = 1;
if ( setsockopt ( m_sock, SOL_SOCKET, SO_REUSEADDR, ( const char* ) &on, sizeof ( on ) ) == -1 )
return false;
return true;
}
bool Socket::bind ( const int port )
{
if ( ! is_valid() )
{
return false;
}
m_addr.sin_family = AF_INET;
m_addr.sin_addr.s_addr = INADDR_ANY;
m_addr.sin_port = htons ( port );
int bind_return = ::bind ( m_sock,
( struct sockaddr * ) &m_addr,
sizeof ( m_addr ) );
if ( bind_return == -1 )
{
return false;
}
return true;
}
bool Socket::listen() const
{
if ( ! is_valid() )
{
return false;
}
int listen_return = ::listen ( m_sock, MAXCONNECTIONS );
if ( listen_return == -1 )
{
return false;
}
return true;
}
bool Socket::accept ( Socket& new_socket ) const
{
int addr_length = sizeof ( m_addr );
new_socket.m_sock = ::accept ( m_sock, ( sockaddr * ) &m_addr, ( socklen_t * ) &addr_length );
if ( new_socket.m_sock <= 0 )
return false;
else
return true;
}
bool Socket::send ( const std::string s ) const
{
int status = ::send ( m_sock, s.c_str(), s.size(), MSG_NOSIGNAL );
if ( status == -1 )
{
return false;
}
else
{
return true;
}
}
int Socket::recv ( std::string& s ) const
{
char buf [ MAXRECV + 1 ];
s = "";
memset ( buf, 0, MAXRECV + 1 );
int status = ::recv ( m_sock, buf, MAXRECV, 0 );
if ( status == -1 )
{
std::cout << "status == -1 errno == " << errno << " in Socket::recv\n";
return 0;
}
else if ( status == 0 )
{
return 0;
}
else
{
s = buf;
return status;
}
}
bool Socket::connect ( const std::string host, const int port )
{
if ( ! is_valid() ) return false;
m_addr.sin_family = AF_INET;
m_addr.sin_port = htons ( port );
int status = inet_pton ( AF_INET, host.c_str(), &m_addr.sin_addr );
if ( errno == EAFNOSUPPORT ) return false;
status = ::connect ( m_sock, ( sockaddr * ) &m_addr, sizeof ( m_addr ) );
if ( status == 0 )
return true;
else
return false;
}
void Socket::set_non_blocking ( const bool b )
{
int opts;
opts = fcntl ( m_sock,
F_GETFL );
if ( opts < 0 )
{
return;
}
if ( b )
opts = ( opts | O_NONBLOCK );
else
opts = ( opts & ~O_NONBLOCK );
fcntl ( m_sock,
F_SETFL,opts );
}
And Socket.h:
// Definition of the Socket class
#ifndef Socket_class
#define Socket_class
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <string>
#include <arpa/inet.h>
const int MAXHOSTNAME = 200;
const int MAXCONNECTIONS = 5;
const int MAXRECV = 500;
class Socket
{
public:
Socket();
virtual ~Socket();
// Server initialization
bool create();
bool bind ( const int port );
bool listen() const;
bool accept ( Socket& ) const;
// Client initialization
bool connect ( const std::string host, const int port );
// Data Transimission
bool send ( const std::string ) const;
int recv ( std::string& ) const;
void set_non_blocking ( const bool );
bool is_valid() const { return m_sock != -1; }
private:
int m_sock;
sockaddr_in m_addr;
};
#endif
Add this at the top of the file:
#include <iostream>
To add to John's answer, also fix the top to this:
// string.h is deprecated
#include <string>
// or
#include <cstring>
I'm writing an app in C++ using openssl, and I can't seem to get the ssl socket connection to work.
I have an abstract class, with multiple functions implemented using various protocols by the inheriting classes and simple TCP and UDP ( posix sockets ) work fine.
I could not get the ssl working though and after some code browsing and tweaking I found out, that it is the fork() function which causes problems.
I wrote a very simple program to check things out, and this is what I get:
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <iostream>
#include <errno.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/timeb.h>
#include <netinet/in.h>
#include <sys/select.h>
#include <netdb.h>
#include <time.h>
#include </usr/local/include/ssl/ssl.h>
#include "SSL_CA.cpp"
int main( int argc, char **argv ) {
int retval;
SSL_CTX *ctx;
SSL *con;
int port = atoi(argv[1]);
sockaddr_in client_addr;
sockaddr_in serv_addr;
socklen_t client_len;
int max_clients = 40;
int serv_sock, client_sock;
if ( ( serv_sock = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
return -12;
SSL_library_init();
int SSL_CA = NowySSL();
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = port;
cout << "Conf " << endl;
if ( bind( serv_sock, ( sockaddr * ) &serv_addr, sizeof( serv_addr ) ) < 0 )
return -1;
if ( listen( serv_sock, max_clients ) < 0 )
return -1;
while (1) {
cout << "Waiting" << endl;
if ( ( client_sock = accept( serv_sock, ( sockaddr * ) &client_addr, &client_len ) ) < 0 )
return -1;
pid_t pid = fork();
if ( pid = 0 ) {
con = NULL;
ctx = NULL;
ctx = (SSL_CTX *)SSL_CTX_new( SSLv23_server_method() );
con = (SSL *)SSL_new( (SSL_CTX *)ctx );
if (
!( ( ( !SSL_CA ) && ( SSL_NO_CA_RUN( con, ctx, client_sock, 0 ) ) ) ||
( ( SSL_CA ) && ( SSL_CA_RUN( con, ctx, client_sock, true ) ) ) )
)
return -1;
char buf[10];
if ( ( retval = SSL_read( con, buf, 10 ) ) <= 0 )
return -16;
cout << "Got msg " << buf << " " << retval << endl;
sprintf(buf, "12345" );
if ( ( retval = SSL_write( con, buf, strlen(buf) ) ) <= 0 )
return -1;
cout << "Sent" << endl;
}
else cout << "Parent " << endl;
}
return 0;
}
These are the SSL_CA_RUN and SSL_NO_CA_RUN functions - not written by me, but i have to use it
int SSL_NO_CA_RUN (SSL *con, SSL_CTX *ctx, int msgsock, int exit_now)
{
// printf ("SSL NO CA\n");
// SSL_library_init();
// ctx=(SSL_CTX *)SSL_CTX_new(SSLv23_server_method());
// con=(SSL *)SSL_new((SSL_CTX *)ctx);
if ((!SSL_set_fd (con,msgsock))||
(!SSL_use_RSAPrivateKey_file (con,"ktsa_u_linux.k",1))||
(!SSL_use_certificate_file (con,"ktsa_u_linux.c",1))||
(!SSL_accept (con)))
{
printf ("SSL ERROR (%s)\n",strerror(errno));
if (exit_now)
{
zamknij_polaczenie (con,ctx,msgsock);
exit(0);
}
return false;
}
// printf ("SSL NO CA OK!!!\n");
return true;
}
int SSL_CA_RUN (SSL *con, SSL_CTX *ctx, int msgsock, int exit_now)
{
int ret, ok=true;
X509 *peer;
FILE *fp;
char error[250];
// printf ("SSL CA\n");
//MARCIN (START)
//robimy linki do certyfikatow Marcin
certhash(CA_DIR,CRL_DIR,getpid());
//dostep_read (890);
// SSL_library_init();
// printf("\n");
// ctx=(SSL_CTX *)SSL_CTX_new(SSLv23_server_method());
// con=(SSL *)SSL_new((SSL_CTX *)ctx);
cb=pem_passwd_cb;
SSL_CTX_set_default_passwd_cb_userdata(ctx,haslo);
SSL_CTX_set_default_passwd_cb(ctx,cb);
//SSL_set_verify_depth(con,10); default = 9
SSL_set_verify(con,SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,NULL);
// SSL_CTX_set_verify(ctx,SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,NULL);
//SSL_CTX_set_cipher_list(ctx,"SHA1+3DES:+RSA:HIGH");
//SSL_set_cipher_list(con,"SHA1+3DES:+RSA:HIGH");
if ((!SSL_set_fd (con,msgsock))||
(!SSL_use_RSAPrivateKey_file (con,KEY_FILE,1))||
(!SSL_use_certificate_file (con,CERT_FILE,1))||
// (!SSL_CTX_load_verify_locations(ctx,CA_FILE,NULL))||
(!SSL_CTX_load_verify_locations(ctx,NULL,linkdir))||
(!SSL_accept (con))
)
{
if (exit_now)
{
remove_symdir(getpid());
// printf("error connect\n");
zamknij_polaczenie (con,ctx,msgsock);
exit(0);
} else remove_symdir(getpid());
ok=false;
}
if (ok)
{
peer=SSL_get_peer_certificate(con);
if(peer!=NULL)
{
//ret=1 - odwolany
//ret=0 - wszystko ok
//ret = inne - jakis inny blad, np. brak wlasciwej listy crl
//ret=check_peer_crl_dir(peer,error,CRL_DIR); //sprawdzenie odwolania certyfikatu przychodzacego
ret=check_peera(peer,error,0); //sprawdzenie odwolania certyfikatu przychodzacego
// printf("peer certyfikat:%s:%i\n",error,ret);
}
if ((peer==NULL)||(ret==1))
{
//nie akceptujemy polaczenia bo nie dostalismy certyfikatu lub certyfikat jest odwolany
// printf("polaczenie odrzucone - certyfikat peera odwolany, brak certyfikatu lub wystawiony przez inne CA \n");
if (exit_now)
{
remove_symdir(getpid());
zamknij_polaczenie (con,ctx,msgsock);
exit(2);
}
ok=false;
}
}
if (ok)
{
X509_free(peer); //potrzebne?
peer=NULL;
//mozna sprawdzic tez nasz certyfikat
if (!(fp = fopen (CERT_FILE, "r"))) //"/router/ssl/cert/sslcert.cer"
printf ("Error reading my certificate file\n");
if (!(peer = PEM_read_X509 (fp, NULL, NULL, NULL)))
printf ("Error reading my certificate in file\n");
if (fp)fclose (fp);
if(peer!=NULL)
{
//ret=1 - odwolany
//ret=0 - wszystko ok
//ret = inne - jakis inny blad, np. brak wlasciwej listy crl
//ret=check_peer_crl_dir(peer,error,CRL_DIR); //sprawdzenie odwolania certyfikatu przychodzacego
ret=check_peera(peer,error,0); //sprawdzenie odwolania certyfikatu naszego
// printf("nasz certyfikat:%s:%i\n",error,ret);
}
if ((peer==NULL)||(ret==1))
{
//nie akceptujemy polaczenia bo nasz certyfikat ma problem lub jest odwolany
// printf("polaczenie odrzucone- nasz certyfikat odwolany\n");
if (exit_now)
{
remove_symdir(getpid());
zamknij_polaczenie (con,ctx,msgsock);
exit(2);
}
ok=false;
}
}
if (ok)
{
X509_free(peer); //potrzebne?
peer=NULL;
}
// printf("connected...ok\n");
remove_symdir(getpid());
return ok;
}
If I don't call fork, all works fine, the message gets through, if the fork's there it gets stuck on ssl_accept.
Any help?
Not sure if this is just a typo or your real problem but this isn't going to do what you intend:
pid_t pid = fork();
if ( pid = 0 )
{
con = NULL;
//.............
Your child code is not going to execute.