Question

I'm trying to learn libevent/libev (in this case its libev) from the following link: http://www.ibm.com/developerworks/aix/library/au-libev/

I'm wondering how to find out what the remote ip address is so I can build my own DHT.

I'm guessing it has something to do with the incoming structure but I just don't know.

Here is the callback code that the incoming variable is used, for the rest of the code click the link above.

void buf_read_callback(struct bufferevent *incoming,
                       void *arg)
{
  struct evbuffer *evreturn;
  char *req;

  req = evbuffer_readline(incoming->input);
  if (req == NULL)
    return;

  evreturn = evbuffer_new();
  evbuffer_add_printf(evreturn,"You said %s\n",req);
  bufferevent_write_buffer(incoming,evreturn);
  evbuffer_free(evreturn);
  free(req);
}

Basically the example code provides a "server" driven by libev events, you can connect to the server by telneting the server ip:port pair and it will just echo back everything you say to it -- Notice the line that says "You said %s\n"

Was it helpful?

Solution

I would store the remote address when calling accept() and then associate that with the new connection instead. However, you could also just use getpeername() in your callback. There's nothing special that libev will do regarding this, just take the FD number and do network programming like you always do it in C.

OTHER TIPS

There is example:

struct sockaddr_in ip_adr_get;
int ip_adr_len;

ip_adr_len = sizeof(ip_adr_get);
getpeername(fd, &ip_adr_get, &ip_adr_len);

printf("IP address is: %s\n", inet_ntoa(ip_adr_get.sin_addr));
printf("Port is: %d\n", (int) ntohs(ip_adr_get.sin_port));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top