ng_echo.c revision 52752
1139823Simp
221259Swollman/*
321259Swollman * ng_echo.c
421259Swollman *
521259Swollman * Copyright (c) 1996-1999 Whistle Communications, Inc.
621259Swollman * All rights reserved.
721259Swollman *
821259Swollman * Subject to the following obligations and disclaimer of warranty, use and
921259Swollman * redistribution of this software, in source or object code forms, with or
1021259Swollman * without modifications are expressly permitted by Whistle Communications;
1121259Swollman * provided, however, that:
1221259Swollman * 1. Any and all reproductions of the source or object code must include the
1321259Swollman *    copyright notice above and the following disclaimer of warranties; and
1421259Swollman * 2. No rights are granted, in any manner or form, to use Whistle
1521259Swollman *    Communications, Inc. trademarks, including the mark "WHISTLE
1621259Swollman *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
1721259Swollman *    such appears in the above copyright notice or in the software.
1821259Swollman *
1921259Swollman * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
2021259Swollman * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
2121259Swollman * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
2221259Swollman * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
2321259Swollman * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
2421259Swollman * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
2521259Swollman * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
2621259Swollman * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
2721259Swollman * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
2821259Swollman * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
2921259Swollman * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
3050477Speter * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
3121259Swollman * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
3221259Swollman * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3321259Swollman * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3421259Swollman * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
3521259Swollman * OF SUCH DAMAGE.
3621259Swollman *
3721259Swollman * Author: Julian Elisher <julian@whistle.com>
3821259Swollman *
3921259Swollman * $FreeBSD: head/sys/netgraph/ng_echo.c 52752 1999-11-01 10:00:40Z julian $
4021259Swollman * $Whistle: ng_echo.c,v 1.13 1999/11/01 09:24:51 julian Exp $
4121259Swollman */
4221259Swollman
4321259Swollman/*
4421259Swollman * Netgraph "echo" node
4521259Swollman *
4621259Swollman * This node simply bounces data and messages back to whence they came.
4721259Swollman */
4821259Swollman
4921259Swollman#include <sys/param.h>
5021259Swollman#include <sys/systm.h>
51108533Sschweikh#include <sys/errno.h>
5221259Swollman#include <sys/kernel.h>
5321259Swollman#include <sys/malloc.h>
5421259Swollman#include <sys/mbuf.h>
5521259Swollman#include <sys/syslog.h>
56108533Sschweikh#include <netgraph/ng_message.h>
5721259Swollman#include <netgraph/netgraph.h>
5821259Swollman#include <netgraph/ng_echo.h>
5921259Swollman
6021259Swollman/* Netgraph methods */
6121259Swollmanstatic ng_rcvmsg_t	nge_rcvmsg;
6221259Swollmanstatic ng_rcvdata_t	nge_rcvdata;
6321259Swollmanstatic ng_disconnect_t	nge_disconnect;
6421259Swollman
6521259Swollman/* Netgraph type */
6683366Sjulianstatic struct ng_type typestruct = {
6721259Swollman	NG_VERSION,
6885074Sru	NG_ECHO_NODE_TYPE,
6921259Swollman	NULL,
7021259Swollman	NULL,
71142215Sglebius	nge_rcvmsg,
72228571Sglebius	NULL,
73155051Sglebius	NULL,
74191148Skmacy	NULL,
75193731Szec	NULL,
7621259Swollman	nge_rcvdata,
7721259Swollman	nge_rcvdata,
7821259Swollman	nge_disconnect
7921259Swollman};
8069224SjlemonNETGRAPH_INIT(echo, &typestruct);
8169152Sjlemon
82126264Smlaier/*
83186207Skmacy * Receive control message. We just bounce it back as a reply.
84195699Srwatson */
8569224Sjlemonstatic int
8674914Sjhbnge_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr,
8774914Sjhb	   struct ng_mesg **rptr)
88186199Skmacy{
89196481Srwatson	if (rptr) {
9083130Sjlemon		msg->header.flags |= NGF_RESP;
91132712Srwatson		*rptr = msg;
9269152Sjlemon	} else {
93121816Sbrooks		FREE(msg, M_NETGRAPH);
94121816Sbrooks	}
95130416Smlaier	return (0);
96130416Smlaier}
9760938Sjake
9860938Sjake/*
9972084Sphk * Receive data
100159781Smlaier */
10121259Swollmanstatic int
102240112Smelifaronge_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
103240099Smelifaro{
104240099Smelifaro	int error = 0;
105240112Smelifaro
106240099Smelifaro	NG_SEND_DATA(error, hook, m, meta);
10721259Swollman	return (error);
10821259Swollman}
10921259Swollman
11021259Swollman/*
11121259Swollman * Removal of the last link destroys the nodeo
11221259Swollman */
11321259Swollmanstatic int
11421259Swollmannge_disconnect(hook_p hook)
11521259Swollman{
11669152Sjlemon	if (hook->node->numhooks == 0)
11721259Swollman		ng_rmnode(hook->node);
11821259Swollman	return (0);
11921259Swollman}
12021259Swollman
12121259Swollman