Package: dico / 2.2-8

client-ipv6.diff Patch series | download
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
Description: Add IPv6 support for Dico client
Forwarded: yes
Author: أحمد المحمودي (Ahmed El-Mahmoudy) <aelmahmoudy@sabily.org>
Bug-Debian: http://bugs.debian.org/582711
diff --git a/dico/cmdline.opt b/dico/cmdline.opt
index 3bd2cb3..8d4eb20 100644
--- a/dico/cmdline.opt
+++ b/dico/cmdline.opt
@@ -51,7 +51,7 @@ END
 OPTION(source,,ADDR,
        [<Set a source address for TCP connections.>])
 BEGIN
-  source_addr = get_ipaddr(optarg);
+  source_addr = optarg;
   if (source_addr == 0)
     dico_die(1, 0, L_ERR, _("%s: Invalid IP or unknown host name"),
              optarg);
diff --git a/dico/connect.c b/dico/connect.c
index 738b28f..dfcdfb1 100644
--- a/dico/connect.c
+++ b/dico/connect.c
@@ -272,44 +272,57 @@ dict_transcript(struct dict_connection *conn, int state)
 int
 dict_connect(struct dict_connection **pconn, dico_url_t url)
 {
-    struct sockaddr_in s;
+    struct addrinfo hints, *s;
+    char urlport[6];
     int fd;
-    IPADDR ip;
     dico_stream_t str;
     struct dict_connection *conn;
     
     XDICO_DEBUG_F2(1, _("Connecting to %s:%d\n"), url->host,
 		   url->port ? url->port : DICO_DICT_PORT);
-    fd = socket(PF_INET, SOCK_STREAM, 0);
-    if (fd == -1) {
-	dico_log(L_ERR, errno,
-		 _("cannot create dict socket"));
-	return 1;
-    }
 
-    s.sin_family = AF_INET;
-    s.sin_addr.s_addr = htonl(source_addr);
-    s.sin_port = 0;
-    if (bind(fd, (struct sockaddr*) &s, sizeof(s)) < 0) {
-	dico_log(L_ERR, errno,
-		 _("cannot bind AUTH socket"));
+    memset(&hints, 0, sizeof hints);
+    hints.ai_family = AF_UNSPEC;  // use IPv4 or IPv6, whichever, TODO: user configurable?
+    hints.ai_socktype = SOCK_STREAM;
+
+    if(source_addr != NULL) {
+      getaddrinfo(source_addr, "0", &hints, &s);
+      fd = socket(s->ai_family, s->ai_socktype, s->ai_protocol);
+      if (fd == -1) {
+		dico_log(L_ERR, errno,
+			 _("cannot create dict socket"));
+		return 1;
+      }
+      if (bind(fd, s->ai_addr, s->ai_addrlen) < 0) {
+		dico_log(L_ERR, errno,
+			 _("cannot bind AUTH socket"));
+      }
+      freeaddrinfo(s);
     }
 
-    ip = get_ipaddr(url->host);
-    if (ip == 0) {
+    sprintf(urlport, "%d", (url->port ? url->port : DICO_DICT_PORT));
+    
+    if (getaddrinfo(url->host, urlport, &hints, &s) != 0) {
 	dico_log(L_ERR, 0, _("%s: Invalid IP or unknown host name"),
 		 url->host);
 	return 1;
     }
-    s.sin_addr.s_addr = htonl(ip);
-    s.sin_port = htons(url->port ? url->port : DICO_DICT_PORT);
-    if (connect(fd, (struct sockaddr*) &s, sizeof(s)) == -1) {
+    if(source_addr == NULL) {
+      fd = socket(s->ai_family, s->ai_socktype, s->ai_protocol);
+      if (fd == -1) {
+		dico_log(L_ERR, errno,
+			 _("cannot create dict socket"));
+		return 1;
+      }
+    }
+    if (connect(fd,  s->ai_addr, s->ai_addrlen) == -1) {
 	dico_log(L_ERR, errno,
 		 _("cannot connect to DICT server %s:%d"),
 		 url->host, url->port ? url->port : DICO_DICT_PORT);
 	close(fd);
 	return 1;
     }
+    freeaddrinfo(s);
 
     if ((str = dico_fd_io_stream_create(fd, fd)) == NULL) {
 	dico_log(L_ERR, errno,
diff --git a/dico/dico-priv.h b/dico/dico-priv.h
index 046ad3f..8c711aa 100644
--- a/dico/dico-priv.h
+++ b/dico/dico-priv.h
@@ -140,7 +140,7 @@ extern struct auth_cred default_cred;
 extern char *client;
 extern enum dico_client_mode mode;
 extern int transcript;
-extern IPADDR source_addr;
+extern char *source_addr;
 extern int noauth_option;
 extern unsigned levenshtein_threshold;
 extern char *autologin_file;
diff --git a/dico/dico.c b/dico/dico.c
index 86491ce..4e9fd77 100644
--- a/dico/dico.c
+++ b/dico/dico.c
@@ -21,7 +21,7 @@ struct auth_cred default_cred;
 char *client = DICO_CLIENT_ID;
 enum dico_client_mode mode = mode_define;
 int transcript;
-IPADDR source_addr = INADDR_ANY;
+char *source_addr = NULL;
 int noauth_option;
 unsigned levenshtein_threshold;
 char *autologin_file;
diff --git a/dico/cmdline.c b/dico/cmdline.c
index a0c9ca2..16218b7 100644
--- a/dico/cmdline.c
+++ b/dico/cmdline.c
@@ -1570,7 +1570,7 @@ get_options (int argc, char *argv[], int *index)
 	  {
 #line 53
 
-  source_addr = get_ipaddr(optarg);
+  source_addr = optarg;
   if (source_addr == 0)
     dico_die(1, 0, L_ERR, _("%s: Invalid IP or unknown host name"),
              optarg);