1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
$NetBSD: patch-af,v 1.3 2009/06/05 09:36:15 hasso Exp $
--- src/contoport.c.orig 2000-03-16 00:13:28 +0200
+++ src/contoport.c 2009-06-05 11:30:39 +0300
@@ -6,7 +6,7 @@
#include <sys/types.h>
#include <fcntl.h>
#include <sys/socket.h>
-#include <strings.h>
+#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netdb.h>
@@ -14,14 +14,15 @@
#include "icb.h"
#include "externs.h"
-int connecttoport (char *host_name, int port_number);
+int connecttoport (char *host_name, int port_number, char *lhost_name);
int
-connecttoport (char *host_name, int port_number)
+connecttoport (char *host_name, int port_number, char *lhost_name)
{
struct addrinfo hints, *res, *a;
+ struct addrinfo lhints, *lres, *la;
char p[10];
- int err, s;
+ int err, s=-1;
snprintf(p, 9, "%d", port_number);
@@ -36,12 +37,43 @@ connecttoport (char *host_name, int port
perror(gai_strerror(err));
return(-1);
}
+
+ if (lhost_name != NULL) {
+ memset(&lhints, 0, sizeof(lhints));
+ lhints.ai_socktype = SOCK_STREAM;
+ lhints.ai_family = PF_UNSPEC;
+ lhints.ai_flags = AI_PASSIVE;
+
+ err = getaddrinfo(lhost_name, p, &lhints, &lres);
+ if (err) {
+ perror(gai_strerror(err));
+ return(-1);
+ }
+ }
+
a = res;
while (a) {
if ((s = socket(a->ai_family, a->ai_socktype, a->ai_protocol)) < 0) {
a = a->ai_next;
continue;
}
+
+ if (lhost_name != NULL) {
+ err = -1;
+ for(la = lres; la; la=la->ai_next) {
+ if (bind(s, la->ai_addr, la->ai_addrlen) == 0) {
+ /* bound locally! */
+ err = 0;
+ break;
+ }
+ }
+ freeaddrinfo(lres);
+ if (err < 0) {
+ perror(gai_strerror(err));
+ return(-1);
+ }
+ }
+
if (connect(s, a->ai_addr, a->ai_addrlen) < 0) {
close(s);
a = a->ai_next;
|