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 /* 28 * chargen inetd service - both stream and dgram based. 29 */ 30 31 #include <sys/types.h> 32 #include <sys/socket.h> 33 #include <unistd.h> 34 #include <stdio.h> 35 #include <strings.h> 36 #include <netinet/in.h> 37 #include <sys/sysmacros.h> 38 #include <ctype.h> 39 #include <inetsvc.h> 40 41 42 #define LINESIZE 72 43 #define RINGSIZE 128 44 45 46 static char ring[RINGSIZE]; 47 static char *endring; 48 49 50 static void 51 initring(void) 52 { 53 unsigned char ch; 54 55 endring = ring; 56 57 for (ch = 0; ch <= RINGSIZE; ++ch) { 58 if (isprint(ch)) 59 *endring++ = ch; 60 } 61 } 62 63 static void 64 chargen_stream(int s, char *argv[]) 65 { 66 char text[LINESIZE+2]; 67 int i; 68 char *rp; 69 char *dp; 70 char *rs = ring; 71 72 setproctitle("chargen", s, argv); 73 74 for (;;) { 75 if (rs >= endring) 76 rs = ring; 77 rp = rs++; 78 dp = text; 79 i = MIN(LINESIZE, endring - rp); 80 (void) memmove(dp, rp, i); 81 dp += i; 82 if ((rp += i) >= endring) 83 rp = ring; 84 if (i < LINESIZE) { 85 i = LINESIZE - i; 86 (void) memmove(dp, rp, i); 87 dp += i; 88 if ((rp += i) >= endring) 89 rp = ring; 90 } 91 92 *dp++ = '\r'; 93 *dp++ = '\n'; 94 95 if (safe_write(s, text, dp - text) != 0) 96 break; 97 } 98 } 99 100 /* ARGSUSED3 */ 101 static void 102 chargen_dg(int s, const struct sockaddr *sap, int sa_size, const void *buf, 103 size_t sz) 104 { 105 char text[LINESIZE+2]; 106 int i; 107 char *rp; 108 static char *rs = ring; 109 110 rp = rs; 111 if (rs++ >= endring) 112 rs = ring; 113 i = MIN(LINESIZE, endring - rp); 114 (void) memmove(text, rp, i); 115 if ((rp += i) >= endring) 116 rp = ring; 117 if (i < LINESIZE) { 118 (void) memmove(text, rp, i); 119 if ((rp += i) >= endring) 120 rp = ring; 121 } 122 123 text[LINESIZE - 2] = '\r'; 124 text[LINESIZE - 1] = '\n'; 125 126 (void) safe_sendto(s, text, sizeof (text), 0, sap, sa_size); 127 } 128 129 int 130 main(int argc, char *argv[]) 131 { 132 opterr = 0; /* disable getopt error msgs */ 133 134 initring(); 135 136 switch (getopt(argc, argv, "ds")) { 137 case 'd': 138 dg_template(chargen_dg, STDIN_FILENO, NULL, 0); 139 break; 140 case 's': 141 chargen_stream(STDIN_FILENO, argv); 142 break; 143 default: 144 return (1); 145 } 146 147 return (0); 148 } 149