1116166Stmm /*
2116166Stmm  * Warning - this relies heavily on the TLI implementation in PTX 2.X and will
3116166Stmm  * probably not work under PTX 4.
4116166Stmm  *
5116166Stmm  * Author: Tim Wright, Sequent Computer Systems Ltd., UK.
6116166Stmm  *
7116166Stmm  * Modified slightly to conform to the new internal interfaces - Wietse
8116166Stmm  */
9116166Stmm
10116166Stmm#ifndef lint
11116166Stmmstatic char sccsid[] = "@(#) tli-sequent.c 1.1 94/12/28 17:42:51";
12116166Stmm#endif
13116166Stmm
14116166Stmm#ifdef TLI_SEQUENT
15116166Stmm
16116166Stmm/* System libraries. */
17116166Stmm
18116166Stmm#include <sys/types.h>
19116166Stmm#include <sys/param.h>
20116166Stmm#include <sys/stat.h>
21116166Stmm#include <sys/tiuser.h>
22116166Stmm#include <sys/stream.h>
23116166Stmm#include <sys/stropts.h>
24116166Stmm#include <sys/tihdr.h>
25116166Stmm#include <sys/timod.h>
26116166Stmm#include <sys/socket.h>
27116166Stmm#include <netinet/in.h>
28116166Stmm#include <stdio.h>
29116166Stmm#include <syslog.h>
30130650Sru#include <errno.h>
31130650Sru#include <string.h>
32194193Smarius
33116166Stmmextern int errno;
34116166Stmmextern char *sys_errlist[];
35116166Stmmextern int sys_nerr;
36116166Stmmextern int t_errno;
37148145Strhodesextern char *t_errlist[];
38116166Stmmextern int t_nerr;
39151046Strhodes
40151046Strhodes/* Local stuff. */
41148220Strhodes
42148145Strhodes#include "tcpd.h"
43116166Stmm#include "tli-sequent.h"
44116166Stmm
45148145Strhodes/* Forward declarations. */
46148145Strhodes
47151046Strhodesstatic char *tli_error();
48151046Strhodesstatic void tli_sink();
49148145Strhodes
50148145Strhodes/* tli_host - determine endpoint info */
51148145Strhodes
52148145Strhodesint     tli_host(request)
53116166Stmmstruct request_info *request;
54116166Stmm{
55116166Stmm    static struct sockaddr_in client;
56194193Smarius    static struct sockaddr_in server;
57194193Smarius    struct _ti_user *tli_state_ptr;
58194193Smarius    union T_primitives *TSI_prim_ptr;
59194193Smarius    struct strpeek peek;
60194193Smarius    int     len;
61194193Smarius
62194193Smarius    /*
63194193Smarius     * Use DNS and socket routines for name and address conversions.
64194193Smarius     */
65194193Smarius
66116166Stmm    sock_methods(request);
67116166Stmm
68116166Stmm    /*
69116166Stmm     * Find out the client address using getpeerinaddr(). This call is the
70116166Stmm     * TLI equivalent to getpeername() under Dynix/ptx.
71116166Stmm     */
72116166Stmm
73116166Stmm    len = sizeof(client);
74116166Stmm    t_sync(request->fd);
75116166Stmm    if (getpeerinaddr(request->fd, &client, len) < 0) {
76116166Stmm	tcpd_warn("can't get client address: %s", tli_error());
77116166Stmm	return;
78130650Sru    }
79116166Stmm    request->client->sin = &client;
80116166Stmm
81130650Sru    /* Call TLI utility routine to get information on endpoint */
82116166Stmm    if ((tli_state_ptr = _t_checkfd(request->fd)) == NULL)
83116166Stmm	return;
84130650Sru
85116166Stmm    if (tli_state_ptr->ti_servtype == T_CLTS) {
86130650Sru	/* UDP - may need to get address the hard way */
87116166Stmm	if (client.sin_addr.s_addr == 0) {
88116166Stmm	    /* The UDP endpoint is not connected so we didn't get the */
89130650Sru	    /* remote address - get it the hard way ! */
90116166Stmm
91116166Stmm	    /* Look at the control part of the top message on the stream */
92130650Sru	    /* we don't want to remove it from the stream so we use I_PEEK */
93116166Stmm	    peek.ctlbuf.maxlen = tli_state_ptr->ti_ctlsize;
94116166Stmm	    peek.ctlbuf.len = 0;
95130650Sru	    peek.ctlbuf.buf = tli_state_ptr->ti_ctlbuf;
96116166Stmm	    /* Don't even look at the data */
97116166Stmm	    peek.databuf.maxlen = -1;
98130650Sru	    peek.databuf.len = 0;
99116166Stmm	    peek.databuf.buf = 0;
100133729Smarius	    peek.flags = 0;
101133729Smarius
102133729Smarius	    switch (ioctl(request->fd, I_PEEK, &peek)) {
103133729Smarius	    case -1:
104133729Smarius		tcpd_warn("can't peek at endpoint: %s", tli_error());
105133729Smarius		return;
106133729Smarius	    case 0:
107133729Smarius		/* No control part - we're hosed */
108133729Smarius		tcpd_warn("can't get UDP info: %s", tli_error());
109133729Smarius		return;
110133729Smarius	    default:
111133729Smarius		/* FALL THROUGH */
112133729Smarius		;
113133729Smarius	    }
114133729Smarius	    /* Can we even check the PRIM_type ? */
115133729Smarius	    if (peek.ctlbuf.len < sizeof(long)) {
116133729Smarius		tcpd_warn("UDP control info garbage");
117133729Smarius		return;
118133729Smarius	    }
119133729Smarius	    TSI_prim_ptr = (union T_primitives *) peek.ctlbuf.buf;
120133729Smarius	    if (TSI_prim_ptr->type != T_UNITDATA_IND) {
121133729Smarius		tcpd_warn("wrong type for UDP control info");
122133729Smarius		return;
123116166Stmm	    }
124166346Sbrueffer	    /* Validate returned unitdata indication packet */
125116166Stmm	    if ((peek.ctlbuf.len < sizeof(struct T_unitdata_ind)) ||
126116166Stmm		((TSI_prim_ptr->unitdata_ind.OPT_length != 0) &&
127116166Stmm		 (peek.ctlbuf.len <
128194193Smarius		  TSI_prim_ptr->unitdata_ind.OPT_length +
129133729Smarius		  TSI_prim_ptr->unitdata_ind.OPT_offset))) {
130116166Stmm		tcpd_warn("UDP control info garbaged");
131116166Stmm		return;
132116166Stmm	    }
133116166Stmm	    /* Extract the address */
134116166Stmm	    memcpy(&client,
135202386Sru		   peek.ctlbuf.buf + TSI_prim_ptr->unitdata_ind.SRC_offset,
136116166Stmm		   TSI_prim_ptr->unitdata_ind.SRC_length);
137116166Stmm	}
138116166Stmm	request->sink = tli_sink;
139116166Stmm    }
140116166Stmm    if (getmyinaddr(request->fd, &server, len) < 0)
141116166Stmm	tcpd_warn("can't get local address: %s", tli_error());
142116166Stmm    else
143116166Stmm	request->server->sin = &server;
144116166Stmm}
145116166Stmm
146116166Stmm/* tli_error - convert tli error number to text */
147116166Stmm
148116166Stmmstatic char *tli_error()
149116166Stmm{
150267938Sbapt    static char buf[40];
151
152    if (t_errno != TSYSERR) {
153	if (t_errno < 0 || t_errno >= t_nerr) {
154	    sprintf(buf, "Unknown TLI error %d", t_errno);
155	    return (buf);
156	} else {
157	    return (t_errlist[t_errno]);
158	}
159    } else {
160	if (errno < 0 || errno >= sys_nerr) {
161	    sprintf(buf, "Unknown UNIX error %d", errno);
162	    return (buf);
163	} else {
164	    return (sys_errlist[errno]);
165	}
166    }
167}
168
169/* tli_sink - absorb unreceived datagram */
170
171static void tli_sink(fd)
172int     fd;
173{
174    struct t_unitdata *unit;
175    int     flags;
176
177    /*
178     * Something went wrong. Absorb the datagram to keep inetd from looping.
179     * Allocate storage for address, control and data. If that fails, sleep
180     * for a couple of seconds in an attempt to keep inetd from looping too
181     * fast.
182     */
183
184    if ((unit = (struct t_unitdata *) t_alloc(fd, T_UNITDATA, T_ALL)) == 0) {
185	tcpd_warn("t_alloc: %s", tli_error());
186	sleep(5);
187    } else {
188	(void) t_rcvudata(fd, unit, &flags);
189	t_free((void *) unit, T_UNITDATA);
190    }
191}
192
193#endif /* TLI_SEQUENT */
194