xref: /aosp_15_r20/external/libpcap/rpcapd/daemon.c (revision 8b26181f966a6af5cf6981a6f474313de533bb28)
1*8b26181fSAndroid Build Coastguard Worker /*
2*8b26181fSAndroid Build Coastguard Worker  * Copyright (c) 2002 - 2003
3*8b26181fSAndroid Build Coastguard Worker  * NetGroup, Politecnico di Torino (Italy)
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 nor the names of its
16*8b26181fSAndroid Build Coastguard Worker  * contributors may be used to endorse or promote products derived from
17*8b26181fSAndroid Build Coastguard Worker  * this software without specific prior written permission.
18*8b26181fSAndroid Build Coastguard Worker  *
19*8b26181fSAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20*8b26181fSAndroid Build Coastguard Worker  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21*8b26181fSAndroid Build Coastguard Worker  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22*8b26181fSAndroid Build Coastguard Worker  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23*8b26181fSAndroid Build Coastguard Worker  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24*8b26181fSAndroid Build Coastguard Worker  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25*8b26181fSAndroid Build Coastguard Worker  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26*8b26181fSAndroid Build Coastguard Worker  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27*8b26181fSAndroid Build Coastguard Worker  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28*8b26181fSAndroid Build Coastguard Worker  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29*8b26181fSAndroid Build Coastguard Worker  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30*8b26181fSAndroid Build Coastguard Worker  */
31*8b26181fSAndroid Build Coastguard Worker 
32*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
33*8b26181fSAndroid Build Coastguard Worker #include <config.h>
34*8b26181fSAndroid Build Coastguard Worker #endif
35*8b26181fSAndroid Build Coastguard Worker 
36*8b26181fSAndroid Build Coastguard Worker #include "ftmacros.h"
37*8b26181fSAndroid Build Coastguard Worker #include "varattrs.h"
38*8b26181fSAndroid Build Coastguard Worker 
39*8b26181fSAndroid Build Coastguard Worker #include <errno.h>		// for the errno variable
40*8b26181fSAndroid Build Coastguard Worker #include <stdlib.h>		// for malloc(), free(), ...
41*8b26181fSAndroid Build Coastguard Worker #include <string.h>		// for strlen(), ...
42*8b26181fSAndroid Build Coastguard Worker #include <limits.h>		// for INT_MAX
43*8b26181fSAndroid Build Coastguard Worker 
44*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
45*8b26181fSAndroid Build Coastguard Worker   #include <process.h>		// for threads
46*8b26181fSAndroid Build Coastguard Worker #else
47*8b26181fSAndroid Build Coastguard Worker   #include <unistd.h>
48*8b26181fSAndroid Build Coastguard Worker   #include <pthread.h>
49*8b26181fSAndroid Build Coastguard Worker   #include <signal.h>
50*8b26181fSAndroid Build Coastguard Worker   #include <sys/time.h>
51*8b26181fSAndroid Build Coastguard Worker   #include <sys/types.h>	// for select() and such
52*8b26181fSAndroid Build Coastguard Worker   #include <pwd.h>		// for password management
53*8b26181fSAndroid Build Coastguard Worker #endif
54*8b26181fSAndroid Build Coastguard Worker 
55*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_GETSPNAM
56*8b26181fSAndroid Build Coastguard Worker #include <shadow.h>		// for password management
57*8b26181fSAndroid Build Coastguard Worker #endif
58*8b26181fSAndroid Build Coastguard Worker 
59*8b26181fSAndroid Build Coastguard Worker #include <pcap.h>		// for libpcap/WinPcap calls
60*8b26181fSAndroid Build Coastguard Worker 
61*8b26181fSAndroid Build Coastguard Worker #include "fmtutils.h"
62*8b26181fSAndroid Build Coastguard Worker #include "sockutils.h"		// for socket calls
63*8b26181fSAndroid Build Coastguard Worker #include "portability.h"
64*8b26181fSAndroid Build Coastguard Worker #include "rpcap-protocol.h"
65*8b26181fSAndroid Build Coastguard Worker #include "daemon.h"
66*8b26181fSAndroid Build Coastguard Worker #include "log.h"
67*8b26181fSAndroid Build Coastguard Worker 
68*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OPENSSL
69*8b26181fSAndroid Build Coastguard Worker #include <openssl/ssl.h>
70*8b26181fSAndroid Build Coastguard Worker #include "sslutils.h"
71*8b26181fSAndroid Build Coastguard Worker #endif
72*8b26181fSAndroid Build Coastguard Worker 
73*8b26181fSAndroid Build Coastguard Worker //
74*8b26181fSAndroid Build Coastguard Worker // Timeout, in seconds, when we're waiting for a client to send us an
75*8b26181fSAndroid Build Coastguard Worker // authentication request; if they don't send us a request within that
76*8b26181fSAndroid Build Coastguard Worker // interval, we drop the connection, so we don't stay stuck forever.
77*8b26181fSAndroid Build Coastguard Worker //
78*8b26181fSAndroid Build Coastguard Worker #define RPCAP_TIMEOUT_INIT 90
79*8b26181fSAndroid Build Coastguard Worker 
80*8b26181fSAndroid Build Coastguard Worker //
81*8b26181fSAndroid Build Coastguard Worker // Timeout, in seconds, when we're waiting for an authenticated client
82*8b26181fSAndroid Build Coastguard Worker // to send us a request, if a capture isn't in progress; if they don't
83*8b26181fSAndroid Build Coastguard Worker // send us a request within that interval, we drop the connection, so
84*8b26181fSAndroid Build Coastguard Worker // we don't stay stuck forever.
85*8b26181fSAndroid Build Coastguard Worker //
86*8b26181fSAndroid Build Coastguard Worker #define RPCAP_TIMEOUT_RUNTIME 180
87*8b26181fSAndroid Build Coastguard Worker 
88*8b26181fSAndroid Build Coastguard Worker //
89*8b26181fSAndroid Build Coastguard Worker // Time, in seconds, that we wait after a failed authentication attempt
90*8b26181fSAndroid Build Coastguard Worker // before processing the next request; this prevents a client from
91*8b26181fSAndroid Build Coastguard Worker // rapidly trying different accounts or passwords.
92*8b26181fSAndroid Build Coastguard Worker //
93*8b26181fSAndroid Build Coastguard Worker #define RPCAP_SUSPEND_WRONGAUTH 1
94*8b26181fSAndroid Build Coastguard Worker 
95*8b26181fSAndroid Build Coastguard Worker // Parameters for the service loop.
96*8b26181fSAndroid Build Coastguard Worker struct daemon_slpars
97*8b26181fSAndroid Build Coastguard Worker {
98*8b26181fSAndroid Build Coastguard Worker 	SOCKET sockctrl;	//!< SOCKET ID of the control connection
99*8b26181fSAndroid Build Coastguard Worker 	SSL *ssl;		//!< Optional SSL handler for the controlling sockets
100*8b26181fSAndroid Build Coastguard Worker 	int isactive;		//!< Not null if the daemon has to run in active mode
101*8b26181fSAndroid Build Coastguard Worker 	int nullAuthAllowed;	//!< '1' if we permit NULL authentication, '0' otherwise
102*8b26181fSAndroid Build Coastguard Worker };
103*8b26181fSAndroid Build Coastguard Worker 
104*8b26181fSAndroid Build Coastguard Worker //
105*8b26181fSAndroid Build Coastguard Worker // Data for a session managed by a thread.
106*8b26181fSAndroid Build Coastguard Worker // It includes both a Boolean indicating whether we *have* a thread,
107*8b26181fSAndroid Build Coastguard Worker // and a platform-dependent (UN*X vs. Windows) identifier for the
108*8b26181fSAndroid Build Coastguard Worker // thread; on Windows, we could use an invalid handle to indicate
109*8b26181fSAndroid Build Coastguard Worker // that we don't have a thread, but there *is* no portable "no thread"
110*8b26181fSAndroid Build Coastguard Worker // value for a pthread_t on UN*X.
111*8b26181fSAndroid Build Coastguard Worker //
112*8b26181fSAndroid Build Coastguard Worker struct session {
113*8b26181fSAndroid Build Coastguard Worker 	SOCKET sockctrl;
114*8b26181fSAndroid Build Coastguard Worker 	SOCKET sockdata;
115*8b26181fSAndroid Build Coastguard Worker 	SSL *ctrl_ssl, *data_ssl; // optional SSL handlers for sockctrl and sockdata.
116*8b26181fSAndroid Build Coastguard Worker 	uint8 protocol_version;
117*8b26181fSAndroid Build Coastguard Worker 	pcap_t *fp;
118*8b26181fSAndroid Build Coastguard Worker 	unsigned int TotCapt;
119*8b26181fSAndroid Build Coastguard Worker 	int	have_thread;
120*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
121*8b26181fSAndroid Build Coastguard Worker 	HANDLE thread;
122*8b26181fSAndroid Build Coastguard Worker #else
123*8b26181fSAndroid Build Coastguard Worker 	pthread_t thread;
124*8b26181fSAndroid Build Coastguard Worker #endif
125*8b26181fSAndroid Build Coastguard Worker };
126*8b26181fSAndroid Build Coastguard Worker 
127*8b26181fSAndroid Build Coastguard Worker // Locally defined functions
128*8b26181fSAndroid Build Coastguard Worker static int daemon_msg_err(SOCKET sockctrl, SSL *, uint32 plen);
129*8b26181fSAndroid Build Coastguard Worker static int daemon_msg_auth_req(struct daemon_slpars *pars, uint32 plen);
130*8b26181fSAndroid Build Coastguard Worker static int daemon_AuthUserPwd(char *username, char *password, char *errbuf);
131*8b26181fSAndroid Build Coastguard Worker 
132*8b26181fSAndroid Build Coastguard Worker static int daemon_msg_findallif_req(uint8 ver, struct daemon_slpars *pars,
133*8b26181fSAndroid Build Coastguard Worker     uint32 plen);
134*8b26181fSAndroid Build Coastguard Worker 
135*8b26181fSAndroid Build Coastguard Worker static int daemon_msg_open_req(uint8 ver, struct daemon_slpars *pars,
136*8b26181fSAndroid Build Coastguard Worker     uint32 plen, char *source, size_t sourcelen);
137*8b26181fSAndroid Build Coastguard Worker static int daemon_msg_startcap_req(uint8 ver, struct daemon_slpars *pars,
138*8b26181fSAndroid Build Coastguard Worker     uint32 plen, char *source, struct session **sessionp,
139*8b26181fSAndroid Build Coastguard Worker     struct rpcap_sampling *samp_param, int uses_ssl);
140*8b26181fSAndroid Build Coastguard Worker static int daemon_msg_endcap_req(uint8 ver, struct daemon_slpars *pars,
141*8b26181fSAndroid Build Coastguard Worker     struct session *session);
142*8b26181fSAndroid Build Coastguard Worker 
143*8b26181fSAndroid Build Coastguard Worker static int daemon_msg_updatefilter_req(uint8 ver, struct daemon_slpars *pars,
144*8b26181fSAndroid Build Coastguard Worker     struct session *session, uint32 plen);
145*8b26181fSAndroid Build Coastguard Worker static int daemon_unpackapplyfilter(SOCKET sockctrl, SSL *, struct session *session, uint32 *plenp, char *errbuf);
146*8b26181fSAndroid Build Coastguard Worker 
147*8b26181fSAndroid Build Coastguard Worker static int daemon_msg_stats_req(uint8 ver, struct daemon_slpars *pars,
148*8b26181fSAndroid Build Coastguard Worker     struct session *session, uint32 plen, struct pcap_stat *stats,
149*8b26181fSAndroid Build Coastguard Worker     unsigned int svrcapt);
150*8b26181fSAndroid Build Coastguard Worker 
151*8b26181fSAndroid Build Coastguard Worker static int daemon_msg_setsampling_req(uint8 ver, struct daemon_slpars *pars,
152*8b26181fSAndroid Build Coastguard Worker     uint32 plen, struct rpcap_sampling *samp_param);
153*8b26181fSAndroid Build Coastguard Worker 
154*8b26181fSAndroid Build Coastguard Worker static void daemon_seraddr(struct sockaddr_storage *sockaddrin, struct rpcap_sockaddr *sockaddrout);
155*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
156*8b26181fSAndroid Build Coastguard Worker static unsigned __stdcall daemon_thrdatamain(void *ptr);
157*8b26181fSAndroid Build Coastguard Worker #else
158*8b26181fSAndroid Build Coastguard Worker static void *daemon_thrdatamain(void *ptr);
159*8b26181fSAndroid Build Coastguard Worker static void noop_handler(int sign);
160*8b26181fSAndroid Build Coastguard Worker #endif
161*8b26181fSAndroid Build Coastguard Worker 
162*8b26181fSAndroid Build Coastguard Worker static int rpcapd_recv_msg_header(SOCKET sock, SSL *, struct rpcap_header *headerp);
163*8b26181fSAndroid Build Coastguard Worker static int rpcapd_recv(SOCKET sock, SSL *, char *buffer, size_t toread, uint32 *plen, char *errmsgbuf);
164*8b26181fSAndroid Build Coastguard Worker static int rpcapd_discard(SOCKET sock, SSL *, uint32 len);
165*8b26181fSAndroid Build Coastguard Worker static void session_close(struct session *);
166*8b26181fSAndroid Build Coastguard Worker 
167*8b26181fSAndroid Build Coastguard Worker //
168*8b26181fSAndroid Build Coastguard Worker // TLS record layer header; used when processing the first message from
169*8b26181fSAndroid Build Coastguard Worker // the client, in case we aren't doing TLS but they are.
170*8b26181fSAndroid Build Coastguard Worker //
171*8b26181fSAndroid Build Coastguard Worker struct tls_record_header {
172*8b26181fSAndroid Build Coastguard Worker 	uint8 type;		// ContentType - will be 22, for Handshake
173*8b26181fSAndroid Build Coastguard Worker 	uint8 version_major;	// TLS protocol major version
174*8b26181fSAndroid Build Coastguard Worker 	uint8 version_injor;	// TLS protocol minor version
175*8b26181fSAndroid Build Coastguard Worker 	// This is *not* aligned on a 2-byte boundary; we just
176*8b26181fSAndroid Build Coastguard Worker 	// declare it as two bytes.  Don't assume any particular
177*8b26181fSAndroid Build Coastguard Worker 	// compiler's mechanism for saying "packed"!
178*8b26181fSAndroid Build Coastguard Worker 	uint8 length_hi;	// Upper 8 bits of payload length
179*8b26181fSAndroid Build Coastguard Worker 	uint8 length_lo;	// Low 8 bits of payload length
180*8b26181fSAndroid Build Coastguard Worker };
181*8b26181fSAndroid Build Coastguard Worker 
182*8b26181fSAndroid Build Coastguard Worker #define TLS_RECORD_HEADER_LEN	5	// Don't use sizeof in case it's padded
183*8b26181fSAndroid Build Coastguard Worker 
184*8b26181fSAndroid Build Coastguard Worker #define TLS_RECORD_TYPE_ALERT		21
185*8b26181fSAndroid Build Coastguard Worker #define TLS_RECORD_TYPE_HANDSHAKE	22
186*8b26181fSAndroid Build Coastguard Worker 
187*8b26181fSAndroid Build Coastguard Worker //
188*8b26181fSAndroid Build Coastguard Worker // TLS alert message.
189*8b26181fSAndroid Build Coastguard Worker //
190*8b26181fSAndroid Build Coastguard Worker struct tls_alert {
191*8b26181fSAndroid Build Coastguard Worker 	uint8 alert_level;
192*8b26181fSAndroid Build Coastguard Worker 	uint8 alert_description;
193*8b26181fSAndroid Build Coastguard Worker };
194*8b26181fSAndroid Build Coastguard Worker 
195*8b26181fSAndroid Build Coastguard Worker #define TLS_ALERT_LEN			2
196*8b26181fSAndroid Build Coastguard Worker 
197*8b26181fSAndroid Build Coastguard Worker #define TLS_ALERT_LEVEL_FATAL		2
198*8b26181fSAndroid Build Coastguard Worker #define TLS_ALERT_HANDSHAKE_FAILURE	40
199*8b26181fSAndroid Build Coastguard Worker 
200*8b26181fSAndroid Build Coastguard Worker static int is_url(const char *source);
201*8b26181fSAndroid Build Coastguard Worker 
202*8b26181fSAndroid Build Coastguard Worker /*
203*8b26181fSAndroid Build Coastguard Worker  * Maximum sizes for fixed-bit-width values.
204*8b26181fSAndroid Build Coastguard Worker  */
205*8b26181fSAndroid Build Coastguard Worker #ifndef UINT16_MAX
206*8b26181fSAndroid Build Coastguard Worker #define UINT16_MAX	65535U
207*8b26181fSAndroid Build Coastguard Worker #endif
208*8b26181fSAndroid Build Coastguard Worker 
209*8b26181fSAndroid Build Coastguard Worker #ifndef UINT32_MAX
210*8b26181fSAndroid Build Coastguard Worker #define UINT32_MAX	4294967295U
211*8b26181fSAndroid Build Coastguard Worker #endif
212*8b26181fSAndroid Build Coastguard Worker 
213*8b26181fSAndroid Build Coastguard Worker int
daemon_serviceloop(SOCKET sockctrl,int isactive,char * passiveClients,int nullAuthAllowed,int uses_ssl)214*8b26181fSAndroid Build Coastguard Worker daemon_serviceloop(SOCKET sockctrl, int isactive, char *passiveClients,
215*8b26181fSAndroid Build Coastguard Worker     int nullAuthAllowed, int uses_ssl)
216*8b26181fSAndroid Build Coastguard Worker {
217*8b26181fSAndroid Build Coastguard Worker 	uint8 first_octet;
218*8b26181fSAndroid Build Coastguard Worker 	struct tls_record_header tls_header;
219*8b26181fSAndroid Build Coastguard Worker 	struct tls_alert tls_alert;
220*8b26181fSAndroid Build Coastguard Worker 	struct daemon_slpars pars;		// service loop parameters
221*8b26181fSAndroid Build Coastguard Worker 	char errbuf[PCAP_ERRBUF_SIZE + 1];	// keeps the error string, prior to be printed
222*8b26181fSAndroid Build Coastguard Worker 	char errmsgbuf[PCAP_ERRBUF_SIZE + 1];	// buffer for errors to send to the client
223*8b26181fSAndroid Build Coastguard Worker 	int host_port_check_status;
224*8b26181fSAndroid Build Coastguard Worker 	SSL *ssl = NULL;
225*8b26181fSAndroid Build Coastguard Worker 	int nrecv;
226*8b26181fSAndroid Build Coastguard Worker 	struct rpcap_header header;		// RPCAP message general header
227*8b26181fSAndroid Build Coastguard Worker 	uint32 plen;				// payload length from header
228*8b26181fSAndroid Build Coastguard Worker 	int authenticated = 0;			// 1 if the client has successfully authenticated
229*8b26181fSAndroid Build Coastguard Worker 	char source[PCAP_BUF_SIZE+1];		// keeps the string that contains the interface to open
230*8b26181fSAndroid Build Coastguard Worker 	int got_source = 0;			// 1 if we've gotten the source from an open request
231*8b26181fSAndroid Build Coastguard Worker #ifndef _WIN32
232*8b26181fSAndroid Build Coastguard Worker 	struct sigaction action;
233*8b26181fSAndroid Build Coastguard Worker #endif
234*8b26181fSAndroid Build Coastguard Worker 	struct session *session = NULL;		// struct session main variable
235*8b26181fSAndroid Build Coastguard Worker 	const char *msg_type_string;		// string for message type
236*8b26181fSAndroid Build Coastguard Worker 	int client_told_us_to_close = 0;	// 1 if the client told us to close the capture
237*8b26181fSAndroid Build Coastguard Worker 
238*8b26181fSAndroid Build Coastguard Worker 	// needed to save the values of the statistics
239*8b26181fSAndroid Build Coastguard Worker 	struct pcap_stat stats;
240*8b26181fSAndroid Build Coastguard Worker 	unsigned int svrcapt;
241*8b26181fSAndroid Build Coastguard Worker 
242*8b26181fSAndroid Build Coastguard Worker 	struct rpcap_sampling samp_param;	// in case sampling has been requested
243*8b26181fSAndroid Build Coastguard Worker 
244*8b26181fSAndroid Build Coastguard Worker 	// Structures needed for the select() call
245*8b26181fSAndroid Build Coastguard Worker 	fd_set rfds;				// set of socket descriptors we have to check
246*8b26181fSAndroid Build Coastguard Worker 	struct timeval tv;			// maximum time the select() can block waiting for data
247*8b26181fSAndroid Build Coastguard Worker 	int retval;				// select() return value
248*8b26181fSAndroid Build Coastguard Worker 
249*8b26181fSAndroid Build Coastguard Worker 	*errbuf = 0;	// Initialize errbuf
250*8b26181fSAndroid Build Coastguard Worker 
251*8b26181fSAndroid Build Coastguard Worker 	//
252*8b26181fSAndroid Build Coastguard Worker 	// Peek into the socket to determine whether the client sent us
253*8b26181fSAndroid Build Coastguard Worker 	// a TLS handshake message or a non-TLS rpcapd message.
254*8b26181fSAndroid Build Coastguard Worker 	//
255*8b26181fSAndroid Build Coastguard Worker 	// The first byte of an rpcapd request is the version number;
256*8b26181fSAndroid Build Coastguard Worker 	// the first byte of a TLS handshake message is 22.  The
257*8b26181fSAndroid Build Coastguard Worker 	// first request to an rpcapd server must be an authentication
258*8b26181fSAndroid Build Coastguard Worker 	// request or a close request, and must have a version number
259*8b26181fSAndroid Build Coastguard Worker 	// of 0, so it will be possible to distinguish between an
260*8b26181fSAndroid Build Coastguard Worker 	// initial plaintext request to a server and an initial TLS
261*8b26181fSAndroid Build Coastguard Worker 	// handshake message.
262*8b26181fSAndroid Build Coastguard Worker 	//
263*8b26181fSAndroid Build Coastguard Worker 	nrecv = sock_recv(sockctrl, NULL, (char *)&first_octet, 1,
264*8b26181fSAndroid Build Coastguard Worker 	    SOCK_EOF_ISNT_ERROR|SOCK_MSG_PEEK, errbuf, PCAP_ERRBUF_SIZE);
265*8b26181fSAndroid Build Coastguard Worker 	if (nrecv == -1)
266*8b26181fSAndroid Build Coastguard Worker 	{
267*8b26181fSAndroid Build Coastguard Worker 		// Fatal error.
268*8b26181fSAndroid Build Coastguard Worker 		rpcapd_log(LOGPRIO_ERROR, "Peek from client failed: %s", errbuf);
269*8b26181fSAndroid Build Coastguard Worker 		goto end;
270*8b26181fSAndroid Build Coastguard Worker 	}
271*8b26181fSAndroid Build Coastguard Worker 	if (nrecv == 0)
272*8b26181fSAndroid Build Coastguard Worker 	{
273*8b26181fSAndroid Build Coastguard Worker 		// Client closed the connection.
274*8b26181fSAndroid Build Coastguard Worker 		goto end;
275*8b26181fSAndroid Build Coastguard Worker 	}
276*8b26181fSAndroid Build Coastguard Worker 
277*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OPENSSL
278*8b26181fSAndroid Build Coastguard Worker 	//
279*8b26181fSAndroid Build Coastguard Worker 	// We have to upgrade to TLS as soon as possible, so that the
280*8b26181fSAndroid Build Coastguard Worker 	// whole protocol goes through the encrypted tunnel, including
281*8b26181fSAndroid Build Coastguard Worker 	// early error messages.
282*8b26181fSAndroid Build Coastguard Worker 	//
283*8b26181fSAndroid Build Coastguard Worker 	// Even in active mode, the other end has to initiate the TLS
284*8b26181fSAndroid Build Coastguard Worker 	// handshake as we still are the server as far as TLS is concerned,
285*8b26181fSAndroid Build Coastguard Worker 	// so we don't check isactive.
286*8b26181fSAndroid Build Coastguard Worker 	//
287*8b26181fSAndroid Build Coastguard Worker 	if (uses_ssl)
288*8b26181fSAndroid Build Coastguard Worker 	{
289*8b26181fSAndroid Build Coastguard Worker 		//
290*8b26181fSAndroid Build Coastguard Worker 		// We're expecting a TLS handshake message.  If this
291*8b26181fSAndroid Build Coastguard Worker 		// isn't one, assume it's a non-TLS rpcapd message.
292*8b26181fSAndroid Build Coastguard Worker 		//
293*8b26181fSAndroid Build Coastguard Worker 		// The first octet of a TLS handshake is
294*8b26181fSAndroid Build Coastguard Worker 		// TLS_RECORD_TYPE_HANDSHAKE.
295*8b26181fSAndroid Build Coastguard Worker 		//
296*8b26181fSAndroid Build Coastguard Worker 		if (first_octet != TLS_RECORD_TYPE_HANDSHAKE)
297*8b26181fSAndroid Build Coastguard Worker 		{
298*8b26181fSAndroid Build Coastguard Worker 			//
299*8b26181fSAndroid Build Coastguard Worker 			// We assume this is a non-TLS rpcapd message.
300*8b26181fSAndroid Build Coastguard Worker 			//
301*8b26181fSAndroid Build Coastguard Worker 			// Read the message header from the client.
302*8b26181fSAndroid Build Coastguard Worker 			//
303*8b26181fSAndroid Build Coastguard Worker 			nrecv = rpcapd_recv_msg_header(sockctrl, NULL, &header);
304*8b26181fSAndroid Build Coastguard Worker 			if (nrecv == -1)
305*8b26181fSAndroid Build Coastguard Worker 			{
306*8b26181fSAndroid Build Coastguard Worker 				// Fatal error.
307*8b26181fSAndroid Build Coastguard Worker 				goto end;
308*8b26181fSAndroid Build Coastguard Worker 			}
309*8b26181fSAndroid Build Coastguard Worker 			if (nrecv == -2)
310*8b26181fSAndroid Build Coastguard Worker 			{
311*8b26181fSAndroid Build Coastguard Worker 				// Client closed the connection.
312*8b26181fSAndroid Build Coastguard Worker 				goto end;
313*8b26181fSAndroid Build Coastguard Worker 			}
314*8b26181fSAndroid Build Coastguard Worker 			plen = header.plen;
315*8b26181fSAndroid Build Coastguard Worker 
316*8b26181fSAndroid Build Coastguard Worker 			// Discard the rest of the message.
317*8b26181fSAndroid Build Coastguard Worker 			if (rpcapd_discard(sockctrl, NULL, plen) == -1)
318*8b26181fSAndroid Build Coastguard Worker 			{
319*8b26181fSAndroid Build Coastguard Worker 				// Network error.
320*8b26181fSAndroid Build Coastguard Worker 				goto end;
321*8b26181fSAndroid Build Coastguard Worker 			}
322*8b26181fSAndroid Build Coastguard Worker 
323*8b26181fSAndroid Build Coastguard Worker 			//
324*8b26181fSAndroid Build Coastguard Worker 			// Send an authentication error, indicating
325*8b26181fSAndroid Build Coastguard Worker 			// that we require TLS.
326*8b26181fSAndroid Build Coastguard Worker 			//
327*8b26181fSAndroid Build Coastguard Worker 			if (rpcap_senderror(sockctrl, NULL, header.ver,
328*8b26181fSAndroid Build Coastguard Worker 			    PCAP_ERR_TLS_REQUIRED,
329*8b26181fSAndroid Build Coastguard Worker 			    "TLS is required by this server", errbuf) == -1)
330*8b26181fSAndroid Build Coastguard Worker 			{
331*8b26181fSAndroid Build Coastguard Worker 				// That failed; log a message and give up.
332*8b26181fSAndroid Build Coastguard Worker 				rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
333*8b26181fSAndroid Build Coastguard Worker 				goto end;
334*8b26181fSAndroid Build Coastguard Worker 			}
335*8b26181fSAndroid Build Coastguard Worker 
336*8b26181fSAndroid Build Coastguard Worker 			// Shut the session down.
337*8b26181fSAndroid Build Coastguard Worker 			goto end;
338*8b26181fSAndroid Build Coastguard Worker 		}
339*8b26181fSAndroid Build Coastguard Worker 		ssl = ssl_promotion(1, sockctrl, errbuf, PCAP_ERRBUF_SIZE);
340*8b26181fSAndroid Build Coastguard Worker 		if (! ssl)
341*8b26181fSAndroid Build Coastguard Worker 		{
342*8b26181fSAndroid Build Coastguard Worker 			rpcapd_log(LOGPRIO_ERROR, "TLS handshake on control connection failed: %s",
343*8b26181fSAndroid Build Coastguard Worker 			    errbuf);
344*8b26181fSAndroid Build Coastguard Worker 			goto end;
345*8b26181fSAndroid Build Coastguard Worker 		}
346*8b26181fSAndroid Build Coastguard Worker 	}
347*8b26181fSAndroid Build Coastguard Worker 	else
348*8b26181fSAndroid Build Coastguard Worker #endif
349*8b26181fSAndroid Build Coastguard Worker 	{
350*8b26181fSAndroid Build Coastguard Worker 		//
351*8b26181fSAndroid Build Coastguard Worker 		// We're expecting a non-TLS rpcapd message.  If this
352*8b26181fSAndroid Build Coastguard Worker 		// looks, instead, like a TLS handshake message, send
353*8b26181fSAndroid Build Coastguard Worker 		// a TLS handshake_failed alert.
354*8b26181fSAndroid Build Coastguard Worker 		//
355*8b26181fSAndroid Build Coastguard Worker 		// The first octet of a TLS handshake is
356*8b26181fSAndroid Build Coastguard Worker 		// TLS_RECORD_TYPE_HANDSHAKE.
357*8b26181fSAndroid Build Coastguard Worker 		//
358*8b26181fSAndroid Build Coastguard Worker 		if (first_octet == TLS_RECORD_TYPE_HANDSHAKE)
359*8b26181fSAndroid Build Coastguard Worker 		{
360*8b26181fSAndroid Build Coastguard Worker 			//
361*8b26181fSAndroid Build Coastguard Worker 			// TLS handshake.
362*8b26181fSAndroid Build Coastguard Worker 			// Read the record header.
363*8b26181fSAndroid Build Coastguard Worker 			//
364*8b26181fSAndroid Build Coastguard Worker 			nrecv = sock_recv(sockctrl, ssl, (char *) &tls_header,
365*8b26181fSAndroid Build Coastguard Worker 			    sizeof tls_header, SOCK_RECEIVEALL_YES|SOCK_EOF_ISNT_ERROR,
366*8b26181fSAndroid Build Coastguard Worker 			    errbuf, PCAP_ERRBUF_SIZE);
367*8b26181fSAndroid Build Coastguard Worker 			if (nrecv == -1)
368*8b26181fSAndroid Build Coastguard Worker 			{
369*8b26181fSAndroid Build Coastguard Worker 				// Network error.
370*8b26181fSAndroid Build Coastguard Worker 				rpcapd_log(LOGPRIO_ERROR, "Read from client failed: %s", errbuf);
371*8b26181fSAndroid Build Coastguard Worker 				goto end;
372*8b26181fSAndroid Build Coastguard Worker 			}
373*8b26181fSAndroid Build Coastguard Worker 			if (nrecv == 0)
374*8b26181fSAndroid Build Coastguard Worker 			{
375*8b26181fSAndroid Build Coastguard Worker 				// Immediate EOF
376*8b26181fSAndroid Build Coastguard Worker 				goto end;
377*8b26181fSAndroid Build Coastguard Worker 			}
378*8b26181fSAndroid Build Coastguard Worker 			plen = (tls_header.length_hi << 8U) | tls_header.length_lo;
379*8b26181fSAndroid Build Coastguard Worker 
380*8b26181fSAndroid Build Coastguard Worker 			// Discard the rest of the message.
381*8b26181fSAndroid Build Coastguard Worker 			if (rpcapd_discard(sockctrl, NULL, plen) == -1)
382*8b26181fSAndroid Build Coastguard Worker 			{
383*8b26181fSAndroid Build Coastguard Worker 				// Network error.
384*8b26181fSAndroid Build Coastguard Worker 				goto end;
385*8b26181fSAndroid Build Coastguard Worker 			}
386*8b26181fSAndroid Build Coastguard Worker 
387*8b26181fSAndroid Build Coastguard Worker 			//
388*8b26181fSAndroid Build Coastguard Worker 			// Send a TLS handshake failure alert.
389*8b26181fSAndroid Build Coastguard Worker 			// Use the same version the client sent us.
390*8b26181fSAndroid Build Coastguard Worker 			//
391*8b26181fSAndroid Build Coastguard Worker 			tls_header.type = TLS_RECORD_TYPE_ALERT;
392*8b26181fSAndroid Build Coastguard Worker 			tls_header.length_hi = 0;
393*8b26181fSAndroid Build Coastguard Worker 			tls_header.length_lo = TLS_ALERT_LEN;
394*8b26181fSAndroid Build Coastguard Worker 
395*8b26181fSAndroid Build Coastguard Worker 			if (sock_send(sockctrl, NULL, (char *) &tls_header,
396*8b26181fSAndroid Build Coastguard Worker 			    TLS_RECORD_HEADER_LEN, errbuf, PCAP_ERRBUF_SIZE) == -1)
397*8b26181fSAndroid Build Coastguard Worker 			{
398*8b26181fSAndroid Build Coastguard Worker 				// That failed; log a message and give up.
399*8b26181fSAndroid Build Coastguard Worker 				rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
400*8b26181fSAndroid Build Coastguard Worker 				goto end;
401*8b26181fSAndroid Build Coastguard Worker 			}
402*8b26181fSAndroid Build Coastguard Worker 
403*8b26181fSAndroid Build Coastguard Worker 			tls_alert.alert_level = TLS_ALERT_LEVEL_FATAL;
404*8b26181fSAndroid Build Coastguard Worker 			tls_alert.alert_description = TLS_ALERT_HANDSHAKE_FAILURE;
405*8b26181fSAndroid Build Coastguard Worker 			if (sock_send(sockctrl, NULL, (char *) &tls_alert,
406*8b26181fSAndroid Build Coastguard Worker 			    TLS_ALERT_LEN, errbuf, PCAP_ERRBUF_SIZE) == -1)
407*8b26181fSAndroid Build Coastguard Worker 			{
408*8b26181fSAndroid Build Coastguard Worker 				// That failed; log a message and give up.
409*8b26181fSAndroid Build Coastguard Worker 				rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
410*8b26181fSAndroid Build Coastguard Worker 				goto end;
411*8b26181fSAndroid Build Coastguard Worker 			}
412*8b26181fSAndroid Build Coastguard Worker 			//
413*8b26181fSAndroid Build Coastguard Worker 			// Give up anyway.
414*8b26181fSAndroid Build Coastguard Worker 			//
415*8b26181fSAndroid Build Coastguard Worker 			goto end;
416*8b26181fSAndroid Build Coastguard Worker 		}
417*8b26181fSAndroid Build Coastguard Worker 	}
418*8b26181fSAndroid Build Coastguard Worker 
419*8b26181fSAndroid Build Coastguard Worker 	// Set parameters structure
420*8b26181fSAndroid Build Coastguard Worker 	pars.sockctrl = sockctrl;
421*8b26181fSAndroid Build Coastguard Worker 	pars.ssl = ssl;
422*8b26181fSAndroid Build Coastguard Worker 	pars.isactive = isactive;		// active mode
423*8b26181fSAndroid Build Coastguard Worker 	pars.nullAuthAllowed = nullAuthAllowed;
424*8b26181fSAndroid Build Coastguard Worker 
425*8b26181fSAndroid Build Coastguard Worker 	//
426*8b26181fSAndroid Build Coastguard Worker 	// We have a connection.
427*8b26181fSAndroid Build Coastguard Worker 	//
428*8b26181fSAndroid Build Coastguard Worker 	// If it's a passive mode connection, check whether the connecting
429*8b26181fSAndroid Build Coastguard Worker 	// host is among the ones allowed.
430*8b26181fSAndroid Build Coastguard Worker 	//
431*8b26181fSAndroid Build Coastguard Worker 	// In either case, we were handed a copy of the host list; free it
432*8b26181fSAndroid Build Coastguard Worker 	// as soon as we're done with it.
433*8b26181fSAndroid Build Coastguard Worker 	//
434*8b26181fSAndroid Build Coastguard Worker 	if (pars.isactive)
435*8b26181fSAndroid Build Coastguard Worker 	{
436*8b26181fSAndroid Build Coastguard Worker 		// Nothing to do.
437*8b26181fSAndroid Build Coastguard Worker 		free(passiveClients);
438*8b26181fSAndroid Build Coastguard Worker 		passiveClients = NULL;
439*8b26181fSAndroid Build Coastguard Worker 	}
440*8b26181fSAndroid Build Coastguard Worker 	else
441*8b26181fSAndroid Build Coastguard Worker 	{
442*8b26181fSAndroid Build Coastguard Worker 		struct sockaddr_storage from;
443*8b26181fSAndroid Build Coastguard Worker 		socklen_t fromlen;
444*8b26181fSAndroid Build Coastguard Worker 
445*8b26181fSAndroid Build Coastguard Worker 		//
446*8b26181fSAndroid Build Coastguard Worker 		// Get the address of the other end of the connection.
447*8b26181fSAndroid Build Coastguard Worker 		//
448*8b26181fSAndroid Build Coastguard Worker 		fromlen = sizeof(struct sockaddr_storage);
449*8b26181fSAndroid Build Coastguard Worker 		if (getpeername(pars.sockctrl, (struct sockaddr *)&from,
450*8b26181fSAndroid Build Coastguard Worker 		    &fromlen) == -1)
451*8b26181fSAndroid Build Coastguard Worker 		{
452*8b26181fSAndroid Build Coastguard Worker 			sock_geterrmsg(errmsgbuf, PCAP_ERRBUF_SIZE,
453*8b26181fSAndroid Build Coastguard Worker 			    "getpeername() failed");
454*8b26181fSAndroid Build Coastguard Worker 			if (rpcap_senderror(pars.sockctrl, pars.ssl, 0, PCAP_ERR_NETW, errmsgbuf, errbuf) == -1)
455*8b26181fSAndroid Build Coastguard Worker 				rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
456*8b26181fSAndroid Build Coastguard Worker 			goto end;
457*8b26181fSAndroid Build Coastguard Worker 		}
458*8b26181fSAndroid Build Coastguard Worker 
459*8b26181fSAndroid Build Coastguard Worker 		//
460*8b26181fSAndroid Build Coastguard Worker 		// Are they in the list of host/port combinations we allow?
461*8b26181fSAndroid Build Coastguard Worker 		//
462*8b26181fSAndroid Build Coastguard Worker 		host_port_check_status = sock_check_hostlist(passiveClients, RPCAP_HOSTLIST_SEP, &from, errmsgbuf, PCAP_ERRBUF_SIZE);
463*8b26181fSAndroid Build Coastguard Worker 		free(passiveClients);
464*8b26181fSAndroid Build Coastguard Worker 		passiveClients = NULL;
465*8b26181fSAndroid Build Coastguard Worker 		if (host_port_check_status < 0)
466*8b26181fSAndroid Build Coastguard Worker 		{
467*8b26181fSAndroid Build Coastguard Worker 			if (host_port_check_status == -2) {
468*8b26181fSAndroid Build Coastguard Worker 				//
469*8b26181fSAndroid Build Coastguard Worker 				// We got an error; log it.
470*8b26181fSAndroid Build Coastguard Worker 				//
471*8b26181fSAndroid Build Coastguard Worker 				rpcapd_log(LOGPRIO_ERROR, "%s", errmsgbuf);
472*8b26181fSAndroid Build Coastguard Worker 			}
473*8b26181fSAndroid Build Coastguard Worker 
474*8b26181fSAndroid Build Coastguard Worker 			//
475*8b26181fSAndroid Build Coastguard Worker 			// Sorry, we can't let you in.
476*8b26181fSAndroid Build Coastguard Worker 			//
477*8b26181fSAndroid Build Coastguard Worker 			if (rpcap_senderror(pars.sockctrl, pars.ssl, 0, PCAP_ERR_HOSTNOAUTH, errmsgbuf, errbuf) == -1)
478*8b26181fSAndroid Build Coastguard Worker 				rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
479*8b26181fSAndroid Build Coastguard Worker 			goto end;
480*8b26181fSAndroid Build Coastguard Worker 		}
481*8b26181fSAndroid Build Coastguard Worker 	}
482*8b26181fSAndroid Build Coastguard Worker 
483*8b26181fSAndroid Build Coastguard Worker #ifndef _WIN32
484*8b26181fSAndroid Build Coastguard Worker 	//
485*8b26181fSAndroid Build Coastguard Worker 	// Catch SIGUSR1, but do nothing.  We use it to interrupt the
486*8b26181fSAndroid Build Coastguard Worker 	// capture thread to break it out of a loop in which it's
487*8b26181fSAndroid Build Coastguard Worker 	// blocked waiting for packets to arrive.
488*8b26181fSAndroid Build Coastguard Worker 	//
489*8b26181fSAndroid Build Coastguard Worker 	// We don't want interrupted system calls to restart, so that
490*8b26181fSAndroid Build Coastguard Worker 	// the read routine for the pcap_t gets EINTR rather than
491*8b26181fSAndroid Build Coastguard Worker 	// restarting if it's blocked in a system call.
492*8b26181fSAndroid Build Coastguard Worker 	//
493*8b26181fSAndroid Build Coastguard Worker 	memset(&action, 0, sizeof (action));
494*8b26181fSAndroid Build Coastguard Worker 	action.sa_handler = noop_handler;
495*8b26181fSAndroid Build Coastguard Worker 	action.sa_flags = 0;
496*8b26181fSAndroid Build Coastguard Worker 	sigemptyset(&action.sa_mask);
497*8b26181fSAndroid Build Coastguard Worker 	sigaction(SIGUSR1, &action, NULL);
498*8b26181fSAndroid Build Coastguard Worker #endif
499*8b26181fSAndroid Build Coastguard Worker 
500*8b26181fSAndroid Build Coastguard Worker 	//
501*8b26181fSAndroid Build Coastguard Worker 	// The client must first authenticate; loop until they send us a
502*8b26181fSAndroid Build Coastguard Worker 	// message with a version we support and credentials we accept,
503*8b26181fSAndroid Build Coastguard Worker 	// they send us a close message indicating that they're giving up,
504*8b26181fSAndroid Build Coastguard Worker 	// or we get a network error or other fatal error.
505*8b26181fSAndroid Build Coastguard Worker 	//
506*8b26181fSAndroid Build Coastguard Worker 	while (!authenticated)
507*8b26181fSAndroid Build Coastguard Worker 	{
508*8b26181fSAndroid Build Coastguard Worker 		//
509*8b26181fSAndroid Build Coastguard Worker 		// If we're not in active mode, we use select(), with a
510*8b26181fSAndroid Build Coastguard Worker 		// timeout, to wait for an authentication request; if
511*8b26181fSAndroid Build Coastguard Worker 		// the timeout expires, we drop the connection, so that
512*8b26181fSAndroid Build Coastguard Worker 		// a client can't just connect to us and leave us
513*8b26181fSAndroid Build Coastguard Worker 		// waiting forever.
514*8b26181fSAndroid Build Coastguard Worker 		//
515*8b26181fSAndroid Build Coastguard Worker 		if (!pars.isactive)
516*8b26181fSAndroid Build Coastguard Worker 		{
517*8b26181fSAndroid Build Coastguard Worker 			FD_ZERO(&rfds);
518*8b26181fSAndroid Build Coastguard Worker 			// We do not have to block here
519*8b26181fSAndroid Build Coastguard Worker 			tv.tv_sec = RPCAP_TIMEOUT_INIT;
520*8b26181fSAndroid Build Coastguard Worker 			tv.tv_usec = 0;
521*8b26181fSAndroid Build Coastguard Worker 
522*8b26181fSAndroid Build Coastguard Worker 			FD_SET(pars.sockctrl, &rfds);
523*8b26181fSAndroid Build Coastguard Worker 
524*8b26181fSAndroid Build Coastguard Worker 			retval = select((int)pars.sockctrl + 1, &rfds, NULL, NULL, &tv);
525*8b26181fSAndroid Build Coastguard Worker 			if (retval == -1)
526*8b26181fSAndroid Build Coastguard Worker 			{
527*8b26181fSAndroid Build Coastguard Worker 				sock_geterrmsg(errmsgbuf, PCAP_ERRBUF_SIZE,
528*8b26181fSAndroid Build Coastguard Worker 				    "select() failed");
529*8b26181fSAndroid Build Coastguard Worker 				if (rpcap_senderror(pars.sockctrl, pars.ssl, 0, PCAP_ERR_NETW, errmsgbuf, errbuf) == -1)
530*8b26181fSAndroid Build Coastguard Worker 					rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
531*8b26181fSAndroid Build Coastguard Worker 				goto end;
532*8b26181fSAndroid Build Coastguard Worker 			}
533*8b26181fSAndroid Build Coastguard Worker 
534*8b26181fSAndroid Build Coastguard Worker 			// The timeout has expired
535*8b26181fSAndroid Build Coastguard Worker 			// So, this was a fake connection. Drop it down
536*8b26181fSAndroid Build Coastguard Worker 			if (retval == 0)
537*8b26181fSAndroid Build Coastguard Worker 			{
538*8b26181fSAndroid Build Coastguard Worker 				if (rpcap_senderror(pars.sockctrl, pars.ssl, 0, PCAP_ERR_INITTIMEOUT, "The RPCAP initial timeout has expired", errbuf) == -1)
539*8b26181fSAndroid Build Coastguard Worker 					rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
540*8b26181fSAndroid Build Coastguard Worker 				goto end;
541*8b26181fSAndroid Build Coastguard Worker 			}
542*8b26181fSAndroid Build Coastguard Worker 		}
543*8b26181fSAndroid Build Coastguard Worker 
544*8b26181fSAndroid Build Coastguard Worker 		//
545*8b26181fSAndroid Build Coastguard Worker 		// Read the message header from the client.
546*8b26181fSAndroid Build Coastguard Worker 		//
547*8b26181fSAndroid Build Coastguard Worker 		nrecv = rpcapd_recv_msg_header(pars.sockctrl, pars.ssl, &header);
548*8b26181fSAndroid Build Coastguard Worker 		if (nrecv == -1)
549*8b26181fSAndroid Build Coastguard Worker 		{
550*8b26181fSAndroid Build Coastguard Worker 			// Fatal error.
551*8b26181fSAndroid Build Coastguard Worker 			goto end;
552*8b26181fSAndroid Build Coastguard Worker 		}
553*8b26181fSAndroid Build Coastguard Worker 		if (nrecv == -2)
554*8b26181fSAndroid Build Coastguard Worker 		{
555*8b26181fSAndroid Build Coastguard Worker 			// Client closed the connection.
556*8b26181fSAndroid Build Coastguard Worker 			goto end;
557*8b26181fSAndroid Build Coastguard Worker 		}
558*8b26181fSAndroid Build Coastguard Worker 
559*8b26181fSAndroid Build Coastguard Worker 		plen = header.plen;
560*8b26181fSAndroid Build Coastguard Worker 
561*8b26181fSAndroid Build Coastguard Worker 		//
562*8b26181fSAndroid Build Coastguard Worker 		// While we're in the authentication pharse, all requests
563*8b26181fSAndroid Build Coastguard Worker 		// must use version 0.
564*8b26181fSAndroid Build Coastguard Worker 		//
565*8b26181fSAndroid Build Coastguard Worker 		if (header.ver != 0)
566*8b26181fSAndroid Build Coastguard Worker 		{
567*8b26181fSAndroid Build Coastguard Worker 			//
568*8b26181fSAndroid Build Coastguard Worker 			// Send it back to them with their version.
569*8b26181fSAndroid Build Coastguard Worker 			//
570*8b26181fSAndroid Build Coastguard Worker 			if (rpcap_senderror(pars.sockctrl, pars.ssl, header.ver,
571*8b26181fSAndroid Build Coastguard Worker 			    PCAP_ERR_WRONGVER,
572*8b26181fSAndroid Build Coastguard Worker 			    "RPCAP version in requests in the authentication phase must be 0",
573*8b26181fSAndroid Build Coastguard Worker 			    errbuf) == -1)
574*8b26181fSAndroid Build Coastguard Worker 			{
575*8b26181fSAndroid Build Coastguard Worker 				// That failed; log a message and give up.
576*8b26181fSAndroid Build Coastguard Worker 				rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
577*8b26181fSAndroid Build Coastguard Worker 				goto end;
578*8b26181fSAndroid Build Coastguard Worker 			}
579*8b26181fSAndroid Build Coastguard Worker 
580*8b26181fSAndroid Build Coastguard Worker 			// Discard the rest of the message and drop the
581*8b26181fSAndroid Build Coastguard Worker 			// connection.
582*8b26181fSAndroid Build Coastguard Worker 			(void)rpcapd_discard(pars.sockctrl, pars.ssl, plen);
583*8b26181fSAndroid Build Coastguard Worker 			goto end;
584*8b26181fSAndroid Build Coastguard Worker 		}
585*8b26181fSAndroid Build Coastguard Worker 
586*8b26181fSAndroid Build Coastguard Worker 		switch (header.type)
587*8b26181fSAndroid Build Coastguard Worker 		{
588*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_AUTH_REQ:
589*8b26181fSAndroid Build Coastguard Worker 				retval = daemon_msg_auth_req(&pars, plen);
590*8b26181fSAndroid Build Coastguard Worker 				if (retval == -1)
591*8b26181fSAndroid Build Coastguard Worker 				{
592*8b26181fSAndroid Build Coastguard Worker 					// Fatal error; a message has
593*8b26181fSAndroid Build Coastguard Worker 					// been logged, so just give up.
594*8b26181fSAndroid Build Coastguard Worker 					goto end;
595*8b26181fSAndroid Build Coastguard Worker 				}
596*8b26181fSAndroid Build Coastguard Worker 				if (retval == -2)
597*8b26181fSAndroid Build Coastguard Worker 				{
598*8b26181fSAndroid Build Coastguard Worker 					// Non-fatal error; we sent back
599*8b26181fSAndroid Build Coastguard Worker 					// an error message, so let them
600*8b26181fSAndroid Build Coastguard Worker 					// try again.
601*8b26181fSAndroid Build Coastguard Worker 					continue;
602*8b26181fSAndroid Build Coastguard Worker 				}
603*8b26181fSAndroid Build Coastguard Worker 
604*8b26181fSAndroid Build Coastguard Worker 				// OK, we're authenticated; we sent back
605*8b26181fSAndroid Build Coastguard Worker 				// a reply, so start serving requests.
606*8b26181fSAndroid Build Coastguard Worker 				authenticated = 1;
607*8b26181fSAndroid Build Coastguard Worker 				break;
608*8b26181fSAndroid Build Coastguard Worker 
609*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_CLOSE:
610*8b26181fSAndroid Build Coastguard Worker 				//
611*8b26181fSAndroid Build Coastguard Worker 				// The client is giving up.
612*8b26181fSAndroid Build Coastguard Worker 				// Discard the rest of the message, if
613*8b26181fSAndroid Build Coastguard Worker 				// there is anything more.
614*8b26181fSAndroid Build Coastguard Worker 				//
615*8b26181fSAndroid Build Coastguard Worker 				(void)rpcapd_discard(pars.sockctrl, pars.ssl, plen);
616*8b26181fSAndroid Build Coastguard Worker 				// We're done with this client.
617*8b26181fSAndroid Build Coastguard Worker 				goto end;
618*8b26181fSAndroid Build Coastguard Worker 
619*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_ERROR:
620*8b26181fSAndroid Build Coastguard Worker 				// Log this and close the connection?
621*8b26181fSAndroid Build Coastguard Worker 				// XXX - is this what happens in active
622*8b26181fSAndroid Build Coastguard Worker 				// mode, where *we* initiate the
623*8b26181fSAndroid Build Coastguard Worker 				// connection, and the client gives us
624*8b26181fSAndroid Build Coastguard Worker 				// an error message rather than a "let
625*8b26181fSAndroid Build Coastguard Worker 				// me log in" message, indicating that
626*8b26181fSAndroid Build Coastguard Worker 				// we're not allowed to connect to them?
627*8b26181fSAndroid Build Coastguard Worker 				(void)daemon_msg_err(pars.sockctrl, pars.ssl, plen);
628*8b26181fSAndroid Build Coastguard Worker 				goto end;
629*8b26181fSAndroid Build Coastguard Worker 
630*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_FINDALLIF_REQ:
631*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_OPEN_REQ:
632*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_STARTCAP_REQ:
633*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_UPDATEFILTER_REQ:
634*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_STATS_REQ:
635*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_ENDCAP_REQ:
636*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_SETSAMPLING_REQ:
637*8b26181fSAndroid Build Coastguard Worker 				//
638*8b26181fSAndroid Build Coastguard Worker 				// These requests can't be sent until
639*8b26181fSAndroid Build Coastguard Worker 				// the client is authenticated.
640*8b26181fSAndroid Build Coastguard Worker 				//
641*8b26181fSAndroid Build Coastguard Worker 				msg_type_string = rpcap_msg_type_string(header.type);
642*8b26181fSAndroid Build Coastguard Worker 				if (msg_type_string != NULL)
643*8b26181fSAndroid Build Coastguard Worker 				{
644*8b26181fSAndroid Build Coastguard Worker 					snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "%s request sent before authentication was completed", msg_type_string);
645*8b26181fSAndroid Build Coastguard Worker 				}
646*8b26181fSAndroid Build Coastguard Worker 				else
647*8b26181fSAndroid Build Coastguard Worker 				{
648*8b26181fSAndroid Build Coastguard Worker 					snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Message of type %u sent before authentication was completed", header.type);
649*8b26181fSAndroid Build Coastguard Worker 				}
650*8b26181fSAndroid Build Coastguard Worker 				if (rpcap_senderror(pars.sockctrl, pars.ssl,
651*8b26181fSAndroid Build Coastguard Worker 				    header.ver, PCAP_ERR_WRONGMSG,
652*8b26181fSAndroid Build Coastguard Worker 				    errmsgbuf, errbuf) == -1)
653*8b26181fSAndroid Build Coastguard Worker 				{
654*8b26181fSAndroid Build Coastguard Worker 					rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
655*8b26181fSAndroid Build Coastguard Worker 					goto end;
656*8b26181fSAndroid Build Coastguard Worker 				}
657*8b26181fSAndroid Build Coastguard Worker 				// Discard the rest of the message.
658*8b26181fSAndroid Build Coastguard Worker 				if (rpcapd_discard(pars.sockctrl, pars.ssl, plen) == -1)
659*8b26181fSAndroid Build Coastguard Worker 				{
660*8b26181fSAndroid Build Coastguard Worker 					// Network error.
661*8b26181fSAndroid Build Coastguard Worker 					goto end;
662*8b26181fSAndroid Build Coastguard Worker 				}
663*8b26181fSAndroid Build Coastguard Worker 				break;
664*8b26181fSAndroid Build Coastguard Worker 
665*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_PACKET:
666*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_FINDALLIF_REPLY:
667*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_OPEN_REPLY:
668*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_STARTCAP_REPLY:
669*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_UPDATEFILTER_REPLY:
670*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_AUTH_REPLY:
671*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_STATS_REPLY:
672*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_ENDCAP_REPLY:
673*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_SETSAMPLING_REPLY:
674*8b26181fSAndroid Build Coastguard Worker 				//
675*8b26181fSAndroid Build Coastguard Worker 				// These are server-to-client messages.
676*8b26181fSAndroid Build Coastguard Worker 				//
677*8b26181fSAndroid Build Coastguard Worker 				msg_type_string = rpcap_msg_type_string(header.type);
678*8b26181fSAndroid Build Coastguard Worker 				if (msg_type_string != NULL)
679*8b26181fSAndroid Build Coastguard Worker 				{
680*8b26181fSAndroid Build Coastguard Worker 					snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Server-to-client message %s received from client", msg_type_string);
681*8b26181fSAndroid Build Coastguard Worker 				}
682*8b26181fSAndroid Build Coastguard Worker 				else
683*8b26181fSAndroid Build Coastguard Worker 				{
684*8b26181fSAndroid Build Coastguard Worker 					snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Server-to-client message of type %u received from client", header.type);
685*8b26181fSAndroid Build Coastguard Worker 				}
686*8b26181fSAndroid Build Coastguard Worker 				if (rpcap_senderror(pars.sockctrl, pars.ssl,
687*8b26181fSAndroid Build Coastguard Worker 				    header.ver, PCAP_ERR_WRONGMSG,
688*8b26181fSAndroid Build Coastguard Worker 				    errmsgbuf, errbuf) == -1)
689*8b26181fSAndroid Build Coastguard Worker 				{
690*8b26181fSAndroid Build Coastguard Worker 					rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
691*8b26181fSAndroid Build Coastguard Worker 					goto end;
692*8b26181fSAndroid Build Coastguard Worker 				}
693*8b26181fSAndroid Build Coastguard Worker 				// Discard the rest of the message.
694*8b26181fSAndroid Build Coastguard Worker 				if (rpcapd_discard(pars.sockctrl, pars.ssl, plen) == -1)
695*8b26181fSAndroid Build Coastguard Worker 				{
696*8b26181fSAndroid Build Coastguard Worker 					// Fatal error.
697*8b26181fSAndroid Build Coastguard Worker 					goto end;
698*8b26181fSAndroid Build Coastguard Worker 				}
699*8b26181fSAndroid Build Coastguard Worker 				break;
700*8b26181fSAndroid Build Coastguard Worker 
701*8b26181fSAndroid Build Coastguard Worker 			default:
702*8b26181fSAndroid Build Coastguard Worker 				//
703*8b26181fSAndroid Build Coastguard Worker 				// Unknown message type.
704*8b26181fSAndroid Build Coastguard Worker 				//
705*8b26181fSAndroid Build Coastguard Worker 				snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Unknown message type %u", header.type);
706*8b26181fSAndroid Build Coastguard Worker 				if (rpcap_senderror(pars.sockctrl, pars.ssl,
707*8b26181fSAndroid Build Coastguard Worker 				    header.ver, PCAP_ERR_WRONGMSG,
708*8b26181fSAndroid Build Coastguard Worker 				    errmsgbuf, errbuf) == -1)
709*8b26181fSAndroid Build Coastguard Worker 				{
710*8b26181fSAndroid Build Coastguard Worker 					rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
711*8b26181fSAndroid Build Coastguard Worker 					goto end;
712*8b26181fSAndroid Build Coastguard Worker 				}
713*8b26181fSAndroid Build Coastguard Worker 				// Discard the rest of the message.
714*8b26181fSAndroid Build Coastguard Worker 				if (rpcapd_discard(pars.sockctrl, pars.ssl, plen) == -1)
715*8b26181fSAndroid Build Coastguard Worker 				{
716*8b26181fSAndroid Build Coastguard Worker 					// Fatal error.
717*8b26181fSAndroid Build Coastguard Worker 					goto end;
718*8b26181fSAndroid Build Coastguard Worker 				}
719*8b26181fSAndroid Build Coastguard Worker 				break;
720*8b26181fSAndroid Build Coastguard Worker 		}
721*8b26181fSAndroid Build Coastguard Worker 	}
722*8b26181fSAndroid Build Coastguard Worker 
723*8b26181fSAndroid Build Coastguard Worker 	//
724*8b26181fSAndroid Build Coastguard Worker 	// OK, the client has authenticated itself, and we can start
725*8b26181fSAndroid Build Coastguard Worker 	// processing regular requests from it.
726*8b26181fSAndroid Build Coastguard Worker 	//
727*8b26181fSAndroid Build Coastguard Worker 
728*8b26181fSAndroid Build Coastguard Worker 	//
729*8b26181fSAndroid Build Coastguard Worker 	// We don't have any statistics yet.
730*8b26181fSAndroid Build Coastguard Worker 	//
731*8b26181fSAndroid Build Coastguard Worker 	stats.ps_ifdrop = 0;
732*8b26181fSAndroid Build Coastguard Worker 	stats.ps_recv = 0;
733*8b26181fSAndroid Build Coastguard Worker 	stats.ps_drop = 0;
734*8b26181fSAndroid Build Coastguard Worker 	svrcapt = 0;
735*8b26181fSAndroid Build Coastguard Worker 
736*8b26181fSAndroid Build Coastguard Worker 	//
737*8b26181fSAndroid Build Coastguard Worker 	// Service requests.
738*8b26181fSAndroid Build Coastguard Worker 	//
739*8b26181fSAndroid Build Coastguard Worker 	for (;;)
740*8b26181fSAndroid Build Coastguard Worker 	{
741*8b26181fSAndroid Build Coastguard Worker 		errbuf[0] = 0;	// clear errbuf
742*8b26181fSAndroid Build Coastguard Worker 
743*8b26181fSAndroid Build Coastguard Worker 		// Avoid zombies connections; check if the connection is opens but no commands are performed
744*8b26181fSAndroid Build Coastguard Worker 		// from more than RPCAP_TIMEOUT_RUNTIME
745*8b26181fSAndroid Build Coastguard Worker 		// Conditions:
746*8b26181fSAndroid Build Coastguard Worker 		// - I have to be in normal mode (no active mode)
747*8b26181fSAndroid Build Coastguard Worker 		// - if the device is open, I don't have to be in the middle of a capture (session->sockdata)
748*8b26181fSAndroid Build Coastguard Worker 		// - if the device is closed, I have always to check if a new command arrives
749*8b26181fSAndroid Build Coastguard Worker 		//
750*8b26181fSAndroid Build Coastguard Worker 		// Be carefully: the capture can have been started, but an error occurred (so session != NULL, but
751*8b26181fSAndroid Build Coastguard Worker 		//  sockdata is 0
752*8b26181fSAndroid Build Coastguard Worker 		if ((!pars.isactive) && (session == NULL || session->sockdata == 0))
753*8b26181fSAndroid Build Coastguard Worker 		{
754*8b26181fSAndroid Build Coastguard Worker 			// Check for the initial timeout
755*8b26181fSAndroid Build Coastguard Worker 			FD_ZERO(&rfds);
756*8b26181fSAndroid Build Coastguard Worker 			// We do not have to block here
757*8b26181fSAndroid Build Coastguard Worker 			tv.tv_sec = RPCAP_TIMEOUT_RUNTIME;
758*8b26181fSAndroid Build Coastguard Worker 			tv.tv_usec = 0;
759*8b26181fSAndroid Build Coastguard Worker 
760*8b26181fSAndroid Build Coastguard Worker 			FD_SET(pars.sockctrl, &rfds);
761*8b26181fSAndroid Build Coastguard Worker #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
762*8b26181fSAndroid Build Coastguard Worker 			retval = 1;
763*8b26181fSAndroid Build Coastguard Worker #else
764*8b26181fSAndroid Build Coastguard Worker 			retval = select((int)pars.sockctrl + 1, &rfds, NULL, NULL, &tv);
765*8b26181fSAndroid Build Coastguard Worker #endif
766*8b26181fSAndroid Build Coastguard Worker 			if (retval == -1)
767*8b26181fSAndroid Build Coastguard Worker 			{
768*8b26181fSAndroid Build Coastguard Worker 				sock_geterrmsg(errmsgbuf, PCAP_ERRBUF_SIZE,
769*8b26181fSAndroid Build Coastguard Worker 				    "select() failed");
770*8b26181fSAndroid Build Coastguard Worker 				if (rpcap_senderror(pars.sockctrl, pars.ssl,
771*8b26181fSAndroid Build Coastguard Worker 				    0, PCAP_ERR_NETW,
772*8b26181fSAndroid Build Coastguard Worker 				    errmsgbuf, errbuf) == -1)
773*8b26181fSAndroid Build Coastguard Worker 					rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
774*8b26181fSAndroid Build Coastguard Worker 				goto end;
775*8b26181fSAndroid Build Coastguard Worker 			}
776*8b26181fSAndroid Build Coastguard Worker 
777*8b26181fSAndroid Build Coastguard Worker 			// The timeout has expired
778*8b26181fSAndroid Build Coastguard Worker 			// So, this was a fake connection. Drop it down
779*8b26181fSAndroid Build Coastguard Worker 			if (retval == 0)
780*8b26181fSAndroid Build Coastguard Worker 			{
781*8b26181fSAndroid Build Coastguard Worker 				if (rpcap_senderror(pars.sockctrl, pars.ssl,
782*8b26181fSAndroid Build Coastguard Worker 				    0, PCAP_ERR_INITTIMEOUT,
783*8b26181fSAndroid Build Coastguard Worker 				    "The RPCAP initial timeout has expired",
784*8b26181fSAndroid Build Coastguard Worker 				    errbuf) == -1)
785*8b26181fSAndroid Build Coastguard Worker 					rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
786*8b26181fSAndroid Build Coastguard Worker 				goto end;
787*8b26181fSAndroid Build Coastguard Worker 			}
788*8b26181fSAndroid Build Coastguard Worker 		}
789*8b26181fSAndroid Build Coastguard Worker 
790*8b26181fSAndroid Build Coastguard Worker 		//
791*8b26181fSAndroid Build Coastguard Worker 		// Read the message header from the client.
792*8b26181fSAndroid Build Coastguard Worker 		//
793*8b26181fSAndroid Build Coastguard Worker 		nrecv = rpcapd_recv_msg_header(pars.sockctrl, pars.ssl, &header);
794*8b26181fSAndroid Build Coastguard Worker 		if (nrecv == -1)
795*8b26181fSAndroid Build Coastguard Worker 		{
796*8b26181fSAndroid Build Coastguard Worker 			// Fatal error.
797*8b26181fSAndroid Build Coastguard Worker 			goto end;
798*8b26181fSAndroid Build Coastguard Worker 		}
799*8b26181fSAndroid Build Coastguard Worker 		if (nrecv == -2)
800*8b26181fSAndroid Build Coastguard Worker 		{
801*8b26181fSAndroid Build Coastguard Worker 			// Client closed the connection.
802*8b26181fSAndroid Build Coastguard Worker 			goto end;
803*8b26181fSAndroid Build Coastguard Worker 		}
804*8b26181fSAndroid Build Coastguard Worker 
805*8b26181fSAndroid Build Coastguard Worker 		plen = header.plen;
806*8b26181fSAndroid Build Coastguard Worker 
807*8b26181fSAndroid Build Coastguard Worker 		//
808*8b26181fSAndroid Build Coastguard Worker 		// Did the client specify a protocol version that we
809*8b26181fSAndroid Build Coastguard Worker 		// support?
810*8b26181fSAndroid Build Coastguard Worker 		//
811*8b26181fSAndroid Build Coastguard Worker 		if (!RPCAP_VERSION_IS_SUPPORTED(header.ver))
812*8b26181fSAndroid Build Coastguard Worker 		{
813*8b26181fSAndroid Build Coastguard Worker 			//
814*8b26181fSAndroid Build Coastguard Worker 			// Tell them it's not a supported version.
815*8b26181fSAndroid Build Coastguard Worker 			// Send the error message with their version,
816*8b26181fSAndroid Build Coastguard Worker 			// so they don't reject it as having the wrong
817*8b26181fSAndroid Build Coastguard Worker 			// version.
818*8b26181fSAndroid Build Coastguard Worker 			//
819*8b26181fSAndroid Build Coastguard Worker 			if (rpcap_senderror(pars.sockctrl, pars.ssl,
820*8b26181fSAndroid Build Coastguard Worker 			    header.ver, PCAP_ERR_WRONGVER,
821*8b26181fSAndroid Build Coastguard Worker 			    "RPCAP version in message isn't supported by the server",
822*8b26181fSAndroid Build Coastguard Worker 			    errbuf) == -1)
823*8b26181fSAndroid Build Coastguard Worker 			{
824*8b26181fSAndroid Build Coastguard Worker 				// That failed; log a message and give up.
825*8b26181fSAndroid Build Coastguard Worker 				rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
826*8b26181fSAndroid Build Coastguard Worker 				goto end;
827*8b26181fSAndroid Build Coastguard Worker 			}
828*8b26181fSAndroid Build Coastguard Worker 
829*8b26181fSAndroid Build Coastguard Worker 			// Discard the rest of the message.
830*8b26181fSAndroid Build Coastguard Worker 			(void)rpcapd_discard(pars.sockctrl, pars.ssl, plen);
831*8b26181fSAndroid Build Coastguard Worker 			// Give up on them.
832*8b26181fSAndroid Build Coastguard Worker 			goto end;
833*8b26181fSAndroid Build Coastguard Worker 		}
834*8b26181fSAndroid Build Coastguard Worker 
835*8b26181fSAndroid Build Coastguard Worker 		switch (header.type)
836*8b26181fSAndroid Build Coastguard Worker 		{
837*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_ERROR:		// The other endpoint reported an error
838*8b26181fSAndroid Build Coastguard Worker 			{
839*8b26181fSAndroid Build Coastguard Worker 				(void)daemon_msg_err(pars.sockctrl, pars.ssl, plen);
840*8b26181fSAndroid Build Coastguard Worker 				// Do nothing; just exit; the error code is already into the errbuf
841*8b26181fSAndroid Build Coastguard Worker 				// XXX - actually exit....
842*8b26181fSAndroid Build Coastguard Worker 				break;
843*8b26181fSAndroid Build Coastguard Worker 			}
844*8b26181fSAndroid Build Coastguard Worker 
845*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_FINDALLIF_REQ:
846*8b26181fSAndroid Build Coastguard Worker 			{
847*8b26181fSAndroid Build Coastguard Worker 				if (daemon_msg_findallif_req(header.ver, &pars, plen) == -1)
848*8b26181fSAndroid Build Coastguard Worker 				{
849*8b26181fSAndroid Build Coastguard Worker 					// Fatal error; a message has
850*8b26181fSAndroid Build Coastguard Worker 					// been logged, so just give up.
851*8b26181fSAndroid Build Coastguard Worker 					goto end;
852*8b26181fSAndroid Build Coastguard Worker 				}
853*8b26181fSAndroid Build Coastguard Worker 				break;
854*8b26181fSAndroid Build Coastguard Worker 			}
855*8b26181fSAndroid Build Coastguard Worker 
856*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_OPEN_REQ:
857*8b26181fSAndroid Build Coastguard Worker 			{
858*8b26181fSAndroid Build Coastguard Worker 				//
859*8b26181fSAndroid Build Coastguard Worker 				// Process the open request, and keep
860*8b26181fSAndroid Build Coastguard Worker 				// the source from it, for use later
861*8b26181fSAndroid Build Coastguard Worker 				// when the capture is started.
862*8b26181fSAndroid Build Coastguard Worker 				//
863*8b26181fSAndroid Build Coastguard Worker 				// XXX - we don't care if the client sends
864*8b26181fSAndroid Build Coastguard Worker 				// us multiple open requests, the last
865*8b26181fSAndroid Build Coastguard Worker 				// one wins.
866*8b26181fSAndroid Build Coastguard Worker 				//
867*8b26181fSAndroid Build Coastguard Worker 				retval = daemon_msg_open_req(header.ver, &pars,
868*8b26181fSAndroid Build Coastguard Worker 				    plen, source, sizeof(source));
869*8b26181fSAndroid Build Coastguard Worker 				if (retval == -1)
870*8b26181fSAndroid Build Coastguard Worker 				{
871*8b26181fSAndroid Build Coastguard Worker 					// Fatal error; a message has
872*8b26181fSAndroid Build Coastguard Worker 					// been logged, so just give up.
873*8b26181fSAndroid Build Coastguard Worker 					goto end;
874*8b26181fSAndroid Build Coastguard Worker 				}
875*8b26181fSAndroid Build Coastguard Worker 				got_source = 1;
876*8b26181fSAndroid Build Coastguard Worker 				break;
877*8b26181fSAndroid Build Coastguard Worker 			}
878*8b26181fSAndroid Build Coastguard Worker 
879*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_STARTCAP_REQ:
880*8b26181fSAndroid Build Coastguard Worker 			{
881*8b26181fSAndroid Build Coastguard Worker 				if (!got_source)
882*8b26181fSAndroid Build Coastguard Worker 				{
883*8b26181fSAndroid Build Coastguard Worker 					// They never told us what device
884*8b26181fSAndroid Build Coastguard Worker 					// to capture on!
885*8b26181fSAndroid Build Coastguard Worker 					if (rpcap_senderror(pars.sockctrl, pars.ssl,
886*8b26181fSAndroid Build Coastguard Worker 					    header.ver,
887*8b26181fSAndroid Build Coastguard Worker 					    PCAP_ERR_STARTCAPTURE,
888*8b26181fSAndroid Build Coastguard Worker 					    "No capture device was specified",
889*8b26181fSAndroid Build Coastguard Worker 					    errbuf) == -1)
890*8b26181fSAndroid Build Coastguard Worker 					{
891*8b26181fSAndroid Build Coastguard Worker 						// Fatal error; log an
892*8b26181fSAndroid Build Coastguard Worker 						// error and  give up.
893*8b26181fSAndroid Build Coastguard Worker 						rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
894*8b26181fSAndroid Build Coastguard Worker 						goto end;
895*8b26181fSAndroid Build Coastguard Worker 					}
896*8b26181fSAndroid Build Coastguard Worker 					if (rpcapd_discard(pars.sockctrl, pars.ssl, plen) == -1)
897*8b26181fSAndroid Build Coastguard Worker 					{
898*8b26181fSAndroid Build Coastguard Worker 						goto end;
899*8b26181fSAndroid Build Coastguard Worker 					}
900*8b26181fSAndroid Build Coastguard Worker 					break;
901*8b26181fSAndroid Build Coastguard Worker 				}
902*8b26181fSAndroid Build Coastguard Worker 
903*8b26181fSAndroid Build Coastguard Worker 				if (daemon_msg_startcap_req(header.ver, &pars,
904*8b26181fSAndroid Build Coastguard Worker 				    plen, source, &session, &samp_param,
905*8b26181fSAndroid Build Coastguard Worker 				    uses_ssl) == -1)
906*8b26181fSAndroid Build Coastguard Worker 				{
907*8b26181fSAndroid Build Coastguard Worker 					// Fatal error; a message has
908*8b26181fSAndroid Build Coastguard Worker 					// been logged, so just give up.
909*8b26181fSAndroid Build Coastguard Worker 					goto end;
910*8b26181fSAndroid Build Coastguard Worker 				}
911*8b26181fSAndroid Build Coastguard Worker 				break;
912*8b26181fSAndroid Build Coastguard Worker 			}
913*8b26181fSAndroid Build Coastguard Worker 
914*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_UPDATEFILTER_REQ:
915*8b26181fSAndroid Build Coastguard Worker 			{
916*8b26181fSAndroid Build Coastguard Worker 				if (session)
917*8b26181fSAndroid Build Coastguard Worker 				{
918*8b26181fSAndroid Build Coastguard Worker 					if (daemon_msg_updatefilter_req(header.ver,
919*8b26181fSAndroid Build Coastguard Worker 					    &pars, session, plen) == -1)
920*8b26181fSAndroid Build Coastguard Worker 					{
921*8b26181fSAndroid Build Coastguard Worker 						// Fatal error; a message has
922*8b26181fSAndroid Build Coastguard Worker 						// been logged, so just give up.
923*8b26181fSAndroid Build Coastguard Worker 						goto end;
924*8b26181fSAndroid Build Coastguard Worker 					}
925*8b26181fSAndroid Build Coastguard Worker 				}
926*8b26181fSAndroid Build Coastguard Worker 				else
927*8b26181fSAndroid Build Coastguard Worker 				{
928*8b26181fSAndroid Build Coastguard Worker 					if (rpcap_senderror(pars.sockctrl, pars.ssl,
929*8b26181fSAndroid Build Coastguard Worker 					    header.ver,
930*8b26181fSAndroid Build Coastguard Worker 					    PCAP_ERR_UPDATEFILTER,
931*8b26181fSAndroid Build Coastguard Worker 					    "Device not opened. Cannot update filter",
932*8b26181fSAndroid Build Coastguard Worker 					    errbuf) == -1)
933*8b26181fSAndroid Build Coastguard Worker 					{
934*8b26181fSAndroid Build Coastguard Worker 						// That failed; log a message and give up.
935*8b26181fSAndroid Build Coastguard Worker 						rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
936*8b26181fSAndroid Build Coastguard Worker 						goto end;
937*8b26181fSAndroid Build Coastguard Worker 					}
938*8b26181fSAndroid Build Coastguard Worker 				}
939*8b26181fSAndroid Build Coastguard Worker 				break;
940*8b26181fSAndroid Build Coastguard Worker 			}
941*8b26181fSAndroid Build Coastguard Worker 
942*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_CLOSE:		// The other endpoint close the pcap session
943*8b26181fSAndroid Build Coastguard Worker 			{
944*8b26181fSAndroid Build Coastguard Worker 				//
945*8b26181fSAndroid Build Coastguard Worker 				// Indicate to our caller that the client
946*8b26181fSAndroid Build Coastguard Worker 				// closed the control connection.
947*8b26181fSAndroid Build Coastguard Worker 				// This is used only in case of active mode.
948*8b26181fSAndroid Build Coastguard Worker 				//
949*8b26181fSAndroid Build Coastguard Worker 				client_told_us_to_close = 1;
950*8b26181fSAndroid Build Coastguard Worker 				rpcapd_log(LOGPRIO_DEBUG, "The other end system asked to close the connection.");
951*8b26181fSAndroid Build Coastguard Worker 				goto end;
952*8b26181fSAndroid Build Coastguard Worker 			}
953*8b26181fSAndroid Build Coastguard Worker 
954*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_STATS_REQ:
955*8b26181fSAndroid Build Coastguard Worker 			{
956*8b26181fSAndroid Build Coastguard Worker 				if (daemon_msg_stats_req(header.ver, &pars,
957*8b26181fSAndroid Build Coastguard Worker 				    session, plen, &stats, svrcapt) == -1)
958*8b26181fSAndroid Build Coastguard Worker 				{
959*8b26181fSAndroid Build Coastguard Worker 					// Fatal error; a message has
960*8b26181fSAndroid Build Coastguard Worker 					// been logged, so just give up.
961*8b26181fSAndroid Build Coastguard Worker 					goto end;
962*8b26181fSAndroid Build Coastguard Worker 				}
963*8b26181fSAndroid Build Coastguard Worker 				break;
964*8b26181fSAndroid Build Coastguard Worker 			}
965*8b26181fSAndroid Build Coastguard Worker 
966*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_ENDCAP_REQ:		// The other endpoint close the current capture session
967*8b26181fSAndroid Build Coastguard Worker 			{
968*8b26181fSAndroid Build Coastguard Worker 				if (session)
969*8b26181fSAndroid Build Coastguard Worker 				{
970*8b26181fSAndroid Build Coastguard Worker 					// Save statistics (we can need them in the future)
971*8b26181fSAndroid Build Coastguard Worker 					if (pcap_stats(session->fp, &stats))
972*8b26181fSAndroid Build Coastguard Worker 					{
973*8b26181fSAndroid Build Coastguard Worker 						svrcapt = session->TotCapt;
974*8b26181fSAndroid Build Coastguard Worker 					}
975*8b26181fSAndroid Build Coastguard Worker 					else
976*8b26181fSAndroid Build Coastguard Worker 					{
977*8b26181fSAndroid Build Coastguard Worker 						stats.ps_ifdrop = 0;
978*8b26181fSAndroid Build Coastguard Worker 						stats.ps_recv = 0;
979*8b26181fSAndroid Build Coastguard Worker 						stats.ps_drop = 0;
980*8b26181fSAndroid Build Coastguard Worker 						svrcapt = 0;
981*8b26181fSAndroid Build Coastguard Worker 					}
982*8b26181fSAndroid Build Coastguard Worker 
983*8b26181fSAndroid Build Coastguard Worker 					if (daemon_msg_endcap_req(header.ver,
984*8b26181fSAndroid Build Coastguard Worker 					    &pars, session) == -1)
985*8b26181fSAndroid Build Coastguard Worker 					{
986*8b26181fSAndroid Build Coastguard Worker 						free(session);
987*8b26181fSAndroid Build Coastguard Worker 						session = NULL;
988*8b26181fSAndroid Build Coastguard Worker 						// Fatal error; a message has
989*8b26181fSAndroid Build Coastguard Worker 						// been logged, so just give up.
990*8b26181fSAndroid Build Coastguard Worker 						goto end;
991*8b26181fSAndroid Build Coastguard Worker 					}
992*8b26181fSAndroid Build Coastguard Worker 					free(session);
993*8b26181fSAndroid Build Coastguard Worker 					session = NULL;
994*8b26181fSAndroid Build Coastguard Worker 				}
995*8b26181fSAndroid Build Coastguard Worker 				else
996*8b26181fSAndroid Build Coastguard Worker 				{
997*8b26181fSAndroid Build Coastguard Worker 					rpcap_senderror(pars.sockctrl, pars.ssl,
998*8b26181fSAndroid Build Coastguard Worker 					    header.ver,
999*8b26181fSAndroid Build Coastguard Worker 					    PCAP_ERR_ENDCAPTURE,
1000*8b26181fSAndroid Build Coastguard Worker 					    "Device not opened. Cannot close the capture",
1001*8b26181fSAndroid Build Coastguard Worker 					    errbuf);
1002*8b26181fSAndroid Build Coastguard Worker 				}
1003*8b26181fSAndroid Build Coastguard Worker 				break;
1004*8b26181fSAndroid Build Coastguard Worker 			}
1005*8b26181fSAndroid Build Coastguard Worker 
1006*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_SETSAMPLING_REQ:
1007*8b26181fSAndroid Build Coastguard Worker 			{
1008*8b26181fSAndroid Build Coastguard Worker 				if (daemon_msg_setsampling_req(header.ver,
1009*8b26181fSAndroid Build Coastguard Worker 				    &pars, plen, &samp_param) == -1)
1010*8b26181fSAndroid Build Coastguard Worker 				{
1011*8b26181fSAndroid Build Coastguard Worker 					// Fatal error; a message has
1012*8b26181fSAndroid Build Coastguard Worker 					// been logged, so just give up.
1013*8b26181fSAndroid Build Coastguard Worker 					goto end;
1014*8b26181fSAndroid Build Coastguard Worker 				}
1015*8b26181fSAndroid Build Coastguard Worker 				break;
1016*8b26181fSAndroid Build Coastguard Worker 			}
1017*8b26181fSAndroid Build Coastguard Worker 
1018*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_AUTH_REQ:
1019*8b26181fSAndroid Build Coastguard Worker 			{
1020*8b26181fSAndroid Build Coastguard Worker 				//
1021*8b26181fSAndroid Build Coastguard Worker 				// We're already authenticated; you don't
1022*8b26181fSAndroid Build Coastguard Worker 				// get to reauthenticate.
1023*8b26181fSAndroid Build Coastguard Worker 				//
1024*8b26181fSAndroid Build Coastguard Worker 				rpcapd_log(LOGPRIO_INFO, "The client sent an RPCAP_MSG_AUTH_REQ message after authentication was completed");
1025*8b26181fSAndroid Build Coastguard Worker 				if (rpcap_senderror(pars.sockctrl, pars.ssl,
1026*8b26181fSAndroid Build Coastguard Worker 				    header.ver,
1027*8b26181fSAndroid Build Coastguard Worker 				    PCAP_ERR_WRONGMSG,
1028*8b26181fSAndroid Build Coastguard Worker 				    "RPCAP_MSG_AUTH_REQ request sent after authentication was completed",
1029*8b26181fSAndroid Build Coastguard Worker 				    errbuf) == -1)
1030*8b26181fSAndroid Build Coastguard Worker 				{
1031*8b26181fSAndroid Build Coastguard Worker 					rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
1032*8b26181fSAndroid Build Coastguard Worker 					goto end;
1033*8b26181fSAndroid Build Coastguard Worker 				}
1034*8b26181fSAndroid Build Coastguard Worker 				// Discard the rest of the message.
1035*8b26181fSAndroid Build Coastguard Worker 				if (rpcapd_discard(pars.sockctrl, pars.ssl, plen) == -1)
1036*8b26181fSAndroid Build Coastguard Worker 				{
1037*8b26181fSAndroid Build Coastguard Worker 					// Fatal error.
1038*8b26181fSAndroid Build Coastguard Worker 					goto end;
1039*8b26181fSAndroid Build Coastguard Worker 				}
1040*8b26181fSAndroid Build Coastguard Worker 				goto end;
1041*8b26181fSAndroid Build Coastguard Worker 
1042*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_PACKET:
1043*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_FINDALLIF_REPLY:
1044*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_OPEN_REPLY:
1045*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_STARTCAP_REPLY:
1046*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_UPDATEFILTER_REPLY:
1047*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_AUTH_REPLY:
1048*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_STATS_REPLY:
1049*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_ENDCAP_REPLY:
1050*8b26181fSAndroid Build Coastguard Worker 			case RPCAP_MSG_SETSAMPLING_REPLY:
1051*8b26181fSAndroid Build Coastguard Worker 				//
1052*8b26181fSAndroid Build Coastguard Worker 				// These are server-to-client messages.
1053*8b26181fSAndroid Build Coastguard Worker 				//
1054*8b26181fSAndroid Build Coastguard Worker 				msg_type_string = rpcap_msg_type_string(header.type);
1055*8b26181fSAndroid Build Coastguard Worker 				if (msg_type_string != NULL)
1056*8b26181fSAndroid Build Coastguard Worker 				{
1057*8b26181fSAndroid Build Coastguard Worker 					rpcapd_log(LOGPRIO_INFO, "The client sent a %s server-to-client message", msg_type_string);
1058*8b26181fSAndroid Build Coastguard Worker 					snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Server-to-client message %s received from client", msg_type_string);
1059*8b26181fSAndroid Build Coastguard Worker 				}
1060*8b26181fSAndroid Build Coastguard Worker 				else
1061*8b26181fSAndroid Build Coastguard Worker 				{
1062*8b26181fSAndroid Build Coastguard Worker 					rpcapd_log(LOGPRIO_INFO, "The client sent a server-to-client message of type %u", header.type);
1063*8b26181fSAndroid Build Coastguard Worker 					snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Server-to-client message of type %u received from client", header.type);
1064*8b26181fSAndroid Build Coastguard Worker 				}
1065*8b26181fSAndroid Build Coastguard Worker 				if (rpcap_senderror(pars.sockctrl, pars.ssl,
1066*8b26181fSAndroid Build Coastguard Worker 				    header.ver, PCAP_ERR_WRONGMSG,
1067*8b26181fSAndroid Build Coastguard Worker 				    errmsgbuf, errbuf) == -1)
1068*8b26181fSAndroid Build Coastguard Worker 				{
1069*8b26181fSAndroid Build Coastguard Worker 					rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
1070*8b26181fSAndroid Build Coastguard Worker 					goto end;
1071*8b26181fSAndroid Build Coastguard Worker 				}
1072*8b26181fSAndroid Build Coastguard Worker 				// Discard the rest of the message.
1073*8b26181fSAndroid Build Coastguard Worker 				if (rpcapd_discard(pars.sockctrl, pars.ssl, plen) == -1)
1074*8b26181fSAndroid Build Coastguard Worker 				{
1075*8b26181fSAndroid Build Coastguard Worker 					// Fatal error.
1076*8b26181fSAndroid Build Coastguard Worker 					goto end;
1077*8b26181fSAndroid Build Coastguard Worker 				}
1078*8b26181fSAndroid Build Coastguard Worker 				goto end;
1079*8b26181fSAndroid Build Coastguard Worker 
1080*8b26181fSAndroid Build Coastguard Worker 			default:
1081*8b26181fSAndroid Build Coastguard Worker 				//
1082*8b26181fSAndroid Build Coastguard Worker 				// Unknown message type.
1083*8b26181fSAndroid Build Coastguard Worker 				//
1084*8b26181fSAndroid Build Coastguard Worker 				rpcapd_log(LOGPRIO_INFO, "The client sent a message of type %u", header.type);
1085*8b26181fSAndroid Build Coastguard Worker 				snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Unknown message type %u", header.type);
1086*8b26181fSAndroid Build Coastguard Worker 				if (rpcap_senderror(pars.sockctrl, pars.ssl,
1087*8b26181fSAndroid Build Coastguard Worker 				    header.ver, PCAP_ERR_WRONGMSG,
1088*8b26181fSAndroid Build Coastguard Worker 				    errbuf, errmsgbuf) == -1)
1089*8b26181fSAndroid Build Coastguard Worker 				{
1090*8b26181fSAndroid Build Coastguard Worker 					rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
1091*8b26181fSAndroid Build Coastguard Worker 					goto end;
1092*8b26181fSAndroid Build Coastguard Worker 				}
1093*8b26181fSAndroid Build Coastguard Worker 				// Discard the rest of the message.
1094*8b26181fSAndroid Build Coastguard Worker 				if (rpcapd_discard(pars.sockctrl, pars.ssl, plen) == -1)
1095*8b26181fSAndroid Build Coastguard Worker 				{
1096*8b26181fSAndroid Build Coastguard Worker 					// Fatal error.
1097*8b26181fSAndroid Build Coastguard Worker 					goto end;
1098*8b26181fSAndroid Build Coastguard Worker 				}
1099*8b26181fSAndroid Build Coastguard Worker 				goto end;
1100*8b26181fSAndroid Build Coastguard Worker 			}
1101*8b26181fSAndroid Build Coastguard Worker 		}
1102*8b26181fSAndroid Build Coastguard Worker 	}
1103*8b26181fSAndroid Build Coastguard Worker 
1104*8b26181fSAndroid Build Coastguard Worker end:
1105*8b26181fSAndroid Build Coastguard Worker 	// The service loop is finishing up.
1106*8b26181fSAndroid Build Coastguard Worker 	// If we have a capture session running, close it.
1107*8b26181fSAndroid Build Coastguard Worker 	if (session)
1108*8b26181fSAndroid Build Coastguard Worker 	{
1109*8b26181fSAndroid Build Coastguard Worker 		session_close(session);
1110*8b26181fSAndroid Build Coastguard Worker 		free(session);
1111*8b26181fSAndroid Build Coastguard Worker 		session = NULL;
1112*8b26181fSAndroid Build Coastguard Worker 	}
1113*8b26181fSAndroid Build Coastguard Worker 
1114*8b26181fSAndroid Build Coastguard Worker 	if (passiveClients) {
1115*8b26181fSAndroid Build Coastguard Worker 		free(passiveClients);
1116*8b26181fSAndroid Build Coastguard Worker 	}
1117*8b26181fSAndroid Build Coastguard Worker 	//
1118*8b26181fSAndroid Build Coastguard Worker 	// Finish using the SSL handle for the control socket, if we
1119*8b26181fSAndroid Build Coastguard Worker 	// have an SSL connection, and close the control socket.
1120*8b26181fSAndroid Build Coastguard Worker 	//
1121*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OPENSSL
1122*8b26181fSAndroid Build Coastguard Worker 	if (ssl)
1123*8b26181fSAndroid Build Coastguard Worker 	{
1124*8b26181fSAndroid Build Coastguard Worker 		// Finish using the SSL handle for the socket.
1125*8b26181fSAndroid Build Coastguard Worker 		// This must be done *before* the socket is closed.
1126*8b26181fSAndroid Build Coastguard Worker 		ssl_finish(ssl);
1127*8b26181fSAndroid Build Coastguard Worker 	}
1128*8b26181fSAndroid Build Coastguard Worker #endif
1129*8b26181fSAndroid Build Coastguard Worker 	sock_close(sockctrl, NULL, 0);
1130*8b26181fSAndroid Build Coastguard Worker 
1131*8b26181fSAndroid Build Coastguard Worker 	// Print message and return
1132*8b26181fSAndroid Build Coastguard Worker 	rpcapd_log(LOGPRIO_DEBUG, "I'm exiting from the child loop");
1133*8b26181fSAndroid Build Coastguard Worker 
1134*8b26181fSAndroid Build Coastguard Worker 	return client_told_us_to_close;
1135*8b26181fSAndroid Build Coastguard Worker }
1136*8b26181fSAndroid Build Coastguard Worker 
1137*8b26181fSAndroid Build Coastguard Worker /*
1138*8b26181fSAndroid Build Coastguard Worker  * This handles the RPCAP_MSG_ERR message.
1139*8b26181fSAndroid Build Coastguard Worker  */
1140*8b26181fSAndroid Build Coastguard Worker static int
daemon_msg_err(SOCKET sockctrl,SSL * ssl,uint32 plen)1141*8b26181fSAndroid Build Coastguard Worker daemon_msg_err(SOCKET sockctrl, SSL *ssl, uint32 plen)
1142*8b26181fSAndroid Build Coastguard Worker {
1143*8b26181fSAndroid Build Coastguard Worker 	char errbuf[PCAP_ERRBUF_SIZE];
1144*8b26181fSAndroid Build Coastguard Worker 	char remote_errbuf[PCAP_ERRBUF_SIZE];
1145*8b26181fSAndroid Build Coastguard Worker 
1146*8b26181fSAndroid Build Coastguard Worker 	if (plen >= PCAP_ERRBUF_SIZE)
1147*8b26181fSAndroid Build Coastguard Worker 	{
1148*8b26181fSAndroid Build Coastguard Worker 		/*
1149*8b26181fSAndroid Build Coastguard Worker 		 * Message is too long; just read as much of it as we
1150*8b26181fSAndroid Build Coastguard Worker 		 * can into the buffer provided, and discard the rest.
1151*8b26181fSAndroid Build Coastguard Worker 		 */
1152*8b26181fSAndroid Build Coastguard Worker 		if (sock_recv(sockctrl, ssl, remote_errbuf, PCAP_ERRBUF_SIZE - 1,
1153*8b26181fSAndroid Build Coastguard Worker 		    SOCK_RECEIVEALL_YES|SOCK_EOF_IS_ERROR, errbuf,
1154*8b26181fSAndroid Build Coastguard Worker 		    PCAP_ERRBUF_SIZE) == -1)
1155*8b26181fSAndroid Build Coastguard Worker 		{
1156*8b26181fSAndroid Build Coastguard Worker 			// Network error.
1157*8b26181fSAndroid Build Coastguard Worker 			rpcapd_log(LOGPRIO_ERROR, "Read from client failed: %s", errbuf);
1158*8b26181fSAndroid Build Coastguard Worker 			return -1;
1159*8b26181fSAndroid Build Coastguard Worker 		}
1160*8b26181fSAndroid Build Coastguard Worker 		if (rpcapd_discard(sockctrl, ssl, plen - (PCAP_ERRBUF_SIZE - 1)) == -1)
1161*8b26181fSAndroid Build Coastguard Worker 		{
1162*8b26181fSAndroid Build Coastguard Worker 			// Network error.
1163*8b26181fSAndroid Build Coastguard Worker 			return -1;
1164*8b26181fSAndroid Build Coastguard Worker 		}
1165*8b26181fSAndroid Build Coastguard Worker 
1166*8b26181fSAndroid Build Coastguard Worker 		/*
1167*8b26181fSAndroid Build Coastguard Worker 		 * Null-terminate it.
1168*8b26181fSAndroid Build Coastguard Worker 		 */
1169*8b26181fSAndroid Build Coastguard Worker 		remote_errbuf[PCAP_ERRBUF_SIZE - 1] = '\0';
1170*8b26181fSAndroid Build Coastguard Worker 	}
1171*8b26181fSAndroid Build Coastguard Worker 	else if (plen == 0)
1172*8b26181fSAndroid Build Coastguard Worker 	{
1173*8b26181fSAndroid Build Coastguard Worker 		/* Empty error string. */
1174*8b26181fSAndroid Build Coastguard Worker 		remote_errbuf[0] = '\0';
1175*8b26181fSAndroid Build Coastguard Worker 	}
1176*8b26181fSAndroid Build Coastguard Worker 	else
1177*8b26181fSAndroid Build Coastguard Worker 	{
1178*8b26181fSAndroid Build Coastguard Worker 		if (sock_recv(sockctrl, ssl, remote_errbuf, plen,
1179*8b26181fSAndroid Build Coastguard Worker 		    SOCK_RECEIVEALL_YES|SOCK_EOF_IS_ERROR, errbuf,
1180*8b26181fSAndroid Build Coastguard Worker 		    PCAP_ERRBUF_SIZE) == -1)
1181*8b26181fSAndroid Build Coastguard Worker 		{
1182*8b26181fSAndroid Build Coastguard Worker 			// Network error.
1183*8b26181fSAndroid Build Coastguard Worker 			rpcapd_log(LOGPRIO_ERROR, "Read from client failed: %s", errbuf);
1184*8b26181fSAndroid Build Coastguard Worker 			return -1;
1185*8b26181fSAndroid Build Coastguard Worker 		}
1186*8b26181fSAndroid Build Coastguard Worker 
1187*8b26181fSAndroid Build Coastguard Worker 		/*
1188*8b26181fSAndroid Build Coastguard Worker 		 * Null-terminate it.
1189*8b26181fSAndroid Build Coastguard Worker 		 */
1190*8b26181fSAndroid Build Coastguard Worker 		remote_errbuf[plen] = '\0';
1191*8b26181fSAndroid Build Coastguard Worker 	}
1192*8b26181fSAndroid Build Coastguard Worker 	// Log the message
1193*8b26181fSAndroid Build Coastguard Worker 	rpcapd_log(LOGPRIO_ERROR, "Error from client: %s", remote_errbuf);
1194*8b26181fSAndroid Build Coastguard Worker 	return 0;
1195*8b26181fSAndroid Build Coastguard Worker }
1196*8b26181fSAndroid Build Coastguard Worker 
1197*8b26181fSAndroid Build Coastguard Worker /*
1198*8b26181fSAndroid Build Coastguard Worker  * This handles the RPCAP_MSG_AUTH_REQ message.
1199*8b26181fSAndroid Build Coastguard Worker  * It checks if the authentication credentials supplied by the user are valid.
1200*8b26181fSAndroid Build Coastguard Worker  *
1201*8b26181fSAndroid Build Coastguard Worker  * This function is called if the daemon receives a RPCAP_MSG_AUTH_REQ
1202*8b26181fSAndroid Build Coastguard Worker  * message in its authentication loop.  It reads the body of the
1203*8b26181fSAndroid Build Coastguard Worker  * authentication message from the network and checks whether the
1204*8b26181fSAndroid Build Coastguard Worker  * credentials are valid.
1205*8b26181fSAndroid Build Coastguard Worker  *
1206*8b26181fSAndroid Build Coastguard Worker  * \param sockctrl: the socket for the control connection.
1207*8b26181fSAndroid Build Coastguard Worker  *
1208*8b26181fSAndroid Build Coastguard Worker  * \param nullAuthAllowed: '1' if the NULL authentication is allowed.
1209*8b26181fSAndroid Build Coastguard Worker  *
1210*8b26181fSAndroid Build Coastguard Worker  * \param errbuf: a user-allocated buffer in which the error message
1211*8b26181fSAndroid Build Coastguard Worker  * (if one) has to be written.  It must be at least PCAP_ERRBUF_SIZE
1212*8b26181fSAndroid Build Coastguard Worker  * bytes long.
1213*8b26181fSAndroid Build Coastguard Worker  *
1214*8b26181fSAndroid Build Coastguard Worker  * \return '0' if everything is fine, '-1' if an unrecoverable error occurred,
1215*8b26181fSAndroid Build Coastguard Worker  * or '-2' if the authentication failed.  For errors, an error message is
1216*8b26181fSAndroid Build Coastguard Worker  * returned in the 'errbuf' variable; this gives a message for the
1217*8b26181fSAndroid Build Coastguard Worker  * unrecoverable error or for the authentication failure.
1218*8b26181fSAndroid Build Coastguard Worker  */
1219*8b26181fSAndroid Build Coastguard Worker static int
daemon_msg_auth_req(struct daemon_slpars * pars,uint32 plen)1220*8b26181fSAndroid Build Coastguard Worker daemon_msg_auth_req(struct daemon_slpars *pars, uint32 plen)
1221*8b26181fSAndroid Build Coastguard Worker {
1222*8b26181fSAndroid Build Coastguard Worker 	char errbuf[PCAP_ERRBUF_SIZE];		// buffer for network errors
1223*8b26181fSAndroid Build Coastguard Worker 	char errmsgbuf[PCAP_ERRBUF_SIZE];	// buffer for errors to send to the client
1224*8b26181fSAndroid Build Coastguard Worker 	int status;
1225*8b26181fSAndroid Build Coastguard Worker 	struct rpcap_auth auth;			// RPCAP authentication header
1226*8b26181fSAndroid Build Coastguard Worker 	char sendbuf[RPCAP_NETBUF_SIZE];	// temporary buffer in which data to be sent is buffered
1227*8b26181fSAndroid Build Coastguard Worker 	int sendbufidx = 0;			// index which keeps the number of bytes currently buffered
1228*8b26181fSAndroid Build Coastguard Worker 	struct rpcap_authreply *authreply;	// authentication reply message
1229*8b26181fSAndroid Build Coastguard Worker 
1230*8b26181fSAndroid Build Coastguard Worker 	status = rpcapd_recv(pars->sockctrl, pars->ssl, (char *) &auth, sizeof(struct rpcap_auth), &plen, errmsgbuf);
1231*8b26181fSAndroid Build Coastguard Worker 	if (status == -1)
1232*8b26181fSAndroid Build Coastguard Worker 	{
1233*8b26181fSAndroid Build Coastguard Worker 		return -1;
1234*8b26181fSAndroid Build Coastguard Worker 	}
1235*8b26181fSAndroid Build Coastguard Worker 	if (status == -2)
1236*8b26181fSAndroid Build Coastguard Worker 	{
1237*8b26181fSAndroid Build Coastguard Worker 		goto error;
1238*8b26181fSAndroid Build Coastguard Worker 	}
1239*8b26181fSAndroid Build Coastguard Worker 
1240*8b26181fSAndroid Build Coastguard Worker 	switch (ntohs(auth.type))
1241*8b26181fSAndroid Build Coastguard Worker 	{
1242*8b26181fSAndroid Build Coastguard Worker 		case RPCAP_RMTAUTH_NULL:
1243*8b26181fSAndroid Build Coastguard Worker 		{
1244*8b26181fSAndroid Build Coastguard Worker 			if (!pars->nullAuthAllowed)
1245*8b26181fSAndroid Build Coastguard Worker 			{
1246*8b26181fSAndroid Build Coastguard Worker 				// Send the client an error reply.
1247*8b26181fSAndroid Build Coastguard Worker 				snprintf(errmsgbuf, PCAP_ERRBUF_SIZE,
1248*8b26181fSAndroid Build Coastguard Worker 				    "Authentication failed; NULL authentication not permitted.");
1249*8b26181fSAndroid Build Coastguard Worker 				if (rpcap_senderror(pars->sockctrl, pars->ssl,
1250*8b26181fSAndroid Build Coastguard Worker 				    0, PCAP_ERR_AUTH_FAILED, errmsgbuf, errbuf) == -1)
1251*8b26181fSAndroid Build Coastguard Worker 				{
1252*8b26181fSAndroid Build Coastguard Worker 					// That failed; log a message and give up.
1253*8b26181fSAndroid Build Coastguard Worker 					rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
1254*8b26181fSAndroid Build Coastguard Worker 					return -1;
1255*8b26181fSAndroid Build Coastguard Worker 				}
1256*8b26181fSAndroid Build Coastguard Worker 				goto error_noreply;
1257*8b26181fSAndroid Build Coastguard Worker 			}
1258*8b26181fSAndroid Build Coastguard Worker 			break;
1259*8b26181fSAndroid Build Coastguard Worker 		}
1260*8b26181fSAndroid Build Coastguard Worker 
1261*8b26181fSAndroid Build Coastguard Worker 		case RPCAP_RMTAUTH_PWD:
1262*8b26181fSAndroid Build Coastguard Worker 		{
1263*8b26181fSAndroid Build Coastguard Worker 			char *username, *passwd;
1264*8b26181fSAndroid Build Coastguard Worker 			uint32 usernamelen, passwdlen;
1265*8b26181fSAndroid Build Coastguard Worker 
1266*8b26181fSAndroid Build Coastguard Worker 			usernamelen = ntohs(auth.slen1);
1267*8b26181fSAndroid Build Coastguard Worker 			username = (char *) malloc (usernamelen + 1);
1268*8b26181fSAndroid Build Coastguard Worker 			if (username == NULL)
1269*8b26181fSAndroid Build Coastguard Worker 			{
1270*8b26181fSAndroid Build Coastguard Worker 				pcap_fmt_errmsg_for_errno(errmsgbuf,
1271*8b26181fSAndroid Build Coastguard Worker 				    PCAP_ERRBUF_SIZE, errno, "malloc() failed");
1272*8b26181fSAndroid Build Coastguard Worker 				goto error;
1273*8b26181fSAndroid Build Coastguard Worker 			}
1274*8b26181fSAndroid Build Coastguard Worker 			status = rpcapd_recv(pars->sockctrl, pars->ssl, username, usernamelen, &plen, errmsgbuf);
1275*8b26181fSAndroid Build Coastguard Worker 			if (status == -1)
1276*8b26181fSAndroid Build Coastguard Worker 			{
1277*8b26181fSAndroid Build Coastguard Worker 				free(username);
1278*8b26181fSAndroid Build Coastguard Worker 				return -1;
1279*8b26181fSAndroid Build Coastguard Worker 			}
1280*8b26181fSAndroid Build Coastguard Worker 			if (status == -2)
1281*8b26181fSAndroid Build Coastguard Worker 			{
1282*8b26181fSAndroid Build Coastguard Worker 				free(username);
1283*8b26181fSAndroid Build Coastguard Worker 				goto error;
1284*8b26181fSAndroid Build Coastguard Worker 			}
1285*8b26181fSAndroid Build Coastguard Worker 			username[usernamelen] = '\0';
1286*8b26181fSAndroid Build Coastguard Worker 
1287*8b26181fSAndroid Build Coastguard Worker 			passwdlen = ntohs(auth.slen2);
1288*8b26181fSAndroid Build Coastguard Worker 			passwd = (char *) malloc (passwdlen + 1);
1289*8b26181fSAndroid Build Coastguard Worker 			if (passwd == NULL)
1290*8b26181fSAndroid Build Coastguard Worker 			{
1291*8b26181fSAndroid Build Coastguard Worker 				pcap_fmt_errmsg_for_errno(errmsgbuf,
1292*8b26181fSAndroid Build Coastguard Worker 				    PCAP_ERRBUF_SIZE, errno, "malloc() failed");
1293*8b26181fSAndroid Build Coastguard Worker 				free(username);
1294*8b26181fSAndroid Build Coastguard Worker 				goto error;
1295*8b26181fSAndroid Build Coastguard Worker 			}
1296*8b26181fSAndroid Build Coastguard Worker 			status = rpcapd_recv(pars->sockctrl, pars->ssl, passwd, passwdlen, &plen, errmsgbuf);
1297*8b26181fSAndroid Build Coastguard Worker 			if (status == -1)
1298*8b26181fSAndroid Build Coastguard Worker 			{
1299*8b26181fSAndroid Build Coastguard Worker 				free(username);
1300*8b26181fSAndroid Build Coastguard Worker 				free(passwd);
1301*8b26181fSAndroid Build Coastguard Worker 				return -1;
1302*8b26181fSAndroid Build Coastguard Worker 			}
1303*8b26181fSAndroid Build Coastguard Worker 			if (status == -2)
1304*8b26181fSAndroid Build Coastguard Worker 			{
1305*8b26181fSAndroid Build Coastguard Worker 				free(username);
1306*8b26181fSAndroid Build Coastguard Worker 				free(passwd);
1307*8b26181fSAndroid Build Coastguard Worker 				goto error;
1308*8b26181fSAndroid Build Coastguard Worker 			}
1309*8b26181fSAndroid Build Coastguard Worker 			passwd[passwdlen] = '\0';
1310*8b26181fSAndroid Build Coastguard Worker 
1311*8b26181fSAndroid Build Coastguard Worker 			if (daemon_AuthUserPwd(username, passwd, errmsgbuf))
1312*8b26181fSAndroid Build Coastguard Worker 			{
1313*8b26181fSAndroid Build Coastguard Worker 				//
1314*8b26181fSAndroid Build Coastguard Worker 				// Authentication failed.  Let the client
1315*8b26181fSAndroid Build Coastguard Worker 				// know.
1316*8b26181fSAndroid Build Coastguard Worker 				//
1317*8b26181fSAndroid Build Coastguard Worker 				free(username);
1318*8b26181fSAndroid Build Coastguard Worker 				free(passwd);
1319*8b26181fSAndroid Build Coastguard Worker 				if (rpcap_senderror(pars->sockctrl, pars->ssl,
1320*8b26181fSAndroid Build Coastguard Worker 				    0, PCAP_ERR_AUTH_FAILED, errmsgbuf, errbuf) == -1)
1321*8b26181fSAndroid Build Coastguard Worker 				{
1322*8b26181fSAndroid Build Coastguard Worker 					// That failed; log a message and give up.
1323*8b26181fSAndroid Build Coastguard Worker 					rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
1324*8b26181fSAndroid Build Coastguard Worker 					return -1;
1325*8b26181fSAndroid Build Coastguard Worker 				}
1326*8b26181fSAndroid Build Coastguard Worker 
1327*8b26181fSAndroid Build Coastguard Worker 				//
1328*8b26181fSAndroid Build Coastguard Worker 				// Suspend for 1 second, so that they can't
1329*8b26181fSAndroid Build Coastguard Worker 				// hammer us with repeated tries with an
1330*8b26181fSAndroid Build Coastguard Worker 				// attack such as a dictionary attack.
1331*8b26181fSAndroid Build Coastguard Worker 				//
1332*8b26181fSAndroid Build Coastguard Worker 				// WARNING: this delay is inserted only
1333*8b26181fSAndroid Build Coastguard Worker 				// at this point; if the client closes the
1334*8b26181fSAndroid Build Coastguard Worker 				// connection and reconnects, the suspension
1335*8b26181fSAndroid Build Coastguard Worker 				// time does not have any effect.
1336*8b26181fSAndroid Build Coastguard Worker 				//
1337*8b26181fSAndroid Build Coastguard Worker 				sleep_secs(RPCAP_SUSPEND_WRONGAUTH);
1338*8b26181fSAndroid Build Coastguard Worker 				goto error_noreply;
1339*8b26181fSAndroid Build Coastguard Worker 			}
1340*8b26181fSAndroid Build Coastguard Worker 
1341*8b26181fSAndroid Build Coastguard Worker 			free(username);
1342*8b26181fSAndroid Build Coastguard Worker 			free(passwd);
1343*8b26181fSAndroid Build Coastguard Worker 			break;
1344*8b26181fSAndroid Build Coastguard Worker 			}
1345*8b26181fSAndroid Build Coastguard Worker 
1346*8b26181fSAndroid Build Coastguard Worker 		default:
1347*8b26181fSAndroid Build Coastguard Worker 			snprintf(errmsgbuf, PCAP_ERRBUF_SIZE,
1348*8b26181fSAndroid Build Coastguard Worker 			    "Authentication type not recognized.");
1349*8b26181fSAndroid Build Coastguard Worker 			if (rpcap_senderror(pars->sockctrl, pars->ssl,
1350*8b26181fSAndroid Build Coastguard Worker 			    0, PCAP_ERR_AUTH_TYPE_NOTSUP, errmsgbuf, errbuf) == -1)
1351*8b26181fSAndroid Build Coastguard Worker 			{
1352*8b26181fSAndroid Build Coastguard Worker 				// That failed; log a message and give up.
1353*8b26181fSAndroid Build Coastguard Worker 				rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
1354*8b26181fSAndroid Build Coastguard Worker 				return -1;
1355*8b26181fSAndroid Build Coastguard Worker 			}
1356*8b26181fSAndroid Build Coastguard Worker 			goto error_noreply;
1357*8b26181fSAndroid Build Coastguard Worker 	}
1358*8b26181fSAndroid Build Coastguard Worker 
1359*8b26181fSAndroid Build Coastguard Worker 	// The authentication succeeded; let the client know.
1360*8b26181fSAndroid Build Coastguard Worker 	if (sock_bufferize(NULL, sizeof(struct rpcap_header), NULL, &sendbufidx,
1361*8b26181fSAndroid Build Coastguard Worker 	    RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
1362*8b26181fSAndroid Build Coastguard Worker 		goto error;
1363*8b26181fSAndroid Build Coastguard Worker 
1364*8b26181fSAndroid Build Coastguard Worker 	rpcap_createhdr((struct rpcap_header *) sendbuf, 0,
1365*8b26181fSAndroid Build Coastguard Worker 	    RPCAP_MSG_AUTH_REPLY, 0, sizeof(struct rpcap_authreply));
1366*8b26181fSAndroid Build Coastguard Worker 
1367*8b26181fSAndroid Build Coastguard Worker 	authreply = (struct rpcap_authreply *) &sendbuf[sendbufidx];
1368*8b26181fSAndroid Build Coastguard Worker 
1369*8b26181fSAndroid Build Coastguard Worker 	if (sock_bufferize(NULL, sizeof(struct rpcap_authreply), NULL, &sendbufidx,
1370*8b26181fSAndroid Build Coastguard Worker 	    RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
1371*8b26181fSAndroid Build Coastguard Worker 		goto error;
1372*8b26181fSAndroid Build Coastguard Worker 
1373*8b26181fSAndroid Build Coastguard Worker 	//
1374*8b26181fSAndroid Build Coastguard Worker 	// Indicate to our peer what versions we support and what our
1375*8b26181fSAndroid Build Coastguard Worker 	// version of the byte-order magic is (which will tell the
1376*8b26181fSAndroid Build Coastguard Worker 	// client whether our byte order differs from theirs, in which
1377*8b26181fSAndroid Build Coastguard Worker 	// case they will need to byte-swap some fields in some
1378*8b26181fSAndroid Build Coastguard Worker 	// link-layer types' headers).
1379*8b26181fSAndroid Build Coastguard Worker 	//
1380*8b26181fSAndroid Build Coastguard Worker 	memset(authreply, 0, sizeof(struct rpcap_authreply));
1381*8b26181fSAndroid Build Coastguard Worker 	authreply->minvers = RPCAP_MIN_VERSION;
1382*8b26181fSAndroid Build Coastguard Worker 	authreply->maxvers = RPCAP_MAX_VERSION;
1383*8b26181fSAndroid Build Coastguard Worker 	authreply->byte_order_magic = RPCAP_BYTE_ORDER_MAGIC;
1384*8b26181fSAndroid Build Coastguard Worker 
1385*8b26181fSAndroid Build Coastguard Worker 	// Send the reply.
1386*8b26181fSAndroid Build Coastguard Worker 	if (sock_send(pars->sockctrl, pars->ssl, sendbuf, sendbufidx, errbuf, PCAP_ERRBUF_SIZE) == -1)
1387*8b26181fSAndroid Build Coastguard Worker 	{
1388*8b26181fSAndroid Build Coastguard Worker 		// That failed; log a message and give up.
1389*8b26181fSAndroid Build Coastguard Worker 		rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
1390*8b26181fSAndroid Build Coastguard Worker 		return -1;
1391*8b26181fSAndroid Build Coastguard Worker 	}
1392*8b26181fSAndroid Build Coastguard Worker 
1393*8b26181fSAndroid Build Coastguard Worker 	// Check if all the data has been read; if not, discard the data in excess
1394*8b26181fSAndroid Build Coastguard Worker 	if (rpcapd_discard(pars->sockctrl, pars->ssl, plen) == -1)
1395*8b26181fSAndroid Build Coastguard Worker 	{
1396*8b26181fSAndroid Build Coastguard Worker 		return -1;
1397*8b26181fSAndroid Build Coastguard Worker 	}
1398*8b26181fSAndroid Build Coastguard Worker 
1399*8b26181fSAndroid Build Coastguard Worker 	return 0;
1400*8b26181fSAndroid Build Coastguard Worker 
1401*8b26181fSAndroid Build Coastguard Worker error:
1402*8b26181fSAndroid Build Coastguard Worker 	if (rpcap_senderror(pars->sockctrl, pars->ssl, 0, PCAP_ERR_AUTH,
1403*8b26181fSAndroid Build Coastguard Worker 	    errmsgbuf, errbuf) == -1)
1404*8b26181fSAndroid Build Coastguard Worker 	{
1405*8b26181fSAndroid Build Coastguard Worker 		// That failed; log a message and give up.
1406*8b26181fSAndroid Build Coastguard Worker 		rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
1407*8b26181fSAndroid Build Coastguard Worker 		return -1;
1408*8b26181fSAndroid Build Coastguard Worker 	}
1409*8b26181fSAndroid Build Coastguard Worker 
1410*8b26181fSAndroid Build Coastguard Worker error_noreply:
1411*8b26181fSAndroid Build Coastguard Worker 	// Check if all the data has been read; if not, discard the data in excess
1412*8b26181fSAndroid Build Coastguard Worker 	if (rpcapd_discard(pars->sockctrl, pars->ssl, plen) == -1)
1413*8b26181fSAndroid Build Coastguard Worker 	{
1414*8b26181fSAndroid Build Coastguard Worker 		return -1;
1415*8b26181fSAndroid Build Coastguard Worker 	}
1416*8b26181fSAndroid Build Coastguard Worker 
1417*8b26181fSAndroid Build Coastguard Worker 	return -2;
1418*8b26181fSAndroid Build Coastguard Worker }
1419*8b26181fSAndroid Build Coastguard Worker 
1420*8b26181fSAndroid Build Coastguard Worker static int
daemon_AuthUserPwd(char * username,char * password,char * errbuf)1421*8b26181fSAndroid Build Coastguard Worker daemon_AuthUserPwd(char *username, char *password, char *errbuf)
1422*8b26181fSAndroid Build Coastguard Worker {
1423*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
1424*8b26181fSAndroid Build Coastguard Worker 	/*
1425*8b26181fSAndroid Build Coastguard Worker 	 * Warning: the user which launches the process must have the
1426*8b26181fSAndroid Build Coastguard Worker 	 * SE_TCB_NAME right.
1427*8b26181fSAndroid Build Coastguard Worker 	 * This corresponds to have the "Act as part of the Operating System"
1428*8b26181fSAndroid Build Coastguard Worker 	 * turned on (administrative tools, local security settings, local
1429*8b26181fSAndroid Build Coastguard Worker 	 * policies, user right assignment)
1430*8b26181fSAndroid Build Coastguard Worker 	 * However, it seems to me that if you run it as a service, this
1431*8b26181fSAndroid Build Coastguard Worker 	 * right should be provided by default.
1432*8b26181fSAndroid Build Coastguard Worker 	 *
1433*8b26181fSAndroid Build Coastguard Worker 	 * XXX - hopefully, this returns errors such as ERROR_LOGON_FAILURE,
1434*8b26181fSAndroid Build Coastguard Worker 	 * which merely indicates that the user name or password is
1435*8b26181fSAndroid Build Coastguard Worker 	 * incorrect, not whether it's the user name or the password
1436*8b26181fSAndroid Build Coastguard Worker 	 * that's incorrect, so a client that's trying to brute-force
1437*8b26181fSAndroid Build Coastguard Worker 	 * accounts doesn't know whether it's the user name or the
1438*8b26181fSAndroid Build Coastguard Worker 	 * password that's incorrect, so it doesn't know whether to
1439*8b26181fSAndroid Build Coastguard Worker 	 * stop trying to log in with a given user name and move on
1440*8b26181fSAndroid Build Coastguard Worker 	 * to another user name.
1441*8b26181fSAndroid Build Coastguard Worker 	 */
1442*8b26181fSAndroid Build Coastguard Worker 	DWORD error;
1443*8b26181fSAndroid Build Coastguard Worker 	HANDLE Token;
1444*8b26181fSAndroid Build Coastguard Worker 	char errmsgbuf[PCAP_ERRBUF_SIZE];	// buffer for errors to log
1445*8b26181fSAndroid Build Coastguard Worker 
1446*8b26181fSAndroid Build Coastguard Worker 	if (LogonUser(username, ".", password, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, &Token) == 0)
1447*8b26181fSAndroid Build Coastguard Worker 	{
1448*8b26181fSAndroid Build Coastguard Worker 		snprintf(errbuf, PCAP_ERRBUF_SIZE, "Authentication failed");
1449*8b26181fSAndroid Build Coastguard Worker 		error = GetLastError();
1450*8b26181fSAndroid Build Coastguard Worker 		if (error != ERROR_LOGON_FAILURE)
1451*8b26181fSAndroid Build Coastguard Worker 		{
1452*8b26181fSAndroid Build Coastguard Worker 			// Some error other than an authentication error;
1453*8b26181fSAndroid Build Coastguard Worker 			// log it.
1454*8b26181fSAndroid Build Coastguard Worker 			pcap_fmt_errmsg_for_win32_err(errmsgbuf,
1455*8b26181fSAndroid Build Coastguard Worker 			    PCAP_ERRBUF_SIZE, error, "LogonUser() failed");
1456*8b26181fSAndroid Build Coastguard Worker 			rpcapd_log(LOGPRIO_ERROR, "%s", errmsgbuf);
1457*8b26181fSAndroid Build Coastguard Worker 		}
1458*8b26181fSAndroid Build Coastguard Worker 		return -1;
1459*8b26181fSAndroid Build Coastguard Worker 	}
1460*8b26181fSAndroid Build Coastguard Worker 
1461*8b26181fSAndroid Build Coastguard Worker 	// This call should change the current thread to the selected user.
1462*8b26181fSAndroid Build Coastguard Worker 	// I didn't test it.
1463*8b26181fSAndroid Build Coastguard Worker 	if (ImpersonateLoggedOnUser(Token) == 0)
1464*8b26181fSAndroid Build Coastguard Worker 	{
1465*8b26181fSAndroid Build Coastguard Worker 		snprintf(errbuf, PCAP_ERRBUF_SIZE, "Authentication failed");
1466*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_win32_err(errmsgbuf, PCAP_ERRBUF_SIZE,
1467*8b26181fSAndroid Build Coastguard Worker 		    GetLastError(), "ImpersonateLoggedOnUser() failed");
1468*8b26181fSAndroid Build Coastguard Worker 		rpcapd_log(LOGPRIO_ERROR, "%s", errmsgbuf);
1469*8b26181fSAndroid Build Coastguard Worker 		CloseHandle(Token);
1470*8b26181fSAndroid Build Coastguard Worker 		return -1;
1471*8b26181fSAndroid Build Coastguard Worker 	}
1472*8b26181fSAndroid Build Coastguard Worker 
1473*8b26181fSAndroid Build Coastguard Worker 	CloseHandle(Token);
1474*8b26181fSAndroid Build Coastguard Worker 	return 0;
1475*8b26181fSAndroid Build Coastguard Worker 
1476*8b26181fSAndroid Build Coastguard Worker #else
1477*8b26181fSAndroid Build Coastguard Worker 	/*
1478*8b26181fSAndroid Build Coastguard Worker 	 * See
1479*8b26181fSAndroid Build Coastguard Worker 	 *
1480*8b26181fSAndroid Build Coastguard Worker 	 *	https://www.unixpapa.com/incnote/passwd.html
1481*8b26181fSAndroid Build Coastguard Worker 	 *
1482*8b26181fSAndroid Build Coastguard Worker 	 * We use the Solaris/Linux shadow password authentication if
1483*8b26181fSAndroid Build Coastguard Worker 	 * we have getspnam(), otherwise we just do traditional
1484*8b26181fSAndroid Build Coastguard Worker 	 * authentication, which, on some platforms, might work, even
1485*8b26181fSAndroid Build Coastguard Worker 	 * with shadow passwords, if we're running as root.  Traditional
1486*8b26181fSAndroid Build Coastguard Worker 	 * authenticaion won't work if we're not running as root, as
1487*8b26181fSAndroid Build Coastguard Worker 	 * I think these days all UN*Xes either won't return the password
1488*8b26181fSAndroid Build Coastguard Worker 	 * at all with getpwnam() or will only do so if you're root.
1489*8b26181fSAndroid Build Coastguard Worker 	 *
1490*8b26181fSAndroid Build Coastguard Worker 	 * XXX - perhaps what we *should* be using is PAM, if we have
1491*8b26181fSAndroid Build Coastguard Worker 	 * it.  That might hide all the details of username/password
1492*8b26181fSAndroid Build Coastguard Worker 	 * authentication, whether it's done with a visible-to-root-
1493*8b26181fSAndroid Build Coastguard Worker 	 * only password database or some other authentication mechanism,
1494*8b26181fSAndroid Build Coastguard Worker 	 * behind its API.
1495*8b26181fSAndroid Build Coastguard Worker 	 */
1496*8b26181fSAndroid Build Coastguard Worker 	int error;
1497*8b26181fSAndroid Build Coastguard Worker 	struct passwd *user;
1498*8b26181fSAndroid Build Coastguard Worker 	char *user_password;
1499*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_GETSPNAM
1500*8b26181fSAndroid Build Coastguard Worker 	struct spwd *usersp;
1501*8b26181fSAndroid Build Coastguard Worker #endif
1502*8b26181fSAndroid Build Coastguard Worker 	char *crypt_password;
1503*8b26181fSAndroid Build Coastguard Worker 
1504*8b26181fSAndroid Build Coastguard Worker 	// This call is needed to get the uid
1505*8b26181fSAndroid Build Coastguard Worker 	if ((user = getpwnam(username)) == NULL)
1506*8b26181fSAndroid Build Coastguard Worker 	{
1507*8b26181fSAndroid Build Coastguard Worker 		snprintf(errbuf, PCAP_ERRBUF_SIZE, "Authentication failed");
1508*8b26181fSAndroid Build Coastguard Worker 		return -1;
1509*8b26181fSAndroid Build Coastguard Worker 	}
1510*8b26181fSAndroid Build Coastguard Worker 
1511*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_GETSPNAM
1512*8b26181fSAndroid Build Coastguard Worker 	// This call is needed to get the password; otherwise 'x' is returned
1513*8b26181fSAndroid Build Coastguard Worker 	if ((usersp = getspnam(username)) == NULL)
1514*8b26181fSAndroid Build Coastguard Worker 	{
1515*8b26181fSAndroid Build Coastguard Worker 		snprintf(errbuf, PCAP_ERRBUF_SIZE, "Authentication failed");
1516*8b26181fSAndroid Build Coastguard Worker 		return -1;
1517*8b26181fSAndroid Build Coastguard Worker 	}
1518*8b26181fSAndroid Build Coastguard Worker 	user_password = usersp->sp_pwdp;
1519*8b26181fSAndroid Build Coastguard Worker #else
1520*8b26181fSAndroid Build Coastguard Worker 	/*
1521*8b26181fSAndroid Build Coastguard Worker 	 * XXX - what about other platforms?
1522*8b26181fSAndroid Build Coastguard Worker 	 * The unixpapa.com page claims this Just Works on *BSD if you're
1523*8b26181fSAndroid Build Coastguard Worker 	 * running as root - it's from 2000, so it doesn't indicate whether
1524*8b26181fSAndroid Build Coastguard Worker 	 * macOS (which didn't come out until 2001, under the name Mac OS
1525*8b26181fSAndroid Build Coastguard Worker 	 * X) behaves like the *BSDs or not, and might also work on AIX.
1526*8b26181fSAndroid Build Coastguard Worker 	 * HP-UX does something else.
1527*8b26181fSAndroid Build Coastguard Worker 	 *
1528*8b26181fSAndroid Build Coastguard Worker 	 * Again, hopefully PAM hides all that.
1529*8b26181fSAndroid Build Coastguard Worker 	 */
1530*8b26181fSAndroid Build Coastguard Worker 	user_password = user->pw_passwd;
1531*8b26181fSAndroid Build Coastguard Worker #endif
1532*8b26181fSAndroid Build Coastguard Worker 
1533*8b26181fSAndroid Build Coastguard Worker 	//
1534*8b26181fSAndroid Build Coastguard Worker 	// The Single UNIX Specification says that if crypt() fails it
1535*8b26181fSAndroid Build Coastguard Worker 	// sets errno, but some implementatons that haven't been run
1536*8b26181fSAndroid Build Coastguard Worker 	// through the SUS test suite might not do so.
1537*8b26181fSAndroid Build Coastguard Worker 	//
1538*8b26181fSAndroid Build Coastguard Worker 	errno = 0;
1539*8b26181fSAndroid Build Coastguard Worker 	crypt_password = crypt(password, user_password);
1540*8b26181fSAndroid Build Coastguard Worker 	if (crypt_password == NULL)
1541*8b26181fSAndroid Build Coastguard Worker 	{
1542*8b26181fSAndroid Build Coastguard Worker 		error = errno;
1543*8b26181fSAndroid Build Coastguard Worker 		snprintf(errbuf, PCAP_ERRBUF_SIZE, "Authentication failed");
1544*8b26181fSAndroid Build Coastguard Worker 		if (error == 0)
1545*8b26181fSAndroid Build Coastguard Worker 		{
1546*8b26181fSAndroid Build Coastguard Worker 			// It didn't set errno.
1547*8b26181fSAndroid Build Coastguard Worker 			rpcapd_log(LOGPRIO_ERROR, "crypt() failed");
1548*8b26181fSAndroid Build Coastguard Worker 		}
1549*8b26181fSAndroid Build Coastguard Worker 		else
1550*8b26181fSAndroid Build Coastguard Worker 		{
1551*8b26181fSAndroid Build Coastguard Worker 			rpcapd_log(LOGPRIO_ERROR, "crypt() failed: %s",
1552*8b26181fSAndroid Build Coastguard Worker 			    strerror(error));
1553*8b26181fSAndroid Build Coastguard Worker 		}
1554*8b26181fSAndroid Build Coastguard Worker 		return -1;
1555*8b26181fSAndroid Build Coastguard Worker 	}
1556*8b26181fSAndroid Build Coastguard Worker 	if (strcmp(user_password, crypt_password) != 0)
1557*8b26181fSAndroid Build Coastguard Worker 	{
1558*8b26181fSAndroid Build Coastguard Worker 		snprintf(errbuf, PCAP_ERRBUF_SIZE, "Authentication failed");
1559*8b26181fSAndroid Build Coastguard Worker 		return -1;
1560*8b26181fSAndroid Build Coastguard Worker 	}
1561*8b26181fSAndroid Build Coastguard Worker 
1562*8b26181fSAndroid Build Coastguard Worker 	if (setuid(user->pw_uid))
1563*8b26181fSAndroid Build Coastguard Worker 	{
1564*8b26181fSAndroid Build Coastguard Worker 		error = errno;
1565*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1566*8b26181fSAndroid Build Coastguard Worker 		    error, "setuid");
1567*8b26181fSAndroid Build Coastguard Worker 		rpcapd_log(LOGPRIO_ERROR, "setuid() failed: %s",
1568*8b26181fSAndroid Build Coastguard Worker 		    strerror(error));
1569*8b26181fSAndroid Build Coastguard Worker 		return -1;
1570*8b26181fSAndroid Build Coastguard Worker 	}
1571*8b26181fSAndroid Build Coastguard Worker 
1572*8b26181fSAndroid Build Coastguard Worker /*	if (setgid(user->pw_gid))
1573*8b26181fSAndroid Build Coastguard Worker 	{
1574*8b26181fSAndroid Build Coastguard Worker 		error = errno;
1575*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1576*8b26181fSAndroid Build Coastguard Worker 		    errno, "setgid");
1577*8b26181fSAndroid Build Coastguard Worker 		rpcapd_log(LOGPRIO_ERROR, "setgid() failed: %s",
1578*8b26181fSAndroid Build Coastguard Worker 		    strerror(error));
1579*8b26181fSAndroid Build Coastguard Worker 		return -1;
1580*8b26181fSAndroid Build Coastguard Worker 	}
1581*8b26181fSAndroid Build Coastguard Worker */
1582*8b26181fSAndroid Build Coastguard Worker 	return 0;
1583*8b26181fSAndroid Build Coastguard Worker 
1584*8b26181fSAndroid Build Coastguard Worker #endif
1585*8b26181fSAndroid Build Coastguard Worker 
1586*8b26181fSAndroid Build Coastguard Worker }
1587*8b26181fSAndroid Build Coastguard Worker 
1588*8b26181fSAndroid Build Coastguard Worker /*
1589*8b26181fSAndroid Build Coastguard Worker  * Make sure that the reply length won't overflow 32 bits if we add the
1590*8b26181fSAndroid Build Coastguard Worker  * specified amount to it.  If it won't, add that amount to it.
1591*8b26181fSAndroid Build Coastguard Worker  *
1592*8b26181fSAndroid Build Coastguard Worker  * We check whether replylen + itemlen > UINT32_MAX, but subtract itemlen
1593*8b26181fSAndroid Build Coastguard Worker  * from both sides, to prevent overflow.
1594*8b26181fSAndroid Build Coastguard Worker  */
1595*8b26181fSAndroid Build Coastguard Worker #define CHECK_AND_INCREASE_REPLY_LEN(itemlen) \
1596*8b26181fSAndroid Build Coastguard Worker 	if (replylen > UINT32_MAX - (itemlen)) { \
1597*8b26181fSAndroid Build Coastguard Worker 		pcap_strlcpy(errmsgbuf, "Reply length doesn't fit in 32 bits", \
1598*8b26181fSAndroid Build Coastguard Worker 		    sizeof (errmsgbuf)); \
1599*8b26181fSAndroid Build Coastguard Worker 		goto error; \
1600*8b26181fSAndroid Build Coastguard Worker 	} \
1601*8b26181fSAndroid Build Coastguard Worker 	replylen += (uint32)(itemlen)
1602*8b26181fSAndroid Build Coastguard Worker 
1603*8b26181fSAndroid Build Coastguard Worker static int
daemon_msg_findallif_req(uint8 ver,struct daemon_slpars * pars,uint32 plen)1604*8b26181fSAndroid Build Coastguard Worker daemon_msg_findallif_req(uint8 ver, struct daemon_slpars *pars, uint32 plen)
1605*8b26181fSAndroid Build Coastguard Worker {
1606*8b26181fSAndroid Build Coastguard Worker 	char errbuf[PCAP_ERRBUF_SIZE];		// buffer for network errors
1607*8b26181fSAndroid Build Coastguard Worker 	char errmsgbuf[PCAP_ERRBUF_SIZE];	// buffer for errors to send to the client
1608*8b26181fSAndroid Build Coastguard Worker 	char sendbuf[RPCAP_NETBUF_SIZE];	// temporary buffer in which data to be sent is buffered
1609*8b26181fSAndroid Build Coastguard Worker 	int sendbufidx = 0;			// index which keeps the number of bytes currently buffered
1610*8b26181fSAndroid Build Coastguard Worker 	pcap_if_t *alldevs = NULL;		// pointer to the header of the interface chain
1611*8b26181fSAndroid Build Coastguard Worker 	pcap_if_t *d;				// temp pointer needed to scan the interface chain
1612*8b26181fSAndroid Build Coastguard Worker 	struct pcap_addr *address;		// pcap structure that keeps a network address of an interface
1613*8b26181fSAndroid Build Coastguard Worker 	uint32 replylen;			// length of reply payload
1614*8b26181fSAndroid Build Coastguard Worker 	uint16 nif = 0;				// counts the number of interface listed
1615*8b26181fSAndroid Build Coastguard Worker 
1616*8b26181fSAndroid Build Coastguard Worker 	// Discard the rest of the message; there shouldn't be any payload.
1617*8b26181fSAndroid Build Coastguard Worker 	if (rpcapd_discard(pars->sockctrl, pars->ssl, plen) == -1)
1618*8b26181fSAndroid Build Coastguard Worker 	{
1619*8b26181fSAndroid Build Coastguard Worker 		// Network error.
1620*8b26181fSAndroid Build Coastguard Worker 		return -1;
1621*8b26181fSAndroid Build Coastguard Worker 	}
1622*8b26181fSAndroid Build Coastguard Worker 
1623*8b26181fSAndroid Build Coastguard Worker 	// Retrieve the device list
1624*8b26181fSAndroid Build Coastguard Worker 	if (pcap_findalldevs(&alldevs, errmsgbuf) == -1)
1625*8b26181fSAndroid Build Coastguard Worker 		goto error;
1626*8b26181fSAndroid Build Coastguard Worker 
1627*8b26181fSAndroid Build Coastguard Worker 	if (alldevs == NULL)
1628*8b26181fSAndroid Build Coastguard Worker 	{
1629*8b26181fSAndroid Build Coastguard Worker 		if (rpcap_senderror(pars->sockctrl, pars->ssl, ver,
1630*8b26181fSAndroid Build Coastguard Worker 			PCAP_ERR_NOREMOTEIF,
1631*8b26181fSAndroid Build Coastguard Worker 			"No interfaces found! Make sure libpcap/WinPcap is properly installed"
1632*8b26181fSAndroid Build Coastguard Worker 			" and you have the right to access to the remote device.",
1633*8b26181fSAndroid Build Coastguard Worker 			errbuf) == -1)
1634*8b26181fSAndroid Build Coastguard Worker 		{
1635*8b26181fSAndroid Build Coastguard Worker 			rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
1636*8b26181fSAndroid Build Coastguard Worker 			return -1;
1637*8b26181fSAndroid Build Coastguard Worker 		}
1638*8b26181fSAndroid Build Coastguard Worker 		return 0;
1639*8b26181fSAndroid Build Coastguard Worker 	}
1640*8b26181fSAndroid Build Coastguard Worker 
1641*8b26181fSAndroid Build Coastguard Worker 	// This checks the number of interfaces and computes the total
1642*8b26181fSAndroid Build Coastguard Worker 	// length of the payload.
1643*8b26181fSAndroid Build Coastguard Worker 	replylen = 0;
1644*8b26181fSAndroid Build Coastguard Worker 	for (d = alldevs; d != NULL; d = d->next)
1645*8b26181fSAndroid Build Coastguard Worker 	{
1646*8b26181fSAndroid Build Coastguard Worker 		nif++;
1647*8b26181fSAndroid Build Coastguard Worker 
1648*8b26181fSAndroid Build Coastguard Worker 		if (d->description) {
1649*8b26181fSAndroid Build Coastguard Worker 			size_t stringlen = strlen(d->description);
1650*8b26181fSAndroid Build Coastguard Worker 			if (stringlen > UINT16_MAX) {
1651*8b26181fSAndroid Build Coastguard Worker 				pcap_strlcpy(errmsgbuf,
1652*8b26181fSAndroid Build Coastguard Worker 				    "Description length doesn't fit in 16 bits",
1653*8b26181fSAndroid Build Coastguard Worker 				    sizeof (errmsgbuf));
1654*8b26181fSAndroid Build Coastguard Worker 				goto error;
1655*8b26181fSAndroid Build Coastguard Worker 			}
1656*8b26181fSAndroid Build Coastguard Worker 			CHECK_AND_INCREASE_REPLY_LEN(stringlen);
1657*8b26181fSAndroid Build Coastguard Worker 		}
1658*8b26181fSAndroid Build Coastguard Worker 		if (d->name) {
1659*8b26181fSAndroid Build Coastguard Worker 			size_t stringlen = strlen(d->name);
1660*8b26181fSAndroid Build Coastguard Worker 			if (stringlen > UINT16_MAX) {
1661*8b26181fSAndroid Build Coastguard Worker 				pcap_strlcpy(errmsgbuf,
1662*8b26181fSAndroid Build Coastguard Worker 				    "Name length doesn't fit in 16 bits",
1663*8b26181fSAndroid Build Coastguard Worker 				    sizeof (errmsgbuf));
1664*8b26181fSAndroid Build Coastguard Worker 				goto error;
1665*8b26181fSAndroid Build Coastguard Worker 			}
1666*8b26181fSAndroid Build Coastguard Worker 			CHECK_AND_INCREASE_REPLY_LEN(stringlen);
1667*8b26181fSAndroid Build Coastguard Worker 		}
1668*8b26181fSAndroid Build Coastguard Worker 
1669*8b26181fSAndroid Build Coastguard Worker 		CHECK_AND_INCREASE_REPLY_LEN(sizeof(struct rpcap_findalldevs_if));
1670*8b26181fSAndroid Build Coastguard Worker 
1671*8b26181fSAndroid Build Coastguard Worker 		uint16_t naddrs = 0;
1672*8b26181fSAndroid Build Coastguard Worker 		for (address = d->addresses; address != NULL; address = address->next)
1673*8b26181fSAndroid Build Coastguard Worker 		{
1674*8b26181fSAndroid Build Coastguard Worker 			/*
1675*8b26181fSAndroid Build Coastguard Worker 			 * Send only IPv4 and IPv6 addresses over the wire.
1676*8b26181fSAndroid Build Coastguard Worker 			 */
1677*8b26181fSAndroid Build Coastguard Worker 			switch (address->addr->sa_family)
1678*8b26181fSAndroid Build Coastguard Worker 			{
1679*8b26181fSAndroid Build Coastguard Worker 			case AF_INET:
1680*8b26181fSAndroid Build Coastguard Worker #ifdef AF_INET6
1681*8b26181fSAndroid Build Coastguard Worker 			case AF_INET6:
1682*8b26181fSAndroid Build Coastguard Worker #endif
1683*8b26181fSAndroid Build Coastguard Worker 				CHECK_AND_INCREASE_REPLY_LEN(sizeof(struct rpcap_sockaddr) * 4);
1684*8b26181fSAndroid Build Coastguard Worker 				if (naddrs == UINT16_MAX) {
1685*8b26181fSAndroid Build Coastguard Worker 					pcap_strlcpy(errmsgbuf,
1686*8b26181fSAndroid Build Coastguard Worker 					    "Number of interfaces doesn't fit in 16 bits",
1687*8b26181fSAndroid Build Coastguard Worker 					    sizeof (errmsgbuf));
1688*8b26181fSAndroid Build Coastguard Worker 					goto error;
1689*8b26181fSAndroid Build Coastguard Worker 				}
1690*8b26181fSAndroid Build Coastguard Worker 				naddrs++;
1691*8b26181fSAndroid Build Coastguard Worker 				break;
1692*8b26181fSAndroid Build Coastguard Worker 
1693*8b26181fSAndroid Build Coastguard Worker 			default:
1694*8b26181fSAndroid Build Coastguard Worker 				break;
1695*8b26181fSAndroid Build Coastguard Worker 			}
1696*8b26181fSAndroid Build Coastguard Worker 		}
1697*8b26181fSAndroid Build Coastguard Worker 	}
1698*8b26181fSAndroid Build Coastguard Worker 
1699*8b26181fSAndroid Build Coastguard Worker 	// RPCAP findalldevs reply
1700*8b26181fSAndroid Build Coastguard Worker 	if (sock_bufferize(NULL, sizeof(struct rpcap_header), NULL,
1701*8b26181fSAndroid Build Coastguard Worker 	    &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errmsgbuf,
1702*8b26181fSAndroid Build Coastguard Worker 	    PCAP_ERRBUF_SIZE) == -1)
1703*8b26181fSAndroid Build Coastguard Worker 		goto error;
1704*8b26181fSAndroid Build Coastguard Worker 
1705*8b26181fSAndroid Build Coastguard Worker 	rpcap_createhdr((struct rpcap_header *) sendbuf, ver,
1706*8b26181fSAndroid Build Coastguard Worker 	    RPCAP_MSG_FINDALLIF_REPLY, nif, replylen);
1707*8b26181fSAndroid Build Coastguard Worker 
1708*8b26181fSAndroid Build Coastguard Worker 	// send the interface list
1709*8b26181fSAndroid Build Coastguard Worker 	for (d = alldevs; d != NULL; d = d->next)
1710*8b26181fSAndroid Build Coastguard Worker 	{
1711*8b26181fSAndroid Build Coastguard Worker 		uint16 lname, ldescr;
1712*8b26181fSAndroid Build Coastguard Worker 
1713*8b26181fSAndroid Build Coastguard Worker 		// Note: the findalldevs_if entries are *not* neatly
1714*8b26181fSAndroid Build Coastguard Worker 		// aligned on 4-byte boundaries, because they're
1715*8b26181fSAndroid Build Coastguard Worker 		// preceded by strings that aren't padded to 4-byte
1716*8b26181fSAndroid Build Coastguard Worker 		// boundaries, so we cannot just cast output buffer
1717*8b26181fSAndroid Build Coastguard Worker 		// boundaries to struct rpcap_findalldevs_if pointers
1718*8b26181fSAndroid Build Coastguard Worker 		// and store into them - we must fill in a structure and
1719*8b26181fSAndroid Build Coastguard Worker 		// then copy the structure to the buffer, as not all
1720*8b26181fSAndroid Build Coastguard Worker 		// systems support unaligned access (some, such as
1721*8b26181fSAndroid Build Coastguard Worker 		// SPARC, crash; others, such as Arm, may just ignore
1722*8b26181fSAndroid Build Coastguard Worker 		// the lower-order bits).
1723*8b26181fSAndroid Build Coastguard Worker 		struct rpcap_findalldevs_if findalldevs_if;
1724*8b26181fSAndroid Build Coastguard Worker 
1725*8b26181fSAndroid Build Coastguard Worker 		/*
1726*8b26181fSAndroid Build Coastguard Worker 		 * We've already established that the string lengths
1727*8b26181fSAndroid Build Coastguard Worker 		 * fit in 16 bits.
1728*8b26181fSAndroid Build Coastguard Worker 		 */
1729*8b26181fSAndroid Build Coastguard Worker 		if (d->description)
1730*8b26181fSAndroid Build Coastguard Worker 			ldescr = (uint16) strlen(d->description);
1731*8b26181fSAndroid Build Coastguard Worker 		else
1732*8b26181fSAndroid Build Coastguard Worker 			ldescr = 0;
1733*8b26181fSAndroid Build Coastguard Worker 		if (d->name)
1734*8b26181fSAndroid Build Coastguard Worker 			lname = (uint16) strlen(d->name);
1735*8b26181fSAndroid Build Coastguard Worker 		else
1736*8b26181fSAndroid Build Coastguard Worker 			lname = 0;
1737*8b26181fSAndroid Build Coastguard Worker 
1738*8b26181fSAndroid Build Coastguard Worker 		findalldevs_if.desclen = htons(ldescr);
1739*8b26181fSAndroid Build Coastguard Worker 		findalldevs_if.namelen = htons(lname);
1740*8b26181fSAndroid Build Coastguard Worker 		findalldevs_if.flags = htonl(d->flags);
1741*8b26181fSAndroid Build Coastguard Worker 
1742*8b26181fSAndroid Build Coastguard Worker 		uint16_t naddrs = 0;
1743*8b26181fSAndroid Build Coastguard Worker 		for (address = d->addresses; address != NULL; address = address->next)
1744*8b26181fSAndroid Build Coastguard Worker 		{
1745*8b26181fSAndroid Build Coastguard Worker 			/*
1746*8b26181fSAndroid Build Coastguard Worker 			 * Send only IPv4 and IPv6 addresses over the wire.
1747*8b26181fSAndroid Build Coastguard Worker 			 */
1748*8b26181fSAndroid Build Coastguard Worker 			switch (address->addr->sa_family)
1749*8b26181fSAndroid Build Coastguard Worker 			{
1750*8b26181fSAndroid Build Coastguard Worker 			case AF_INET:
1751*8b26181fSAndroid Build Coastguard Worker #ifdef AF_INET6
1752*8b26181fSAndroid Build Coastguard Worker 			case AF_INET6:
1753*8b26181fSAndroid Build Coastguard Worker #endif
1754*8b26181fSAndroid Build Coastguard Worker 				naddrs++;
1755*8b26181fSAndroid Build Coastguard Worker 				break;
1756*8b26181fSAndroid Build Coastguard Worker 
1757*8b26181fSAndroid Build Coastguard Worker 			default:
1758*8b26181fSAndroid Build Coastguard Worker 				break;
1759*8b26181fSAndroid Build Coastguard Worker 			}
1760*8b26181fSAndroid Build Coastguard Worker 		}
1761*8b26181fSAndroid Build Coastguard Worker 		findalldevs_if.naddr = htons(naddrs);
1762*8b26181fSAndroid Build Coastguard Worker 		findalldevs_if.dummy = 0;
1763*8b26181fSAndroid Build Coastguard Worker 
1764*8b26181fSAndroid Build Coastguard Worker 		if (sock_bufferize(&findalldevs_if, sizeof(struct rpcap_findalldevs_if), sendbuf,
1765*8b26181fSAndroid Build Coastguard Worker 		    &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_BUFFERIZE, errmsgbuf,
1766*8b26181fSAndroid Build Coastguard Worker 		    PCAP_ERRBUF_SIZE) == -1)
1767*8b26181fSAndroid Build Coastguard Worker 			goto error;
1768*8b26181fSAndroid Build Coastguard Worker 
1769*8b26181fSAndroid Build Coastguard Worker 		if (sock_bufferize(d->name, lname, sendbuf, &sendbufidx,
1770*8b26181fSAndroid Build Coastguard Worker 		    RPCAP_NETBUF_SIZE, SOCKBUF_BUFFERIZE, errmsgbuf,
1771*8b26181fSAndroid Build Coastguard Worker 		    PCAP_ERRBUF_SIZE) == -1)
1772*8b26181fSAndroid Build Coastguard Worker 			goto error;
1773*8b26181fSAndroid Build Coastguard Worker 
1774*8b26181fSAndroid Build Coastguard Worker 		if (sock_bufferize(d->description, ldescr, sendbuf, &sendbufidx,
1775*8b26181fSAndroid Build Coastguard Worker 		    RPCAP_NETBUF_SIZE, SOCKBUF_BUFFERIZE, errmsgbuf,
1776*8b26181fSAndroid Build Coastguard Worker 		    PCAP_ERRBUF_SIZE) == -1)
1777*8b26181fSAndroid Build Coastguard Worker 			goto error;
1778*8b26181fSAndroid Build Coastguard Worker 
1779*8b26181fSAndroid Build Coastguard Worker 		// send all addresses
1780*8b26181fSAndroid Build Coastguard Worker 		for (address = d->addresses; address != NULL; address = address->next)
1781*8b26181fSAndroid Build Coastguard Worker 		{
1782*8b26181fSAndroid Build Coastguard Worker 			struct rpcap_sockaddr *sockaddr;
1783*8b26181fSAndroid Build Coastguard Worker 
1784*8b26181fSAndroid Build Coastguard Worker 			/*
1785*8b26181fSAndroid Build Coastguard Worker 			 * Send only IPv4 and IPv6 addresses over the wire.
1786*8b26181fSAndroid Build Coastguard Worker 			 */
1787*8b26181fSAndroid Build Coastguard Worker 			switch (address->addr->sa_family)
1788*8b26181fSAndroid Build Coastguard Worker 			{
1789*8b26181fSAndroid Build Coastguard Worker 			case AF_INET:
1790*8b26181fSAndroid Build Coastguard Worker #ifdef AF_INET6
1791*8b26181fSAndroid Build Coastguard Worker 			case AF_INET6:
1792*8b26181fSAndroid Build Coastguard Worker #endif
1793*8b26181fSAndroid Build Coastguard Worker 				sockaddr = (struct rpcap_sockaddr *) &sendbuf[sendbufidx];
1794*8b26181fSAndroid Build Coastguard Worker 				if (sock_bufferize(NULL, sizeof(struct rpcap_sockaddr), NULL,
1795*8b26181fSAndroid Build Coastguard Worker 				    &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
1796*8b26181fSAndroid Build Coastguard Worker 					goto error;
1797*8b26181fSAndroid Build Coastguard Worker 				daemon_seraddr((struct sockaddr_storage *) address->addr, sockaddr);
1798*8b26181fSAndroid Build Coastguard Worker 
1799*8b26181fSAndroid Build Coastguard Worker 				sockaddr = (struct rpcap_sockaddr *) &sendbuf[sendbufidx];
1800*8b26181fSAndroid Build Coastguard Worker 				if (sock_bufferize(NULL, sizeof(struct rpcap_sockaddr), NULL,
1801*8b26181fSAndroid Build Coastguard Worker 				    &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
1802*8b26181fSAndroid Build Coastguard Worker 					goto error;
1803*8b26181fSAndroid Build Coastguard Worker 				daemon_seraddr((struct sockaddr_storage *) address->netmask, sockaddr);
1804*8b26181fSAndroid Build Coastguard Worker 
1805*8b26181fSAndroid Build Coastguard Worker 				sockaddr = (struct rpcap_sockaddr *) &sendbuf[sendbufidx];
1806*8b26181fSAndroid Build Coastguard Worker 				if (sock_bufferize(NULL, sizeof(struct rpcap_sockaddr), NULL,
1807*8b26181fSAndroid Build Coastguard Worker 				    &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
1808*8b26181fSAndroid Build Coastguard Worker 					goto error;
1809*8b26181fSAndroid Build Coastguard Worker 				daemon_seraddr((struct sockaddr_storage *) address->broadaddr, sockaddr);
1810*8b26181fSAndroid Build Coastguard Worker 
1811*8b26181fSAndroid Build Coastguard Worker 				sockaddr = (struct rpcap_sockaddr *) &sendbuf[sendbufidx];
1812*8b26181fSAndroid Build Coastguard Worker 				if (sock_bufferize(NULL, sizeof(struct rpcap_sockaddr), NULL,
1813*8b26181fSAndroid Build Coastguard Worker 				    &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
1814*8b26181fSAndroid Build Coastguard Worker 					goto error;
1815*8b26181fSAndroid Build Coastguard Worker 				daemon_seraddr((struct sockaddr_storage *) address->dstaddr, sockaddr);
1816*8b26181fSAndroid Build Coastguard Worker 				break;
1817*8b26181fSAndroid Build Coastguard Worker 
1818*8b26181fSAndroid Build Coastguard Worker 			default:
1819*8b26181fSAndroid Build Coastguard Worker 				break;
1820*8b26181fSAndroid Build Coastguard Worker 			}
1821*8b26181fSAndroid Build Coastguard Worker 		}
1822*8b26181fSAndroid Build Coastguard Worker 	}
1823*8b26181fSAndroid Build Coastguard Worker 
1824*8b26181fSAndroid Build Coastguard Worker 	// We no longer need the device list. Free it.
1825*8b26181fSAndroid Build Coastguard Worker 	pcap_freealldevs(alldevs);
1826*8b26181fSAndroid Build Coastguard Worker 
1827*8b26181fSAndroid Build Coastguard Worker 	// Send a final command that says "now send it!"
1828*8b26181fSAndroid Build Coastguard Worker 	if (sock_send(pars->sockctrl, pars->ssl, sendbuf, sendbufidx, errbuf, PCAP_ERRBUF_SIZE) == -1)
1829*8b26181fSAndroid Build Coastguard Worker 	{
1830*8b26181fSAndroid Build Coastguard Worker 		rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
1831*8b26181fSAndroid Build Coastguard Worker 		return -1;
1832*8b26181fSAndroid Build Coastguard Worker 	}
1833*8b26181fSAndroid Build Coastguard Worker 
1834*8b26181fSAndroid Build Coastguard Worker 	return 0;
1835*8b26181fSAndroid Build Coastguard Worker 
1836*8b26181fSAndroid Build Coastguard Worker error:
1837*8b26181fSAndroid Build Coastguard Worker 	if (alldevs)
1838*8b26181fSAndroid Build Coastguard Worker 		pcap_freealldevs(alldevs);
1839*8b26181fSAndroid Build Coastguard Worker 
1840*8b26181fSAndroid Build Coastguard Worker 	if (rpcap_senderror(pars->sockctrl, pars->ssl, ver,
1841*8b26181fSAndroid Build Coastguard Worker 	    PCAP_ERR_FINDALLIF, errmsgbuf, errbuf) == -1)
1842*8b26181fSAndroid Build Coastguard Worker 	{
1843*8b26181fSAndroid Build Coastguard Worker 		rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
1844*8b26181fSAndroid Build Coastguard Worker 		return -1;
1845*8b26181fSAndroid Build Coastguard Worker 	}
1846*8b26181fSAndroid Build Coastguard Worker 	return 0;
1847*8b26181fSAndroid Build Coastguard Worker }
1848*8b26181fSAndroid Build Coastguard Worker 
1849*8b26181fSAndroid Build Coastguard Worker /*
1850*8b26181fSAndroid Build Coastguard Worker 	\param plen: the length of the current message (needed in order to be able
1851*8b26181fSAndroid Build Coastguard Worker 	to discard excess data in the message, if present)
1852*8b26181fSAndroid Build Coastguard Worker */
1853*8b26181fSAndroid Build Coastguard Worker static int
daemon_msg_open_req(uint8 ver,struct daemon_slpars * pars,uint32 plen,char * source,size_t sourcelen)1854*8b26181fSAndroid Build Coastguard Worker daemon_msg_open_req(uint8 ver, struct daemon_slpars *pars, uint32 plen,
1855*8b26181fSAndroid Build Coastguard Worker     char *source, size_t sourcelen)
1856*8b26181fSAndroid Build Coastguard Worker {
1857*8b26181fSAndroid Build Coastguard Worker 	char errbuf[PCAP_ERRBUF_SIZE];		// buffer for network errors
1858*8b26181fSAndroid Build Coastguard Worker 	char errmsgbuf[PCAP_ERRBUF_SIZE];	// buffer for errors to send to the client
1859*8b26181fSAndroid Build Coastguard Worker 	pcap_t *fp;				// pcap_t main variable
1860*8b26181fSAndroid Build Coastguard Worker 	int nread;
1861*8b26181fSAndroid Build Coastguard Worker 	char sendbuf[RPCAP_NETBUF_SIZE];	// temporary buffer in which data to be sent is buffered
1862*8b26181fSAndroid Build Coastguard Worker 	int sendbufidx = 0;			// index which keeps the number of bytes currently buffered
1863*8b26181fSAndroid Build Coastguard Worker 	struct rpcap_openreply *openreply;	// open reply message
1864*8b26181fSAndroid Build Coastguard Worker 
1865*8b26181fSAndroid Build Coastguard Worker 	if (plen > sourcelen - 1)
1866*8b26181fSAndroid Build Coastguard Worker 	{
1867*8b26181fSAndroid Build Coastguard Worker 		snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Source string too long");
1868*8b26181fSAndroid Build Coastguard Worker 		goto error;
1869*8b26181fSAndroid Build Coastguard Worker 	}
1870*8b26181fSAndroid Build Coastguard Worker 
1871*8b26181fSAndroid Build Coastguard Worker 	nread = sock_recv(pars->sockctrl, pars->ssl, source, plen,
1872*8b26181fSAndroid Build Coastguard Worker 	    SOCK_RECEIVEALL_YES|SOCK_EOF_IS_ERROR, errbuf, PCAP_ERRBUF_SIZE);
1873*8b26181fSAndroid Build Coastguard Worker 	if (nread == -1)
1874*8b26181fSAndroid Build Coastguard Worker 	{
1875*8b26181fSAndroid Build Coastguard Worker 		rpcapd_log(LOGPRIO_ERROR, "Read from client failed: %s", errbuf);
1876*8b26181fSAndroid Build Coastguard Worker 		return -1;
1877*8b26181fSAndroid Build Coastguard Worker 	}
1878*8b26181fSAndroid Build Coastguard Worker 	source[nread] = '\0';
1879*8b26181fSAndroid Build Coastguard Worker 	plen -= nread;
1880*8b26181fSAndroid Build Coastguard Worker 
1881*8b26181fSAndroid Build Coastguard Worker 	// Is this a URL rather than a device?
1882*8b26181fSAndroid Build Coastguard Worker 	// If so, reject it.
1883*8b26181fSAndroid Build Coastguard Worker 	if (is_url(source))
1884*8b26181fSAndroid Build Coastguard Worker 	{
1885*8b26181fSAndroid Build Coastguard Worker 		snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Source string refers to a remote device");
1886*8b26181fSAndroid Build Coastguard Worker 		goto error;
1887*8b26181fSAndroid Build Coastguard Worker 	}
1888*8b26181fSAndroid Build Coastguard Worker 
1889*8b26181fSAndroid Build Coastguard Worker 	// Open the selected device
1890*8b26181fSAndroid Build Coastguard Worker 	// This is a fake open, since we do that only to get the needed parameters, then we close the device again
1891*8b26181fSAndroid Build Coastguard Worker 	if ((fp = pcap_open_live(source,
1892*8b26181fSAndroid Build Coastguard Worker 			1500 /* fake snaplen */,
1893*8b26181fSAndroid Build Coastguard Worker 			0 /* no promisc */,
1894*8b26181fSAndroid Build Coastguard Worker 			1000 /* fake timeout */,
1895*8b26181fSAndroid Build Coastguard Worker 			errmsgbuf)) == NULL)
1896*8b26181fSAndroid Build Coastguard Worker 		goto error;
1897*8b26181fSAndroid Build Coastguard Worker 
1898*8b26181fSAndroid Build Coastguard Worker 	// Now, I can send a RPCAP open reply message
1899*8b26181fSAndroid Build Coastguard Worker 	if (sock_bufferize(NULL, sizeof(struct rpcap_header), NULL, &sendbufidx,
1900*8b26181fSAndroid Build Coastguard Worker 	    RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
1901*8b26181fSAndroid Build Coastguard Worker 		goto error;
1902*8b26181fSAndroid Build Coastguard Worker 
1903*8b26181fSAndroid Build Coastguard Worker 	rpcap_createhdr((struct rpcap_header *) sendbuf, ver,
1904*8b26181fSAndroid Build Coastguard Worker 	    RPCAP_MSG_OPEN_REPLY, 0, sizeof(struct rpcap_openreply));
1905*8b26181fSAndroid Build Coastguard Worker 
1906*8b26181fSAndroid Build Coastguard Worker 	openreply = (struct rpcap_openreply *) &sendbuf[sendbufidx];
1907*8b26181fSAndroid Build Coastguard Worker 
1908*8b26181fSAndroid Build Coastguard Worker 	if (sock_bufferize(NULL, sizeof(struct rpcap_openreply), NULL, &sendbufidx,
1909*8b26181fSAndroid Build Coastguard Worker 	    RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
1910*8b26181fSAndroid Build Coastguard Worker 		goto error;
1911*8b26181fSAndroid Build Coastguard Worker 
1912*8b26181fSAndroid Build Coastguard Worker 	memset(openreply, 0, sizeof(struct rpcap_openreply));
1913*8b26181fSAndroid Build Coastguard Worker 	openreply->linktype = htonl(pcap_datalink(fp));
1914*8b26181fSAndroid Build Coastguard Worker 	/*
1915*8b26181fSAndroid Build Coastguard Worker 	 * This is always 0 for live captures; we no longer support it
1916*8b26181fSAndroid Build Coastguard Worker 	 * as something we read from capture files and supply to
1917*8b26181fSAndroid Build Coastguard Worker 	 * clients, but we have to send it over the wire, as open
1918*8b26181fSAndroid Build Coastguard Worker 	 * replies are expected to have 8 bytes of payload by
1919*8b26181fSAndroid Build Coastguard Worker 	 * existing clients.
1920*8b26181fSAndroid Build Coastguard Worker 	 */
1921*8b26181fSAndroid Build Coastguard Worker 	openreply->tzoff = 0;
1922*8b26181fSAndroid Build Coastguard Worker 
1923*8b26181fSAndroid Build Coastguard Worker 	// We're done with the pcap_t.
1924*8b26181fSAndroid Build Coastguard Worker 	pcap_close(fp);
1925*8b26181fSAndroid Build Coastguard Worker 
1926*8b26181fSAndroid Build Coastguard Worker 	// Send the reply.
1927*8b26181fSAndroid Build Coastguard Worker 	if (sock_send(pars->sockctrl, pars->ssl, sendbuf, sendbufidx, errbuf, PCAP_ERRBUF_SIZE) == -1)
1928*8b26181fSAndroid Build Coastguard Worker 	{
1929*8b26181fSAndroid Build Coastguard Worker 		rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
1930*8b26181fSAndroid Build Coastguard Worker 		return -1;
1931*8b26181fSAndroid Build Coastguard Worker 	}
1932*8b26181fSAndroid Build Coastguard Worker 	return 0;
1933*8b26181fSAndroid Build Coastguard Worker 
1934*8b26181fSAndroid Build Coastguard Worker error:
1935*8b26181fSAndroid Build Coastguard Worker 	if (rpcap_senderror(pars->sockctrl, pars->ssl, ver, PCAP_ERR_OPEN,
1936*8b26181fSAndroid Build Coastguard Worker 	    errmsgbuf, errbuf) == -1)
1937*8b26181fSAndroid Build Coastguard Worker 	{
1938*8b26181fSAndroid Build Coastguard Worker 		// That failed; log a message and give up.
1939*8b26181fSAndroid Build Coastguard Worker 		rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
1940*8b26181fSAndroid Build Coastguard Worker 		return -1;
1941*8b26181fSAndroid Build Coastguard Worker 	}
1942*8b26181fSAndroid Build Coastguard Worker 
1943*8b26181fSAndroid Build Coastguard Worker 	// Check if all the data has been read; if not, discard the data in excess
1944*8b26181fSAndroid Build Coastguard Worker 	if (rpcapd_discard(pars->sockctrl, pars->ssl, plen) == -1)
1945*8b26181fSAndroid Build Coastguard Worker 	{
1946*8b26181fSAndroid Build Coastguard Worker 		return -1;
1947*8b26181fSAndroid Build Coastguard Worker 	}
1948*8b26181fSAndroid Build Coastguard Worker 	return 0;
1949*8b26181fSAndroid Build Coastguard Worker }
1950*8b26181fSAndroid Build Coastguard Worker 
1951*8b26181fSAndroid Build Coastguard Worker /*
1952*8b26181fSAndroid Build Coastguard Worker 	\param plen: the length of the current message (needed in order to be able
1953*8b26181fSAndroid Build Coastguard Worker 	to discard excess data in the message, if present)
1954*8b26181fSAndroid Build Coastguard Worker */
1955*8b26181fSAndroid Build Coastguard Worker static int
daemon_msg_startcap_req(uint8 ver,struct daemon_slpars * pars,uint32 plen,char * source,struct session ** sessionp,struct rpcap_sampling * samp_param _U_,int uses_ssl)1956*8b26181fSAndroid Build Coastguard Worker daemon_msg_startcap_req(uint8 ver, struct daemon_slpars *pars, uint32 plen,
1957*8b26181fSAndroid Build Coastguard Worker     char *source, struct session **sessionp,
1958*8b26181fSAndroid Build Coastguard Worker     struct rpcap_sampling *samp_param _U_, int uses_ssl)
1959*8b26181fSAndroid Build Coastguard Worker {
1960*8b26181fSAndroid Build Coastguard Worker 	char errbuf[PCAP_ERRBUF_SIZE];		// buffer for network errors
1961*8b26181fSAndroid Build Coastguard Worker 	char errmsgbuf[PCAP_ERRBUF_SIZE];	// buffer for errors to send to the client
1962*8b26181fSAndroid Build Coastguard Worker 	char portdata[PCAP_BUF_SIZE];		// temp variable needed to derive the data port
1963*8b26181fSAndroid Build Coastguard Worker 	char peerhost[PCAP_BUF_SIZE];		// temp variable needed to derive the host name of our peer
1964*8b26181fSAndroid Build Coastguard Worker 	struct session *session = NULL;		// saves state of session
1965*8b26181fSAndroid Build Coastguard Worker 	int status;
1966*8b26181fSAndroid Build Coastguard Worker 	char sendbuf[RPCAP_NETBUF_SIZE];	// temporary buffer in which data to be sent is buffered
1967*8b26181fSAndroid Build Coastguard Worker 	int sendbufidx = 0;			// index which keeps the number of bytes currently buffered
1968*8b26181fSAndroid Build Coastguard Worker 
1969*8b26181fSAndroid Build Coastguard Worker 	// socket-related variables
1970*8b26181fSAndroid Build Coastguard Worker 	struct addrinfo hints;			// temp, needed to open a socket connection
1971*8b26181fSAndroid Build Coastguard Worker 	struct addrinfo *addrinfo;		// temp, needed to open a socket connection
1972*8b26181fSAndroid Build Coastguard Worker 	struct sockaddr_storage saddr;		// temp, needed to retrieve the network data port chosen on the local machine
1973*8b26181fSAndroid Build Coastguard Worker 	socklen_t saddrlen;			// temp, needed to retrieve the network data port chosen on the local machine
1974*8b26181fSAndroid Build Coastguard Worker 	int ret;				// return value from functions
1975*8b26181fSAndroid Build Coastguard Worker 
1976*8b26181fSAndroid Build Coastguard Worker 	// RPCAP-related variables
1977*8b26181fSAndroid Build Coastguard Worker 	struct rpcap_startcapreq startcapreq;		// start capture request message
1978*8b26181fSAndroid Build Coastguard Worker 	struct rpcap_startcapreply *startcapreply;	// start capture reply message
1979*8b26181fSAndroid Build Coastguard Worker 	int serveropen_dp;							// keeps who is going to open the data connection
1980*8b26181fSAndroid Build Coastguard Worker 
1981*8b26181fSAndroid Build Coastguard Worker 	addrinfo = NULL;
1982*8b26181fSAndroid Build Coastguard Worker 
1983*8b26181fSAndroid Build Coastguard Worker 	status = rpcapd_recv(pars->sockctrl, pars->ssl, (char *) &startcapreq,
1984*8b26181fSAndroid Build Coastguard Worker 	    sizeof(struct rpcap_startcapreq), &plen, errmsgbuf);
1985*8b26181fSAndroid Build Coastguard Worker 	if (status == -1)
1986*8b26181fSAndroid Build Coastguard Worker 	{
1987*8b26181fSAndroid Build Coastguard Worker 		goto fatal_error;
1988*8b26181fSAndroid Build Coastguard Worker 	}
1989*8b26181fSAndroid Build Coastguard Worker 	if (status == -2)
1990*8b26181fSAndroid Build Coastguard Worker 	{
1991*8b26181fSAndroid Build Coastguard Worker 		goto error;
1992*8b26181fSAndroid Build Coastguard Worker 	}
1993*8b26181fSAndroid Build Coastguard Worker 
1994*8b26181fSAndroid Build Coastguard Worker 	startcapreq.flags = ntohs(startcapreq.flags);
1995*8b26181fSAndroid Build Coastguard Worker 
1996*8b26181fSAndroid Build Coastguard Worker 	// Check that the client does not ask for UDP is the server has been asked
1997*8b26181fSAndroid Build Coastguard Worker 	// to enforce encryption, as SSL is not supported yet with UDP:
1998*8b26181fSAndroid Build Coastguard Worker 	if (uses_ssl && (startcapreq.flags & RPCAP_STARTCAPREQ_FLAG_DGRAM))
1999*8b26181fSAndroid Build Coastguard Worker 	{
2000*8b26181fSAndroid Build Coastguard Worker 		snprintf(errbuf, PCAP_ERRBUF_SIZE,
2001*8b26181fSAndroid Build Coastguard Worker 		    "SSL not supported with UDP forward of remote packets");
2002*8b26181fSAndroid Build Coastguard Worker 		goto error;
2003*8b26181fSAndroid Build Coastguard Worker 	}
2004*8b26181fSAndroid Build Coastguard Worker 
2005*8b26181fSAndroid Build Coastguard Worker 	// Create a session structure
2006*8b26181fSAndroid Build Coastguard Worker 	session = malloc(sizeof(struct session));
2007*8b26181fSAndroid Build Coastguard Worker 	if (session == NULL)
2008*8b26181fSAndroid Build Coastguard Worker 	{
2009*8b26181fSAndroid Build Coastguard Worker 		snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Can't allocate session structure");
2010*8b26181fSAndroid Build Coastguard Worker 		goto error;
2011*8b26181fSAndroid Build Coastguard Worker 	}
2012*8b26181fSAndroid Build Coastguard Worker 
2013*8b26181fSAndroid Build Coastguard Worker 	session->sockdata = INVALID_SOCKET;
2014*8b26181fSAndroid Build Coastguard Worker 	session->ctrl_ssl = session->data_ssl = NULL;
2015*8b26181fSAndroid Build Coastguard Worker 	// We don't have a thread yet.
2016*8b26181fSAndroid Build Coastguard Worker 	session->have_thread = 0;
2017*8b26181fSAndroid Build Coastguard Worker 	//
2018*8b26181fSAndroid Build Coastguard Worker 	// We *shouldn't* have to initialize the thread indicator
2019*8b26181fSAndroid Build Coastguard Worker 	// itself, because the compiler *should* realize that we
2020*8b26181fSAndroid Build Coastguard Worker 	// only use this if have_thread isn't 0, but we *do* have
2021*8b26181fSAndroid Build Coastguard Worker 	// to do it, because not all compilers *do* realize that.
2022*8b26181fSAndroid Build Coastguard Worker 	//
2023*8b26181fSAndroid Build Coastguard Worker 	// There is no "invalid thread handle" value for a UN*X
2024*8b26181fSAndroid Build Coastguard Worker 	// pthread_t, so we just zero it out.
2025*8b26181fSAndroid Build Coastguard Worker 	//
2026*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
2027*8b26181fSAndroid Build Coastguard Worker 	session->thread = INVALID_HANDLE_VALUE;
2028*8b26181fSAndroid Build Coastguard Worker #else
2029*8b26181fSAndroid Build Coastguard Worker 	memset(&session->thread, 0, sizeof(session->thread));
2030*8b26181fSAndroid Build Coastguard Worker #endif
2031*8b26181fSAndroid Build Coastguard Worker 
2032*8b26181fSAndroid Build Coastguard Worker 	// Open the selected device
2033*8b26181fSAndroid Build Coastguard Worker 	if ((session->fp = pcap_open_live(source,
2034*8b26181fSAndroid Build Coastguard Worker 			ntohl(startcapreq.snaplen),
2035*8b26181fSAndroid Build Coastguard Worker 			(startcapreq.flags & RPCAP_STARTCAPREQ_FLAG_PROMISC) ? 1 : 0 /* local device, other flags not needed */,
2036*8b26181fSAndroid Build Coastguard Worker 			ntohl(startcapreq.read_timeout),
2037*8b26181fSAndroid Build Coastguard Worker 			errmsgbuf)) == NULL)
2038*8b26181fSAndroid Build Coastguard Worker 		goto error;
2039*8b26181fSAndroid Build Coastguard Worker 
2040*8b26181fSAndroid Build Coastguard Worker #if 0
2041*8b26181fSAndroid Build Coastguard Worker 	// Apply sampling parameters
2042*8b26181fSAndroid Build Coastguard Worker 	fp->rmt_samp.method = samp_param->method;
2043*8b26181fSAndroid Build Coastguard Worker 	fp->rmt_samp.value = samp_param->value;
2044*8b26181fSAndroid Build Coastguard Worker #endif
2045*8b26181fSAndroid Build Coastguard Worker 
2046*8b26181fSAndroid Build Coastguard Worker 	/*
2047*8b26181fSAndroid Build Coastguard Worker 	We're in active mode if:
2048*8b26181fSAndroid Build Coastguard Worker 	- we're using TCP, and the user wants us to be in active mode
2049*8b26181fSAndroid Build Coastguard Worker 	- we're using UDP
2050*8b26181fSAndroid Build Coastguard Worker 	*/
2051*8b26181fSAndroid Build Coastguard Worker 	serveropen_dp = (startcapreq.flags & RPCAP_STARTCAPREQ_FLAG_SERVEROPEN) || (startcapreq.flags & RPCAP_STARTCAPREQ_FLAG_DGRAM) || pars->isactive;
2052*8b26181fSAndroid Build Coastguard Worker 
2053*8b26181fSAndroid Build Coastguard Worker 	/*
2054*8b26181fSAndroid Build Coastguard Worker 	Gets the sockaddr structure referred to the other peer in the ctrl connection
2055*8b26181fSAndroid Build Coastguard Worker 
2056*8b26181fSAndroid Build Coastguard Worker 	We need that because:
2057*8b26181fSAndroid Build Coastguard Worker 	- if we're in passive mode, we need to know the address family we want to use
2058*8b26181fSAndroid Build Coastguard Worker 	(the same used for the ctrl socket)
2059*8b26181fSAndroid Build Coastguard Worker 	- if we're in active mode, we need to know the network address of the other host
2060*8b26181fSAndroid Build Coastguard Worker 	we want to connect to
2061*8b26181fSAndroid Build Coastguard Worker 	*/
2062*8b26181fSAndroid Build Coastguard Worker 	saddrlen = sizeof(struct sockaddr_storage);
2063*8b26181fSAndroid Build Coastguard Worker 	if (getpeername(pars->sockctrl, (struct sockaddr *) &saddr, &saddrlen) == -1)
2064*8b26181fSAndroid Build Coastguard Worker 	{
2065*8b26181fSAndroid Build Coastguard Worker 		sock_geterrmsg(errmsgbuf, PCAP_ERRBUF_SIZE,
2066*8b26181fSAndroid Build Coastguard Worker 		    "getpeername() failed");
2067*8b26181fSAndroid Build Coastguard Worker 		goto error;
2068*8b26181fSAndroid Build Coastguard Worker 	}
2069*8b26181fSAndroid Build Coastguard Worker 
2070*8b26181fSAndroid Build Coastguard Worker 	memset(&hints, 0, sizeof(struct addrinfo));
2071*8b26181fSAndroid Build Coastguard Worker 	hints.ai_socktype = (startcapreq.flags & RPCAP_STARTCAPREQ_FLAG_DGRAM) ? SOCK_DGRAM : SOCK_STREAM;
2072*8b26181fSAndroid Build Coastguard Worker 	hints.ai_family = saddr.ss_family;
2073*8b26181fSAndroid Build Coastguard Worker 
2074*8b26181fSAndroid Build Coastguard Worker 	// Now we have to create a new socket to send packets
2075*8b26181fSAndroid Build Coastguard Worker 	if (serveropen_dp)		// Data connection is opened by the server toward the client
2076*8b26181fSAndroid Build Coastguard Worker 	{
2077*8b26181fSAndroid Build Coastguard Worker 		snprintf(portdata, sizeof portdata, "%d", ntohs(startcapreq.portdata));
2078*8b26181fSAndroid Build Coastguard Worker 
2079*8b26181fSAndroid Build Coastguard Worker 		// Get the name of the other peer (needed to connect to that specific network address)
2080*8b26181fSAndroid Build Coastguard Worker 		if (getnameinfo((struct sockaddr *) &saddr, saddrlen, peerhost,
2081*8b26181fSAndroid Build Coastguard Worker 				sizeof(peerhost), NULL, 0, NI_NUMERICHOST))
2082*8b26181fSAndroid Build Coastguard Worker 		{
2083*8b26181fSAndroid Build Coastguard Worker 			sock_geterrmsg(errmsgbuf, PCAP_ERRBUF_SIZE,
2084*8b26181fSAndroid Build Coastguard Worker 			    "getnameinfo() failed");
2085*8b26181fSAndroid Build Coastguard Worker 			goto error;
2086*8b26181fSAndroid Build Coastguard Worker 		}
2087*8b26181fSAndroid Build Coastguard Worker 
2088*8b26181fSAndroid Build Coastguard Worker 		if (sock_initaddress(peerhost, portdata, &hints, &addrinfo, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
2089*8b26181fSAndroid Build Coastguard Worker 			goto error;
2090*8b26181fSAndroid Build Coastguard Worker 
2091*8b26181fSAndroid Build Coastguard Worker 		if ((session->sockdata = sock_open(peerhost, addrinfo, SOCKOPEN_CLIENT, 0, errmsgbuf, PCAP_ERRBUF_SIZE)) == INVALID_SOCKET)
2092*8b26181fSAndroid Build Coastguard Worker 			goto error;
2093*8b26181fSAndroid Build Coastguard Worker 	}
2094*8b26181fSAndroid Build Coastguard Worker 	else		// Data connection is opened by the client toward the server
2095*8b26181fSAndroid Build Coastguard Worker 	{
2096*8b26181fSAndroid Build Coastguard Worker 		hints.ai_flags = AI_PASSIVE;
2097*8b26181fSAndroid Build Coastguard Worker 
2098*8b26181fSAndroid Build Coastguard Worker 		// Make the server socket pick up a free network port for us
2099*8b26181fSAndroid Build Coastguard Worker 		if (sock_initaddress(NULL, NULL, &hints, &addrinfo, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
2100*8b26181fSAndroid Build Coastguard Worker 			goto error;
2101*8b26181fSAndroid Build Coastguard Worker 
2102*8b26181fSAndroid Build Coastguard Worker 		if ((session->sockdata = sock_open(NULL, addrinfo, SOCKOPEN_SERVER, 1 /* max 1 connection in queue */, errmsgbuf, PCAP_ERRBUF_SIZE)) == INVALID_SOCKET)
2103*8b26181fSAndroid Build Coastguard Worker 			goto error;
2104*8b26181fSAndroid Build Coastguard Worker 
2105*8b26181fSAndroid Build Coastguard Worker 		// get the complete sockaddr structure used in the data connection
2106*8b26181fSAndroid Build Coastguard Worker 		saddrlen = sizeof(struct sockaddr_storage);
2107*8b26181fSAndroid Build Coastguard Worker 		if (getsockname(session->sockdata, (struct sockaddr *) &saddr, &saddrlen) == -1)
2108*8b26181fSAndroid Build Coastguard Worker 		{
2109*8b26181fSAndroid Build Coastguard Worker 			sock_geterrmsg(errmsgbuf, PCAP_ERRBUF_SIZE,
2110*8b26181fSAndroid Build Coastguard Worker 			    "getsockname() failed");
2111*8b26181fSAndroid Build Coastguard Worker 			goto error;
2112*8b26181fSAndroid Build Coastguard Worker 		}
2113*8b26181fSAndroid Build Coastguard Worker 
2114*8b26181fSAndroid Build Coastguard Worker 		// Get the local port the system picked up
2115*8b26181fSAndroid Build Coastguard Worker 		if (getnameinfo((struct sockaddr *) &saddr, saddrlen, NULL,
2116*8b26181fSAndroid Build Coastguard Worker 				0, portdata, sizeof(portdata), NI_NUMERICSERV))
2117*8b26181fSAndroid Build Coastguard Worker 		{
2118*8b26181fSAndroid Build Coastguard Worker 			sock_geterrmsg(errmsgbuf, PCAP_ERRBUF_SIZE,
2119*8b26181fSAndroid Build Coastguard Worker 			    "getnameinfo() failed");
2120*8b26181fSAndroid Build Coastguard Worker 			goto error;
2121*8b26181fSAndroid Build Coastguard Worker 		}
2122*8b26181fSAndroid Build Coastguard Worker 	}
2123*8b26181fSAndroid Build Coastguard Worker 
2124*8b26181fSAndroid Build Coastguard Worker 	// addrinfo is no longer used
2125*8b26181fSAndroid Build Coastguard Worker 	freeaddrinfo(addrinfo);
2126*8b26181fSAndroid Build Coastguard Worker 	addrinfo = NULL;
2127*8b26181fSAndroid Build Coastguard Worker 
2128*8b26181fSAndroid Build Coastguard Worker 	// Needed to send an error on the ctrl connection
2129*8b26181fSAndroid Build Coastguard Worker 	session->sockctrl = pars->sockctrl;
2130*8b26181fSAndroid Build Coastguard Worker 	session->ctrl_ssl = pars->ssl;
2131*8b26181fSAndroid Build Coastguard Worker 	session->protocol_version = ver;
2132*8b26181fSAndroid Build Coastguard Worker 
2133*8b26181fSAndroid Build Coastguard Worker 	// Now I can set the filter
2134*8b26181fSAndroid Build Coastguard Worker 	ret = daemon_unpackapplyfilter(pars->sockctrl, pars->ssl, session, &plen, errmsgbuf);
2135*8b26181fSAndroid Build Coastguard Worker 	if (ret == -1)
2136*8b26181fSAndroid Build Coastguard Worker 	{
2137*8b26181fSAndroid Build Coastguard Worker 		// Fatal error.  A message has been logged; just give up.
2138*8b26181fSAndroid Build Coastguard Worker 		goto fatal_error;
2139*8b26181fSAndroid Build Coastguard Worker 	}
2140*8b26181fSAndroid Build Coastguard Worker 	if (ret == -2)
2141*8b26181fSAndroid Build Coastguard Worker 	{
2142*8b26181fSAndroid Build Coastguard Worker 		// Non-fatal error.  Send an error message to the client.
2143*8b26181fSAndroid Build Coastguard Worker 		goto error;
2144*8b26181fSAndroid Build Coastguard Worker 	}
2145*8b26181fSAndroid Build Coastguard Worker 
2146*8b26181fSAndroid Build Coastguard Worker 	// Now, I can send a RPCAP start capture reply message
2147*8b26181fSAndroid Build Coastguard Worker 	if (sock_bufferize(NULL, sizeof(struct rpcap_header), NULL, &sendbufidx,
2148*8b26181fSAndroid Build Coastguard Worker 	    RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
2149*8b26181fSAndroid Build Coastguard Worker 		goto error;
2150*8b26181fSAndroid Build Coastguard Worker 
2151*8b26181fSAndroid Build Coastguard Worker 	rpcap_createhdr((struct rpcap_header *) sendbuf, ver,
2152*8b26181fSAndroid Build Coastguard Worker 	    RPCAP_MSG_STARTCAP_REPLY, 0, sizeof(struct rpcap_startcapreply));
2153*8b26181fSAndroid Build Coastguard Worker 
2154*8b26181fSAndroid Build Coastguard Worker 	startcapreply = (struct rpcap_startcapreply *) &sendbuf[sendbufidx];
2155*8b26181fSAndroid Build Coastguard Worker 
2156*8b26181fSAndroid Build Coastguard Worker 	if (sock_bufferize(NULL, sizeof(struct rpcap_startcapreply), NULL,
2157*8b26181fSAndroid Build Coastguard Worker 	    &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
2158*8b26181fSAndroid Build Coastguard Worker 		goto error;
2159*8b26181fSAndroid Build Coastguard Worker 
2160*8b26181fSAndroid Build Coastguard Worker 	memset(startcapreply, 0, sizeof(struct rpcap_startcapreply));
2161*8b26181fSAndroid Build Coastguard Worker 	startcapreply->bufsize = htonl(pcap_bufsize(session->fp));
2162*8b26181fSAndroid Build Coastguard Worker 
2163*8b26181fSAndroid Build Coastguard Worker 	if (!serveropen_dp)
2164*8b26181fSAndroid Build Coastguard Worker 	{
2165*8b26181fSAndroid Build Coastguard Worker 		unsigned short port = (unsigned short)strtoul(portdata,NULL,10);
2166*8b26181fSAndroid Build Coastguard Worker 		startcapreply->portdata = htons(port);
2167*8b26181fSAndroid Build Coastguard Worker 	}
2168*8b26181fSAndroid Build Coastguard Worker 
2169*8b26181fSAndroid Build Coastguard Worker 	if (sock_send(pars->sockctrl, pars->ssl, sendbuf, sendbufidx, errbuf, PCAP_ERRBUF_SIZE) == -1)
2170*8b26181fSAndroid Build Coastguard Worker 	{
2171*8b26181fSAndroid Build Coastguard Worker 		// That failed; log a message and give up.
2172*8b26181fSAndroid Build Coastguard Worker 		rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
2173*8b26181fSAndroid Build Coastguard Worker 		goto fatal_error;
2174*8b26181fSAndroid Build Coastguard Worker 	}
2175*8b26181fSAndroid Build Coastguard Worker 
2176*8b26181fSAndroid Build Coastguard Worker 	if (!serveropen_dp)
2177*8b26181fSAndroid Build Coastguard Worker 	{
2178*8b26181fSAndroid Build Coastguard Worker 		SOCKET socktemp;	// We need another socket, since we're going to accept() a connection
2179*8b26181fSAndroid Build Coastguard Worker 
2180*8b26181fSAndroid Build Coastguard Worker 		// Connection creation
2181*8b26181fSAndroid Build Coastguard Worker 		saddrlen = sizeof(struct sockaddr_storage);
2182*8b26181fSAndroid Build Coastguard Worker 
2183*8b26181fSAndroid Build Coastguard Worker 		socktemp = accept(session->sockdata, (struct sockaddr *) &saddr, &saddrlen);
2184*8b26181fSAndroid Build Coastguard Worker 
2185*8b26181fSAndroid Build Coastguard Worker 		if (socktemp == INVALID_SOCKET)
2186*8b26181fSAndroid Build Coastguard Worker 		{
2187*8b26181fSAndroid Build Coastguard Worker 			sock_geterrmsg(errbuf, PCAP_ERRBUF_SIZE,
2188*8b26181fSAndroid Build Coastguard Worker 			   "accept() failed");
2189*8b26181fSAndroid Build Coastguard Worker 			rpcapd_log(LOGPRIO_ERROR, "Accept of data connection failed: %s",
2190*8b26181fSAndroid Build Coastguard Worker 			    errbuf);
2191*8b26181fSAndroid Build Coastguard Worker 			goto error;
2192*8b26181fSAndroid Build Coastguard Worker 		}
2193*8b26181fSAndroid Build Coastguard Worker 
2194*8b26181fSAndroid Build Coastguard Worker 		// Now that I accepted the connection, the server socket is no longer needed
2195*8b26181fSAndroid Build Coastguard Worker 		sock_close(session->sockdata, NULL, 0);
2196*8b26181fSAndroid Build Coastguard Worker 		session->sockdata = socktemp;
2197*8b26181fSAndroid Build Coastguard Worker 	}
2198*8b26181fSAndroid Build Coastguard Worker 
2199*8b26181fSAndroid Build Coastguard Worker 	SSL *ssl = NULL;
2200*8b26181fSAndroid Build Coastguard Worker 	if (uses_ssl)
2201*8b26181fSAndroid Build Coastguard Worker 	{
2202*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OPENSSL
2203*8b26181fSAndroid Build Coastguard Worker 		/* In both active or passive cases, wait for the client to initiate the
2204*8b26181fSAndroid Build Coastguard Worker 		 * TLS handshake. Yes during that time the control socket will not be
2205*8b26181fSAndroid Build Coastguard Worker 		 * served, but the same was true from the above call to accept(). */
2206*8b26181fSAndroid Build Coastguard Worker 		ssl = ssl_promotion(1, session->sockdata, errbuf, PCAP_ERRBUF_SIZE);
2207*8b26181fSAndroid Build Coastguard Worker 		if (! ssl)
2208*8b26181fSAndroid Build Coastguard Worker 		{
2209*8b26181fSAndroid Build Coastguard Worker 			rpcapd_log(LOGPRIO_ERROR, "TLS handshake failed: %s", errbuf);
2210*8b26181fSAndroid Build Coastguard Worker 			goto error;
2211*8b26181fSAndroid Build Coastguard Worker 		}
2212*8b26181fSAndroid Build Coastguard Worker #endif
2213*8b26181fSAndroid Build Coastguard Worker 	}
2214*8b26181fSAndroid Build Coastguard Worker 	session->data_ssl = ssl;
2215*8b26181fSAndroid Build Coastguard Worker 
2216*8b26181fSAndroid Build Coastguard Worker 	// Now we have to create a new thread to receive packets
2217*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
2218*8b26181fSAndroid Build Coastguard Worker 	session->thread = (HANDLE)_beginthreadex(NULL, 0, daemon_thrdatamain,
2219*8b26181fSAndroid Build Coastguard Worker 	    (void *) session, 0, NULL);
2220*8b26181fSAndroid Build Coastguard Worker 	if (session->thread == 0)
2221*8b26181fSAndroid Build Coastguard Worker 	{
2222*8b26181fSAndroid Build Coastguard Worker 		snprintf(errbuf, PCAP_ERRBUF_SIZE, "Error creating the data thread");
2223*8b26181fSAndroid Build Coastguard Worker 		goto error;
2224*8b26181fSAndroid Build Coastguard Worker 	}
2225*8b26181fSAndroid Build Coastguard Worker #else
2226*8b26181fSAndroid Build Coastguard Worker 	ret = pthread_create(&session->thread, NULL, daemon_thrdatamain,
2227*8b26181fSAndroid Build Coastguard Worker 	    (void *) session);
2228*8b26181fSAndroid Build Coastguard Worker 	if (ret != 0)
2229*8b26181fSAndroid Build Coastguard Worker 	{
2230*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
2231*8b26181fSAndroid Build Coastguard Worker 		    ret, "Error creating the data thread");
2232*8b26181fSAndroid Build Coastguard Worker 		goto error;
2233*8b26181fSAndroid Build Coastguard Worker 	}
2234*8b26181fSAndroid Build Coastguard Worker #endif
2235*8b26181fSAndroid Build Coastguard Worker 	session->have_thread = 1;
2236*8b26181fSAndroid Build Coastguard Worker 
2237*8b26181fSAndroid Build Coastguard Worker 	// Check if all the data has been read; if not, discard the data in excess
2238*8b26181fSAndroid Build Coastguard Worker 	if (rpcapd_discard(pars->sockctrl, pars->ssl, plen) == -1)
2239*8b26181fSAndroid Build Coastguard Worker 		goto fatal_error;
2240*8b26181fSAndroid Build Coastguard Worker 
2241*8b26181fSAndroid Build Coastguard Worker 	*sessionp = session;
2242*8b26181fSAndroid Build Coastguard Worker 	return 0;
2243*8b26181fSAndroid Build Coastguard Worker 
2244*8b26181fSAndroid Build Coastguard Worker error:
2245*8b26181fSAndroid Build Coastguard Worker 	//
2246*8b26181fSAndroid Build Coastguard Worker 	// Not a fatal error, so send the client an error message and
2247*8b26181fSAndroid Build Coastguard Worker 	// keep serving client requests.
2248*8b26181fSAndroid Build Coastguard Worker 	//
2249*8b26181fSAndroid Build Coastguard Worker 	*sessionp = NULL;
2250*8b26181fSAndroid Build Coastguard Worker 
2251*8b26181fSAndroid Build Coastguard Worker 	if (addrinfo)
2252*8b26181fSAndroid Build Coastguard Worker 		freeaddrinfo(addrinfo);
2253*8b26181fSAndroid Build Coastguard Worker 
2254*8b26181fSAndroid Build Coastguard Worker 	if (session)
2255*8b26181fSAndroid Build Coastguard Worker 	{
2256*8b26181fSAndroid Build Coastguard Worker 		session_close(session);
2257*8b26181fSAndroid Build Coastguard Worker 		free(session);
2258*8b26181fSAndroid Build Coastguard Worker 	}
2259*8b26181fSAndroid Build Coastguard Worker 
2260*8b26181fSAndroid Build Coastguard Worker 	if (rpcap_senderror(pars->sockctrl, pars->ssl, ver,
2261*8b26181fSAndroid Build Coastguard Worker 	    PCAP_ERR_STARTCAPTURE, errmsgbuf, errbuf) == -1)
2262*8b26181fSAndroid Build Coastguard Worker 	{
2263*8b26181fSAndroid Build Coastguard Worker 		// That failed; log a message and give up.
2264*8b26181fSAndroid Build Coastguard Worker 		rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
2265*8b26181fSAndroid Build Coastguard Worker 		return -1;
2266*8b26181fSAndroid Build Coastguard Worker 	}
2267*8b26181fSAndroid Build Coastguard Worker 
2268*8b26181fSAndroid Build Coastguard Worker 	// Check if all the data has been read; if not, discard the data in excess
2269*8b26181fSAndroid Build Coastguard Worker 	if (rpcapd_discard(pars->sockctrl, pars->ssl, plen) == -1)
2270*8b26181fSAndroid Build Coastguard Worker 	{
2271*8b26181fSAndroid Build Coastguard Worker 		// Network error.
2272*8b26181fSAndroid Build Coastguard Worker 		return -1;
2273*8b26181fSAndroid Build Coastguard Worker 	}
2274*8b26181fSAndroid Build Coastguard Worker 
2275*8b26181fSAndroid Build Coastguard Worker 	return 0;
2276*8b26181fSAndroid Build Coastguard Worker 
2277*8b26181fSAndroid Build Coastguard Worker fatal_error:
2278*8b26181fSAndroid Build Coastguard Worker 	//
2279*8b26181fSAndroid Build Coastguard Worker 	// Fatal network error, so don't try to communicate with
2280*8b26181fSAndroid Build Coastguard Worker 	// the client, just give up.
2281*8b26181fSAndroid Build Coastguard Worker 	//
2282*8b26181fSAndroid Build Coastguard Worker 	*sessionp = NULL;
2283*8b26181fSAndroid Build Coastguard Worker 
2284*8b26181fSAndroid Build Coastguard Worker 	if (session)
2285*8b26181fSAndroid Build Coastguard Worker 	{
2286*8b26181fSAndroid Build Coastguard Worker 		session_close(session);
2287*8b26181fSAndroid Build Coastguard Worker 		free(session);
2288*8b26181fSAndroid Build Coastguard Worker 	}
2289*8b26181fSAndroid Build Coastguard Worker 
2290*8b26181fSAndroid Build Coastguard Worker 	return -1;
2291*8b26181fSAndroid Build Coastguard Worker }
2292*8b26181fSAndroid Build Coastguard Worker 
2293*8b26181fSAndroid Build Coastguard Worker static int
daemon_msg_endcap_req(uint8 ver,struct daemon_slpars * pars,struct session * session)2294*8b26181fSAndroid Build Coastguard Worker daemon_msg_endcap_req(uint8 ver, struct daemon_slpars *pars,
2295*8b26181fSAndroid Build Coastguard Worker     struct session *session)
2296*8b26181fSAndroid Build Coastguard Worker {
2297*8b26181fSAndroid Build Coastguard Worker 	char errbuf[PCAP_ERRBUF_SIZE];		// buffer for network errors
2298*8b26181fSAndroid Build Coastguard Worker 	struct rpcap_header header;
2299*8b26181fSAndroid Build Coastguard Worker 
2300*8b26181fSAndroid Build Coastguard Worker 	session_close(session);
2301*8b26181fSAndroid Build Coastguard Worker 
2302*8b26181fSAndroid Build Coastguard Worker 	rpcap_createhdr(&header, ver, RPCAP_MSG_ENDCAP_REPLY, 0, 0);
2303*8b26181fSAndroid Build Coastguard Worker 
2304*8b26181fSAndroid Build Coastguard Worker 	if (sock_send(pars->sockctrl, pars->ssl, (char *) &header, sizeof(struct rpcap_header), errbuf, PCAP_ERRBUF_SIZE) == -1)
2305*8b26181fSAndroid Build Coastguard Worker 	{
2306*8b26181fSAndroid Build Coastguard Worker 		// That failed; log a message and give up.
2307*8b26181fSAndroid Build Coastguard Worker 		rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
2308*8b26181fSAndroid Build Coastguard Worker 		return -1;
2309*8b26181fSAndroid Build Coastguard Worker 	}
2310*8b26181fSAndroid Build Coastguard Worker 
2311*8b26181fSAndroid Build Coastguard Worker 	return 0;
2312*8b26181fSAndroid Build Coastguard Worker }
2313*8b26181fSAndroid Build Coastguard Worker 
2314*8b26181fSAndroid Build Coastguard Worker //
2315*8b26181fSAndroid Build Coastguard Worker // We impose a limit on the filter program size, so that, on Windows,
2316*8b26181fSAndroid Build Coastguard Worker // where there's only one server process with multiple threads, it's
2317*8b26181fSAndroid Build Coastguard Worker // harder to eat all the server address space by sending larger filter
2318*8b26181fSAndroid Build Coastguard Worker // programs.  (This isn't an issue on UN*X, where there are multiple
2319*8b26181fSAndroid Build Coastguard Worker // server processes, one per client connection.)
2320*8b26181fSAndroid Build Coastguard Worker //
2321*8b26181fSAndroid Build Coastguard Worker // We pick a value that limits each filter to 64K; that value is twice
2322*8b26181fSAndroid Build Coastguard Worker // the in-kernel limit for Linux and 16 times the in-kernel limit for
2323*8b26181fSAndroid Build Coastguard Worker // *BSD and macOS.
2324*8b26181fSAndroid Build Coastguard Worker //
2325*8b26181fSAndroid Build Coastguard Worker // It also prevents an overflow on 32-bit platforms when calculating
2326*8b26181fSAndroid Build Coastguard Worker // the total size of the filter program.  (It's not an issue on 64-bit
2327*8b26181fSAndroid Build Coastguard Worker // platforms with a 64-bit size_t, as the filter size is 32 bits.)
2328*8b26181fSAndroid Build Coastguard Worker //
2329*8b26181fSAndroid Build Coastguard Worker #define RPCAP_BPF_MAXINSNS	8192
2330*8b26181fSAndroid Build Coastguard Worker 
2331*8b26181fSAndroid Build Coastguard Worker static int
daemon_unpackapplyfilter(SOCKET sockctrl,SSL * ctrl_ssl,struct session * session,uint32 * plenp,char * errmsgbuf)2332*8b26181fSAndroid Build Coastguard Worker daemon_unpackapplyfilter(SOCKET sockctrl, SSL *ctrl_ssl, struct session *session, uint32 *plenp, char *errmsgbuf)
2333*8b26181fSAndroid Build Coastguard Worker {
2334*8b26181fSAndroid Build Coastguard Worker 	int status;
2335*8b26181fSAndroid Build Coastguard Worker 	struct rpcap_filter filter;
2336*8b26181fSAndroid Build Coastguard Worker 	struct rpcap_filterbpf_insn insn;
2337*8b26181fSAndroid Build Coastguard Worker 	struct bpf_insn *bf_insn;
2338*8b26181fSAndroid Build Coastguard Worker 	struct bpf_program bf_prog;
2339*8b26181fSAndroid Build Coastguard Worker 	unsigned int i;
2340*8b26181fSAndroid Build Coastguard Worker 
2341*8b26181fSAndroid Build Coastguard Worker 	status = rpcapd_recv(sockctrl, ctrl_ssl, (char *) &filter,
2342*8b26181fSAndroid Build Coastguard Worker 	    sizeof(struct rpcap_filter), plenp, errmsgbuf);
2343*8b26181fSAndroid Build Coastguard Worker 	if (status == -1)
2344*8b26181fSAndroid Build Coastguard Worker 	{
2345*8b26181fSAndroid Build Coastguard Worker 		return -1;
2346*8b26181fSAndroid Build Coastguard Worker 	}
2347*8b26181fSAndroid Build Coastguard Worker 	if (status == -2)
2348*8b26181fSAndroid Build Coastguard Worker 	{
2349*8b26181fSAndroid Build Coastguard Worker 		return -2;
2350*8b26181fSAndroid Build Coastguard Worker 	}
2351*8b26181fSAndroid Build Coastguard Worker 
2352*8b26181fSAndroid Build Coastguard Worker 	bf_prog.bf_len = ntohl(filter.nitems);
2353*8b26181fSAndroid Build Coastguard Worker 
2354*8b26181fSAndroid Build Coastguard Worker 	if (ntohs(filter.filtertype) != RPCAP_UPDATEFILTER_BPF)
2355*8b26181fSAndroid Build Coastguard Worker 	{
2356*8b26181fSAndroid Build Coastguard Worker 		snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Only BPF/NPF filters are currently supported");
2357*8b26181fSAndroid Build Coastguard Worker 		return -2;
2358*8b26181fSAndroid Build Coastguard Worker 	}
2359*8b26181fSAndroid Build Coastguard Worker 
2360*8b26181fSAndroid Build Coastguard Worker 	if (bf_prog.bf_len > RPCAP_BPF_MAXINSNS)
2361*8b26181fSAndroid Build Coastguard Worker 	{
2362*8b26181fSAndroid Build Coastguard Worker 		snprintf(errmsgbuf, PCAP_ERRBUF_SIZE,
2363*8b26181fSAndroid Build Coastguard Worker 		    "Filter program is larger than the maximum size of %d instructions",
2364*8b26181fSAndroid Build Coastguard Worker 		    RPCAP_BPF_MAXINSNS);
2365*8b26181fSAndroid Build Coastguard Worker 		return -2;
2366*8b26181fSAndroid Build Coastguard Worker 	}
2367*8b26181fSAndroid Build Coastguard Worker 	bf_insn = (struct bpf_insn *) malloc (sizeof(struct bpf_insn) * bf_prog.bf_len);
2368*8b26181fSAndroid Build Coastguard Worker 	if (bf_insn == NULL)
2369*8b26181fSAndroid Build Coastguard Worker 	{
2370*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(errmsgbuf, PCAP_ERRBUF_SIZE,
2371*8b26181fSAndroid Build Coastguard Worker 		    errno, "malloc() failed");
2372*8b26181fSAndroid Build Coastguard Worker 		return -2;
2373*8b26181fSAndroid Build Coastguard Worker 	}
2374*8b26181fSAndroid Build Coastguard Worker 
2375*8b26181fSAndroid Build Coastguard Worker 	bf_prog.bf_insns = bf_insn;
2376*8b26181fSAndroid Build Coastguard Worker 
2377*8b26181fSAndroid Build Coastguard Worker 	for (i = 0; i < bf_prog.bf_len; i++)
2378*8b26181fSAndroid Build Coastguard Worker 	{
2379*8b26181fSAndroid Build Coastguard Worker 		status = rpcapd_recv(sockctrl, ctrl_ssl, (char *) &insn,
2380*8b26181fSAndroid Build Coastguard Worker 		    sizeof(struct rpcap_filterbpf_insn), plenp, errmsgbuf);
2381*8b26181fSAndroid Build Coastguard Worker 		if (status == -1)
2382*8b26181fSAndroid Build Coastguard Worker 		{
2383*8b26181fSAndroid Build Coastguard Worker 			return -1;
2384*8b26181fSAndroid Build Coastguard Worker 		}
2385*8b26181fSAndroid Build Coastguard Worker 		if (status == -2)
2386*8b26181fSAndroid Build Coastguard Worker 		{
2387*8b26181fSAndroid Build Coastguard Worker 			return -2;
2388*8b26181fSAndroid Build Coastguard Worker 		}
2389*8b26181fSAndroid Build Coastguard Worker 
2390*8b26181fSAndroid Build Coastguard Worker 		bf_insn->code = ntohs(insn.code);
2391*8b26181fSAndroid Build Coastguard Worker 		bf_insn->jf = insn.jf;
2392*8b26181fSAndroid Build Coastguard Worker 		bf_insn->jt = insn.jt;
2393*8b26181fSAndroid Build Coastguard Worker 		bf_insn->k = ntohl(insn.k);
2394*8b26181fSAndroid Build Coastguard Worker 
2395*8b26181fSAndroid Build Coastguard Worker 		bf_insn++;
2396*8b26181fSAndroid Build Coastguard Worker 	}
2397*8b26181fSAndroid Build Coastguard Worker 
2398*8b26181fSAndroid Build Coastguard Worker 	//
2399*8b26181fSAndroid Build Coastguard Worker 	// XXX - pcap_setfilter() should do the validation for us.
2400*8b26181fSAndroid Build Coastguard Worker 	//
2401*8b26181fSAndroid Build Coastguard Worker 	if (bpf_validate(bf_prog.bf_insns, bf_prog.bf_len) == 0)
2402*8b26181fSAndroid Build Coastguard Worker 	{
2403*8b26181fSAndroid Build Coastguard Worker 		snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "The filter contains bogus instructions");
2404*8b26181fSAndroid Build Coastguard Worker 		return -2;
2405*8b26181fSAndroid Build Coastguard Worker 	}
2406*8b26181fSAndroid Build Coastguard Worker 
2407*8b26181fSAndroid Build Coastguard Worker 	if (pcap_setfilter(session->fp, &bf_prog))
2408*8b26181fSAndroid Build Coastguard Worker 	{
2409*8b26181fSAndroid Build Coastguard Worker 		snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "RPCAP error: %s", pcap_geterr(session->fp));
2410*8b26181fSAndroid Build Coastguard Worker 		return -2;
2411*8b26181fSAndroid Build Coastguard Worker 	}
2412*8b26181fSAndroid Build Coastguard Worker 
2413*8b26181fSAndroid Build Coastguard Worker 	return 0;
2414*8b26181fSAndroid Build Coastguard Worker }
2415*8b26181fSAndroid Build Coastguard Worker 
2416*8b26181fSAndroid Build Coastguard Worker static int
daemon_msg_updatefilter_req(uint8 ver,struct daemon_slpars * pars,struct session * session,uint32 plen)2417*8b26181fSAndroid Build Coastguard Worker daemon_msg_updatefilter_req(uint8 ver, struct daemon_slpars *pars,
2418*8b26181fSAndroid Build Coastguard Worker     struct session *session, uint32 plen)
2419*8b26181fSAndroid Build Coastguard Worker {
2420*8b26181fSAndroid Build Coastguard Worker 	char errbuf[PCAP_ERRBUF_SIZE];
2421*8b26181fSAndroid Build Coastguard Worker 	char errmsgbuf[PCAP_ERRBUF_SIZE];	// buffer for errors to send to the client
2422*8b26181fSAndroid Build Coastguard Worker 	int ret;				// status of daemon_unpackapplyfilter()
2423*8b26181fSAndroid Build Coastguard Worker 	struct rpcap_header header;		// keeps the answer to the updatefilter command
2424*8b26181fSAndroid Build Coastguard Worker 
2425*8b26181fSAndroid Build Coastguard Worker 	ret = daemon_unpackapplyfilter(pars->sockctrl, pars->ssl, session, &plen, errmsgbuf);
2426*8b26181fSAndroid Build Coastguard Worker 	if (ret == -1)
2427*8b26181fSAndroid Build Coastguard Worker 	{
2428*8b26181fSAndroid Build Coastguard Worker 		// Fatal error.  A message has been logged; just give up.
2429*8b26181fSAndroid Build Coastguard Worker 		return -1;
2430*8b26181fSAndroid Build Coastguard Worker 	}
2431*8b26181fSAndroid Build Coastguard Worker 	if (ret == -2)
2432*8b26181fSAndroid Build Coastguard Worker 	{
2433*8b26181fSAndroid Build Coastguard Worker 		// Non-fatal error.  Send an error reply to the client.
2434*8b26181fSAndroid Build Coastguard Worker 		goto error;
2435*8b26181fSAndroid Build Coastguard Worker 	}
2436*8b26181fSAndroid Build Coastguard Worker 
2437*8b26181fSAndroid Build Coastguard Worker 	// Check if all the data has been read; if not, discard the data in excess
2438*8b26181fSAndroid Build Coastguard Worker 	if (rpcapd_discard(pars->sockctrl, pars->ssl, plen) == -1)
2439*8b26181fSAndroid Build Coastguard Worker 	{
2440*8b26181fSAndroid Build Coastguard Worker 		// Network error.
2441*8b26181fSAndroid Build Coastguard Worker 		return -1;
2442*8b26181fSAndroid Build Coastguard Worker 	}
2443*8b26181fSAndroid Build Coastguard Worker 
2444*8b26181fSAndroid Build Coastguard Worker 	// A response is needed, otherwise the other host does not know that everything went well
2445*8b26181fSAndroid Build Coastguard Worker 	rpcap_createhdr(&header, ver, RPCAP_MSG_UPDATEFILTER_REPLY, 0, 0);
2446*8b26181fSAndroid Build Coastguard Worker 
2447*8b26181fSAndroid Build Coastguard Worker 	if (sock_send(pars->sockctrl, pars->ssl, (char *) &header, sizeof (struct rpcap_header), errbuf, PCAP_ERRBUF_SIZE))
2448*8b26181fSAndroid Build Coastguard Worker 	{
2449*8b26181fSAndroid Build Coastguard Worker 		// That failed; log a message and give up.
2450*8b26181fSAndroid Build Coastguard Worker 		rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
2451*8b26181fSAndroid Build Coastguard Worker 		return -1;
2452*8b26181fSAndroid Build Coastguard Worker 	}
2453*8b26181fSAndroid Build Coastguard Worker 
2454*8b26181fSAndroid Build Coastguard Worker 	return 0;
2455*8b26181fSAndroid Build Coastguard Worker 
2456*8b26181fSAndroid Build Coastguard Worker error:
2457*8b26181fSAndroid Build Coastguard Worker 	if (rpcapd_discard(pars->sockctrl, pars->ssl, plen) == -1)
2458*8b26181fSAndroid Build Coastguard Worker 	{
2459*8b26181fSAndroid Build Coastguard Worker 		return -1;
2460*8b26181fSAndroid Build Coastguard Worker 	}
2461*8b26181fSAndroid Build Coastguard Worker 	rpcap_senderror(pars->sockctrl, pars->ssl, ver, PCAP_ERR_UPDATEFILTER,
2462*8b26181fSAndroid Build Coastguard Worker 	    errmsgbuf, NULL);
2463*8b26181fSAndroid Build Coastguard Worker 
2464*8b26181fSAndroid Build Coastguard Worker 	return 0;
2465*8b26181fSAndroid Build Coastguard Worker }
2466*8b26181fSAndroid Build Coastguard Worker 
2467*8b26181fSAndroid Build Coastguard Worker /*!
2468*8b26181fSAndroid Build Coastguard Worker 	\brief Received the sampling parameters from remote host and it stores in the pcap_t structure.
2469*8b26181fSAndroid Build Coastguard Worker */
2470*8b26181fSAndroid Build Coastguard Worker static int
daemon_msg_setsampling_req(uint8 ver,struct daemon_slpars * pars,uint32 plen,struct rpcap_sampling * samp_param)2471*8b26181fSAndroid Build Coastguard Worker daemon_msg_setsampling_req(uint8 ver, struct daemon_slpars *pars, uint32 plen,
2472*8b26181fSAndroid Build Coastguard Worker     struct rpcap_sampling *samp_param)
2473*8b26181fSAndroid Build Coastguard Worker {
2474*8b26181fSAndroid Build Coastguard Worker 	char errbuf[PCAP_ERRBUF_SIZE];		// buffer for network errors
2475*8b26181fSAndroid Build Coastguard Worker 	char errmsgbuf[PCAP_ERRBUF_SIZE];
2476*8b26181fSAndroid Build Coastguard Worker 	struct rpcap_header header;
2477*8b26181fSAndroid Build Coastguard Worker 	struct rpcap_sampling rpcap_samp;
2478*8b26181fSAndroid Build Coastguard Worker 	int status;
2479*8b26181fSAndroid Build Coastguard Worker 
2480*8b26181fSAndroid Build Coastguard Worker 	status = rpcapd_recv(pars->sockctrl, pars->ssl, (char *) &rpcap_samp, sizeof(struct rpcap_sampling), &plen, errmsgbuf);
2481*8b26181fSAndroid Build Coastguard Worker 	if (status == -1)
2482*8b26181fSAndroid Build Coastguard Worker 	{
2483*8b26181fSAndroid Build Coastguard Worker 		return -1;
2484*8b26181fSAndroid Build Coastguard Worker 	}
2485*8b26181fSAndroid Build Coastguard Worker 	if (status == -2)
2486*8b26181fSAndroid Build Coastguard Worker 	{
2487*8b26181fSAndroid Build Coastguard Worker 		goto error;
2488*8b26181fSAndroid Build Coastguard Worker 	}
2489*8b26181fSAndroid Build Coastguard Worker 
2490*8b26181fSAndroid Build Coastguard Worker 	// Save these settings in the pcap_t
2491*8b26181fSAndroid Build Coastguard Worker 	samp_param->method = rpcap_samp.method;
2492*8b26181fSAndroid Build Coastguard Worker 	samp_param->value = ntohl(rpcap_samp.value);
2493*8b26181fSAndroid Build Coastguard Worker 
2494*8b26181fSAndroid Build Coastguard Worker 	// A response is needed, otherwise the other host does not know that everything went well
2495*8b26181fSAndroid Build Coastguard Worker 	rpcap_createhdr(&header, ver, RPCAP_MSG_SETSAMPLING_REPLY, 0, 0);
2496*8b26181fSAndroid Build Coastguard Worker 
2497*8b26181fSAndroid Build Coastguard Worker 	if (sock_send(pars->sockctrl, pars->ssl, (char *) &header, sizeof (struct rpcap_header), errbuf, PCAP_ERRBUF_SIZE) == -1)
2498*8b26181fSAndroid Build Coastguard Worker 	{
2499*8b26181fSAndroid Build Coastguard Worker 		// That failed; log a message and give up.
2500*8b26181fSAndroid Build Coastguard Worker 		rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
2501*8b26181fSAndroid Build Coastguard Worker 		return -1;
2502*8b26181fSAndroid Build Coastguard Worker 	}
2503*8b26181fSAndroid Build Coastguard Worker 
2504*8b26181fSAndroid Build Coastguard Worker 	if (rpcapd_discard(pars->sockctrl, pars->ssl, plen) == -1)
2505*8b26181fSAndroid Build Coastguard Worker 	{
2506*8b26181fSAndroid Build Coastguard Worker 		return -1;
2507*8b26181fSAndroid Build Coastguard Worker 	}
2508*8b26181fSAndroid Build Coastguard Worker 
2509*8b26181fSAndroid Build Coastguard Worker 	return 0;
2510*8b26181fSAndroid Build Coastguard Worker 
2511*8b26181fSAndroid Build Coastguard Worker error:
2512*8b26181fSAndroid Build Coastguard Worker 	if (rpcap_senderror(pars->sockctrl, pars->ssl, ver, PCAP_ERR_SETSAMPLING,
2513*8b26181fSAndroid Build Coastguard Worker 	    errmsgbuf, errbuf) == -1)
2514*8b26181fSAndroid Build Coastguard Worker 	{
2515*8b26181fSAndroid Build Coastguard Worker 		// That failed; log a message and give up.
2516*8b26181fSAndroid Build Coastguard Worker 		rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
2517*8b26181fSAndroid Build Coastguard Worker 		return -1;
2518*8b26181fSAndroid Build Coastguard Worker 	}
2519*8b26181fSAndroid Build Coastguard Worker 
2520*8b26181fSAndroid Build Coastguard Worker 	// Check if all the data has been read; if not, discard the data in excess
2521*8b26181fSAndroid Build Coastguard Worker 	if (rpcapd_discard(pars->sockctrl, pars->ssl, plen) == -1)
2522*8b26181fSAndroid Build Coastguard Worker 	{
2523*8b26181fSAndroid Build Coastguard Worker 		return -1;
2524*8b26181fSAndroid Build Coastguard Worker 	}
2525*8b26181fSAndroid Build Coastguard Worker 
2526*8b26181fSAndroid Build Coastguard Worker 	return 0;
2527*8b26181fSAndroid Build Coastguard Worker }
2528*8b26181fSAndroid Build Coastguard Worker 
2529*8b26181fSAndroid Build Coastguard Worker static int
daemon_msg_stats_req(uint8 ver,struct daemon_slpars * pars,struct session * session,uint32 plen,struct pcap_stat * stats,unsigned int svrcapt)2530*8b26181fSAndroid Build Coastguard Worker daemon_msg_stats_req(uint8 ver, struct daemon_slpars *pars,
2531*8b26181fSAndroid Build Coastguard Worker     struct session *session, uint32 plen, struct pcap_stat *stats,
2532*8b26181fSAndroid Build Coastguard Worker     unsigned int svrcapt)
2533*8b26181fSAndroid Build Coastguard Worker {
2534*8b26181fSAndroid Build Coastguard Worker 	char errbuf[PCAP_ERRBUF_SIZE];		// buffer for network errors
2535*8b26181fSAndroid Build Coastguard Worker 	char errmsgbuf[PCAP_ERRBUF_SIZE];	// buffer for errors to send to the client
2536*8b26181fSAndroid Build Coastguard Worker 	char sendbuf[RPCAP_NETBUF_SIZE];	// temporary buffer in which data to be sent is buffered
2537*8b26181fSAndroid Build Coastguard Worker 	int sendbufidx = 0;			// index which keeps the number of bytes currently buffered
2538*8b26181fSAndroid Build Coastguard Worker 	struct rpcap_stats *netstats;		// statistics sent on the network
2539*8b26181fSAndroid Build Coastguard Worker 
2540*8b26181fSAndroid Build Coastguard Worker 	// Checks that the header does not contain other data; if so, discard it
2541*8b26181fSAndroid Build Coastguard Worker 	if (rpcapd_discard(pars->sockctrl, pars->ssl, plen) == -1)
2542*8b26181fSAndroid Build Coastguard Worker 	{
2543*8b26181fSAndroid Build Coastguard Worker 		// Network error.
2544*8b26181fSAndroid Build Coastguard Worker 		return -1;
2545*8b26181fSAndroid Build Coastguard Worker 	}
2546*8b26181fSAndroid Build Coastguard Worker 
2547*8b26181fSAndroid Build Coastguard Worker 	if (sock_bufferize(NULL, sizeof(struct rpcap_header), NULL,
2548*8b26181fSAndroid Build Coastguard Worker 	    &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
2549*8b26181fSAndroid Build Coastguard Worker 		goto error;
2550*8b26181fSAndroid Build Coastguard Worker 
2551*8b26181fSAndroid Build Coastguard Worker 	rpcap_createhdr((struct rpcap_header *) sendbuf, ver,
2552*8b26181fSAndroid Build Coastguard Worker 	    RPCAP_MSG_STATS_REPLY, 0, (uint16) sizeof(struct rpcap_stats));
2553*8b26181fSAndroid Build Coastguard Worker 
2554*8b26181fSAndroid Build Coastguard Worker 	netstats = (struct rpcap_stats *) &sendbuf[sendbufidx];
2555*8b26181fSAndroid Build Coastguard Worker 
2556*8b26181fSAndroid Build Coastguard Worker 	if (sock_bufferize(NULL, sizeof(struct rpcap_stats), NULL,
2557*8b26181fSAndroid Build Coastguard Worker 	    &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
2558*8b26181fSAndroid Build Coastguard Worker 		goto error;
2559*8b26181fSAndroid Build Coastguard Worker 
2560*8b26181fSAndroid Build Coastguard Worker 	if (session && session->fp)
2561*8b26181fSAndroid Build Coastguard Worker 	{
2562*8b26181fSAndroid Build Coastguard Worker 		if (pcap_stats(session->fp, stats) == -1)
2563*8b26181fSAndroid Build Coastguard Worker 		{
2564*8b26181fSAndroid Build Coastguard Worker 			snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "%s", pcap_geterr(session->fp));
2565*8b26181fSAndroid Build Coastguard Worker 			goto error;
2566*8b26181fSAndroid Build Coastguard Worker 		}
2567*8b26181fSAndroid Build Coastguard Worker 
2568*8b26181fSAndroid Build Coastguard Worker 		netstats->ifdrop = htonl(stats->ps_ifdrop);
2569*8b26181fSAndroid Build Coastguard Worker 		netstats->ifrecv = htonl(stats->ps_recv);
2570*8b26181fSAndroid Build Coastguard Worker 		netstats->krnldrop = htonl(stats->ps_drop);
2571*8b26181fSAndroid Build Coastguard Worker 		netstats->svrcapt = htonl(session->TotCapt);
2572*8b26181fSAndroid Build Coastguard Worker 	}
2573*8b26181fSAndroid Build Coastguard Worker 	else
2574*8b26181fSAndroid Build Coastguard Worker 	{
2575*8b26181fSAndroid Build Coastguard Worker 		// We have to keep compatibility with old applications,
2576*8b26181fSAndroid Build Coastguard Worker 		// which ask for statistics also when the capture has
2577*8b26181fSAndroid Build Coastguard Worker 		// already stopped.
2578*8b26181fSAndroid Build Coastguard Worker 		netstats->ifdrop = htonl(stats->ps_ifdrop);
2579*8b26181fSAndroid Build Coastguard Worker 		netstats->ifrecv = htonl(stats->ps_recv);
2580*8b26181fSAndroid Build Coastguard Worker 		netstats->krnldrop = htonl(stats->ps_drop);
2581*8b26181fSAndroid Build Coastguard Worker 		netstats->svrcapt = htonl(svrcapt);
2582*8b26181fSAndroid Build Coastguard Worker 	}
2583*8b26181fSAndroid Build Coastguard Worker 
2584*8b26181fSAndroid Build Coastguard Worker 	// Send the packet
2585*8b26181fSAndroid Build Coastguard Worker 	if (sock_send(pars->sockctrl, pars->ssl, sendbuf, sendbufidx, errbuf, PCAP_ERRBUF_SIZE) == -1)
2586*8b26181fSAndroid Build Coastguard Worker 	{
2587*8b26181fSAndroid Build Coastguard Worker 		rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
2588*8b26181fSAndroid Build Coastguard Worker 		return -1;
2589*8b26181fSAndroid Build Coastguard Worker 	}
2590*8b26181fSAndroid Build Coastguard Worker 
2591*8b26181fSAndroid Build Coastguard Worker 	return 0;
2592*8b26181fSAndroid Build Coastguard Worker 
2593*8b26181fSAndroid Build Coastguard Worker error:
2594*8b26181fSAndroid Build Coastguard Worker 	rpcap_senderror(pars->sockctrl, pars->ssl, ver, PCAP_ERR_GETSTATS,
2595*8b26181fSAndroid Build Coastguard Worker 	    errmsgbuf, NULL);
2596*8b26181fSAndroid Build Coastguard Worker 	return 0;
2597*8b26181fSAndroid Build Coastguard Worker }
2598*8b26181fSAndroid Build Coastguard Worker 
2599*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
2600*8b26181fSAndroid Build Coastguard Worker static unsigned __stdcall
2601*8b26181fSAndroid Build Coastguard Worker #else
2602*8b26181fSAndroid Build Coastguard Worker static void *
2603*8b26181fSAndroid Build Coastguard Worker #endif
daemon_thrdatamain(void * ptr)2604*8b26181fSAndroid Build Coastguard Worker daemon_thrdatamain(void *ptr)
2605*8b26181fSAndroid Build Coastguard Worker {
2606*8b26181fSAndroid Build Coastguard Worker 	char errbuf[PCAP_ERRBUF_SIZE + 1];	// error buffer
2607*8b26181fSAndroid Build Coastguard Worker 	struct session *session;		// pointer to the struct session for this session
2608*8b26181fSAndroid Build Coastguard Worker 	int retval;							// general variable used to keep the return value of other functions
2609*8b26181fSAndroid Build Coastguard Worker 	struct rpcap_pkthdr *net_pkt_header;// header of the packet
2610*8b26181fSAndroid Build Coastguard Worker 	struct pcap_pkthdr *pkt_header;		// pointer to the buffer that contains the header of the current packet
2611*8b26181fSAndroid Build Coastguard Worker 	u_char *pkt_data;					// pointer to the buffer that contains the current packet
2612*8b26181fSAndroid Build Coastguard Worker 	size_t sendbufsize;			// size for the send buffer
2613*8b26181fSAndroid Build Coastguard Worker 	char *sendbuf;						// temporary buffer in which data to be sent is buffered
2614*8b26181fSAndroid Build Coastguard Worker 	int sendbufidx;						// index which keeps the number of bytes currently buffered
2615*8b26181fSAndroid Build Coastguard Worker 	int status;
2616*8b26181fSAndroid Build Coastguard Worker #ifndef _WIN32
2617*8b26181fSAndroid Build Coastguard Worker 	sigset_t sigusr1;			// signal set with just SIGUSR1
2618*8b26181fSAndroid Build Coastguard Worker #endif
2619*8b26181fSAndroid Build Coastguard Worker 
2620*8b26181fSAndroid Build Coastguard Worker 	session = (struct session *) ptr;
2621*8b26181fSAndroid Build Coastguard Worker 
2622*8b26181fSAndroid Build Coastguard Worker 	session->TotCapt = 0;			// counter which is incremented each time a packet is received
2623*8b26181fSAndroid Build Coastguard Worker 
2624*8b26181fSAndroid Build Coastguard Worker 	// Initialize errbuf
2625*8b26181fSAndroid Build Coastguard Worker 	memset(errbuf, 0, sizeof(errbuf));
2626*8b26181fSAndroid Build Coastguard Worker 
2627*8b26181fSAndroid Build Coastguard Worker 	//
2628*8b26181fSAndroid Build Coastguard Worker 	// We need a buffer large enough to hold a buffer large enough
2629*8b26181fSAndroid Build Coastguard Worker 	// for a maximum-size packet for this pcap_t.
2630*8b26181fSAndroid Build Coastguard Worker 	//
2631*8b26181fSAndroid Build Coastguard Worker 	if (pcap_snapshot(session->fp) < 0)
2632*8b26181fSAndroid Build Coastguard Worker 	{
2633*8b26181fSAndroid Build Coastguard Worker 		//
2634*8b26181fSAndroid Build Coastguard Worker 		// The snapshot length is negative.
2635*8b26181fSAndroid Build Coastguard Worker 		// This "should not happen".
2636*8b26181fSAndroid Build Coastguard Worker 		//
2637*8b26181fSAndroid Build Coastguard Worker 		rpcapd_log(LOGPRIO_ERROR,
2638*8b26181fSAndroid Build Coastguard Worker 		    "Unable to allocate the buffer for this child thread: snapshot length of %d is negative",
2639*8b26181fSAndroid Build Coastguard Worker 		        pcap_snapshot(session->fp));
2640*8b26181fSAndroid Build Coastguard Worker 		sendbuf = NULL;	// we can't allocate a buffer, so nothing to free
2641*8b26181fSAndroid Build Coastguard Worker 		goto error;
2642*8b26181fSAndroid Build Coastguard Worker 	}
2643*8b26181fSAndroid Build Coastguard Worker 	//
2644*8b26181fSAndroid Build Coastguard Worker 	// size_t is unsigned, and the result of pcap_snapshot() is signed;
2645*8b26181fSAndroid Build Coastguard Worker 	// on no platform that we support is int larger than size_t.
2646*8b26181fSAndroid Build Coastguard Worker 	// This means that, unless the extra information we prepend to
2647*8b26181fSAndroid Build Coastguard Worker 	// a maximum-sized packet is impossibly large, the sum of the
2648*8b26181fSAndroid Build Coastguard Worker 	// snapshot length and the size of that extra information will
2649*8b26181fSAndroid Build Coastguard Worker 	// fit in a size_t.
2650*8b26181fSAndroid Build Coastguard Worker 	//
2651*8b26181fSAndroid Build Coastguard Worker 	// So we don't need to make sure that sendbufsize will overflow.
2652*8b26181fSAndroid Build Coastguard Worker 	//
2653*8b26181fSAndroid Build Coastguard Worker 	// However, we *do* need to make sure its value fits in an int,
2654*8b26181fSAndroid Build Coastguard Worker 	// because sock_send() can't send more than INT_MAX bytes (it could
2655*8b26181fSAndroid Build Coastguard Worker 	// do so on 64-bit UN*Xes, but can't do so on Windows, not even
2656*8b26181fSAndroid Build Coastguard Worker 	// 64-bit Windows, as the send() buffer size argument is an int
2657*8b26181fSAndroid Build Coastguard Worker 	// in Winsock).
2658*8b26181fSAndroid Build Coastguard Worker 	//
2659*8b26181fSAndroid Build Coastguard Worker 	sendbufsize = sizeof(struct rpcap_header) + sizeof(struct rpcap_pkthdr) + pcap_snapshot(session->fp);
2660*8b26181fSAndroid Build Coastguard Worker 	if (sendbufsize > INT_MAX)
2661*8b26181fSAndroid Build Coastguard Worker 	{
2662*8b26181fSAndroid Build Coastguard Worker 		rpcapd_log(LOGPRIO_ERROR,
2663*8b26181fSAndroid Build Coastguard Worker 		    "Buffer size for this child thread would be larger than %d",
2664*8b26181fSAndroid Build Coastguard Worker 		    INT_MAX);
2665*8b26181fSAndroid Build Coastguard Worker 		sendbuf = NULL;	// we haven't allocated a buffer, so nothing to free
2666*8b26181fSAndroid Build Coastguard Worker 		goto error;
2667*8b26181fSAndroid Build Coastguard Worker 	}
2668*8b26181fSAndroid Build Coastguard Worker 	sendbuf = (char *) malloc (sendbufsize);
2669*8b26181fSAndroid Build Coastguard Worker 	if (sendbuf == NULL)
2670*8b26181fSAndroid Build Coastguard Worker 	{
2671*8b26181fSAndroid Build Coastguard Worker 		rpcapd_log(LOGPRIO_ERROR,
2672*8b26181fSAndroid Build Coastguard Worker 		    "Unable to allocate the buffer for this child thread");
2673*8b26181fSAndroid Build Coastguard Worker 		goto error;
2674*8b26181fSAndroid Build Coastguard Worker 	}
2675*8b26181fSAndroid Build Coastguard Worker 
2676*8b26181fSAndroid Build Coastguard Worker #ifndef _WIN32
2677*8b26181fSAndroid Build Coastguard Worker 	//
2678*8b26181fSAndroid Build Coastguard Worker 	// Set the signal set to include just SIGUSR1, and block that
2679*8b26181fSAndroid Build Coastguard Worker 	// signal; we only want it unblocked when we're reading
2680*8b26181fSAndroid Build Coastguard Worker 	// packets - we dn't want any other system calls, such as
2681*8b26181fSAndroid Build Coastguard Worker 	// ones being used to send to the client or to log messages,
2682*8b26181fSAndroid Build Coastguard Worker 	// to be interrupted.
2683*8b26181fSAndroid Build Coastguard Worker 	//
2684*8b26181fSAndroid Build Coastguard Worker 	sigemptyset(&sigusr1);
2685*8b26181fSAndroid Build Coastguard Worker 	sigaddset(&sigusr1, SIGUSR1);
2686*8b26181fSAndroid Build Coastguard Worker 	pthread_sigmask(SIG_BLOCK, &sigusr1, NULL);
2687*8b26181fSAndroid Build Coastguard Worker #endif
2688*8b26181fSAndroid Build Coastguard Worker 
2689*8b26181fSAndroid Build Coastguard Worker 	// Retrieve the packets
2690*8b26181fSAndroid Build Coastguard Worker 	for (;;)
2691*8b26181fSAndroid Build Coastguard Worker 	{
2692*8b26181fSAndroid Build Coastguard Worker #ifndef _WIN32
2693*8b26181fSAndroid Build Coastguard Worker 		//
2694*8b26181fSAndroid Build Coastguard Worker 		// Unblock SIGUSR1 while we might be waiting for packets.
2695*8b26181fSAndroid Build Coastguard Worker 		//
2696*8b26181fSAndroid Build Coastguard Worker 		pthread_sigmask(SIG_UNBLOCK, &sigusr1, NULL);
2697*8b26181fSAndroid Build Coastguard Worker #endif
2698*8b26181fSAndroid Build Coastguard Worker 		retval = pcap_next_ex(session->fp, &pkt_header, (const u_char **) &pkt_data);	// cast to avoid a compiler warning
2699*8b26181fSAndroid Build Coastguard Worker #ifndef _WIN32
2700*8b26181fSAndroid Build Coastguard Worker 		//
2701*8b26181fSAndroid Build Coastguard Worker 		// Now block it again.
2702*8b26181fSAndroid Build Coastguard Worker 		//
2703*8b26181fSAndroid Build Coastguard Worker 		pthread_sigmask(SIG_BLOCK, &sigusr1, NULL);
2704*8b26181fSAndroid Build Coastguard Worker #endif
2705*8b26181fSAndroid Build Coastguard Worker 		if (retval < 0)
2706*8b26181fSAndroid Build Coastguard Worker 			break;		// error
2707*8b26181fSAndroid Build Coastguard Worker 		if (retval == 0)	// Read timeout elapsed
2708*8b26181fSAndroid Build Coastguard Worker 			continue;
2709*8b26181fSAndroid Build Coastguard Worker 
2710*8b26181fSAndroid Build Coastguard Worker 		sendbufidx = 0;
2711*8b26181fSAndroid Build Coastguard Worker 
2712*8b26181fSAndroid Build Coastguard Worker 		// Bufferize the general header
2713*8b26181fSAndroid Build Coastguard Worker 		if (sock_bufferize(NULL, sizeof(struct rpcap_header), NULL,
2714*8b26181fSAndroid Build Coastguard Worker 		    &sendbufidx, (int)sendbufsize, SOCKBUF_CHECKONLY, errbuf,
2715*8b26181fSAndroid Build Coastguard Worker 		    PCAP_ERRBUF_SIZE) == -1)
2716*8b26181fSAndroid Build Coastguard Worker 		{
2717*8b26181fSAndroid Build Coastguard Worker 			rpcapd_log(LOGPRIO_ERROR,
2718*8b26181fSAndroid Build Coastguard Worker 			    "sock_bufferize() error sending packet message: %s",
2719*8b26181fSAndroid Build Coastguard Worker 			    errbuf);
2720*8b26181fSAndroid Build Coastguard Worker 			goto error;
2721*8b26181fSAndroid Build Coastguard Worker 		}
2722*8b26181fSAndroid Build Coastguard Worker 
2723*8b26181fSAndroid Build Coastguard Worker 		rpcap_createhdr((struct rpcap_header *) sendbuf,
2724*8b26181fSAndroid Build Coastguard Worker 		    session->protocol_version, RPCAP_MSG_PACKET, 0,
2725*8b26181fSAndroid Build Coastguard Worker 		    (uint16) (sizeof(struct rpcap_pkthdr) + pkt_header->caplen));
2726*8b26181fSAndroid Build Coastguard Worker 
2727*8b26181fSAndroid Build Coastguard Worker 		net_pkt_header = (struct rpcap_pkthdr *) &sendbuf[sendbufidx];
2728*8b26181fSAndroid Build Coastguard Worker 
2729*8b26181fSAndroid Build Coastguard Worker 		// Bufferize the pkt header
2730*8b26181fSAndroid Build Coastguard Worker 		if (sock_bufferize(NULL, sizeof(struct rpcap_pkthdr), NULL,
2731*8b26181fSAndroid Build Coastguard Worker 		    &sendbufidx, (int)sendbufsize, SOCKBUF_CHECKONLY, errbuf,
2732*8b26181fSAndroid Build Coastguard Worker 		    PCAP_ERRBUF_SIZE) == -1)
2733*8b26181fSAndroid Build Coastguard Worker 		{
2734*8b26181fSAndroid Build Coastguard Worker 			rpcapd_log(LOGPRIO_ERROR,
2735*8b26181fSAndroid Build Coastguard Worker 			    "sock_bufferize() error sending packet message: %s",
2736*8b26181fSAndroid Build Coastguard Worker 			    errbuf);
2737*8b26181fSAndroid Build Coastguard Worker 			goto error;
2738*8b26181fSAndroid Build Coastguard Worker 		}
2739*8b26181fSAndroid Build Coastguard Worker 
2740*8b26181fSAndroid Build Coastguard Worker 		net_pkt_header->caplen = htonl(pkt_header->caplen);
2741*8b26181fSAndroid Build Coastguard Worker 		net_pkt_header->len = htonl(pkt_header->len);
2742*8b26181fSAndroid Build Coastguard Worker 		net_pkt_header->npkt = htonl(++(session->TotCapt));
2743*8b26181fSAndroid Build Coastguard Worker 		//
2744*8b26181fSAndroid Build Coastguard Worker 		// This protocol needs to be updated with a new version
2745*8b26181fSAndroid Build Coastguard Worker 		// before 2038-01-19 03:14:07 UTC.
2746*8b26181fSAndroid Build Coastguard Worker 		//
2747*8b26181fSAndroid Build Coastguard Worker 		net_pkt_header->timestamp_sec = htonl((uint32)pkt_header->ts.tv_sec);
2748*8b26181fSAndroid Build Coastguard Worker 		net_pkt_header->timestamp_usec = htonl((uint32)pkt_header->ts.tv_usec);
2749*8b26181fSAndroid Build Coastguard Worker 
2750*8b26181fSAndroid Build Coastguard Worker 		// Bufferize the pkt data
2751*8b26181fSAndroid Build Coastguard Worker 		if (sock_bufferize((char *) pkt_data, pkt_header->caplen,
2752*8b26181fSAndroid Build Coastguard Worker 		    sendbuf, &sendbufidx, (int)sendbufsize, SOCKBUF_BUFFERIZE,
2753*8b26181fSAndroid Build Coastguard Worker 		    errbuf, PCAP_ERRBUF_SIZE) == -1)
2754*8b26181fSAndroid Build Coastguard Worker 		{
2755*8b26181fSAndroid Build Coastguard Worker 			rpcapd_log(LOGPRIO_ERROR,
2756*8b26181fSAndroid Build Coastguard Worker 			    "sock_bufferize() error sending packet message: %s",
2757*8b26181fSAndroid Build Coastguard Worker 			    errbuf);
2758*8b26181fSAndroid Build Coastguard Worker 			goto error;
2759*8b26181fSAndroid Build Coastguard Worker 		}
2760*8b26181fSAndroid Build Coastguard Worker 
2761*8b26181fSAndroid Build Coastguard Worker 		// Send the packet
2762*8b26181fSAndroid Build Coastguard Worker 		// If the client dropped the connection, don't report an
2763*8b26181fSAndroid Build Coastguard Worker 		// error, just quit.
2764*8b26181fSAndroid Build Coastguard Worker 		status = sock_send(session->sockdata, session->data_ssl, sendbuf, sendbufidx, errbuf, PCAP_ERRBUF_SIZE);
2765*8b26181fSAndroid Build Coastguard Worker 		if (status < 0)
2766*8b26181fSAndroid Build Coastguard Worker 		{
2767*8b26181fSAndroid Build Coastguard Worker 			if (status == -1)
2768*8b26181fSAndroid Build Coastguard Worker 			{
2769*8b26181fSAndroid Build Coastguard Worker 				//
2770*8b26181fSAndroid Build Coastguard Worker 				// Error other than "client closed the
2771*8b26181fSAndroid Build Coastguard Worker 				// connection out from under us"; report
2772*8b26181fSAndroid Build Coastguard Worker 				// it.
2773*8b26181fSAndroid Build Coastguard Worker 				//
2774*8b26181fSAndroid Build Coastguard Worker 				rpcapd_log(LOGPRIO_ERROR,
2775*8b26181fSAndroid Build Coastguard Worker 				    "Send of packet to client failed: %s",
2776*8b26181fSAndroid Build Coastguard Worker 				    errbuf);
2777*8b26181fSAndroid Build Coastguard Worker 			}
2778*8b26181fSAndroid Build Coastguard Worker 
2779*8b26181fSAndroid Build Coastguard Worker 			//
2780*8b26181fSAndroid Build Coastguard Worker 			// Give up in either case.
2781*8b26181fSAndroid Build Coastguard Worker 			//
2782*8b26181fSAndroid Build Coastguard Worker 			goto error;
2783*8b26181fSAndroid Build Coastguard Worker 		}
2784*8b26181fSAndroid Build Coastguard Worker 	}
2785*8b26181fSAndroid Build Coastguard Worker 
2786*8b26181fSAndroid Build Coastguard Worker 	if (retval < 0 && retval != PCAP_ERROR_BREAK)
2787*8b26181fSAndroid Build Coastguard Worker 	{
2788*8b26181fSAndroid Build Coastguard Worker 		//
2789*8b26181fSAndroid Build Coastguard Worker 		// Failed with an error other than "we were told to break
2790*8b26181fSAndroid Build Coastguard Worker 		// out of the loop".
2791*8b26181fSAndroid Build Coastguard Worker 		//
2792*8b26181fSAndroid Build Coastguard Worker 		// The latter just means that the client told us to stop
2793*8b26181fSAndroid Build Coastguard Worker 		// capturing, so there's no error to report.
2794*8b26181fSAndroid Build Coastguard Worker 		//
2795*8b26181fSAndroid Build Coastguard Worker 		snprintf(errbuf, PCAP_ERRBUF_SIZE, "Error reading the packets: %s", pcap_geterr(session->fp));
2796*8b26181fSAndroid Build Coastguard Worker 		rpcap_senderror(session->sockctrl, session->ctrl_ssl, session->protocol_version,
2797*8b26181fSAndroid Build Coastguard Worker 		    PCAP_ERR_READEX, errbuf, NULL);
2798*8b26181fSAndroid Build Coastguard Worker 	}
2799*8b26181fSAndroid Build Coastguard Worker 
2800*8b26181fSAndroid Build Coastguard Worker error:
2801*8b26181fSAndroid Build Coastguard Worker 	//
2802*8b26181fSAndroid Build Coastguard Worker 	// The main thread will clean up the session structure.
2803*8b26181fSAndroid Build Coastguard Worker 	//
2804*8b26181fSAndroid Build Coastguard Worker 	free(sendbuf);
2805*8b26181fSAndroid Build Coastguard Worker 
2806*8b26181fSAndroid Build Coastguard Worker 	return 0;
2807*8b26181fSAndroid Build Coastguard Worker }
2808*8b26181fSAndroid Build Coastguard Worker 
2809*8b26181fSAndroid Build Coastguard Worker #ifndef _WIN32
2810*8b26181fSAndroid Build Coastguard Worker //
2811*8b26181fSAndroid Build Coastguard Worker // Do-nothing handler for SIGUSR1; the sole purpose of SIGUSR1 is to
2812*8b26181fSAndroid Build Coastguard Worker // interrupt the data thread if it's blocked in a system call waiting
2813*8b26181fSAndroid Build Coastguard Worker // for packets to arrive.
2814*8b26181fSAndroid Build Coastguard Worker //
noop_handler(int sign _U_)2815*8b26181fSAndroid Build Coastguard Worker static void noop_handler(int sign _U_)
2816*8b26181fSAndroid Build Coastguard Worker {
2817*8b26181fSAndroid Build Coastguard Worker }
2818*8b26181fSAndroid Build Coastguard Worker #endif
2819*8b26181fSAndroid Build Coastguard Worker 
2820*8b26181fSAndroid Build Coastguard Worker /*!
2821*8b26181fSAndroid Build Coastguard Worker 	\brief It serializes a network address.
2822*8b26181fSAndroid Build Coastguard Worker 
2823*8b26181fSAndroid Build Coastguard Worker 	It accepts a 'sockaddr_storage' structure as input, and it converts it appropriately into a format
2824*8b26181fSAndroid Build Coastguard Worker 	that can be used to be sent on the network. Basically, it applies all the hton()
2825*8b26181fSAndroid Build Coastguard Worker 	conversion required to the input variable.
2826*8b26181fSAndroid Build Coastguard Worker 
2827*8b26181fSAndroid Build Coastguard Worker 	\param sockaddrin a 'sockaddr_storage' pointer to the variable that has to be
2828*8b26181fSAndroid Build Coastguard Worker 	serialized. This variable can be both a 'sockaddr_in' and 'sockaddr_in6'.
2829*8b26181fSAndroid Build Coastguard Worker 
2830*8b26181fSAndroid Build Coastguard Worker 	\param sockaddrout an 'rpcap_sockaddr' pointer to the variable that will contain
2831*8b26181fSAndroid Build Coastguard Worker 	the serialized data. This variable has to be allocated by the user.
2832*8b26181fSAndroid Build Coastguard Worker 
2833*8b26181fSAndroid Build Coastguard Worker 	\warning This function supports only AF_INET and AF_INET6 address families.
2834*8b26181fSAndroid Build Coastguard Worker */
2835*8b26181fSAndroid Build Coastguard Worker static void
daemon_seraddr(struct sockaddr_storage * sockaddrin,struct rpcap_sockaddr * sockaddrout)2836*8b26181fSAndroid Build Coastguard Worker daemon_seraddr(struct sockaddr_storage *sockaddrin, struct rpcap_sockaddr *sockaddrout)
2837*8b26181fSAndroid Build Coastguard Worker {
2838*8b26181fSAndroid Build Coastguard Worker 	memset(sockaddrout, 0, sizeof(struct sockaddr_storage));
2839*8b26181fSAndroid Build Coastguard Worker 
2840*8b26181fSAndroid Build Coastguard Worker 	// There can be the case in which the sockaddrin is not available
2841*8b26181fSAndroid Build Coastguard Worker 	if (sockaddrin == NULL) return;
2842*8b26181fSAndroid Build Coastguard Worker 
2843*8b26181fSAndroid Build Coastguard Worker 	// Warning: we support only AF_INET and AF_INET6
2844*8b26181fSAndroid Build Coastguard Worker 	//
2845*8b26181fSAndroid Build Coastguard Worker 	// Note: as noted above, the output structures are not
2846*8b26181fSAndroid Build Coastguard Worker 	// neatly aligned on 4-byte boundaries, so we must fill
2847*8b26181fSAndroid Build Coastguard Worker 	// in an aligned structure and then copy it to the output
2848*8b26181fSAndroid Build Coastguard Worker 	// buffer with memcpy().
2849*8b26181fSAndroid Build Coastguard Worker 	switch (sockaddrin->ss_family)
2850*8b26181fSAndroid Build Coastguard Worker 	{
2851*8b26181fSAndroid Build Coastguard Worker 	case AF_INET:
2852*8b26181fSAndroid Build Coastguard Worker 		{
2853*8b26181fSAndroid Build Coastguard Worker 		struct sockaddr_in *sockaddrin_ipv4;
2854*8b26181fSAndroid Build Coastguard Worker 		struct rpcap_sockaddr_in sockaddrout_ipv4;
2855*8b26181fSAndroid Build Coastguard Worker 
2856*8b26181fSAndroid Build Coastguard Worker 		sockaddrin_ipv4 = (struct sockaddr_in *) sockaddrin;
2857*8b26181fSAndroid Build Coastguard Worker 
2858*8b26181fSAndroid Build Coastguard Worker 		sockaddrout_ipv4.family = htons(RPCAP_AF_INET);
2859*8b26181fSAndroid Build Coastguard Worker 		sockaddrout_ipv4.port = htons(sockaddrin_ipv4->sin_port);
2860*8b26181fSAndroid Build Coastguard Worker 		memcpy(&sockaddrout_ipv4.addr, &sockaddrin_ipv4->sin_addr, sizeof(sockaddrout_ipv4.addr));
2861*8b26181fSAndroid Build Coastguard Worker 		memset(sockaddrout_ipv4.zero, 0, sizeof(sockaddrout_ipv4.zero));
2862*8b26181fSAndroid Build Coastguard Worker 		memcpy(sockaddrout, &sockaddrout_ipv4, sizeof(struct rpcap_sockaddr_in));
2863*8b26181fSAndroid Build Coastguard Worker 		break;
2864*8b26181fSAndroid Build Coastguard Worker 		}
2865*8b26181fSAndroid Build Coastguard Worker 
2866*8b26181fSAndroid Build Coastguard Worker #ifdef AF_INET6
2867*8b26181fSAndroid Build Coastguard Worker 	case AF_INET6:
2868*8b26181fSAndroid Build Coastguard Worker 		{
2869*8b26181fSAndroid Build Coastguard Worker 		struct sockaddr_in6 *sockaddrin_ipv6;
2870*8b26181fSAndroid Build Coastguard Worker 		struct rpcap_sockaddr_in6 sockaddrout_ipv6;
2871*8b26181fSAndroid Build Coastguard Worker 
2872*8b26181fSAndroid Build Coastguard Worker 		sockaddrin_ipv6 = (struct sockaddr_in6 *) sockaddrin;
2873*8b26181fSAndroid Build Coastguard Worker 
2874*8b26181fSAndroid Build Coastguard Worker 		sockaddrout_ipv6.family = htons(RPCAP_AF_INET6);
2875*8b26181fSAndroid Build Coastguard Worker 		sockaddrout_ipv6.port = htons(sockaddrin_ipv6->sin6_port);
2876*8b26181fSAndroid Build Coastguard Worker 		sockaddrout_ipv6.flowinfo = htonl(sockaddrin_ipv6->sin6_flowinfo);
2877*8b26181fSAndroid Build Coastguard Worker 		memcpy(&sockaddrout_ipv6.addr, &sockaddrin_ipv6->sin6_addr, sizeof(sockaddrout_ipv6.addr));
2878*8b26181fSAndroid Build Coastguard Worker 		sockaddrout_ipv6.scope_id = htonl(sockaddrin_ipv6->sin6_scope_id);
2879*8b26181fSAndroid Build Coastguard Worker 		memcpy(sockaddrout, &sockaddrout_ipv6, sizeof(struct rpcap_sockaddr_in6));
2880*8b26181fSAndroid Build Coastguard Worker 		break;
2881*8b26181fSAndroid Build Coastguard Worker 		}
2882*8b26181fSAndroid Build Coastguard Worker #endif
2883*8b26181fSAndroid Build Coastguard Worker 	}
2884*8b26181fSAndroid Build Coastguard Worker }
2885*8b26181fSAndroid Build Coastguard Worker 
2886*8b26181fSAndroid Build Coastguard Worker 
2887*8b26181fSAndroid Build Coastguard Worker /*!
2888*8b26181fSAndroid Build Coastguard Worker 	\brief Suspends a thread for secs seconds.
2889*8b26181fSAndroid Build Coastguard Worker */
sleep_secs(int secs)2890*8b26181fSAndroid Build Coastguard Worker void sleep_secs(int secs)
2891*8b26181fSAndroid Build Coastguard Worker {
2892*8b26181fSAndroid Build Coastguard Worker #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
2893*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
2894*8b26181fSAndroid Build Coastguard Worker 	Sleep(secs*1000);
2895*8b26181fSAndroid Build Coastguard Worker #else
2896*8b26181fSAndroid Build Coastguard Worker 	unsigned secs_remaining;
2897*8b26181fSAndroid Build Coastguard Worker 
2898*8b26181fSAndroid Build Coastguard Worker 	if (secs <= 0)
2899*8b26181fSAndroid Build Coastguard Worker 		return;
2900*8b26181fSAndroid Build Coastguard Worker 	secs_remaining = secs;
2901*8b26181fSAndroid Build Coastguard Worker 	while (secs_remaining != 0)
2902*8b26181fSAndroid Build Coastguard Worker 		secs_remaining = sleep(secs_remaining);
2903*8b26181fSAndroid Build Coastguard Worker #endif
2904*8b26181fSAndroid Build Coastguard Worker #endif
2905*8b26181fSAndroid Build Coastguard Worker }
2906*8b26181fSAndroid Build Coastguard Worker 
2907*8b26181fSAndroid Build Coastguard Worker /*
2908*8b26181fSAndroid Build Coastguard Worker  * Read the header of a message.
2909*8b26181fSAndroid Build Coastguard Worker  */
2910*8b26181fSAndroid Build Coastguard Worker static int
rpcapd_recv_msg_header(SOCKET sock,SSL * ssl,struct rpcap_header * headerp)2911*8b26181fSAndroid Build Coastguard Worker rpcapd_recv_msg_header(SOCKET sock, SSL *ssl, struct rpcap_header *headerp)
2912*8b26181fSAndroid Build Coastguard Worker {
2913*8b26181fSAndroid Build Coastguard Worker 	int nread;
2914*8b26181fSAndroid Build Coastguard Worker 	char errbuf[PCAP_ERRBUF_SIZE];		// buffer for network errors
2915*8b26181fSAndroid Build Coastguard Worker 
2916*8b26181fSAndroid Build Coastguard Worker 	nread = sock_recv(sock, ssl, (char *) headerp, sizeof(struct rpcap_header),
2917*8b26181fSAndroid Build Coastguard Worker 	    SOCK_RECEIVEALL_YES|SOCK_EOF_ISNT_ERROR, errbuf, PCAP_ERRBUF_SIZE);
2918*8b26181fSAndroid Build Coastguard Worker 	if (nread == -1)
2919*8b26181fSAndroid Build Coastguard Worker 	{
2920*8b26181fSAndroid Build Coastguard Worker 		// Network error.
2921*8b26181fSAndroid Build Coastguard Worker 		rpcapd_log(LOGPRIO_ERROR, "Read from client failed: %s", errbuf);
2922*8b26181fSAndroid Build Coastguard Worker 		return -1;
2923*8b26181fSAndroid Build Coastguard Worker 	}
2924*8b26181fSAndroid Build Coastguard Worker 	if (nread == 0)
2925*8b26181fSAndroid Build Coastguard Worker 	{
2926*8b26181fSAndroid Build Coastguard Worker 		// Immediate EOF; that's treated like a close message.
2927*8b26181fSAndroid Build Coastguard Worker 		return -2;
2928*8b26181fSAndroid Build Coastguard Worker 	}
2929*8b26181fSAndroid Build Coastguard Worker 	headerp->plen = ntohl(headerp->plen);
2930*8b26181fSAndroid Build Coastguard Worker 	return 0;
2931*8b26181fSAndroid Build Coastguard Worker }
2932*8b26181fSAndroid Build Coastguard Worker 
2933*8b26181fSAndroid Build Coastguard Worker /*
2934*8b26181fSAndroid Build Coastguard Worker  * Read data from a message.
2935*8b26181fSAndroid Build Coastguard Worker  * If we're trying to read more data that remains, puts an error
2936*8b26181fSAndroid Build Coastguard Worker  * message into errmsgbuf and returns -2.  Otherwise, tries to read
2937*8b26181fSAndroid Build Coastguard Worker  * the data and, if that succeeds, subtracts the amount read from
2938*8b26181fSAndroid Build Coastguard Worker  * the number of bytes of data that remains.
2939*8b26181fSAndroid Build Coastguard Worker  * Returns 0 on success, logs a message and returns -1 on a network
2940*8b26181fSAndroid Build Coastguard Worker  * error.
2941*8b26181fSAndroid Build Coastguard Worker  */
2942*8b26181fSAndroid Build Coastguard Worker static int
rpcapd_recv(SOCKET sock,SSL * ssl,char * buffer,size_t toread,uint32 * plen,char * errmsgbuf)2943*8b26181fSAndroid Build Coastguard Worker rpcapd_recv(SOCKET sock, SSL *ssl, char *buffer, size_t toread, uint32 *plen, char *errmsgbuf)
2944*8b26181fSAndroid Build Coastguard Worker {
2945*8b26181fSAndroid Build Coastguard Worker 	int nread;
2946*8b26181fSAndroid Build Coastguard Worker 	char errbuf[PCAP_ERRBUF_SIZE];		// buffer for network errors
2947*8b26181fSAndroid Build Coastguard Worker 
2948*8b26181fSAndroid Build Coastguard Worker 	if (toread > *plen)
2949*8b26181fSAndroid Build Coastguard Worker 	{
2950*8b26181fSAndroid Build Coastguard Worker 		// Tell the client and continue.
2951*8b26181fSAndroid Build Coastguard Worker 		snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Message payload is too short");
2952*8b26181fSAndroid Build Coastguard Worker 		return -2;
2953*8b26181fSAndroid Build Coastguard Worker 	}
2954*8b26181fSAndroid Build Coastguard Worker 	nread = sock_recv(sock, ssl, buffer, toread,
2955*8b26181fSAndroid Build Coastguard Worker 	    SOCK_RECEIVEALL_YES|SOCK_EOF_IS_ERROR, errbuf, PCAP_ERRBUF_SIZE);
2956*8b26181fSAndroid Build Coastguard Worker 	if (nread == -1)
2957*8b26181fSAndroid Build Coastguard Worker 	{
2958*8b26181fSAndroid Build Coastguard Worker 		rpcapd_log(LOGPRIO_ERROR, "Read from client failed: %s", errbuf);
2959*8b26181fSAndroid Build Coastguard Worker 		return -1;
2960*8b26181fSAndroid Build Coastguard Worker 	}
2961*8b26181fSAndroid Build Coastguard Worker 	*plen -= nread;
2962*8b26181fSAndroid Build Coastguard Worker 	return 0;
2963*8b26181fSAndroid Build Coastguard Worker }
2964*8b26181fSAndroid Build Coastguard Worker 
2965*8b26181fSAndroid Build Coastguard Worker /*
2966*8b26181fSAndroid Build Coastguard Worker  * Discard data from a connection.
2967*8b26181fSAndroid Build Coastguard Worker  * Mostly used to discard wrong-sized messages.
2968*8b26181fSAndroid Build Coastguard Worker  * Returns 0 on success, logs a message and returns -1 on a network
2969*8b26181fSAndroid Build Coastguard Worker  * error.
2970*8b26181fSAndroid Build Coastguard Worker  */
2971*8b26181fSAndroid Build Coastguard Worker static int
rpcapd_discard(SOCKET sock,SSL * ssl,uint32 len)2972*8b26181fSAndroid Build Coastguard Worker rpcapd_discard(SOCKET sock, SSL *ssl, uint32 len)
2973*8b26181fSAndroid Build Coastguard Worker {
2974*8b26181fSAndroid Build Coastguard Worker 	char errbuf[PCAP_ERRBUF_SIZE + 1];	// keeps the error string, prior to be printed
2975*8b26181fSAndroid Build Coastguard Worker 
2976*8b26181fSAndroid Build Coastguard Worker 	if (len != 0)
2977*8b26181fSAndroid Build Coastguard Worker 	{
2978*8b26181fSAndroid Build Coastguard Worker 		if (sock_discard(sock, ssl, len, errbuf, PCAP_ERRBUF_SIZE) == -1)
2979*8b26181fSAndroid Build Coastguard Worker 		{
2980*8b26181fSAndroid Build Coastguard Worker 			// Network error.
2981*8b26181fSAndroid Build Coastguard Worker 			rpcapd_log(LOGPRIO_ERROR, "Read from client failed: %s", errbuf);
2982*8b26181fSAndroid Build Coastguard Worker 			return -1;
2983*8b26181fSAndroid Build Coastguard Worker 		}
2984*8b26181fSAndroid Build Coastguard Worker 	}
2985*8b26181fSAndroid Build Coastguard Worker 	return 0;
2986*8b26181fSAndroid Build Coastguard Worker }
2987*8b26181fSAndroid Build Coastguard Worker 
2988*8b26181fSAndroid Build Coastguard Worker //
2989*8b26181fSAndroid Build Coastguard Worker // Shut down any packet-capture thread associated with the session,
2990*8b26181fSAndroid Build Coastguard Worker // close the SSL handle for the data socket if we have one, close
2991*8b26181fSAndroid Build Coastguard Worker // the data socket if we have one, and close the underlying packet
2992*8b26181fSAndroid Build Coastguard Worker // capture handle if we have one.
2993*8b26181fSAndroid Build Coastguard Worker //
2994*8b26181fSAndroid Build Coastguard Worker // We do not, of course, touch the controlling socket that's also
2995*8b26181fSAndroid Build Coastguard Worker // copied into the session, as the service loop might still use it.
2996*8b26181fSAndroid Build Coastguard Worker //
session_close(struct session * session)2997*8b26181fSAndroid Build Coastguard Worker static void session_close(struct session *session)
2998*8b26181fSAndroid Build Coastguard Worker {
2999*8b26181fSAndroid Build Coastguard Worker 	if (session->have_thread)
3000*8b26181fSAndroid Build Coastguard Worker 	{
3001*8b26181fSAndroid Build Coastguard Worker 		//
3002*8b26181fSAndroid Build Coastguard Worker 		// Tell the data connection thread main capture loop to
3003*8b26181fSAndroid Build Coastguard Worker 		// break out of that loop.
3004*8b26181fSAndroid Build Coastguard Worker 		//
3005*8b26181fSAndroid Build Coastguard Worker 		// This may be sufficient to wake up a blocked thread,
3006*8b26181fSAndroid Build Coastguard Worker 		// but it's not guaranteed to be sufficient.
3007*8b26181fSAndroid Build Coastguard Worker 		//
3008*8b26181fSAndroid Build Coastguard Worker 		pcap_breakloop(session->fp);
3009*8b26181fSAndroid Build Coastguard Worker 
3010*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
3011*8b26181fSAndroid Build Coastguard Worker 		//
3012*8b26181fSAndroid Build Coastguard Worker 		// Set the event on which a read would block, so that,
3013*8b26181fSAndroid Build Coastguard Worker 		// if it's currently blocked waiting for packets to
3014*8b26181fSAndroid Build Coastguard Worker 		// arrive, it'll wake up, so it can see the "break
3015*8b26181fSAndroid Build Coastguard Worker 		// out of the loop" indication.  (pcap_breakloop()
3016*8b26181fSAndroid Build Coastguard Worker 		// might do this, but older versions don't.  Setting
3017*8b26181fSAndroid Build Coastguard Worker 		// it twice should, at worst, cause an extra wakeup,
3018*8b26181fSAndroid Build Coastguard Worker 		// which shouldn't be a problem.)
3019*8b26181fSAndroid Build Coastguard Worker 		//
3020*8b26181fSAndroid Build Coastguard Worker 		// XXX - what about modules other than NPF?
3021*8b26181fSAndroid Build Coastguard Worker 		//
3022*8b26181fSAndroid Build Coastguard Worker 		SetEvent(pcap_getevent(session->fp));
3023*8b26181fSAndroid Build Coastguard Worker 
3024*8b26181fSAndroid Build Coastguard Worker 		//
3025*8b26181fSAndroid Build Coastguard Worker 		// Wait for the thread to exit, so we don't close
3026*8b26181fSAndroid Build Coastguard Worker 		// sockets out from under it.
3027*8b26181fSAndroid Build Coastguard Worker 		//
3028*8b26181fSAndroid Build Coastguard Worker 		// XXX - have a timeout, so we don't wait forever?
3029*8b26181fSAndroid Build Coastguard Worker 		//
3030*8b26181fSAndroid Build Coastguard Worker 		WaitForSingleObject(session->thread, INFINITE);
3031*8b26181fSAndroid Build Coastguard Worker 
3032*8b26181fSAndroid Build Coastguard Worker 		//
3033*8b26181fSAndroid Build Coastguard Worker 		// Release the thread handle, as we're done with
3034*8b26181fSAndroid Build Coastguard Worker 		// it.
3035*8b26181fSAndroid Build Coastguard Worker 		//
3036*8b26181fSAndroid Build Coastguard Worker 		CloseHandle(session->thread);
3037*8b26181fSAndroid Build Coastguard Worker 		session->have_thread = 0;
3038*8b26181fSAndroid Build Coastguard Worker 		session->thread = INVALID_HANDLE_VALUE;
3039*8b26181fSAndroid Build Coastguard Worker #else
3040*8b26181fSAndroid Build Coastguard Worker 		//
3041*8b26181fSAndroid Build Coastguard Worker 		// Send a SIGUSR1 signal to the thread, so that, if
3042*8b26181fSAndroid Build Coastguard Worker 		// it's currently blocked waiting for packets to arrive,
3043*8b26181fSAndroid Build Coastguard Worker 		// it'll wake up (we've turned off SA_RESTART for
3044*8b26181fSAndroid Build Coastguard Worker 		// SIGUSR1, so that the system call in which it's blocked
3045*8b26181fSAndroid Build Coastguard Worker 		// should return EINTR rather than restarting).
3046*8b26181fSAndroid Build Coastguard Worker 		//
3047*8b26181fSAndroid Build Coastguard Worker 		pthread_kill(session->thread, SIGUSR1);
3048*8b26181fSAndroid Build Coastguard Worker 
3049*8b26181fSAndroid Build Coastguard Worker 		//
3050*8b26181fSAndroid Build Coastguard Worker 		// Wait for the thread to exit, so we don't close
3051*8b26181fSAndroid Build Coastguard Worker 		// sockets out from under it.
3052*8b26181fSAndroid Build Coastguard Worker 		//
3053*8b26181fSAndroid Build Coastguard Worker 		// XXX - have a timeout, so we don't wait forever?
3054*8b26181fSAndroid Build Coastguard Worker 		//
3055*8b26181fSAndroid Build Coastguard Worker 		pthread_join(session->thread, NULL);
3056*8b26181fSAndroid Build Coastguard Worker 		session->have_thread = 0;
3057*8b26181fSAndroid Build Coastguard Worker 		memset(&session->thread, 0, sizeof(session->thread));
3058*8b26181fSAndroid Build Coastguard Worker #endif
3059*8b26181fSAndroid Build Coastguard Worker 	}
3060*8b26181fSAndroid Build Coastguard Worker 
3061*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OPENSSL
3062*8b26181fSAndroid Build Coastguard Worker 	if (session->data_ssl)
3063*8b26181fSAndroid Build Coastguard Worker 	{
3064*8b26181fSAndroid Build Coastguard Worker 		// Finish using the SSL handle for the socket.
3065*8b26181fSAndroid Build Coastguard Worker 		// This must be done *before* the socket is closed.
3066*8b26181fSAndroid Build Coastguard Worker 		ssl_finish(session->data_ssl);
3067*8b26181fSAndroid Build Coastguard Worker 		session->data_ssl = NULL;
3068*8b26181fSAndroid Build Coastguard Worker 	}
3069*8b26181fSAndroid Build Coastguard Worker #endif
3070*8b26181fSAndroid Build Coastguard Worker 
3071*8b26181fSAndroid Build Coastguard Worker 	if (session->sockdata != INVALID_SOCKET)
3072*8b26181fSAndroid Build Coastguard Worker 	{
3073*8b26181fSAndroid Build Coastguard Worker 		sock_close(session->sockdata, NULL, 0);
3074*8b26181fSAndroid Build Coastguard Worker 		session->sockdata = INVALID_SOCKET;
3075*8b26181fSAndroid Build Coastguard Worker 	}
3076*8b26181fSAndroid Build Coastguard Worker 
3077*8b26181fSAndroid Build Coastguard Worker 	if (session->fp)
3078*8b26181fSAndroid Build Coastguard Worker 	{
3079*8b26181fSAndroid Build Coastguard Worker 		pcap_close(session->fp);
3080*8b26181fSAndroid Build Coastguard Worker 		session->fp = NULL;
3081*8b26181fSAndroid Build Coastguard Worker 	}
3082*8b26181fSAndroid Build Coastguard Worker }
3083*8b26181fSAndroid Build Coastguard Worker 
3084*8b26181fSAndroid Build Coastguard Worker //
3085*8b26181fSAndroid Build Coastguard Worker // Check whether a capture source string is a URL or not.
3086*8b26181fSAndroid Build Coastguard Worker // This includes URLs that refer to a local device; a scheme, followed
3087*8b26181fSAndroid Build Coastguard Worker // by ://, followed by *another* scheme and ://, is just silly, and
3088*8b26181fSAndroid Build Coastguard Worker // anybody who supplies that will get an error.
3089*8b26181fSAndroid Build Coastguard Worker //
3090*8b26181fSAndroid Build Coastguard Worker static int
is_url(const char * source)3091*8b26181fSAndroid Build Coastguard Worker is_url(const char *source)
3092*8b26181fSAndroid Build Coastguard Worker {
3093*8b26181fSAndroid Build Coastguard Worker 	char *colonp;
3094*8b26181fSAndroid Build Coastguard Worker 
3095*8b26181fSAndroid Build Coastguard Worker 	/*
3096*8b26181fSAndroid Build Coastguard Worker 	 * RFC 3986 says:
3097*8b26181fSAndroid Build Coastguard Worker 	 *
3098*8b26181fSAndroid Build Coastguard Worker 	 *   URI         = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
3099*8b26181fSAndroid Build Coastguard Worker 	 *
3100*8b26181fSAndroid Build Coastguard Worker 	 *   hier-part   = "//" authority path-abempty
3101*8b26181fSAndroid Build Coastguard Worker 	 *               / path-absolute
3102*8b26181fSAndroid Build Coastguard Worker 	 *               / path-rootless
3103*8b26181fSAndroid Build Coastguard Worker 	 *               / path-empty
3104*8b26181fSAndroid Build Coastguard Worker 	 *
3105*8b26181fSAndroid Build Coastguard Worker 	 *   authority   = [ userinfo "@" ] host [ ":" port ]
3106*8b26181fSAndroid Build Coastguard Worker 	 *
3107*8b26181fSAndroid Build Coastguard Worker 	 *   userinfo    = *( unreserved / pct-encoded / sub-delims / ":" )
3108*8b26181fSAndroid Build Coastguard Worker 	 *
3109*8b26181fSAndroid Build Coastguard Worker 	 * Step 1: look for the ":" at the end of the scheme.
3110*8b26181fSAndroid Build Coastguard Worker 	 * A colon in the source is *NOT* sufficient to indicate that
3111*8b26181fSAndroid Build Coastguard Worker 	 * this is a URL, as interface names on some platforms might
3112*8b26181fSAndroid Build Coastguard Worker 	 * include colons (e.g., I think some Solaris interfaces
3113*8b26181fSAndroid Build Coastguard Worker 	 * might).
3114*8b26181fSAndroid Build Coastguard Worker 	 */
3115*8b26181fSAndroid Build Coastguard Worker 	colonp = strchr(source, ':');
3116*8b26181fSAndroid Build Coastguard Worker 	if (colonp == NULL)
3117*8b26181fSAndroid Build Coastguard Worker 	{
3118*8b26181fSAndroid Build Coastguard Worker 		/*
3119*8b26181fSAndroid Build Coastguard Worker 		 * The source is the device to open.  It's not a URL.
3120*8b26181fSAndroid Build Coastguard Worker 		 */
3121*8b26181fSAndroid Build Coastguard Worker 		return (0);
3122*8b26181fSAndroid Build Coastguard Worker 	}
3123*8b26181fSAndroid Build Coastguard Worker 
3124*8b26181fSAndroid Build Coastguard Worker 	/*
3125*8b26181fSAndroid Build Coastguard Worker 	 * All schemes must have "//" after them, i.e. we only support
3126*8b26181fSAndroid Build Coastguard Worker 	 * hier-part   = "//" authority path-abempty, not
3127*8b26181fSAndroid Build Coastguard Worker 	 * hier-part   = path-absolute
3128*8b26181fSAndroid Build Coastguard Worker 	 * hier-part   = path-rootless
3129*8b26181fSAndroid Build Coastguard Worker 	 * hier-part   = path-empty
3130*8b26181fSAndroid Build Coastguard Worker 	 *
3131*8b26181fSAndroid Build Coastguard Worker 	 * We need that in order to distinguish between a local device
3132*8b26181fSAndroid Build Coastguard Worker 	 * name that happens to contain a colon and a URI.
3133*8b26181fSAndroid Build Coastguard Worker 	 */
3134*8b26181fSAndroid Build Coastguard Worker 	if (strncmp(colonp + 1, "//", 2) != 0)
3135*8b26181fSAndroid Build Coastguard Worker 	{
3136*8b26181fSAndroid Build Coastguard Worker 		/*
3137*8b26181fSAndroid Build Coastguard Worker 		 * The source is the device to open.  It's not a URL.
3138*8b26181fSAndroid Build Coastguard Worker 		 */
3139*8b26181fSAndroid Build Coastguard Worker 		return (0);
3140*8b26181fSAndroid Build Coastguard Worker 	}
3141*8b26181fSAndroid Build Coastguard Worker 
3142*8b26181fSAndroid Build Coastguard Worker 	/*
3143*8b26181fSAndroid Build Coastguard Worker 	 * It's a URL.
3144*8b26181fSAndroid Build Coastguard Worker 	 */
3145*8b26181fSAndroid Build Coastguard Worker 	return (1);
3146*8b26181fSAndroid Build Coastguard Worker }
3147