I have this trivial program that I keep getting a segfault trying to use event_asr_run(). I have #if 0'd working code to show my progression from getaddrinfo() to event_asr_run(). It is hopefully something trivial that I'm overlooking. Anyway I compiled like so:

cc -g -o test test.c -levent

#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include <arpa/inet.h>

#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <asr.h>
#include <event.h>

void
ev_cb(struct asr_result *ar, void *arg)
{
    if (ar->ar_addrinfo)
        freeaddrinfo(ar->ar_addrinfo);
    if (ar->ar_gai_errno == EAI_NODATA || ar->ar_gai_errno == EAI_NONAME)
        printf("nodata\n");
    else
        printf("data\n");
}

int
main(void)
{
    in_addr_t ia;
    struct addrinfo hints, *info, *p;
    int rv;
    char buf[512];
    char *host = "a.root-servers.net";
    struct sockaddr_in sa;
    struct asr_query *aq;
/*    struct asr_result *ar;
    int ans; */

    inet_pton(AF_INET, "108.61.222.55", &(sa.sin_addr));
    ia = sa.sin_addr.s_addr;

    ia = ntohl(ia);

    if (snprintf(buf, sizeof(buf), "%d.%d.%d.%d.%s.",
ia & 0xff, (ia >> 8) & 0xff, (ia >> 16) & 0xff, (ia >> 24) & 0xff, host) >= sizeof(buf))
        exit(1);

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

    aq = getaddrinfo_async(buf, NULL, &hints, NULL);
    event_asr_run(aq, ev_cb, (void *)NULL);
#if 0
    ar = calloc(1, sizeof *ar);
    if (ar == NULL)
        exit(1);
    do {
        ans = asr_run(aq, ar);
    } while (ans != 1);

    if (ar->ar_gai_errno == EAI_NODATA || ar->ar_gai_errno == EAI_NONAME)
        printf("nodata\n");
    else
        printf("data\n");
#endif
#if 0
    if ((rv = getaddrinfo(buf, NULL, &hints, &info)) != 0) {
        if (rv == EAI_NODATA || rv == EAI_NONAME)
            printf("nodata\n");
        else
            printf("failed\n");
        exit(1);
    } else {
        printf("data\n");
    }
#endif
    return 0;
}

Here is what gdb has to say about it.

laptop$ gdb ./test ./test.core
GNU gdb 6.3
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "amd64-unknown-openbsd6.2"...
Core was generated by `test'.
Program terminated with signal 11, Segmentation fault.
Loaded symbols for /home/edgar/test
Reading symbols from /usr/lib/libevent.so.4.1...done.
Loaded symbols for /usr/lib/libevent.so.4.1
Reading symbols from /usr/lib/libc.so.90.0...done.
Loaded symbols for /usr/lib/libc.so.90.0
Reading symbols from /usr/libexec/ld.so...done.
Loaded symbols for /usr/libexec/ld.so
#0 event_add (ev=0x155e476b1a00, tv=0x7f7ffffe1ab0) at /usr/src/lib/libevent/event.c:680
680             const struct eventop *evsel = base->evsel;
(gdb) bt
#0 event_add (ev=0x155e476b1a00, tv=0x7f7ffffe1ab0) at /usr/src/lib/libevent/event.c:680 #1 0x0000155e6b5fb227 in event_asr_run (async=0x155e476b1c00, cb=0x155bb72005a0 <ev_cb>, arg=0x0)
    at /usr/src/lib/libevent/event.c:1035
#2  0x0000155bb720080e in main () at test.c:54
Current language:  auto; currently minimal

Makes me think I'm doing something wrong with the callback function, but I just can't see it.


Thanks in advance.

Reply via email to