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