Search code examples
cpointersgetaddrinfo

C null pointer check fails in ai_next linked list


Background: I'm following Beej's Guide to Network Programming on how to listen for and accept incoming connections using C sockets.

Problem: The problem I'm having is I'm getting segmentation faults when traversing the linked list of addresses to listen on. I do check if the next struct is null, but that isn't working.

What I've Tried: Between this code and previous working examples I've written, I can tell only one difference which is that these are addresses on my own computer vs another host. I've looked at the man pages for getaddrinfo too, and I'm following the example implementation as far as I can tell.

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <errno.h>

#define MYPORT "6161"

int main() {
    struct addrinfo hints, *res;

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_family = AI_PASSIVE;

    getaddrinfo(NULL, MYPORT, &hints, &res);

    struct addrinfo *addr;
    for (addr = res; addr != NULL; addr = addr->ai_next) {
        printf("Here's one addr!");
    }

    return 0;
}

Solution

  • I found out the problem...

    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <string.h>
    #include <errno.h>
    
    #define MYPORT "6161"
    
    int main() {
        struct addrinfo hints, *res;
    
        memset(&hints, 0, sizeof hints);
        hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_family = AI_PASSIVE;
    
        getaddrinfo(NULL, MYPORT, &hints, &res);
    
        struct addrinfo *addr;
        for (addr = res; addr != NULL; addr = addr->ai_next) {
            printf("Here's one addr!");
        }
    
        return 0;
    }
    

    This is your code.

    This is the corrected code:

    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <string.h>
    #include <errno.h>
    
    #define MYPORT "6161"
    
    int main() {
        struct addrinfo hints, *res;
    
        memset(&hints, 0, sizeof hints);
        hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_flags = AI_PASSIVE;
    
        getaddrinfo(NULL, MYPORT, &hints, &res);
    
        struct addrinfo *addr;
        for (addr = res; addr != NULL; addr = addr->ai_next) {
            printf("Here's one addr!");
        }
    
        return 0;
    }
    

    You just wrote ai_family twice, thus assigning AI_PASSIVE to hints.ai_family instead of hints.ai_flags.

    I tried the corrected code and it works fine.