1 /* 2 * Copyright (c) 1994 Christos Zoulas 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Christos Zoulas. 16 * 4. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * 30 * $Id: sprayd.c,v 1.2 1994/06/24 08:17:42 deraadt Exp $ 31 */ 32 33 #ifndef lint 34 static char rcsid[] = "$Id: sprayd.c,v 1.2 1994/06/24 08:17:42 deraadt Exp $"; 35 #endif /* not lint */ 36 37 #include <stdio.h> 38 #include <signal.h> 39 #include <rpc/rpc.h> 40 #include <sys/time.h> 41 #include <syslog.h> 42 #include <rpcsvc/spray.h> 43 44 static void spray_service __P((struct svc_req *, SVCXPRT *)); 45 46 static int from_inetd = 1; 47 48 #define TIMEOUT 120 49 50 static void 51 cleanup() 52 { 53 (void) pmap_unset(SPRAYPROG, SPRAYVERS); 54 exit(0); 55 } 56 57 58 int 59 main(argc, argv) 60 int argc; 61 char *argv[]; 62 { 63 SVCXPRT *transp; 64 int sock = 0; 65 int proto = 0; 66 struct sockaddr_in from; 67 int fromlen; 68 69 /* 70 * See if inetd started us 71 */ 72 if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) { 73 from_inetd = 0; 74 sock = RPC_ANYSOCK; 75 proto = IPPROTO_UDP; 76 } 77 78 if (!from_inetd) { 79 daemon(0, 0); 80 81 (void) pmap_unset(SPRAYPROG, SPRAYVERS); 82 83 (void) signal(SIGINT, cleanup); 84 (void) signal(SIGTERM, cleanup); 85 (void) signal(SIGHUP, cleanup); 86 } 87 88 openlog("rpc.sprayd", LOG_CONS|LOG_PID, LOG_DAEMON); 89 90 transp = svcudp_create(sock); 91 if (transp == NULL) { 92 syslog(LOG_ERR, "cannot create udp service."); 93 return 1; 94 } 95 if (!svc_register(transp, SPRAYPROG, SPRAYVERS, spray_service, proto)) { 96 syslog(LOG_ERR, 97 "unable to register (SPRAYPROG, SPRAYVERS, %s).", 98 proto ? "udp" : "(inetd)"); 99 return 1; 100 } 101 102 alarm(TIMEOUT); 103 svc_run(); 104 syslog(LOG_ERR, "svc_run returned"); 105 return 1; 106 } 107 108 109 static void 110 spray_service(rqstp, transp) 111 struct svc_req *rqstp; 112 SVCXPRT *transp; 113 { 114 static spraycumul scum; 115 static struct timeval clear; 116 117 switch (rqstp->rq_proc) { 118 case SPRAYPROC_CLEAR: 119 scum.counter = 0; 120 (void) gettimeofday(&clear, 0); 121 /*FALLTHROUGH*/ 122 123 case NULLPROC: 124 if (!svc_sendreply(transp, xdr_void, (char *)NULL)) { 125 svcerr_systemerr(transp); 126 syslog(LOG_ERR, "bad svc_sendreply"); 127 } 128 return; 129 130 case SPRAYPROC_SPRAY: 131 scum.counter++; 132 return; 133 134 case SPRAYPROC_GET: 135 (void) gettimeofday((struct timeval *)&scum.clock, 0); 136 if (scum.clock.usec < clear.tv_usec) { 137 scum.clock.sec--; 138 scum.clock.usec += 1000000; 139 } 140 scum.clock.sec -= clear.tv_sec; 141 scum.clock.usec -= clear.tv_usec; 142 break; 143 144 default: 145 svcerr_noproc(transp); 146 return; 147 } 148 149 if (!svc_sendreply(transp, xdr_spraycumul, (caddr_t)&scum)) { 150 svcerr_systemerr(transp); 151 syslog(LOG_ERR, "bad svc_sendreply"); 152 } 153 } 154