I have a simple socket server written in c++ 10.0 and I need it to use SSL.
After dig into this for awhile, I was not able to find a solution.
My server now looks like this:
int connect_socket(int port) {
WSADATA wsa;
SOCKET s, new_socket;
struct sockaddr_in server, client;
int c;
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
std::cout << "Error Code:" << WSAGetLastError() << '\n';
return 1;
}
// Create a socket
if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
std::cout << "Could not create sockete:" << WSAGetLastError() << '\n';
return 1;
}
// Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(port);
// Bind
if (bind(s, (struct sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) {
std::cout << "Bind failed with error code:" << WSAGetLastError() << '\n';
exit(EXIT_FAILURE);
}
// Listen to incoming connections
listen(s, 3);
std::cout << "Waiting for incoming connections..." << '\n';
c = sizeof(struct sockaddr_in);
while ((new_socket = accept(s, (struct sockaddr*)&client, &c)) != INVALID_SOCKET) {
_beginthread(connection_handler, 0, &new_socket);
}
if (new_socket < 0) {
std::cout << "recv failed with error: " << '\n';
return 1;
}
return 0;
}
I try to implement OpenSSL following the example from here https://stackoverflow.com/a/16328115/1394578 but without success.
I don't have a client, just use telnet to connect with the server, when the server uses ssl I will just use openSSL to connect.
Because I'm not really comfortable with c this is a complicated task for me, if anyone can help with an example I will be grateful.