annotate fake-rfc2553.c @ 1306:34e6127ef02e

merge fixes from PuTTY import.c toint() from misc.c (revids are from hggit conversion) changeset: 4620:60a336a6c85c user: Simon Tatham <[email protected]> date: Thu Feb 25 20:26:33 2016 +0000 files: import.c description: Fix potential segfaults in reading OpenSSH's ASN.1 key format. The length coming back from ber_read_id_len might have overflowed, so treat it as potentially negative. Also, while I'm here, accumulate it inside ber_read_id_len as an unsigned, so as to avoid undefined behaviour on integer overflow, and toint() it before return. Thanks to Hanno Böck for spotting this, with the aid of AFL. (cherry picked from commit 5b7833cd474a24ec098654dcba8cb9509f3bf2c1) Conflicts: import.c (cherry-picker's note: resolving the conflict involved removing an entire section of the original commit which fixed ECDSA code not present on this branch) changeset: 4619:9c6c638d98d8 user: Simon Tatham <[email protected]> date: Sun Jul 14 10:45:54 2013 +0000 files: import.c ssh.c sshdss.c sshpubk.c sshrsa.c description: Tighten up a lot of casts from unsigned to int which are read by one of the GET_32BIT macros and then used as length fields. Missing bounds checks against zero have been added, and also I've introduced a helper function toint() which casts from unsigned to int in such a way as to avoid C undefined behaviour, since I'm not sure I trust compilers any more to do the obviously sensible thing. [originally from svn r9918] changeset: 4618:3957829f24d3 user: Simon Tatham <[email protected]> date: Mon Jul 08 22:36:04 2013 +0000 files: import.c sshdss.c sshrsa.c description: Add an assortment of extra safety checks. [originally from svn r9896] changeset: 4617:2cddee0bce12 user: Jacob Nevins <[email protected]> date: Wed Dec 07 00:24:45 2005 +0000 files: import.c description: Institutional failure to memset() things pointed at rather than pointers. Things should now be zeroed and memory not leaked. Spotted by Brant Thomsen. [originally from svn r6476] changeset: 4616:24ac78a9c71d user: Simon Tatham <[email protected]> date: Wed Feb 11 13:58:27 2004 +0000 files: import.c description: Jacob's last-minute testing found a couple of trivial bugs in import.c, and my attempts to reproduce them in cmdgen found another one there :-) [originally from svn r3847] changeset: 4615:088d39a73db0 user: Simon Tatham <[email protected]> date: Thu Jan 22 18:52:49 2004 +0000 files: import.c description: Placate some gcc warnings. [originally from svn r3761] changeset: 4614:e4288bad4d93 parent: 1758:108b8924593d user: Simon Tatham <[email protected]> date: Fri Oct 03 21:21:23 2003 +0000 files: import.c description: My ASN.1 decoder returned wrong IDs for anything above 0x1E! Good job it's never had to yet. Ahem. [originally from svn r3479]
author Matt Johnston <matt@ucc.asn.au>
date Tue, 12 Jul 2016 23:00:01 +0800
parents 9fd27bc37807
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
589
9fd27bc37807 - Update fake-rfc2553.{c,h} from OpenSSH 5.5p1
Matt Johnston <matt@ucc.asn.au>
parents: 67
diff changeset
1 /* Taken for Dropbear from OpenSSH 5.5p1 */
9fd27bc37807 - Update fake-rfc2553.{c,h} from OpenSSH 5.5p1
Matt Johnston <matt@ucc.asn.au>
parents: 67
diff changeset
2
64
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 /*
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 * Copyright (C) 2000-2003 Damien Miller. All rights reserved.
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 * Copyright (C) 1999 WIDE Project. All rights reserved.
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 *
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 * Redistribution and use in source and binary forms, with or without
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 * modification, are permitted provided that the following conditions
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 * are met:
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 * 1. Redistributions of source code must retain the above copyright
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 * notice, this list of conditions and the following disclaimer.
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 * 3. Neither the name of the project nor the names of its contributors
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 * may be used to endorse or promote products derived from this software
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 * without specific prior written permission.
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 *
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 * SUCH DAMAGE.
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 */
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 /*
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 * Pseudo-implementation of RFC2553 name / address resolution functions
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 *
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 * But these functions are not implemented correctly. The minimum subset
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 * is implemented for ssh use only. For example, this routine assumes
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 * that ai_family is AF_INET. Don't use it for another purpose.
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 */
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 #include "includes.h"
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41
589
9fd27bc37807 - Update fake-rfc2553.{c,h} from OpenSSH 5.5p1
Matt Johnston <matt@ucc.asn.au>
parents: 67
diff changeset
42 #include <stdlib.h>
9fd27bc37807 - Update fake-rfc2553.{c,h} from OpenSSH 5.5p1
Matt Johnston <matt@ucc.asn.au>
parents: 67
diff changeset
43 #include <string.h>
9fd27bc37807 - Update fake-rfc2553.{c,h} from OpenSSH 5.5p1
Matt Johnston <matt@ucc.asn.au>
parents: 67
diff changeset
44
9fd27bc37807 - Update fake-rfc2553.{c,h} from OpenSSH 5.5p1
Matt Johnston <matt@ucc.asn.au>
parents: 67
diff changeset
45 #include <netinet/in.h>
9fd27bc37807 - Update fake-rfc2553.{c,h} from OpenSSH 5.5p1
Matt Johnston <matt@ucc.asn.au>
parents: 67
diff changeset
46 #include <arpa/inet.h>
64
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 #ifndef HAVE_GETNAMEINFO
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 int getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 size_t hostlen, char *serv, size_t servlen, int flags)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 struct hostent *hp;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 char tmpserv[16];
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55
589
9fd27bc37807 - Update fake-rfc2553.{c,h} from OpenSSH 5.5p1
Matt Johnston <matt@ucc.asn.au>
parents: 67
diff changeset
56 if (sa->sa_family != AF_UNSPEC && sa->sa_family != AF_INET)
9fd27bc37807 - Update fake-rfc2553.{c,h} from OpenSSH 5.5p1
Matt Johnston <matt@ucc.asn.au>
parents: 67
diff changeset
57 return (EAI_FAMILY);
64
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 if (serv != NULL) {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 snprintf(tmpserv, sizeof(tmpserv), "%d", ntohs(sin->sin_port));
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 if (strlcpy(serv, tmpserv, servlen) >= servlen)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 return (EAI_MEMORY);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 if (host != NULL) {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 if (flags & NI_NUMERICHOST) {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 if (strlcpy(host, inet_ntoa(sin->sin_addr),
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 hostlen) >= hostlen)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 return (EAI_MEMORY);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 else
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 return (0);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 } else {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 hp = gethostbyaddr((char *)&sin->sin_addr,
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 sizeof(struct in_addr), AF_INET);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 if (hp == NULL)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 return (EAI_NODATA);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 if (strlcpy(host, hp->h_name, hostlen) >= hostlen)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 return (EAI_MEMORY);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 else
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 return (0);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 return (0);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 #endif /* !HAVE_GETNAMEINFO */
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 #ifndef HAVE_GAI_STRERROR
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 #ifdef HAVE_CONST_GAI_STRERROR_PROTO
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 const char *
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 #else
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 char *
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 #endif
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 gai_strerror(int err)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 switch (err) {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 case EAI_NODATA:
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 return ("no address associated with name");
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 case EAI_MEMORY:
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 return ("memory allocation failure.");
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 case EAI_NONAME:
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 return ("nodename nor servname provided, or not known");
589
9fd27bc37807 - Update fake-rfc2553.{c,h} from OpenSSH 5.5p1
Matt Johnston <matt@ucc.asn.au>
parents: 67
diff changeset
102 case EAI_FAMILY:
9fd27bc37807 - Update fake-rfc2553.{c,h} from OpenSSH 5.5p1
Matt Johnston <matt@ucc.asn.au>
parents: 67
diff changeset
103 return ("ai_family not supported");
64
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 default:
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 return ("unknown/invalid error.");
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 #endif /* !HAVE_GAI_STRERROR */
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 #ifndef HAVE_FREEADDRINFO
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 void
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 freeaddrinfo(struct addrinfo *ai)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 struct addrinfo *next;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 for(; ai != NULL;) {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 next = ai->ai_next;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118 free(ai);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119 ai = next;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122 #endif /* !HAVE_FREEADDRINFO */
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 #ifndef HAVE_GETADDRINFO
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125 static struct
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 addrinfo *malloc_ai(int port, u_long addr, const struct addrinfo *hints)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127 {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128 struct addrinfo *ai;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130 ai = malloc(sizeof(*ai) + sizeof(struct sockaddr_in));
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131 if (ai == NULL)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132 return (NULL);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134 memset(ai, '\0', sizeof(*ai) + sizeof(struct sockaddr_in));
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
135
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136 ai->ai_addr = (struct sockaddr *)(ai + 1);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137 /* XXX -- ssh doesn't use sa_len */
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138 ai->ai_addrlen = sizeof(struct sockaddr_in);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139 ai->ai_addr->sa_family = ai->ai_family = AF_INET;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141 ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142 ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 /* XXX: the following is not generally correct, but does what we want */
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145 if (hints->ai_socktype)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146 ai->ai_socktype = hints->ai_socktype;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 else
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148 ai->ai_socktype = SOCK_STREAM;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150 if (hints->ai_protocol)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151 ai->ai_protocol = hints->ai_protocol;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
153 return (ai);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
154 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
155
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156 int
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157 getaddrinfo(const char *hostname, const char *servname,
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
158 const struct addrinfo *hints, struct addrinfo **res)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159 {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
160 struct hostent *hp;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
161 struct servent *sp;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162 struct in_addr in;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163 int i;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164 long int port;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
165 u_long addr;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
166
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
167 port = 0;
589
9fd27bc37807 - Update fake-rfc2553.{c,h} from OpenSSH 5.5p1
Matt Johnston <matt@ucc.asn.au>
parents: 67
diff changeset
168 if (hints && hints->ai_family != AF_UNSPEC &&
9fd27bc37807 - Update fake-rfc2553.{c,h} from OpenSSH 5.5p1
Matt Johnston <matt@ucc.asn.au>
parents: 67
diff changeset
169 hints->ai_family != AF_INET)
9fd27bc37807 - Update fake-rfc2553.{c,h} from OpenSSH 5.5p1
Matt Johnston <matt@ucc.asn.au>
parents: 67
diff changeset
170 return (EAI_FAMILY);
64
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
171 if (servname != NULL) {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172 char *cp;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
173
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
174 port = strtol(servname, &cp, 10);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
175 if (port > 0 && port <= 65535 && *cp == '\0')
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
176 port = htons(port);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
177 else if ((sp = getservbyname(servname, NULL)) != NULL)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
178 port = sp->s_port;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
179 else
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
180 port = 0;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
182
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
183 if (hints && hints->ai_flags & AI_PASSIVE) {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
184 addr = htonl(0x00000000);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
185 if (hostname && inet_aton(hostname, &in) != 0)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
186 addr = in.s_addr;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
187 *res = malloc_ai(port, addr, hints);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
188 if (*res == NULL)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
189 return (EAI_MEMORY);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
190 return (0);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
191 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
192
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
193 if (!hostname) {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
194 *res = malloc_ai(port, htonl(0x7f000001), hints);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
195 if (*res == NULL)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
196 return (EAI_MEMORY);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
197 return (0);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
198 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
199
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
200 if (inet_aton(hostname, &in)) {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
201 *res = malloc_ai(port, in.s_addr, hints);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
202 if (*res == NULL)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
203 return (EAI_MEMORY);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
204 return (0);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
205 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
206
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
207 /* Don't try DNS if AI_NUMERICHOST is set */
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
208 if (hints && hints->ai_flags & AI_NUMERICHOST)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
209 return (EAI_NONAME);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
210
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
211 hp = gethostbyname(hostname);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
212 if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
213 struct addrinfo *cur, *prev;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
214
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
215 cur = prev = *res = NULL;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
216 for (i = 0; hp->h_addr_list[i]; i++) {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
217 struct in_addr *in = (struct in_addr *)hp->h_addr_list[i];
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
218
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
219 cur = malloc_ai(port, in->s_addr, hints);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
220 if (cur == NULL) {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
221 if (*res != NULL)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
222 freeaddrinfo(*res);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
223 return (EAI_MEMORY);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
224 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
225 if (prev)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
226 prev->ai_next = cur;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
227 else
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
228 *res = cur;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
229
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
230 prev = cur;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
231 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
232 return (0);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
233 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
234
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
235 return (EAI_NODATA);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
236 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
237 #endif /* !HAVE_GETADDRINFO */