Path: blob/main/japanese/FreeWnn-lib/files/patch-Wnn-etc-fake-rfc2553.c
18157 views
Index: Wnn/etc/fake-rfc2553.c1===================================================================2RCS file: Wnn/etc/fake-rfc2553.c3diff -N Wnn/etc/fake-rfc2553.c4--- /dev/null 1 Jan 1970 00:00:00 -00005+++ Wnn/etc/fake-rfc2553.c 2 Jan 2009 21:09:34 -0000 1.16@@ -0,0 +1,229 @@7+/*8+ * Copyright (C) 2000-2003 Damien Miller. All rights reserved.9+ * Copyright (C) 1999 WIDE Project. All rights reserved.10+ *11+ * Redistribution and use in source and binary forms, with or without12+ * modification, are permitted provided that the following conditions13+ * are met:14+ * 1. Redistributions of source code must retain the above copyright15+ * notice, this list of conditions and the following disclaimer.16+ * 2. Redistributions in binary form must reproduce the above copyright17+ * notice, this list of conditions and the following disclaimer in the18+ * documentation and/or other materials provided with the distribution.19+ * 3. Neither the name of the project nor the names of its contributors20+ * may be used to endorse or promote products derived from this software21+ * without specific prior written permission.22+ *23+ * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND24+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE25+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE26+ * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE27+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL28+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS29+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)30+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT31+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY32+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF33+ * SUCH DAMAGE.34+ */35+36+/*37+ * Pseudo-implementation of RFC2553 name / address resolution functions38+ *39+ * But these functions are not implemented correctly. The minimum subset40+ * is implemented for ssh use only. For example, this routine assumes41+ * that ai_family is AF_INET. Don't use it for another purpose.42+ */43+44+#include <stdlib.h>45+#include <string.h>46+47+#include <netinet/in.h>48+#include <arpa/inet.h>49+50+#include "config.h"51+#include "fake-rfc2553.h"52+53+#ifndef HAVE_GETNAMEINFO54+int getnameinfo(const struct sockaddr *sa, size_t salen, char *host,55+ size_t hostlen, char *serv, size_t servlen, int flags)56+{57+ struct sockaddr_in *sin = (struct sockaddr_in *)sa;58+ struct hostent *hp;59+ char tmpserv[16];60+61+ if (serv != NULL) {62+ snprintf(tmpserv, sizeof(tmpserv), "%d", ntohs(sin->sin_port));63+ if (strlcpy(serv, tmpserv, servlen) >= servlen)64+ return (EAI_MEMORY);65+ }66+67+ if (host != NULL) {68+ if (flags & NI_NUMERICHOST) {69+ if (strlcpy(host, inet_ntoa(sin->sin_addr),70+ hostlen) >= hostlen)71+ return (EAI_MEMORY);72+ else73+ return (0);74+ } else {75+ hp = gethostbyaddr((char *)&sin->sin_addr,76+ sizeof(struct in_addr), AF_INET);77+ if (hp == NULL)78+ return (EAI_NODATA);79+80+ if (strlcpy(host, hp->h_name, hostlen) >= hostlen)81+ return (EAI_MEMORY);82+ else83+ return (0);84+ }85+ }86+ return (0);87+}88+#endif /* !HAVE_GETNAMEINFO */89+90+#ifndef HAVE_GAI_STRERROR91+#ifdef HAVE_CONST_GAI_STRERROR_PROTO92+const char *93+#else94+char *95+#endif96+gai_strerror(int err)97+{98+ switch (err) {99+ case EAI_NODATA:100+ return ("no address associated with name");101+ case EAI_MEMORY:102+ return ("memory allocation failure.");103+ case EAI_NONAME:104+ return ("nodename nor servname provided, or not known");105+ default:106+ return ("unknown/invalid error.");107+ }108+}109+#endif /* !HAVE_GAI_STRERROR */110+111+#ifndef HAVE_FREEADDRINFO112+void113+freeaddrinfo(struct addrinfo *ai)114+{115+ struct addrinfo *next;116+117+ for(; ai != NULL;) {118+ next = ai->ai_next;119+ free(ai);120+ ai = next;121+ }122+}123+#endif /* !HAVE_FREEADDRINFO */124+125+#ifndef HAVE_GETADDRINFO126+static struct127+addrinfo *malloc_ai(int port, u_long addr, const struct addrinfo *hints)128+{129+ struct addrinfo *ai;130+131+ ai = malloc(sizeof(*ai) + sizeof(struct sockaddr_in));132+ if (ai == NULL)133+ return (NULL);134+135+ memset(ai, '\0', sizeof(*ai) + sizeof(struct sockaddr_in));136+137+ ai->ai_addr = (struct sockaddr *)(ai + 1);138+ /* XXX -- ssh doesn't use sa_len */139+ ai->ai_addrlen = sizeof(struct sockaddr_in);140+ ai->ai_addr->sa_family = ai->ai_family = AF_INET;141+142+ ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;143+ ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;144+145+ /* XXX: the following is not generally correct, but does what we want */146+ if (hints->ai_socktype)147+ ai->ai_socktype = hints->ai_socktype;148+ else149+ ai->ai_socktype = SOCK_STREAM;150+151+ if (hints->ai_protocol)152+ ai->ai_protocol = hints->ai_protocol;153+154+ return (ai);155+}156+157+int158+getaddrinfo(const char *hostname, const char *servname,159+ const struct addrinfo *hints, struct addrinfo **res)160+{161+ struct hostent *hp;162+ struct servent *sp;163+ struct in_addr in;164+ int i;165+ long int port;166+ u_long addr;167+168+ port = 0;169+ if (servname != NULL) {170+ char *cp;171+172+ port = strtol(servname, &cp, 10);173+ if (port > 0 && port <= 65535 && *cp == '\0')174+ port = htons(port);175+ else if ((sp = getservbyname(servname, NULL)) != NULL)176+ port = sp->s_port;177+ else178+ port = 0;179+ }180+181+ if (hints && hints->ai_flags & AI_PASSIVE) {182+ addr = htonl(0x00000000);183+ if (hostname && inet_aton(hostname, &in) != 0)184+ addr = in.s_addr;185+ *res = malloc_ai(port, addr, hints);186+ if (*res == NULL)187+ return (EAI_MEMORY);188+ return (0);189+ }190+191+ if (!hostname) {192+ *res = malloc_ai(port, htonl(0x7f000001), hints);193+ if (*res == NULL)194+ return (EAI_MEMORY);195+ return (0);196+ }197+198+ if (inet_aton(hostname, &in)) {199+ *res = malloc_ai(port, in.s_addr, hints);200+ if (*res == NULL)201+ return (EAI_MEMORY);202+ return (0);203+ }204+205+ /* Don't try DNS if AI_NUMERICHOST is set */206+ if (hints && hints->ai_flags & AI_NUMERICHOST)207+ return (EAI_NONAME);208+209+ hp = gethostbyname(hostname);210+ if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {211+ struct addrinfo *cur, *prev;212+213+ cur = prev = *res = NULL;214+ for (i = 0; hp->h_addr_list[i]; i++) {215+ struct in_addr *in = (struct in_addr *)hp->h_addr_list[i];216+217+ cur = malloc_ai(port, in->s_addr, hints);218+ if (cur == NULL) {219+ if (*res != NULL)220+ freeaddrinfo(*res);221+ return (EAI_MEMORY);222+ }223+ if (prev)224+ prev->ai_next = cur;225+ else226+ *res = cur;227+228+ prev = cur;229+ }230+ return (0);231+ }232+233+ return (EAI_NODATA);234+}235+#endif /* !HAVE_GETADDRINFO */236237238