Support outgoing connections bound to a specific local address.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@18953 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-Macdonald 2004-03-29 14:53:37 +00:00
parent a99d3f85db
commit 8bb1aeb737
7 changed files with 108 additions and 17 deletions

View file

@ -685,6 +685,8 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
static BOOL beenHere = NO;
SOCKET net;
struct sockaddr_in sin;
struct sockaddr_in lsin;
NSString *lhost = nil;
NSString *shost = nil;
NSString *sport = nil;
@ -720,6 +722,30 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
return nil;
}
if ([p hasPrefix: @"bind-"] == YES)
{
NSRange r;
lhost = [p substringFromIndex: 5];
r = [lhost rangeOfString: @":"];
if (r.length > 0)
{
p = [lhost substringFromIndex: NSMaxRange(r)];
lhost = [lhost substringToIndex: r.location];
}
else
{
p = nil;
}
if (getAddr(lhost, p, @"tcp", &lsin) == NO)
{
NSLog(@"bad bind address specification");
RELEASE(self);
return nil;
}
p = @"tcp";
}
/**
* A protocol fo the form 'socks-...' controls socks operation,
* overriding defaults and environment variables.<br />
@ -795,6 +821,21 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
RELEASE(self);
return nil;
}
if (lhost != nil)
{
if (bind(net, (struct sockaddr *)&lsin, sizeof(lsin)) == SOCKET_ERROR)
{
NSLog(@"unable to bind to port %s:%d - %s", inet_ntoa(lsin.sin_addr),
GSSwapBigI16ToHost(sin.sin_port), GSLastErrorStr(errno));
#if defined(__MINGW__)
(void) closesocket(net);
#else
(void) close(net);
#endif
RELEASE(self);
return nil;
}
}
#if defined(__MINGW__)
self = [self initWithNativeHandle: (void*)net closeOnDealloc: YES];