LCOV - code coverage report
Current view: top level - felix-star/src - config.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 14 19 73.7 %
Date: 2025-06-10 03:23:28 Functions: 1 1 100.0 %

          Line data    Source code
       1             : #include <sys/socket.h>
       2             : #include <sys/types.h>
       3             : #include <netdb.h>
       4             : #include <ifaddrs.h>
       5             : #include <stdio.h>
       6             : #include <string.h>
       7             : 
       8             : #include "config.h"
       9             : 
      10             : // Returns (first) IP address for Interface
      11             : // Returns null in case neither the Interface was found.
      12           2 : char* get_ip_from_interface(char* interface) {
      13           2 :   struct ifaddrs *ifaddr, *ifa;
      14           2 :   static char ip[NI_MAXHOST];
      15             : 
      16           2 :   if (getifaddrs(&ifaddr) == -1) {
      17           0 :     printf("getifaddrs problem\n");
      18           0 :     return NULL;
      19             :   }
      20             : 
      21          16 :   for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
      22          16 :     if (ifa->ifa_addr == NULL)
      23           0 :       continue;
      24             : 
      25          16 :     int status = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), ip, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
      26          16 :     if (status != 0) {
      27          10 :       continue;
      28             :     }
      29             : 
      30           6 :     if (((strcmp(ip, interface) == 0) || (strcmp(ifa->ifa_name, interface) == 0)) && (ifa->ifa_addr->sa_family==AF_INET)) {
      31           2 :       printf("Interface : <%s>\n", ifa->ifa_name);
      32           2 :       printf("Address : <%s>\n", ip);
      33           2 :       freeifaddrs(ifaddr);
      34           2 :       return ip;
      35             :     }
      36             :   }
      37             : 
      38             :   // not found
      39           0 :   freeifaddrs(ifaddr);
      40           0 :   return NULL;
      41             : }

Generated by: LCOV version 1.0