cURL / Mailing Lists / curl-library / Single Mail

curl-library

Issue with SO_REUSEADDR in bindlocal

From: Rhys Feingold <rhyma12_at_yahoo.com>
Date: Sat, 19 Mar 2005 01:09:09 -0800 (PST)

Hi,

I need the ability to set my client port number to a
fixed value, and could not find any way to do this in
cURL. So I changed bindlocal in lib/connect.c to
extract a port number from data->set.device (in
host!port format) and then set sin_port to this value.
It seems to work.

However, then I run up against the bind failure during
TIME_WAIT problem, and so I call setsockopt with
SO_REUSEADDR just before the call to bind() in
bindlocal. This does not seem to work. Any pointers ?

- Platform is Solaris 2.8.
- Options: --disable-shared --enable-debug
--disable-ipv6
- I've tried without the last two options, same
results.
- CURLOPT_INTERFACE set to XX.XX.XX.XX!40000 with
curl_easy_setopt().

Error messages
--------------

First run :

* About to connect() to <remote_host_name> port 80
* Trying <remote_host_ip>... *
* We bind local end to XX.XX.XX.XX (port 40000)
* SO_REUSEADDR set for port 40000
* connected
* Connected to <remote_host_name> (<remote_host_ip>)
port 80
> POST /cgi-bin/simulator.cgi HTTP/1.1

Second run with same settings:

* About to connect() to <remote_host_name> port 80
* Trying <remote_host_ip>... *
* We bind local end to XX.XX.XX.XX (port 40000)
* SO_REUSEADDR set for port 40000
* Failed to connect to <remote_host_ip>: Address
already in use
* Error 0
* couldn't connect to host
* Closing connection #0
CURL_ERROR: Failed to connect to <remote_host_ip>:
Address already in use

At this point, netstat -an | grep 40000 gives

XX.XX.XX.XX.40000 <remote_host_ip>.80 8760 0
24820 0 TIME_WAIT

The main change is as follows (This is just before the
bind() call in bindlocal() in connect.c):

< if (myport >= 1 && myport <= 0xffff) {
< int on = 1;
< struct sockaddr_in *myaddr = (struct sockaddr_in
*)addr->ai_addr;
< myaddr->sin_port = htons(myport);
<
< if
(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on))
< 0) {
< infof(data, "SO_REUSEADDR could not be set
(port %d)\n", myport);
< } else {
< infof(data, "SO_REUSEADDR set for port %d\n",
myport);
< }
< }

Thanks for your time/help.

Regards,
Rhys F.

                
__________________________________
Do you Yahoo!?
Yahoo! Small Business - Try our new resources site!
http://smallbusiness.yahoo.com/resources/
Received on 2005-03-19