util.h
1 #ifndef _UTIL_H_
2 #define _UTIL_H_
3 
4 #include <stdio.h>
5 #include <wolfssl/ssl.h>
6 #include <ifaddrs.h>
7 #include <applibs/log.h>
8 
9 #define _GNU_SOURCE /* defines NI_NUMERICHOST */
10 #ifndef NI_MAXHOST
11  #define NI_MAXHOST 256
12 #endif
13 
14 static void util_Cleanup(int sockfd, WOLFSSL_CTX* ctx, WOLFSSL* ssl)
15 {
16  wolfSSL_free(ssl); /* Free the wolfSSL object */
17  wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */
18  wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */
19  close(sockfd); /* Close the connection to the server */
20 }
21 
22 /* Displays each AF_INET interface and it's IP Address
23 * Return: WOLFSSL_SUCCESS if print is successful else WOLFSSL_FAILURE
24 */
25 static int util_PrintIfAddr(void)
26 {
27  char host[NI_MAXHOST];
28  struct ifaddrs* ifaddr, * nxt;
29  int family, info, n;
30 
31  /* Get a linked list of 'struct ifaddrs*' */
32  if (getifaddrs(&ifaddr) != 0) {
33  fprintf(stderr, "ERROR: Getting network interface and IP address");
34  return WOLFSSL_FAILURE;
35  }
36  printf("\nInterface IP Address\n");
37  /* Traverse ifaddr linked list using nxt */
38  for (nxt = ifaddr; nxt != NULL; nxt = nxt->ifa_next) {
39  if (nxt->ifa_addr == NULL)
40  continue;
41  family = nxt->ifa_addr->sa_family;
42  /* Display the address of each AF_INET* interface */
43  if (family == AF_INET) {
44  info = getnameinfo(nxt->ifa_addr, sizeof(struct sockaddr_in),
45  host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
46  if (info != 0) {
47  fprintf(stderr, "Failed to getnameinfo");
48  freeifaddrs(ifaddr);
49  return WOLFSSL_FAILURE;
50  }
51  /* Determine amount of space, n, to justify IP Address */
52  n = (int)strlen("Interface ") - (int)strlen(nxt->ifa_name);
53  n = (n > 0) ? n : 1; /* Set space to 1 if n is negative */
54  printf("%s %*c%s>\n", nxt->ifa_name, n, '<', host);
55  }
56  }
57  printf("\n");
58  freeifaddrs(ifaddr);
59  return WOLFSSL_SUCCESS;
60 }
61 #endif
Header file containing key wolfSSL API.
Definition: internal.h:2595
WOLFSSL_API void wolfSSL_free(WOLFSSL *)
This function frees an allocated wolfSSL object.
Definition: ssl.c:557
WOLFSSL_API int wolfSSL_Cleanup(void)
Un-initializes the wolfSSL library from further use. Doesn’t have to be called, though it will free ...
Definition: ssl.c:12129
WOLFSSL_API void wolfSSL_CTX_free(WOLFSSL_CTX *)
This function frees an allocated WOLFSSL_CTX object. This function decrements the CTX reference count...
Definition: ssl.c:446
Definition: internal.h:3849