1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate * 22*7c478bd9Sstevel@tonic-gate * Copyright 2002 Sun Microsystems, Inc. All rights reserved. 23*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 24*7c478bd9Sstevel@tonic-gate */ 25*7c478bd9Sstevel@tonic-gate 26*7c478bd9Sstevel@tonic-gate /* 27*7c478bd9Sstevel@tonic-gate * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T 28*7c478bd9Sstevel@tonic-gate * All Rights Reserved. 29*7c478bd9Sstevel@tonic-gate */ 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gate /* 32*7c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 33*7c478bd9Sstevel@tonic-gate * The Regents of the University of California. 34*7c478bd9Sstevel@tonic-gate * All Rights Reserved. 35*7c478bd9Sstevel@tonic-gate * 36*7c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 37*7c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 38*7c478bd9Sstevel@tonic-gate * contributors. 39*7c478bd9Sstevel@tonic-gate */ 40*7c478bd9Sstevel@tonic-gate 41*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 42*7c478bd9Sstevel@tonic-gate 43*7c478bd9Sstevel@tonic-gate /* 44*7c478bd9Sstevel@tonic-gate * Invoked by the Internet daemon to handle talk requests 45*7c478bd9Sstevel@tonic-gate * Processes talk requests until MAX_LIFE seconds go by with 46*7c478bd9Sstevel@tonic-gate * no action, then dies. 47*7c478bd9Sstevel@tonic-gate */ 48*7c478bd9Sstevel@tonic-gate 49*7c478bd9Sstevel@tonic-gate #include <stdio.h> 50*7c478bd9Sstevel@tonic-gate #include <errno.h> 51*7c478bd9Sstevel@tonic-gate #include <sys/ioctl.h> 52*7c478bd9Sstevel@tonic-gate #include <sys/time.h> 53*7c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h> 54*7c478bd9Sstevel@tonic-gate #include <sys/wait.h> 55*7c478bd9Sstevel@tonic-gate #include <unistd.h> 56*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 57*7c478bd9Sstevel@tonic-gate #include <string.h> 58*7c478bd9Sstevel@tonic-gate #include "talkd_impl.h" 59*7c478bd9Sstevel@tonic-gate 60*7c478bd9Sstevel@tonic-gate static CTL_MSG request; 61*7c478bd9Sstevel@tonic-gate static CTL_RESPONSE response; 62*7c478bd9Sstevel@tonic-gate 63*7c478bd9Sstevel@tonic-gate char hostname[HOST_NAME_LENGTH]; 64*7c478bd9Sstevel@tonic-gate int debug = 0; 65*7c478bd9Sstevel@tonic-gate 66*7c478bd9Sstevel@tonic-gate static CTL_MSG swapmsg(CTL_MSG req); 67*7c478bd9Sstevel@tonic-gate 68*7c478bd9Sstevel@tonic-gate int 69*7c478bd9Sstevel@tonic-gate main() 70*7c478bd9Sstevel@tonic-gate { 71*7c478bd9Sstevel@tonic-gate struct sockaddr_in from; 72*7c478bd9Sstevel@tonic-gate socklen_t from_size = (socklen_t)sizeof (from); 73*7c478bd9Sstevel@tonic-gate int cc; 74*7c478bd9Sstevel@tonic-gate int name_length = sizeof (hostname); 75*7c478bd9Sstevel@tonic-gate fd_set rfds; 76*7c478bd9Sstevel@tonic-gate struct timeval tv; 77*7c478bd9Sstevel@tonic-gate 78*7c478bd9Sstevel@tonic-gate (void) sysinfo(SI_HOSTNAME, hostname, name_length); 79*7c478bd9Sstevel@tonic-gate 80*7c478bd9Sstevel@tonic-gate for (;;) { 81*7c478bd9Sstevel@tonic-gate tv.tv_sec = MAX_LIFE; 82*7c478bd9Sstevel@tonic-gate tv.tv_usec = 0; 83*7c478bd9Sstevel@tonic-gate FD_ZERO(&rfds); 84*7c478bd9Sstevel@tonic-gate FD_SET(0, &rfds); 85*7c478bd9Sstevel@tonic-gate if (select(1, &rfds, 0, 0, &tv) <= 0) 86*7c478bd9Sstevel@tonic-gate return (0); 87*7c478bd9Sstevel@tonic-gate cc = recvfrom(0, (char *)&request, sizeof (request), 0, 88*7c478bd9Sstevel@tonic-gate (struct sockaddr *)&from, &from_size); 89*7c478bd9Sstevel@tonic-gate 90*7c478bd9Sstevel@tonic-gate if (cc != sizeof (request)) { 91*7c478bd9Sstevel@tonic-gate if (cc < 0 && errno != EINTR) { 92*7c478bd9Sstevel@tonic-gate print_error("receive"); 93*7c478bd9Sstevel@tonic-gate } 94*7c478bd9Sstevel@tonic-gate } else { 95*7c478bd9Sstevel@tonic-gate 96*7c478bd9Sstevel@tonic-gate if (debug) { 97*7c478bd9Sstevel@tonic-gate (void) printf("Request received : \n"); 98*7c478bd9Sstevel@tonic-gate (void) print_request(&request); 99*7c478bd9Sstevel@tonic-gate } 100*7c478bd9Sstevel@tonic-gate 101*7c478bd9Sstevel@tonic-gate request = swapmsg(request); 102*7c478bd9Sstevel@tonic-gate process_request(&request, &response); 103*7c478bd9Sstevel@tonic-gate 104*7c478bd9Sstevel@tonic-gate if (debug) { 105*7c478bd9Sstevel@tonic-gate (void) printf("Response sent : \n"); 106*7c478bd9Sstevel@tonic-gate print_response(&response); 107*7c478bd9Sstevel@tonic-gate } 108*7c478bd9Sstevel@tonic-gate 109*7c478bd9Sstevel@tonic-gate /* 110*7c478bd9Sstevel@tonic-gate * Can block here, is this what I want? 111*7c478bd9Sstevel@tonic-gate */ 112*7c478bd9Sstevel@tonic-gate cc = sendto(0, (char *)&response, sizeof (response), 0, 113*7c478bd9Sstevel@tonic-gate (struct sockaddr *)&request.ctl_addr, 114*7c478bd9Sstevel@tonic-gate (socklen_t)sizeof (request.ctl_addr)); 115*7c478bd9Sstevel@tonic-gate 116*7c478bd9Sstevel@tonic-gate if (cc != sizeof (response)) { 117*7c478bd9Sstevel@tonic-gate print_error("Send"); 118*7c478bd9Sstevel@tonic-gate } 119*7c478bd9Sstevel@tonic-gate } 120*7c478bd9Sstevel@tonic-gate } 121*7c478bd9Sstevel@tonic-gate } 122*7c478bd9Sstevel@tonic-gate 123*7c478bd9Sstevel@tonic-gate void 124*7c478bd9Sstevel@tonic-gate print_error(char *string) 125*7c478bd9Sstevel@tonic-gate { 126*7c478bd9Sstevel@tonic-gate FILE *cons; 127*7c478bd9Sstevel@tonic-gate char *err_dev = "/dev/console"; 128*7c478bd9Sstevel@tonic-gate char *sys; 129*7c478bd9Sstevel@tonic-gate pid_t val, pid; 130*7c478bd9Sstevel@tonic-gate 131*7c478bd9Sstevel@tonic-gate if (debug) 132*7c478bd9Sstevel@tonic-gate err_dev = "/dev/tty"; 133*7c478bd9Sstevel@tonic-gate 134*7c478bd9Sstevel@tonic-gate if ((sys = strerror(errno)) == (char *)NULL) 135*7c478bd9Sstevel@tonic-gate sys = "Unknown error"; 136*7c478bd9Sstevel@tonic-gate 137*7c478bd9Sstevel@tonic-gate /* don't ever open tty's directly, let a child do it */ 138*7c478bd9Sstevel@tonic-gate if ((pid = fork()) == 0) { 139*7c478bd9Sstevel@tonic-gate cons = fopen(err_dev, "a"); 140*7c478bd9Sstevel@tonic-gate if (cons != NULL) { 141*7c478bd9Sstevel@tonic-gate (void) fprintf(cons, "Talkd : %s : %s(%d)\n\r", string, 142*7c478bd9Sstevel@tonic-gate sys, errno); 143*7c478bd9Sstevel@tonic-gate (void) fclose(cons); 144*7c478bd9Sstevel@tonic-gate } 145*7c478bd9Sstevel@tonic-gate exit(0); 146*7c478bd9Sstevel@tonic-gate } else { 147*7c478bd9Sstevel@tonic-gate /* wait for the child process to return */ 148*7c478bd9Sstevel@tonic-gate do { 149*7c478bd9Sstevel@tonic-gate val = wait(0); 150*7c478bd9Sstevel@tonic-gate if (val == (pid_t)-1) { 151*7c478bd9Sstevel@tonic-gate if (errno == EINTR) { 152*7c478bd9Sstevel@tonic-gate continue; 153*7c478bd9Sstevel@tonic-gate } else if (errno == ECHILD) { 154*7c478bd9Sstevel@tonic-gate break; 155*7c478bd9Sstevel@tonic-gate } 156*7c478bd9Sstevel@tonic-gate } 157*7c478bd9Sstevel@tonic-gate } while (val != pid); 158*7c478bd9Sstevel@tonic-gate } 159*7c478bd9Sstevel@tonic-gate } 160*7c478bd9Sstevel@tonic-gate 161*7c478bd9Sstevel@tonic-gate #define swapshort(a) (((a << 8) | ((unsigned short) a >> 8)) & 0xffff) 162*7c478bd9Sstevel@tonic-gate #define swaplong(a) ((swapshort(a) << 16) | (swapshort(((unsigned)a >> 16)))) 163*7c478bd9Sstevel@tonic-gate 164*7c478bd9Sstevel@tonic-gate /* 165*7c478bd9Sstevel@tonic-gate * Heuristic to detect if need to swap bytes. 166*7c478bd9Sstevel@tonic-gate */ 167*7c478bd9Sstevel@tonic-gate 168*7c478bd9Sstevel@tonic-gate static CTL_MSG 169*7c478bd9Sstevel@tonic-gate swapmsg(CTL_MSG req) 170*7c478bd9Sstevel@tonic-gate { 171*7c478bd9Sstevel@tonic-gate CTL_MSG swapreq; 172*7c478bd9Sstevel@tonic-gate 173*7c478bd9Sstevel@tonic-gate if (req.ctl_addr.sin_family == swapshort(AF_INET)) { 174*7c478bd9Sstevel@tonic-gate swapreq = req; 175*7c478bd9Sstevel@tonic-gate swapreq.id_num = swaplong(req.id_num); 176*7c478bd9Sstevel@tonic-gate swapreq.pid = swaplong(req.pid); 177*7c478bd9Sstevel@tonic-gate swapreq.addr.sin_family = swapshort(req.addr.sin_family); 178*7c478bd9Sstevel@tonic-gate swapreq.ctl_addr.sin_family = 179*7c478bd9Sstevel@tonic-gate swapshort(req.ctl_addr.sin_family); 180*7c478bd9Sstevel@tonic-gate return (swapreq); 181*7c478bd9Sstevel@tonic-gate } else { 182*7c478bd9Sstevel@tonic-gate return (req); 183*7c478bd9Sstevel@tonic-gate } 184*7c478bd9Sstevel@tonic-gate } 185