xref: /freebsd/crypto/openssh/dispatch.c (revision a04a10f8915401e0ac20dce0ade6c5b6e1bb13da)
1a04a10f8SKris Kennaway /*
2a04a10f8SKris Kennaway  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
3a04a10f8SKris Kennaway  *
4a04a10f8SKris Kennaway  * Redistribution and use in source and binary forms, with or without
5a04a10f8SKris Kennaway  * modification, are permitted provided that the following conditions
6a04a10f8SKris Kennaway  * are met:
7a04a10f8SKris Kennaway  * 1. Redistributions of source code must retain the above copyright
8a04a10f8SKris Kennaway  *    notice, this list of conditions and the following disclaimer.
9a04a10f8SKris Kennaway  * 2. Redistributions in binary form must reproduce the above copyright
10a04a10f8SKris Kennaway  *    notice, this list of conditions and the following disclaimer in the
11a04a10f8SKris Kennaway  *    documentation and/or other materials provided with the distribution.
12a04a10f8SKris Kennaway  * 3. All advertising materials mentioning features or use of this software
13a04a10f8SKris Kennaway  *    must display the following acknowledgement:
14a04a10f8SKris Kennaway  *      This product includes software developed by Markus Friedl.
15a04a10f8SKris Kennaway  * 4. The name of the author may not be used to endorse or promote products
16a04a10f8SKris Kennaway  *    derived from this software without specific prior written permission.
17a04a10f8SKris Kennaway  *
18a04a10f8SKris Kennaway  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19a04a10f8SKris Kennaway  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20a04a10f8SKris Kennaway  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21a04a10f8SKris Kennaway  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22a04a10f8SKris Kennaway  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23a04a10f8SKris Kennaway  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24a04a10f8SKris Kennaway  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25a04a10f8SKris Kennaway  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26a04a10f8SKris Kennaway  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27a04a10f8SKris Kennaway  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28a04a10f8SKris Kennaway  */
29a04a10f8SKris Kennaway #include "includes.h"
30a04a10f8SKris Kennaway RCSID("$Id: dispatch.c,v 1.2 2000/04/14 10:30:31 markus Exp $");
31a04a10f8SKris Kennaway #include "ssh.h"
32a04a10f8SKris Kennaway #include "dispatch.h"
33a04a10f8SKris Kennaway #include "packet.h"
34a04a10f8SKris Kennaway 
35a04a10f8SKris Kennaway #define DISPATCH_MIN	0
36a04a10f8SKris Kennaway #define DISPATCH_MAX	255
37a04a10f8SKris Kennaway 
38a04a10f8SKris Kennaway dispatch_fn *dispatch[DISPATCH_MAX];
39a04a10f8SKris Kennaway 
40a04a10f8SKris Kennaway void
41a04a10f8SKris Kennaway dispatch_protocol_error(int type, int plen)
42a04a10f8SKris Kennaway {
43a04a10f8SKris Kennaway 	error("Hm, dispatch protocol error: type %d plen %d", type, plen);
44a04a10f8SKris Kennaway }
45a04a10f8SKris Kennaway void
46a04a10f8SKris Kennaway dispatch_init(dispatch_fn *dflt)
47a04a10f8SKris Kennaway {
48a04a10f8SKris Kennaway 	int i;
49a04a10f8SKris Kennaway 	for (i = 0; i < DISPATCH_MAX; i++)
50a04a10f8SKris Kennaway 		dispatch[i] = dflt;
51a04a10f8SKris Kennaway }
52a04a10f8SKris Kennaway void
53a04a10f8SKris Kennaway dispatch_set(int type, dispatch_fn *fn)
54a04a10f8SKris Kennaway {
55a04a10f8SKris Kennaway 	dispatch[type] = fn;
56a04a10f8SKris Kennaway }
57a04a10f8SKris Kennaway void
58a04a10f8SKris Kennaway dispatch_run(int mode, int *done)
59a04a10f8SKris Kennaway {
60a04a10f8SKris Kennaway 	for (;;) {
61a04a10f8SKris Kennaway 		int plen;
62a04a10f8SKris Kennaway 		int type;
63a04a10f8SKris Kennaway 
64a04a10f8SKris Kennaway 		if (mode == DISPATCH_BLOCK) {
65a04a10f8SKris Kennaway 			type = packet_read(&plen);
66a04a10f8SKris Kennaway 		} else {
67a04a10f8SKris Kennaway 			type = packet_read_poll(&plen);
68a04a10f8SKris Kennaway 			if (type == SSH_MSG_NONE)
69a04a10f8SKris Kennaway 				return;
70a04a10f8SKris Kennaway 		}
71a04a10f8SKris Kennaway 		if (type > 0 && type < DISPATCH_MAX && dispatch[type] != NULL)
72a04a10f8SKris Kennaway 			(*dispatch[type])(type, plen);
73a04a10f8SKris Kennaway 		else
74a04a10f8SKris Kennaway 			packet_disconnect("protocol error: rcvd type %d", type);
75a04a10f8SKris Kennaway 		if (done != NULL && *done)
76a04a10f8SKris Kennaway 			return;
77a04a10f8SKris Kennaway 	}
78a04a10f8SKris Kennaway }
79