[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [Libevent-users] Bind outgoing IP with bufferevents



Nick Mathewson wrote:

When are you calling it?  It should return the actual fd once
bufferevent_socket_connect() has been called -- but of course that
won't help you, since you need to bind the socket before connect is called.

I was calling it after creating the socket with '_socket_new', hoping it would mimic the socket() behavior and would have created a fd already.

I believe it should work if you do it like this:

   fd = socket(...);
   bind(fd, ...);
   evutil_make_socket_nonblocking(fd);
   bev = bufferevent_socket_new(client->base, fd, BEV_OPT_CLOSE_ON_FREE);
bufferevent_socket_connect(event, (struct sockaddr *)&sockaddr, sizeof(sockaddr));


Does that turn out okay for you?

That seems to work fine indeed.

Initially I forgot the 'evutil_make_socket_nonblocking(fd);'. Libevent clearly doesn't like this but instead of returning an error it simply crashes (segfault).

#1 0xb7ede21a in _bufferevent_run_eventcb (bufev=0x9bb9848, what=128) at bufferevent.c:206
No locals.
#2 0xb7edef8a in bufferevent_socket_connect (bev=0x9bb9848, sa=0xbfb27a40, socklen=16) at bufferevent_sock.c:382
       fd = 10
       r = <value optimized out>
       result = 0
       ownfd = 0
#3 0x08049372 in irc_client_connect_with_addr (client=0x9bb9760, addr=0x9bb99c0) at src/irc_client.c:294 sockaddr = {sin_family = 2, sin_port = 2842, sin_addr = {s_addr = 509068891}, sin_zero = "\000\000\000\000\000\000\000"} sockaddr_bind = {sin_family = 2, sin_port = 0, sin_addr = {s_addr = 1951909467}, sin_zero = "\000\000\000\000\000\000\000"}
       event = (struct bufferevent *) 0x9bb9848
       fd = 10
..

Libevent seems to want to run the 'event callback', which I have not yet registered. Registering the callbacks prior to calling 'bufferevent_socket_connect' stops it from crashing. Not sure if this is a bug?

My problems have been solved so far, it works fine when I add the evutil_make_socket_nonblocking(fd).

Thanks!


***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.