summaryrefslogtreecommitdiff
path: root/usr/src/lib/libwrap/misc.c
blob: dbc3a7cca1a743a6335c93165b2ee95cc8c13600 (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
/*
 * Copyright 2001 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */
#pragma ident	"%Z%%M%	%I%	%E% SMI"

 /*
  * Misc routines that are used by tcpd and by tcpdchk.
  * 
  * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
  */

#ifndef lint
static char sccsic[] = "@(#) misc.c 1.2 96/02/11 17:01:29";
#endif

#include <sys/types.h>
#include <sys/param.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <netdb.h>

#include "tcpd.h"

extern char *fgets();

#ifndef	INADDR_NONE
#define	INADDR_NONE	(-1)		/* XXX should be 0xffffffff */
#endif

/* xgets - fgets() with backslash-newline stripping */

char   *xgets(ptr, len, fp)
char   *ptr;
int     len;
FILE   *fp;
{
    int     got;
    char   *start = ptr;

    while (fgets(ptr, len, fp)) {
	got = strlen(ptr);
	if (got >= 1 && ptr[got - 1] == '\n') {
	    tcpd_context.line++;
	    if (got >= 2 && ptr[got - 2] == '\\') {
		got -= 2;
	    } else {
		return (start);
	    }
	}
	ptr += got;
	len -= got;
	ptr[0] = 0;
    }
    return (ptr > start ? start : 0);
}

/* split_at - break string at delimiter or return NULL */

char   *split_at(string, delimiter)
char   *string;
int     delimiter;
{
    char   *cp;

    if ((cp = strchr(string, delimiter)) != 0)
	*cp++ = 0;
    return (cp);
}

/* dot_quad_addr - convert dotted quad to internal form */

unsigned long dot_quad_addr(str)
char   *str;
{
    int     in_run = 0;
    int     runs = 0;
    char   *cp = str;

    /* Count the number of runs of non-dot characters. */

    while (*cp) {
	if (*cp == '.') {
	    in_run = 0;
	} else if (in_run == 0) {
	    in_run = 1;
	    runs++;
	}
	cp++;
    }
    return (runs == 4 ? inet_addr(str) : INADDR_NONE);
}

/* numeric_addr - convert textual IP address to binary form */

int numeric_addr(str, addr, af, len)
char *str;
union gen_addr *addr;
int *af;
int *len;
{
    union gen_addr t;

    if (addr == NULL)
	addr = &t;
#ifdef HAVE_IPV6
    if (strchr(str,':')) {
	if (af) *af = AF_INET6;
	if (len) *len = sizeof(struct in6_addr);
	if (inet_pton(AF_INET6, str, (void*) addr) == 1)
	    return 0;
	return -1;
    }
#endif
    if (af) *af = AF_INET;
    if (len) *len = sizeof(struct in_addr);
    addr->ga_in.s_addr = dot_quad_addr(str);
    return addr->ga_in.s_addr == INADDR_NONE ? -1 : 0;
}

/* For none RFC 2553 compliant systems */
#ifdef USE_GETHOSTBYNAME2
#define getipnodebyname(h,af,flags,err)	gethostbyname2(h,af)
#define freehostent(x)			x = 0
#endif

/* tcpd_gethostbyname - an IP family neutral gethostbyname */

struct hostent *tcpd_gethostbyname(host, af)
char *host;
int af;
{
#ifdef HAVE_IPV6
    struct hostent *hp;
    static struct hostent *hs;		/* freehostent() on next call */
    int err;

    if (af == AF_INET6) {		/* must be AF_INET6 */
	if (hs)
	    freehostent(hs);
	return (hs = getipnodebyname(host, AF_INET6, 0, &err));
    }
    hp = gethostbyname(host);
    if (hp != NULL || af == AF_INET) { 	/* found or must be AF_INET */
	return hp;
    } else {				/* Try INET6 */
	if (hs)
	    freehostent(hs);
	return (hs = getipnodebyname(host, AF_INET6, 0, &err));
    }
#else
    return gethostbyname(host);
#endif
}

#ifdef HAVE_IPV6
/*
 * When using IPv6 addresses, we'll be seeing lots of ":"s;
 * we require the addresses to be specified as [address].
 * An IPv6 address can be specified in 3 ways:
 *
 * x:x:x:x:x:x:x:x		(fully specified)
 * x::x:x:x:x			(zeroes squashed)
 * ::FFFF:1.2.3.4		(IPv4 mapped)
 *
 * These need to be skipped to get at the ":" delimeters.
 *
 * We also allow a '/prefix' specifier.
 */
char *skip_ipv6_addrs(str)
char *str;
{
    char *obr, *cbr, *colon;
    char *p = str;
    char *q;

    while (1) {
	if ((colon = strchr(p, ':')) == NULL)
	    return p;
	if ((obr = strchr(p, '[')) == NULL || obr > colon)
	    return p;
	if ((cbr = strchr(obr, ']')) == NULL)
	    return p;

	for (q = obr + 1; q < cbr; q++) {
	    /*
	     * Quick and dirty parse, cheaper than inet_pton
	     * Could count colons and dots (must be 0 or 3 dots, no
	     * colons after dots seens, only one double :, etc, etc)
	     */
	    if (*q != ':' && *q != '.' && *q != '/' && !isxdigit(*q & 0xff))
		return p;
	}
	p = cbr + 1;
    }
}
#endif /* HAVE_IPV6 */