1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- #ifndef _UTIL_H_
- #define _UTIL_H_
- #include <stdio.h>
- #include <wolfssl/ssl.h>
- #include <ifaddrs.h>
- #include <applibs/log.h>
- #include <netdb.h>
- #define _GNU_SOURCE
- #ifndef NI_MAXHOST
- #define NI_MAXHOST 256
- #endif
- static void util_Cleanup(int sockfd, WOLFSSL_CTX* ctx, WOLFSSL* ssl)
- {
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- wolfSSL_Cleanup();
- close(sockfd);
- }
- static int util_PrintIfAddr(void)
- {
- char host[NI_MAXHOST];
- struct ifaddrs* ifaddr, * nxt;
- int family, info, n;
-
- if (getifaddrs(&ifaddr) != 0) {
- fprintf(stderr, "ERROR: Getting network interface and IP address");
- return WOLFSSL_FAILURE;
- }
- printf("\nInterface IP Address\n");
-
- for (nxt = ifaddr; nxt != NULL; nxt = nxt->ifa_next) {
- if (nxt->ifa_addr == NULL)
- continue;
- family = nxt->ifa_addr->sa_family;
-
- 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;
- }
-
- n = (int)strlen("Interface ") - (int)strlen(nxt->ifa_name);
- n = (n > 0) ? n : 1;
- printf("%s %*c%s>\n", nxt->ifa_name, n, '<', host);
- }
- }
- printf("\n");
- freeifaddrs(ifaddr);
- return WOLFSSL_SUCCESS;
- }
- #endif
|