mirror of
https://github.com/wolfSSL/wolfssl.git
synced 2025-07-29 18:27:29 +02:00
Add function to print network interface and IP Address
This commit is contained in:
@ -31,23 +31,17 @@
|
||||
#include <netinet/in.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* utility functions shared between client and server */
|
||||
#include <shared/util.h>
|
||||
|
||||
/* wolfSSL */
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/ssl.h>
|
||||
#include <wolfssl/certs_test.h>
|
||||
|
||||
/* Azure Sphere */
|
||||
#include <applibs/log.h>
|
||||
#include <applibs/networking.h>
|
||||
|
||||
static void client_Cleanup(int sockfd, WOLFSSL_CTX* ctx, WOLFSSL* ssl)
|
||||
{
|
||||
wolfSSL_free(ssl); /* Free the wolfSSL object */
|
||||
wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */
|
||||
wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */
|
||||
close(sockfd); /* Close the connection to the server */
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
bool isNetworkingReady = false;
|
||||
@ -60,6 +54,8 @@ int main(int argc, char** argv)
|
||||
WOLFSSL_CTX* ctx = NULL;
|
||||
WOLFSSL* ssl = NULL;
|
||||
|
||||
util_PrintIfAddr();
|
||||
|
||||
/* Check if the Azure Sphere Dev Board has network connectivity. */
|
||||
if ((Networking_IsNetworkingReady(&isNetworkingReady) < 0) || !isNetworkingReady) {
|
||||
fprintf(stderr, "ERROR: network is not up.\n");
|
||||
@ -79,7 +75,7 @@ int main(int argc, char** argv)
|
||||
ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
|
||||
if (ctx == NULL) {
|
||||
fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
|
||||
client_Cleanup(sockfd,ctx,ssl);
|
||||
util_Cleanup(sockfd,ctx,ssl);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -87,14 +83,14 @@ int main(int argc, char** argv)
|
||||
ret = wolfSSL_CTX_load_verify_buffer(ctx, CERT, SIZEOF_CERT, WOLFSSL_FILETYPE_ASN1);
|
||||
if (ret != SSL_SUCCESS) {
|
||||
fprintf(stderr, "ERROR: failed to load %s, please check the buffer.\n");
|
||||
client_Cleanup(sockfd,ctx,ssl);
|
||||
util_Cleanup(sockfd,ctx,ssl);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Create a WOLFSSL object */
|
||||
if ((ssl = wolfSSL_new(ctx)) == NULL) {
|
||||
fprintf(stderr, "ERROR: failed to create WOLFSSL object\n");
|
||||
client_Cleanup(sockfd,ctx,ssl);
|
||||
util_Cleanup(sockfd,ctx,ssl);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -104,7 +100,7 @@ int main(int argc, char** argv)
|
||||
/* Connect to wolfSSL on the server side */
|
||||
if (wolfSSL_connect(ssl) != SSL_SUCCESS) {
|
||||
fprintf(stderr, "ERROR: failed to connect to wolfSSL\n");
|
||||
client_Cleanup(sockfd,ctx,ssl);
|
||||
util_Cleanup(sockfd,ctx,ssl);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -115,7 +111,7 @@ int main(int argc, char** argv)
|
||||
/* Send the message to the server */
|
||||
if (wolfSSL_write(ssl, msg, (int)len) != len) {
|
||||
fprintf(stderr, "ERROR: failed to write\n");
|
||||
client_Cleanup(sockfd,ctx,ssl);
|
||||
util_Cleanup(sockfd,ctx,ssl);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -123,7 +119,7 @@ int main(int argc, char** argv)
|
||||
memset(buff, 0, sizeof(buff));
|
||||
if (wolfSSL_read(ssl, buff, sizeof(buff) - 1) == -1) {
|
||||
fprintf(stderr, "ERROR: failed to read\n");
|
||||
client_Cleanup(sockfd,ctx,ssl);
|
||||
util_Cleanup(sockfd,ctx,ssl);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -131,6 +127,6 @@ int main(int argc, char** argv)
|
||||
printf("Server Reply: %s\n", buff);
|
||||
|
||||
/* Cleanup and return */
|
||||
client_Cleanup(sockfd,ctx,ssl);
|
||||
util_Cleanup(sockfd,ctx,ssl);
|
||||
return 0; /* Return reporting a success */
|
||||
}
|
||||
|
@ -31,6 +31,9 @@
|
||||
#include <netinet/in.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* <shared/util.h> includes */
|
||||
#include <shared/util.h>
|
||||
|
||||
/* wolfSSL */
|
||||
#include <wolfssl/ssl.h>
|
||||
#include <wolfssl/certs_test.h>
|
||||
@ -39,20 +42,12 @@
|
||||
#include <applibs/log.h>
|
||||
#include <applibs/networking.h>
|
||||
|
||||
#define DEFAULT_PORT 11111
|
||||
#define BIND_PORT 11111
|
||||
#define CERT_BUF server_cert_der_2048
|
||||
#define SIZEOF_CERT_BUF sizeof_server_cert_der_2048
|
||||
#define KEY_BUF server_key_der_2048
|
||||
#define SIZEOF_KEY_BUF sizeof_server_key_der_2048
|
||||
|
||||
static void server_Cleanup(int sockfd, WOLFSSL_CTX* ctx, WOLFSSL* ssl)
|
||||
{
|
||||
wolfSSL_free(ssl); /* Free the wolfSSL object */
|
||||
wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */
|
||||
wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */
|
||||
close(sockfd); /* Close the socket listening for clients */
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
bool isNetworkingReady = false;
|
||||
@ -71,9 +66,11 @@ int main(void)
|
||||
WOLFSSL_CTX* ctx = NULL;
|
||||
WOLFSSL* ssl = NULL;
|
||||
|
||||
util_PrintIfAddr();
|
||||
|
||||
/* Check if the Azure Sphere Dev Board has network connectivity. */
|
||||
if ((Networking_IsNetworkingReady(&isNetworkingReady) < 0) || !isNetworkingReady) {
|
||||
Log_Debug("\nNetwork is not up.\n");
|
||||
fprintf(stderr,"Error: Network is not up.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -85,14 +82,14 @@ int main(void)
|
||||
* 0 means choose the default protocol. */
|
||||
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
|
||||
fprintf(stderr, "ERROR: failed to create the socket\n");
|
||||
server_Cleanup(sockfd, ctx, ssl);
|
||||
util_Cleanup(sockfd, ctx, ssl);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Create and initialize WOLFSSL_CTX */
|
||||
if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method())) == NULL) {
|
||||
fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
|
||||
server_Cleanup(sockfd, ctx, ssl);
|
||||
util_Cleanup(sockfd, ctx, ssl);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -101,7 +98,7 @@ int main(void)
|
||||
!= SSL_SUCCESS) {
|
||||
fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
|
||||
CERT_BUF);
|
||||
server_Cleanup(sockfd, ctx, ssl);
|
||||
util_Cleanup(sockfd, ctx, ssl);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -110,7 +107,7 @@ int main(void)
|
||||
!= SSL_SUCCESS) {
|
||||
fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
|
||||
KEY_BUF);
|
||||
server_Cleanup(sockfd, ctx, ssl);
|
||||
util_Cleanup(sockfd, ctx, ssl);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -119,20 +116,20 @@ int main(void)
|
||||
|
||||
/* Fill in the server address */
|
||||
servAddr.sin_family = AF_INET; /* using IPv4 */
|
||||
servAddr.sin_port = htons(DEFAULT_PORT); /* on DEFAULT_PORT */
|
||||
servAddr.sin_port = htons(BIND_PORT); /* on BIND_PORT */
|
||||
servAddr.sin_addr.s_addr = INADDR_ANY; /* from anywhere */
|
||||
|
||||
/* Bind the server socket to our port */
|
||||
if (bind(sockfd, (struct sockaddr*)&servAddr, sizeof(servAddr)) == -1) {
|
||||
fprintf(stderr, "ERROR: failed to bind\n");
|
||||
server_Cleanup(sockfd, ctx, ssl);
|
||||
util_Cleanup(sockfd, ctx, ssl);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Listen for a new connection, allow 5 pending connections */
|
||||
if (listen(sockfd, 5) == -1) {
|
||||
fprintf(stderr, "ERROR: failed to listen\n");
|
||||
server_Cleanup(sockfd, ctx, ssl);
|
||||
util_Cleanup(sockfd, ctx, ssl);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -144,14 +141,14 @@ int main(void)
|
||||
if ((connd = accept(sockfd, (struct sockaddr*)&clientAddr, &size))
|
||||
== -1) {
|
||||
fprintf(stderr, "ERROR: failed to accept the connection\n\n");
|
||||
server_Cleanup(sockfd, ctx, ssl);
|
||||
util_Cleanup(sockfd, ctx, ssl);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Create a WOLFSSL object */
|
||||
if ((ssl = wolfSSL_new(ctx)) == NULL) {
|
||||
fprintf(stderr, "ERROR: failed to create WOLFSSL object\n");
|
||||
server_Cleanup(sockfd, ctx, ssl);
|
||||
util_Cleanup(sockfd, ctx, ssl);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -163,7 +160,7 @@ int main(void)
|
||||
if (ret != SSL_SUCCESS) {
|
||||
fprintf(stderr, "wolfSSL_accept error = %d\n",
|
||||
wolfSSL_get_error(ssl, ret));
|
||||
server_Cleanup(sockfd, ctx, ssl);
|
||||
util_Cleanup(sockfd, ctx, ssl);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -173,7 +170,7 @@ int main(void)
|
||||
memset(buff, 0, sizeof(buff));
|
||||
if (wolfSSL_read(ssl, buff, sizeof(buff)-1) == -1) {
|
||||
fprintf(stderr, "ERROR: failed to read\n");
|
||||
server_Cleanup(sockfd, ctx, ssl);
|
||||
util_Cleanup(sockfd, ctx, ssl);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -194,7 +191,7 @@ int main(void)
|
||||
/* Reply back to the client */
|
||||
if (wolfSSL_write(ssl, buff, (int)len) != len) {
|
||||
fprintf(stderr, "ERROR: failed to write\n");
|
||||
server_Cleanup(sockfd, ctx, ssl);
|
||||
util_Cleanup(sockfd, ctx, ssl);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -206,6 +203,6 @@ int main(void)
|
||||
printf("Shutdown complete\n");
|
||||
|
||||
/* Cleanup and return */
|
||||
server_Cleanup(sockfd, ctx, ssl);
|
||||
util_Cleanup(sockfd, ctx, ssl);
|
||||
return 0; /* Return reporting a success */
|
||||
}
|
||||
|
61
IDE/VS-AZURE-SPHERE/shared/util.h
Normal file
61
IDE/VS-AZURE-SPHERE/shared/util.h
Normal file
@ -0,0 +1,61 @@
|
||||
#ifndef _UTIL_H_
|
||||
#define _UTIL_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#include <wolfssl/ssl.h>
|
||||
#include <ifaddrs.h>
|
||||
#include <applibs/log.h>
|
||||
|
||||
#define _GNU_SOURCE /* defines NI_NUMERICHOST */
|
||||
#ifndef NI_MAXHOST
|
||||
#define NI_MAXHOST 256
|
||||
#endif
|
||||
|
||||
static void util_Cleanup(int sockfd, WOLFSSL_CTX* ctx, WOLFSSL* ssl)
|
||||
{
|
||||
wolfSSL_free(ssl); /* Free the wolfSSL object */
|
||||
wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */
|
||||
wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */
|
||||
close(sockfd); /* Close the connection to the server */
|
||||
}
|
||||
|
||||
/* Displays each AF_INET interface and it's IP Address
|
||||
* Return: WOLFSSL_SUCCESS if print is successful else WOLFSSL_FAILURE
|
||||
*/
|
||||
static int util_PrintIfAddr(void)
|
||||
{
|
||||
char host[NI_MAXHOST];
|
||||
struct ifaddrs* ifaddr, * nxt;
|
||||
int family, info, n;
|
||||
|
||||
/* Get a linked list of 'struct ifaddrs*' */
|
||||
if (getifaddrs(&ifaddr) != 0) {
|
||||
fprintf(stderr, "ERROR: Getting network interface and IP address");
|
||||
return WOLFSSL_FAILURE;
|
||||
}
|
||||
printf("\nInterface IP Address\n");
|
||||
/* Traverse ifaddr linked list using nxt */
|
||||
for (nxt = ifaddr; nxt != NULL; nxt = nxt->ifa_next) {
|
||||
if (nxt->ifa_addr == NULL)
|
||||
continue;
|
||||
family = nxt->ifa_addr->sa_family;
|
||||
/* Display the address of each AF_INET* interface */
|
||||
if (family == AF_INET) {
|
||||
info = getnameinfo(nxt->ifa_addr, sizeof(struct sockaddr_in),
|
||||
host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
|
||||
if (info != 0) {
|
||||
fprintf(stderr, "Failed to getnameinfo");
|
||||
freeifaddrs(ifaddr);
|
||||
return WOLFSSL_FAILURE;
|
||||
}
|
||||
/* Determine amount of space, n, to justify IP Address */
|
||||
n = (int)strlen("Interface ") - (int)strlen(nxt->ifa_name);
|
||||
n = (n > 0) ? n : 1; /* Set space to 1 if n is negative */
|
||||
printf("%s %*c%s>\n", nxt->ifa_name, n, '<', host);
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
freeifaddrs(ifaddr);
|
||||
return WOLFSSL_SUCCESS;
|
||||
}
|
||||
#endif
|
@ -7,17 +7,21 @@
|
||||
#ifndef SERVER_IP
|
||||
#define SERVER_IP "192.168.1.200" /* Local Test Server IP */
|
||||
#endif
|
||||
#ifndef DEFAULT_PORT
|
||||
#define DEFAULT_PORT 11111
|
||||
#endif
|
||||
#define CERT ca_cert_der_2048
|
||||
#define SIZEOF_CERT sizeof_ca_cert_der_2048
|
||||
#define DEFAULT_PORT 11111
|
||||
static const char msg[] = "Are you listening wolfSSL Server?";
|
||||
#else
|
||||
#ifndef SERVER_IP
|
||||
#define SERVER_IP "www.wolfssl.com"
|
||||
#endif
|
||||
#ifndef DEFAULT_PORT
|
||||
#define DEFAULT_PORT 443
|
||||
#endif
|
||||
#define CERT wolfssl_website_root_ca
|
||||
#define SIZEOF_CERT sizeof_wolfssl_website_root_ca
|
||||
#define DEFAULT_PORT 443
|
||||
static const char msg[] = "GET /index.html HTTP/1.1\r\n\r\n";
|
||||
#endif
|
||||
|
||||
@ -53,9 +57,6 @@
|
||||
#define USE_CERT_BUFFERS_2048
|
||||
#define USE_CERT_BUFFERS_256
|
||||
|
||||
/* Redirect printf to Log_Debug */
|
||||
#define printf Log_Debug
|
||||
|
||||
/* OS */
|
||||
#define SINGLE_THREADED
|
||||
|
||||
@ -63,6 +64,7 @@
|
||||
#define NO_FILESYSTEM
|
||||
|
||||
/* Debug */
|
||||
#define printf Log_Debug
|
||||
#define WOLFIO_DEBUG
|
||||
|
||||
#endif /* _USER_SETTINGS_H_ */
|
||||
|
Reference in New Issue
Block a user