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

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



On Wed, Dec 23, 2009 at 01:03:56PM +0100, Bas Verhoeven wrote:
> Hello,
> 
> I want to bind an outgoing IP to a 'bufferevent socket' with libevent 
> 2.0.3-alpha, how would I do this?  I'm currently using the following 
> (trimmed down) snippet of code to connect to a remote host:
> 
> event = bufferevent_socket_new(client->base, -1, BEV_OPT_CLOSE_ON_FREE);
> bufferevent_socket_connect(event, (struct sockaddr *)&sockaddr, 
> sizeof(sockaddr));
> 
> Normally I'd just call bind() on the fd. But this time libevent creates 
> the socket for me.
> 
> Calling bufferevent_getfd(event) returns -1, which seems to be the same 
> FD that was passed to _socket_new, but isn't usable at all.

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.

> Am I missing some function that allows me to do this? Should I create a 
> socket manually, bind it to the outgoing IP and then feed it to 
> bufferevent, if so what is the best way to do this?

Exactly.

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?

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