xref: /freebsd/lib/libnetgraph/msg.c (revision 4cf49a43559ed9fdad601bdcccd2c55963008675)
1 
2 /*
3  * msg.c
4  *
5  * Copyright (c) 1996-1999 Whistle Communications, Inc.
6  * All rights reserved.
7  *
8  * Subject to the following obligations and disclaimer of warranty, use and
9  * redistribution of this software, in source or object code forms, with or
10  * without modifications are expressly permitted by Whistle Communications;
11  * provided, however, that:
12  * 1. Any and all reproductions of the source or object code must include the
13  *    copyright notice above and the following disclaimer of warranties; and
14  * 2. No rights are granted, in any manner or form, to use Whistle
15  *    Communications, Inc. trademarks, including the mark "WHISTLE
16  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17  *    such appears in the above copyright notice or in the software.
18  *
19  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35  * OF SUCH DAMAGE.
36  *
37  * Author: Archie Cobbs <archie@whistle.com>
38  *
39  * $FreeBSD$
40  * $Whistle: msg.c,v 1.9 1999/01/20 00:57:23 archie Exp $
41  */
42 
43 #include <sys/types.h>
44 #include <stdarg.h>
45 #include <netgraph/ng_message.h>
46 #include <netgraph/ng_socket.h>
47 
48 #include "netgraph.h"
49 #include "internal.h"
50 
51 /* Next message token value */
52 static int	gMsgId;
53 
54 /* For delivering both messages and replies */
55 static int	NgDeliverMsg(int cs, const char *path,
56 		  const struct ng_mesg *hdr, const void *args, size_t arglen);
57 
58 /*
59  * Send a message to a node using control socket node "cs".
60  * Returns -1 if error and sets errno appropriately.
61  * If successful, returns the message ID (token) used.
62  */
63 int
64 NgSendMsg(int cs, const char *path,
65 	  int cookie, int cmd, const void *args, size_t arglen)
66 {
67 	struct ng_mesg msg;
68 
69 	/* Prepare message header */
70 	memset(&msg, 0, sizeof(msg));
71 	msg.header.version = NG_VERSION;
72 	msg.header.typecookie = cookie;
73 	msg.header.token = ++gMsgId;
74 	msg.header.flags = NGF_ORIG;
75 	msg.header.cmd = cmd;
76 	snprintf(msg.header.cmdstr, NG_CMDSTRLEN + 1, "cmd%d", cmd);
77 
78 	/* Deliver message */
79 	if (NgDeliverMsg(cs, path, &msg, args, arglen) < 0)
80 		return (-1);
81 	return(gMsgId);
82 }
83 
84 /*
85  * Send a message that is a reply to a previously received message.
86  * Returns -1 and sets errno on error, otherwise returns zero.
87  */
88 int
89 NgSendReplyMsg(int cs, const char *path,
90 	const struct ng_mesg *msg, const void *args, size_t arglen)
91 {
92 	struct ng_mesg rep;
93 
94 	/* Prepare message header */
95 	rep = *msg;
96 	rep.header.flags = NGF_RESP;
97 
98 	/* Deliver message */
99 	return (NgDeliverMsg(cs, path, &rep, args, arglen));
100 }
101 
102 /*
103  * Send a message to a node using control socket node "cs".
104  * Returns -1 if error and sets errno appropriately, otherwise zero.
105  */
106 static int
107 NgDeliverMsg(int cs, const char *path,
108 	const struct ng_mesg *hdr, const void *args, size_t arglen)
109 {
110 	u_char sgbuf[NG_PATHLEN + 3];
111 	struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf;
112 	u_char *buf = NULL;
113 	struct ng_mesg *msg;
114 	int errnosv = 0;
115 	int rtn = 0;
116 
117 	/* Sanity check */
118 	if (args == NULL)
119 		arglen = 0;
120 
121 	/* Get buffer */
122 	if ((buf = malloc(sizeof(*msg) + arglen)) == NULL) {
123 		errnosv = errno;
124 		if (_gNgDebugLevel >= 1)
125 			NGLOG("malloc");
126 		rtn = -1;
127 		goto done;
128 	}
129 	msg = (struct ng_mesg *) buf;
130 
131 	/* Finalize message */
132 	*msg = *hdr;
133 	msg->header.arglen = arglen;
134 	memcpy(msg->data, args, arglen);
135 
136 	/* Prepare socket address */
137 	sg->sg_family = AF_NETGRAPH;
138 	snprintf(sg->sg_data, NG_PATHLEN + 1, "%s", path);
139 	sg->sg_len = strlen(sg->sg_data) + 3;
140 
141 	/* Debugging */
142 	if (_gNgDebugLevel >= 2) {
143 		NGLOGX("SENDING %s:",
144 		    (msg->header.flags & NGF_RESP) ? "RESPONSE" : "MESSAGE");
145 		_NgDebugSockaddr(sg);
146 		_NgDebugMsg(msg);
147 	}
148 
149 	/* Send it */
150 	if (sendto(cs, msg, sizeof(*msg) + arglen,
151 		   0, (struct sockaddr *) sg, sg->sg_len) < 0) {
152 		errnosv = errno;
153 		if (_gNgDebugLevel >= 1)
154 			NGLOG("sendto(%s)", sg->sg_data);
155 		rtn = -1;
156 		goto done;
157 	}
158 
159 done:
160 	/* Done */
161 	free(buf);		/* OK if buf is NULL */
162 	errno = errnosv;
163 	return (rtn);
164 }
165 
166 /*
167  * Receive a control message.
168  *
169  * On error, this returns -1 and sets errno.
170  * Otherwise, it returns the length of the received reply.
171  */
172 int
173 NgRecvMsg(int cs, struct ng_mesg *rep, size_t replen, char *path)
174 {
175 	u_char sgbuf[NG_PATHLEN + sizeof(struct sockaddr_ng)];
176 	struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf;
177 	int len, sglen = sizeof(sgbuf);
178 	int errnosv;
179 
180 	/* Read reply */
181 	len = recvfrom(cs, rep, replen, 0, (struct sockaddr *) sg, &sglen);
182 	if (len < 0) {
183 		errnosv = errno;
184 		if (_gNgDebugLevel >= 1)
185 			NGLOG("recvfrom");
186 		goto errout;
187 	}
188 	if (path != NULL)
189 		snprintf(path, NG_PATHLEN + 1, "%s", sg->sg_data);
190 
191 	/* Debugging */
192 	if (_gNgDebugLevel >= 2) {
193 		NGLOGX("RECEIVED %s:",
194 		    (rep->header.flags & NGF_RESP) ? "RESPONSE" : "MESSAGE");
195 		_NgDebugSockaddr(sg);
196 		_NgDebugMsg(rep);
197 	}
198 
199 	/* Done */
200 	return (len);
201 
202 errout:
203 	errno = errnosv;
204 	return (-1);
205 }
206 
207