Re: sockets and openSSSL

  • From: "Littlefield, Tyler" <tyler@xxxxxxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Thu, 10 Mar 2011 09:22:57 -0700

Thanks; I'll have to resubscribe. I was on for a while, but I kind of got overwelmed with the whole "plzzzzzzzz do ma hw 4 me!" messages. :p

On 3/10/2011 9:15 AM, Alex Midence wrote:
Ty,

If I was you, I'd ask on the c-prog list.  This sort of thing is just
exactly the type of message they will dissect and redissect for you.
If you want, I can forward it to them and see what they say.  If you
want to subscribe, you can find them on yahoogroups.  Really
knowledgeable guys on that list.

Let me know,

Alex M

On 3/10/11, Littlefield, Tyler<tyler@xxxxxxxxxxxxx>  wrote:
Hello all: I have a server that runs in python using Twisted as it's
core, which is essentially an echo server. I wanted to see how dificult
this was, so I wrote a quick program that would talk to it in c++. I am
getting an error however; the handshake fails. I've pasted in the code,
if someone wouldn't mind taking a poke at it. It's kind of a tentative
setup since I haven't handled things like the socket closing, etc; this
is just a quick test for the moment.
#include<openssl/ssl.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/select.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<netdb.h>
#include<cstdio>

//used for printing an error and then exiting.
inline void error(const char* message)
{
    fprintf(stderr, "%s\n", message);
    exit(EXIT_FAILURE);
}
//the buffer size we will be working with:
#define MAX_BUFF 4096

int main()
{
    int ret; //used for holding bytes read.
    char buff[MAX_BUFF]; //a buffer for holding i/o data.
    fd_set rdesc, wdesc, srset, swset; //file descriptor sets.
    timeval tv; //used for holding the time select should wait.
    SSL_CTX* context = NULL; //ssl context.
    SSL* ssl = NULL; //main ssl object.
    sockaddr_in addr; //server socket address.

//clean out the struct:
    bzero(&addr, sizeof(sockaddr_in));
//then fill it in.
    addr.sin_family = AF_INET;
    addr.sin_port = htons(4000);
    inet_pton(AF_INET, "127.0.0.1",&addr.sin_addr.s_addr);

//create the socket
    sock=socket(AF_INET, SOCK_STREAM, 0);
    if (sock<  0)
      {
        error("Error creating initial socket.");
      }

//initialize SSL.
    SSL_load_error_strings();
    SSL_library_init();
//create the ssl context
    context = SSL_CTX_new(SSLv3_client_method());
    if (!context)
      {
        error("Could not create SSL context.");
      }

//connect the socket to the server.
    if (connect(sock, (sockaddr*)&addr, sizeof(sockaddr_in))<  0)
      {
        error("Could not connect to specified socket.");
      }

//create the ssl object.
    ssl = SSL_new(context);
    if (!ssl)
      {
        error("Could not create ssl object.");
      }

//try to set the socket as the fd for the ssl object.
    if (!SSL_set_fd(ssl, sock))
      {
        error("Error, could not bind fd to the ssl object.");
      }

//link ssl up with the socket.
    if (!SSL_connect(ssl))
      {
        error("Could not perform ssl handshake.");
      }

//set our file descriptor sets.
    FD_SET(fileno(stdin),&wdesc);
    FD_SET(sock,&srset);

//wait for data, read, then print.
    while (1)
      {
//we need to zero out our i/o buffer.
        bzero(buff, MAX_BUFF);
//initialize our temp fd sets.
        srset = rdesc;
        swset = wdesc;
//each time select finishes it changes this to how much time it actually
slept, so we need to reset it.
        tv.tv_usec = 50*1000; //50 ms
        tv.tv_sec = 0;
//perform the actual select operation.
        select(2,&srset,&swset, NULL,&tv);

//check to see if data was written on stdin (user input)
        if (FD_ISSET(fileno(stdin),&swset))
          {
//read inputted data.
            ret = read(fileno(stdin), buff, MAX_BUFF);
            if (ret)
              {
//write it to the socket.
                SSL_write(ssl, buff, ret);
              }
          }
//check to see if we received anything.
        if (FD_ISSET(sock,&srset))
          {
//read it
            ret = SSL_read(ssl, buff, MAX_BUFF);
            if (ret)
              {
//write it to screen.
                printf("%s\n", buff);
              }
          }
      }
    return 0;
}

--

Thanks,
Ty

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind




--

Thanks,
Ty

__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts: