Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Writing a network checker in C, but error handling troubles me

I am still pretty new to pointers so need help with this.
I am writing program that will list network adapter.

This is the whole code;

int main() {
    struct ifaddrs *addresses;
    
    if (getifaddrs(&addresses == -1)) {
        printf("getifaddrs call failed!\n");
        return -1;
    }

    struct ifaddrs *address = addresses;
    while (address) {
        int family = address->ifa_addr->sa_family;
        if (family == AF_INET || family == AF_INET6) {
            printf("%s\t", address->ifa_name);
            printf("%s\t", family == AF_INET ? "IPv4" : "IPv6");

            char ap[100];
            const int family_size = family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
            getnameinfo(address->ifa_addr, family_size, ap, sizeof(ap), 0, 0, NI_NUMERICHOST);
            printf("\t%s\n", ap);
        }
        address = address->ifa_next;
    }

    freeifaddrs(addresses);
    
    return 0;
}    

My issue is with this part:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

struct ifaddrs *addresses;
    
    if (getifaddrs(&addresses == -1)) {
        printf("getifaddrs call failed!\n");
        return -1;
    }

when compiling i get this error:

integer without a cast [-Wint-conversion]
   11 |     if (getifaddrs(&addresses == -1)) {
      |                    ~~~~~~~~~~~^~~~~
      |                               |
      |                               int
I fil inkluderet fra list_adapter.c:5:
/usr/include/ifaddrs.h:66:41: bemærk: expected »struct ifaddrs **« but argument is of type »int«
   66 | extern int getifaddrs (struct ifaddrs **__ifap) __THROW;
      |                        ~~~~~~~~~~~~~~~~~^~~~~~

I have tried to cast the address pointer to both int and char before using it, but it keeps giving the same error and if i outcomment the whole if-else that contains the error handling, then i get thrown a segmetation fault.

Also a void pointer didn’t help.

Thanks

>Solution :

You have your closing parenthesis in the wrong place in this line:

if (getifaddrs(&addresses == -1)) {

It should be

if (getifaddrs(&addresses) == -1) {

The segmentation fault you get when you comment out the if else block is probably because you are trying to use the uninitialised pointer.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading