summaryrefslogtreecommitdiff
path: root/chat/i2cb/patches/patch-af
blob: 548ce3ad17d7977d22fa7757626ce1b1ffb98383 (plain)
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
$NetBSD: patch-af,v 1.2 2004/04/13 21:19:48 hubertf Exp $

--- src/contoport.c.orig	Tue Apr 13 22:29:28 2004
+++ src/contoport.c
@@ -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;