util.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* util.h
  2. *
  3. * Copyright (C) 2006-2022 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifndef _UTIL_H_
  22. #define _UTIL_H_
  23. #include <stdio.h>
  24. #include <wolfssl/ssl.h>
  25. #include <ifaddrs.h>
  26. #include <applibs/log.h>
  27. #define _GNU_SOURCE /* defines NI_NUMERICHOST */
  28. #ifndef NI_MAXHOST
  29. #define NI_MAXHOST 256
  30. #endif
  31. static void util_Cleanup(int sockfd, WOLFSSL_CTX* ctx, WOLFSSL* ssl)
  32. {
  33. wolfSSL_free(ssl); /* Free the wolfSSL object */
  34. wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */
  35. wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */
  36. close(sockfd); /* Close the connection to the server */
  37. }
  38. /* Displays each AF_INET interface and it's IP Address
  39. * Return: WOLFSSL_SUCCESS if print is successful else WOLFSSL_FAILURE
  40. */
  41. static int util_PrintIfAddr(void)
  42. {
  43. char host[NI_MAXHOST];
  44. struct ifaddrs* ifaddr, * nxt;
  45. int family, info, n;
  46. /* Get a linked list of 'struct ifaddrs*' */
  47. if (getifaddrs(&ifaddr) != 0) {
  48. fprintf(stderr, "ERROR: Getting network interface and IP address");
  49. return WOLFSSL_FAILURE;
  50. }
  51. printf("\nInterface IP Address\n");
  52. /* Traverse ifaddr linked list using nxt */
  53. for (nxt = ifaddr; nxt != NULL; nxt = nxt->ifa_next) {
  54. if (nxt->ifa_addr == NULL)
  55. continue;
  56. family = nxt->ifa_addr->sa_family;
  57. /* Display the address of each AF_INET* interface */
  58. if (family == AF_INET) {
  59. info = getnameinfo(nxt->ifa_addr, sizeof(struct sockaddr_in),
  60. host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
  61. if (info != 0) {
  62. fprintf(stderr, "Failed to getnameinfo");
  63. freeifaddrs(ifaddr);
  64. return WOLFSSL_FAILURE;
  65. }
  66. /* Determine amount of space, n, to justify IP Address */
  67. n = (int)strlen("Interface ") - (int)strlen(nxt->ifa_name);
  68. n = (n > 0) ? n : 1; /* Set space to 1 if n is negative */
  69. printf("%s %*c%s>\n", nxt->ifa_name, n, '<', host);
  70. }
  71. }
  72. printf("\n");
  73. freeifaddrs(ifaddr);
  74. return WOLFSSL_SUCCESS;
  75. }
  76. #endif