简体   繁体   中英

C - addrinfo - pointers segmentation fault

C - addrinfo - pointers segmentation fault

I'm getting a segmentation fault; I made a weird fix but I'm still worried because I can't figure out what the problem was. Any insight would be amazing! Working "fix" in the last code block!

segmentation fault when trying to bind using target_addrinfo:

struct addrinfo *my_server_res;
struct addrinfo *their_server_res;

int main(int argc, char **argv) {
//........
    set_addrinfo();
    handle_binding();
}

void set_addrinfo() {
    int status;
    struct addrinfo hints;
    struct addrinfo *target_addrinfo; // fix (1/3): **target_addrinfo
    char ipstr[INET6_ADDRSTRLEN];

    // THIS IS TRUE, takes my_server_res
    // fix(2/3): (l ? &my_server_res : &their_server_res)
    target_addrinfo = (l ? my_server_res : their_server_res); 

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;
    char *node;
    node = (l ? "localhost" : host_name);

    // fix (3/3) 3rd argument: target_addrinfo
    if ((status = getaddrinfo(node, port, &hints, &target_addrinfo)) != 0) {
        fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
        exit(1);
    }

}

void handle_binding() {
    struct addrinfo *target_addrinfo;
    // THIS IS TRUE, takes my_server_res
    target_addrinfo = (l ? my_server_res : their_server_res);
    int b_res = bind(sockfd, target_addrinfo->ai_addr, target_addrinfo->ai_addrlen);
    if (b_res == -1) {
        perror("__Bind");
        exit(1);
    }
}

stack trace: problem when trying to use my_server_res again?:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000401e16 in handle_binding () at ncTh.c:385
385     int b_res = bind(sockfd, target_addrinfo->ai_addr, target_addrinfo->ai_addrlen);
(gdb) backtrace
#0  0x0000000000401e16 in handle_binding () at ncTh.c:385
#1  0x0000000000401cad in setup_socket () at ncTh.c:330
#2  0x0000000000401ad0 in main (argc=6, argv=0x7fffffffdea8) at ncTh.c:266
(gdb) frame 0
#0  0x0000000000401e16 in handle_binding () at ncTh.c:385
385     int b_res = bind(sockfd, target_addrinfo->ai_addr, target_addrinfo->ai_addrlen);
(gdb) 

working "fix", changes to set_addrinfo():

struct addrinfo **target_addrinfo; // was *target_addrinfo
char ipstr[INET6_ADDRSTRLEN];

target_addrinfo = (l ? &my_server_res : &their_server_res); // was my_server_res : their_server_res

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

char *node;
node = (l ? "localhost" : host_name);
                               // 3rd argument was &target_addrinfo :
if ((status = getaddrinfo(node, port, &hints, target_addrinfo)) != 0) {
    fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
    exit(1);
}

In the original code:

struct addrinfo *target_addrinfo;
target_addrinfo = (l ? my_server_res : their_server_res);

This copies the value of the pointer my_server_res into target_addrinfo .

getaddrinfo(node, port, &hints, &target_addrinfo)

This passes a pointer to target_addrinfo (a pointer to a pointer) into the function getaddrinfo which then assigns a new value to it, by doing the equivalent of *&target_addrinfo = new_ptr . This new value is a pointer to the information getaddrinfo is retrieving for you.

All this has no effect on my_server_res which remains with its original value, likely NULL.

In the new code:

struct addrinfo **target_addrinfo;
target_addrinfo = (l ? &my_server_res : &their_server_res);

This copies a pointer to my_server_res into target_addrinfo (which is now a pointer to a pointer).

getaddrinfo(node, port, &hints, target_addrinfo)

This passes the value of target_addrinfo (which is currently a pointer to my_server_res ) into the function getaddrinfo which then assigns a new value to it, by doing the equivalent of *target_addrinfo = new_ptr . my_server_res now contains a pointer to the information getaddrinfo is retrieving for you.

you will probably fall in dereferencing of target_addrinfo problem, i posted this code. (better late then never :))

struct addrinfo *my_server_res;
struct addrinfo *their_server_res;

struct addrinfo **target_addrinfo; //this should be global 

int main(int argc, char **argv) {
//........
    set_addrinfo();
    handle_binding();
}

void set_addrinfo() {
    int status;
    struct addrinfo hints;

    char ipstr[INET6_ADDRSTRLEN];

    // THIS IS TRUE, takes my_server_res
    target_addrinfo = (l ? &my_server_res : &their_server_res); //set it once here

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;
    char *node;
    node = (l ? "localhost" : host_name);
    if ((status = getaddrinfo(node, port, &hints, &target_addrinfo)) != 0) {
        fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
        exit(1);
    }

}

void handle_binding() {
    //target_addrinfo should be initialized in set_addrinfo()
    int b_res = bind(sockfd, 
        (*target_addrinfo)->ai_addr,    // don't forget to dereference target_addrinfo, here
        (*target_addrinfo)->ai_addrlen); // and here

    if (b_res == -1) {
        perror("__Bind");
        exit(1);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM