2
0

util.h 2.1 KB

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