1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Misc routines that are used by tcpd and by tcpdchk.
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
5*0Sstevel@tonic-gate  */
6*0Sstevel@tonic-gate
7*0Sstevel@tonic-gate#ifndef lint
8*0Sstevel@tonic-gatestatic char sccsic[] = "@(#) misc.c 1.2 96/02/11 17:01:29";
9*0Sstevel@tonic-gate#endif
10*0Sstevel@tonic-gate
11*0Sstevel@tonic-gate#include <sys/types.h>
12*0Sstevel@tonic-gate#include <sys/param.h>
13*0Sstevel@tonic-gate#include <netinet/in.h>
14*0Sstevel@tonic-gate#include <arpa/inet.h>
15*0Sstevel@tonic-gate#include <stdio.h>
16*0Sstevel@tonic-gate#include <string.h>
17*0Sstevel@tonic-gate
18*0Sstevel@tonic-gate#include "tcpd.h"
19*0Sstevel@tonic-gate
20*0Sstevel@tonic-gateextern char *fgets();
21*0Sstevel@tonic-gate
22*0Sstevel@tonic-gate#ifndef	INADDR_NONE
23*0Sstevel@tonic-gate#define	INADDR_NONE	(-1)		/* XXX should be 0xffffffff */
24*0Sstevel@tonic-gate#endif
25*0Sstevel@tonic-gate
26*0Sstevel@tonic-gate/* xgets - fgets() with backslash-newline stripping */
27*0Sstevel@tonic-gate
28*0Sstevel@tonic-gatechar   *xgets(ptr, len, fp)
29*0Sstevel@tonic-gatechar   *ptr;
30*0Sstevel@tonic-gateint     len;
31*0Sstevel@tonic-gateFILE   *fp;
32*0Sstevel@tonic-gate{
33*0Sstevel@tonic-gate    int     got;
34*0Sstevel@tonic-gate    char   *start = ptr;
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gate    while (fgets(ptr, len, fp)) {
37*0Sstevel@tonic-gate	got = strlen(ptr);
38*0Sstevel@tonic-gate	if (got >= 1 && ptr[got - 1] == '\n') {
39*0Sstevel@tonic-gate	    tcpd_context.line++;
40*0Sstevel@tonic-gate	    if (got >= 2 && ptr[got - 2] == '\\') {
41*0Sstevel@tonic-gate		got -= 2;
42*0Sstevel@tonic-gate	    } else {
43*0Sstevel@tonic-gate		return (start);
44*0Sstevel@tonic-gate	    }
45*0Sstevel@tonic-gate	}
46*0Sstevel@tonic-gate	ptr += got;
47*0Sstevel@tonic-gate	len -= got;
48*0Sstevel@tonic-gate	ptr[0] = 0;
49*0Sstevel@tonic-gate    }
50*0Sstevel@tonic-gate    return (ptr > start ? start : 0);
51*0Sstevel@tonic-gate}
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gate/* split_at - break string at delimiter or return NULL */
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gatechar   *split_at(string, delimiter)
56*0Sstevel@tonic-gatechar   *string;
57*0Sstevel@tonic-gateint     delimiter;
58*0Sstevel@tonic-gate{
59*0Sstevel@tonic-gate    char   *cp;
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate    if ((cp = strchr(string, delimiter)) != 0)
62*0Sstevel@tonic-gate	*cp++ = 0;
63*0Sstevel@tonic-gate    return (cp);
64*0Sstevel@tonic-gate}
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gate/* dot_quad_addr - convert dotted quad to internal form */
67*0Sstevel@tonic-gate
68*0Sstevel@tonic-gateunsigned long dot_quad_addr(str)
69*0Sstevel@tonic-gatechar   *str;
70*0Sstevel@tonic-gate{
71*0Sstevel@tonic-gate    int     in_run = 0;
72*0Sstevel@tonic-gate    int     runs = 0;
73*0Sstevel@tonic-gate    char   *cp = str;
74*0Sstevel@tonic-gate
75*0Sstevel@tonic-gate    /* Count the number of runs of non-dot characters. */
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gate    while (*cp) {
78*0Sstevel@tonic-gate	if (*cp == '.') {
79*0Sstevel@tonic-gate	    in_run = 0;
80*0Sstevel@tonic-gate	} else if (in_run == 0) {
81*0Sstevel@tonic-gate	    in_run = 1;
82*0Sstevel@tonic-gate	    runs++;
83*0Sstevel@tonic-gate	}
84*0Sstevel@tonic-gate	cp++;
85*0Sstevel@tonic-gate    }
86*0Sstevel@tonic-gate    return (runs == 4 ? inet_addr(str) : INADDR_NONE);
87*0Sstevel@tonic-gate}
88