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