xref: /titanic_41/usr/src/cmd/ssh/libssh/common/dispatch.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
5*7c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
6*7c478bd9Sstevel@tonic-gate  * are met:
7*7c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
8*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
9*7c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
10*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
11*7c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
12*7c478bd9Sstevel@tonic-gate  *
13*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14*7c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15*7c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16*7c478bd9Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17*7c478bd9Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18*7c478bd9Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19*7c478bd9Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20*7c478bd9Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21*7c478bd9Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23*7c478bd9Sstevel@tonic-gate  */
24*7c478bd9Sstevel@tonic-gate #include "includes.h"
25*7c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: dispatch.c,v 1.15 2002/01/11 13:39:36 markus Exp $");
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include "ssh1.h"
30*7c478bd9Sstevel@tonic-gate #include "ssh2.h"
31*7c478bd9Sstevel@tonic-gate #include "log.h"
32*7c478bd9Sstevel@tonic-gate #include "dispatch.h"
33*7c478bd9Sstevel@tonic-gate #include "packet.h"
34*7c478bd9Sstevel@tonic-gate #include "compat.h"
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #define DISPATCH_MIN	0
37*7c478bd9Sstevel@tonic-gate #define DISPATCH_MAX	255
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate dispatch_fn *dispatch[DISPATCH_MAX];
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate void
dispatch_protocol_error(int type,u_int32_t seq,void * ctxt)42*7c478bd9Sstevel@tonic-gate dispatch_protocol_error(int type, u_int32_t seq, void *ctxt)
43*7c478bd9Sstevel@tonic-gate {
44*7c478bd9Sstevel@tonic-gate 	log("dispatch_protocol_error: type %d seq %u", type, seq);
45*7c478bd9Sstevel@tonic-gate 	if (!compat20)
46*7c478bd9Sstevel@tonic-gate 		fatal("protocol error");
47*7c478bd9Sstevel@tonic-gate 	packet_start(SSH2_MSG_UNIMPLEMENTED);
48*7c478bd9Sstevel@tonic-gate 	packet_put_int(seq);
49*7c478bd9Sstevel@tonic-gate 	packet_send();
50*7c478bd9Sstevel@tonic-gate 	packet_write_wait();
51*7c478bd9Sstevel@tonic-gate }
52*7c478bd9Sstevel@tonic-gate void
dispatch_protocol_ignore(int type,u_int32_t seq,void * ctxt)53*7c478bd9Sstevel@tonic-gate dispatch_protocol_ignore(int type, u_int32_t seq, void *ctxt)
54*7c478bd9Sstevel@tonic-gate {
55*7c478bd9Sstevel@tonic-gate 	log("dispatch_protocol_ignore: type %d seq %u", type, seq);
56*7c478bd9Sstevel@tonic-gate }
57*7c478bd9Sstevel@tonic-gate void
dispatch_init(dispatch_fn * dflt)58*7c478bd9Sstevel@tonic-gate dispatch_init(dispatch_fn *dflt)
59*7c478bd9Sstevel@tonic-gate {
60*7c478bd9Sstevel@tonic-gate 	u_int i;
61*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < DISPATCH_MAX; i++)
62*7c478bd9Sstevel@tonic-gate 		dispatch[i] = dflt;
63*7c478bd9Sstevel@tonic-gate }
64*7c478bd9Sstevel@tonic-gate void
dispatch_range(u_int from,u_int to,dispatch_fn * fn)65*7c478bd9Sstevel@tonic-gate dispatch_range(u_int from, u_int to, dispatch_fn *fn)
66*7c478bd9Sstevel@tonic-gate {
67*7c478bd9Sstevel@tonic-gate 	u_int i;
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	for (i = from; i <= to; i++) {
70*7c478bd9Sstevel@tonic-gate 		if (i >= DISPATCH_MAX)
71*7c478bd9Sstevel@tonic-gate 			break;
72*7c478bd9Sstevel@tonic-gate 		dispatch[i] = fn;
73*7c478bd9Sstevel@tonic-gate 	}
74*7c478bd9Sstevel@tonic-gate }
75*7c478bd9Sstevel@tonic-gate void
dispatch_set(int type,dispatch_fn * fn)76*7c478bd9Sstevel@tonic-gate dispatch_set(int type, dispatch_fn *fn)
77*7c478bd9Sstevel@tonic-gate {
78*7c478bd9Sstevel@tonic-gate 	dispatch[type] = fn;
79*7c478bd9Sstevel@tonic-gate }
80*7c478bd9Sstevel@tonic-gate void
dispatch_run(int mode,int * done,void * ctxt)81*7c478bd9Sstevel@tonic-gate dispatch_run(int mode, int *done, void *ctxt)
82*7c478bd9Sstevel@tonic-gate {
83*7c478bd9Sstevel@tonic-gate 	for (;;) {
84*7c478bd9Sstevel@tonic-gate 		int type;
85*7c478bd9Sstevel@tonic-gate 		u_int32_t seqnr;
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 		if (mode == DISPATCH_BLOCK) {
88*7c478bd9Sstevel@tonic-gate 			type = packet_read_seqnr(&seqnr);
89*7c478bd9Sstevel@tonic-gate 		} else {
90*7c478bd9Sstevel@tonic-gate 			type = packet_read_poll_seqnr(&seqnr);
91*7c478bd9Sstevel@tonic-gate 			if (type == SSH_MSG_NONE)
92*7c478bd9Sstevel@tonic-gate 				return;
93*7c478bd9Sstevel@tonic-gate 		}
94*7c478bd9Sstevel@tonic-gate 		if (type > 0 && type < DISPATCH_MAX && dispatch[type] != NULL)
95*7c478bd9Sstevel@tonic-gate 			(*dispatch[type])(type, seqnr, ctxt);
96*7c478bd9Sstevel@tonic-gate 		else
97*7c478bd9Sstevel@tonic-gate 			packet_disconnect("protocol error: rcvd type %d", type);
98*7c478bd9Sstevel@tonic-gate 		if (done != NULL && *done)
99*7c478bd9Sstevel@tonic-gate 			return;
100*7c478bd9Sstevel@tonic-gate 	}
101*7c478bd9Sstevel@tonic-gate }
102