Skip to content

Instantly share code, notes, and snippets.

@mcchae
Created March 25, 2015 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcchae/282bcfe1c79cfb288864 to your computer and use it in GitHub Desktop.
Save mcchae/282bcfe1c79cfb288864 to your computer and use it in GitHub Desktop.
CyaSSL sample
/******************************************************************************************/
// echorecv.c
//
// receiving server sample using CyaSSL
/******************************************************************************************/
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <cyassl/openssl/ssl.h>
#include <cyassl/ssl.h>
#include <cyassl/test.h>
#define MAXLINE 1024
typedef struct sockaddr_in SA;
/******************************************************************************************/
int en_recv(int clientfd)
{
int err = 0, ret;
char buffer[1024];
/* Define a structure to hold the CyaSSL context. */
CYASSL_CTX* xCyaSSL_Context;
/* Initialise CyaSSL. This must be done before any other CyaSSL functions
are called. */
CyaSSL_Init();
/* Attempt to create a context that uses the TLS V1 server protocol. */
xCyaSSL_Context = CyaSSL_CTX_new( CyaTLSv1_server_method() );
if( xCyaSSL_Context == NULL )
{
fprintf(stderr, "[%s] xCyaSSL_Context error NULL\n", __FUNCTION__);
return 0;
}
/* Load the CA certificate. Real applications should ensure that
CyaSSL_CTX_load_verify_locations() returns SSL_SUCCESS before proceeding. */
CyaSSL_CTX_load_verify_locations( xCyaSSL_Context, "ca-cert.pem", 0 );
/* Again, checking of the return values is omitted from this example,
just for clarity. Real applications must ensure the following two
functions return SSL_SUCCESS. */
CyaSSL_CTX_use_certificate_file( xCyaSSL_Context, "./certs/server-cert.pem", SSL_FILETYPE_PEM );
CyaSSL_CTX_use_PrivateKey_file( xCyaSSL_Context, "./certs/server-key.pem", SSL_FILETYPE_PEM );
CYASSL* xCyaSSL_Object;
/* A connection has been accepted by the server. Create a CyaSSL
object for use with the newly connected socket. */
xCyaSSL_Object = CyaSSL_new( xCyaSSL_Context );
if( xCyaSSL_Object == NULL )
{
fprintf(stderr, "[%s] xCyaSSL_Object error NULL\n", __FUNCTION__);
return 0;
}
/* Associate the created CyaSSL object with the connected socket
(clientfd). */
CyaSSL_set_fd( xCyaSSL_Object, clientfd );
while(1) {
char ucRxBuf[ MAXLINE ];
ret = CyaSSL_read( xCyaSSL_Object, ucRxBuf, MAXLINE );
if( ret <= 0 )
{
break;
}
printf("<%d>%s\n", ret, ucRxBuf);
}
/* CyaSSL objects should be deleted when they are no longer required. */
CyaSSL_free( xCyaSSL_Object );
/* The CyaSSL context should be deleted if it is no longer required. However,
because most deeply embedded applications will keep the context for the lifetime
of the application, and only ever be restarted when the system is rebooted, it
might be that the context is never explicitly freed. */
CyaSSL_CTX_free( xCyaSSL_Context );
/* The library itself should be shut down cleanly if it too is no longer
required. Again, because most deeply embedded applications will require the
library for the lifetime of the application, and only ever be restarted when
the system is rebooted, it might be that the library is never explicitly closed. */
CyaSSL_Cleanup();
return 0;
}
/******************************************************************************************/
int ne_recv(int clientfd)
{
int cnt = 0, ret;
while(1) {
char ucRxBuf[ MAXLINE ];
ret = read( clientfd, ucRxBuf, MAXLINE );
if( ret <= 0 )
{
break;
}
cnt++;
printf("<%d>%s\n", ret, ucRxBuf);
}
printf("\n");
return cnt;
}
/******************************************************************************************/
int main()
{
struct linger solinger = { 1, 0 };
int listenfd = socket(AF_INET, SOCK_STREAM, 0);
SA servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htons(INADDR_ANY);
servaddr.sin_port = htons(514); // CC는 514포트만 사용.
if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) {
fprintf(stderr, "[%s] bind ERROR! %s\n",__FUNCTION__, strerror(errno));
return 0;
}
if (listen(listenfd, 10) == -1) {
fprintf(stderr, "[%s] listen ERROR!\n",__FUNCTION__);
return 0;
}
while (1) {
int clientfd = accept(listenfd, NULL, NULL);
if ( clientfd < 0 ) {
fprintf(stderr, "[%s] Error calling accept()\n",__FUNCTION__);
break;
}
if (setsockopt(clientfd, SOL_SOCKET, SO_LINGER, &solinger, sizeof(struct linger)) == -1) {
perror("setsockopt(SO_LINGER)");
}
// int rc = ne_recv(clientfd);
int rc = en_recv(clientfd);
close(clientfd);
}
close(listenfd);
return 0;
}
/******************************************************************************************/
// echosend.c
//
// sending client sample using CyaSSL
/******************************************************************************************/
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <cyassl/openssl/ssl.h>
#include <cyassl/ssl.h>
#include <cyassl/test.h>
#define MAXLINE 1024
typedef struct sockaddr_in SA;
/******************************************************************************************/
int en_send(int sockfd)
{
int err = 0, ret;
char buffer[1024];
/* Define a structure to hold the CyaSSL context. */
CYASSL_CTX* xCyaSSL_Context;
/* Initialise CyaSSL. This must be done before any other CyaSSL functions
are called. */
CyaSSL_Init();
/* Attempt to create a context that uses the TLS V1 client protocol. */
xCyaSSL_Context = CyaSSL_CTX_new( CyaTLSv1_client_method() );
if( xCyaSSL_Context == NULL )
{
fprintf(stderr, "[%s] xCyaSSL_Context error NULL\n", __FUNCTION__);
return 0;
}
/* Load the CA certificate. Real applications should ensure that
CyaSSL_CTX_load_verify_locations() returns SSL_SUCCESS before proceeding. */
CyaSSL_CTX_load_verify_locations( xCyaSSL_Context, "./certs/ca-cert.pem", 0 );
CYASSL* xCyaSSL_Object;
/* The connect was successful. Create a CyaSSL object to associate with
this connection. The context created during initialisation is passed as
the function parameter. */
xCyaSSL_Object = CyaSSL_new( xCyaSSL_Context );
if( xCyaSSL_Object == NULL )
{
fprintf(stderr, "[%s] xCyaSSL_Object error NULL\n", __FUNCTION__);
return 0;
}
/* Associate the created CyaSSL object with the connected socket. */
CyaSSL_set_fd( xCyaSSL_Object, sockfd );
char ucTxBuf[ MAXLINE ];
int i;
for (i = 1; i <= 10; ++i) {
sprintf(ucTxBuf, "[%08d] log log log ...", i);
ret = CyaSSL_write( xCyaSSL_Object, ucTxBuf, strlen( ucTxBuf ) + 1);
if( ret != strlen( ucTxBuf ) + 1)
{
/* Send failed. */
err = CyaSSL_get_error(xCyaSSL_Object, ret);
fprintf(stderr, "[%s] CyaSSL_write error = %d, %s\n", __FUNCTION__,
err, CyaSSL_ERR_error_string(err, buffer));
}
printf("<%d>%s\n", ret, ucTxBuf);
}
/* CyaSSL objects should be deleted when they are no longer required. */
CyaSSL_free( xCyaSSL_Object );
/* The CyaSSL context should be deleted if it is no longer required. However,
because most deeply embedded applications will keep the context for the lifetime
of the application, and only ever be restarted when the system is rebooted, it
might be that the context is never explicitly freed. */
CyaSSL_CTX_free( xCyaSSL_Context );
/* The library itself should be shut down cleanly if it too is no longer
required. Again, because most deeply embedded applications will require the
library for the lifetime of the application, and only ever be restarted when
the system is rebooted, it might be that the library is never explicitly closed. */
CyaSSL_Cleanup();
return i;
}
/******************************************************************************************/
int ne_send(int sockfd)
{
char ucTxBuf[ MAXLINE ];
int i, ret;
for (i = 1; i <= 10; ++i) {
sprintf(ucTxBuf, "[%08d] log log log ...", i);
ret = write( sockfd, ucTxBuf, strlen( ucTxBuf )+1 );
if( ret != strlen( ucTxBuf ) + 1)
{
/* Send failed. */
fprintf(stderr, "[%s] write error: %s\n", __FUNCTION__, strerror(errno));
}
printf("<%d>%s\n", ret, ucTxBuf);
}
return i;
}
/******************************************************************************************/
int main()
{
struct timeval tv_timeo = {2};
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
SA servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(514);
if (setsockopt(sockfd, SOL_SOCKET,SO_RCVTIMEO, &tv_timeo, sizeof(tv_timeo)) == -1) {
fprintf(stderr, "[%s] SET SOCKET ERROR!",__FUNCTION__);
return 0;
}
/* Standard Berkeley sockets connect function. */
if( connect( sockfd, (const struct sockaddr *)&servaddr, sizeof( servaddr ) ) < 0 )
{
fprintf(stderr, "[%s] connect error! %s\n", __FUNCTION__, strerror(errno));
return 0;
}
// int rc = ne_send(sockfd);
int rc = en_send(sockfd);
close(sockfd);
return rc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment