如何在函数之间创建SSL套接字?

时间:2010-11-30 02:44:26

标签: visual-studio-2010 visual-c++ openssl visual-c++-2010 winsock2

这是我的班级:

  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;

             };

这是我的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");

}

如何声明sslSocket,以便在我的班级中可以使用其他函数?

1 个答案:

答案 0 :(得分:0)

我会把sslSocket放在标题中:

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;
};

这样,sslSocket和socket可供整个类实例使用,但在类之外不可用。然后,如果您需要send_data方法,则此方法可以根据需要使用this->socketthis->sslSocket

您可以在方法中初始化这些套接字:

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

即。在构造函数中。这是通常的做法。