summaryrefslogtreecommitdiffstats
path: root/src/util/find_inet.c
blob: 294634c50269f9a84faf7c70fac509593087a401 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*++
/* NAME
/*	find_inet 3
/* SUMMARY
/*	inet-domain name services
/* SYNOPSIS
/*	#include <find_inet.h>
/*
/*	unsigned find_inet_addr(host)
/*	const char *host;
/*
/*	int	find_inet_port(port, proto)
/*	const char *port;
/*	const char *proto;
/* DESCRIPTION
/*	These functions translate network address information from
/*	between printable form to the internal the form used by the
/*	BSD TCP/IP network software.
/*
/*	find_inet_addr() translates a symbolic or numerical hostname.
/*	This function is deprecated. Use hostname_to_hostaddr() instead.
/*
/*	find_inet_port() translates a symbolic or numerical port name.
/* BUGS
/*	find_inet_addr() ignores all but the first address listed for
/*	a symbolic hostname.
/* DIAGNOSTICS
/*	Lookup and conversion errors are fatal.
/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	IBM T.J. Watson Research
/*	P.O. Box 704
/*	Yorktown Heights, NY 10598, USA
/*
/*	Wietse Venema
/*	Google, Inc.
/*	111 8th Avenue
/*	New York, NY 10011, USA
/*--*/

/* System libraries. */

#include <sys_defs.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>

/* Application-specific. */

#include "msg.h"
#include "stringops.h"
#include "find_inet.h"
#include "known_tcp_ports.h"

#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif

#ifdef TEST
extern NORETURN PRINTFLIKE(1, 2) test_msg_fatal(const char *,...);

#define msg_fatal test_msg_fatal
#endif

/* find_inet_addr - translate numerical or symbolic host name */

unsigned find_inet_addr(const char *host)
{
    struct in_addr addr;
    struct hostent *hp;

    addr.s_addr = inet_addr(host);
    if ((addr.s_addr == INADDR_NONE) || (addr.s_addr == 0)) {
	if ((hp = gethostbyname(host)) == 0)
	    msg_fatal("host not found: %s", host);
	if (hp->h_addrtype != AF_INET)
	    msg_fatal("unexpected address family: %d", hp->h_addrtype);
	if (hp->h_length != sizeof(addr))
	    msg_fatal("unexpected address length %d", hp->h_length);
	memcpy((void *) &addr, hp->h_addr, hp->h_length);
    }
    return (addr.s_addr);
}

/* find_inet_port - translate numerical or symbolic service name */

int     find_inet_port(const char *service, const char *protocol)
{
    struct servent *sp;
    int     port;

    service = filter_known_tcp_port(service);
    if (alldig(service) && (port = atoi(service)) != 0) {
	if (port < 0 || port > 65535)
	    msg_fatal("bad port number: %s", service);
	return (htons(port));
    } else {
	if ((sp = getservbyname(service, protocol)) == 0)
	    msg_fatal("unknown service: %s/%s", service, protocol);
	return (sp->s_port);
    }
}

#ifdef TEST

#include <stdlib.h>
#include <setjmp.h>
#include <string.h>

#include <vstream.h>
#include <vstring.h>
#include <msg_vstream.h>

#define STR(x) vstring_str(x)

 /* TODO(wietse) make this a proper VSTREAM interface */

/* vstream_swap - kludge to capture output for testing */

static void vstream_swap(VSTREAM *one, VSTREAM *two)
{
    VSTREAM save;

    save = *one;
    *one = *two;
    *two = save;
}

jmp_buf test_fatal_jbuf;

#undef msg_fatal

/* test_msg_fatal - does not return, and does not terminate */

void    test_msg_fatal(const char *fmt,...)
{
    va_list ap;

    va_start(ap, fmt);
    vmsg_warn(fmt, ap);
    va_end(ap);
    longjmp(test_fatal_jbuf, 1);
}

struct association {
    const char *lhs;			/* service name */
    const char *rhs;			/* service port */
};

struct test_case {
    const char *label;			/* identifies test case */
    struct association associations[10];
    const char *service;
    const char *proto;
    const char *exp_warning;		/* expected error */
    int     exp_hport;			/* expected port, host byte order */
};

struct test_case test_cases[] = {
    {"good-symbolic",
	 /* association */ {{"foobar", "25252"}, 0},
	 /* service */ "foobar",
	 /* proto */ "tcp",
	 /* exp_warning */ "",
	 /* exp_hport */ 25252,
    },
    {"good-numeric",
	 /* association */ {{"foobar", "25252"}, 0},
	 /* service */ "25252",
	 /* proto */ "tcp",
	 /* exp_warning */ "",
	 /* exp_hport */ 25252,
    },
    {"bad-symbolic",
	 /* association */ {{"foobar", "25252"}, 0},
	 /* service */ "an-impossible-name",
	 /* proto */ "tcp",
	 /* exp_warning */ "find_inet: warning: unknown service: an-impossible-name/tcp\n",
    },
    {"bad-numeric",
	 /* association */ {{"foobar", "25252"}, 0},
	 /* service */ "123456",
	 /* proto */ "tcp",
	 /* exp_warning */ "find_inet: warning: bad port number: 123456\n",
    },
};

int main(int argc, char **argv) {
    struct test_case *tp;
    struct association *ap;
    int     pass = 0;
    int     fail = 0;
    const char *err;
    int     test_failed;
    int     nport;
    VSTRING *msg_buf;
    VSTREAM *memory_stream;

    msg_vstream_init("find_inet", VSTREAM_ERR);
    msg_buf = vstring_alloc(100);

    for (tp = test_cases; tp->label != 0; tp++) {
	test_failed = 0;
	VSTRING_RESET(msg_buf);
	VSTRING_TERMINATE(msg_buf);
	clear_known_tcp_ports();
	for (err = 0, ap = tp->associations; err == 0 && ap->lhs != 0; ap++)
	    err = add_known_tcp_port(ap->lhs, ap->rhs);
	if (err != 0) {
	    msg_warn("test case %s: got err: \"%s\"", tp->label, err);
	    test_failed = 1;
	} else {
	    if ((memory_stream = vstream_memopen(msg_buf, O_WRONLY)) == 0)
		msg_fatal("open memory stream: %m");
	    vstream_swap(VSTREAM_ERR, memory_stream);
	    if (setjmp(test_fatal_jbuf) == 0)
		nport = find_inet_port(tp->service, tp->proto);
	    vstream_swap(memory_stream, VSTREAM_ERR);
	    if (vstream_fclose(memory_stream))
		msg_fatal("close memory stream: %m");
	    if (strcmp(STR(msg_buf), tp->exp_warning) != 0) {
		msg_warn("test case %s: got error: \"%s\", want: \"%s\"",
			 tp->label, STR(msg_buf), tp->exp_warning);
		test_failed = 1;
	    } else if (tp->exp_warning[0] == 0) {
		if (ntohs(nport) != tp->exp_hport) {
		    msg_warn("test case %s: got port \"%d\", want: \"%d\"",
			     tp->label, ntohs(nport), tp->exp_hport);
		    test_failed = 1;
		}
	    }
	}
	if (test_failed) {
	    msg_info("%s: FAIL", tp->label);
	    fail++;
	} else {
	    msg_info("%s: PASS", tp->label);
	    pass++;
	}
    }
    msg_info("PASS=%d FAIL=%d", pass, fail);
    vstring_free(msg_buf);
    exit(fail != 0);
}

#endif