Home > OS >  [Ubuntu] [] OpenSSL programming under Ubuntu compilation error when using the OpenSSL library
[Ubuntu] [] OpenSSL programming under Ubuntu compilation error when using the OpenSSL library

Time:10-04

Ubuntu version: 16.04 64
The server-side code below
 
# include & lt; stdio.h>
# include & lt; stdlib.h>
# include & lt; Errno. H>
# include & lt; string.h>
# include & lt; sys/types.h>
# include & lt; Netinet/in. H>
# include & lt; Sys/socket. H>
# include & lt; Sys/wait. H>
# include & lt; unistd.h>
# include & lt; ARPA/inet. H>
# include & lt; Openssl/SSL. H>
# include & lt; Openssl/err. H>

# define MAXBUF 1024

Int main (int arg c, char * * argv)
{
Int sockfd, new_fd;
Socklen_t len.
Struct sockaddr_in my_addr their_addr;
Unsigned int myport lisnum;
Char buf [MAXBUF + 1];
An SSL_CTX * CTX;

If (argv [1])
Myport=atoi (argv [1]);
The else
Myport=7838;

If (argv [2])
Lisnum=atoi (argv [2]);
The else
Lisnum=2;

SSL library initialization *//*
SSL_library_init ();
/* * load all SSL algorithm/
OpenSSL_add_all_algorithms ();
/* to load all the SSL error message */
SSL_load_error_strings ();
/* to SSL V2 and V3 standard produces a an SSL_CTX compatibility mode, namely the SSL Content Text */
CTX=SSL_CTX_new (SSLv23_server_method ());
/* SSLv2_server_method can also be used () or SSLv3_server_method () separate said V2 and V3 standard */
If (CTX==NULL) {
ERR_print_errors_fp (stdout);
exit(1);
}
/* load the user digital certificate, the certificate used to send to the client, the certificate contains a public key */
If (SSL_CTX_use_certificate_file (CTX, argv [3], SSL_FILETYPE_PEM) & lt;=0) {
ERR_print_errors_fp (stdout);
exit(1);
}
/* load the user's private key */
If (SSL_CTX_use_PrivateKey_file (CTX, argv [4], SSL_FILETYPE_PEM) & lt;=0) {
ERR_print_errors_fp (stdout);
exit(1);
}
/* check whether the user's private key is correct */
if (! SSL_CTX_check_private_key (CTX)) {
ERR_print_errors_fp (stdout);
exit(1);
}

/* open a socket listening */
If ((sockfd=socket (PF_INET SOCK_STREAM, 0))==1) {
Perror (" socket ");
exit(1);
} the else
Printf (" socket created \ n ");

Bzero (& amp; My_addr, sizeof (my_addr));
My_addr. Sin_family=PF_INET;
My_addr. Sin_port=htons (myport);
My_addr. Sin_addr. S_addr=INADDR_ANY;

If (bind (sockfd, (struct sockaddr *) & amp; My_addr, sizeof (struct sockaddr))
==1) {
Perror (" bind ");
exit(1);
} the else
Printf (" binded \ n ");

If (listen (sockfd, lisnum)==1) {
Perror (" listen ");
exit(1);
} the else
Printf (" begin listen \ n ");

While (1) {
SSL * SSL;
Len=sizeof (struct sockaddr);
/* up waiting for the client even */
If ((new_fd=
Accept (sockfd, (struct sockaddr *) & amp; Their_addr,
& Len))==1) {
Perror (" accept ");
The exit (errno);
} the else
Printf (" server: got connection from % s, port % d, socket % d \ n ",
Inet_ntoa (their_addr sin_addr),
Ntohs (their_addr sin_port), new_fd);

Based on CTX to create a new SSL/* */
SSL=SSL_new (CTX);
Socket will connect users to join SSL/* */
SSL_set_fd (SSL, new_fd);
SSL connection to/* */
If (SSL_accept (SSL)==1) {
Perror (" accept ");
Close (new_fd);
break;
}

/* start dealing with the data on the each new connection to send and receive */
Bzero (buf MAXBUF + 1);
Strcpy (buf, "server - & gt; The client ");
/* to send a message to the client */
Len=SSL_write (SSL, buf, strlen (buf));

If (len & lt;=0) {
Printf
(the "message '% s' send failed! Error code was % d, error message is the '% s' \ n ",
Buf, errno, strerror (errno));
Goto finish;
} the else
Printf (" news' % s' send success, sent a total of % d bytes! \ n ",
Buf, len);

Bzero (buf MAXBUF + 1);
Receive client message/* */
Len=SSL_read (SSL, buf, MAXBUF);
If (len & gt; 0)
Printf (" receives the message success: '% s', % d bytes of data \ n ",
Buf, len);
The else
Printf
(the "message receive failure! Error code was % d, error message is the '% s' \ n ",
Errno, strerror (errno));
/* process on each new connection data sending and receiving end */
Finish:
/* close the SSL connection */
SSL_shutdown (SSL);
Release SSL/* */
SSL_free (SSL);
/* close socket */
Close (new_fd);
}
/* close listening socket */
Close (sockfd);
Release CTX/* */
SSL_CTX_free (CTX);
return 0;
}


The following error message is


Enter the g + + sll_server. C showing


Doing his homework when meet, the feeling is a question of header files? Don't know how to solve, the great god answers,

CodePudding user response:

CodePudding user response:

Need to add - LSSL with path.

CodePudding user response:

There should be no find header files, you should check whether the openssl headers under the default search path
  • Related