开发者

How do I make an SSL socket available between functions?

This is my class:

  class SSLIRC : public Bot
            {
              public:
                      SSLIRC(void);
                      void sockconnect(char*, int, bool);
                      void sockwrite(char*);
                      std::string sockread(void);
                      bool connected;
                      bool verbose;
              private:
      开发者_如何学JAVA                WSADATA wsaData;
                      SOCKET m_socket;
                      sockaddr_in clientService;
                      LPHOSTENT hostEntry;

             };

and this is my SSLIRC::sockconnect

void SSLIRC::sockconnect(char* hName, int portNum, bool debug)
{
    verbose = debug;
    SSL *sslSocket = 0;
    SSL_CTX *sslContext = 0;
    int error = WSAStartup(MAKEWORD(2,0),&wsaData);

    if (error != NO_ERROR)
        std::cout << "Client: Error at WSAStartup().\n";
    else
        std::cout << "Client: WSAStartup() is OK.\n";

    fflush(stdout);

    // socket 
    int socket = 0;
    // struct for socket
    struct sockaddr_in host_addr = {0};
    //information about host
    struct hostent *host = {0};
    // name of host

    host = gethostbyname(hName);
    if (host == NULL) 
    {
      printf("Unknown Host %s\n", hName);
      exit (0);
    }
    // create socket
    // for SSL need SOCK_STREAM socket
    socket = ::socket(PF_INET, SOCK_STREAM, 0);
    if (socket < 0) 
    {
      printf("Socket Error\n");
      exit (0);
    }

    // create host struct
    host_addr.sin_family = AF_INET;
    // set IP addres
    host_addr.sin_addr = *((struct in_addr *)host->h_addr);
    // set HTTPS port
    host_addr.sin_port = htons(portNum);
    // connet
    if (connect(socket, (struct sockaddr *)&host_addr, sizeof(host_addr)) == -1) 
    {
      closesocket(socket);
      printf("Connection Error\n");
      exit (0);
    }

    sslContext = SSL_CTX_new(SSLv23_client_method());
    sslSocket = SSL_new(sslContext);

    if(!sslSocket) 
    {
      closesocket(socket);
      printf("SSL creation error\n");
      exit(0);
    }
    // join sslSocket and socket
    SSL_set_fd(sslSocket, socket);
    // conect sslSocket
    error = SSL_connect(sslSocket);
    if(!error) 
    {
      closesocket(socket);
      printf("SSL connect error = %d\n", error);
      error = SSL_get_error(sslSocket, error);
      printf("SSL error: %d\n", error);
      exit (0);
    }

  printf("SSL handshake established.\n");

}

How do I declare sslSocket so it's available in my class for the other functions to use?


I would put sslSocket in the header like so:

class SSLIRC : public Bot
{
public:
    SSLIRC(void);
    void sockconnect(char*, int, bool);
    void sockwrite(char*);
    std::string sockread(void);
    bool connected;
    bool verbose;
private:
    WSADATA wsaData;
    SOCKET m_socket;
    sockaddr_in clientService;
    LPHOSTENT hostEntry;
    SSL *sslSocket;
    int socket;
};

This way sslSocket and socket are available for the entire instance of the class to use, but not available outside of the class. Then, if you wanted a send_data method this method could use this->socket or this->sslSocket as required.

You could init these sockets in the method:

SSLIRC::SSLIRC()
{
    this->socket = 0;
    this->sslSocket = 0;
}

i.e. in the constructor. This is the usual practise.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜