xref: /illumos-gate/usr/src/cmd/cmd-inet/usr.lib/in.chargend/in.chargend.c (revision eb0cc229f19c437a6b538d3ac0d0443268290b7e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * chargen inetd service - both stream and dgram based.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <unistd.h>
36 #include <stdio.h>
37 #include <strings.h>
38 #include <netinet/in.h>
39 #include <sys/sysmacros.h>
40 #include <ctype.h>
41 #include <inetsvc.h>
42 
43 
44 #define	LINESIZE		72
45 #define	RINGSIZE		128
46 
47 
48 static	char	ring[RINGSIZE];
49 static	char	*endring;
50 
51 
52 static void
53 initring(void)
54 {
55 	unsigned char ch;
56 
57 	endring = ring;
58 
59 	for (ch = 0; ch <= RINGSIZE; ++ch) {
60 		if (isprint(ch))
61 			*endring++ = ch;
62 	}
63 }
64 
65 static void
66 chargen_stream(int s, char *argv[])
67 {
68 	char	text[LINESIZE+2];
69 	int	i;
70 	char	*rp;
71 	char	*dp;
72 	char	*rs = ring;
73 
74 	setproctitle("chargen", s, argv);
75 
76 	for (;;) {
77 		if (rs >= endring)
78 			rs = ring;
79 		rp = rs++;
80 		dp = text;
81 		i = MIN(LINESIZE, endring - rp);
82 		(void) memmove(dp, rp, i);
83 		dp += i;
84 		if ((rp += i) >= endring)
85 			rp = ring;
86 		if (i < LINESIZE) {
87 			i = LINESIZE - i;
88 			(void) memmove(dp, rp, i);
89 			dp += i;
90 			if ((rp += i) >= endring)
91 				rp = ring;
92 		}
93 
94 		*dp++ = '\r';
95 		*dp++ = '\n';
96 
97 		if (safe_write(s, text, dp - text) != 0)
98 			break;
99 	}
100 }
101 
102 /* ARGSUSED3 */
103 static void
104 chargen_dg(int s, const struct sockaddr *sap, int sa_size, const void *buf,
105     size_t sz)
106 {
107 	char		text[LINESIZE+2];
108 	int		i;
109 	char		*rp;
110 	static char 	*rs = ring;
111 
112 	rp = rs;
113 	if (rs++ >= endring)
114 		rs = ring;
115 	i = MIN(LINESIZE, endring - rp);
116 	(void) memmove(text, rp, i);
117 	if ((rp += i) >= endring)
118 		rp = ring;
119 	if (i < LINESIZE) {
120 		(void) memmove(text, rp, i);
121 		if ((rp += i) >= endring)
122 			rp = ring;
123 	}
124 
125 	text[LINESIZE - 2] = '\r';
126 	text[LINESIZE - 1] = '\n';
127 
128 	(void) safe_sendto(s, text, sizeof (text), 0, sap, sa_size);
129 }
130 
131 int
132 main(int argc, char *argv[])
133 {
134 	opterr = 0;	/* disable getopt error msgs */
135 
136 	initring();
137 
138 	switch (getopt(argc, argv, "ds")) {
139 	case 'd':
140 		dg_template(chargen_dg, STDIN_FILENO, NULL, 0);
141 		break;
142 	case 's':
143 		chargen_stream(STDIN_FILENO, argv);
144 		break;
145 	default:
146 		return (1);
147 	}
148 
149 	return (0);
150 }
151