123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- #include <wolfssl/wolfcrypt/settings.h>
- #include <wolfssl/ssl.h>
- #include <addrinfo.h>
- #define DEFAULT_PORT 11111
- #define CERT_FILE "../certs/server-cert.pem"
- #define KEY_FILE "../certs/server-key.pem"
- int main()
- {
- int sockfd;
- int connd;
- struct sockaddr_in servAddr;
- struct sockaddr_in clientAddr;
- socklen_t size = sizeof(clientAddr);
- char buff[256];
- size_t len;
- int shutdown = 0;
- int ret;
- const char* reply = "I hear ya fa shizzle!\n";
-
- WOLFSSL_CTX* ctx;
- WOLFSSL* ssl;
-
- wolfSSL_Init();
-
- if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
- fprintf(stderr, "ERROR: failed to create the socket\n");
- return -1;
- }
-
- if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method())) == NULL) {
- fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
- return -1;
- }
-
- if (wolfSSL_CTX_use_certificate_file(ctx, CERT_FILE, SSL_FILETYPE_PEM)
- != SSL_SUCCESS) {
- fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
- CERT_FILE);
- return -1;
- }
-
- if (wolfSSL_CTX_use_PrivateKey_file(ctx, KEY_FILE, SSL_FILETYPE_PEM)
- != SSL_SUCCESS) {
- fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
- KEY_FILE);
- return -1;
- }
-
- memset(&servAddr, 0, sizeof(servAddr));
-
- servAddr.sin_family = AF_INET;
- servAddr.sin_port = htons(DEFAULT_PORT);
- servAddr.sin_addr.s_addr = INADDR_ANY;
-
- if (bind(sockfd, (struct sockaddr*)&servAddr, sizeof(servAddr)) == -1) {
- fprintf(stderr, "ERROR: failed to bind\n");
- return -1;
- }
-
- if (listen(sockfd, 5) == -1) {
- fprintf(stderr, "ERROR: failed to listen\n");
- return -1;
- }
-
- while (!shutdown) {
- printf("Waiting for a connection...\n");
-
- if ((connd = accept(sockfd, (struct sockaddr*)&clientAddr, &size))
- == -1) {
- fprintf(stderr, "ERROR: failed to accept the connection\n\n");
- return -1;
- }
-
- if ((ssl = wolfSSL_new(ctx)) == NULL) {
- fprintf(stderr, "ERROR: failed to create WOLFSSL object\n");
- return -1;
- }
-
- wolfSSL_set_fd(ssl, connd);
-
- ret = wolfSSL_accept(ssl);
- if (ret != SSL_SUCCESS) {
- fprintf(stderr, "wolfSSL_accept error = %d\n",
- wolfSSL_get_error(ssl, ret));
- return -1;
- }
- printf("Client connected successfully\n");
-
- memset(buff, 0, sizeof(buff));
- if (wolfSSL_read(ssl, buff, sizeof(buff)-1) == -1) {
- fprintf(stderr, "ERROR: failed to read\n");
- return -1;
- }
-
- printf("Client: %s\n", buff);
-
- if (strncmp(buff, "shutdown", 8) == 0) {
- printf("Shutdown command issued!\n");
- shutdown = 1;
- }
-
- memset(buff, 0, sizeof(buff));
- memcpy(buff, reply, strlen(reply));
- len = strnlen(buff, sizeof(buff));
-
- if (wolfSSL_write(ssl, buff, len) != len) {
- fprintf(stderr, "ERROR: failed to write\n");
- return -1;
- }
-
- wolfSSL_free(ssl);
- close(connd);
- }
- printf("Shutdown complete\n");
-
- wolfSSL_CTX_free(ctx);
- wolfSSL_Cleanup();
- close(sockfd);
- return 0;
- }
|