@@ -2152,6 +2152,15 @@ - (BOOL)connectWithAddress4:(NSData *)address4 address6:(NSData *)address6 error
2152
2152
{
2153
2153
LogVerbose (@" Binding socket..." );
2154
2154
2155
+ if ([[self class ] portFromAddress: connectInterface] > 0 )
2156
+ {
2157
+ // Since we're going to be binding to a specific port,
2158
+ // we should turn on reuseaddr to allow us to override sockets in time_wait.
2159
+
2160
+ int reuseOn = 1 ;
2161
+ setsockopt (socketFD, SOL_SOCKET, SO_REUSEADDR, &reuseOn, sizeof (reuseOn));
2162
+ }
2163
+
2155
2164
struct sockaddr *interfaceAddr = (struct sockaddr *)[connectInterface bytes ];
2156
2165
2157
2166
int result = bind (socketFD, interfaceAddr, (socklen_t )[connectInterface length ]);
@@ -3136,6 +3145,10 @@ - (BOOL)isIPv6
3136
3145
/* *
3137
3146
* Finds the address of an interface description.
3138
3147
* An inteface description may be an interface name (en0, en1, lo0) or corresponding IP (192.168.4.34).
3148
+ *
3149
+ * The interface description may optionally contain a port number at the end, separated by a colon.
3150
+ * If a non-zeor port parameter is provided, any port number in the interface description is ignored.
3151
+ *
3139
3152
* The returned value is a 'struct sockaddr' wrapped in an NSData object.
3140
3153
**/
3141
3154
- (void )getInterfaceAddress4 : (NSData **)interfaceAddr4Ptr
@@ -3146,7 +3159,28 @@ - (void)getInterfaceAddress4:(NSData **)interfaceAddr4Ptr
3146
3159
NSData *addr4 = nil ;
3147
3160
NSData *addr6 = nil ;
3148
3161
3149
- if (interfaceDescription == nil )
3162
+ NSString *interface = nil ;
3163
+
3164
+ NSArray *components = [interfaceDescription componentsSeparatedByString: @" :" ];
3165
+ if ([components count ] > 0 )
3166
+ {
3167
+ NSString *temp = [components objectAtIndex: 0 ];
3168
+ if ([temp length ] > 0 )
3169
+ {
3170
+ interface = temp;
3171
+ }
3172
+ }
3173
+ if ([components count ] > 1 && port == 0 )
3174
+ {
3175
+ long portL = strtol ([[components objectAtIndex: 1 ] UTF8String ], NULL , 10 );
3176
+
3177
+ if (portL > 0 && portL <= UINT16_MAX)
3178
+ {
3179
+ port = (UInt16)portL;
3180
+ }
3181
+ }
3182
+
3183
+ if (interface == nil )
3150
3184
{
3151
3185
// ANY address
3152
3186
@@ -3169,7 +3203,7 @@ - (void)getInterfaceAddress4:(NSData **)interfaceAddr4Ptr
3169
3203
addr4 = [NSData dataWithBytes: &nativeAddr4 length: sizeof (nativeAddr4)];
3170
3204
addr6 = [NSData dataWithBytes: &nativeAddr6 length: sizeof (nativeAddr6)];
3171
3205
}
3172
- else if ([interfaceDescription isEqualToString: @" localhost" ] || [interfaceDescription isEqualToString: @" loopback" ])
3206
+ else if ([interface isEqualToString: @" localhost" ] || [interface isEqualToString: @" loopback" ])
3173
3207
{
3174
3208
// LOOPBACK address
3175
3209
@@ -3194,7 +3228,7 @@ - (void)getInterfaceAddress4:(NSData **)interfaceAddr4Ptr
3194
3228
}
3195
3229
else
3196
3230
{
3197
- const char *interface = [interfaceDescription UTF8String ];
3231
+ const char *iface = [interface UTF8String ];
3198
3232
3199
3233
struct ifaddrs *addrs;
3200
3234
const struct ifaddrs *cursor;
@@ -3210,7 +3244,7 @@ - (void)getInterfaceAddress4:(NSData **)interfaceAddr4Ptr
3210
3244
3211
3245
struct sockaddr_in *addr = (struct sockaddr_in *)cursor->ifa_addr ;
3212
3246
3213
- if (strcmp (cursor->ifa_name , interface ) == 0 )
3247
+ if (strcmp (cursor->ifa_name , iface ) == 0 )
3214
3248
{
3215
3249
// Name match
3216
3250
@@ -3226,7 +3260,7 @@ - (void)getInterfaceAddress4:(NSData **)interfaceAddr4Ptr
3226
3260
const char *conversion;
3227
3261
conversion = inet_ntop (AF_INET, &addr->sin_addr , ip, sizeof (ip));
3228
3262
3229
- if ((conversion != NULL ) && (strcmp (ip, interface ) == 0 ))
3263
+ if ((conversion != NULL ) && (strcmp (ip, iface ) == 0 ))
3230
3264
{
3231
3265
// IP match
3232
3266
@@ -3243,7 +3277,7 @@ - (void)getInterfaceAddress4:(NSData **)interfaceAddr4Ptr
3243
3277
3244
3278
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)cursor->ifa_addr ;
3245
3279
3246
- if (strcmp (cursor->ifa_name , interface ) == 0 )
3280
+ if (strcmp (cursor->ifa_name , iface ) == 0 )
3247
3281
{
3248
3282
// Name match
3249
3283
@@ -3259,7 +3293,7 @@ - (void)getInterfaceAddress4:(NSData **)interfaceAddr4Ptr
3259
3293
const char *conversion;
3260
3294
conversion = inet_ntop (AF_INET6, &addr->sin6_addr , ip, sizeof (ip));
3261
3295
3262
- if ((conversion != NULL ) && (strcmp (ip, interface ) == 0 ))
3296
+ if ((conversion != NULL ) && (strcmp (ip, iface ) == 0 ))
3263
3297
{
3264
3298
// IP match
3265
3299
0 commit comments