xref: /freebsd/contrib/tcp_wrappers/misc.c (revision d93a896ef95946b0bf1219866fcb324b78543444)
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 extern char *fgets();
23 
24 #ifndef	INADDR_NONE
25 #define	INADDR_NONE	(-1)		/* XXX should be 0xffffffff */
26 #endif
27 
28 /* xgets - fgets() with backslash-newline stripping */
29 
30 char   *xgets(ptr, len, fp)
31 char   *ptr;
32 int     len;
33 FILE   *fp;
34 {
35     int     got;
36     char   *start = ptr;
37 
38     while (fgets(ptr, len, fp)) {
39 	got = strlen(ptr);
40 	if (got >= 1 && ptr[got - 1] == '\n') {
41 	    tcpd_context.line++;
42 	    if (got >= 2 && ptr[got - 2] == '\\') {
43 		got -= 2;
44 	    } else {
45 		return (start);
46 	    }
47 	}
48 	ptr += got;
49 	len -= got;
50 	ptr[0] = 0;
51     }
52     return (ptr > start ? start : 0);
53 }
54 
55 /* split_at - break string at delimiter or return NULL */
56 
57 char   *split_at(string, delimiter)
58 char   *string;
59 int     delimiter;
60 {
61     char   *cp;
62 
63 #ifdef INET6
64     int bracket = 0;
65 
66     for (cp = string; cp && *cp; cp++) {
67 	switch (*cp) {
68 	case '[':
69 	    bracket++;
70 	    break;
71 	case ']':
72 	    bracket--;
73 	    break;
74 	default:
75 	    if (bracket == 0 && *cp == delimiter) {
76 		*cp++ = 0;
77 		return cp;
78 	    }
79 	    break;
80 	}
81     }
82     return (NULL);
83 #else
84     if ((cp = strchr(string, delimiter)) != 0)
85 	*cp++ = 0;
86     return (cp);
87 #endif
88 }
89 
90 /* dot_quad_addr - convert dotted quad to internal form */
91 
92 unsigned long dot_quad_addr(str)
93 char   *str;
94 {
95     int     in_run = 0;
96     int     runs = 0;
97     char   *cp = str;
98 
99     /* Count the number of runs of non-dot characters. */
100 
101     while (*cp) {
102 	if (*cp == '.') {
103 	    in_run = 0;
104 	} else if (in_run == 0) {
105 	    in_run = 1;
106 	    runs++;
107 	}
108 	cp++;
109     }
110     return (runs == 4 ? inet_addr(str) : INADDR_NONE);
111 }
112