xref: /aosp_15_r20/external/libpcap/rpcap-protocol.c (revision 8b26181f966a6af5cf6981a6f474313de533bb28)
1*8b26181fSAndroid Build Coastguard Worker /*
2*8b26181fSAndroid Build Coastguard Worker  * Copyright (c) 2002 - 2005 NetGroup, Politecnico di Torino (Italy)
3*8b26181fSAndroid Build Coastguard Worker  * Copyright (c) 2005 - 2008 CACE Technologies, Davis (California)
4*8b26181fSAndroid Build Coastguard Worker  * All rights reserved.
5*8b26181fSAndroid Build Coastguard Worker  *
6*8b26181fSAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
7*8b26181fSAndroid Build Coastguard Worker  * modification, are permitted provided that the following conditions
8*8b26181fSAndroid Build Coastguard Worker  * are met:
9*8b26181fSAndroid Build Coastguard Worker  *
10*8b26181fSAndroid Build Coastguard Worker  * 1. Redistributions of source code must retain the above copyright
11*8b26181fSAndroid Build Coastguard Worker  * notice, this list of conditions and the following disclaimer.
12*8b26181fSAndroid Build Coastguard Worker  * 2. Redistributions in binary form must reproduce the above copyright
13*8b26181fSAndroid Build Coastguard Worker  * notice, this list of conditions and the following disclaimer in the
14*8b26181fSAndroid Build Coastguard Worker  * documentation and/or other materials provided with the distribution.
15*8b26181fSAndroid Build Coastguard Worker  * 3. Neither the name of the Politecnico di Torino, CACE Technologies
16*8b26181fSAndroid Build Coastguard Worker  * nor the names of its contributors may be used to endorse or promote
17*8b26181fSAndroid Build Coastguard Worker  * products derived from this software without specific prior written
18*8b26181fSAndroid Build Coastguard Worker  * permission.
19*8b26181fSAndroid Build Coastguard Worker  *
20*8b26181fSAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*8b26181fSAndroid Build Coastguard Worker  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*8b26181fSAndroid Build Coastguard Worker  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23*8b26181fSAndroid Build Coastguard Worker  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24*8b26181fSAndroid Build Coastguard Worker  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25*8b26181fSAndroid Build Coastguard Worker  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26*8b26181fSAndroid Build Coastguard Worker  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27*8b26181fSAndroid Build Coastguard Worker  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28*8b26181fSAndroid Build Coastguard Worker  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29*8b26181fSAndroid Build Coastguard Worker  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30*8b26181fSAndroid Build Coastguard Worker  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31*8b26181fSAndroid Build Coastguard Worker  *
32*8b26181fSAndroid Build Coastguard Worker  */
33*8b26181fSAndroid Build Coastguard Worker 
34*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
35*8b26181fSAndroid Build Coastguard Worker #include <config.h>
36*8b26181fSAndroid Build Coastguard Worker #endif
37*8b26181fSAndroid Build Coastguard Worker 
38*8b26181fSAndroid Build Coastguard Worker #include <string.h>		/* for strlen(), ... */
39*8b26181fSAndroid Build Coastguard Worker #include <stdlib.h>		/* for malloc(), free(), ... */
40*8b26181fSAndroid Build Coastguard Worker #include <stdarg.h>		/* for functions with variable number of arguments */
41*8b26181fSAndroid Build Coastguard Worker #include <errno.h>		/* for the errno variable */
42*8b26181fSAndroid Build Coastguard Worker #include "sockutils.h"
43*8b26181fSAndroid Build Coastguard Worker #include "portability.h"
44*8b26181fSAndroid Build Coastguard Worker #include "rpcap-protocol.h"
45*8b26181fSAndroid Build Coastguard Worker #include <pcap/pcap.h>
46*8b26181fSAndroid Build Coastguard Worker 
47*8b26181fSAndroid Build Coastguard Worker /*
48*8b26181fSAndroid Build Coastguard Worker  * This file contains functions used both by the rpcap client and the
49*8b26181fSAndroid Build Coastguard Worker  * rpcap daemon.
50*8b26181fSAndroid Build Coastguard Worker  */
51*8b26181fSAndroid Build Coastguard Worker 
52*8b26181fSAndroid Build Coastguard Worker /*
53*8b26181fSAndroid Build Coastguard Worker  * This function sends a RPCAP error to our peer.
54*8b26181fSAndroid Build Coastguard Worker  *
55*8b26181fSAndroid Build Coastguard Worker  * It has to be called when the main program detects an error.
56*8b26181fSAndroid Build Coastguard Worker  * It will send to our peer the 'buffer' specified by the user.
57*8b26181fSAndroid Build Coastguard Worker  * This function *does not* request a RPCAP CLOSE connection. A CLOSE
58*8b26181fSAndroid Build Coastguard Worker  * command must be sent explicitly by the program, since we do not know
59*8b26181fSAndroid Build Coastguard Worker  * whether the error can be recovered in some way or if it is a
60*8b26181fSAndroid Build Coastguard Worker  * non-recoverable one.
61*8b26181fSAndroid Build Coastguard Worker  *
62*8b26181fSAndroid Build Coastguard Worker  * \param sock: the socket we are currently using.
63*8b26181fSAndroid Build Coastguard Worker  *
64*8b26181fSAndroid Build Coastguard Worker  * \param ssl: if compiled with openssl, the optional ssl handler to use with the above socket.
65*8b26181fSAndroid Build Coastguard Worker  *
66*8b26181fSAndroid Build Coastguard Worker  * \param ver: the protocol version we want to put in the reply.
67*8b26181fSAndroid Build Coastguard Worker  *
68*8b26181fSAndroid Build Coastguard Worker  * \param errcode: a integer which tells the other party the type of error
69*8b26181fSAndroid Build Coastguard Worker  * we had.
70*8b26181fSAndroid Build Coastguard Worker  *
71*8b26181fSAndroid Build Coastguard Worker  * \param error: an user-allocated (and '0' terminated) buffer that contains
72*8b26181fSAndroid Build Coastguard Worker  * the error description that has to be transmitted to our peer. The
73*8b26181fSAndroid Build Coastguard Worker  * error message cannot be longer than PCAP_ERRBUF_SIZE.
74*8b26181fSAndroid Build Coastguard Worker  *
75*8b26181fSAndroid Build Coastguard Worker  * \param errbuf: a pointer to a user-allocated buffer (of size
76*8b26181fSAndroid Build Coastguard Worker  * PCAP_ERRBUF_SIZE) that will contain the error message (in case there
77*8b26181fSAndroid Build Coastguard Worker  * is one). It could be network problem.
78*8b26181fSAndroid Build Coastguard Worker  *
79*8b26181fSAndroid Build Coastguard Worker  * \return '0' if everything is fine, '-1' if some errors occurred. The
80*8b26181fSAndroid Build Coastguard Worker  * error message is returned in the 'errbuf' variable.
81*8b26181fSAndroid Build Coastguard Worker  */
82*8b26181fSAndroid Build Coastguard Worker int
rpcap_senderror(SOCKET sock,SSL * ssl,uint8 ver,unsigned short errcode,const char * error,char * errbuf)83*8b26181fSAndroid Build Coastguard Worker rpcap_senderror(SOCKET sock, SSL *ssl, uint8 ver, unsigned short errcode, const char *error, char *errbuf)
84*8b26181fSAndroid Build Coastguard Worker {
85*8b26181fSAndroid Build Coastguard Worker 	char sendbuf[RPCAP_NETBUF_SIZE];	/* temporary buffer in which data to be sent is buffered */
86*8b26181fSAndroid Build Coastguard Worker 	int sendbufidx = 0;			/* index which keeps the number of bytes currently buffered */
87*8b26181fSAndroid Build Coastguard Worker 	uint16 length;
88*8b26181fSAndroid Build Coastguard Worker 
89*8b26181fSAndroid Build Coastguard Worker 	length = (uint16)strlen(error);
90*8b26181fSAndroid Build Coastguard Worker 
91*8b26181fSAndroid Build Coastguard Worker 	if (length > PCAP_ERRBUF_SIZE)
92*8b26181fSAndroid Build Coastguard Worker 		length = PCAP_ERRBUF_SIZE;
93*8b26181fSAndroid Build Coastguard Worker 
94*8b26181fSAndroid Build Coastguard Worker 	rpcap_createhdr((struct rpcap_header *) sendbuf, ver, RPCAP_MSG_ERROR, errcode, length);
95*8b26181fSAndroid Build Coastguard Worker 
96*8b26181fSAndroid Build Coastguard Worker 	if (sock_bufferize(NULL, sizeof(struct rpcap_header), NULL, &sendbufidx,
97*8b26181fSAndroid Build Coastguard Worker 		RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errbuf, PCAP_ERRBUF_SIZE))
98*8b26181fSAndroid Build Coastguard Worker 		return -1;
99*8b26181fSAndroid Build Coastguard Worker 
100*8b26181fSAndroid Build Coastguard Worker 	if (sock_bufferize(error, length, sendbuf, &sendbufidx,
101*8b26181fSAndroid Build Coastguard Worker 		RPCAP_NETBUF_SIZE, SOCKBUF_BUFFERIZE, errbuf, PCAP_ERRBUF_SIZE))
102*8b26181fSAndroid Build Coastguard Worker 		return -1;
103*8b26181fSAndroid Build Coastguard Worker 
104*8b26181fSAndroid Build Coastguard Worker 	if (sock_send(sock, ssl, sendbuf, sendbufidx, errbuf, PCAP_ERRBUF_SIZE) < 0)
105*8b26181fSAndroid Build Coastguard Worker 		return -1;
106*8b26181fSAndroid Build Coastguard Worker 
107*8b26181fSAndroid Build Coastguard Worker 	return 0;
108*8b26181fSAndroid Build Coastguard Worker }
109*8b26181fSAndroid Build Coastguard Worker 
110*8b26181fSAndroid Build Coastguard Worker /*
111*8b26181fSAndroid Build Coastguard Worker  * This function fills in a structure of type rpcap_header.
112*8b26181fSAndroid Build Coastguard Worker  *
113*8b26181fSAndroid Build Coastguard Worker  * It is provided just because the creation of an rpcap header is a common
114*8b26181fSAndroid Build Coastguard Worker  * task. It accepts all the values that appears into an rpcap_header, and
115*8b26181fSAndroid Build Coastguard Worker  * it puts them in place using the proper hton() calls.
116*8b26181fSAndroid Build Coastguard Worker  *
117*8b26181fSAndroid Build Coastguard Worker  * \param header: a pointer to a user-allocated buffer which will contain
118*8b26181fSAndroid Build Coastguard Worker  * the serialized header, ready to be sent on the network.
119*8b26181fSAndroid Build Coastguard Worker  *
120*8b26181fSAndroid Build Coastguard Worker  * \param ver: a value (in the host byte order) which will be placed into the
121*8b26181fSAndroid Build Coastguard Worker  * header.ver field and that represents the protocol version number of the
122*8b26181fSAndroid Build Coastguard Worker  * current message.
123*8b26181fSAndroid Build Coastguard Worker  *
124*8b26181fSAndroid Build Coastguard Worker  * \param type: a value (in the host byte order) which will be placed into the
125*8b26181fSAndroid Build Coastguard Worker  * header.type field and that represents the type of the current message.
126*8b26181fSAndroid Build Coastguard Worker  *
127*8b26181fSAndroid Build Coastguard Worker  * \param value: a value (in the host byte order) which will be placed into
128*8b26181fSAndroid Build Coastguard Worker  * the header.value field and that has a message-dependent meaning.
129*8b26181fSAndroid Build Coastguard Worker  *
130*8b26181fSAndroid Build Coastguard Worker  * \param length: a value (in the host by order) which will be placed into
131*8b26181fSAndroid Build Coastguard Worker  * the header.length field, representing the payload length of the message.
132*8b26181fSAndroid Build Coastguard Worker  *
133*8b26181fSAndroid Build Coastguard Worker  * \return Nothing. The serialized header is returned into the 'header'
134*8b26181fSAndroid Build Coastguard Worker  * variable.
135*8b26181fSAndroid Build Coastguard Worker  */
136*8b26181fSAndroid Build Coastguard Worker void
rpcap_createhdr(struct rpcap_header * header,uint8 ver,uint8 type,uint16 value,uint32 length)137*8b26181fSAndroid Build Coastguard Worker rpcap_createhdr(struct rpcap_header *header, uint8 ver, uint8 type, uint16 value, uint32 length)
138*8b26181fSAndroid Build Coastguard Worker {
139*8b26181fSAndroid Build Coastguard Worker 	memset(header, 0, sizeof(struct rpcap_header));
140*8b26181fSAndroid Build Coastguard Worker 
141*8b26181fSAndroid Build Coastguard Worker 	header->ver = ver;
142*8b26181fSAndroid Build Coastguard Worker 	header->type = type;
143*8b26181fSAndroid Build Coastguard Worker 	header->value = htons(value);
144*8b26181fSAndroid Build Coastguard Worker 	header->plen = htonl(length);
145*8b26181fSAndroid Build Coastguard Worker }
146*8b26181fSAndroid Build Coastguard Worker 
147*8b26181fSAndroid Build Coastguard Worker /*
148*8b26181fSAndroid Build Coastguard Worker  * Convert a message type to a string containing the type name.
149*8b26181fSAndroid Build Coastguard Worker  */
150*8b26181fSAndroid Build Coastguard Worker static const char *requests[] =
151*8b26181fSAndroid Build Coastguard Worker {
152*8b26181fSAndroid Build Coastguard Worker 	NULL,				/* not a valid message type */
153*8b26181fSAndroid Build Coastguard Worker 	"RPCAP_MSG_ERROR",
154*8b26181fSAndroid Build Coastguard Worker 	"RPCAP_MSG_FINDALLIF_REQ",
155*8b26181fSAndroid Build Coastguard Worker 	"RPCAP_MSG_OPEN_REQ",
156*8b26181fSAndroid Build Coastguard Worker 	"RPCAP_MSG_STARTCAP_REQ",
157*8b26181fSAndroid Build Coastguard Worker 	"RPCAP_MSG_UPDATEFILTER_REQ",
158*8b26181fSAndroid Build Coastguard Worker 	"RPCAP_MSG_CLOSE",
159*8b26181fSAndroid Build Coastguard Worker 	"RPCAP_MSG_PACKET",
160*8b26181fSAndroid Build Coastguard Worker 	"RPCAP_MSG_AUTH_REQ",
161*8b26181fSAndroid Build Coastguard Worker 	"RPCAP_MSG_STATS_REQ",
162*8b26181fSAndroid Build Coastguard Worker 	"RPCAP_MSG_ENDCAP_REQ",
163*8b26181fSAndroid Build Coastguard Worker 	"RPCAP_MSG_SETSAMPLING_REQ",
164*8b26181fSAndroid Build Coastguard Worker };
165*8b26181fSAndroid Build Coastguard Worker #define NUM_REQ_TYPES	(sizeof requests / sizeof requests[0])
166*8b26181fSAndroid Build Coastguard Worker 
167*8b26181fSAndroid Build Coastguard Worker static const char *replies[] =
168*8b26181fSAndroid Build Coastguard Worker {
169*8b26181fSAndroid Build Coastguard Worker 	NULL,
170*8b26181fSAndroid Build Coastguard Worker 	NULL,			/* this would be a reply to RPCAP_MSG_ERROR */
171*8b26181fSAndroid Build Coastguard Worker 	"RPCAP_MSG_FINDALLIF_REPLY",
172*8b26181fSAndroid Build Coastguard Worker 	"RPCAP_MSG_OPEN_REPLY",
173*8b26181fSAndroid Build Coastguard Worker 	"RPCAP_MSG_STARTCAP_REPLY",
174*8b26181fSAndroid Build Coastguard Worker 	"RPCAP_MSG_UPDATEFILTER_REPLY",
175*8b26181fSAndroid Build Coastguard Worker 	NULL,			/* this would be a reply to RPCAP_MSG_CLOSE */
176*8b26181fSAndroid Build Coastguard Worker 	NULL,			/* this would be a reply to RPCAP_MSG_PACKET */
177*8b26181fSAndroid Build Coastguard Worker 	"RPCAP_MSG_AUTH_REPLY",
178*8b26181fSAndroid Build Coastguard Worker 	"RPCAP_MSG_STATS_REPLY",
179*8b26181fSAndroid Build Coastguard Worker 	"RPCAP_MSG_ENDCAP_REPLY",
180*8b26181fSAndroid Build Coastguard Worker 	"RPCAP_MSG_SETSAMPLING_REPLY",
181*8b26181fSAndroid Build Coastguard Worker };
182*8b26181fSAndroid Build Coastguard Worker #define NUM_REPLY_TYPES	(sizeof replies / sizeof replies[0])
183*8b26181fSAndroid Build Coastguard Worker 
184*8b26181fSAndroid Build Coastguard Worker const char *
rpcap_msg_type_string(uint8 type)185*8b26181fSAndroid Build Coastguard Worker rpcap_msg_type_string(uint8 type)
186*8b26181fSAndroid Build Coastguard Worker {
187*8b26181fSAndroid Build Coastguard Worker 	if (type & RPCAP_MSG_IS_REPLY) {
188*8b26181fSAndroid Build Coastguard Worker 		type &= ~RPCAP_MSG_IS_REPLY;
189*8b26181fSAndroid Build Coastguard Worker 		if (type >= NUM_REPLY_TYPES)
190*8b26181fSAndroid Build Coastguard Worker 			return NULL;
191*8b26181fSAndroid Build Coastguard Worker 		return replies[type];
192*8b26181fSAndroid Build Coastguard Worker 	} else {
193*8b26181fSAndroid Build Coastguard Worker 		if (type >= NUM_REQ_TYPES)
194*8b26181fSAndroid Build Coastguard Worker 			return NULL;
195*8b26181fSAndroid Build Coastguard Worker 		return requests[type];
196*8b26181fSAndroid Build Coastguard Worker 	}
197*8b26181fSAndroid Build Coastguard Worker }
198