1*a1fd9364Slogwang /*
2*a1fd9364Slogwang * ng_echo.c
3*a1fd9364Slogwang */
4*a1fd9364Slogwang
5*a1fd9364Slogwang /*-
6*a1fd9364Slogwang * Copyright (c) 1996-1999 Whistle Communications, Inc.
7*a1fd9364Slogwang * All rights reserved.
8*a1fd9364Slogwang *
9*a1fd9364Slogwang * Subject to the following obligations and disclaimer of warranty, use and
10*a1fd9364Slogwang * redistribution of this software, in source or object code forms, with or
11*a1fd9364Slogwang * without modifications are expressly permitted by Whistle Communications;
12*a1fd9364Slogwang * provided, however, that:
13*a1fd9364Slogwang * 1. Any and all reproductions of the source or object code must include the
14*a1fd9364Slogwang * copyright notice above and the following disclaimer of warranties; and
15*a1fd9364Slogwang * 2. No rights are granted, in any manner or form, to use Whistle
16*a1fd9364Slogwang * Communications, Inc. trademarks, including the mark "WHISTLE
17*a1fd9364Slogwang * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18*a1fd9364Slogwang * such appears in the above copyright notice or in the software.
19*a1fd9364Slogwang *
20*a1fd9364Slogwang * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21*a1fd9364Slogwang * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22*a1fd9364Slogwang * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23*a1fd9364Slogwang * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24*a1fd9364Slogwang * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25*a1fd9364Slogwang * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26*a1fd9364Slogwang * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27*a1fd9364Slogwang * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28*a1fd9364Slogwang * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29*a1fd9364Slogwang * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30*a1fd9364Slogwang * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31*a1fd9364Slogwang * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32*a1fd9364Slogwang * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33*a1fd9364Slogwang * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34*a1fd9364Slogwang * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35*a1fd9364Slogwang * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36*a1fd9364Slogwang * OF SUCH DAMAGE.
37*a1fd9364Slogwang *
38*a1fd9364Slogwang * Author: Julian Elisher <[email protected]>
39*a1fd9364Slogwang *
40*a1fd9364Slogwang * $FreeBSD$
41*a1fd9364Slogwang * $Whistle: ng_echo.c,v 1.13 1999/11/01 09:24:51 julian Exp $
42*a1fd9364Slogwang */
43*a1fd9364Slogwang
44*a1fd9364Slogwang /*
45*a1fd9364Slogwang * Netgraph "echo" node
46*a1fd9364Slogwang *
47*a1fd9364Slogwang * This node simply bounces data and messages back to whence they came.
48*a1fd9364Slogwang */
49*a1fd9364Slogwang
50*a1fd9364Slogwang #include <sys/param.h>
51*a1fd9364Slogwang #include <sys/systm.h>
52*a1fd9364Slogwang #include <sys/kernel.h>
53*a1fd9364Slogwang #include <sys/malloc.h>
54*a1fd9364Slogwang #include <sys/mbuf.h>
55*a1fd9364Slogwang #include <netgraph/ng_message.h>
56*a1fd9364Slogwang #include <netgraph/netgraph.h>
57*a1fd9364Slogwang #include <netgraph/ng_echo.h>
58*a1fd9364Slogwang
59*a1fd9364Slogwang /* Netgraph methods */
60*a1fd9364Slogwang static ng_constructor_t nge_cons;
61*a1fd9364Slogwang static ng_rcvmsg_t nge_rcvmsg;
62*a1fd9364Slogwang static ng_rcvdata_t nge_rcvdata;
63*a1fd9364Slogwang static ng_disconnect_t nge_disconnect;
64*a1fd9364Slogwang
65*a1fd9364Slogwang /* Netgraph type */
66*a1fd9364Slogwang static struct ng_type typestruct = {
67*a1fd9364Slogwang .version = NG_ABI_VERSION,
68*a1fd9364Slogwang .name = NG_ECHO_NODE_TYPE,
69*a1fd9364Slogwang .constructor = nge_cons,
70*a1fd9364Slogwang .rcvmsg = nge_rcvmsg,
71*a1fd9364Slogwang .rcvdata = nge_rcvdata,
72*a1fd9364Slogwang .disconnect = nge_disconnect,
73*a1fd9364Slogwang };
74*a1fd9364Slogwang NETGRAPH_INIT(echo, &typestruct);
75*a1fd9364Slogwang
76*a1fd9364Slogwang static int
nge_cons(node_p node)77*a1fd9364Slogwang nge_cons(node_p node)
78*a1fd9364Slogwang {
79*a1fd9364Slogwang return (0);
80*a1fd9364Slogwang }
81*a1fd9364Slogwang
82*a1fd9364Slogwang /*
83*a1fd9364Slogwang * Receive control message. We just bounce it back as a reply.
84*a1fd9364Slogwang */
85*a1fd9364Slogwang static int
nge_rcvmsg(node_p node,item_p item,hook_p lasthook)86*a1fd9364Slogwang nge_rcvmsg(node_p node, item_p item, hook_p lasthook)
87*a1fd9364Slogwang {
88*a1fd9364Slogwang struct ng_mesg *msg;
89*a1fd9364Slogwang int error = 0;
90*a1fd9364Slogwang
91*a1fd9364Slogwang NGI_GET_MSG(item, msg);
92*a1fd9364Slogwang msg->header.flags |= NGF_RESP;
93*a1fd9364Slogwang NG_RESPOND_MSG(error, node, item, msg);
94*a1fd9364Slogwang return (error);
95*a1fd9364Slogwang }
96*a1fd9364Slogwang
97*a1fd9364Slogwang /*
98*a1fd9364Slogwang * Receive data
99*a1fd9364Slogwang */
100*a1fd9364Slogwang static int
nge_rcvdata(hook_p hook,item_p item)101*a1fd9364Slogwang nge_rcvdata(hook_p hook, item_p item)
102*a1fd9364Slogwang {
103*a1fd9364Slogwang int error;
104*a1fd9364Slogwang
105*a1fd9364Slogwang NG_FWD_ITEM_HOOK(error, item, hook);
106*a1fd9364Slogwang return (error);
107*a1fd9364Slogwang }
108*a1fd9364Slogwang
109*a1fd9364Slogwang /*
110*a1fd9364Slogwang * Removal of the last link destroys the nodeo
111*a1fd9364Slogwang */
112*a1fd9364Slogwang static int
nge_disconnect(hook_p hook)113*a1fd9364Slogwang nge_disconnect(hook_p hook)
114*a1fd9364Slogwang {
115*a1fd9364Slogwang if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
116*a1fd9364Slogwang && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) {
117*a1fd9364Slogwang ng_rmnode_self(NG_HOOK_NODE(hook));
118*a1fd9364Slogwang }
119*a1fd9364Slogwang return (0);
120*a1fd9364Slogwang }
121