summaryrefslogtreecommitdiff
path: root/inputmethod/skkfep/patches/patch-bb
blob: 4a433fa38eae78d65ec76ebd3e072c4225d96c24 (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
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
138
139
140
141
142
143
144
145
146
$NetBSD: patch-bb,v 1.1.1.1 2002/05/31 13:00:04 seb Exp $

--- connserv.c.orig	Tue Jul  7 00:31:16 1998
+++ connserv.c	Tue Nov  7 20:58:57 2000
@@ -14,6 +14,10 @@
 #include <netinet/in.h>
 #include <netdb.h>
 
+#ifdef PF_INET6
+# define HAVE_GETADDRINFO
+#endif
+
 #define SKK_PORT_NUMBER	1178
 #define SKK_SERVICENAME	"skkserv"
 
@@ -36,50 +40,112 @@
 	int sock;
 	int i;
 	unsigned short port;
+#ifdef HAVE_GETADDRINFO
+	struct addrinfo aihint, *ai0, *ai;
+	int error;
+#else
 	struct sockaddr_in hostaddr;
 	struct hostent *entry;
 	struct servent *serv;
 	struct protoent *proto;
-	int a1,a2,a3,a4;
+#endif
 	char *hostname;
-
-	serv = getservbyname(SKK_SERVICENAME,"tcp");
-	fillzero((char*)&hostaddr,sizeof(struct sockaddr_in));
-	if ((proto = getprotobyname("tcp")) == NULL) {
-		return -1;
-	}
-
-	if ((sock = socket(AF_INET,SOCK_STREAM,proto->p_proto)) < 0) {
-		return -1;
+#ifdef SKK_CONF		/* use skk.conf */
+	FILE *conffp;
+	char line[128];
+	char hostbuf[128];
+	char servbuf[128];
+	char *confhost = NULL;
+	char *confport = SKK_SERVICENAME;
+
+	if ((conffp = fopen(SKK_CONF, "r")) != NULL) {
+		char *p, *data;
+
+		while (fgets(line, sizeof line, conffp) != NULL) {
+			if ((p = strchr(line, '#')) != NULL)
+				*p = '\0';
+			if ((p = strtok(line, ": \t\n")) == NULL)
+				continue;
+			if ((data = strtok((char *) NULL, " \t\n")) == NULL)
+				continue;
+			if (!strcmp(p, "skkserv_host")) {
+				strcpy(hostbuf, data);
+				confhost = hostbuf;
+			} else if (!strcmp(p, "skkserv_port")) {
+				strcpy(servbuf, data);
+				confport = servbuf;
+			}
+		}
 	}
+# undef SKK_SERVICENAME
+# define SKK_SERVICENAME confport
+#endif
 
 	if (SKKServerHost)
 		hostname = SKKServerHost;
 	else if ((hostname = getenv("SKKSERVER")) == NULL) {
+#ifdef SKK_CONF
+		if ((hostname = confhost) == NULL)
+#endif
 #ifdef SKK_SERVER_HOST
 		hostname = SKK_SERVER_HOST;
 #else
 		return -1;
 #endif
 	}
-	if ('0' <= *hostname && *hostname <= '9') {
-		if (sscanf(hostname,"%d.%d.%d.%d",&a1,&a2,&a3,&a4) != 4) {
-			return -1;
-		}
-		a1 = (a1<<24)|(a2<<16)|(a3<<8)|a4;
-		hostaddr.sin_addr.s_addr = htonl(a1);
+
+#ifdef HAVE_GETADDRINFO
+	fillzero((char*)&aihint, sizeof aihint);
+	aihint.ai_family = PF_UNSPEC;
+	aihint.ai_socktype = SOCK_STREAM;
+	aihint.ai_flags = AI_CANONNAME;
+	error = getaddrinfo(hostname, SKK_SERVICENAME, &aihint, &ai0);
+	if (error) {
+		printf("%s: %s\r\n", gai_strerror(error), hostname);
+		return -1;
+	}
+
+	sock = -1;
+	for (ai = ai0; ai; ai = ai->ai_next) {
+		sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+		if (sock >= 0 &&
+		    connect(sock, ai->ai_addr, ai->ai_addrlen) == 0)
+			break;
+		close(sock);
+		sock = -1;
+	}
+	if (sock < 0) {
+		perror(hostname);
+		return -1;
+	}
+#else
+	serv = getservbyname(SKK_SERVICENAME,"tcp");
+	fillzero((char*)&hostaddr,sizeof(struct sockaddr_in));
+	if ((proto = getprotobyname("tcp")) == NULL) {
+		return -1;
 	}
-	else {
+
+	if ((sock = socket(AF_INET,SOCK_STREAM,proto->p_proto)) < 0) {
+		return -1;
+	}
+
+	if ((hostaddr.sin_addr.s_addr = inet_addr(hostname)) == INADDR_NONE) {
 		if ((entry = gethostbyname(hostname)) == NULL) {
 			return -1;
 		}
 		bincopy(entry->h_addr, &hostaddr.sin_addr, entry->h_length);
 	}
         hostaddr.sin_family = AF_INET;
-	hostaddr.sin_port = serv ? serv->s_port : htons(SKK_PORT_NUMBER);
+	hostaddr.sin_port =
+		serv ? serv->s_port :
+#ifdef SKK_CONF
+		(i = atoi(confport)) > 0 ? htons(i) :
+#endif
+		htons(SKK_PORT_NUMBER);
 	if (connect(sock,(struct sockaddr *)&hostaddr,sizeof(struct sockaddr_in)) < 0) {
 		return -1;
 	}
+#endif
 	printf("SKKSERVER=%s\r\n",hostname);
 	skkservsock = sock;
 	rserv = fdopen(sock,"r");