util.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* util.h
  2. *
  3. * Copyright (C) 2006-2023 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. #include <netdb.h>
  28. #define _GNU_SOURCE /* defines NI_NUMERICHOST */
  29. #ifndef NI_MAXHOST
  30. #define NI_MAXHOST 256
  31. #endif
  32. static void util_Cleanup(int sockfd, WOLFSSL_CTX* ctx, WOLFSSL* ssl)
  33. {
  34. wolfSSL_free(ssl); /* Free the wolfSSL object */
  35. wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */
  36. wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */
  37. close(sockfd); /* Close the connection to the server */
  38. }
  39. /* Displays each AF_INET interface and it's IP Address
  40. * Return: WOLFSSL_SUCCESS if print is successful else WOLFSSL_FAILURE
  41. */
  42. static int util_PrintIfAddr(void)
  43. {
  44. char host[NI_MAXHOST];
  45. struct ifaddrs* ifaddr, * nxt;
  46. int family, info, n;
  47. /* Get a linked list of 'struct ifaddrs*' */
  48. if (getifaddrs(&ifaddr) != 0) {
  49. fprintf(stderr, "ERROR: Getting network interface and IP address");
  50. return WOLFSSL_FAILURE;
  51. }
  52. printf("\nInterface IP Address\n");
  53. /* Traverse ifaddr linked list using nxt */
  54. for (nxt = ifaddr; nxt != NULL; nxt = nxt->ifa_next) {
  55. if (nxt->ifa_addr == NULL)
  56. continue;
  57. family = nxt->ifa_addr->sa_family;
  58. /* Display the address of each AF_INET* interface */
  59. if (family == AF_INET) {
  60. info = getnameinfo(nxt->ifa_addr, sizeof(struct sockaddr_in),
  61. host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
  62. if (info != 0) {
  63. fprintf(stderr, "Failed to getnameinfo");
  64. freeifaddrs(ifaddr);
  65. return WOLFSSL_FAILURE;
  66. }
  67. /* Determine amount of space, n, to justify IP Address */
  68. n = (int)strlen("Interface ") - (int)strlen(nxt->ifa_name);
  69. n = (n > 0) ? n : 1; /* Set space to 1 if n is negative */
  70. printf("%s %*c%s>\n", nxt->ifa_name, n, '<', host);
  71. }
  72. }
  73. printf("\n");
  74. freeifaddrs(ifaddr);
  75. return WOLFSSL_SUCCESS;
  76. }
  77. #endif