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