1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| while(1)
{
FD_ZERO(&readfd);
FD_SET(listen_sock, &readfd);
fd_max = listen_sock;
myList = head;
while(myList != NULL)
{
FD_SET(myList->val, &readfd);
fd_max = max(myList->val, listen_sock);
myList = myList->next;
}
printf("\nSockets ouverts: "); viewItem(myList);
printf("\nAttente action !!\n");
err = select(fd_max+1, &readfd, 0, 0, 0);
EXIT_ERR(err, "select()");
myList = head;
while(myList != NULL)
{
if (FD_ISSET(myList->val, &readfd))
{
printf("Action sur socket: %i\n", myList->val);
readSock(myList->pssl, buffR, buffRSize); // myList->pssl contient la structure SSL associé au socket
printf("socket %i lu !!\n\n", myList->val);
err = SSL-shutdown(myList->pssl);
EXIT_SSL(err);
err = close(myList->val);
EXIT_ERR(err, "close");
SSL_free(myList->pssl);
delItem(&myList, myList->val);
}
if(myList != NULL) myList = myList->next;
}
if (FD_ISSET(listen_sock, &readfd))
{
/* Socket for a TCP/IP connection is created */
sock = accept(listen_sock, (struct sockaddr*)&sa_cli, &client_len);
EXIT_ERR(sock, "accept");
//close (listen_sock);
printf ("Connection from %s, port %u\n", inet_ntoa(sa_cli.sin_addr), ntohs(sa_cli.sin_port));
/* TCP connection is ready. */
/* A SSL structure is created */
ssl = SSL_new(ctx);
EXIT_NULL(ssl);
/* Assign the socket into the SSL structure */
err = SSL_set_fd(ssl, sock);
EXIT_ZERO(err);
/* Perform SSL Handshake on the SSL server */
err = SSL_accept(ssl);
EXIT_SSL(err);
/* Add the socket in the list */
err = addItem(&head, sock, ssl); //&myList // Ajout de la structure et du socket à la liste chainée
EXIT_SSL(err);
/* Informational output (optional) */
printf("SSL connection using %s\n", SSL_get_cipher(ssl));
if(SSL_ENABLED)
{
/* Get the client s certificate (optional) */
client_cert = SSL_get_peer_certificate(ssl);
if (client_cert != NULL)
{
printf ("Client certificate:\n");
str = X509_NAME_oneline(X509_get_subject_name(client_cert), 0, 0);
EXIT_NULL(str);
printf ("\t subject: %s\n", str);
free (str);
str = X509_NAME_oneline(X509_get_issuer_name(client_cert), 0, 0);
EXIT_NULL(str);
printf ("\t issuer: %s\n", str);
free (str);
X509_free(client_cert);
}
else
{
printf("The SSL client does not have certificate.\n");
}
}
} // end if (listen_sock)
} // end while |
Partager