1/*
2 * Mar  8, 2000 by Hajimu UMEMOTO <ume@mahoroba.org>
3 * $Id: getnameinfo.c,v 1.3 2004/07/07 22:48:35 snsimon Exp $
4 *
5 * This module is besed on ssh-1.2.27-IPv6-1.5 written by
6 * KIKUCHI Takahiro <kick@kyoto.wide.ad.jp>
7 */
8/*
9 * Copyright (c) 1998-2003 Carnegie Mellon University.  All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 *
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in
20 *    the documentation and/or other materials provided with the
21 *    distribution.
22 *
23 * 3. The name "Carnegie Mellon University" must not be used to
24 *    endorse or promote products derived from this software without
25 *    prior written permission. For permission or any other legal
26 *    details, please contact
27 *      Office of Technology Transfer
28 *      Carnegie Mellon University
29 *      5000 Forbes Avenue
30 *      Pittsburgh, PA  15213-3890
31 *      (412) 268-4387, fax: (412) 268-7395
32 *      tech-transfer@andrew.cmu.edu
33 *
34 * 4. Redistributions of any form whatsoever must retain the following
35 *    acknowledgment:
36 *    "This product includes software developed by Computing Services
37 *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
38 *
39 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
40 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
41 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
42 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
43 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
44 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
45 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
46 */
47/*
48 * fake library for ssh
49 *
50 * This file includes getnameinfo().
51 * These funtions are defined in rfc2133.
52 *
53 * But these functions are not implemented correctly. The minimum subset
54 * is implemented for ssh use only. For exapmle, this routine assumes
55 * that ai_family is AF_INET. Don't use it for another purpose.
56 *
57 * In the case not using 'configure --enable-ipv6', this getnameinfo.c
58 * will be used if you have broken getnameinfo or no getnameinfo.
59 */
60
61#include "config.h"
62#ifndef WIN32
63# include <arpa/inet.h>
64#endif /* WIN32 */
65#include <stdio.h>
66#include <string.h>
67
68int
69getnameinfo(const struct sockaddr *sa, socklen_t salen __attribute__((unused)),
70	    char *host, size_t hostlen, char *serv, size_t servlen, int flags)
71{
72    struct sockaddr_in *sin = (struct sockaddr_in *)sa;
73    struct hostent *hp;
74    char tmpserv[16];
75
76    if (serv) {
77	sprintf(tmpserv, "%d", ntohs(sin->sin_port));
78	if (strlen(tmpserv) > servlen)
79	    return EAI_MEMORY;
80	else
81	    strcpy(serv, tmpserv);
82    }
83    if (host) {
84	if (flags & NI_NUMERICHOST) {
85	    if (strlen(inet_ntoa(sin->sin_addr)) >= hostlen)
86		return EAI_MEMORY;
87	    else {
88		strcpy(host, inet_ntoa(sin->sin_addr));
89		return 0;
90	    }
91	} else {
92	    hp = gethostbyaddr((char *)&sin->sin_addr,
93			       sizeof(struct in_addr), AF_INET);
94	    if (hp) {
95		if (strlen(hp->h_name) >= hostlen)
96		    return EAI_MEMORY;
97		else {
98		    strcpy(host, hp->h_name);
99		    return 0;
100		}
101	    }
102	    else
103		return EAI_NODATA;
104	}
105    }
106
107    return 0;
108}
109