1*8b26181fSAndroid Build Coastguard Worker /*
2*8b26181fSAndroid Build Coastguard Worker * Copyright (c) 2002 - 2005 NetGroup, Politecnico di Torino (Italy)
3*8b26181fSAndroid Build Coastguard Worker * Copyright (c) 2005 - 2008 CACE Technologies, Davis (California)
4*8b26181fSAndroid Build Coastguard Worker * All rights reserved.
5*8b26181fSAndroid Build Coastguard Worker *
6*8b26181fSAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
7*8b26181fSAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions
8*8b26181fSAndroid Build Coastguard Worker * are met:
9*8b26181fSAndroid Build Coastguard Worker *
10*8b26181fSAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright
11*8b26181fSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
12*8b26181fSAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright
13*8b26181fSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the
14*8b26181fSAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution.
15*8b26181fSAndroid Build Coastguard Worker * 3. Neither the name of the Politecnico di Torino, CACE Technologies
16*8b26181fSAndroid Build Coastguard Worker * nor the names of its contributors may be used to endorse or promote
17*8b26181fSAndroid Build Coastguard Worker * products derived from this software without specific prior written
18*8b26181fSAndroid Build Coastguard Worker * permission.
19*8b26181fSAndroid Build Coastguard Worker *
20*8b26181fSAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*8b26181fSAndroid Build Coastguard Worker * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*8b26181fSAndroid Build Coastguard Worker * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23*8b26181fSAndroid Build Coastguard Worker * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24*8b26181fSAndroid Build Coastguard Worker * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25*8b26181fSAndroid Build Coastguard Worker * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26*8b26181fSAndroid Build Coastguard Worker * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27*8b26181fSAndroid Build Coastguard Worker * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28*8b26181fSAndroid Build Coastguard Worker * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29*8b26181fSAndroid Build Coastguard Worker * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30*8b26181fSAndroid Build Coastguard Worker * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31*8b26181fSAndroid Build Coastguard Worker *
32*8b26181fSAndroid Build Coastguard Worker */
33*8b26181fSAndroid Build Coastguard Worker
34*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
35*8b26181fSAndroid Build Coastguard Worker #include <config.h>
36*8b26181fSAndroid Build Coastguard Worker #endif
37*8b26181fSAndroid Build Coastguard Worker
38*8b26181fSAndroid Build Coastguard Worker #include "ftmacros.h"
39*8b26181fSAndroid Build Coastguard Worker #include "diag-control.h"
40*8b26181fSAndroid Build Coastguard Worker
41*8b26181fSAndroid Build Coastguard Worker #include <string.h> /* for strlen(), ... */
42*8b26181fSAndroid Build Coastguard Worker #include <stdlib.h> /* for malloc(), free(), ... */
43*8b26181fSAndroid Build Coastguard Worker #include <stdarg.h> /* for functions with variable number of arguments */
44*8b26181fSAndroid Build Coastguard Worker #include <errno.h> /* for the errno variable */
45*8b26181fSAndroid Build Coastguard Worker #include <limits.h> /* for INT_MAX */
46*8b26181fSAndroid Build Coastguard Worker #include "sockutils.h"
47*8b26181fSAndroid Build Coastguard Worker #include "pcap-int.h"
48*8b26181fSAndroid Build Coastguard Worker #include "pcap-util.h"
49*8b26181fSAndroid Build Coastguard Worker #include "rpcap-protocol.h"
50*8b26181fSAndroid Build Coastguard Worker #include "pcap-rpcap.h"
51*8b26181fSAndroid Build Coastguard Worker
52*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
53*8b26181fSAndroid Build Coastguard Worker #include "charconv.h" /* for utf_8_to_acp_truncated() */
54*8b26181fSAndroid Build Coastguard Worker #endif
55*8b26181fSAndroid Build Coastguard Worker
56*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OPENSSL
57*8b26181fSAndroid Build Coastguard Worker #include "sslutils.h"
58*8b26181fSAndroid Build Coastguard Worker #endif
59*8b26181fSAndroid Build Coastguard Worker
60*8b26181fSAndroid Build Coastguard Worker /*
61*8b26181fSAndroid Build Coastguard Worker * This file contains the pcap module for capturing from a remote machine's
62*8b26181fSAndroid Build Coastguard Worker * interfaces using the RPCAP protocol.
63*8b26181fSAndroid Build Coastguard Worker *
64*8b26181fSAndroid Build Coastguard Worker * WARNING: All the RPCAP functions that are allowed to return a buffer
65*8b26181fSAndroid Build Coastguard Worker * containing the error description can return max PCAP_ERRBUF_SIZE characters.
66*8b26181fSAndroid Build Coastguard Worker * However there is no guarantees that the string will be zero-terminated.
67*8b26181fSAndroid Build Coastguard Worker * Best practice is to define the errbuf variable as a char of size
68*8b26181fSAndroid Build Coastguard Worker * 'PCAP_ERRBUF_SIZE+1' and to insert manually a NULL character at the end
69*8b26181fSAndroid Build Coastguard Worker * of the buffer. This will guarantee that no buffer overflows occur even
70*8b26181fSAndroid Build Coastguard Worker * if we use the printf() to show the error on the screen.
71*8b26181fSAndroid Build Coastguard Worker *
72*8b26181fSAndroid Build Coastguard Worker * XXX - actually, null-terminating the error string is part of the
73*8b26181fSAndroid Build Coastguard Worker * contract for the pcap API; if there's any place in the pcap code
74*8b26181fSAndroid Build Coastguard Worker * that doesn't guarantee null-termination, even at the expense of
75*8b26181fSAndroid Build Coastguard Worker * cutting the message short, that's a bug and needs to be fixed.
76*8b26181fSAndroid Build Coastguard Worker */
77*8b26181fSAndroid Build Coastguard Worker
78*8b26181fSAndroid Build Coastguard Worker #define PCAP_STATS_STANDARD 0 /* Used by pcap_stats_rpcap to see if we want standard or extended statistics */
79*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
80*8b26181fSAndroid Build Coastguard Worker #define PCAP_STATS_EX 1 /* Used by pcap_stats_rpcap to see if we want standard or extended statistics */
81*8b26181fSAndroid Build Coastguard Worker #endif
82*8b26181fSAndroid Build Coastguard Worker
83*8b26181fSAndroid Build Coastguard Worker /*
84*8b26181fSAndroid Build Coastguard Worker * \brief Keeps a list of all the opened connections in the active mode.
85*8b26181fSAndroid Build Coastguard Worker *
86*8b26181fSAndroid Build Coastguard Worker * This structure defines a linked list of items that are needed to keep the info required to
87*8b26181fSAndroid Build Coastguard Worker * manage the active mode.
88*8b26181fSAndroid Build Coastguard Worker * In other words, when a new connection in active mode starts, this structure is updated so that
89*8b26181fSAndroid Build Coastguard Worker * it reflects the list of active mode connections currently opened.
90*8b26181fSAndroid Build Coastguard Worker * This structure is required by findalldevs() and open_remote() to see if they have to open a new
91*8b26181fSAndroid Build Coastguard Worker * control connection toward the host, or they already have a control connection in place.
92*8b26181fSAndroid Build Coastguard Worker */
93*8b26181fSAndroid Build Coastguard Worker struct activehosts
94*8b26181fSAndroid Build Coastguard Worker {
95*8b26181fSAndroid Build Coastguard Worker struct sockaddr_storage host;
96*8b26181fSAndroid Build Coastguard Worker SOCKET sockctrl;
97*8b26181fSAndroid Build Coastguard Worker SSL *ssl;
98*8b26181fSAndroid Build Coastguard Worker uint8 protocol_version;
99*8b26181fSAndroid Build Coastguard Worker int byte_swapped;
100*8b26181fSAndroid Build Coastguard Worker struct activehosts *next;
101*8b26181fSAndroid Build Coastguard Worker };
102*8b26181fSAndroid Build Coastguard Worker
103*8b26181fSAndroid Build Coastguard Worker /* Keeps a list of all the opened connections in the active mode. */
104*8b26181fSAndroid Build Coastguard Worker static struct activehosts *activeHosts;
105*8b26181fSAndroid Build Coastguard Worker
106*8b26181fSAndroid Build Coastguard Worker /*
107*8b26181fSAndroid Build Coastguard Worker * Keeps the main socket identifier when we want to accept a new remote
108*8b26181fSAndroid Build Coastguard Worker * connection (active mode only).
109*8b26181fSAndroid Build Coastguard Worker * See the documentation of pcap_remoteact_accept() and
110*8b26181fSAndroid Build Coastguard Worker * pcap_remoteact_cleanup() for more details.
111*8b26181fSAndroid Build Coastguard Worker */
112*8b26181fSAndroid Build Coastguard Worker static SOCKET sockmain;
113*8b26181fSAndroid Build Coastguard Worker static SSL *ssl_main;
114*8b26181fSAndroid Build Coastguard Worker
115*8b26181fSAndroid Build Coastguard Worker /*
116*8b26181fSAndroid Build Coastguard Worker * Private data for capturing remotely using the rpcap protocol.
117*8b26181fSAndroid Build Coastguard Worker */
118*8b26181fSAndroid Build Coastguard Worker struct pcap_rpcap {
119*8b26181fSAndroid Build Coastguard Worker /*
120*8b26181fSAndroid Build Coastguard Worker * This is '1' if we're the network client; it is needed by several
121*8b26181fSAndroid Build Coastguard Worker * functions (such as pcap_setfilter()) to know whether they have
122*8b26181fSAndroid Build Coastguard Worker * to use the socket or have to open the local adapter.
123*8b26181fSAndroid Build Coastguard Worker */
124*8b26181fSAndroid Build Coastguard Worker int rmt_clientside;
125*8b26181fSAndroid Build Coastguard Worker
126*8b26181fSAndroid Build Coastguard Worker SOCKET rmt_sockctrl; /* socket ID of the socket used for the control connection */
127*8b26181fSAndroid Build Coastguard Worker SOCKET rmt_sockdata; /* socket ID of the socket used for the data connection */
128*8b26181fSAndroid Build Coastguard Worker SSL *ctrl_ssl, *data_ssl; /* optional transport of rmt_sockctrl and rmt_sockdata via TLS */
129*8b26181fSAndroid Build Coastguard Worker int rmt_flags; /* we have to save flags, since they are passed by the pcap_open_live(), but they are used by the pcap_startcapture() */
130*8b26181fSAndroid Build Coastguard Worker int rmt_capstarted; /* 'true' if the capture is already started (needed to knoe if we have to call the pcap_startcapture() */
131*8b26181fSAndroid Build Coastguard Worker char *currentfilter; /* Pointer to a buffer (allocated at run-time) that stores the current filter. Needed when flag PCAP_OPENFLAG_NOCAPTURE_RPCAP is turned on. */
132*8b26181fSAndroid Build Coastguard Worker
133*8b26181fSAndroid Build Coastguard Worker uint8 protocol_version; /* negotiated protocol version */
134*8b26181fSAndroid Build Coastguard Worker uint8 uses_ssl; /* User asked for rpcaps scheme */
135*8b26181fSAndroid Build Coastguard Worker int byte_swapped; /* Server byte order is swapped from ours */
136*8b26181fSAndroid Build Coastguard Worker
137*8b26181fSAndroid Build Coastguard Worker unsigned int TotNetDrops; /* keeps the number of packets that have been dropped by the network */
138*8b26181fSAndroid Build Coastguard Worker
139*8b26181fSAndroid Build Coastguard Worker /*
140*8b26181fSAndroid Build Coastguard Worker * This keeps the number of packets that have been received by the
141*8b26181fSAndroid Build Coastguard Worker * application.
142*8b26181fSAndroid Build Coastguard Worker *
143*8b26181fSAndroid Build Coastguard Worker * Packets dropped by the kernel buffer are not counted in this
144*8b26181fSAndroid Build Coastguard Worker * variable. It is always equal to (TotAccepted - TotDrops),
145*8b26181fSAndroid Build Coastguard Worker * except for the case of remote capture, in which we have also
146*8b26181fSAndroid Build Coastguard Worker * packets in flight, i.e. that have been transmitted by the remote
147*8b26181fSAndroid Build Coastguard Worker * host, but that have not been received (yet) from the client.
148*8b26181fSAndroid Build Coastguard Worker * In this case, (TotAccepted - TotDrops - TotNetDrops) gives a
149*8b26181fSAndroid Build Coastguard Worker * wrong result, since this number does not corresponds always to
150*8b26181fSAndroid Build Coastguard Worker * the number of packet received by the application. For this reason,
151*8b26181fSAndroid Build Coastguard Worker * in the remote capture we need another variable that takes into
152*8b26181fSAndroid Build Coastguard Worker * account of the number of packets actually received by the
153*8b26181fSAndroid Build Coastguard Worker * application.
154*8b26181fSAndroid Build Coastguard Worker */
155*8b26181fSAndroid Build Coastguard Worker unsigned int TotCapt;
156*8b26181fSAndroid Build Coastguard Worker
157*8b26181fSAndroid Build Coastguard Worker struct pcap_stat stat;
158*8b26181fSAndroid Build Coastguard Worker /* XXX */
159*8b26181fSAndroid Build Coastguard Worker struct pcap *next; /* list of open pcaps that need stuff cleared on close */
160*8b26181fSAndroid Build Coastguard Worker };
161*8b26181fSAndroid Build Coastguard Worker
162*8b26181fSAndroid Build Coastguard Worker /****************************************************
163*8b26181fSAndroid Build Coastguard Worker * *
164*8b26181fSAndroid Build Coastguard Worker * Locally defined functions *
165*8b26181fSAndroid Build Coastguard Worker * *
166*8b26181fSAndroid Build Coastguard Worker ****************************************************/
167*8b26181fSAndroid Build Coastguard Worker static struct pcap_stat *rpcap_stats_rpcap(pcap_t *p, struct pcap_stat *ps, int mode);
168*8b26181fSAndroid Build Coastguard Worker static int pcap_pack_bpffilter(pcap_t *fp, char *sendbuf, int *sendbufidx, struct bpf_program *prog);
169*8b26181fSAndroid Build Coastguard Worker static int pcap_createfilter_norpcappkt(pcap_t *fp, struct bpf_program *prog);
170*8b26181fSAndroid Build Coastguard Worker static int pcap_updatefilter_remote(pcap_t *fp, struct bpf_program *prog);
171*8b26181fSAndroid Build Coastguard Worker static void pcap_save_current_filter_rpcap(pcap_t *fp, const char *filter);
172*8b26181fSAndroid Build Coastguard Worker static int pcap_setfilter_rpcap(pcap_t *fp, struct bpf_program *prog);
173*8b26181fSAndroid Build Coastguard Worker static int pcap_setsampling_remote(pcap_t *fp);
174*8b26181fSAndroid Build Coastguard Worker static int pcap_startcapture_remote(pcap_t *fp);
175*8b26181fSAndroid Build Coastguard Worker static int rpcap_recv_msg_header(SOCKET sock, SSL *, struct rpcap_header *header, char *errbuf);
176*8b26181fSAndroid Build Coastguard Worker static int rpcap_check_msg_ver(SOCKET sock, SSL *, uint8 expected_ver, struct rpcap_header *header, char *errbuf);
177*8b26181fSAndroid Build Coastguard Worker static int rpcap_check_msg_type(SOCKET sock, SSL *, uint8 request_type, struct rpcap_header *header, uint16 *errcode, char *errbuf);
178*8b26181fSAndroid Build Coastguard Worker static int rpcap_process_msg_header(SOCKET sock, SSL *, uint8 ver, uint8 request_type, struct rpcap_header *header, char *errbuf);
179*8b26181fSAndroid Build Coastguard Worker static int rpcap_recv(SOCKET sock, SSL *, void *buffer, size_t toread, uint32 *plen, char *errbuf);
180*8b26181fSAndroid Build Coastguard Worker static void rpcap_msg_err(SOCKET sockctrl, SSL *, uint32 plen, char *remote_errbuf);
181*8b26181fSAndroid Build Coastguard Worker static int rpcap_discard(SOCKET sock, SSL *, uint32 len, char *errbuf);
182*8b26181fSAndroid Build Coastguard Worker static int rpcap_read_packet_msg(struct pcap_rpcap const *, pcap_t *p, size_t size);
183*8b26181fSAndroid Build Coastguard Worker
184*8b26181fSAndroid Build Coastguard Worker /****************************************************
185*8b26181fSAndroid Build Coastguard Worker * *
186*8b26181fSAndroid Build Coastguard Worker * Function bodies *
187*8b26181fSAndroid Build Coastguard Worker * *
188*8b26181fSAndroid Build Coastguard Worker ****************************************************/
189*8b26181fSAndroid Build Coastguard Worker
190*8b26181fSAndroid Build Coastguard Worker /*
191*8b26181fSAndroid Build Coastguard Worker * This function translates (i.e. de-serializes) a 'rpcap_sockaddr'
192*8b26181fSAndroid Build Coastguard Worker * structure from the network byte order to a 'sockaddr_in" or
193*8b26181fSAndroid Build Coastguard Worker * 'sockaddr_in6' structure in the host byte order.
194*8b26181fSAndroid Build Coastguard Worker *
195*8b26181fSAndroid Build Coastguard Worker * It accepts an 'rpcap_sockaddr' structure as it is received from the
196*8b26181fSAndroid Build Coastguard Worker * network, and checks the address family field against various values
197*8b26181fSAndroid Build Coastguard Worker * to see whether it looks like an IPv4 address, an IPv6 address, or
198*8b26181fSAndroid Build Coastguard Worker * neither of those. It checks for multiple values in order to try
199*8b26181fSAndroid Build Coastguard Worker * to handle older rpcap daemons that sent the native OS's 'sockaddr_in'
200*8b26181fSAndroid Build Coastguard Worker * or 'sockaddr_in6' structures over the wire with some members
201*8b26181fSAndroid Build Coastguard Worker * byte-swapped, and to handle the fact that AF_INET6 has different
202*8b26181fSAndroid Build Coastguard Worker * values on different OSes.
203*8b26181fSAndroid Build Coastguard Worker *
204*8b26181fSAndroid Build Coastguard Worker * For IPv4 addresses, it converts the address family to host byte
205*8b26181fSAndroid Build Coastguard Worker * order from network byte order and puts it into the structure,
206*8b26181fSAndroid Build Coastguard Worker * sets the length if a sockaddr structure has a length, converts the
207*8b26181fSAndroid Build Coastguard Worker * port number to host byte order from network byte order and puts
208*8b26181fSAndroid Build Coastguard Worker * it into the structure, copies over the IPv4 address, and zeroes
209*8b26181fSAndroid Build Coastguard Worker * out the zero padding.
210*8b26181fSAndroid Build Coastguard Worker *
211*8b26181fSAndroid Build Coastguard Worker * For IPv6 addresses, it converts the address family to host byte
212*8b26181fSAndroid Build Coastguard Worker * order from network byte order and puts it into the structure,
213*8b26181fSAndroid Build Coastguard Worker * sets the length if a sockaddr structure has a length, converts the
214*8b26181fSAndroid Build Coastguard Worker * port number and flow information to host byte order from network
215*8b26181fSAndroid Build Coastguard Worker * byte order and puts them into the structure, copies over the IPv6
216*8b26181fSAndroid Build Coastguard Worker * address, and converts the scope ID to host byte order from network
217*8b26181fSAndroid Build Coastguard Worker * byte order and puts it into the structure.
218*8b26181fSAndroid Build Coastguard Worker *
219*8b26181fSAndroid Build Coastguard Worker * The function will allocate the 'sockaddrout' variable according to the
220*8b26181fSAndroid Build Coastguard Worker * address family in use. In case the address does not belong to the
221*8b26181fSAndroid Build Coastguard Worker * AF_INET nor AF_INET6 families, 'sockaddrout' is not allocated and a
222*8b26181fSAndroid Build Coastguard Worker * NULL pointer is returned. This usually happens because that address
223*8b26181fSAndroid Build Coastguard Worker * does not exist on the other host, or is of an address family other
224*8b26181fSAndroid Build Coastguard Worker * than AF_INET or AF_INET6, so the RPCAP daemon sent a 'sockaddr_storage'
225*8b26181fSAndroid Build Coastguard Worker * structure containing all 'zero' values.
226*8b26181fSAndroid Build Coastguard Worker *
227*8b26181fSAndroid Build Coastguard Worker * Older RPCAPDs sent the addresses over the wire in the OS's native
228*8b26181fSAndroid Build Coastguard Worker * structure format. For most OSes, this looks like the over-the-wire
229*8b26181fSAndroid Build Coastguard Worker * format, but might have a different value for AF_INET6 than the value
230*8b26181fSAndroid Build Coastguard Worker * on the machine receiving the reply. For OSes with the newer BSD-style
231*8b26181fSAndroid Build Coastguard Worker * sockaddr structures, this has, instead of a 2-byte address family,
232*8b26181fSAndroid Build Coastguard Worker * a 1-byte structure length followed by a 1-byte address family. The
233*8b26181fSAndroid Build Coastguard Worker * RPCAPD code would put the address family in network byte order before
234*8b26181fSAndroid Build Coastguard Worker * sending it; that would set it to 0 on a little-endian machine, as
235*8b26181fSAndroid Build Coastguard Worker * htons() of any value between 1 and 255 would result in a value > 255,
236*8b26181fSAndroid Build Coastguard Worker * with its lower 8 bits zero, so putting that back into a 1-byte field
237*8b26181fSAndroid Build Coastguard Worker * would set it to 0.
238*8b26181fSAndroid Build Coastguard Worker *
239*8b26181fSAndroid Build Coastguard Worker * Therefore, for older RPCAPDs running on an OS with newer BSD-style
240*8b26181fSAndroid Build Coastguard Worker * sockaddr structures, the family field, if treated as a big-endian
241*8b26181fSAndroid Build Coastguard Worker * (network byte order) 16-bit field, would be:
242*8b26181fSAndroid Build Coastguard Worker *
243*8b26181fSAndroid Build Coastguard Worker * (length << 8) | family if sent by a big-endian machine
244*8b26181fSAndroid Build Coastguard Worker * (length << 8) if sent by a little-endian machine
245*8b26181fSAndroid Build Coastguard Worker *
246*8b26181fSAndroid Build Coastguard Worker * For current RPCAPDs, and for older RPCAPDs running on an OS with
247*8b26181fSAndroid Build Coastguard Worker * older BSD-style sockaddr structures, the family field, if treated
248*8b26181fSAndroid Build Coastguard Worker * as a big-endian 16-bit field, would just contain the family.
249*8b26181fSAndroid Build Coastguard Worker *
250*8b26181fSAndroid Build Coastguard Worker * \param sockaddrin: a 'rpcap_sockaddr' pointer to the variable that has
251*8b26181fSAndroid Build Coastguard Worker * to be de-serialized.
252*8b26181fSAndroid Build Coastguard Worker *
253*8b26181fSAndroid Build Coastguard Worker * \param sockaddrout: a 'sockaddr_storage' pointer to the variable that will contain
254*8b26181fSAndroid Build Coastguard Worker * the de-serialized data. The structure returned can be either a 'sockaddr_in' or 'sockaddr_in6'.
255*8b26181fSAndroid Build Coastguard Worker * This variable will be allocated automatically inside this function.
256*8b26181fSAndroid Build Coastguard Worker *
257*8b26181fSAndroid Build Coastguard Worker * \param errbuf: a pointer to a user-allocated buffer (of size PCAP_ERRBUF_SIZE)
258*8b26181fSAndroid Build Coastguard Worker * that will contain the error message (in case there is one).
259*8b26181fSAndroid Build Coastguard Worker *
260*8b26181fSAndroid Build Coastguard Worker * \return '0' if everything is fine, '-1' if some errors occurred. Basically, the error
261*8b26181fSAndroid Build Coastguard Worker * can be only the fact that the malloc() failed to allocate memory.
262*8b26181fSAndroid Build Coastguard Worker * The error message is returned in the 'errbuf' variable, while the deserialized address
263*8b26181fSAndroid Build Coastguard Worker * is returned into the 'sockaddrout' variable.
264*8b26181fSAndroid Build Coastguard Worker *
265*8b26181fSAndroid Build Coastguard Worker * \warning This function supports only AF_INET and AF_INET6 address families.
266*8b26181fSAndroid Build Coastguard Worker *
267*8b26181fSAndroid Build Coastguard Worker * \warning The sockaddrout (if not NULL) must be deallocated by the user.
268*8b26181fSAndroid Build Coastguard Worker */
269*8b26181fSAndroid Build Coastguard Worker
270*8b26181fSAndroid Build Coastguard Worker /*
271*8b26181fSAndroid Build Coastguard Worker * Possible IPv4 family values other than the designated over-the-wire value,
272*8b26181fSAndroid Build Coastguard Worker * which is 2 (because everybody uses 2 for AF_INET4).
273*8b26181fSAndroid Build Coastguard Worker */
274*8b26181fSAndroid Build Coastguard Worker #define SOCKADDR_IN_LEN 16 /* length of struct sockaddr_in */
275*8b26181fSAndroid Build Coastguard Worker #define SOCKADDR_IN6_LEN 28 /* length of struct sockaddr_in6 */
276*8b26181fSAndroid Build Coastguard Worker #define NEW_BSD_AF_INET_BE ((SOCKADDR_IN_LEN << 8) | 2)
277*8b26181fSAndroid Build Coastguard Worker #define NEW_BSD_AF_INET_LE (SOCKADDR_IN_LEN << 8)
278*8b26181fSAndroid Build Coastguard Worker
279*8b26181fSAndroid Build Coastguard Worker /*
280*8b26181fSAndroid Build Coastguard Worker * Possible IPv6 family values other than the designated over-the-wire value,
281*8b26181fSAndroid Build Coastguard Worker * which is 23 (because that's what Windows uses, and most RPCAP servers
282*8b26181fSAndroid Build Coastguard Worker * out there are probably running Windows, as WinPcap includes the server
283*8b26181fSAndroid Build Coastguard Worker * but few if any UN*Xes build and ship it).
284*8b26181fSAndroid Build Coastguard Worker *
285*8b26181fSAndroid Build Coastguard Worker * The new BSD sockaddr structure format was in place before 4.4-Lite, so
286*8b26181fSAndroid Build Coastguard Worker * all the free-software BSDs use it.
287*8b26181fSAndroid Build Coastguard Worker */
288*8b26181fSAndroid Build Coastguard Worker #define NEW_BSD_AF_INET6_BSD_BE ((SOCKADDR_IN6_LEN << 8) | 24) /* NetBSD, OpenBSD, BSD/OS */
289*8b26181fSAndroid Build Coastguard Worker #define NEW_BSD_AF_INET6_FREEBSD_BE ((SOCKADDR_IN6_LEN << 8) | 28) /* FreeBSD, DragonFly BSD */
290*8b26181fSAndroid Build Coastguard Worker #define NEW_BSD_AF_INET6_DARWIN_BE ((SOCKADDR_IN6_LEN << 8) | 30) /* macOS, iOS, anything else Darwin-based */
291*8b26181fSAndroid Build Coastguard Worker #define NEW_BSD_AF_INET6_LE (SOCKADDR_IN6_LEN << 8)
292*8b26181fSAndroid Build Coastguard Worker #define LINUX_AF_INET6 10
293*8b26181fSAndroid Build Coastguard Worker #define HPUX_AF_INET6 22
294*8b26181fSAndroid Build Coastguard Worker #define AIX_AF_INET6 24
295*8b26181fSAndroid Build Coastguard Worker #define SOLARIS_AF_INET6 26
296*8b26181fSAndroid Build Coastguard Worker
297*8b26181fSAndroid Build Coastguard Worker static int
rpcap_deseraddr(struct rpcap_sockaddr * sockaddrin,struct sockaddr_storage ** sockaddrout,char * errbuf)298*8b26181fSAndroid Build Coastguard Worker rpcap_deseraddr(struct rpcap_sockaddr *sockaddrin, struct sockaddr_storage **sockaddrout, char *errbuf)
299*8b26181fSAndroid Build Coastguard Worker {
300*8b26181fSAndroid Build Coastguard Worker /* Warning: we support only AF_INET and AF_INET6 */
301*8b26181fSAndroid Build Coastguard Worker switch (ntohs(sockaddrin->family))
302*8b26181fSAndroid Build Coastguard Worker {
303*8b26181fSAndroid Build Coastguard Worker case RPCAP_AF_INET:
304*8b26181fSAndroid Build Coastguard Worker case NEW_BSD_AF_INET_BE:
305*8b26181fSAndroid Build Coastguard Worker case NEW_BSD_AF_INET_LE:
306*8b26181fSAndroid Build Coastguard Worker {
307*8b26181fSAndroid Build Coastguard Worker struct rpcap_sockaddr_in *sockaddrin_ipv4;
308*8b26181fSAndroid Build Coastguard Worker struct sockaddr_in *sockaddrout_ipv4;
309*8b26181fSAndroid Build Coastguard Worker
310*8b26181fSAndroid Build Coastguard Worker (*sockaddrout) = (struct sockaddr_storage *) malloc(sizeof(struct sockaddr_in));
311*8b26181fSAndroid Build Coastguard Worker if ((*sockaddrout) == NULL)
312*8b26181fSAndroid Build Coastguard Worker {
313*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
314*8b26181fSAndroid Build Coastguard Worker errno, "malloc() failed");
315*8b26181fSAndroid Build Coastguard Worker return -1;
316*8b26181fSAndroid Build Coastguard Worker }
317*8b26181fSAndroid Build Coastguard Worker sockaddrin_ipv4 = (struct rpcap_sockaddr_in *) sockaddrin;
318*8b26181fSAndroid Build Coastguard Worker sockaddrout_ipv4 = (struct sockaddr_in *) (*sockaddrout);
319*8b26181fSAndroid Build Coastguard Worker sockaddrout_ipv4->sin_family = AF_INET;
320*8b26181fSAndroid Build Coastguard Worker sockaddrout_ipv4->sin_port = ntohs(sockaddrin_ipv4->port);
321*8b26181fSAndroid Build Coastguard Worker memcpy(&sockaddrout_ipv4->sin_addr, &sockaddrin_ipv4->addr, sizeof(sockaddrout_ipv4->sin_addr));
322*8b26181fSAndroid Build Coastguard Worker memset(sockaddrout_ipv4->sin_zero, 0, sizeof(sockaddrout_ipv4->sin_zero));
323*8b26181fSAndroid Build Coastguard Worker break;
324*8b26181fSAndroid Build Coastguard Worker }
325*8b26181fSAndroid Build Coastguard Worker
326*8b26181fSAndroid Build Coastguard Worker #ifdef AF_INET6
327*8b26181fSAndroid Build Coastguard Worker case RPCAP_AF_INET6:
328*8b26181fSAndroid Build Coastguard Worker case NEW_BSD_AF_INET6_BSD_BE:
329*8b26181fSAndroid Build Coastguard Worker case NEW_BSD_AF_INET6_FREEBSD_BE:
330*8b26181fSAndroid Build Coastguard Worker case NEW_BSD_AF_INET6_DARWIN_BE:
331*8b26181fSAndroid Build Coastguard Worker case NEW_BSD_AF_INET6_LE:
332*8b26181fSAndroid Build Coastguard Worker case LINUX_AF_INET6:
333*8b26181fSAndroid Build Coastguard Worker case HPUX_AF_INET6:
334*8b26181fSAndroid Build Coastguard Worker case AIX_AF_INET6:
335*8b26181fSAndroid Build Coastguard Worker case SOLARIS_AF_INET6:
336*8b26181fSAndroid Build Coastguard Worker {
337*8b26181fSAndroid Build Coastguard Worker struct rpcap_sockaddr_in6 *sockaddrin_ipv6;
338*8b26181fSAndroid Build Coastguard Worker struct sockaddr_in6 *sockaddrout_ipv6;
339*8b26181fSAndroid Build Coastguard Worker
340*8b26181fSAndroid Build Coastguard Worker (*sockaddrout) = (struct sockaddr_storage *) malloc(sizeof(struct sockaddr_in6));
341*8b26181fSAndroid Build Coastguard Worker if ((*sockaddrout) == NULL)
342*8b26181fSAndroid Build Coastguard Worker {
343*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
344*8b26181fSAndroid Build Coastguard Worker errno, "malloc() failed");
345*8b26181fSAndroid Build Coastguard Worker return -1;
346*8b26181fSAndroid Build Coastguard Worker }
347*8b26181fSAndroid Build Coastguard Worker sockaddrin_ipv6 = (struct rpcap_sockaddr_in6 *) sockaddrin;
348*8b26181fSAndroid Build Coastguard Worker sockaddrout_ipv6 = (struct sockaddr_in6 *) (*sockaddrout);
349*8b26181fSAndroid Build Coastguard Worker sockaddrout_ipv6->sin6_family = AF_INET6;
350*8b26181fSAndroid Build Coastguard Worker sockaddrout_ipv6->sin6_port = ntohs(sockaddrin_ipv6->port);
351*8b26181fSAndroid Build Coastguard Worker sockaddrout_ipv6->sin6_flowinfo = ntohl(sockaddrin_ipv6->flowinfo);
352*8b26181fSAndroid Build Coastguard Worker memcpy(&sockaddrout_ipv6->sin6_addr, &sockaddrin_ipv6->addr, sizeof(sockaddrout_ipv6->sin6_addr));
353*8b26181fSAndroid Build Coastguard Worker sockaddrout_ipv6->sin6_scope_id = ntohl(sockaddrin_ipv6->scope_id);
354*8b26181fSAndroid Build Coastguard Worker break;
355*8b26181fSAndroid Build Coastguard Worker }
356*8b26181fSAndroid Build Coastguard Worker #endif
357*8b26181fSAndroid Build Coastguard Worker
358*8b26181fSAndroid Build Coastguard Worker default:
359*8b26181fSAndroid Build Coastguard Worker /*
360*8b26181fSAndroid Build Coastguard Worker * It is neither AF_INET nor AF_INET6 (or, if the OS doesn't
361*8b26181fSAndroid Build Coastguard Worker * support AF_INET6, it's not AF_INET).
362*8b26181fSAndroid Build Coastguard Worker */
363*8b26181fSAndroid Build Coastguard Worker *sockaddrout = NULL;
364*8b26181fSAndroid Build Coastguard Worker break;
365*8b26181fSAndroid Build Coastguard Worker }
366*8b26181fSAndroid Build Coastguard Worker return 0;
367*8b26181fSAndroid Build Coastguard Worker }
368*8b26181fSAndroid Build Coastguard Worker
369*8b26181fSAndroid Build Coastguard Worker /*
370*8b26181fSAndroid Build Coastguard Worker * This function reads a packet from the network socket. It does not
371*8b26181fSAndroid Build Coastguard Worker * deliver the packet to a pcap_dispatch()/pcap_loop() callback (hence
372*8b26181fSAndroid Build Coastguard Worker * the "nocb" string into its name).
373*8b26181fSAndroid Build Coastguard Worker *
374*8b26181fSAndroid Build Coastguard Worker * This function is called by pcap_read_rpcap().
375*8b26181fSAndroid Build Coastguard Worker *
376*8b26181fSAndroid Build Coastguard Worker * WARNING: By choice, this function does not make use of semaphores. A smarter
377*8b26181fSAndroid Build Coastguard Worker * implementation should put a semaphore into the data thread, and a signal will
378*8b26181fSAndroid Build Coastguard Worker * be raised as soon as there is data into the socket buffer.
379*8b26181fSAndroid Build Coastguard Worker * However this is complicated and it does not bring any advantages when reading
380*8b26181fSAndroid Build Coastguard Worker * from the network, in which network delays can be much more important than
381*8b26181fSAndroid Build Coastguard Worker * these optimizations. Therefore, we chose the following approach:
382*8b26181fSAndroid Build Coastguard Worker * - the 'timeout' chosen by the user is split in two (half on the server side,
383*8b26181fSAndroid Build Coastguard Worker * with the usual meaning, and half on the client side)
384*8b26181fSAndroid Build Coastguard Worker * - this function checks for packets; if there are no packets, it waits for
385*8b26181fSAndroid Build Coastguard Worker * timeout/2 and then it checks again. If packets are still missing, it returns,
386*8b26181fSAndroid Build Coastguard Worker * otherwise it reads packets.
387*8b26181fSAndroid Build Coastguard Worker */
pcap_read_nocb_remote(pcap_t * p,struct pcap_pkthdr * pkt_header,u_char ** pkt_data)388*8b26181fSAndroid Build Coastguard Worker static int pcap_read_nocb_remote(pcap_t *p, struct pcap_pkthdr *pkt_header, u_char **pkt_data)
389*8b26181fSAndroid Build Coastguard Worker {
390*8b26181fSAndroid Build Coastguard Worker struct pcap_rpcap *pr = p->priv; /* structure used when doing a remote live capture */
391*8b26181fSAndroid Build Coastguard Worker struct rpcap_header *header; /* general header according to the RPCAP format */
392*8b26181fSAndroid Build Coastguard Worker struct rpcap_pkthdr *net_pkt_header; /* header of the packet, from the message */
393*8b26181fSAndroid Build Coastguard Worker u_char *net_pkt_data; /* packet data from the message */
394*8b26181fSAndroid Build Coastguard Worker uint32 plen;
395*8b26181fSAndroid Build Coastguard Worker int retval = 0; /* generic return value */
396*8b26181fSAndroid Build Coastguard Worker int msglen;
397*8b26181fSAndroid Build Coastguard Worker
398*8b26181fSAndroid Build Coastguard Worker /* Structures needed for the select() call */
399*8b26181fSAndroid Build Coastguard Worker struct timeval tv; /* maximum time the select() can block waiting for data */
400*8b26181fSAndroid Build Coastguard Worker fd_set rfds; /* set of socket descriptors we have to check */
401*8b26181fSAndroid Build Coastguard Worker
402*8b26181fSAndroid Build Coastguard Worker /*
403*8b26181fSAndroid Build Coastguard Worker * Define the packet buffer timeout, to be used in the select()
404*8b26181fSAndroid Build Coastguard Worker * 'timeout', in pcap_t, is in milliseconds; we have to convert it into sec and microsec
405*8b26181fSAndroid Build Coastguard Worker */
406*8b26181fSAndroid Build Coastguard Worker tv.tv_sec = p->opt.timeout / 1000;
407*8b26181fSAndroid Build Coastguard Worker tv.tv_usec = (suseconds_t)((p->opt.timeout - tv.tv_sec * 1000) * 1000);
408*8b26181fSAndroid Build Coastguard Worker
409*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OPENSSL
410*8b26181fSAndroid Build Coastguard Worker /* Check if we still have bytes available in the last decoded TLS record.
411*8b26181fSAndroid Build Coastguard Worker * If that's the case, we know SSL_read will not block. */
412*8b26181fSAndroid Build Coastguard Worker retval = pr->data_ssl && SSL_pending(pr->data_ssl) > 0;
413*8b26181fSAndroid Build Coastguard Worker #endif
414*8b26181fSAndroid Build Coastguard Worker if (! retval)
415*8b26181fSAndroid Build Coastguard Worker {
416*8b26181fSAndroid Build Coastguard Worker /* Watch out sockdata to see if it has input */
417*8b26181fSAndroid Build Coastguard Worker FD_ZERO(&rfds);
418*8b26181fSAndroid Build Coastguard Worker
419*8b26181fSAndroid Build Coastguard Worker /*
420*8b26181fSAndroid Build Coastguard Worker * 'fp->rmt_sockdata' has always to be set before calling the select(),
421*8b26181fSAndroid Build Coastguard Worker * since it is cleared by the select()
422*8b26181fSAndroid Build Coastguard Worker */
423*8b26181fSAndroid Build Coastguard Worker FD_SET(pr->rmt_sockdata, &rfds);
424*8b26181fSAndroid Build Coastguard Worker
425*8b26181fSAndroid Build Coastguard Worker #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
426*8b26181fSAndroid Build Coastguard Worker retval = 1;
427*8b26181fSAndroid Build Coastguard Worker #else
428*8b26181fSAndroid Build Coastguard Worker retval = select((int) pr->rmt_sockdata + 1, &rfds, NULL, NULL, &tv);
429*8b26181fSAndroid Build Coastguard Worker #endif
430*8b26181fSAndroid Build Coastguard Worker
431*8b26181fSAndroid Build Coastguard Worker if (retval == -1)
432*8b26181fSAndroid Build Coastguard Worker {
433*8b26181fSAndroid Build Coastguard Worker #ifndef _WIN32
434*8b26181fSAndroid Build Coastguard Worker if (errno == EINTR)
435*8b26181fSAndroid Build Coastguard Worker {
436*8b26181fSAndroid Build Coastguard Worker /* Interrupted. */
437*8b26181fSAndroid Build Coastguard Worker return 0;
438*8b26181fSAndroid Build Coastguard Worker }
439*8b26181fSAndroid Build Coastguard Worker #endif
440*8b26181fSAndroid Build Coastguard Worker sock_geterrmsg(p->errbuf, PCAP_ERRBUF_SIZE,
441*8b26181fSAndroid Build Coastguard Worker "select() failed");
442*8b26181fSAndroid Build Coastguard Worker return -1;
443*8b26181fSAndroid Build Coastguard Worker }
444*8b26181fSAndroid Build Coastguard Worker }
445*8b26181fSAndroid Build Coastguard Worker
446*8b26181fSAndroid Build Coastguard Worker /* There is no data waiting, so return '0' */
447*8b26181fSAndroid Build Coastguard Worker if (retval == 0)
448*8b26181fSAndroid Build Coastguard Worker return 0;
449*8b26181fSAndroid Build Coastguard Worker
450*8b26181fSAndroid Build Coastguard Worker /*
451*8b26181fSAndroid Build Coastguard Worker * We have to define 'header' as a pointer to a larger buffer,
452*8b26181fSAndroid Build Coastguard Worker * because in case of UDP we have to read all the message within a single call
453*8b26181fSAndroid Build Coastguard Worker */
454*8b26181fSAndroid Build Coastguard Worker header = (struct rpcap_header *) p->buffer;
455*8b26181fSAndroid Build Coastguard Worker net_pkt_header = (struct rpcap_pkthdr *) ((char *)p->buffer + sizeof(struct rpcap_header));
456*8b26181fSAndroid Build Coastguard Worker net_pkt_data = (u_char *)p->buffer + sizeof(struct rpcap_header) + sizeof(struct rpcap_pkthdr);
457*8b26181fSAndroid Build Coastguard Worker
458*8b26181fSAndroid Build Coastguard Worker if (pr->rmt_flags & PCAP_OPENFLAG_DATATX_UDP)
459*8b26181fSAndroid Build Coastguard Worker {
460*8b26181fSAndroid Build Coastguard Worker /* Read the entire message from the network */
461*8b26181fSAndroid Build Coastguard Worker msglen = sock_recv_dgram(pr->rmt_sockdata, pr->data_ssl, p->buffer,
462*8b26181fSAndroid Build Coastguard Worker p->bufsize, p->errbuf, PCAP_ERRBUF_SIZE);
463*8b26181fSAndroid Build Coastguard Worker if (msglen == -1)
464*8b26181fSAndroid Build Coastguard Worker {
465*8b26181fSAndroid Build Coastguard Worker /* Network error. */
466*8b26181fSAndroid Build Coastguard Worker return -1;
467*8b26181fSAndroid Build Coastguard Worker }
468*8b26181fSAndroid Build Coastguard Worker if (msglen == -3)
469*8b26181fSAndroid Build Coastguard Worker {
470*8b26181fSAndroid Build Coastguard Worker /* Interrupted receive. */
471*8b26181fSAndroid Build Coastguard Worker return 0;
472*8b26181fSAndroid Build Coastguard Worker }
473*8b26181fSAndroid Build Coastguard Worker if ((size_t)msglen < sizeof(struct rpcap_header))
474*8b26181fSAndroid Build Coastguard Worker {
475*8b26181fSAndroid Build Coastguard Worker /*
476*8b26181fSAndroid Build Coastguard Worker * Message is shorter than an rpcap header.
477*8b26181fSAndroid Build Coastguard Worker */
478*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
479*8b26181fSAndroid Build Coastguard Worker "UDP packet message is shorter than an rpcap header");
480*8b26181fSAndroid Build Coastguard Worker return -1;
481*8b26181fSAndroid Build Coastguard Worker }
482*8b26181fSAndroid Build Coastguard Worker plen = ntohl(header->plen);
483*8b26181fSAndroid Build Coastguard Worker if ((size_t)msglen < sizeof(struct rpcap_header) + plen)
484*8b26181fSAndroid Build Coastguard Worker {
485*8b26181fSAndroid Build Coastguard Worker /*
486*8b26181fSAndroid Build Coastguard Worker * Message is shorter than the header claims it
487*8b26181fSAndroid Build Coastguard Worker * is.
488*8b26181fSAndroid Build Coastguard Worker */
489*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
490*8b26181fSAndroid Build Coastguard Worker "UDP packet message is shorter than its rpcap header claims");
491*8b26181fSAndroid Build Coastguard Worker return -1;
492*8b26181fSAndroid Build Coastguard Worker }
493*8b26181fSAndroid Build Coastguard Worker }
494*8b26181fSAndroid Build Coastguard Worker else
495*8b26181fSAndroid Build Coastguard Worker {
496*8b26181fSAndroid Build Coastguard Worker int status;
497*8b26181fSAndroid Build Coastguard Worker
498*8b26181fSAndroid Build Coastguard Worker if ((size_t)p->cc < sizeof(struct rpcap_header))
499*8b26181fSAndroid Build Coastguard Worker {
500*8b26181fSAndroid Build Coastguard Worker /*
501*8b26181fSAndroid Build Coastguard Worker * We haven't read any of the packet header yet.
502*8b26181fSAndroid Build Coastguard Worker * The size we should get is the size of the
503*8b26181fSAndroid Build Coastguard Worker * packet header.
504*8b26181fSAndroid Build Coastguard Worker */
505*8b26181fSAndroid Build Coastguard Worker status = rpcap_read_packet_msg(pr, p, sizeof(struct rpcap_header));
506*8b26181fSAndroid Build Coastguard Worker if (status == -1)
507*8b26181fSAndroid Build Coastguard Worker {
508*8b26181fSAndroid Build Coastguard Worker /* Network error. */
509*8b26181fSAndroid Build Coastguard Worker return -1;
510*8b26181fSAndroid Build Coastguard Worker }
511*8b26181fSAndroid Build Coastguard Worker if (status == -3)
512*8b26181fSAndroid Build Coastguard Worker {
513*8b26181fSAndroid Build Coastguard Worker /* Interrupted receive. */
514*8b26181fSAndroid Build Coastguard Worker return 0;
515*8b26181fSAndroid Build Coastguard Worker }
516*8b26181fSAndroid Build Coastguard Worker }
517*8b26181fSAndroid Build Coastguard Worker
518*8b26181fSAndroid Build Coastguard Worker /*
519*8b26181fSAndroid Build Coastguard Worker * We have the header, so we know how long the
520*8b26181fSAndroid Build Coastguard Worker * message payload is. The size we should get
521*8b26181fSAndroid Build Coastguard Worker * is the size of the packet header plus the
522*8b26181fSAndroid Build Coastguard Worker * size of the payload.
523*8b26181fSAndroid Build Coastguard Worker */
524*8b26181fSAndroid Build Coastguard Worker plen = ntohl(header->plen);
525*8b26181fSAndroid Build Coastguard Worker if (plen > p->bufsize - sizeof(struct rpcap_header))
526*8b26181fSAndroid Build Coastguard Worker {
527*8b26181fSAndroid Build Coastguard Worker /*
528*8b26181fSAndroid Build Coastguard Worker * This is bigger than the largest
529*8b26181fSAndroid Build Coastguard Worker * record we'd expect. (We do it by
530*8b26181fSAndroid Build Coastguard Worker * subtracting in order to avoid an
531*8b26181fSAndroid Build Coastguard Worker * overflow.)
532*8b26181fSAndroid Build Coastguard Worker */
533*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
534*8b26181fSAndroid Build Coastguard Worker "Server sent us a message larger than the largest expected packet message");
535*8b26181fSAndroid Build Coastguard Worker return -1;
536*8b26181fSAndroid Build Coastguard Worker }
537*8b26181fSAndroid Build Coastguard Worker status = rpcap_read_packet_msg(pr, p, sizeof(struct rpcap_header) + plen);
538*8b26181fSAndroid Build Coastguard Worker if (status == -1)
539*8b26181fSAndroid Build Coastguard Worker {
540*8b26181fSAndroid Build Coastguard Worker /* Network error. */
541*8b26181fSAndroid Build Coastguard Worker return -1;
542*8b26181fSAndroid Build Coastguard Worker }
543*8b26181fSAndroid Build Coastguard Worker if (status == -3)
544*8b26181fSAndroid Build Coastguard Worker {
545*8b26181fSAndroid Build Coastguard Worker /* Interrupted receive. */
546*8b26181fSAndroid Build Coastguard Worker return 0;
547*8b26181fSAndroid Build Coastguard Worker }
548*8b26181fSAndroid Build Coastguard Worker
549*8b26181fSAndroid Build Coastguard Worker /*
550*8b26181fSAndroid Build Coastguard Worker * We have the entire message; reset the buffer pointer
551*8b26181fSAndroid Build Coastguard Worker * and count, as the next read should start a new
552*8b26181fSAndroid Build Coastguard Worker * message.
553*8b26181fSAndroid Build Coastguard Worker */
554*8b26181fSAndroid Build Coastguard Worker p->bp = p->buffer;
555*8b26181fSAndroid Build Coastguard Worker p->cc = 0;
556*8b26181fSAndroid Build Coastguard Worker }
557*8b26181fSAndroid Build Coastguard Worker
558*8b26181fSAndroid Build Coastguard Worker /*
559*8b26181fSAndroid Build Coastguard Worker * We have the entire message.
560*8b26181fSAndroid Build Coastguard Worker */
561*8b26181fSAndroid Build Coastguard Worker header->plen = plen;
562*8b26181fSAndroid Build Coastguard Worker
563*8b26181fSAndroid Build Coastguard Worker /*
564*8b26181fSAndroid Build Coastguard Worker * Did the server specify the version we negotiated?
565*8b26181fSAndroid Build Coastguard Worker */
566*8b26181fSAndroid Build Coastguard Worker if (rpcap_check_msg_ver(pr->rmt_sockdata, pr->data_ssl, pr->protocol_version,
567*8b26181fSAndroid Build Coastguard Worker header, p->errbuf) == -1)
568*8b26181fSAndroid Build Coastguard Worker {
569*8b26181fSAndroid Build Coastguard Worker return 0; /* Return 'no packets received' */
570*8b26181fSAndroid Build Coastguard Worker }
571*8b26181fSAndroid Build Coastguard Worker
572*8b26181fSAndroid Build Coastguard Worker /*
573*8b26181fSAndroid Build Coastguard Worker * Is this a RPCAP_MSG_PACKET message?
574*8b26181fSAndroid Build Coastguard Worker */
575*8b26181fSAndroid Build Coastguard Worker if (header->type != RPCAP_MSG_PACKET)
576*8b26181fSAndroid Build Coastguard Worker {
577*8b26181fSAndroid Build Coastguard Worker return 0; /* Return 'no packets received' */
578*8b26181fSAndroid Build Coastguard Worker }
579*8b26181fSAndroid Build Coastguard Worker
580*8b26181fSAndroid Build Coastguard Worker if (ntohl(net_pkt_header->caplen) > plen)
581*8b26181fSAndroid Build Coastguard Worker {
582*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
583*8b26181fSAndroid Build Coastguard Worker "Packet's captured data goes past the end of the received packet message.");
584*8b26181fSAndroid Build Coastguard Worker return -1;
585*8b26181fSAndroid Build Coastguard Worker }
586*8b26181fSAndroid Build Coastguard Worker
587*8b26181fSAndroid Build Coastguard Worker /* Fill in packet header */
588*8b26181fSAndroid Build Coastguard Worker pkt_header->caplen = ntohl(net_pkt_header->caplen);
589*8b26181fSAndroid Build Coastguard Worker pkt_header->len = ntohl(net_pkt_header->len);
590*8b26181fSAndroid Build Coastguard Worker pkt_header->ts.tv_sec = ntohl(net_pkt_header->timestamp_sec);
591*8b26181fSAndroid Build Coastguard Worker pkt_header->ts.tv_usec = ntohl(net_pkt_header->timestamp_usec);
592*8b26181fSAndroid Build Coastguard Worker
593*8b26181fSAndroid Build Coastguard Worker /* Supply a pointer to the beginning of the packet data */
594*8b26181fSAndroid Build Coastguard Worker *pkt_data = net_pkt_data;
595*8b26181fSAndroid Build Coastguard Worker
596*8b26181fSAndroid Build Coastguard Worker /*
597*8b26181fSAndroid Build Coastguard Worker * I don't update the counter of the packets dropped by the network since we're using TCP,
598*8b26181fSAndroid Build Coastguard Worker * therefore no packets are dropped. Just update the number of packets received correctly
599*8b26181fSAndroid Build Coastguard Worker */
600*8b26181fSAndroid Build Coastguard Worker pr->TotCapt++;
601*8b26181fSAndroid Build Coastguard Worker
602*8b26181fSAndroid Build Coastguard Worker if (pr->rmt_flags & PCAP_OPENFLAG_DATATX_UDP)
603*8b26181fSAndroid Build Coastguard Worker {
604*8b26181fSAndroid Build Coastguard Worker unsigned int npkt;
605*8b26181fSAndroid Build Coastguard Worker
606*8b26181fSAndroid Build Coastguard Worker /* We're using UDP, so we need to update the counter of the packets dropped by the network */
607*8b26181fSAndroid Build Coastguard Worker npkt = ntohl(net_pkt_header->npkt);
608*8b26181fSAndroid Build Coastguard Worker
609*8b26181fSAndroid Build Coastguard Worker if (pr->TotCapt != npkt)
610*8b26181fSAndroid Build Coastguard Worker {
611*8b26181fSAndroid Build Coastguard Worker pr->TotNetDrops += (npkt - pr->TotCapt);
612*8b26181fSAndroid Build Coastguard Worker pr->TotCapt = npkt;
613*8b26181fSAndroid Build Coastguard Worker }
614*8b26181fSAndroid Build Coastguard Worker }
615*8b26181fSAndroid Build Coastguard Worker
616*8b26181fSAndroid Build Coastguard Worker /* Packet read successfully */
617*8b26181fSAndroid Build Coastguard Worker return 1;
618*8b26181fSAndroid Build Coastguard Worker }
619*8b26181fSAndroid Build Coastguard Worker
620*8b26181fSAndroid Build Coastguard Worker /*
621*8b26181fSAndroid Build Coastguard Worker * This function reads a packet from the network socket.
622*8b26181fSAndroid Build Coastguard Worker *
623*8b26181fSAndroid Build Coastguard Worker * This function relies on the pcap_read_nocb_remote to deliver packets. The
624*8b26181fSAndroid Build Coastguard Worker * difference, here, is that as soon as a packet is read, it is delivered
625*8b26181fSAndroid Build Coastguard Worker * to the application by means of a callback function.
626*8b26181fSAndroid Build Coastguard Worker */
pcap_read_rpcap(pcap_t * p,int cnt,pcap_handler callback,u_char * user)627*8b26181fSAndroid Build Coastguard Worker static int pcap_read_rpcap(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
628*8b26181fSAndroid Build Coastguard Worker {
629*8b26181fSAndroid Build Coastguard Worker struct pcap_rpcap *pr = p->priv; /* structure used when doing a remote live capture */
630*8b26181fSAndroid Build Coastguard Worker struct pcap_pkthdr pkt_header;
631*8b26181fSAndroid Build Coastguard Worker u_char *pkt_data;
632*8b26181fSAndroid Build Coastguard Worker int n = 0;
633*8b26181fSAndroid Build Coastguard Worker int ret;
634*8b26181fSAndroid Build Coastguard Worker
635*8b26181fSAndroid Build Coastguard Worker /*
636*8b26181fSAndroid Build Coastguard Worker * If this is client-side, and we haven't already started
637*8b26181fSAndroid Build Coastguard Worker * the capture, start it now.
638*8b26181fSAndroid Build Coastguard Worker */
639*8b26181fSAndroid Build Coastguard Worker if (pr->rmt_clientside)
640*8b26181fSAndroid Build Coastguard Worker {
641*8b26181fSAndroid Build Coastguard Worker /* We are on an remote capture */
642*8b26181fSAndroid Build Coastguard Worker if (!pr->rmt_capstarted)
643*8b26181fSAndroid Build Coastguard Worker {
644*8b26181fSAndroid Build Coastguard Worker /*
645*8b26181fSAndroid Build Coastguard Worker * The capture isn't started yet, so try to
646*8b26181fSAndroid Build Coastguard Worker * start it.
647*8b26181fSAndroid Build Coastguard Worker */
648*8b26181fSAndroid Build Coastguard Worker if (pcap_startcapture_remote(p))
649*8b26181fSAndroid Build Coastguard Worker return -1;
650*8b26181fSAndroid Build Coastguard Worker }
651*8b26181fSAndroid Build Coastguard Worker }
652*8b26181fSAndroid Build Coastguard Worker
653*8b26181fSAndroid Build Coastguard Worker /*
654*8b26181fSAndroid Build Coastguard Worker * This can conceivably process more than INT_MAX packets,
655*8b26181fSAndroid Build Coastguard Worker * which would overflow the packet count, causing it either
656*8b26181fSAndroid Build Coastguard Worker * to look like a negative number, and thus cause us to
657*8b26181fSAndroid Build Coastguard Worker * return a value that looks like an error, or overflow
658*8b26181fSAndroid Build Coastguard Worker * back into positive territory, and thus cause us to
659*8b26181fSAndroid Build Coastguard Worker * return a too-low count.
660*8b26181fSAndroid Build Coastguard Worker *
661*8b26181fSAndroid Build Coastguard Worker * Therefore, if the packet count is unlimited, we clip
662*8b26181fSAndroid Build Coastguard Worker * it at INT_MAX; this routine is not expected to
663*8b26181fSAndroid Build Coastguard Worker * process packets indefinitely, so that's not an issue.
664*8b26181fSAndroid Build Coastguard Worker */
665*8b26181fSAndroid Build Coastguard Worker if (PACKET_COUNT_IS_UNLIMITED(cnt))
666*8b26181fSAndroid Build Coastguard Worker cnt = INT_MAX;
667*8b26181fSAndroid Build Coastguard Worker
668*8b26181fSAndroid Build Coastguard Worker while (n < cnt || PACKET_COUNT_IS_UNLIMITED(cnt))
669*8b26181fSAndroid Build Coastguard Worker {
670*8b26181fSAndroid Build Coastguard Worker /*
671*8b26181fSAndroid Build Coastguard Worker * Has "pcap_breakloop()" been called?
672*8b26181fSAndroid Build Coastguard Worker */
673*8b26181fSAndroid Build Coastguard Worker if (p->break_loop) {
674*8b26181fSAndroid Build Coastguard Worker /*
675*8b26181fSAndroid Build Coastguard Worker * Yes - clear the flag that indicates that it
676*8b26181fSAndroid Build Coastguard Worker * has, and return PCAP_ERROR_BREAK to indicate
677*8b26181fSAndroid Build Coastguard Worker * that we were told to break out of the loop.
678*8b26181fSAndroid Build Coastguard Worker */
679*8b26181fSAndroid Build Coastguard Worker p->break_loop = 0;
680*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR_BREAK);
681*8b26181fSAndroid Build Coastguard Worker }
682*8b26181fSAndroid Build Coastguard Worker
683*8b26181fSAndroid Build Coastguard Worker /*
684*8b26181fSAndroid Build Coastguard Worker * Read some packets.
685*8b26181fSAndroid Build Coastguard Worker */
686*8b26181fSAndroid Build Coastguard Worker ret = pcap_read_nocb_remote(p, &pkt_header, &pkt_data);
687*8b26181fSAndroid Build Coastguard Worker if (ret == 1)
688*8b26181fSAndroid Build Coastguard Worker {
689*8b26181fSAndroid Build Coastguard Worker /*
690*8b26181fSAndroid Build Coastguard Worker * We got a packet.
691*8b26181fSAndroid Build Coastguard Worker *
692*8b26181fSAndroid Build Coastguard Worker * Do whatever post-processing is necessary, hand
693*8b26181fSAndroid Build Coastguard Worker * it to the callback, and count it so we can
694*8b26181fSAndroid Build Coastguard Worker * return the count.
695*8b26181fSAndroid Build Coastguard Worker */
696*8b26181fSAndroid Build Coastguard Worker pcap_post_process(p->linktype, pr->byte_swapped,
697*8b26181fSAndroid Build Coastguard Worker &pkt_header, pkt_data);
698*8b26181fSAndroid Build Coastguard Worker (*callback)(user, &pkt_header, pkt_data);
699*8b26181fSAndroid Build Coastguard Worker n++;
700*8b26181fSAndroid Build Coastguard Worker }
701*8b26181fSAndroid Build Coastguard Worker else if (ret == -1)
702*8b26181fSAndroid Build Coastguard Worker {
703*8b26181fSAndroid Build Coastguard Worker /* Error. */
704*8b26181fSAndroid Build Coastguard Worker return ret;
705*8b26181fSAndroid Build Coastguard Worker }
706*8b26181fSAndroid Build Coastguard Worker else
707*8b26181fSAndroid Build Coastguard Worker {
708*8b26181fSAndroid Build Coastguard Worker /*
709*8b26181fSAndroid Build Coastguard Worker * No packet; this could mean that we timed
710*8b26181fSAndroid Build Coastguard Worker * out, or that we got interrupted, or that
711*8b26181fSAndroid Build Coastguard Worker * we got a bad packet.
712*8b26181fSAndroid Build Coastguard Worker *
713*8b26181fSAndroid Build Coastguard Worker * Were we told to break out of the loop?
714*8b26181fSAndroid Build Coastguard Worker */
715*8b26181fSAndroid Build Coastguard Worker if (p->break_loop) {
716*8b26181fSAndroid Build Coastguard Worker /*
717*8b26181fSAndroid Build Coastguard Worker * Yes.
718*8b26181fSAndroid Build Coastguard Worker */
719*8b26181fSAndroid Build Coastguard Worker p->break_loop = 0;
720*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR_BREAK);
721*8b26181fSAndroid Build Coastguard Worker }
722*8b26181fSAndroid Build Coastguard Worker /* No - return the number of packets we've processed. */
723*8b26181fSAndroid Build Coastguard Worker return n;
724*8b26181fSAndroid Build Coastguard Worker }
725*8b26181fSAndroid Build Coastguard Worker }
726*8b26181fSAndroid Build Coastguard Worker return n;
727*8b26181fSAndroid Build Coastguard Worker }
728*8b26181fSAndroid Build Coastguard Worker
729*8b26181fSAndroid Build Coastguard Worker /*
730*8b26181fSAndroid Build Coastguard Worker * This function sends a CLOSE command to the capture server if we're in
731*8b26181fSAndroid Build Coastguard Worker * passive mode and an ENDCAP command to the capture server if we're in
732*8b26181fSAndroid Build Coastguard Worker * active mode.
733*8b26181fSAndroid Build Coastguard Worker *
734*8b26181fSAndroid Build Coastguard Worker * It is called when the user calls pcap_close(). It sends a command
735*8b26181fSAndroid Build Coastguard Worker * to our peer that says 'ok, let's stop capturing'.
736*8b26181fSAndroid Build Coastguard Worker *
737*8b26181fSAndroid Build Coastguard Worker * WARNING: Since we're closing the connection, we do not check for errors.
738*8b26181fSAndroid Build Coastguard Worker */
pcap_cleanup_rpcap(pcap_t * fp)739*8b26181fSAndroid Build Coastguard Worker static void pcap_cleanup_rpcap(pcap_t *fp)
740*8b26181fSAndroid Build Coastguard Worker {
741*8b26181fSAndroid Build Coastguard Worker struct pcap_rpcap *pr = fp->priv; /* structure used when doing a remote live capture */
742*8b26181fSAndroid Build Coastguard Worker struct rpcap_header header; /* header of the RPCAP packet */
743*8b26181fSAndroid Build Coastguard Worker struct activehosts *temp; /* temp var needed to scan the host list chain, to detect if we're in active mode */
744*8b26181fSAndroid Build Coastguard Worker int active = 0; /* active mode or not? */
745*8b26181fSAndroid Build Coastguard Worker
746*8b26181fSAndroid Build Coastguard Worker /* detect if we're in active mode */
747*8b26181fSAndroid Build Coastguard Worker temp = activeHosts;
748*8b26181fSAndroid Build Coastguard Worker while (temp)
749*8b26181fSAndroid Build Coastguard Worker {
750*8b26181fSAndroid Build Coastguard Worker if (temp->sockctrl == pr->rmt_sockctrl)
751*8b26181fSAndroid Build Coastguard Worker {
752*8b26181fSAndroid Build Coastguard Worker active = 1;
753*8b26181fSAndroid Build Coastguard Worker break;
754*8b26181fSAndroid Build Coastguard Worker }
755*8b26181fSAndroid Build Coastguard Worker temp = temp->next;
756*8b26181fSAndroid Build Coastguard Worker }
757*8b26181fSAndroid Build Coastguard Worker
758*8b26181fSAndroid Build Coastguard Worker if (!active)
759*8b26181fSAndroid Build Coastguard Worker {
760*8b26181fSAndroid Build Coastguard Worker rpcap_createhdr(&header, pr->protocol_version,
761*8b26181fSAndroid Build Coastguard Worker RPCAP_MSG_CLOSE, 0, 0);
762*8b26181fSAndroid Build Coastguard Worker
763*8b26181fSAndroid Build Coastguard Worker /*
764*8b26181fSAndroid Build Coastguard Worker * Send the close request; don't report any errors, as
765*8b26181fSAndroid Build Coastguard Worker * we're closing this pcap_t, and have no place to report
766*8b26181fSAndroid Build Coastguard Worker * the error. No reply is sent to this message.
767*8b26181fSAndroid Build Coastguard Worker */
768*8b26181fSAndroid Build Coastguard Worker (void)sock_send(pr->rmt_sockctrl, pr->ctrl_ssl, (char *)&header,
769*8b26181fSAndroid Build Coastguard Worker sizeof(struct rpcap_header), NULL, 0);
770*8b26181fSAndroid Build Coastguard Worker }
771*8b26181fSAndroid Build Coastguard Worker else
772*8b26181fSAndroid Build Coastguard Worker {
773*8b26181fSAndroid Build Coastguard Worker rpcap_createhdr(&header, pr->protocol_version,
774*8b26181fSAndroid Build Coastguard Worker RPCAP_MSG_ENDCAP_REQ, 0, 0);
775*8b26181fSAndroid Build Coastguard Worker
776*8b26181fSAndroid Build Coastguard Worker /*
777*8b26181fSAndroid Build Coastguard Worker * Send the end capture request; don't report any errors,
778*8b26181fSAndroid Build Coastguard Worker * as we're closing this pcap_t, and have no place to
779*8b26181fSAndroid Build Coastguard Worker * report the error.
780*8b26181fSAndroid Build Coastguard Worker */
781*8b26181fSAndroid Build Coastguard Worker if (sock_send(pr->rmt_sockctrl, pr->ctrl_ssl, (char *)&header,
782*8b26181fSAndroid Build Coastguard Worker sizeof(struct rpcap_header), NULL, 0) == 0)
783*8b26181fSAndroid Build Coastguard Worker {
784*8b26181fSAndroid Build Coastguard Worker /*
785*8b26181fSAndroid Build Coastguard Worker * Wait for the answer; don't report any errors,
786*8b26181fSAndroid Build Coastguard Worker * as we're closing this pcap_t, and have no
787*8b26181fSAndroid Build Coastguard Worker * place to report the error.
788*8b26181fSAndroid Build Coastguard Worker */
789*8b26181fSAndroid Build Coastguard Worker if (rpcap_process_msg_header(pr->rmt_sockctrl, pr->ctrl_ssl,
790*8b26181fSAndroid Build Coastguard Worker pr->protocol_version, RPCAP_MSG_ENDCAP_REQ,
791*8b26181fSAndroid Build Coastguard Worker &header, NULL) == 0)
792*8b26181fSAndroid Build Coastguard Worker {
793*8b26181fSAndroid Build Coastguard Worker (void)rpcap_discard(pr->rmt_sockctrl, pr->ctrl_ssl,
794*8b26181fSAndroid Build Coastguard Worker header.plen, NULL);
795*8b26181fSAndroid Build Coastguard Worker }
796*8b26181fSAndroid Build Coastguard Worker }
797*8b26181fSAndroid Build Coastguard Worker }
798*8b26181fSAndroid Build Coastguard Worker
799*8b26181fSAndroid Build Coastguard Worker if (pr->rmt_sockdata)
800*8b26181fSAndroid Build Coastguard Worker {
801*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OPENSSL
802*8b26181fSAndroid Build Coastguard Worker if (pr->data_ssl)
803*8b26181fSAndroid Build Coastguard Worker {
804*8b26181fSAndroid Build Coastguard Worker // Finish using the SSL handle for the data socket.
805*8b26181fSAndroid Build Coastguard Worker // This must be done *before* the socket is closed.
806*8b26181fSAndroid Build Coastguard Worker ssl_finish(pr->data_ssl);
807*8b26181fSAndroid Build Coastguard Worker pr->data_ssl = NULL;
808*8b26181fSAndroid Build Coastguard Worker }
809*8b26181fSAndroid Build Coastguard Worker #endif
810*8b26181fSAndroid Build Coastguard Worker sock_close(pr->rmt_sockdata, NULL, 0);
811*8b26181fSAndroid Build Coastguard Worker pr->rmt_sockdata = 0;
812*8b26181fSAndroid Build Coastguard Worker }
813*8b26181fSAndroid Build Coastguard Worker
814*8b26181fSAndroid Build Coastguard Worker if ((!active) && (pr->rmt_sockctrl))
815*8b26181fSAndroid Build Coastguard Worker {
816*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OPENSSL
817*8b26181fSAndroid Build Coastguard Worker if (pr->ctrl_ssl)
818*8b26181fSAndroid Build Coastguard Worker {
819*8b26181fSAndroid Build Coastguard Worker // Finish using the SSL handle for the control socket.
820*8b26181fSAndroid Build Coastguard Worker // This must be done *before* the socket is closed.
821*8b26181fSAndroid Build Coastguard Worker ssl_finish(pr->ctrl_ssl);
822*8b26181fSAndroid Build Coastguard Worker pr->ctrl_ssl = NULL;
823*8b26181fSAndroid Build Coastguard Worker }
824*8b26181fSAndroid Build Coastguard Worker #endif
825*8b26181fSAndroid Build Coastguard Worker sock_close(pr->rmt_sockctrl, NULL, 0);
826*8b26181fSAndroid Build Coastguard Worker }
827*8b26181fSAndroid Build Coastguard Worker
828*8b26181fSAndroid Build Coastguard Worker pr->rmt_sockctrl = 0;
829*8b26181fSAndroid Build Coastguard Worker pr->ctrl_ssl = NULL;
830*8b26181fSAndroid Build Coastguard Worker
831*8b26181fSAndroid Build Coastguard Worker if (pr->currentfilter)
832*8b26181fSAndroid Build Coastguard Worker {
833*8b26181fSAndroid Build Coastguard Worker free(pr->currentfilter);
834*8b26181fSAndroid Build Coastguard Worker pr->currentfilter = NULL;
835*8b26181fSAndroid Build Coastguard Worker }
836*8b26181fSAndroid Build Coastguard Worker
837*8b26181fSAndroid Build Coastguard Worker pcap_cleanup_live_common(fp);
838*8b26181fSAndroid Build Coastguard Worker
839*8b26181fSAndroid Build Coastguard Worker /* To avoid inconsistencies in the number of sock_init() */
840*8b26181fSAndroid Build Coastguard Worker sock_cleanup();
841*8b26181fSAndroid Build Coastguard Worker }
842*8b26181fSAndroid Build Coastguard Worker
843*8b26181fSAndroid Build Coastguard Worker /*
844*8b26181fSAndroid Build Coastguard Worker * This function retrieves network statistics from our peer;
845*8b26181fSAndroid Build Coastguard Worker * it provides only the standard statistics.
846*8b26181fSAndroid Build Coastguard Worker */
pcap_stats_rpcap(pcap_t * p,struct pcap_stat * ps)847*8b26181fSAndroid Build Coastguard Worker static int pcap_stats_rpcap(pcap_t *p, struct pcap_stat *ps)
848*8b26181fSAndroid Build Coastguard Worker {
849*8b26181fSAndroid Build Coastguard Worker struct pcap_stat *retval;
850*8b26181fSAndroid Build Coastguard Worker
851*8b26181fSAndroid Build Coastguard Worker retval = rpcap_stats_rpcap(p, ps, PCAP_STATS_STANDARD);
852*8b26181fSAndroid Build Coastguard Worker
853*8b26181fSAndroid Build Coastguard Worker if (retval)
854*8b26181fSAndroid Build Coastguard Worker return 0;
855*8b26181fSAndroid Build Coastguard Worker else
856*8b26181fSAndroid Build Coastguard Worker return -1;
857*8b26181fSAndroid Build Coastguard Worker }
858*8b26181fSAndroid Build Coastguard Worker
859*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
860*8b26181fSAndroid Build Coastguard Worker /*
861*8b26181fSAndroid Build Coastguard Worker * This function retrieves network statistics from our peer;
862*8b26181fSAndroid Build Coastguard Worker * it provides the additional statistics supported by pcap_stats_ex().
863*8b26181fSAndroid Build Coastguard Worker */
pcap_stats_ex_rpcap(pcap_t * p,int * pcap_stat_size)864*8b26181fSAndroid Build Coastguard Worker static struct pcap_stat *pcap_stats_ex_rpcap(pcap_t *p, int *pcap_stat_size)
865*8b26181fSAndroid Build Coastguard Worker {
866*8b26181fSAndroid Build Coastguard Worker *pcap_stat_size = sizeof (p->stat);
867*8b26181fSAndroid Build Coastguard Worker
868*8b26181fSAndroid Build Coastguard Worker /* PCAP_STATS_EX (third param) means 'extended pcap_stats()' */
869*8b26181fSAndroid Build Coastguard Worker return (rpcap_stats_rpcap(p, &(p->stat), PCAP_STATS_EX));
870*8b26181fSAndroid Build Coastguard Worker }
871*8b26181fSAndroid Build Coastguard Worker #endif
872*8b26181fSAndroid Build Coastguard Worker
873*8b26181fSAndroid Build Coastguard Worker /*
874*8b26181fSAndroid Build Coastguard Worker * This function retrieves network statistics from our peer. It
875*8b26181fSAndroid Build Coastguard Worker * is used by the two previous functions.
876*8b26181fSAndroid Build Coastguard Worker *
877*8b26181fSAndroid Build Coastguard Worker * It can be called in two modes:
878*8b26181fSAndroid Build Coastguard Worker * - PCAP_STATS_STANDARD: if we want just standard statistics (i.e.,
879*8b26181fSAndroid Build Coastguard Worker * for pcap_stats())
880*8b26181fSAndroid Build Coastguard Worker * - PCAP_STATS_EX: if we want extended statistics (i.e., for
881*8b26181fSAndroid Build Coastguard Worker * pcap_stats_ex())
882*8b26181fSAndroid Build Coastguard Worker *
883*8b26181fSAndroid Build Coastguard Worker * This 'mode' parameter is needed because in pcap_stats() the variable that
884*8b26181fSAndroid Build Coastguard Worker * keeps the statistics is allocated by the user. On Windows, this structure
885*8b26181fSAndroid Build Coastguard Worker * has been extended in order to keep new stats. However, if the user has a
886*8b26181fSAndroid Build Coastguard Worker * smaller structure and it passes it to pcap_stats(), this function will
887*8b26181fSAndroid Build Coastguard Worker * try to fill in more data than the size of the structure, so that memory
888*8b26181fSAndroid Build Coastguard Worker * after the structure will be overwritten.
889*8b26181fSAndroid Build Coastguard Worker *
890*8b26181fSAndroid Build Coastguard Worker * So, we need to know it we have to copy just the standard fields, or the
891*8b26181fSAndroid Build Coastguard Worker * extended fields as well.
892*8b26181fSAndroid Build Coastguard Worker *
893*8b26181fSAndroid Build Coastguard Worker * In case we want to copy the extended fields as well, the problem of
894*8b26181fSAndroid Build Coastguard Worker * memory overflow no longer exists because the structure that's filled
895*8b26181fSAndroid Build Coastguard Worker * in is part of the pcap_t, so that it can be guaranteed to be large
896*8b26181fSAndroid Build Coastguard Worker * enough for the additional statistics.
897*8b26181fSAndroid Build Coastguard Worker *
898*8b26181fSAndroid Build Coastguard Worker * \param p: the pcap_t structure related to the current instance.
899*8b26181fSAndroid Build Coastguard Worker *
900*8b26181fSAndroid Build Coastguard Worker * \param ps: a pointer to a 'pcap_stat' structure, needed for compatibility
901*8b26181fSAndroid Build Coastguard Worker * with pcap_stat(), where the structure is allocated by the user. In case
902*8b26181fSAndroid Build Coastguard Worker * of pcap_stats_ex(), this structure and the function return value point
903*8b26181fSAndroid Build Coastguard Worker * to the same variable.
904*8b26181fSAndroid Build Coastguard Worker *
905*8b26181fSAndroid Build Coastguard Worker * \param mode: one of PCAP_STATS_STANDARD or PCAP_STATS_EX.
906*8b26181fSAndroid Build Coastguard Worker *
907*8b26181fSAndroid Build Coastguard Worker * \return The structure that keeps the statistics, or NULL in case of error.
908*8b26181fSAndroid Build Coastguard Worker * The error string is placed in the pcap_t structure.
909*8b26181fSAndroid Build Coastguard Worker */
rpcap_stats_rpcap(pcap_t * p,struct pcap_stat * ps,int mode)910*8b26181fSAndroid Build Coastguard Worker static struct pcap_stat *rpcap_stats_rpcap(pcap_t *p, struct pcap_stat *ps, int mode)
911*8b26181fSAndroid Build Coastguard Worker {
912*8b26181fSAndroid Build Coastguard Worker struct pcap_rpcap *pr = p->priv; /* structure used when doing a remote live capture */
913*8b26181fSAndroid Build Coastguard Worker struct rpcap_header header; /* header of the RPCAP packet */
914*8b26181fSAndroid Build Coastguard Worker struct rpcap_stats netstats; /* statistics sent on the network */
915*8b26181fSAndroid Build Coastguard Worker uint32 plen; /* data remaining in the message */
916*8b26181fSAndroid Build Coastguard Worker
917*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
918*8b26181fSAndroid Build Coastguard Worker if (mode != PCAP_STATS_STANDARD && mode != PCAP_STATS_EX)
919*8b26181fSAndroid Build Coastguard Worker #else
920*8b26181fSAndroid Build Coastguard Worker if (mode != PCAP_STATS_STANDARD)
921*8b26181fSAndroid Build Coastguard Worker #endif
922*8b26181fSAndroid Build Coastguard Worker {
923*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
924*8b26181fSAndroid Build Coastguard Worker "Invalid stats mode %d", mode);
925*8b26181fSAndroid Build Coastguard Worker return NULL;
926*8b26181fSAndroid Build Coastguard Worker }
927*8b26181fSAndroid Build Coastguard Worker
928*8b26181fSAndroid Build Coastguard Worker /*
929*8b26181fSAndroid Build Coastguard Worker * If the capture has not yet started, we cannot request statistics
930*8b26181fSAndroid Build Coastguard Worker * for the capture from our peer, so we return 0 for all statistics,
931*8b26181fSAndroid Build Coastguard Worker * as nothing's been seen yet.
932*8b26181fSAndroid Build Coastguard Worker */
933*8b26181fSAndroid Build Coastguard Worker if (!pr->rmt_capstarted)
934*8b26181fSAndroid Build Coastguard Worker {
935*8b26181fSAndroid Build Coastguard Worker ps->ps_drop = 0;
936*8b26181fSAndroid Build Coastguard Worker ps->ps_ifdrop = 0;
937*8b26181fSAndroid Build Coastguard Worker ps->ps_recv = 0;
938*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
939*8b26181fSAndroid Build Coastguard Worker if (mode == PCAP_STATS_EX)
940*8b26181fSAndroid Build Coastguard Worker {
941*8b26181fSAndroid Build Coastguard Worker ps->ps_capt = 0;
942*8b26181fSAndroid Build Coastguard Worker ps->ps_sent = 0;
943*8b26181fSAndroid Build Coastguard Worker ps->ps_netdrop = 0;
944*8b26181fSAndroid Build Coastguard Worker }
945*8b26181fSAndroid Build Coastguard Worker #endif /* _WIN32 */
946*8b26181fSAndroid Build Coastguard Worker
947*8b26181fSAndroid Build Coastguard Worker return ps;
948*8b26181fSAndroid Build Coastguard Worker }
949*8b26181fSAndroid Build Coastguard Worker
950*8b26181fSAndroid Build Coastguard Worker rpcap_createhdr(&header, pr->protocol_version,
951*8b26181fSAndroid Build Coastguard Worker RPCAP_MSG_STATS_REQ, 0, 0);
952*8b26181fSAndroid Build Coastguard Worker
953*8b26181fSAndroid Build Coastguard Worker /* Send the PCAP_STATS command */
954*8b26181fSAndroid Build Coastguard Worker if (sock_send(pr->rmt_sockctrl, pr->ctrl_ssl, (char *)&header,
955*8b26181fSAndroid Build Coastguard Worker sizeof(struct rpcap_header), p->errbuf, PCAP_ERRBUF_SIZE) < 0)
956*8b26181fSAndroid Build Coastguard Worker return NULL; /* Unrecoverable network error */
957*8b26181fSAndroid Build Coastguard Worker
958*8b26181fSAndroid Build Coastguard Worker /* Receive and process the reply message header. */
959*8b26181fSAndroid Build Coastguard Worker if (rpcap_process_msg_header(pr->rmt_sockctrl, pr->ctrl_ssl, pr->protocol_version,
960*8b26181fSAndroid Build Coastguard Worker RPCAP_MSG_STATS_REQ, &header, p->errbuf) == -1)
961*8b26181fSAndroid Build Coastguard Worker return NULL; /* Error */
962*8b26181fSAndroid Build Coastguard Worker
963*8b26181fSAndroid Build Coastguard Worker plen = header.plen;
964*8b26181fSAndroid Build Coastguard Worker
965*8b26181fSAndroid Build Coastguard Worker /* Read the reply body */
966*8b26181fSAndroid Build Coastguard Worker if (rpcap_recv(pr->rmt_sockctrl, pr->ctrl_ssl, (char *)&netstats,
967*8b26181fSAndroid Build Coastguard Worker sizeof(struct rpcap_stats), &plen, p->errbuf) == -1)
968*8b26181fSAndroid Build Coastguard Worker goto error;
969*8b26181fSAndroid Build Coastguard Worker
970*8b26181fSAndroid Build Coastguard Worker ps->ps_drop = ntohl(netstats.krnldrop);
971*8b26181fSAndroid Build Coastguard Worker ps->ps_ifdrop = ntohl(netstats.ifdrop);
972*8b26181fSAndroid Build Coastguard Worker ps->ps_recv = ntohl(netstats.ifrecv);
973*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
974*8b26181fSAndroid Build Coastguard Worker if (mode == PCAP_STATS_EX)
975*8b26181fSAndroid Build Coastguard Worker {
976*8b26181fSAndroid Build Coastguard Worker ps->ps_capt = pr->TotCapt;
977*8b26181fSAndroid Build Coastguard Worker ps->ps_netdrop = pr->TotNetDrops;
978*8b26181fSAndroid Build Coastguard Worker ps->ps_sent = ntohl(netstats.svrcapt);
979*8b26181fSAndroid Build Coastguard Worker }
980*8b26181fSAndroid Build Coastguard Worker #endif /* _WIN32 */
981*8b26181fSAndroid Build Coastguard Worker
982*8b26181fSAndroid Build Coastguard Worker /* Discard the rest of the message. */
983*8b26181fSAndroid Build Coastguard Worker if (rpcap_discard(pr->rmt_sockctrl, pr->ctrl_ssl, plen, p->errbuf) == -1)
984*8b26181fSAndroid Build Coastguard Worker goto error_nodiscard;
985*8b26181fSAndroid Build Coastguard Worker
986*8b26181fSAndroid Build Coastguard Worker return ps;
987*8b26181fSAndroid Build Coastguard Worker
988*8b26181fSAndroid Build Coastguard Worker error:
989*8b26181fSAndroid Build Coastguard Worker /*
990*8b26181fSAndroid Build Coastguard Worker * Discard the rest of the message.
991*8b26181fSAndroid Build Coastguard Worker * We already reported an error; if this gets an error, just
992*8b26181fSAndroid Build Coastguard Worker * drive on.
993*8b26181fSAndroid Build Coastguard Worker */
994*8b26181fSAndroid Build Coastguard Worker (void)rpcap_discard(pr->rmt_sockctrl, pr->ctrl_ssl, plen, NULL);
995*8b26181fSAndroid Build Coastguard Worker
996*8b26181fSAndroid Build Coastguard Worker error_nodiscard:
997*8b26181fSAndroid Build Coastguard Worker return NULL;
998*8b26181fSAndroid Build Coastguard Worker }
999*8b26181fSAndroid Build Coastguard Worker
1000*8b26181fSAndroid Build Coastguard Worker /*
1001*8b26181fSAndroid Build Coastguard Worker * This function returns the entry in the list of active hosts for this
1002*8b26181fSAndroid Build Coastguard Worker * active connection (active mode only), or NULL if there is no
1003*8b26181fSAndroid Build Coastguard Worker * active connection or an error occurred. It is just for internal
1004*8b26181fSAndroid Build Coastguard Worker * use.
1005*8b26181fSAndroid Build Coastguard Worker *
1006*8b26181fSAndroid Build Coastguard Worker * \param host: a string that keeps the host name of the host for which we
1007*8b26181fSAndroid Build Coastguard Worker * want to get the socket ID for that active connection.
1008*8b26181fSAndroid Build Coastguard Worker *
1009*8b26181fSAndroid Build Coastguard Worker * \param error: a pointer to an int that is set to 1 if an error occurred
1010*8b26181fSAndroid Build Coastguard Worker * and 0 otherwise.
1011*8b26181fSAndroid Build Coastguard Worker *
1012*8b26181fSAndroid Build Coastguard Worker * \param errbuf: a pointer to a user-allocated buffer (of size
1013*8b26181fSAndroid Build Coastguard Worker * PCAP_ERRBUF_SIZE) that will contain the error message (in case
1014*8b26181fSAndroid Build Coastguard Worker * there is one).
1015*8b26181fSAndroid Build Coastguard Worker *
1016*8b26181fSAndroid Build Coastguard Worker * \return the entry for this host in the list of active connections
1017*8b26181fSAndroid Build Coastguard Worker * if found, NULL if it's not found or there's an error.
1018*8b26181fSAndroid Build Coastguard Worker */
1019*8b26181fSAndroid Build Coastguard Worker static struct activehosts *
rpcap_remoteact_getsock(const char * host,int * error,char * errbuf)1020*8b26181fSAndroid Build Coastguard Worker rpcap_remoteact_getsock(const char *host, int *error, char *errbuf)
1021*8b26181fSAndroid Build Coastguard Worker {
1022*8b26181fSAndroid Build Coastguard Worker struct activehosts *temp; /* temp var needed to scan the host list chain */
1023*8b26181fSAndroid Build Coastguard Worker struct addrinfo hints, *addrinfo, *ai_next; /* temp var needed to translate between hostname to its address */
1024*8b26181fSAndroid Build Coastguard Worker int retval;
1025*8b26181fSAndroid Build Coastguard Worker
1026*8b26181fSAndroid Build Coastguard Worker /* retrieve the network address corresponding to 'host' */
1027*8b26181fSAndroid Build Coastguard Worker addrinfo = NULL;
1028*8b26181fSAndroid Build Coastguard Worker memset(&hints, 0, sizeof(struct addrinfo));
1029*8b26181fSAndroid Build Coastguard Worker hints.ai_family = PF_UNSPEC;
1030*8b26181fSAndroid Build Coastguard Worker hints.ai_socktype = SOCK_STREAM;
1031*8b26181fSAndroid Build Coastguard Worker
1032*8b26181fSAndroid Build Coastguard Worker retval = sock_initaddress(host, NULL, &hints, &addrinfo, errbuf,
1033*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE);
1034*8b26181fSAndroid Build Coastguard Worker if (retval != 0)
1035*8b26181fSAndroid Build Coastguard Worker {
1036*8b26181fSAndroid Build Coastguard Worker *error = 1;
1037*8b26181fSAndroid Build Coastguard Worker return NULL;
1038*8b26181fSAndroid Build Coastguard Worker }
1039*8b26181fSAndroid Build Coastguard Worker
1040*8b26181fSAndroid Build Coastguard Worker temp = activeHosts;
1041*8b26181fSAndroid Build Coastguard Worker
1042*8b26181fSAndroid Build Coastguard Worker while (temp)
1043*8b26181fSAndroid Build Coastguard Worker {
1044*8b26181fSAndroid Build Coastguard Worker ai_next = addrinfo;
1045*8b26181fSAndroid Build Coastguard Worker while (ai_next)
1046*8b26181fSAndroid Build Coastguard Worker {
1047*8b26181fSAndroid Build Coastguard Worker if (sock_cmpaddr(&temp->host, (struct sockaddr_storage *) ai_next->ai_addr) == 0)
1048*8b26181fSAndroid Build Coastguard Worker {
1049*8b26181fSAndroid Build Coastguard Worker *error = 0;
1050*8b26181fSAndroid Build Coastguard Worker freeaddrinfo(addrinfo);
1051*8b26181fSAndroid Build Coastguard Worker return temp;
1052*8b26181fSAndroid Build Coastguard Worker }
1053*8b26181fSAndroid Build Coastguard Worker
1054*8b26181fSAndroid Build Coastguard Worker ai_next = ai_next->ai_next;
1055*8b26181fSAndroid Build Coastguard Worker }
1056*8b26181fSAndroid Build Coastguard Worker temp = temp->next;
1057*8b26181fSAndroid Build Coastguard Worker }
1058*8b26181fSAndroid Build Coastguard Worker
1059*8b26181fSAndroid Build Coastguard Worker if (addrinfo)
1060*8b26181fSAndroid Build Coastguard Worker freeaddrinfo(addrinfo);
1061*8b26181fSAndroid Build Coastguard Worker
1062*8b26181fSAndroid Build Coastguard Worker /*
1063*8b26181fSAndroid Build Coastguard Worker * The host for which you want to get the socket ID does not have an
1064*8b26181fSAndroid Build Coastguard Worker * active connection.
1065*8b26181fSAndroid Build Coastguard Worker */
1066*8b26181fSAndroid Build Coastguard Worker *error = 0;
1067*8b26181fSAndroid Build Coastguard Worker return NULL;
1068*8b26181fSAndroid Build Coastguard Worker }
1069*8b26181fSAndroid Build Coastguard Worker
1070*8b26181fSAndroid Build Coastguard Worker /*
1071*8b26181fSAndroid Build Coastguard Worker * This function starts a remote capture.
1072*8b26181fSAndroid Build Coastguard Worker *
1073*8b26181fSAndroid Build Coastguard Worker * This function is required since the RPCAP protocol decouples the 'open'
1074*8b26181fSAndroid Build Coastguard Worker * from the 'start capture' functions.
1075*8b26181fSAndroid Build Coastguard Worker * This function takes all the parameters needed (which have been stored
1076*8b26181fSAndroid Build Coastguard Worker * into the pcap_t structure) and sends them to the server.
1077*8b26181fSAndroid Build Coastguard Worker *
1078*8b26181fSAndroid Build Coastguard Worker * \param fp: the pcap_t descriptor of the device currently open.
1079*8b26181fSAndroid Build Coastguard Worker *
1080*8b26181fSAndroid Build Coastguard Worker * \return '0' if everything is fine, '-1' otherwise. The error message
1081*8b26181fSAndroid Build Coastguard Worker * (if one) is returned into the 'errbuf' field of the pcap_t structure.
1082*8b26181fSAndroid Build Coastguard Worker */
pcap_startcapture_remote(pcap_t * fp)1083*8b26181fSAndroid Build Coastguard Worker static int pcap_startcapture_remote(pcap_t *fp)
1084*8b26181fSAndroid Build Coastguard Worker {
1085*8b26181fSAndroid Build Coastguard Worker struct pcap_rpcap *pr = fp->priv; /* structure used when doing a remote live capture */
1086*8b26181fSAndroid Build Coastguard Worker char sendbuf[RPCAP_NETBUF_SIZE]; /* temporary buffer in which data to be sent is buffered */
1087*8b26181fSAndroid Build Coastguard Worker int sendbufidx = 0; /* index which keeps the number of bytes currently buffered */
1088*8b26181fSAndroid Build Coastguard Worker uint16 portdata = 0; /* temp variable needed to keep the network port for the data connection */
1089*8b26181fSAndroid Build Coastguard Worker uint32 plen;
1090*8b26181fSAndroid Build Coastguard Worker int active = 0; /* '1' if we're in active mode */
1091*8b26181fSAndroid Build Coastguard Worker struct activehosts *temp; /* temp var needed to scan the host list chain, to detect if we're in active mode */
1092*8b26181fSAndroid Build Coastguard Worker char host[INET6_ADDRSTRLEN + 1]; /* numeric name of the other host */
1093*8b26181fSAndroid Build Coastguard Worker
1094*8b26181fSAndroid Build Coastguard Worker /* socket-related variables*/
1095*8b26181fSAndroid Build Coastguard Worker struct addrinfo hints; /* temp, needed to open a socket connection */
1096*8b26181fSAndroid Build Coastguard Worker struct addrinfo *addrinfo; /* temp, needed to open a socket connection */
1097*8b26181fSAndroid Build Coastguard Worker SOCKET sockdata = 0; /* socket descriptor of the data connection */
1098*8b26181fSAndroid Build Coastguard Worker struct sockaddr_storage saddr; /* temp, needed to retrieve the network data port chosen on the local machine */
1099*8b26181fSAndroid Build Coastguard Worker socklen_t saddrlen; /* temp, needed to retrieve the network data port chosen on the local machine */
1100*8b26181fSAndroid Build Coastguard Worker int ai_family; /* temp, keeps the address family used by the control connection */
1101*8b26181fSAndroid Build Coastguard Worker struct sockaddr_in *sin4;
1102*8b26181fSAndroid Build Coastguard Worker struct sockaddr_in6 *sin6;
1103*8b26181fSAndroid Build Coastguard Worker
1104*8b26181fSAndroid Build Coastguard Worker /* RPCAP-related variables*/
1105*8b26181fSAndroid Build Coastguard Worker struct rpcap_header header; /* header of the RPCAP packet */
1106*8b26181fSAndroid Build Coastguard Worker struct rpcap_startcapreq *startcapreq; /* start capture request message */
1107*8b26181fSAndroid Build Coastguard Worker struct rpcap_startcapreply startcapreply; /* start capture reply message */
1108*8b26181fSAndroid Build Coastguard Worker
1109*8b26181fSAndroid Build Coastguard Worker /* Variables related to the buffer setting */
1110*8b26181fSAndroid Build Coastguard Worker int res;
1111*8b26181fSAndroid Build Coastguard Worker socklen_t itemp;
1112*8b26181fSAndroid Build Coastguard Worker int sockbufsize = 0;
1113*8b26181fSAndroid Build Coastguard Worker uint32 server_sockbufsize;
1114*8b26181fSAndroid Build Coastguard Worker
1115*8b26181fSAndroid Build Coastguard Worker // Take the opportunity to clear pr->data_ssl before any goto error,
1116*8b26181fSAndroid Build Coastguard Worker // as it seems p->priv is not zeroed after its malloced.
1117*8b26181fSAndroid Build Coastguard Worker // XXX - it now should be, as it's allocated by pcap_alloc_pcap_t(),
1118*8b26181fSAndroid Build Coastguard Worker // which does a calloc().
1119*8b26181fSAndroid Build Coastguard Worker pr->data_ssl = NULL;
1120*8b26181fSAndroid Build Coastguard Worker
1121*8b26181fSAndroid Build Coastguard Worker /*
1122*8b26181fSAndroid Build Coastguard Worker * Let's check if sampling has been required.
1123*8b26181fSAndroid Build Coastguard Worker * If so, let's set it first
1124*8b26181fSAndroid Build Coastguard Worker */
1125*8b26181fSAndroid Build Coastguard Worker if (pcap_setsampling_remote(fp) != 0)
1126*8b26181fSAndroid Build Coastguard Worker return -1;
1127*8b26181fSAndroid Build Coastguard Worker
1128*8b26181fSAndroid Build Coastguard Worker /* detect if we're in active mode */
1129*8b26181fSAndroid Build Coastguard Worker temp = activeHosts;
1130*8b26181fSAndroid Build Coastguard Worker while (temp)
1131*8b26181fSAndroid Build Coastguard Worker {
1132*8b26181fSAndroid Build Coastguard Worker if (temp->sockctrl == pr->rmt_sockctrl)
1133*8b26181fSAndroid Build Coastguard Worker {
1134*8b26181fSAndroid Build Coastguard Worker active = 1;
1135*8b26181fSAndroid Build Coastguard Worker break;
1136*8b26181fSAndroid Build Coastguard Worker }
1137*8b26181fSAndroid Build Coastguard Worker temp = temp->next;
1138*8b26181fSAndroid Build Coastguard Worker }
1139*8b26181fSAndroid Build Coastguard Worker
1140*8b26181fSAndroid Build Coastguard Worker addrinfo = NULL;
1141*8b26181fSAndroid Build Coastguard Worker
1142*8b26181fSAndroid Build Coastguard Worker /*
1143*8b26181fSAndroid Build Coastguard Worker * Gets the complete sockaddr structure used in the ctrl connection
1144*8b26181fSAndroid Build Coastguard Worker * This is needed to get the address family of the control socket
1145*8b26181fSAndroid Build Coastguard Worker * Tip: I cannot save the ai_family of the ctrl sock in the pcap_t struct,
1146*8b26181fSAndroid Build Coastguard Worker * since the ctrl socket can already be open in case of active mode;
1147*8b26181fSAndroid Build Coastguard Worker * so I would have to call getpeername() anyway
1148*8b26181fSAndroid Build Coastguard Worker */
1149*8b26181fSAndroid Build Coastguard Worker saddrlen = sizeof(struct sockaddr_storage);
1150*8b26181fSAndroid Build Coastguard Worker if (getpeername(pr->rmt_sockctrl, (struct sockaddr *) &saddr, &saddrlen) == -1)
1151*8b26181fSAndroid Build Coastguard Worker {
1152*8b26181fSAndroid Build Coastguard Worker sock_geterrmsg(fp->errbuf, PCAP_ERRBUF_SIZE,
1153*8b26181fSAndroid Build Coastguard Worker "getsockname() failed");
1154*8b26181fSAndroid Build Coastguard Worker goto error_nodiscard;
1155*8b26181fSAndroid Build Coastguard Worker }
1156*8b26181fSAndroid Build Coastguard Worker ai_family = ((struct sockaddr_storage *) &saddr)->ss_family;
1157*8b26181fSAndroid Build Coastguard Worker
1158*8b26181fSAndroid Build Coastguard Worker /* Get the numeric address of the remote host we are connected to */
1159*8b26181fSAndroid Build Coastguard Worker if (getnameinfo((struct sockaddr *) &saddr, saddrlen, host,
1160*8b26181fSAndroid Build Coastguard Worker sizeof(host), NULL, 0, NI_NUMERICHOST))
1161*8b26181fSAndroid Build Coastguard Worker {
1162*8b26181fSAndroid Build Coastguard Worker sock_geterrmsg(fp->errbuf, PCAP_ERRBUF_SIZE,
1163*8b26181fSAndroid Build Coastguard Worker "getnameinfo() failed");
1164*8b26181fSAndroid Build Coastguard Worker goto error_nodiscard;
1165*8b26181fSAndroid Build Coastguard Worker }
1166*8b26181fSAndroid Build Coastguard Worker
1167*8b26181fSAndroid Build Coastguard Worker /*
1168*8b26181fSAndroid Build Coastguard Worker * Data connection is opened by the server toward the client if:
1169*8b26181fSAndroid Build Coastguard Worker * - we're using TCP, and the user wants us to be in active mode
1170*8b26181fSAndroid Build Coastguard Worker * - we're using UDP
1171*8b26181fSAndroid Build Coastguard Worker */
1172*8b26181fSAndroid Build Coastguard Worker if ((active) || (pr->rmt_flags & PCAP_OPENFLAG_DATATX_UDP))
1173*8b26181fSAndroid Build Coastguard Worker {
1174*8b26181fSAndroid Build Coastguard Worker /*
1175*8b26181fSAndroid Build Coastguard Worker * We have to create a new socket to receive packets
1176*8b26181fSAndroid Build Coastguard Worker * We have to do that immediately, since we have to tell the other
1177*8b26181fSAndroid Build Coastguard Worker * end which network port we picked up
1178*8b26181fSAndroid Build Coastguard Worker */
1179*8b26181fSAndroid Build Coastguard Worker memset(&hints, 0, sizeof(struct addrinfo));
1180*8b26181fSAndroid Build Coastguard Worker /* TEMP addrinfo is NULL in case of active */
1181*8b26181fSAndroid Build Coastguard Worker hints.ai_family = ai_family; /* Use the same address family of the control socket */
1182*8b26181fSAndroid Build Coastguard Worker hints.ai_socktype = (pr->rmt_flags & PCAP_OPENFLAG_DATATX_UDP) ? SOCK_DGRAM : SOCK_STREAM;
1183*8b26181fSAndroid Build Coastguard Worker hints.ai_flags = AI_PASSIVE; /* Data connection is opened by the server toward the client */
1184*8b26181fSAndroid Build Coastguard Worker
1185*8b26181fSAndroid Build Coastguard Worker /* Let's the server pick up a free network port for us */
1186*8b26181fSAndroid Build Coastguard Worker if (sock_initaddress(NULL, NULL, &hints, &addrinfo, fp->errbuf, PCAP_ERRBUF_SIZE) == -1)
1187*8b26181fSAndroid Build Coastguard Worker goto error_nodiscard;
1188*8b26181fSAndroid Build Coastguard Worker
1189*8b26181fSAndroid Build Coastguard Worker if ((sockdata = sock_open(NULL, addrinfo, SOCKOPEN_SERVER,
1190*8b26181fSAndroid Build Coastguard Worker 1 /* max 1 connection in queue */, fp->errbuf, PCAP_ERRBUF_SIZE)) == INVALID_SOCKET)
1191*8b26181fSAndroid Build Coastguard Worker goto error_nodiscard;
1192*8b26181fSAndroid Build Coastguard Worker
1193*8b26181fSAndroid Build Coastguard Worker /* addrinfo is no longer used */
1194*8b26181fSAndroid Build Coastguard Worker freeaddrinfo(addrinfo);
1195*8b26181fSAndroid Build Coastguard Worker addrinfo = NULL;
1196*8b26181fSAndroid Build Coastguard Worker
1197*8b26181fSAndroid Build Coastguard Worker /* get the complete sockaddr structure used in the data connection */
1198*8b26181fSAndroid Build Coastguard Worker saddrlen = sizeof(struct sockaddr_storage);
1199*8b26181fSAndroid Build Coastguard Worker if (getsockname(sockdata, (struct sockaddr *) &saddr, &saddrlen) == -1)
1200*8b26181fSAndroid Build Coastguard Worker {
1201*8b26181fSAndroid Build Coastguard Worker sock_geterrmsg(fp->errbuf, PCAP_ERRBUF_SIZE,
1202*8b26181fSAndroid Build Coastguard Worker "getsockname() failed");
1203*8b26181fSAndroid Build Coastguard Worker goto error_nodiscard;
1204*8b26181fSAndroid Build Coastguard Worker }
1205*8b26181fSAndroid Build Coastguard Worker
1206*8b26181fSAndroid Build Coastguard Worker switch (saddr.ss_family) {
1207*8b26181fSAndroid Build Coastguard Worker
1208*8b26181fSAndroid Build Coastguard Worker case AF_INET:
1209*8b26181fSAndroid Build Coastguard Worker sin4 = (struct sockaddr_in *)&saddr;
1210*8b26181fSAndroid Build Coastguard Worker portdata = sin4->sin_port;
1211*8b26181fSAndroid Build Coastguard Worker break;
1212*8b26181fSAndroid Build Coastguard Worker
1213*8b26181fSAndroid Build Coastguard Worker case AF_INET6:
1214*8b26181fSAndroid Build Coastguard Worker sin6 = (struct sockaddr_in6 *)&saddr;
1215*8b26181fSAndroid Build Coastguard Worker portdata = sin6->sin6_port;
1216*8b26181fSAndroid Build Coastguard Worker break;
1217*8b26181fSAndroid Build Coastguard Worker
1218*8b26181fSAndroid Build Coastguard Worker default:
1219*8b26181fSAndroid Build Coastguard Worker snprintf(fp->errbuf, PCAP_ERRBUF_SIZE,
1220*8b26181fSAndroid Build Coastguard Worker "Local address has unknown address family %u",
1221*8b26181fSAndroid Build Coastguard Worker saddr.ss_family);
1222*8b26181fSAndroid Build Coastguard Worker goto error_nodiscard;
1223*8b26181fSAndroid Build Coastguard Worker }
1224*8b26181fSAndroid Build Coastguard Worker }
1225*8b26181fSAndroid Build Coastguard Worker
1226*8b26181fSAndroid Build Coastguard Worker /*
1227*8b26181fSAndroid Build Coastguard Worker * Now it's time to start playing with the RPCAP protocol
1228*8b26181fSAndroid Build Coastguard Worker * RPCAP start capture command: create the request message
1229*8b26181fSAndroid Build Coastguard Worker */
1230*8b26181fSAndroid Build Coastguard Worker if (sock_bufferize(NULL, sizeof(struct rpcap_header), NULL,
1231*8b26181fSAndroid Build Coastguard Worker &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, fp->errbuf, PCAP_ERRBUF_SIZE))
1232*8b26181fSAndroid Build Coastguard Worker goto error_nodiscard;
1233*8b26181fSAndroid Build Coastguard Worker
1234*8b26181fSAndroid Build Coastguard Worker rpcap_createhdr((struct rpcap_header *) sendbuf,
1235*8b26181fSAndroid Build Coastguard Worker pr->protocol_version, RPCAP_MSG_STARTCAP_REQ, 0,
1236*8b26181fSAndroid Build Coastguard Worker sizeof(struct rpcap_startcapreq) + sizeof(struct rpcap_filter) + fp->fcode.bf_len * sizeof(struct rpcap_filterbpf_insn));
1237*8b26181fSAndroid Build Coastguard Worker
1238*8b26181fSAndroid Build Coastguard Worker /* Fill the structure needed to open an adapter remotely */
1239*8b26181fSAndroid Build Coastguard Worker startcapreq = (struct rpcap_startcapreq *) &sendbuf[sendbufidx];
1240*8b26181fSAndroid Build Coastguard Worker
1241*8b26181fSAndroid Build Coastguard Worker if (sock_bufferize(NULL, sizeof(struct rpcap_startcapreq), NULL,
1242*8b26181fSAndroid Build Coastguard Worker &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, fp->errbuf, PCAP_ERRBUF_SIZE))
1243*8b26181fSAndroid Build Coastguard Worker goto error_nodiscard;
1244*8b26181fSAndroid Build Coastguard Worker
1245*8b26181fSAndroid Build Coastguard Worker memset(startcapreq, 0, sizeof(struct rpcap_startcapreq));
1246*8b26181fSAndroid Build Coastguard Worker
1247*8b26181fSAndroid Build Coastguard Worker /* By default, apply half the timeout on one side, half of the other */
1248*8b26181fSAndroid Build Coastguard Worker fp->opt.timeout = fp->opt.timeout / 2;
1249*8b26181fSAndroid Build Coastguard Worker startcapreq->read_timeout = htonl(fp->opt.timeout);
1250*8b26181fSAndroid Build Coastguard Worker
1251*8b26181fSAndroid Build Coastguard Worker /* portdata on the openreq is meaningful only if we're in active mode */
1252*8b26181fSAndroid Build Coastguard Worker if ((active) || (pr->rmt_flags & PCAP_OPENFLAG_DATATX_UDP))
1253*8b26181fSAndroid Build Coastguard Worker {
1254*8b26181fSAndroid Build Coastguard Worker startcapreq->portdata = portdata;
1255*8b26181fSAndroid Build Coastguard Worker }
1256*8b26181fSAndroid Build Coastguard Worker
1257*8b26181fSAndroid Build Coastguard Worker startcapreq->snaplen = htonl(fp->snapshot);
1258*8b26181fSAndroid Build Coastguard Worker startcapreq->flags = 0;
1259*8b26181fSAndroid Build Coastguard Worker
1260*8b26181fSAndroid Build Coastguard Worker if (pr->rmt_flags & PCAP_OPENFLAG_PROMISCUOUS)
1261*8b26181fSAndroid Build Coastguard Worker startcapreq->flags |= RPCAP_STARTCAPREQ_FLAG_PROMISC;
1262*8b26181fSAndroid Build Coastguard Worker if (pr->rmt_flags & PCAP_OPENFLAG_DATATX_UDP)
1263*8b26181fSAndroid Build Coastguard Worker startcapreq->flags |= RPCAP_STARTCAPREQ_FLAG_DGRAM;
1264*8b26181fSAndroid Build Coastguard Worker if (active)
1265*8b26181fSAndroid Build Coastguard Worker startcapreq->flags |= RPCAP_STARTCAPREQ_FLAG_SERVEROPEN;
1266*8b26181fSAndroid Build Coastguard Worker
1267*8b26181fSAndroid Build Coastguard Worker startcapreq->flags = htons(startcapreq->flags);
1268*8b26181fSAndroid Build Coastguard Worker
1269*8b26181fSAndroid Build Coastguard Worker /* Pack the capture filter */
1270*8b26181fSAndroid Build Coastguard Worker if (pcap_pack_bpffilter(fp, &sendbuf[sendbufidx], &sendbufidx, &fp->fcode))
1271*8b26181fSAndroid Build Coastguard Worker goto error_nodiscard;
1272*8b26181fSAndroid Build Coastguard Worker
1273*8b26181fSAndroid Build Coastguard Worker if (sock_send(pr->rmt_sockctrl, pr->ctrl_ssl, sendbuf, sendbufidx, fp->errbuf,
1274*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE) < 0)
1275*8b26181fSAndroid Build Coastguard Worker goto error_nodiscard;
1276*8b26181fSAndroid Build Coastguard Worker
1277*8b26181fSAndroid Build Coastguard Worker /* Receive and process the reply message header. */
1278*8b26181fSAndroid Build Coastguard Worker if (rpcap_process_msg_header(pr->rmt_sockctrl, pr->ctrl_ssl, pr->protocol_version,
1279*8b26181fSAndroid Build Coastguard Worker RPCAP_MSG_STARTCAP_REQ, &header, fp->errbuf) == -1)
1280*8b26181fSAndroid Build Coastguard Worker goto error_nodiscard;
1281*8b26181fSAndroid Build Coastguard Worker
1282*8b26181fSAndroid Build Coastguard Worker plen = header.plen;
1283*8b26181fSAndroid Build Coastguard Worker
1284*8b26181fSAndroid Build Coastguard Worker if (rpcap_recv(pr->rmt_sockctrl, pr->ctrl_ssl, (char *)&startcapreply,
1285*8b26181fSAndroid Build Coastguard Worker sizeof(struct rpcap_startcapreply), &plen, fp->errbuf) == -1)
1286*8b26181fSAndroid Build Coastguard Worker goto error;
1287*8b26181fSAndroid Build Coastguard Worker
1288*8b26181fSAndroid Build Coastguard Worker /*
1289*8b26181fSAndroid Build Coastguard Worker * In case of UDP data stream, the connection is always opened by the daemon
1290*8b26181fSAndroid Build Coastguard Worker * So, this case is already covered by the code above.
1291*8b26181fSAndroid Build Coastguard Worker * Now, we have still to handle TCP connections, because:
1292*8b26181fSAndroid Build Coastguard Worker * - if we're in active mode, we have to wait for a remote connection
1293*8b26181fSAndroid Build Coastguard Worker * - if we're in passive more, we have to start a connection
1294*8b26181fSAndroid Build Coastguard Worker *
1295*8b26181fSAndroid Build Coastguard Worker * We have to do he job in two steps because in case we're opening a TCP connection, we have
1296*8b26181fSAndroid Build Coastguard Worker * to tell the port we're using to the remote side; in case we're accepting a TCP
1297*8b26181fSAndroid Build Coastguard Worker * connection, we have to wait this info from the remote side.
1298*8b26181fSAndroid Build Coastguard Worker */
1299*8b26181fSAndroid Build Coastguard Worker if (!(pr->rmt_flags & PCAP_OPENFLAG_DATATX_UDP))
1300*8b26181fSAndroid Build Coastguard Worker {
1301*8b26181fSAndroid Build Coastguard Worker if (!active)
1302*8b26181fSAndroid Build Coastguard Worker {
1303*8b26181fSAndroid Build Coastguard Worker char portstring[PCAP_BUF_SIZE];
1304*8b26181fSAndroid Build Coastguard Worker
1305*8b26181fSAndroid Build Coastguard Worker memset(&hints, 0, sizeof(struct addrinfo));
1306*8b26181fSAndroid Build Coastguard Worker hints.ai_family = ai_family; /* Use the same address family of the control socket */
1307*8b26181fSAndroid Build Coastguard Worker hints.ai_socktype = (pr->rmt_flags & PCAP_OPENFLAG_DATATX_UDP) ? SOCK_DGRAM : SOCK_STREAM;
1308*8b26181fSAndroid Build Coastguard Worker snprintf(portstring, PCAP_BUF_SIZE, "%d", ntohs(startcapreply.portdata));
1309*8b26181fSAndroid Build Coastguard Worker
1310*8b26181fSAndroid Build Coastguard Worker /* Let's the server pick up a free network port for us */
1311*8b26181fSAndroid Build Coastguard Worker if (sock_initaddress(host, portstring, &hints, &addrinfo, fp->errbuf, PCAP_ERRBUF_SIZE) == -1)
1312*8b26181fSAndroid Build Coastguard Worker goto error;
1313*8b26181fSAndroid Build Coastguard Worker
1314*8b26181fSAndroid Build Coastguard Worker if ((sockdata = sock_open(host, addrinfo, SOCKOPEN_CLIENT, 0, fp->errbuf, PCAP_ERRBUF_SIZE)) == INVALID_SOCKET)
1315*8b26181fSAndroid Build Coastguard Worker goto error;
1316*8b26181fSAndroid Build Coastguard Worker
1317*8b26181fSAndroid Build Coastguard Worker /* addrinfo is no longer used */
1318*8b26181fSAndroid Build Coastguard Worker freeaddrinfo(addrinfo);
1319*8b26181fSAndroid Build Coastguard Worker addrinfo = NULL;
1320*8b26181fSAndroid Build Coastguard Worker }
1321*8b26181fSAndroid Build Coastguard Worker else
1322*8b26181fSAndroid Build Coastguard Worker {
1323*8b26181fSAndroid Build Coastguard Worker SOCKET socktemp; /* We need another socket, since we're going to accept() a connection */
1324*8b26181fSAndroid Build Coastguard Worker
1325*8b26181fSAndroid Build Coastguard Worker /* Connection creation */
1326*8b26181fSAndroid Build Coastguard Worker saddrlen = sizeof(struct sockaddr_storage);
1327*8b26181fSAndroid Build Coastguard Worker
1328*8b26181fSAndroid Build Coastguard Worker socktemp = accept(sockdata, (struct sockaddr *) &saddr, &saddrlen);
1329*8b26181fSAndroid Build Coastguard Worker
1330*8b26181fSAndroid Build Coastguard Worker if (socktemp == INVALID_SOCKET)
1331*8b26181fSAndroid Build Coastguard Worker {
1332*8b26181fSAndroid Build Coastguard Worker sock_geterrmsg(fp->errbuf, PCAP_ERRBUF_SIZE,
1333*8b26181fSAndroid Build Coastguard Worker "accept() failed");
1334*8b26181fSAndroid Build Coastguard Worker goto error;
1335*8b26181fSAndroid Build Coastguard Worker }
1336*8b26181fSAndroid Build Coastguard Worker
1337*8b26181fSAndroid Build Coastguard Worker /* Now that I accepted the connection, the server socket is no longer needed */
1338*8b26181fSAndroid Build Coastguard Worker sock_close(sockdata, fp->errbuf, PCAP_ERRBUF_SIZE);
1339*8b26181fSAndroid Build Coastguard Worker sockdata = socktemp;
1340*8b26181fSAndroid Build Coastguard Worker }
1341*8b26181fSAndroid Build Coastguard Worker }
1342*8b26181fSAndroid Build Coastguard Worker
1343*8b26181fSAndroid Build Coastguard Worker /* Let's save the socket of the data connection */
1344*8b26181fSAndroid Build Coastguard Worker pr->rmt_sockdata = sockdata;
1345*8b26181fSAndroid Build Coastguard Worker
1346*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OPENSSL
1347*8b26181fSAndroid Build Coastguard Worker if (pr->uses_ssl)
1348*8b26181fSAndroid Build Coastguard Worker {
1349*8b26181fSAndroid Build Coastguard Worker pr->data_ssl = ssl_promotion(0, sockdata, fp->errbuf, PCAP_ERRBUF_SIZE);
1350*8b26181fSAndroid Build Coastguard Worker if (! pr->data_ssl) goto error;
1351*8b26181fSAndroid Build Coastguard Worker }
1352*8b26181fSAndroid Build Coastguard Worker #endif
1353*8b26181fSAndroid Build Coastguard Worker
1354*8b26181fSAndroid Build Coastguard Worker /*
1355*8b26181fSAndroid Build Coastguard Worker * Set the size of the socket buffer for the data socket.
1356*8b26181fSAndroid Build Coastguard Worker * It has the same size as the local capture buffer used
1357*8b26181fSAndroid Build Coastguard Worker * on the other side of the connection.
1358*8b26181fSAndroid Build Coastguard Worker */
1359*8b26181fSAndroid Build Coastguard Worker server_sockbufsize = ntohl(startcapreply.bufsize);
1360*8b26181fSAndroid Build Coastguard Worker
1361*8b26181fSAndroid Build Coastguard Worker /* Let's get the actual size of the socket buffer */
1362*8b26181fSAndroid Build Coastguard Worker itemp = sizeof(sockbufsize);
1363*8b26181fSAndroid Build Coastguard Worker
1364*8b26181fSAndroid Build Coastguard Worker res = getsockopt(sockdata, SOL_SOCKET, SO_RCVBUF, (char *)&sockbufsize, &itemp);
1365*8b26181fSAndroid Build Coastguard Worker if (res == -1)
1366*8b26181fSAndroid Build Coastguard Worker {
1367*8b26181fSAndroid Build Coastguard Worker sock_geterrmsg(fp->errbuf, PCAP_ERRBUF_SIZE,
1368*8b26181fSAndroid Build Coastguard Worker "pcap_startcapture_remote(): getsockopt() failed");
1369*8b26181fSAndroid Build Coastguard Worker goto error;
1370*8b26181fSAndroid Build Coastguard Worker }
1371*8b26181fSAndroid Build Coastguard Worker
1372*8b26181fSAndroid Build Coastguard Worker /*
1373*8b26181fSAndroid Build Coastguard Worker * Warning: on some kernels (e.g. Linux), the size of the user
1374*8b26181fSAndroid Build Coastguard Worker * buffer does not take into account the pcap_header and such,
1375*8b26181fSAndroid Build Coastguard Worker * and it is set equal to the snaplen.
1376*8b26181fSAndroid Build Coastguard Worker *
1377*8b26181fSAndroid Build Coastguard Worker * In my view, this is wrong (the meaning of the bufsize became
1378*8b26181fSAndroid Build Coastguard Worker * a bit strange). So, here bufsize is the whole size of the
1379*8b26181fSAndroid Build Coastguard Worker * user buffer. In case the bufsize returned is too small,
1380*8b26181fSAndroid Build Coastguard Worker * let's adjust it accordingly.
1381*8b26181fSAndroid Build Coastguard Worker */
1382*8b26181fSAndroid Build Coastguard Worker if (server_sockbufsize <= (u_int) fp->snapshot)
1383*8b26181fSAndroid Build Coastguard Worker server_sockbufsize += sizeof(struct pcap_pkthdr);
1384*8b26181fSAndroid Build Coastguard Worker
1385*8b26181fSAndroid Build Coastguard Worker /* if the current socket buffer is smaller than the desired one */
1386*8b26181fSAndroid Build Coastguard Worker if ((u_int) sockbufsize < server_sockbufsize)
1387*8b26181fSAndroid Build Coastguard Worker {
1388*8b26181fSAndroid Build Coastguard Worker /*
1389*8b26181fSAndroid Build Coastguard Worker * Loop until the buffer size is OK or the original
1390*8b26181fSAndroid Build Coastguard Worker * socket buffer size is larger than this one.
1391*8b26181fSAndroid Build Coastguard Worker */
1392*8b26181fSAndroid Build Coastguard Worker for (;;)
1393*8b26181fSAndroid Build Coastguard Worker {
1394*8b26181fSAndroid Build Coastguard Worker res = setsockopt(sockdata, SOL_SOCKET, SO_RCVBUF,
1395*8b26181fSAndroid Build Coastguard Worker (char *)&(server_sockbufsize),
1396*8b26181fSAndroid Build Coastguard Worker sizeof(server_sockbufsize));
1397*8b26181fSAndroid Build Coastguard Worker
1398*8b26181fSAndroid Build Coastguard Worker if (res == 0)
1399*8b26181fSAndroid Build Coastguard Worker break;
1400*8b26181fSAndroid Build Coastguard Worker
1401*8b26181fSAndroid Build Coastguard Worker /*
1402*8b26181fSAndroid Build Coastguard Worker * If something goes wrong, halve the buffer size
1403*8b26181fSAndroid Build Coastguard Worker * (checking that it does not become smaller than
1404*8b26181fSAndroid Build Coastguard Worker * the current one).
1405*8b26181fSAndroid Build Coastguard Worker */
1406*8b26181fSAndroid Build Coastguard Worker server_sockbufsize /= 2;
1407*8b26181fSAndroid Build Coastguard Worker
1408*8b26181fSAndroid Build Coastguard Worker if ((u_int) sockbufsize >= server_sockbufsize)
1409*8b26181fSAndroid Build Coastguard Worker {
1410*8b26181fSAndroid Build Coastguard Worker server_sockbufsize = sockbufsize;
1411*8b26181fSAndroid Build Coastguard Worker break;
1412*8b26181fSAndroid Build Coastguard Worker }
1413*8b26181fSAndroid Build Coastguard Worker }
1414*8b26181fSAndroid Build Coastguard Worker }
1415*8b26181fSAndroid Build Coastguard Worker
1416*8b26181fSAndroid Build Coastguard Worker /*
1417*8b26181fSAndroid Build Coastguard Worker * Let's allocate the packet; this is required in order to put
1418*8b26181fSAndroid Build Coastguard Worker * the packet somewhere when extracting data from the socket.
1419*8b26181fSAndroid Build Coastguard Worker * Since buffering has already been done in the socket buffer,
1420*8b26181fSAndroid Build Coastguard Worker * here we need just a buffer whose size is equal to the
1421*8b26181fSAndroid Build Coastguard Worker * largest possible packet message for the snapshot size,
1422*8b26181fSAndroid Build Coastguard Worker * namely the length of the message header plus the length
1423*8b26181fSAndroid Build Coastguard Worker * of the packet header plus the snapshot length.
1424*8b26181fSAndroid Build Coastguard Worker */
1425*8b26181fSAndroid Build Coastguard Worker fp->bufsize = sizeof(struct rpcap_header) + sizeof(struct rpcap_pkthdr) + fp->snapshot;
1426*8b26181fSAndroid Build Coastguard Worker
1427*8b26181fSAndroid Build Coastguard Worker fp->buffer = (u_char *)malloc(fp->bufsize);
1428*8b26181fSAndroid Build Coastguard Worker if (fp->buffer == NULL)
1429*8b26181fSAndroid Build Coastguard Worker {
1430*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(fp->errbuf, PCAP_ERRBUF_SIZE,
1431*8b26181fSAndroid Build Coastguard Worker errno, "malloc");
1432*8b26181fSAndroid Build Coastguard Worker goto error;
1433*8b26181fSAndroid Build Coastguard Worker }
1434*8b26181fSAndroid Build Coastguard Worker
1435*8b26181fSAndroid Build Coastguard Worker /*
1436*8b26181fSAndroid Build Coastguard Worker * The buffer is currently empty.
1437*8b26181fSAndroid Build Coastguard Worker */
1438*8b26181fSAndroid Build Coastguard Worker fp->bp = fp->buffer;
1439*8b26181fSAndroid Build Coastguard Worker fp->cc = 0;
1440*8b26181fSAndroid Build Coastguard Worker
1441*8b26181fSAndroid Build Coastguard Worker /* Discard the rest of the message. */
1442*8b26181fSAndroid Build Coastguard Worker if (rpcap_discard(pr->rmt_sockctrl, pr->ctrl_ssl, plen, fp->errbuf) == -1)
1443*8b26181fSAndroid Build Coastguard Worker goto error_nodiscard;
1444*8b26181fSAndroid Build Coastguard Worker
1445*8b26181fSAndroid Build Coastguard Worker /*
1446*8b26181fSAndroid Build Coastguard Worker * In case the user does not want to capture RPCAP packets, let's update the filter
1447*8b26181fSAndroid Build Coastguard Worker * We have to update it here (instead of sending it into the 'StartCapture' message
1448*8b26181fSAndroid Build Coastguard Worker * because when we generate the 'start capture' we do not know (yet) all the ports
1449*8b26181fSAndroid Build Coastguard Worker * we're currently using.
1450*8b26181fSAndroid Build Coastguard Worker */
1451*8b26181fSAndroid Build Coastguard Worker if (pr->rmt_flags & PCAP_OPENFLAG_NOCAPTURE_RPCAP)
1452*8b26181fSAndroid Build Coastguard Worker {
1453*8b26181fSAndroid Build Coastguard Worker struct bpf_program fcode;
1454*8b26181fSAndroid Build Coastguard Worker
1455*8b26181fSAndroid Build Coastguard Worker if (pcap_createfilter_norpcappkt(fp, &fcode) == -1)
1456*8b26181fSAndroid Build Coastguard Worker goto error;
1457*8b26181fSAndroid Build Coastguard Worker
1458*8b26181fSAndroid Build Coastguard Worker /* We cannot use 'pcap_setfilter_rpcap' because formally the capture has not been started yet */
1459*8b26181fSAndroid Build Coastguard Worker /* (the 'pr->rmt_capstarted' variable will be updated some lines below) */
1460*8b26181fSAndroid Build Coastguard Worker if (pcap_updatefilter_remote(fp, &fcode) == -1)
1461*8b26181fSAndroid Build Coastguard Worker goto error;
1462*8b26181fSAndroid Build Coastguard Worker
1463*8b26181fSAndroid Build Coastguard Worker pcap_freecode(&fcode);
1464*8b26181fSAndroid Build Coastguard Worker }
1465*8b26181fSAndroid Build Coastguard Worker
1466*8b26181fSAndroid Build Coastguard Worker pr->rmt_capstarted = 1;
1467*8b26181fSAndroid Build Coastguard Worker return 0;
1468*8b26181fSAndroid Build Coastguard Worker
1469*8b26181fSAndroid Build Coastguard Worker error:
1470*8b26181fSAndroid Build Coastguard Worker /*
1471*8b26181fSAndroid Build Coastguard Worker * When the connection has been established, we have to close it. So, at the
1472*8b26181fSAndroid Build Coastguard Worker * beginning of this function, if an error occur we return immediately with
1473*8b26181fSAndroid Build Coastguard Worker * a return NULL; when the connection is established, we have to come here
1474*8b26181fSAndroid Build Coastguard Worker * ('goto error;') in order to close everything properly.
1475*8b26181fSAndroid Build Coastguard Worker */
1476*8b26181fSAndroid Build Coastguard Worker
1477*8b26181fSAndroid Build Coastguard Worker /*
1478*8b26181fSAndroid Build Coastguard Worker * Discard the rest of the message.
1479*8b26181fSAndroid Build Coastguard Worker * We already reported an error; if this gets an error, just
1480*8b26181fSAndroid Build Coastguard Worker * drive on.
1481*8b26181fSAndroid Build Coastguard Worker */
1482*8b26181fSAndroid Build Coastguard Worker (void)rpcap_discard(pr->rmt_sockctrl, pr->ctrl_ssl, plen, NULL);
1483*8b26181fSAndroid Build Coastguard Worker
1484*8b26181fSAndroid Build Coastguard Worker error_nodiscard:
1485*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OPENSSL
1486*8b26181fSAndroid Build Coastguard Worker if (pr->data_ssl)
1487*8b26181fSAndroid Build Coastguard Worker {
1488*8b26181fSAndroid Build Coastguard Worker // Finish using the SSL handle for the data socket.
1489*8b26181fSAndroid Build Coastguard Worker // This must be done *before* the socket is closed.
1490*8b26181fSAndroid Build Coastguard Worker ssl_finish(pr->data_ssl);
1491*8b26181fSAndroid Build Coastguard Worker pr->data_ssl = NULL;
1492*8b26181fSAndroid Build Coastguard Worker }
1493*8b26181fSAndroid Build Coastguard Worker #endif
1494*8b26181fSAndroid Build Coastguard Worker
1495*8b26181fSAndroid Build Coastguard Worker /* we can be here because sockdata said 'error' */
1496*8b26181fSAndroid Build Coastguard Worker if ((sockdata != 0) && (sockdata != INVALID_SOCKET))
1497*8b26181fSAndroid Build Coastguard Worker sock_close(sockdata, NULL, 0);
1498*8b26181fSAndroid Build Coastguard Worker
1499*8b26181fSAndroid Build Coastguard Worker if (!active)
1500*8b26181fSAndroid Build Coastguard Worker {
1501*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OPENSSL
1502*8b26181fSAndroid Build Coastguard Worker if (pr->ctrl_ssl)
1503*8b26181fSAndroid Build Coastguard Worker {
1504*8b26181fSAndroid Build Coastguard Worker // Finish using the SSL handle for the control socket.
1505*8b26181fSAndroid Build Coastguard Worker // This must be done *before* the socket is closed.
1506*8b26181fSAndroid Build Coastguard Worker ssl_finish(pr->ctrl_ssl);
1507*8b26181fSAndroid Build Coastguard Worker pr->ctrl_ssl = NULL;
1508*8b26181fSAndroid Build Coastguard Worker }
1509*8b26181fSAndroid Build Coastguard Worker #endif
1510*8b26181fSAndroid Build Coastguard Worker sock_close(pr->rmt_sockctrl, NULL, 0);
1511*8b26181fSAndroid Build Coastguard Worker }
1512*8b26181fSAndroid Build Coastguard Worker
1513*8b26181fSAndroid Build Coastguard Worker if (addrinfo != NULL)
1514*8b26181fSAndroid Build Coastguard Worker freeaddrinfo(addrinfo);
1515*8b26181fSAndroid Build Coastguard Worker
1516*8b26181fSAndroid Build Coastguard Worker /*
1517*8b26181fSAndroid Build Coastguard Worker * We do not have to call pcap_close() here, because this function is always called
1518*8b26181fSAndroid Build Coastguard Worker * by the user in case something bad happens
1519*8b26181fSAndroid Build Coastguard Worker */
1520*8b26181fSAndroid Build Coastguard Worker #if 0
1521*8b26181fSAndroid Build Coastguard Worker if (fp)
1522*8b26181fSAndroid Build Coastguard Worker {
1523*8b26181fSAndroid Build Coastguard Worker pcap_close(fp);
1524*8b26181fSAndroid Build Coastguard Worker fp= NULL;
1525*8b26181fSAndroid Build Coastguard Worker }
1526*8b26181fSAndroid Build Coastguard Worker #endif
1527*8b26181fSAndroid Build Coastguard Worker
1528*8b26181fSAndroid Build Coastguard Worker return -1;
1529*8b26181fSAndroid Build Coastguard Worker }
1530*8b26181fSAndroid Build Coastguard Worker
1531*8b26181fSAndroid Build Coastguard Worker /*
1532*8b26181fSAndroid Build Coastguard Worker * This function takes a bpf program and sends it to the other host.
1533*8b26181fSAndroid Build Coastguard Worker *
1534*8b26181fSAndroid Build Coastguard Worker * This function can be called in two cases:
1535*8b26181fSAndroid Build Coastguard Worker * - pcap_startcapture_remote() is called (we have to send the filter
1536*8b26181fSAndroid Build Coastguard Worker * along with the 'start capture' command)
1537*8b26181fSAndroid Build Coastguard Worker * - we want to update the filter during a capture (i.e. pcap_setfilter()
1538*8b26181fSAndroid Build Coastguard Worker * after the capture has been started)
1539*8b26181fSAndroid Build Coastguard Worker *
1540*8b26181fSAndroid Build Coastguard Worker * This function serializes the filter into the sending buffer ('sendbuf',
1541*8b26181fSAndroid Build Coastguard Worker * passed as a parameter) and return back. It does not send anything on
1542*8b26181fSAndroid Build Coastguard Worker * the network.
1543*8b26181fSAndroid Build Coastguard Worker *
1544*8b26181fSAndroid Build Coastguard Worker * \param fp: the pcap_t descriptor of the device currently opened.
1545*8b26181fSAndroid Build Coastguard Worker *
1546*8b26181fSAndroid Build Coastguard Worker * \param sendbuf: the buffer on which the serialized data has to copied.
1547*8b26181fSAndroid Build Coastguard Worker *
1548*8b26181fSAndroid Build Coastguard Worker * \param sendbufidx: it is used to return the abounf of bytes copied into the buffer.
1549*8b26181fSAndroid Build Coastguard Worker *
1550*8b26181fSAndroid Build Coastguard Worker * \param prog: the bpf program we have to copy.
1551*8b26181fSAndroid Build Coastguard Worker *
1552*8b26181fSAndroid Build Coastguard Worker * \return '0' if everything is fine, '-1' otherwise. The error message (if one)
1553*8b26181fSAndroid Build Coastguard Worker * is returned into the 'errbuf' field of the pcap_t structure.
1554*8b26181fSAndroid Build Coastguard Worker */
pcap_pack_bpffilter(pcap_t * fp,char * sendbuf,int * sendbufidx,struct bpf_program * prog)1555*8b26181fSAndroid Build Coastguard Worker static int pcap_pack_bpffilter(pcap_t *fp, char *sendbuf, int *sendbufidx, struct bpf_program *prog)
1556*8b26181fSAndroid Build Coastguard Worker {
1557*8b26181fSAndroid Build Coastguard Worker struct rpcap_filter *filter;
1558*8b26181fSAndroid Build Coastguard Worker struct rpcap_filterbpf_insn *insn;
1559*8b26181fSAndroid Build Coastguard Worker struct bpf_insn *bf_insn;
1560*8b26181fSAndroid Build Coastguard Worker struct bpf_program fake_prog; /* To be used just in case the user forgot to set a filter */
1561*8b26181fSAndroid Build Coastguard Worker unsigned int i;
1562*8b26181fSAndroid Build Coastguard Worker
1563*8b26181fSAndroid Build Coastguard Worker if (prog->bf_len == 0) /* No filters have been specified; so, let's apply a "fake" filter */
1564*8b26181fSAndroid Build Coastguard Worker {
1565*8b26181fSAndroid Build Coastguard Worker if (pcap_compile(fp, &fake_prog, NULL /* buffer */, 1, 0) == -1)
1566*8b26181fSAndroid Build Coastguard Worker return -1;
1567*8b26181fSAndroid Build Coastguard Worker
1568*8b26181fSAndroid Build Coastguard Worker prog = &fake_prog;
1569*8b26181fSAndroid Build Coastguard Worker }
1570*8b26181fSAndroid Build Coastguard Worker
1571*8b26181fSAndroid Build Coastguard Worker filter = (struct rpcap_filter *) sendbuf;
1572*8b26181fSAndroid Build Coastguard Worker
1573*8b26181fSAndroid Build Coastguard Worker if (sock_bufferize(NULL, sizeof(struct rpcap_filter), NULL, sendbufidx,
1574*8b26181fSAndroid Build Coastguard Worker RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, fp->errbuf, PCAP_ERRBUF_SIZE))
1575*8b26181fSAndroid Build Coastguard Worker return -1;
1576*8b26181fSAndroid Build Coastguard Worker
1577*8b26181fSAndroid Build Coastguard Worker filter->filtertype = htons(RPCAP_UPDATEFILTER_BPF);
1578*8b26181fSAndroid Build Coastguard Worker filter->nitems = htonl((int32)prog->bf_len);
1579*8b26181fSAndroid Build Coastguard Worker
1580*8b26181fSAndroid Build Coastguard Worker if (sock_bufferize(NULL, prog->bf_len * sizeof(struct rpcap_filterbpf_insn),
1581*8b26181fSAndroid Build Coastguard Worker NULL, sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, fp->errbuf, PCAP_ERRBUF_SIZE))
1582*8b26181fSAndroid Build Coastguard Worker return -1;
1583*8b26181fSAndroid Build Coastguard Worker
1584*8b26181fSAndroid Build Coastguard Worker insn = (struct rpcap_filterbpf_insn *) (filter + 1);
1585*8b26181fSAndroid Build Coastguard Worker bf_insn = prog->bf_insns;
1586*8b26181fSAndroid Build Coastguard Worker
1587*8b26181fSAndroid Build Coastguard Worker for (i = 0; i < prog->bf_len; i++)
1588*8b26181fSAndroid Build Coastguard Worker {
1589*8b26181fSAndroid Build Coastguard Worker insn->code = htons(bf_insn->code);
1590*8b26181fSAndroid Build Coastguard Worker insn->jf = bf_insn->jf;
1591*8b26181fSAndroid Build Coastguard Worker insn->jt = bf_insn->jt;
1592*8b26181fSAndroid Build Coastguard Worker insn->k = htonl(bf_insn->k);
1593*8b26181fSAndroid Build Coastguard Worker
1594*8b26181fSAndroid Build Coastguard Worker insn++;
1595*8b26181fSAndroid Build Coastguard Worker bf_insn++;
1596*8b26181fSAndroid Build Coastguard Worker }
1597*8b26181fSAndroid Build Coastguard Worker
1598*8b26181fSAndroid Build Coastguard Worker return 0;
1599*8b26181fSAndroid Build Coastguard Worker }
1600*8b26181fSAndroid Build Coastguard Worker
1601*8b26181fSAndroid Build Coastguard Worker /*
1602*8b26181fSAndroid Build Coastguard Worker * This function updates a filter on a remote host.
1603*8b26181fSAndroid Build Coastguard Worker *
1604*8b26181fSAndroid Build Coastguard Worker * It is called when the user wants to update a filter.
1605*8b26181fSAndroid Build Coastguard Worker * In case we're capturing from the network, it sends the filter to our
1606*8b26181fSAndroid Build Coastguard Worker * peer.
1607*8b26181fSAndroid Build Coastguard Worker * This function is *not* called automatically when the user calls
1608*8b26181fSAndroid Build Coastguard Worker * pcap_setfilter().
1609*8b26181fSAndroid Build Coastguard Worker * There will be two cases:
1610*8b26181fSAndroid Build Coastguard Worker * - the capture has been started: in this case, pcap_setfilter_rpcap()
1611*8b26181fSAndroid Build Coastguard Worker * calls pcap_updatefilter_remote()
1612*8b26181fSAndroid Build Coastguard Worker * - the capture has not started yet: in this case, pcap_setfilter_rpcap()
1613*8b26181fSAndroid Build Coastguard Worker * stores the filter into the pcap_t structure, and then the filter is
1614*8b26181fSAndroid Build Coastguard Worker * sent with pcap_startcap().
1615*8b26181fSAndroid Build Coastguard Worker *
1616*8b26181fSAndroid Build Coastguard Worker * WARNING This function *does not* clear the packet currently into the
1617*8b26181fSAndroid Build Coastguard Worker * buffers. Therefore, the user has to expect to receive some packets
1618*8b26181fSAndroid Build Coastguard Worker * that are related to the previous filter. If you want to discard all
1619*8b26181fSAndroid Build Coastguard Worker * the packets before applying a new filter, you have to close the
1620*8b26181fSAndroid Build Coastguard Worker * current capture session and start a new one.
1621*8b26181fSAndroid Build Coastguard Worker *
1622*8b26181fSAndroid Build Coastguard Worker * XXX - we really should have pcap_setfilter() always discard packets
1623*8b26181fSAndroid Build Coastguard Worker * received with the old filter, and have a separate pcap_setfilter_noflush()
1624*8b26181fSAndroid Build Coastguard Worker * function that doesn't discard any packets.
1625*8b26181fSAndroid Build Coastguard Worker */
pcap_updatefilter_remote(pcap_t * fp,struct bpf_program * prog)1626*8b26181fSAndroid Build Coastguard Worker static int pcap_updatefilter_remote(pcap_t *fp, struct bpf_program *prog)
1627*8b26181fSAndroid Build Coastguard Worker {
1628*8b26181fSAndroid Build Coastguard Worker struct pcap_rpcap *pr = fp->priv; /* structure used when doing a remote live capture */
1629*8b26181fSAndroid Build Coastguard Worker char sendbuf[RPCAP_NETBUF_SIZE]; /* temporary buffer in which data to be sent is buffered */
1630*8b26181fSAndroid Build Coastguard Worker int sendbufidx = 0; /* index which keeps the number of bytes currently buffered */
1631*8b26181fSAndroid Build Coastguard Worker struct rpcap_header header; /* To keep the reply message */
1632*8b26181fSAndroid Build Coastguard Worker
1633*8b26181fSAndroid Build Coastguard Worker if (sock_bufferize(NULL, sizeof(struct rpcap_header), NULL, &sendbufidx,
1634*8b26181fSAndroid Build Coastguard Worker RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, fp->errbuf, PCAP_ERRBUF_SIZE))
1635*8b26181fSAndroid Build Coastguard Worker return -1;
1636*8b26181fSAndroid Build Coastguard Worker
1637*8b26181fSAndroid Build Coastguard Worker rpcap_createhdr((struct rpcap_header *) sendbuf,
1638*8b26181fSAndroid Build Coastguard Worker pr->protocol_version, RPCAP_MSG_UPDATEFILTER_REQ, 0,
1639*8b26181fSAndroid Build Coastguard Worker sizeof(struct rpcap_filter) + prog->bf_len * sizeof(struct rpcap_filterbpf_insn));
1640*8b26181fSAndroid Build Coastguard Worker
1641*8b26181fSAndroid Build Coastguard Worker if (pcap_pack_bpffilter(fp, &sendbuf[sendbufidx], &sendbufidx, prog))
1642*8b26181fSAndroid Build Coastguard Worker return -1;
1643*8b26181fSAndroid Build Coastguard Worker
1644*8b26181fSAndroid Build Coastguard Worker if (sock_send(pr->rmt_sockctrl, pr->ctrl_ssl, sendbuf, sendbufidx, fp->errbuf,
1645*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE) < 0)
1646*8b26181fSAndroid Build Coastguard Worker return -1;
1647*8b26181fSAndroid Build Coastguard Worker
1648*8b26181fSAndroid Build Coastguard Worker /* Receive and process the reply message header. */
1649*8b26181fSAndroid Build Coastguard Worker if (rpcap_process_msg_header(pr->rmt_sockctrl, pr->ctrl_ssl, pr->protocol_version,
1650*8b26181fSAndroid Build Coastguard Worker RPCAP_MSG_UPDATEFILTER_REQ, &header, fp->errbuf) == -1)
1651*8b26181fSAndroid Build Coastguard Worker return -1;
1652*8b26181fSAndroid Build Coastguard Worker
1653*8b26181fSAndroid Build Coastguard Worker /*
1654*8b26181fSAndroid Build Coastguard Worker * It shouldn't have any contents; discard it if it does.
1655*8b26181fSAndroid Build Coastguard Worker */
1656*8b26181fSAndroid Build Coastguard Worker if (rpcap_discard(pr->rmt_sockctrl, pr->ctrl_ssl, header.plen, fp->errbuf) == -1)
1657*8b26181fSAndroid Build Coastguard Worker return -1;
1658*8b26181fSAndroid Build Coastguard Worker
1659*8b26181fSAndroid Build Coastguard Worker return 0;
1660*8b26181fSAndroid Build Coastguard Worker }
1661*8b26181fSAndroid Build Coastguard Worker
1662*8b26181fSAndroid Build Coastguard Worker static void
pcap_save_current_filter_rpcap(pcap_t * fp,const char * filter)1663*8b26181fSAndroid Build Coastguard Worker pcap_save_current_filter_rpcap(pcap_t *fp, const char *filter)
1664*8b26181fSAndroid Build Coastguard Worker {
1665*8b26181fSAndroid Build Coastguard Worker struct pcap_rpcap *pr = fp->priv; /* structure used when doing a remote live capture */
1666*8b26181fSAndroid Build Coastguard Worker
1667*8b26181fSAndroid Build Coastguard Worker /*
1668*8b26181fSAndroid Build Coastguard Worker * Check if:
1669*8b26181fSAndroid Build Coastguard Worker * - We are on an remote capture
1670*8b26181fSAndroid Build Coastguard Worker * - we do not want to capture RPCAP traffic
1671*8b26181fSAndroid Build Coastguard Worker *
1672*8b26181fSAndroid Build Coastguard Worker * If so, we have to save the current filter, because we have to
1673*8b26181fSAndroid Build Coastguard Worker * add some piece of stuff later
1674*8b26181fSAndroid Build Coastguard Worker */
1675*8b26181fSAndroid Build Coastguard Worker if (pr->rmt_clientside &&
1676*8b26181fSAndroid Build Coastguard Worker (pr->rmt_flags & PCAP_OPENFLAG_NOCAPTURE_RPCAP))
1677*8b26181fSAndroid Build Coastguard Worker {
1678*8b26181fSAndroid Build Coastguard Worker if (pr->currentfilter)
1679*8b26181fSAndroid Build Coastguard Worker free(pr->currentfilter);
1680*8b26181fSAndroid Build Coastguard Worker
1681*8b26181fSAndroid Build Coastguard Worker if (filter == NULL)
1682*8b26181fSAndroid Build Coastguard Worker filter = "";
1683*8b26181fSAndroid Build Coastguard Worker
1684*8b26181fSAndroid Build Coastguard Worker pr->currentfilter = strdup(filter);
1685*8b26181fSAndroid Build Coastguard Worker }
1686*8b26181fSAndroid Build Coastguard Worker }
1687*8b26181fSAndroid Build Coastguard Worker
1688*8b26181fSAndroid Build Coastguard Worker /*
1689*8b26181fSAndroid Build Coastguard Worker * This function sends a filter to a remote host.
1690*8b26181fSAndroid Build Coastguard Worker *
1691*8b26181fSAndroid Build Coastguard Worker * This function is called when the user wants to set a filter.
1692*8b26181fSAndroid Build Coastguard Worker * It sends the filter to our peer.
1693*8b26181fSAndroid Build Coastguard Worker * This function is called automatically when the user calls pcap_setfilter().
1694*8b26181fSAndroid Build Coastguard Worker *
1695*8b26181fSAndroid Build Coastguard Worker * Parameters and return values are exactly the same of pcap_setfilter().
1696*8b26181fSAndroid Build Coastguard Worker */
pcap_setfilter_rpcap(pcap_t * fp,struct bpf_program * prog)1697*8b26181fSAndroid Build Coastguard Worker static int pcap_setfilter_rpcap(pcap_t *fp, struct bpf_program *prog)
1698*8b26181fSAndroid Build Coastguard Worker {
1699*8b26181fSAndroid Build Coastguard Worker struct pcap_rpcap *pr = fp->priv; /* structure used when doing a remote live capture */
1700*8b26181fSAndroid Build Coastguard Worker
1701*8b26181fSAndroid Build Coastguard Worker if (!pr->rmt_capstarted)
1702*8b26181fSAndroid Build Coastguard Worker {
1703*8b26181fSAndroid Build Coastguard Worker /* copy filter into the pcap_t structure */
1704*8b26181fSAndroid Build Coastguard Worker if (install_bpf_program(fp, prog) == -1)
1705*8b26181fSAndroid Build Coastguard Worker return -1;
1706*8b26181fSAndroid Build Coastguard Worker return 0;
1707*8b26181fSAndroid Build Coastguard Worker }
1708*8b26181fSAndroid Build Coastguard Worker
1709*8b26181fSAndroid Build Coastguard Worker /* we have to update a filter during run-time */
1710*8b26181fSAndroid Build Coastguard Worker if (pcap_updatefilter_remote(fp, prog))
1711*8b26181fSAndroid Build Coastguard Worker return -1;
1712*8b26181fSAndroid Build Coastguard Worker
1713*8b26181fSAndroid Build Coastguard Worker return 0;
1714*8b26181fSAndroid Build Coastguard Worker }
1715*8b26181fSAndroid Build Coastguard Worker
1716*8b26181fSAndroid Build Coastguard Worker /*
1717*8b26181fSAndroid Build Coastguard Worker * This function updates the current filter in order not to capture rpcap
1718*8b26181fSAndroid Build Coastguard Worker * packets.
1719*8b26181fSAndroid Build Coastguard Worker *
1720*8b26181fSAndroid Build Coastguard Worker * This function is called *only* when the user wants exclude RPCAP packets
1721*8b26181fSAndroid Build Coastguard Worker * related to the current session from the captured packets.
1722*8b26181fSAndroid Build Coastguard Worker *
1723*8b26181fSAndroid Build Coastguard Worker * \return '0' if everything is fine, '-1' otherwise. The error message (if one)
1724*8b26181fSAndroid Build Coastguard Worker * is returned into the 'errbuf' field of the pcap_t structure.
1725*8b26181fSAndroid Build Coastguard Worker */
pcap_createfilter_norpcappkt(pcap_t * fp,struct bpf_program * prog)1726*8b26181fSAndroid Build Coastguard Worker static int pcap_createfilter_norpcappkt(pcap_t *fp, struct bpf_program *prog)
1727*8b26181fSAndroid Build Coastguard Worker {
1728*8b26181fSAndroid Build Coastguard Worker struct pcap_rpcap *pr = fp->priv; /* structure used when doing a remote live capture */
1729*8b26181fSAndroid Build Coastguard Worker int RetVal = 0;
1730*8b26181fSAndroid Build Coastguard Worker
1731*8b26181fSAndroid Build Coastguard Worker /* We do not want to capture our RPCAP traffic. So, let's update the filter */
1732*8b26181fSAndroid Build Coastguard Worker if (pr->rmt_flags & PCAP_OPENFLAG_NOCAPTURE_RPCAP)
1733*8b26181fSAndroid Build Coastguard Worker {
1734*8b26181fSAndroid Build Coastguard Worker struct sockaddr_storage saddr; /* temp, needed to retrieve the network data port chosen on the local machine */
1735*8b26181fSAndroid Build Coastguard Worker socklen_t saddrlen; /* temp, needed to retrieve the network data port chosen on the local machine */
1736*8b26181fSAndroid Build Coastguard Worker char myaddress[128];
1737*8b26181fSAndroid Build Coastguard Worker char myctrlport[128];
1738*8b26181fSAndroid Build Coastguard Worker char mydataport[128];
1739*8b26181fSAndroid Build Coastguard Worker char peeraddress[128];
1740*8b26181fSAndroid Build Coastguard Worker char peerctrlport[128];
1741*8b26181fSAndroid Build Coastguard Worker char *newfilter;
1742*8b26181fSAndroid Build Coastguard Worker
1743*8b26181fSAndroid Build Coastguard Worker /* Get the name/port of our peer */
1744*8b26181fSAndroid Build Coastguard Worker saddrlen = sizeof(struct sockaddr_storage);
1745*8b26181fSAndroid Build Coastguard Worker if (getpeername(pr->rmt_sockctrl, (struct sockaddr *) &saddr, &saddrlen) == -1)
1746*8b26181fSAndroid Build Coastguard Worker {
1747*8b26181fSAndroid Build Coastguard Worker sock_geterrmsg(fp->errbuf, PCAP_ERRBUF_SIZE,
1748*8b26181fSAndroid Build Coastguard Worker "getpeername() failed");
1749*8b26181fSAndroid Build Coastguard Worker return -1;
1750*8b26181fSAndroid Build Coastguard Worker }
1751*8b26181fSAndroid Build Coastguard Worker
1752*8b26181fSAndroid Build Coastguard Worker if (getnameinfo((struct sockaddr *) &saddr, saddrlen, peeraddress,
1753*8b26181fSAndroid Build Coastguard Worker sizeof(peeraddress), peerctrlport, sizeof(peerctrlport), NI_NUMERICHOST | NI_NUMERICSERV))
1754*8b26181fSAndroid Build Coastguard Worker {
1755*8b26181fSAndroid Build Coastguard Worker sock_geterrmsg(fp->errbuf, PCAP_ERRBUF_SIZE,
1756*8b26181fSAndroid Build Coastguard Worker "getnameinfo() failed");
1757*8b26181fSAndroid Build Coastguard Worker return -1;
1758*8b26181fSAndroid Build Coastguard Worker }
1759*8b26181fSAndroid Build Coastguard Worker
1760*8b26181fSAndroid Build Coastguard Worker /* We cannot check the data port, because this is available only in case of TCP sockets */
1761*8b26181fSAndroid Build Coastguard Worker /* Get the name/port of the current host */
1762*8b26181fSAndroid Build Coastguard Worker if (getsockname(pr->rmt_sockctrl, (struct sockaddr *) &saddr, &saddrlen) == -1)
1763*8b26181fSAndroid Build Coastguard Worker {
1764*8b26181fSAndroid Build Coastguard Worker sock_geterrmsg(fp->errbuf, PCAP_ERRBUF_SIZE,
1765*8b26181fSAndroid Build Coastguard Worker "getsockname() failed");
1766*8b26181fSAndroid Build Coastguard Worker return -1;
1767*8b26181fSAndroid Build Coastguard Worker }
1768*8b26181fSAndroid Build Coastguard Worker
1769*8b26181fSAndroid Build Coastguard Worker /* Get the local port the system picked up */
1770*8b26181fSAndroid Build Coastguard Worker if (getnameinfo((struct sockaddr *) &saddr, saddrlen, myaddress,
1771*8b26181fSAndroid Build Coastguard Worker sizeof(myaddress), myctrlport, sizeof(myctrlport), NI_NUMERICHOST | NI_NUMERICSERV))
1772*8b26181fSAndroid Build Coastguard Worker {
1773*8b26181fSAndroid Build Coastguard Worker sock_geterrmsg(fp->errbuf, PCAP_ERRBUF_SIZE,
1774*8b26181fSAndroid Build Coastguard Worker "getnameinfo() failed");
1775*8b26181fSAndroid Build Coastguard Worker return -1;
1776*8b26181fSAndroid Build Coastguard Worker }
1777*8b26181fSAndroid Build Coastguard Worker
1778*8b26181fSAndroid Build Coastguard Worker /* Let's now check the data port */
1779*8b26181fSAndroid Build Coastguard Worker if (getsockname(pr->rmt_sockdata, (struct sockaddr *) &saddr, &saddrlen) == -1)
1780*8b26181fSAndroid Build Coastguard Worker {
1781*8b26181fSAndroid Build Coastguard Worker sock_geterrmsg(fp->errbuf, PCAP_ERRBUF_SIZE,
1782*8b26181fSAndroid Build Coastguard Worker "getsockname() failed");
1783*8b26181fSAndroid Build Coastguard Worker return -1;
1784*8b26181fSAndroid Build Coastguard Worker }
1785*8b26181fSAndroid Build Coastguard Worker
1786*8b26181fSAndroid Build Coastguard Worker /* Get the local port the system picked up */
1787*8b26181fSAndroid Build Coastguard Worker if (getnameinfo((struct sockaddr *) &saddr, saddrlen, NULL, 0, mydataport, sizeof(mydataport), NI_NUMERICSERV))
1788*8b26181fSAndroid Build Coastguard Worker {
1789*8b26181fSAndroid Build Coastguard Worker sock_geterrmsg(fp->errbuf, PCAP_ERRBUF_SIZE,
1790*8b26181fSAndroid Build Coastguard Worker "getnameinfo() failed");
1791*8b26181fSAndroid Build Coastguard Worker return -1;
1792*8b26181fSAndroid Build Coastguard Worker }
1793*8b26181fSAndroid Build Coastguard Worker
1794*8b26181fSAndroid Build Coastguard Worker if (pr->currentfilter && pr->currentfilter[0] != '\0')
1795*8b26181fSAndroid Build Coastguard Worker {
1796*8b26181fSAndroid Build Coastguard Worker /*
1797*8b26181fSAndroid Build Coastguard Worker * We have a current filter; add items to it to
1798*8b26181fSAndroid Build Coastguard Worker * filter out this rpcap session.
1799*8b26181fSAndroid Build Coastguard Worker */
1800*8b26181fSAndroid Build Coastguard Worker if (pcap_asprintf(&newfilter,
1801*8b26181fSAndroid Build Coastguard Worker "(%s) and not (host %s and host %s and port %s and port %s) and not (host %s and host %s and port %s)",
1802*8b26181fSAndroid Build Coastguard Worker pr->currentfilter, myaddress, peeraddress,
1803*8b26181fSAndroid Build Coastguard Worker myctrlport, peerctrlport, myaddress, peeraddress,
1804*8b26181fSAndroid Build Coastguard Worker mydataport) == -1)
1805*8b26181fSAndroid Build Coastguard Worker {
1806*8b26181fSAndroid Build Coastguard Worker /* Failed. */
1807*8b26181fSAndroid Build Coastguard Worker snprintf(fp->errbuf, PCAP_ERRBUF_SIZE,
1808*8b26181fSAndroid Build Coastguard Worker "Can't allocate memory for new filter");
1809*8b26181fSAndroid Build Coastguard Worker return -1;
1810*8b26181fSAndroid Build Coastguard Worker }
1811*8b26181fSAndroid Build Coastguard Worker }
1812*8b26181fSAndroid Build Coastguard Worker else
1813*8b26181fSAndroid Build Coastguard Worker {
1814*8b26181fSAndroid Build Coastguard Worker /*
1815*8b26181fSAndroid Build Coastguard Worker * We have no current filter; construct a filter to
1816*8b26181fSAndroid Build Coastguard Worker * filter out this rpcap session.
1817*8b26181fSAndroid Build Coastguard Worker */
1818*8b26181fSAndroid Build Coastguard Worker if (pcap_asprintf(&newfilter,
1819*8b26181fSAndroid Build Coastguard Worker "not (host %s and host %s and port %s and port %s) and not (host %s and host %s and port %s)",
1820*8b26181fSAndroid Build Coastguard Worker myaddress, peeraddress, myctrlport, peerctrlport,
1821*8b26181fSAndroid Build Coastguard Worker myaddress, peeraddress, mydataport) == -1)
1822*8b26181fSAndroid Build Coastguard Worker {
1823*8b26181fSAndroid Build Coastguard Worker /* Failed. */
1824*8b26181fSAndroid Build Coastguard Worker snprintf(fp->errbuf, PCAP_ERRBUF_SIZE,
1825*8b26181fSAndroid Build Coastguard Worker "Can't allocate memory for new filter");
1826*8b26181fSAndroid Build Coastguard Worker return -1;
1827*8b26181fSAndroid Build Coastguard Worker }
1828*8b26181fSAndroid Build Coastguard Worker }
1829*8b26181fSAndroid Build Coastguard Worker
1830*8b26181fSAndroid Build Coastguard Worker /*
1831*8b26181fSAndroid Build Coastguard Worker * This is only an hack to prevent the save_current_filter
1832*8b26181fSAndroid Build Coastguard Worker * routine, which will be called when we call pcap_compile(),
1833*8b26181fSAndroid Build Coastguard Worker * from saving the modified filter.
1834*8b26181fSAndroid Build Coastguard Worker */
1835*8b26181fSAndroid Build Coastguard Worker pr->rmt_clientside = 0;
1836*8b26181fSAndroid Build Coastguard Worker
1837*8b26181fSAndroid Build Coastguard Worker if (pcap_compile(fp, prog, newfilter, 1, 0) == -1)
1838*8b26181fSAndroid Build Coastguard Worker RetVal = -1;
1839*8b26181fSAndroid Build Coastguard Worker
1840*8b26181fSAndroid Build Coastguard Worker /* Undo the hack. */
1841*8b26181fSAndroid Build Coastguard Worker pr->rmt_clientside = 1;
1842*8b26181fSAndroid Build Coastguard Worker
1843*8b26181fSAndroid Build Coastguard Worker free(newfilter);
1844*8b26181fSAndroid Build Coastguard Worker }
1845*8b26181fSAndroid Build Coastguard Worker
1846*8b26181fSAndroid Build Coastguard Worker return RetVal;
1847*8b26181fSAndroid Build Coastguard Worker }
1848*8b26181fSAndroid Build Coastguard Worker
1849*8b26181fSAndroid Build Coastguard Worker /*
1850*8b26181fSAndroid Build Coastguard Worker * This function sets sampling parameters in the remote host.
1851*8b26181fSAndroid Build Coastguard Worker *
1852*8b26181fSAndroid Build Coastguard Worker * It is called when the user wants to set activate sampling on the
1853*8b26181fSAndroid Build Coastguard Worker * remote host.
1854*8b26181fSAndroid Build Coastguard Worker *
1855*8b26181fSAndroid Build Coastguard Worker * Sampling parameters are defined into the 'pcap_t' structure.
1856*8b26181fSAndroid Build Coastguard Worker *
1857*8b26181fSAndroid Build Coastguard Worker * \param p: the pcap_t descriptor of the device currently opened.
1858*8b26181fSAndroid Build Coastguard Worker *
1859*8b26181fSAndroid Build Coastguard Worker * \return '0' if everything is OK, '-1' is something goes wrong. The
1860*8b26181fSAndroid Build Coastguard Worker * error message is returned in the 'errbuf' member of the pcap_t structure.
1861*8b26181fSAndroid Build Coastguard Worker */
pcap_setsampling_remote(pcap_t * fp)1862*8b26181fSAndroid Build Coastguard Worker static int pcap_setsampling_remote(pcap_t *fp)
1863*8b26181fSAndroid Build Coastguard Worker {
1864*8b26181fSAndroid Build Coastguard Worker struct pcap_rpcap *pr = fp->priv; /* structure used when doing a remote live capture */
1865*8b26181fSAndroid Build Coastguard Worker char sendbuf[RPCAP_NETBUF_SIZE];/* temporary buffer in which data to be sent is buffered */
1866*8b26181fSAndroid Build Coastguard Worker int sendbufidx = 0; /* index which keeps the number of bytes currently buffered */
1867*8b26181fSAndroid Build Coastguard Worker struct rpcap_header header; /* To keep the reply message */
1868*8b26181fSAndroid Build Coastguard Worker struct rpcap_sampling *sampling_pars; /* Structure that is needed to send sampling parameters to the remote host */
1869*8b26181fSAndroid Build Coastguard Worker
1870*8b26181fSAndroid Build Coastguard Worker /* If no samping is requested, return 'ok' */
1871*8b26181fSAndroid Build Coastguard Worker if (fp->rmt_samp.method == PCAP_SAMP_NOSAMP)
1872*8b26181fSAndroid Build Coastguard Worker return 0;
1873*8b26181fSAndroid Build Coastguard Worker
1874*8b26181fSAndroid Build Coastguard Worker /*
1875*8b26181fSAndroid Build Coastguard Worker * Check for sampling parameters that don't fit in a message.
1876*8b26181fSAndroid Build Coastguard Worker * We'll let the server complain about invalid parameters
1877*8b26181fSAndroid Build Coastguard Worker * that do fit into the message.
1878*8b26181fSAndroid Build Coastguard Worker */
1879*8b26181fSAndroid Build Coastguard Worker if (fp->rmt_samp.method < 0 || fp->rmt_samp.method > 255) {
1880*8b26181fSAndroid Build Coastguard Worker snprintf(fp->errbuf, PCAP_ERRBUF_SIZE,
1881*8b26181fSAndroid Build Coastguard Worker "Invalid sampling method %d", fp->rmt_samp.method);
1882*8b26181fSAndroid Build Coastguard Worker return -1;
1883*8b26181fSAndroid Build Coastguard Worker }
1884*8b26181fSAndroid Build Coastguard Worker if (fp->rmt_samp.value < 0 || fp->rmt_samp.value > 65535) {
1885*8b26181fSAndroid Build Coastguard Worker snprintf(fp->errbuf, PCAP_ERRBUF_SIZE,
1886*8b26181fSAndroid Build Coastguard Worker "Invalid sampling value %d", fp->rmt_samp.value);
1887*8b26181fSAndroid Build Coastguard Worker return -1;
1888*8b26181fSAndroid Build Coastguard Worker }
1889*8b26181fSAndroid Build Coastguard Worker
1890*8b26181fSAndroid Build Coastguard Worker if (sock_bufferize(NULL, sizeof(struct rpcap_header), NULL,
1891*8b26181fSAndroid Build Coastguard Worker &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, fp->errbuf, PCAP_ERRBUF_SIZE))
1892*8b26181fSAndroid Build Coastguard Worker return -1;
1893*8b26181fSAndroid Build Coastguard Worker
1894*8b26181fSAndroid Build Coastguard Worker rpcap_createhdr((struct rpcap_header *) sendbuf,
1895*8b26181fSAndroid Build Coastguard Worker pr->protocol_version, RPCAP_MSG_SETSAMPLING_REQ, 0,
1896*8b26181fSAndroid Build Coastguard Worker sizeof(struct rpcap_sampling));
1897*8b26181fSAndroid Build Coastguard Worker
1898*8b26181fSAndroid Build Coastguard Worker /* Fill the structure needed to open an adapter remotely */
1899*8b26181fSAndroid Build Coastguard Worker sampling_pars = (struct rpcap_sampling *) &sendbuf[sendbufidx];
1900*8b26181fSAndroid Build Coastguard Worker
1901*8b26181fSAndroid Build Coastguard Worker if (sock_bufferize(NULL, sizeof(struct rpcap_sampling), NULL,
1902*8b26181fSAndroid Build Coastguard Worker &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, fp->errbuf, PCAP_ERRBUF_SIZE))
1903*8b26181fSAndroid Build Coastguard Worker return -1;
1904*8b26181fSAndroid Build Coastguard Worker
1905*8b26181fSAndroid Build Coastguard Worker memset(sampling_pars, 0, sizeof(struct rpcap_sampling));
1906*8b26181fSAndroid Build Coastguard Worker
1907*8b26181fSAndroid Build Coastguard Worker sampling_pars->method = (uint8)fp->rmt_samp.method;
1908*8b26181fSAndroid Build Coastguard Worker sampling_pars->value = (uint16)htonl(fp->rmt_samp.value);
1909*8b26181fSAndroid Build Coastguard Worker
1910*8b26181fSAndroid Build Coastguard Worker if (sock_send(pr->rmt_sockctrl, pr->ctrl_ssl, sendbuf, sendbufidx, fp->errbuf,
1911*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE) < 0)
1912*8b26181fSAndroid Build Coastguard Worker return -1;
1913*8b26181fSAndroid Build Coastguard Worker
1914*8b26181fSAndroid Build Coastguard Worker /* Receive and process the reply message header. */
1915*8b26181fSAndroid Build Coastguard Worker if (rpcap_process_msg_header(pr->rmt_sockctrl, pr->ctrl_ssl, pr->protocol_version,
1916*8b26181fSAndroid Build Coastguard Worker RPCAP_MSG_SETSAMPLING_REQ, &header, fp->errbuf) == -1)
1917*8b26181fSAndroid Build Coastguard Worker return -1;
1918*8b26181fSAndroid Build Coastguard Worker
1919*8b26181fSAndroid Build Coastguard Worker /*
1920*8b26181fSAndroid Build Coastguard Worker * It shouldn't have any contents; discard it if it does.
1921*8b26181fSAndroid Build Coastguard Worker */
1922*8b26181fSAndroid Build Coastguard Worker if (rpcap_discard(pr->rmt_sockctrl, pr->ctrl_ssl, header.plen, fp->errbuf) == -1)
1923*8b26181fSAndroid Build Coastguard Worker return -1;
1924*8b26181fSAndroid Build Coastguard Worker
1925*8b26181fSAndroid Build Coastguard Worker return 0;
1926*8b26181fSAndroid Build Coastguard Worker }
1927*8b26181fSAndroid Build Coastguard Worker
1928*8b26181fSAndroid Build Coastguard Worker /*********************************************************
1929*8b26181fSAndroid Build Coastguard Worker * *
1930*8b26181fSAndroid Build Coastguard Worker * Miscellaneous functions *
1931*8b26181fSAndroid Build Coastguard Worker * *
1932*8b26181fSAndroid Build Coastguard Worker *********************************************************/
1933*8b26181fSAndroid Build Coastguard Worker
1934*8b26181fSAndroid Build Coastguard Worker /*
1935*8b26181fSAndroid Build Coastguard Worker * This function performs authentication and protocol version
1936*8b26181fSAndroid Build Coastguard Worker * negotiation. It is required in order to open the connection
1937*8b26181fSAndroid Build Coastguard Worker * with the other end party.
1938*8b26181fSAndroid Build Coastguard Worker *
1939*8b26181fSAndroid Build Coastguard Worker * It sends authentication parameters on the control socket and
1940*8b26181fSAndroid Build Coastguard Worker * reads the reply. If the reply is a success indication, it
1941*8b26181fSAndroid Build Coastguard Worker * checks whether the reply includes minimum and maximum supported
1942*8b26181fSAndroid Build Coastguard Worker * versions from the server; if not, it assumes both are 0, as
1943*8b26181fSAndroid Build Coastguard Worker * that means it's an older server that doesn't return supported
1944*8b26181fSAndroid Build Coastguard Worker * version numbers in authentication replies, so it only supports
1945*8b26181fSAndroid Build Coastguard Worker * version 0. It then tries to determine the maximum version
1946*8b26181fSAndroid Build Coastguard Worker * supported both by us and by the server. If it can find such a
1947*8b26181fSAndroid Build Coastguard Worker * version, it sets us up to use that version; otherwise, it fails,
1948*8b26181fSAndroid Build Coastguard Worker * indicating that there is no version supported by us and by the
1949*8b26181fSAndroid Build Coastguard Worker * server.
1950*8b26181fSAndroid Build Coastguard Worker *
1951*8b26181fSAndroid Build Coastguard Worker * \param sock: the socket we are currently using.
1952*8b26181fSAndroid Build Coastguard Worker *
1953*8b26181fSAndroid Build Coastguard Worker * \param ver: pointer to variable to which to set the protocol version
1954*8b26181fSAndroid Build Coastguard Worker * number we selected.
1955*8b26181fSAndroid Build Coastguard Worker *
1956*8b26181fSAndroid Build Coastguard Worker * \param byte_swapped: pointer to variable to which to set 1 if the
1957*8b26181fSAndroid Build Coastguard Worker * byte order the server says it has is byte-swapped from ours, 0
1958*8b26181fSAndroid Build Coastguard Worker * otherwise (whether it's the same as ours or is unknown).
1959*8b26181fSAndroid Build Coastguard Worker *
1960*8b26181fSAndroid Build Coastguard Worker * \param auth: authentication parameters that have to be sent.
1961*8b26181fSAndroid Build Coastguard Worker *
1962*8b26181fSAndroid Build Coastguard Worker * \param errbuf: a pointer to a user-allocated buffer (of size
1963*8b26181fSAndroid Build Coastguard Worker * PCAP_ERRBUF_SIZE) that will contain the error message (in case there
1964*8b26181fSAndroid Build Coastguard Worker * is one). It could be a network problem or the fact that the authorization
1965*8b26181fSAndroid Build Coastguard Worker * failed.
1966*8b26181fSAndroid Build Coastguard Worker *
1967*8b26181fSAndroid Build Coastguard Worker * \return '0' if everything is fine, '-1' for an error. For errors,
1968*8b26181fSAndroid Build Coastguard Worker * an error message string is returned in the 'errbuf' variable.
1969*8b26181fSAndroid Build Coastguard Worker */
rpcap_doauth(SOCKET sockctrl,SSL * ssl,uint8 * ver,int * byte_swapped,struct pcap_rmtauth * auth,char * errbuf)1970*8b26181fSAndroid Build Coastguard Worker static int rpcap_doauth(SOCKET sockctrl, SSL *ssl, uint8 *ver,
1971*8b26181fSAndroid Build Coastguard Worker int *byte_swapped, struct pcap_rmtauth *auth, char *errbuf)
1972*8b26181fSAndroid Build Coastguard Worker {
1973*8b26181fSAndroid Build Coastguard Worker char sendbuf[RPCAP_NETBUF_SIZE]; /* temporary buffer in which data that has to be sent is buffered */
1974*8b26181fSAndroid Build Coastguard Worker int sendbufidx = 0; /* index which keeps the number of bytes currently buffered */
1975*8b26181fSAndroid Build Coastguard Worker uint16 length; /* length of the payload of this message */
1976*8b26181fSAndroid Build Coastguard Worker struct rpcap_auth *rpauth;
1977*8b26181fSAndroid Build Coastguard Worker uint16 auth_type;
1978*8b26181fSAndroid Build Coastguard Worker struct rpcap_header header;
1979*8b26181fSAndroid Build Coastguard Worker size_t str_length;
1980*8b26181fSAndroid Build Coastguard Worker uint32 plen;
1981*8b26181fSAndroid Build Coastguard Worker struct rpcap_authreply authreply; /* authentication reply message */
1982*8b26181fSAndroid Build Coastguard Worker uint8 ourvers;
1983*8b26181fSAndroid Build Coastguard Worker int has_byte_order; /* The server sent its version of the byte-order magic number */
1984*8b26181fSAndroid Build Coastguard Worker u_int their_byte_order_magic; /* Here's what it is */
1985*8b26181fSAndroid Build Coastguard Worker
1986*8b26181fSAndroid Build Coastguard Worker if (auth)
1987*8b26181fSAndroid Build Coastguard Worker {
1988*8b26181fSAndroid Build Coastguard Worker switch (auth->type)
1989*8b26181fSAndroid Build Coastguard Worker {
1990*8b26181fSAndroid Build Coastguard Worker case RPCAP_RMTAUTH_NULL:
1991*8b26181fSAndroid Build Coastguard Worker length = sizeof(struct rpcap_auth);
1992*8b26181fSAndroid Build Coastguard Worker break;
1993*8b26181fSAndroid Build Coastguard Worker
1994*8b26181fSAndroid Build Coastguard Worker case RPCAP_RMTAUTH_PWD:
1995*8b26181fSAndroid Build Coastguard Worker length = sizeof(struct rpcap_auth);
1996*8b26181fSAndroid Build Coastguard Worker if (auth->username)
1997*8b26181fSAndroid Build Coastguard Worker {
1998*8b26181fSAndroid Build Coastguard Worker str_length = strlen(auth->username);
1999*8b26181fSAndroid Build Coastguard Worker if (str_length > 65535)
2000*8b26181fSAndroid Build Coastguard Worker {
2001*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE, "User name is too long (> 65535 bytes)");
2002*8b26181fSAndroid Build Coastguard Worker return -1;
2003*8b26181fSAndroid Build Coastguard Worker }
2004*8b26181fSAndroid Build Coastguard Worker length += (uint16)str_length;
2005*8b26181fSAndroid Build Coastguard Worker }
2006*8b26181fSAndroid Build Coastguard Worker if (auth->password)
2007*8b26181fSAndroid Build Coastguard Worker {
2008*8b26181fSAndroid Build Coastguard Worker str_length = strlen(auth->password);
2009*8b26181fSAndroid Build Coastguard Worker if (str_length > 65535)
2010*8b26181fSAndroid Build Coastguard Worker {
2011*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE, "Password is too long (> 65535 bytes)");
2012*8b26181fSAndroid Build Coastguard Worker return -1;
2013*8b26181fSAndroid Build Coastguard Worker }
2014*8b26181fSAndroid Build Coastguard Worker length += (uint16)str_length;
2015*8b26181fSAndroid Build Coastguard Worker }
2016*8b26181fSAndroid Build Coastguard Worker break;
2017*8b26181fSAndroid Build Coastguard Worker
2018*8b26181fSAndroid Build Coastguard Worker default:
2019*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE, "Authentication type not recognized.");
2020*8b26181fSAndroid Build Coastguard Worker return -1;
2021*8b26181fSAndroid Build Coastguard Worker }
2022*8b26181fSAndroid Build Coastguard Worker
2023*8b26181fSAndroid Build Coastguard Worker auth_type = (uint16)auth->type;
2024*8b26181fSAndroid Build Coastguard Worker }
2025*8b26181fSAndroid Build Coastguard Worker else
2026*8b26181fSAndroid Build Coastguard Worker {
2027*8b26181fSAndroid Build Coastguard Worker auth_type = RPCAP_RMTAUTH_NULL;
2028*8b26181fSAndroid Build Coastguard Worker length = sizeof(struct rpcap_auth);
2029*8b26181fSAndroid Build Coastguard Worker }
2030*8b26181fSAndroid Build Coastguard Worker
2031*8b26181fSAndroid Build Coastguard Worker if (sock_bufferize(NULL, sizeof(struct rpcap_header), NULL,
2032*8b26181fSAndroid Build Coastguard Worker &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errbuf, PCAP_ERRBUF_SIZE))
2033*8b26181fSAndroid Build Coastguard Worker return -1;
2034*8b26181fSAndroid Build Coastguard Worker
2035*8b26181fSAndroid Build Coastguard Worker rpcap_createhdr((struct rpcap_header *) sendbuf, 0,
2036*8b26181fSAndroid Build Coastguard Worker RPCAP_MSG_AUTH_REQ, 0, length);
2037*8b26181fSAndroid Build Coastguard Worker
2038*8b26181fSAndroid Build Coastguard Worker rpauth = (struct rpcap_auth *) &sendbuf[sendbufidx];
2039*8b26181fSAndroid Build Coastguard Worker
2040*8b26181fSAndroid Build Coastguard Worker if (sock_bufferize(NULL, sizeof(struct rpcap_auth), NULL,
2041*8b26181fSAndroid Build Coastguard Worker &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errbuf, PCAP_ERRBUF_SIZE))
2042*8b26181fSAndroid Build Coastguard Worker return -1;
2043*8b26181fSAndroid Build Coastguard Worker
2044*8b26181fSAndroid Build Coastguard Worker memset(rpauth, 0, sizeof(struct rpcap_auth));
2045*8b26181fSAndroid Build Coastguard Worker
2046*8b26181fSAndroid Build Coastguard Worker rpauth->type = htons(auth_type);
2047*8b26181fSAndroid Build Coastguard Worker
2048*8b26181fSAndroid Build Coastguard Worker if (auth_type == RPCAP_RMTAUTH_PWD)
2049*8b26181fSAndroid Build Coastguard Worker {
2050*8b26181fSAndroid Build Coastguard Worker if (auth->username)
2051*8b26181fSAndroid Build Coastguard Worker rpauth->slen1 = (uint16)strlen(auth->username);
2052*8b26181fSAndroid Build Coastguard Worker else
2053*8b26181fSAndroid Build Coastguard Worker rpauth->slen1 = 0;
2054*8b26181fSAndroid Build Coastguard Worker
2055*8b26181fSAndroid Build Coastguard Worker if (sock_bufferize(auth->username, rpauth->slen1, sendbuf,
2056*8b26181fSAndroid Build Coastguard Worker &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_BUFFERIZE, errbuf, PCAP_ERRBUF_SIZE))
2057*8b26181fSAndroid Build Coastguard Worker return -1;
2058*8b26181fSAndroid Build Coastguard Worker
2059*8b26181fSAndroid Build Coastguard Worker if (auth->password)
2060*8b26181fSAndroid Build Coastguard Worker rpauth->slen2 = (uint16)strlen(auth->password);
2061*8b26181fSAndroid Build Coastguard Worker else
2062*8b26181fSAndroid Build Coastguard Worker rpauth->slen2 = 0;
2063*8b26181fSAndroid Build Coastguard Worker
2064*8b26181fSAndroid Build Coastguard Worker if (sock_bufferize(auth->password, rpauth->slen2, sendbuf,
2065*8b26181fSAndroid Build Coastguard Worker &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_BUFFERIZE, errbuf, PCAP_ERRBUF_SIZE))
2066*8b26181fSAndroid Build Coastguard Worker return -1;
2067*8b26181fSAndroid Build Coastguard Worker
2068*8b26181fSAndroid Build Coastguard Worker rpauth->slen1 = htons(rpauth->slen1);
2069*8b26181fSAndroid Build Coastguard Worker rpauth->slen2 = htons(rpauth->slen2);
2070*8b26181fSAndroid Build Coastguard Worker }
2071*8b26181fSAndroid Build Coastguard Worker
2072*8b26181fSAndroid Build Coastguard Worker if (sock_send(sockctrl, ssl, sendbuf, sendbufidx, errbuf,
2073*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE) < 0)
2074*8b26181fSAndroid Build Coastguard Worker return -1;
2075*8b26181fSAndroid Build Coastguard Worker
2076*8b26181fSAndroid Build Coastguard Worker /* Receive and process the reply message header */
2077*8b26181fSAndroid Build Coastguard Worker if (rpcap_process_msg_header(sockctrl, ssl, 0, RPCAP_MSG_AUTH_REQ,
2078*8b26181fSAndroid Build Coastguard Worker &header, errbuf) == -1)
2079*8b26181fSAndroid Build Coastguard Worker return -1;
2080*8b26181fSAndroid Build Coastguard Worker
2081*8b26181fSAndroid Build Coastguard Worker /*
2082*8b26181fSAndroid Build Coastguard Worker * OK, it's an authentication reply, so we're logged in.
2083*8b26181fSAndroid Build Coastguard Worker *
2084*8b26181fSAndroid Build Coastguard Worker * Did it send any additional information?
2085*8b26181fSAndroid Build Coastguard Worker */
2086*8b26181fSAndroid Build Coastguard Worker plen = header.plen;
2087*8b26181fSAndroid Build Coastguard Worker if (plen != 0)
2088*8b26181fSAndroid Build Coastguard Worker {
2089*8b26181fSAndroid Build Coastguard Worker size_t reply_len;
2090*8b26181fSAndroid Build Coastguard Worker
2091*8b26181fSAndroid Build Coastguard Worker /* Yes - is it big enough to include version information? */
2092*8b26181fSAndroid Build Coastguard Worker if (plen < sizeof(struct rpcap_authreply_old))
2093*8b26181fSAndroid Build Coastguard Worker {
2094*8b26181fSAndroid Build Coastguard Worker /* No - discard it and fail. */
2095*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE,
2096*8b26181fSAndroid Build Coastguard Worker "Authenticaton reply from server is too short");
2097*8b26181fSAndroid Build Coastguard Worker (void)rpcap_discard(sockctrl, ssl, plen, NULL);
2098*8b26181fSAndroid Build Coastguard Worker return -1;
2099*8b26181fSAndroid Build Coastguard Worker }
2100*8b26181fSAndroid Build Coastguard Worker
2101*8b26181fSAndroid Build Coastguard Worker /* Yes - does it include server byte order information? */
2102*8b26181fSAndroid Build Coastguard Worker if (plen == sizeof(struct rpcap_authreply_old))
2103*8b26181fSAndroid Build Coastguard Worker {
2104*8b26181fSAndroid Build Coastguard Worker /* No - just read the version information */
2105*8b26181fSAndroid Build Coastguard Worker has_byte_order = 0;
2106*8b26181fSAndroid Build Coastguard Worker reply_len = sizeof(struct rpcap_authreply_old);
2107*8b26181fSAndroid Build Coastguard Worker }
2108*8b26181fSAndroid Build Coastguard Worker else if (plen >= sizeof(struct rpcap_authreply_old))
2109*8b26181fSAndroid Build Coastguard Worker {
2110*8b26181fSAndroid Build Coastguard Worker /* Yes - read it all. */
2111*8b26181fSAndroid Build Coastguard Worker has_byte_order = 1;
2112*8b26181fSAndroid Build Coastguard Worker reply_len = sizeof(struct rpcap_authreply);
2113*8b26181fSAndroid Build Coastguard Worker }
2114*8b26181fSAndroid Build Coastguard Worker else
2115*8b26181fSAndroid Build Coastguard Worker {
2116*8b26181fSAndroid Build Coastguard Worker /*
2117*8b26181fSAndroid Build Coastguard Worker * Too long for old reply, too short for new reply.
2118*8b26181fSAndroid Build Coastguard Worker * Discard it and fail.
2119*8b26181fSAndroid Build Coastguard Worker */
2120*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE,
2121*8b26181fSAndroid Build Coastguard Worker "Authenticaton reply from server is too short");
2122*8b26181fSAndroid Build Coastguard Worker (void)rpcap_discard(sockctrl, ssl, plen, NULL);
2123*8b26181fSAndroid Build Coastguard Worker return -1;
2124*8b26181fSAndroid Build Coastguard Worker }
2125*8b26181fSAndroid Build Coastguard Worker
2126*8b26181fSAndroid Build Coastguard Worker /* Read the reply body */
2127*8b26181fSAndroid Build Coastguard Worker if (rpcap_recv(sockctrl, ssl, (char *)&authreply,
2128*8b26181fSAndroid Build Coastguard Worker reply_len, &plen, errbuf) == -1)
2129*8b26181fSAndroid Build Coastguard Worker {
2130*8b26181fSAndroid Build Coastguard Worker (void)rpcap_discard(sockctrl, ssl, plen, NULL);
2131*8b26181fSAndroid Build Coastguard Worker return -1;
2132*8b26181fSAndroid Build Coastguard Worker }
2133*8b26181fSAndroid Build Coastguard Worker
2134*8b26181fSAndroid Build Coastguard Worker /* Discard the rest of the message, if there is any. */
2135*8b26181fSAndroid Build Coastguard Worker if (rpcap_discard(sockctrl, ssl, plen, errbuf) == -1)
2136*8b26181fSAndroid Build Coastguard Worker return -1;
2137*8b26181fSAndroid Build Coastguard Worker
2138*8b26181fSAndroid Build Coastguard Worker /*
2139*8b26181fSAndroid Build Coastguard Worker * Check the minimum and maximum versions for sanity;
2140*8b26181fSAndroid Build Coastguard Worker * the minimum must be <= the maximum.
2141*8b26181fSAndroid Build Coastguard Worker */
2142*8b26181fSAndroid Build Coastguard Worker if (authreply.minvers > authreply.maxvers)
2143*8b26181fSAndroid Build Coastguard Worker {
2144*8b26181fSAndroid Build Coastguard Worker /*
2145*8b26181fSAndroid Build Coastguard Worker * Bogus - give up on this server.
2146*8b26181fSAndroid Build Coastguard Worker */
2147*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE,
2148*8b26181fSAndroid Build Coastguard Worker "The server's minimum supported protocol version is greater than its maximum supported protocol version");
2149*8b26181fSAndroid Build Coastguard Worker return -1;
2150*8b26181fSAndroid Build Coastguard Worker }
2151*8b26181fSAndroid Build Coastguard Worker
2152*8b26181fSAndroid Build Coastguard Worker if (has_byte_order)
2153*8b26181fSAndroid Build Coastguard Worker {
2154*8b26181fSAndroid Build Coastguard Worker their_byte_order_magic = authreply.byte_order_magic;
2155*8b26181fSAndroid Build Coastguard Worker }
2156*8b26181fSAndroid Build Coastguard Worker else
2157*8b26181fSAndroid Build Coastguard Worker {
2158*8b26181fSAndroid Build Coastguard Worker /*
2159*8b26181fSAndroid Build Coastguard Worker * The server didn't tell us what its byte
2160*8b26181fSAndroid Build Coastguard Worker * order is; assume it's ours.
2161*8b26181fSAndroid Build Coastguard Worker */
2162*8b26181fSAndroid Build Coastguard Worker their_byte_order_magic = RPCAP_BYTE_ORDER_MAGIC;
2163*8b26181fSAndroid Build Coastguard Worker }
2164*8b26181fSAndroid Build Coastguard Worker }
2165*8b26181fSAndroid Build Coastguard Worker else
2166*8b26181fSAndroid Build Coastguard Worker {
2167*8b26181fSAndroid Build Coastguard Worker /* No - it supports only version 0. */
2168*8b26181fSAndroid Build Coastguard Worker authreply.minvers = 0;
2169*8b26181fSAndroid Build Coastguard Worker authreply.maxvers = 0;
2170*8b26181fSAndroid Build Coastguard Worker
2171*8b26181fSAndroid Build Coastguard Worker /*
2172*8b26181fSAndroid Build Coastguard Worker * And it didn't tell us what its byte order is; assume
2173*8b26181fSAndroid Build Coastguard Worker * it's ours.
2174*8b26181fSAndroid Build Coastguard Worker */
2175*8b26181fSAndroid Build Coastguard Worker has_byte_order = 0;
2176*8b26181fSAndroid Build Coastguard Worker their_byte_order_magic = RPCAP_BYTE_ORDER_MAGIC;
2177*8b26181fSAndroid Build Coastguard Worker }
2178*8b26181fSAndroid Build Coastguard Worker
2179*8b26181fSAndroid Build Coastguard Worker /*
2180*8b26181fSAndroid Build Coastguard Worker * OK, let's start with the maximum version the server supports.
2181*8b26181fSAndroid Build Coastguard Worker */
2182*8b26181fSAndroid Build Coastguard Worker ourvers = authreply.maxvers;
2183*8b26181fSAndroid Build Coastguard Worker
2184*8b26181fSAndroid Build Coastguard Worker #if RPCAP_MIN_VERSION != 0
2185*8b26181fSAndroid Build Coastguard Worker /*
2186*8b26181fSAndroid Build Coastguard Worker * If that's less than the minimum version we support, we
2187*8b26181fSAndroid Build Coastguard Worker * can't communicate.
2188*8b26181fSAndroid Build Coastguard Worker */
2189*8b26181fSAndroid Build Coastguard Worker if (ourvers < RPCAP_MIN_VERSION)
2190*8b26181fSAndroid Build Coastguard Worker goto novers;
2191*8b26181fSAndroid Build Coastguard Worker #endif
2192*8b26181fSAndroid Build Coastguard Worker
2193*8b26181fSAndroid Build Coastguard Worker /*
2194*8b26181fSAndroid Build Coastguard Worker * If that's greater than the maximum version we support,
2195*8b26181fSAndroid Build Coastguard Worker * choose the maximum version we support.
2196*8b26181fSAndroid Build Coastguard Worker */
2197*8b26181fSAndroid Build Coastguard Worker if (ourvers > RPCAP_MAX_VERSION)
2198*8b26181fSAndroid Build Coastguard Worker {
2199*8b26181fSAndroid Build Coastguard Worker ourvers = RPCAP_MAX_VERSION;
2200*8b26181fSAndroid Build Coastguard Worker
2201*8b26181fSAndroid Build Coastguard Worker /*
2202*8b26181fSAndroid Build Coastguard Worker * If that's less than the minimum version they
2203*8b26181fSAndroid Build Coastguard Worker * support, we can't communicate.
2204*8b26181fSAndroid Build Coastguard Worker */
2205*8b26181fSAndroid Build Coastguard Worker if (ourvers < authreply.minvers)
2206*8b26181fSAndroid Build Coastguard Worker goto novers;
2207*8b26181fSAndroid Build Coastguard Worker }
2208*8b26181fSAndroid Build Coastguard Worker
2209*8b26181fSAndroid Build Coastguard Worker /*
2210*8b26181fSAndroid Build Coastguard Worker * Is the server byte order the opposite of ours?
2211*8b26181fSAndroid Build Coastguard Worker */
2212*8b26181fSAndroid Build Coastguard Worker if (their_byte_order_magic == RPCAP_BYTE_ORDER_MAGIC)
2213*8b26181fSAndroid Build Coastguard Worker {
2214*8b26181fSAndroid Build Coastguard Worker /* No, it's the same. */
2215*8b26181fSAndroid Build Coastguard Worker *byte_swapped = 0;
2216*8b26181fSAndroid Build Coastguard Worker }
2217*8b26181fSAndroid Build Coastguard Worker else if (their_byte_order_magic == RPCAP_BYTE_ORDER_MAGIC_SWAPPED)
2218*8b26181fSAndroid Build Coastguard Worker {
2219*8b26181fSAndroid Build Coastguard Worker /* Yes, it's the opposite of ours. */
2220*8b26181fSAndroid Build Coastguard Worker *byte_swapped = 1;
2221*8b26181fSAndroid Build Coastguard Worker }
2222*8b26181fSAndroid Build Coastguard Worker else
2223*8b26181fSAndroid Build Coastguard Worker {
2224*8b26181fSAndroid Build Coastguard Worker /* They sent us something bogus. */
2225*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE,
2226*8b26181fSAndroid Build Coastguard Worker "The server did not send us a valid byte order value");
2227*8b26181fSAndroid Build Coastguard Worker return -1;
2228*8b26181fSAndroid Build Coastguard Worker }
2229*8b26181fSAndroid Build Coastguard Worker
2230*8b26181fSAndroid Build Coastguard Worker *ver = ourvers;
2231*8b26181fSAndroid Build Coastguard Worker return 0;
2232*8b26181fSAndroid Build Coastguard Worker
2233*8b26181fSAndroid Build Coastguard Worker novers:
2234*8b26181fSAndroid Build Coastguard Worker /*
2235*8b26181fSAndroid Build Coastguard Worker * There is no version we both support; that is a fatal error.
2236*8b26181fSAndroid Build Coastguard Worker */
2237*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE,
2238*8b26181fSAndroid Build Coastguard Worker "The server doesn't support any protocol version that we support");
2239*8b26181fSAndroid Build Coastguard Worker return -1;
2240*8b26181fSAndroid Build Coastguard Worker }
2241*8b26181fSAndroid Build Coastguard Worker
2242*8b26181fSAndroid Build Coastguard Worker /* We don't currently support non-blocking mode. */
2243*8b26181fSAndroid Build Coastguard Worker static int
pcap_getnonblock_rpcap(pcap_t * p)2244*8b26181fSAndroid Build Coastguard Worker pcap_getnonblock_rpcap(pcap_t *p)
2245*8b26181fSAndroid Build Coastguard Worker {
2246*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2247*8b26181fSAndroid Build Coastguard Worker "Non-blocking mode isn't supported for capturing remotely with rpcap");
2248*8b26181fSAndroid Build Coastguard Worker return (-1);
2249*8b26181fSAndroid Build Coastguard Worker }
2250*8b26181fSAndroid Build Coastguard Worker
2251*8b26181fSAndroid Build Coastguard Worker static int
pcap_setnonblock_rpcap(pcap_t * p,int nonblock _U_)2252*8b26181fSAndroid Build Coastguard Worker pcap_setnonblock_rpcap(pcap_t *p, int nonblock _U_)
2253*8b26181fSAndroid Build Coastguard Worker {
2254*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2255*8b26181fSAndroid Build Coastguard Worker "Non-blocking mode isn't supported for capturing remotely with rpcap");
2256*8b26181fSAndroid Build Coastguard Worker return (-1);
2257*8b26181fSAndroid Build Coastguard Worker }
2258*8b26181fSAndroid Build Coastguard Worker
2259*8b26181fSAndroid Build Coastguard Worker static int
rpcap_setup_session(const char * source,struct pcap_rmtauth * auth,int * activep,SOCKET * sockctrlp,uint8 * uses_sslp,SSL ** sslp,int rmt_flags,uint8 * protocol_versionp,int * byte_swappedp,char * host,char * port,char * iface,char * errbuf)2260*8b26181fSAndroid Build Coastguard Worker rpcap_setup_session(const char *source, struct pcap_rmtauth *auth,
2261*8b26181fSAndroid Build Coastguard Worker int *activep, SOCKET *sockctrlp, uint8 *uses_sslp, SSL **sslp,
2262*8b26181fSAndroid Build Coastguard Worker int rmt_flags, uint8 *protocol_versionp, int *byte_swappedp,
2263*8b26181fSAndroid Build Coastguard Worker char *host, char *port, char *iface, char *errbuf)
2264*8b26181fSAndroid Build Coastguard Worker {
2265*8b26181fSAndroid Build Coastguard Worker int type;
2266*8b26181fSAndroid Build Coastguard Worker struct activehosts *activeconn; /* active connection, if there is one */
2267*8b26181fSAndroid Build Coastguard Worker int error; /* 1 if rpcap_remoteact_getsock got an error */
2268*8b26181fSAndroid Build Coastguard Worker
2269*8b26181fSAndroid Build Coastguard Worker /*
2270*8b26181fSAndroid Build Coastguard Worker * Determine the type of the source (NULL, file, local, remote).
2271*8b26181fSAndroid Build Coastguard Worker * You must have a valid source string even if we're in active mode,
2272*8b26181fSAndroid Build Coastguard Worker * because otherwise the call to the following function will fail.
2273*8b26181fSAndroid Build Coastguard Worker */
2274*8b26181fSAndroid Build Coastguard Worker if (pcap_parsesrcstr_ex(source, &type, host, port, iface, uses_sslp,
2275*8b26181fSAndroid Build Coastguard Worker errbuf) == -1)
2276*8b26181fSAndroid Build Coastguard Worker return -1;
2277*8b26181fSAndroid Build Coastguard Worker
2278*8b26181fSAndroid Build Coastguard Worker /*
2279*8b26181fSAndroid Build Coastguard Worker * It must be remote.
2280*8b26181fSAndroid Build Coastguard Worker */
2281*8b26181fSAndroid Build Coastguard Worker if (type != PCAP_SRC_IFREMOTE)
2282*8b26181fSAndroid Build Coastguard Worker {
2283*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE,
2284*8b26181fSAndroid Build Coastguard Worker "Non-remote interface passed to remote capture routine");
2285*8b26181fSAndroid Build Coastguard Worker return -1;
2286*8b26181fSAndroid Build Coastguard Worker }
2287*8b26181fSAndroid Build Coastguard Worker
2288*8b26181fSAndroid Build Coastguard Worker /*
2289*8b26181fSAndroid Build Coastguard Worker * We don't yet support DTLS, so if the user asks for a TLS
2290*8b26181fSAndroid Build Coastguard Worker * connection and asks for data packets to be sent over UDP,
2291*8b26181fSAndroid Build Coastguard Worker * we have to give up.
2292*8b26181fSAndroid Build Coastguard Worker */
2293*8b26181fSAndroid Build Coastguard Worker if (*uses_sslp && (rmt_flags & PCAP_OPENFLAG_DATATX_UDP))
2294*8b26181fSAndroid Build Coastguard Worker {
2295*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE,
2296*8b26181fSAndroid Build Coastguard Worker "TLS not supported with UDP forward of remote packets");
2297*8b26181fSAndroid Build Coastguard Worker return -1;
2298*8b26181fSAndroid Build Coastguard Worker }
2299*8b26181fSAndroid Build Coastguard Worker
2300*8b26181fSAndroid Build Coastguard Worker /* Warning: this call can be the first one called by the user. */
2301*8b26181fSAndroid Build Coastguard Worker /* For this reason, we have to initialize the Winsock support. */
2302*8b26181fSAndroid Build Coastguard Worker if (sock_init(errbuf, PCAP_ERRBUF_SIZE) == -1)
2303*8b26181fSAndroid Build Coastguard Worker return -1;
2304*8b26181fSAndroid Build Coastguard Worker
2305*8b26181fSAndroid Build Coastguard Worker /* Check for active mode */
2306*8b26181fSAndroid Build Coastguard Worker activeconn = rpcap_remoteact_getsock(host, &error, errbuf);
2307*8b26181fSAndroid Build Coastguard Worker if (activeconn != NULL)
2308*8b26181fSAndroid Build Coastguard Worker {
2309*8b26181fSAndroid Build Coastguard Worker *activep = 1;
2310*8b26181fSAndroid Build Coastguard Worker *sockctrlp = activeconn->sockctrl;
2311*8b26181fSAndroid Build Coastguard Worker *sslp = activeconn->ssl;
2312*8b26181fSAndroid Build Coastguard Worker *protocol_versionp = activeconn->protocol_version;
2313*8b26181fSAndroid Build Coastguard Worker *byte_swappedp = activeconn->byte_swapped;
2314*8b26181fSAndroid Build Coastguard Worker }
2315*8b26181fSAndroid Build Coastguard Worker else
2316*8b26181fSAndroid Build Coastguard Worker {
2317*8b26181fSAndroid Build Coastguard Worker *activep = 0;
2318*8b26181fSAndroid Build Coastguard Worker struct addrinfo hints; /* temp variable needed to resolve hostnames into to socket representation */
2319*8b26181fSAndroid Build Coastguard Worker struct addrinfo *addrinfo; /* temp variable needed to resolve hostnames into to socket representation */
2320*8b26181fSAndroid Build Coastguard Worker
2321*8b26181fSAndroid Build Coastguard Worker if (error)
2322*8b26181fSAndroid Build Coastguard Worker {
2323*8b26181fSAndroid Build Coastguard Worker /*
2324*8b26181fSAndroid Build Coastguard Worker * Call failed.
2325*8b26181fSAndroid Build Coastguard Worker */
2326*8b26181fSAndroid Build Coastguard Worker return -1;
2327*8b26181fSAndroid Build Coastguard Worker }
2328*8b26181fSAndroid Build Coastguard Worker
2329*8b26181fSAndroid Build Coastguard Worker /*
2330*8b26181fSAndroid Build Coastguard Worker * We're not in active mode; let's try to open a new
2331*8b26181fSAndroid Build Coastguard Worker * control connection.
2332*8b26181fSAndroid Build Coastguard Worker */
2333*8b26181fSAndroid Build Coastguard Worker memset(&hints, 0, sizeof(struct addrinfo));
2334*8b26181fSAndroid Build Coastguard Worker hints.ai_family = PF_UNSPEC;
2335*8b26181fSAndroid Build Coastguard Worker hints.ai_socktype = SOCK_STREAM;
2336*8b26181fSAndroid Build Coastguard Worker
2337*8b26181fSAndroid Build Coastguard Worker if (port[0] == 0)
2338*8b26181fSAndroid Build Coastguard Worker {
2339*8b26181fSAndroid Build Coastguard Worker /* the user chose not to specify the port */
2340*8b26181fSAndroid Build Coastguard Worker if (sock_initaddress(host, RPCAP_DEFAULT_NETPORT,
2341*8b26181fSAndroid Build Coastguard Worker &hints, &addrinfo, errbuf, PCAP_ERRBUF_SIZE) == -1)
2342*8b26181fSAndroid Build Coastguard Worker return -1;
2343*8b26181fSAndroid Build Coastguard Worker }
2344*8b26181fSAndroid Build Coastguard Worker else
2345*8b26181fSAndroid Build Coastguard Worker {
2346*8b26181fSAndroid Build Coastguard Worker if (sock_initaddress(host, port, &hints, &addrinfo,
2347*8b26181fSAndroid Build Coastguard Worker errbuf, PCAP_ERRBUF_SIZE) == -1)
2348*8b26181fSAndroid Build Coastguard Worker return -1;
2349*8b26181fSAndroid Build Coastguard Worker }
2350*8b26181fSAndroid Build Coastguard Worker
2351*8b26181fSAndroid Build Coastguard Worker if ((*sockctrlp = sock_open(host, addrinfo, SOCKOPEN_CLIENT, 0,
2352*8b26181fSAndroid Build Coastguard Worker errbuf, PCAP_ERRBUF_SIZE)) == INVALID_SOCKET)
2353*8b26181fSAndroid Build Coastguard Worker {
2354*8b26181fSAndroid Build Coastguard Worker freeaddrinfo(addrinfo);
2355*8b26181fSAndroid Build Coastguard Worker return -1;
2356*8b26181fSAndroid Build Coastguard Worker }
2357*8b26181fSAndroid Build Coastguard Worker
2358*8b26181fSAndroid Build Coastguard Worker /* addrinfo is no longer used */
2359*8b26181fSAndroid Build Coastguard Worker freeaddrinfo(addrinfo);
2360*8b26181fSAndroid Build Coastguard Worker addrinfo = NULL;
2361*8b26181fSAndroid Build Coastguard Worker
2362*8b26181fSAndroid Build Coastguard Worker if (*uses_sslp)
2363*8b26181fSAndroid Build Coastguard Worker {
2364*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OPENSSL
2365*8b26181fSAndroid Build Coastguard Worker *sslp = ssl_promotion(0, *sockctrlp, errbuf,
2366*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE);
2367*8b26181fSAndroid Build Coastguard Worker if (!*sslp)
2368*8b26181fSAndroid Build Coastguard Worker {
2369*8b26181fSAndroid Build Coastguard Worker sock_close(*sockctrlp, NULL, 0);
2370*8b26181fSAndroid Build Coastguard Worker return -1;
2371*8b26181fSAndroid Build Coastguard Worker }
2372*8b26181fSAndroid Build Coastguard Worker #else
2373*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE,
2374*8b26181fSAndroid Build Coastguard Worker "No TLS support");
2375*8b26181fSAndroid Build Coastguard Worker sock_close(*sockctrlp, NULL, 0);
2376*8b26181fSAndroid Build Coastguard Worker return -1;
2377*8b26181fSAndroid Build Coastguard Worker #endif
2378*8b26181fSAndroid Build Coastguard Worker }
2379*8b26181fSAndroid Build Coastguard Worker
2380*8b26181fSAndroid Build Coastguard Worker if (rpcap_doauth(*sockctrlp, *sslp, protocol_versionp,
2381*8b26181fSAndroid Build Coastguard Worker byte_swappedp, auth, errbuf) == -1)
2382*8b26181fSAndroid Build Coastguard Worker {
2383*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OPENSSL
2384*8b26181fSAndroid Build Coastguard Worker if (*sslp)
2385*8b26181fSAndroid Build Coastguard Worker {
2386*8b26181fSAndroid Build Coastguard Worker // Finish using the SSL handle for the socket.
2387*8b26181fSAndroid Build Coastguard Worker // This must be done *before* the socket is
2388*8b26181fSAndroid Build Coastguard Worker // closed.
2389*8b26181fSAndroid Build Coastguard Worker ssl_finish(*sslp);
2390*8b26181fSAndroid Build Coastguard Worker }
2391*8b26181fSAndroid Build Coastguard Worker #endif
2392*8b26181fSAndroid Build Coastguard Worker sock_close(*sockctrlp, NULL, 0);
2393*8b26181fSAndroid Build Coastguard Worker return -1;
2394*8b26181fSAndroid Build Coastguard Worker }
2395*8b26181fSAndroid Build Coastguard Worker }
2396*8b26181fSAndroid Build Coastguard Worker return 0;
2397*8b26181fSAndroid Build Coastguard Worker }
2398*8b26181fSAndroid Build Coastguard Worker
2399*8b26181fSAndroid Build Coastguard Worker /*
2400*8b26181fSAndroid Build Coastguard Worker * This function opens a remote adapter by opening an RPCAP connection and
2401*8b26181fSAndroid Build Coastguard Worker * so on.
2402*8b26181fSAndroid Build Coastguard Worker *
2403*8b26181fSAndroid Build Coastguard Worker * It does the job of pcap_open_live() for a remote interface; it's called
2404*8b26181fSAndroid Build Coastguard Worker * by pcap_open() for remote interfaces.
2405*8b26181fSAndroid Build Coastguard Worker *
2406*8b26181fSAndroid Build Coastguard Worker * We do not start the capture until pcap_startcapture_remote() is called.
2407*8b26181fSAndroid Build Coastguard Worker *
2408*8b26181fSAndroid Build Coastguard Worker * This is because, when doing a remote capture, we cannot start capturing
2409*8b26181fSAndroid Build Coastguard Worker * data as soon as the 'open adapter' command is sent. Suppose the remote
2410*8b26181fSAndroid Build Coastguard Worker * adapter is already overloaded; if we start a capture (which, by default,
2411*8b26181fSAndroid Build Coastguard Worker * has a NULL filter) the new traffic can saturate the network.
2412*8b26181fSAndroid Build Coastguard Worker *
2413*8b26181fSAndroid Build Coastguard Worker * Instead, we want to "open" the adapter, then send a "start capture"
2414*8b26181fSAndroid Build Coastguard Worker * command only when we're ready to start the capture.
2415*8b26181fSAndroid Build Coastguard Worker * This function does this job: it sends an "open adapter" command
2416*8b26181fSAndroid Build Coastguard Worker * (according to the RPCAP protocol), but it does not start the capture.
2417*8b26181fSAndroid Build Coastguard Worker *
2418*8b26181fSAndroid Build Coastguard Worker * Since the other libpcap functions do not share this way of life, we
2419*8b26181fSAndroid Build Coastguard Worker * have to do some dirty things in order to make everything work.
2420*8b26181fSAndroid Build Coastguard Worker *
2421*8b26181fSAndroid Build Coastguard Worker * \param source: see pcap_open().
2422*8b26181fSAndroid Build Coastguard Worker * \param snaplen: see pcap_open().
2423*8b26181fSAndroid Build Coastguard Worker * \param flags: see pcap_open().
2424*8b26181fSAndroid Build Coastguard Worker * \param read_timeout: see pcap_open().
2425*8b26181fSAndroid Build Coastguard Worker * \param auth: see pcap_open().
2426*8b26181fSAndroid Build Coastguard Worker * \param errbuf: see pcap_open().
2427*8b26181fSAndroid Build Coastguard Worker *
2428*8b26181fSAndroid Build Coastguard Worker * \return a pcap_t pointer in case of success, NULL otherwise. In case of
2429*8b26181fSAndroid Build Coastguard Worker * success, the pcap_t pointer can be used as a parameter to the following
2430*8b26181fSAndroid Build Coastguard Worker * calls (pcap_compile() and so on). In case of problems, errbuf contains
2431*8b26181fSAndroid Build Coastguard Worker * a text explanation of error.
2432*8b26181fSAndroid Build Coastguard Worker *
2433*8b26181fSAndroid Build Coastguard Worker * WARNING: In case we call pcap_compile() and the capture has not yet
2434*8b26181fSAndroid Build Coastguard Worker * been started, the filter will be saved into the pcap_t structure,
2435*8b26181fSAndroid Build Coastguard Worker * and it will be sent to the other host later (when
2436*8b26181fSAndroid Build Coastguard Worker * pcap_startcapture_remote() is called).
2437*8b26181fSAndroid Build Coastguard Worker */
pcap_open_rpcap(const char * source,int snaplen,int flags,int read_timeout,struct pcap_rmtauth * auth,char * errbuf)2438*8b26181fSAndroid Build Coastguard Worker pcap_t *pcap_open_rpcap(const char *source, int snaplen, int flags, int read_timeout, struct pcap_rmtauth *auth, char *errbuf)
2439*8b26181fSAndroid Build Coastguard Worker {
2440*8b26181fSAndroid Build Coastguard Worker pcap_t *fp;
2441*8b26181fSAndroid Build Coastguard Worker char *source_str;
2442*8b26181fSAndroid Build Coastguard Worker struct pcap_rpcap *pr; /* structure used when doing a remote live capture */
2443*8b26181fSAndroid Build Coastguard Worker char host[PCAP_BUF_SIZE], ctrlport[PCAP_BUF_SIZE], iface[PCAP_BUF_SIZE];
2444*8b26181fSAndroid Build Coastguard Worker SOCKET sockctrl;
2445*8b26181fSAndroid Build Coastguard Worker SSL *ssl = NULL;
2446*8b26181fSAndroid Build Coastguard Worker uint8 protocol_version; /* negotiated protocol version */
2447*8b26181fSAndroid Build Coastguard Worker int byte_swapped; /* server is known to be byte-swapped */
2448*8b26181fSAndroid Build Coastguard Worker int active;
2449*8b26181fSAndroid Build Coastguard Worker uint32 plen;
2450*8b26181fSAndroid Build Coastguard Worker char sendbuf[RPCAP_NETBUF_SIZE]; /* temporary buffer in which data to be sent is buffered */
2451*8b26181fSAndroid Build Coastguard Worker int sendbufidx = 0; /* index which keeps the number of bytes currently buffered */
2452*8b26181fSAndroid Build Coastguard Worker
2453*8b26181fSAndroid Build Coastguard Worker /* RPCAP-related variables */
2454*8b26181fSAndroid Build Coastguard Worker struct rpcap_header header; /* header of the RPCAP packet */
2455*8b26181fSAndroid Build Coastguard Worker struct rpcap_openreply openreply; /* open reply message */
2456*8b26181fSAndroid Build Coastguard Worker
2457*8b26181fSAndroid Build Coastguard Worker fp = PCAP_CREATE_COMMON(errbuf, struct pcap_rpcap);
2458*8b26181fSAndroid Build Coastguard Worker if (fp == NULL)
2459*8b26181fSAndroid Build Coastguard Worker {
2460*8b26181fSAndroid Build Coastguard Worker return NULL;
2461*8b26181fSAndroid Build Coastguard Worker }
2462*8b26181fSAndroid Build Coastguard Worker source_str = strdup(source);
2463*8b26181fSAndroid Build Coastguard Worker if (source_str == NULL) {
2464*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
2465*8b26181fSAndroid Build Coastguard Worker errno, "malloc");
2466*8b26181fSAndroid Build Coastguard Worker return NULL;
2467*8b26181fSAndroid Build Coastguard Worker }
2468*8b26181fSAndroid Build Coastguard Worker
2469*8b26181fSAndroid Build Coastguard Worker /*
2470*8b26181fSAndroid Build Coastguard Worker * Turn a negative snapshot value (invalid), a snapshot value of
2471*8b26181fSAndroid Build Coastguard Worker * 0 (unspecified), or a value bigger than the normal maximum
2472*8b26181fSAndroid Build Coastguard Worker * value, into the maximum allowed value.
2473*8b26181fSAndroid Build Coastguard Worker *
2474*8b26181fSAndroid Build Coastguard Worker * If some application really *needs* a bigger snapshot
2475*8b26181fSAndroid Build Coastguard Worker * length, we should just increase MAXIMUM_SNAPLEN.
2476*8b26181fSAndroid Build Coastguard Worker *
2477*8b26181fSAndroid Build Coastguard Worker * XXX - should we leave this up to the remote server to
2478*8b26181fSAndroid Build Coastguard Worker * do?
2479*8b26181fSAndroid Build Coastguard Worker */
2480*8b26181fSAndroid Build Coastguard Worker if (snaplen <= 0 || snaplen > MAXIMUM_SNAPLEN)
2481*8b26181fSAndroid Build Coastguard Worker snaplen = MAXIMUM_SNAPLEN;
2482*8b26181fSAndroid Build Coastguard Worker
2483*8b26181fSAndroid Build Coastguard Worker fp->opt.device = source_str;
2484*8b26181fSAndroid Build Coastguard Worker fp->snapshot = snaplen;
2485*8b26181fSAndroid Build Coastguard Worker fp->opt.timeout = read_timeout;
2486*8b26181fSAndroid Build Coastguard Worker pr = fp->priv;
2487*8b26181fSAndroid Build Coastguard Worker pr->rmt_flags = flags;
2488*8b26181fSAndroid Build Coastguard Worker
2489*8b26181fSAndroid Build Coastguard Worker /*
2490*8b26181fSAndroid Build Coastguard Worker * Attempt to set up the session with the server.
2491*8b26181fSAndroid Build Coastguard Worker */
2492*8b26181fSAndroid Build Coastguard Worker if (rpcap_setup_session(fp->opt.device, auth, &active, &sockctrl,
2493*8b26181fSAndroid Build Coastguard Worker &pr->uses_ssl, &ssl, flags, &protocol_version, &byte_swapped,
2494*8b26181fSAndroid Build Coastguard Worker host, ctrlport, iface, errbuf) == -1)
2495*8b26181fSAndroid Build Coastguard Worker {
2496*8b26181fSAndroid Build Coastguard Worker /* Session setup failed. */
2497*8b26181fSAndroid Build Coastguard Worker pcap_close(fp);
2498*8b26181fSAndroid Build Coastguard Worker return NULL;
2499*8b26181fSAndroid Build Coastguard Worker }
2500*8b26181fSAndroid Build Coastguard Worker
2501*8b26181fSAndroid Build Coastguard Worker /* All good so far, save the ssl handler */
2502*8b26181fSAndroid Build Coastguard Worker ssl_main = ssl;
2503*8b26181fSAndroid Build Coastguard Worker
2504*8b26181fSAndroid Build Coastguard Worker /*
2505*8b26181fSAndroid Build Coastguard Worker * Now it's time to start playing with the RPCAP protocol
2506*8b26181fSAndroid Build Coastguard Worker * RPCAP open command: create the request message
2507*8b26181fSAndroid Build Coastguard Worker */
2508*8b26181fSAndroid Build Coastguard Worker if (sock_bufferize(NULL, sizeof(struct rpcap_header), NULL,
2509*8b26181fSAndroid Build Coastguard Worker &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errbuf, PCAP_ERRBUF_SIZE))
2510*8b26181fSAndroid Build Coastguard Worker goto error_nodiscard;
2511*8b26181fSAndroid Build Coastguard Worker
2512*8b26181fSAndroid Build Coastguard Worker rpcap_createhdr((struct rpcap_header *) sendbuf, protocol_version,
2513*8b26181fSAndroid Build Coastguard Worker RPCAP_MSG_OPEN_REQ, 0, (uint32) strlen(iface));
2514*8b26181fSAndroid Build Coastguard Worker
2515*8b26181fSAndroid Build Coastguard Worker if (sock_bufferize(iface, (int) strlen(iface), sendbuf, &sendbufidx,
2516*8b26181fSAndroid Build Coastguard Worker RPCAP_NETBUF_SIZE, SOCKBUF_BUFFERIZE, errbuf, PCAP_ERRBUF_SIZE))
2517*8b26181fSAndroid Build Coastguard Worker goto error_nodiscard;
2518*8b26181fSAndroid Build Coastguard Worker
2519*8b26181fSAndroid Build Coastguard Worker if (sock_send(sockctrl, ssl, sendbuf, sendbufidx, errbuf,
2520*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE) < 0)
2521*8b26181fSAndroid Build Coastguard Worker goto error_nodiscard;
2522*8b26181fSAndroid Build Coastguard Worker
2523*8b26181fSAndroid Build Coastguard Worker /* Receive and process the reply message header. */
2524*8b26181fSAndroid Build Coastguard Worker if (rpcap_process_msg_header(sockctrl, ssl, protocol_version,
2525*8b26181fSAndroid Build Coastguard Worker RPCAP_MSG_OPEN_REQ, &header, errbuf) == -1)
2526*8b26181fSAndroid Build Coastguard Worker goto error_nodiscard;
2527*8b26181fSAndroid Build Coastguard Worker plen = header.plen;
2528*8b26181fSAndroid Build Coastguard Worker
2529*8b26181fSAndroid Build Coastguard Worker /* Read the reply body */
2530*8b26181fSAndroid Build Coastguard Worker if (rpcap_recv(sockctrl, ssl, (char *)&openreply,
2531*8b26181fSAndroid Build Coastguard Worker sizeof(struct rpcap_openreply), &plen, errbuf) == -1)
2532*8b26181fSAndroid Build Coastguard Worker goto error;
2533*8b26181fSAndroid Build Coastguard Worker
2534*8b26181fSAndroid Build Coastguard Worker /* Discard the rest of the message, if there is any. */
2535*8b26181fSAndroid Build Coastguard Worker if (rpcap_discard(sockctrl, ssl, plen, errbuf) == -1)
2536*8b26181fSAndroid Build Coastguard Worker goto error_nodiscard;
2537*8b26181fSAndroid Build Coastguard Worker
2538*8b26181fSAndroid Build Coastguard Worker /* Set proper fields into the pcap_t struct */
2539*8b26181fSAndroid Build Coastguard Worker fp->linktype = ntohl(openreply.linktype);
2540*8b26181fSAndroid Build Coastguard Worker pr->rmt_sockctrl = sockctrl;
2541*8b26181fSAndroid Build Coastguard Worker pr->ctrl_ssl = ssl;
2542*8b26181fSAndroid Build Coastguard Worker pr->protocol_version = protocol_version;
2543*8b26181fSAndroid Build Coastguard Worker pr->byte_swapped = byte_swapped;
2544*8b26181fSAndroid Build Coastguard Worker pr->rmt_clientside = 1;
2545*8b26181fSAndroid Build Coastguard Worker
2546*8b26181fSAndroid Build Coastguard Worker /* This code is duplicated from the end of this function */
2547*8b26181fSAndroid Build Coastguard Worker fp->read_op = pcap_read_rpcap;
2548*8b26181fSAndroid Build Coastguard Worker fp->save_current_filter_op = pcap_save_current_filter_rpcap;
2549*8b26181fSAndroid Build Coastguard Worker fp->setfilter_op = pcap_setfilter_rpcap;
2550*8b26181fSAndroid Build Coastguard Worker fp->getnonblock_op = pcap_getnonblock_rpcap;
2551*8b26181fSAndroid Build Coastguard Worker fp->setnonblock_op = pcap_setnonblock_rpcap;
2552*8b26181fSAndroid Build Coastguard Worker fp->stats_op = pcap_stats_rpcap;
2553*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
2554*8b26181fSAndroid Build Coastguard Worker fp->stats_ex_op = pcap_stats_ex_rpcap;
2555*8b26181fSAndroid Build Coastguard Worker #endif
2556*8b26181fSAndroid Build Coastguard Worker fp->cleanup_op = pcap_cleanup_rpcap;
2557*8b26181fSAndroid Build Coastguard Worker
2558*8b26181fSAndroid Build Coastguard Worker fp->activated = 1;
2559*8b26181fSAndroid Build Coastguard Worker return fp;
2560*8b26181fSAndroid Build Coastguard Worker
2561*8b26181fSAndroid Build Coastguard Worker error:
2562*8b26181fSAndroid Build Coastguard Worker /*
2563*8b26181fSAndroid Build Coastguard Worker * When the connection has been established, we have to close it. So, at the
2564*8b26181fSAndroid Build Coastguard Worker * beginning of this function, if an error occur we return immediately with
2565*8b26181fSAndroid Build Coastguard Worker * a return NULL; when the connection is established, we have to come here
2566*8b26181fSAndroid Build Coastguard Worker * ('goto error;') in order to close everything properly.
2567*8b26181fSAndroid Build Coastguard Worker */
2568*8b26181fSAndroid Build Coastguard Worker
2569*8b26181fSAndroid Build Coastguard Worker /*
2570*8b26181fSAndroid Build Coastguard Worker * Discard the rest of the message.
2571*8b26181fSAndroid Build Coastguard Worker * We already reported an error; if this gets an error, just
2572*8b26181fSAndroid Build Coastguard Worker * drive on.
2573*8b26181fSAndroid Build Coastguard Worker */
2574*8b26181fSAndroid Build Coastguard Worker (void)rpcap_discard(sockctrl, pr->ctrl_ssl, plen, NULL);
2575*8b26181fSAndroid Build Coastguard Worker
2576*8b26181fSAndroid Build Coastguard Worker error_nodiscard:
2577*8b26181fSAndroid Build Coastguard Worker if (!active)
2578*8b26181fSAndroid Build Coastguard Worker {
2579*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OPENSSL
2580*8b26181fSAndroid Build Coastguard Worker if (ssl)
2581*8b26181fSAndroid Build Coastguard Worker {
2582*8b26181fSAndroid Build Coastguard Worker // Finish using the SSL handle for the socket.
2583*8b26181fSAndroid Build Coastguard Worker // This must be done *before* the socket is closed.
2584*8b26181fSAndroid Build Coastguard Worker ssl_finish(ssl);
2585*8b26181fSAndroid Build Coastguard Worker }
2586*8b26181fSAndroid Build Coastguard Worker #endif
2587*8b26181fSAndroid Build Coastguard Worker sock_close(sockctrl, NULL, 0);
2588*8b26181fSAndroid Build Coastguard Worker }
2589*8b26181fSAndroid Build Coastguard Worker
2590*8b26181fSAndroid Build Coastguard Worker pcap_close(fp);
2591*8b26181fSAndroid Build Coastguard Worker return NULL;
2592*8b26181fSAndroid Build Coastguard Worker }
2593*8b26181fSAndroid Build Coastguard Worker
2594*8b26181fSAndroid Build Coastguard Worker /* String identifier to be used in the pcap_findalldevs_ex() */
2595*8b26181fSAndroid Build Coastguard Worker #define PCAP_TEXT_SOURCE_ADAPTER "Network adapter"
2596*8b26181fSAndroid Build Coastguard Worker #define PCAP_TEXT_SOURCE_ADAPTER_LEN (sizeof PCAP_TEXT_SOURCE_ADAPTER - 1)
2597*8b26181fSAndroid Build Coastguard Worker /* String identifier to be used in the pcap_findalldevs_ex() */
2598*8b26181fSAndroid Build Coastguard Worker #define PCAP_TEXT_SOURCE_ON_REMOTE_HOST "on remote node"
2599*8b26181fSAndroid Build Coastguard Worker #define PCAP_TEXT_SOURCE_ON_REMOTE_HOST_LEN (sizeof PCAP_TEXT_SOURCE_ON_REMOTE_HOST - 1)
2600*8b26181fSAndroid Build Coastguard Worker
2601*8b26181fSAndroid Build Coastguard Worker static void
freeaddr(struct pcap_addr * addr)2602*8b26181fSAndroid Build Coastguard Worker freeaddr(struct pcap_addr *addr)
2603*8b26181fSAndroid Build Coastguard Worker {
2604*8b26181fSAndroid Build Coastguard Worker free(addr->addr);
2605*8b26181fSAndroid Build Coastguard Worker free(addr->netmask);
2606*8b26181fSAndroid Build Coastguard Worker free(addr->broadaddr);
2607*8b26181fSAndroid Build Coastguard Worker free(addr->dstaddr);
2608*8b26181fSAndroid Build Coastguard Worker free(addr);
2609*8b26181fSAndroid Build Coastguard Worker }
2610*8b26181fSAndroid Build Coastguard Worker
2611*8b26181fSAndroid Build Coastguard Worker int
pcap_findalldevs_ex_remote(const char * source,struct pcap_rmtauth * auth,pcap_if_t ** alldevs,char * errbuf)2612*8b26181fSAndroid Build Coastguard Worker pcap_findalldevs_ex_remote(const char *source, struct pcap_rmtauth *auth, pcap_if_t **alldevs, char *errbuf)
2613*8b26181fSAndroid Build Coastguard Worker {
2614*8b26181fSAndroid Build Coastguard Worker uint8 protocol_version; /* protocol version */
2615*8b26181fSAndroid Build Coastguard Worker int byte_swapped; /* Server byte order is swapped from ours */
2616*8b26181fSAndroid Build Coastguard Worker SOCKET sockctrl; /* socket descriptor of the control connection */
2617*8b26181fSAndroid Build Coastguard Worker SSL *ssl = NULL; /* optional SSL handler for sockctrl */
2618*8b26181fSAndroid Build Coastguard Worker uint32 plen;
2619*8b26181fSAndroid Build Coastguard Worker struct rpcap_header header; /* structure that keeps the general header of the rpcap protocol */
2620*8b26181fSAndroid Build Coastguard Worker int i, j; /* temp variables */
2621*8b26181fSAndroid Build Coastguard Worker int nif; /* Number of interfaces listed */
2622*8b26181fSAndroid Build Coastguard Worker int active; /* 'true' if we the other end-party is in active mode */
2623*8b26181fSAndroid Build Coastguard Worker uint8 uses_ssl;
2624*8b26181fSAndroid Build Coastguard Worker char host[PCAP_BUF_SIZE], port[PCAP_BUF_SIZE];
2625*8b26181fSAndroid Build Coastguard Worker char tmpstring[PCAP_BUF_SIZE + 1]; /* Needed to convert names and descriptions from 'old' syntax to the 'new' one */
2626*8b26181fSAndroid Build Coastguard Worker pcap_if_t *lastdev; /* Last device in the pcap_if_t list */
2627*8b26181fSAndroid Build Coastguard Worker pcap_if_t *dev; /* Device we're adding to the pcap_if_t list */
2628*8b26181fSAndroid Build Coastguard Worker
2629*8b26181fSAndroid Build Coastguard Worker /* List starts out empty. */
2630*8b26181fSAndroid Build Coastguard Worker (*alldevs) = NULL;
2631*8b26181fSAndroid Build Coastguard Worker lastdev = NULL;
2632*8b26181fSAndroid Build Coastguard Worker
2633*8b26181fSAndroid Build Coastguard Worker /*
2634*8b26181fSAndroid Build Coastguard Worker * Attempt to set up the session with the server.
2635*8b26181fSAndroid Build Coastguard Worker */
2636*8b26181fSAndroid Build Coastguard Worker if (rpcap_setup_session(source, auth, &active, &sockctrl, &uses_ssl,
2637*8b26181fSAndroid Build Coastguard Worker &ssl, 0, &protocol_version, &byte_swapped, host, port, NULL,
2638*8b26181fSAndroid Build Coastguard Worker errbuf) == -1)
2639*8b26181fSAndroid Build Coastguard Worker {
2640*8b26181fSAndroid Build Coastguard Worker /* Session setup failed. */
2641*8b26181fSAndroid Build Coastguard Worker return -1;
2642*8b26181fSAndroid Build Coastguard Worker }
2643*8b26181fSAndroid Build Coastguard Worker
2644*8b26181fSAndroid Build Coastguard Worker /* RPCAP findalldevs command */
2645*8b26181fSAndroid Build Coastguard Worker rpcap_createhdr(&header, protocol_version, RPCAP_MSG_FINDALLIF_REQ,
2646*8b26181fSAndroid Build Coastguard Worker 0, 0);
2647*8b26181fSAndroid Build Coastguard Worker
2648*8b26181fSAndroid Build Coastguard Worker if (sock_send(sockctrl, ssl, (char *)&header, sizeof(struct rpcap_header),
2649*8b26181fSAndroid Build Coastguard Worker errbuf, PCAP_ERRBUF_SIZE) < 0)
2650*8b26181fSAndroid Build Coastguard Worker goto error_nodiscard;
2651*8b26181fSAndroid Build Coastguard Worker
2652*8b26181fSAndroid Build Coastguard Worker /* Receive and process the reply message header. */
2653*8b26181fSAndroid Build Coastguard Worker if (rpcap_process_msg_header(sockctrl, ssl, protocol_version,
2654*8b26181fSAndroid Build Coastguard Worker RPCAP_MSG_FINDALLIF_REQ, &header, errbuf) == -1)
2655*8b26181fSAndroid Build Coastguard Worker goto error_nodiscard;
2656*8b26181fSAndroid Build Coastguard Worker
2657*8b26181fSAndroid Build Coastguard Worker plen = header.plen;
2658*8b26181fSAndroid Build Coastguard Worker
2659*8b26181fSAndroid Build Coastguard Worker /* read the number of interfaces */
2660*8b26181fSAndroid Build Coastguard Worker nif = ntohs(header.value);
2661*8b26181fSAndroid Build Coastguard Worker
2662*8b26181fSAndroid Build Coastguard Worker /* loop until all interfaces have been received */
2663*8b26181fSAndroid Build Coastguard Worker for (i = 0; i < nif; i++)
2664*8b26181fSAndroid Build Coastguard Worker {
2665*8b26181fSAndroid Build Coastguard Worker struct rpcap_findalldevs_if findalldevs_if;
2666*8b26181fSAndroid Build Coastguard Worker char tmpstring2[PCAP_BUF_SIZE + 1]; /* Needed to convert names and descriptions from 'old' syntax to the 'new' one */
2667*8b26181fSAndroid Build Coastguard Worker struct pcap_addr *addr, *prevaddr;
2668*8b26181fSAndroid Build Coastguard Worker
2669*8b26181fSAndroid Build Coastguard Worker tmpstring2[PCAP_BUF_SIZE] = 0;
2670*8b26181fSAndroid Build Coastguard Worker
2671*8b26181fSAndroid Build Coastguard Worker /* receive the findalldevs structure from remote host */
2672*8b26181fSAndroid Build Coastguard Worker if (rpcap_recv(sockctrl, ssl, (char *)&findalldevs_if,
2673*8b26181fSAndroid Build Coastguard Worker sizeof(struct rpcap_findalldevs_if), &plen, errbuf) == -1)
2674*8b26181fSAndroid Build Coastguard Worker goto error;
2675*8b26181fSAndroid Build Coastguard Worker
2676*8b26181fSAndroid Build Coastguard Worker findalldevs_if.namelen = ntohs(findalldevs_if.namelen);
2677*8b26181fSAndroid Build Coastguard Worker findalldevs_if.desclen = ntohs(findalldevs_if.desclen);
2678*8b26181fSAndroid Build Coastguard Worker findalldevs_if.naddr = ntohs(findalldevs_if.naddr);
2679*8b26181fSAndroid Build Coastguard Worker
2680*8b26181fSAndroid Build Coastguard Worker /* allocate the main structure */
2681*8b26181fSAndroid Build Coastguard Worker dev = (pcap_if_t *)malloc(sizeof(pcap_if_t));
2682*8b26181fSAndroid Build Coastguard Worker if (dev == NULL)
2683*8b26181fSAndroid Build Coastguard Worker {
2684*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
2685*8b26181fSAndroid Build Coastguard Worker errno, "malloc() failed");
2686*8b26181fSAndroid Build Coastguard Worker goto error;
2687*8b26181fSAndroid Build Coastguard Worker }
2688*8b26181fSAndroid Build Coastguard Worker
2689*8b26181fSAndroid Build Coastguard Worker /* Initialize the structure to 'zero' */
2690*8b26181fSAndroid Build Coastguard Worker memset(dev, 0, sizeof(pcap_if_t));
2691*8b26181fSAndroid Build Coastguard Worker
2692*8b26181fSAndroid Build Coastguard Worker /* Append it to the list. */
2693*8b26181fSAndroid Build Coastguard Worker if (lastdev == NULL)
2694*8b26181fSAndroid Build Coastguard Worker {
2695*8b26181fSAndroid Build Coastguard Worker /*
2696*8b26181fSAndroid Build Coastguard Worker * List is empty, so it's also the first device.
2697*8b26181fSAndroid Build Coastguard Worker */
2698*8b26181fSAndroid Build Coastguard Worker *alldevs = dev;
2699*8b26181fSAndroid Build Coastguard Worker }
2700*8b26181fSAndroid Build Coastguard Worker else
2701*8b26181fSAndroid Build Coastguard Worker {
2702*8b26181fSAndroid Build Coastguard Worker /*
2703*8b26181fSAndroid Build Coastguard Worker * Append after the last device.
2704*8b26181fSAndroid Build Coastguard Worker */
2705*8b26181fSAndroid Build Coastguard Worker lastdev->next = dev;
2706*8b26181fSAndroid Build Coastguard Worker }
2707*8b26181fSAndroid Build Coastguard Worker /* It's now the last device. */
2708*8b26181fSAndroid Build Coastguard Worker lastdev = dev;
2709*8b26181fSAndroid Build Coastguard Worker
2710*8b26181fSAndroid Build Coastguard Worker /* allocate mem for name and description */
2711*8b26181fSAndroid Build Coastguard Worker if (findalldevs_if.namelen)
2712*8b26181fSAndroid Build Coastguard Worker {
2713*8b26181fSAndroid Build Coastguard Worker
2714*8b26181fSAndroid Build Coastguard Worker if (findalldevs_if.namelen >= sizeof(tmpstring))
2715*8b26181fSAndroid Build Coastguard Worker {
2716*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE, "Interface name too long");
2717*8b26181fSAndroid Build Coastguard Worker goto error;
2718*8b26181fSAndroid Build Coastguard Worker }
2719*8b26181fSAndroid Build Coastguard Worker
2720*8b26181fSAndroid Build Coastguard Worker /* Retrieve adapter name */
2721*8b26181fSAndroid Build Coastguard Worker if (rpcap_recv(sockctrl, ssl, tmpstring,
2722*8b26181fSAndroid Build Coastguard Worker findalldevs_if.namelen, &plen, errbuf) == -1)
2723*8b26181fSAndroid Build Coastguard Worker goto error;
2724*8b26181fSAndroid Build Coastguard Worker
2725*8b26181fSAndroid Build Coastguard Worker tmpstring[findalldevs_if.namelen] = 0;
2726*8b26181fSAndroid Build Coastguard Worker
2727*8b26181fSAndroid Build Coastguard Worker /* Create the new device identifier */
2728*8b26181fSAndroid Build Coastguard Worker if (pcap_createsrcstr_ex(tmpstring2, PCAP_SRC_IFREMOTE,
2729*8b26181fSAndroid Build Coastguard Worker host, port, tmpstring, uses_ssl, errbuf) == -1)
2730*8b26181fSAndroid Build Coastguard Worker goto error;
2731*8b26181fSAndroid Build Coastguard Worker
2732*8b26181fSAndroid Build Coastguard Worker dev->name = strdup(tmpstring2);
2733*8b26181fSAndroid Build Coastguard Worker if (dev->name == NULL)
2734*8b26181fSAndroid Build Coastguard Worker {
2735*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(errbuf,
2736*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE, errno, "malloc() failed");
2737*8b26181fSAndroid Build Coastguard Worker goto error;
2738*8b26181fSAndroid Build Coastguard Worker }
2739*8b26181fSAndroid Build Coastguard Worker }
2740*8b26181fSAndroid Build Coastguard Worker
2741*8b26181fSAndroid Build Coastguard Worker if (findalldevs_if.desclen)
2742*8b26181fSAndroid Build Coastguard Worker {
2743*8b26181fSAndroid Build Coastguard Worker if (findalldevs_if.desclen >= sizeof(tmpstring))
2744*8b26181fSAndroid Build Coastguard Worker {
2745*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE, "Interface description too long");
2746*8b26181fSAndroid Build Coastguard Worker goto error;
2747*8b26181fSAndroid Build Coastguard Worker }
2748*8b26181fSAndroid Build Coastguard Worker
2749*8b26181fSAndroid Build Coastguard Worker /* Retrieve adapter description */
2750*8b26181fSAndroid Build Coastguard Worker if (rpcap_recv(sockctrl, ssl, tmpstring,
2751*8b26181fSAndroid Build Coastguard Worker findalldevs_if.desclen, &plen, errbuf) == -1)
2752*8b26181fSAndroid Build Coastguard Worker goto error;
2753*8b26181fSAndroid Build Coastguard Worker
2754*8b26181fSAndroid Build Coastguard Worker tmpstring[findalldevs_if.desclen] = 0;
2755*8b26181fSAndroid Build Coastguard Worker
2756*8b26181fSAndroid Build Coastguard Worker if (pcap_asprintf(&dev->description,
2757*8b26181fSAndroid Build Coastguard Worker "%s '%s' %s %s", PCAP_TEXT_SOURCE_ADAPTER,
2758*8b26181fSAndroid Build Coastguard Worker tmpstring, PCAP_TEXT_SOURCE_ON_REMOTE_HOST, host) == -1)
2759*8b26181fSAndroid Build Coastguard Worker {
2760*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(errbuf,
2761*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE, errno, "malloc() failed");
2762*8b26181fSAndroid Build Coastguard Worker goto error;
2763*8b26181fSAndroid Build Coastguard Worker }
2764*8b26181fSAndroid Build Coastguard Worker }
2765*8b26181fSAndroid Build Coastguard Worker
2766*8b26181fSAndroid Build Coastguard Worker dev->flags = ntohl(findalldevs_if.flags);
2767*8b26181fSAndroid Build Coastguard Worker
2768*8b26181fSAndroid Build Coastguard Worker prevaddr = NULL;
2769*8b26181fSAndroid Build Coastguard Worker /* loop until all addresses have been received */
2770*8b26181fSAndroid Build Coastguard Worker for (j = 0; j < findalldevs_if.naddr; j++)
2771*8b26181fSAndroid Build Coastguard Worker {
2772*8b26181fSAndroid Build Coastguard Worker struct rpcap_findalldevs_ifaddr ifaddr;
2773*8b26181fSAndroid Build Coastguard Worker
2774*8b26181fSAndroid Build Coastguard Worker /* Retrieve the interface addresses */
2775*8b26181fSAndroid Build Coastguard Worker if (rpcap_recv(sockctrl, ssl, (char *)&ifaddr,
2776*8b26181fSAndroid Build Coastguard Worker sizeof(struct rpcap_findalldevs_ifaddr),
2777*8b26181fSAndroid Build Coastguard Worker &plen, errbuf) == -1)
2778*8b26181fSAndroid Build Coastguard Worker goto error;
2779*8b26181fSAndroid Build Coastguard Worker
2780*8b26181fSAndroid Build Coastguard Worker /*
2781*8b26181fSAndroid Build Coastguard Worker * Deserialize all the address components.
2782*8b26181fSAndroid Build Coastguard Worker */
2783*8b26181fSAndroid Build Coastguard Worker addr = (struct pcap_addr *) malloc(sizeof(struct pcap_addr));
2784*8b26181fSAndroid Build Coastguard Worker if (addr == NULL)
2785*8b26181fSAndroid Build Coastguard Worker {
2786*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(errbuf,
2787*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE, errno, "malloc() failed");
2788*8b26181fSAndroid Build Coastguard Worker goto error;
2789*8b26181fSAndroid Build Coastguard Worker }
2790*8b26181fSAndroid Build Coastguard Worker addr->next = NULL;
2791*8b26181fSAndroid Build Coastguard Worker addr->addr = NULL;
2792*8b26181fSAndroid Build Coastguard Worker addr->netmask = NULL;
2793*8b26181fSAndroid Build Coastguard Worker addr->broadaddr = NULL;
2794*8b26181fSAndroid Build Coastguard Worker addr->dstaddr = NULL;
2795*8b26181fSAndroid Build Coastguard Worker
2796*8b26181fSAndroid Build Coastguard Worker if (rpcap_deseraddr(&ifaddr.addr,
2797*8b26181fSAndroid Build Coastguard Worker (struct sockaddr_storage **) &addr->addr, errbuf) == -1)
2798*8b26181fSAndroid Build Coastguard Worker {
2799*8b26181fSAndroid Build Coastguard Worker freeaddr(addr);
2800*8b26181fSAndroid Build Coastguard Worker goto error;
2801*8b26181fSAndroid Build Coastguard Worker }
2802*8b26181fSAndroid Build Coastguard Worker if (rpcap_deseraddr(&ifaddr.netmask,
2803*8b26181fSAndroid Build Coastguard Worker (struct sockaddr_storage **) &addr->netmask, errbuf) == -1)
2804*8b26181fSAndroid Build Coastguard Worker {
2805*8b26181fSAndroid Build Coastguard Worker freeaddr(addr);
2806*8b26181fSAndroid Build Coastguard Worker goto error;
2807*8b26181fSAndroid Build Coastguard Worker }
2808*8b26181fSAndroid Build Coastguard Worker if (rpcap_deseraddr(&ifaddr.broadaddr,
2809*8b26181fSAndroid Build Coastguard Worker (struct sockaddr_storage **) &addr->broadaddr, errbuf) == -1)
2810*8b26181fSAndroid Build Coastguard Worker {
2811*8b26181fSAndroid Build Coastguard Worker freeaddr(addr);
2812*8b26181fSAndroid Build Coastguard Worker goto error;
2813*8b26181fSAndroid Build Coastguard Worker }
2814*8b26181fSAndroid Build Coastguard Worker if (rpcap_deseraddr(&ifaddr.dstaddr,
2815*8b26181fSAndroid Build Coastguard Worker (struct sockaddr_storage **) &addr->dstaddr, errbuf) == -1)
2816*8b26181fSAndroid Build Coastguard Worker {
2817*8b26181fSAndroid Build Coastguard Worker freeaddr(addr);
2818*8b26181fSAndroid Build Coastguard Worker goto error;
2819*8b26181fSAndroid Build Coastguard Worker }
2820*8b26181fSAndroid Build Coastguard Worker
2821*8b26181fSAndroid Build Coastguard Worker if ((addr->addr == NULL) && (addr->netmask == NULL) &&
2822*8b26181fSAndroid Build Coastguard Worker (addr->broadaddr == NULL) && (addr->dstaddr == NULL))
2823*8b26181fSAndroid Build Coastguard Worker {
2824*8b26181fSAndroid Build Coastguard Worker /*
2825*8b26181fSAndroid Build Coastguard Worker * None of the addresses are IPv4 or IPv6
2826*8b26181fSAndroid Build Coastguard Worker * addresses, so throw this entry away.
2827*8b26181fSAndroid Build Coastguard Worker */
2828*8b26181fSAndroid Build Coastguard Worker free(addr);
2829*8b26181fSAndroid Build Coastguard Worker }
2830*8b26181fSAndroid Build Coastguard Worker else
2831*8b26181fSAndroid Build Coastguard Worker {
2832*8b26181fSAndroid Build Coastguard Worker /*
2833*8b26181fSAndroid Build Coastguard Worker * Add this entry to the list.
2834*8b26181fSAndroid Build Coastguard Worker */
2835*8b26181fSAndroid Build Coastguard Worker if (prevaddr == NULL)
2836*8b26181fSAndroid Build Coastguard Worker {
2837*8b26181fSAndroid Build Coastguard Worker dev->addresses = addr;
2838*8b26181fSAndroid Build Coastguard Worker }
2839*8b26181fSAndroid Build Coastguard Worker else
2840*8b26181fSAndroid Build Coastguard Worker {
2841*8b26181fSAndroid Build Coastguard Worker prevaddr->next = addr;
2842*8b26181fSAndroid Build Coastguard Worker }
2843*8b26181fSAndroid Build Coastguard Worker prevaddr = addr;
2844*8b26181fSAndroid Build Coastguard Worker }
2845*8b26181fSAndroid Build Coastguard Worker }
2846*8b26181fSAndroid Build Coastguard Worker }
2847*8b26181fSAndroid Build Coastguard Worker
2848*8b26181fSAndroid Build Coastguard Worker /* Discard the rest of the message. */
2849*8b26181fSAndroid Build Coastguard Worker if (rpcap_discard(sockctrl, ssl, plen, errbuf) == 1)
2850*8b26181fSAndroid Build Coastguard Worker goto error_nodiscard;
2851*8b26181fSAndroid Build Coastguard Worker
2852*8b26181fSAndroid Build Coastguard Worker /* Control connection has to be closed only in case the remote machine is in passive mode */
2853*8b26181fSAndroid Build Coastguard Worker if (!active)
2854*8b26181fSAndroid Build Coastguard Worker {
2855*8b26181fSAndroid Build Coastguard Worker /* DO not send RPCAP_CLOSE, since we did not open a pcap_t; no need to free resources */
2856*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OPENSSL
2857*8b26181fSAndroid Build Coastguard Worker if (ssl)
2858*8b26181fSAndroid Build Coastguard Worker {
2859*8b26181fSAndroid Build Coastguard Worker // Finish using the SSL handle for the socket.
2860*8b26181fSAndroid Build Coastguard Worker // This must be done *before* the socket is closed.
2861*8b26181fSAndroid Build Coastguard Worker ssl_finish(ssl);
2862*8b26181fSAndroid Build Coastguard Worker }
2863*8b26181fSAndroid Build Coastguard Worker #endif
2864*8b26181fSAndroid Build Coastguard Worker if (sock_close(sockctrl, errbuf, PCAP_ERRBUF_SIZE))
2865*8b26181fSAndroid Build Coastguard Worker return -1;
2866*8b26181fSAndroid Build Coastguard Worker }
2867*8b26181fSAndroid Build Coastguard Worker
2868*8b26181fSAndroid Build Coastguard Worker /* To avoid inconsistencies in the number of sock_init() */
2869*8b26181fSAndroid Build Coastguard Worker sock_cleanup();
2870*8b26181fSAndroid Build Coastguard Worker
2871*8b26181fSAndroid Build Coastguard Worker return 0;
2872*8b26181fSAndroid Build Coastguard Worker
2873*8b26181fSAndroid Build Coastguard Worker error:
2874*8b26181fSAndroid Build Coastguard Worker /*
2875*8b26181fSAndroid Build Coastguard Worker * In case there has been an error, I don't want to overwrite it with a new one
2876*8b26181fSAndroid Build Coastguard Worker * if the following call fails. I want to return always the original error.
2877*8b26181fSAndroid Build Coastguard Worker *
2878*8b26181fSAndroid Build Coastguard Worker * Take care: this connection can already be closed when we try to close it.
2879*8b26181fSAndroid Build Coastguard Worker * This happens because a previous error in the rpcapd, which requested to
2880*8b26181fSAndroid Build Coastguard Worker * closed the connection. In that case, we already recognized that into the
2881*8b26181fSAndroid Build Coastguard Worker * rpspck_isheaderok() and we already acknowledged the closing.
2882*8b26181fSAndroid Build Coastguard Worker * In that sense, this call is useless here (however it is needed in case
2883*8b26181fSAndroid Build Coastguard Worker * the client generates the error).
2884*8b26181fSAndroid Build Coastguard Worker *
2885*8b26181fSAndroid Build Coastguard Worker * Checks if all the data has been read; if not, discard the data in excess
2886*8b26181fSAndroid Build Coastguard Worker */
2887*8b26181fSAndroid Build Coastguard Worker (void) rpcap_discard(sockctrl, ssl, plen, NULL);
2888*8b26181fSAndroid Build Coastguard Worker
2889*8b26181fSAndroid Build Coastguard Worker error_nodiscard:
2890*8b26181fSAndroid Build Coastguard Worker /* Control connection has to be closed only in case the remote machine is in passive mode */
2891*8b26181fSAndroid Build Coastguard Worker if (!active)
2892*8b26181fSAndroid Build Coastguard Worker {
2893*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OPENSSL
2894*8b26181fSAndroid Build Coastguard Worker if (ssl)
2895*8b26181fSAndroid Build Coastguard Worker {
2896*8b26181fSAndroid Build Coastguard Worker // Finish using the SSL handle for the socket.
2897*8b26181fSAndroid Build Coastguard Worker // This must be done *before* the socket is closed.
2898*8b26181fSAndroid Build Coastguard Worker ssl_finish(ssl);
2899*8b26181fSAndroid Build Coastguard Worker }
2900*8b26181fSAndroid Build Coastguard Worker #endif
2901*8b26181fSAndroid Build Coastguard Worker sock_close(sockctrl, NULL, 0);
2902*8b26181fSAndroid Build Coastguard Worker }
2903*8b26181fSAndroid Build Coastguard Worker
2904*8b26181fSAndroid Build Coastguard Worker /* To avoid inconsistencies in the number of sock_init() */
2905*8b26181fSAndroid Build Coastguard Worker sock_cleanup();
2906*8b26181fSAndroid Build Coastguard Worker
2907*8b26181fSAndroid Build Coastguard Worker /* Free whatever interfaces we've allocated. */
2908*8b26181fSAndroid Build Coastguard Worker pcap_freealldevs(*alldevs);
2909*8b26181fSAndroid Build Coastguard Worker
2910*8b26181fSAndroid Build Coastguard Worker return -1;
2911*8b26181fSAndroid Build Coastguard Worker }
2912*8b26181fSAndroid Build Coastguard Worker
2913*8b26181fSAndroid Build Coastguard Worker /*
2914*8b26181fSAndroid Build Coastguard Worker * Active mode routines.
2915*8b26181fSAndroid Build Coastguard Worker *
2916*8b26181fSAndroid Build Coastguard Worker * The old libpcap API is somewhat ugly, and makes active mode difficult
2917*8b26181fSAndroid Build Coastguard Worker * to implement; we provide some APIs for it that work only with rpcap.
2918*8b26181fSAndroid Build Coastguard Worker */
2919*8b26181fSAndroid Build Coastguard Worker
pcap_remoteact_accept_ex(const char * address,const char * port,const char * hostlist,char * connectinghost,struct pcap_rmtauth * auth,int uses_ssl,char * errbuf)2920*8b26181fSAndroid Build Coastguard Worker SOCKET pcap_remoteact_accept_ex(const char *address, const char *port, const char *hostlist, char *connectinghost, struct pcap_rmtauth *auth, int uses_ssl, char *errbuf)
2921*8b26181fSAndroid Build Coastguard Worker {
2922*8b26181fSAndroid Build Coastguard Worker /* socket-related variables */
2923*8b26181fSAndroid Build Coastguard Worker struct addrinfo hints; /* temporary struct to keep settings needed to open the new socket */
2924*8b26181fSAndroid Build Coastguard Worker struct addrinfo *addrinfo; /* keeps the addrinfo chain; required to open a new socket */
2925*8b26181fSAndroid Build Coastguard Worker struct sockaddr_storage from; /* generic sockaddr_storage variable */
2926*8b26181fSAndroid Build Coastguard Worker socklen_t fromlen; /* keeps the length of the sockaddr_storage variable */
2927*8b26181fSAndroid Build Coastguard Worker SOCKET sockctrl; /* keeps the main socket identifier */
2928*8b26181fSAndroid Build Coastguard Worker SSL *ssl = NULL; /* Optional SSL handler for sockctrl */
2929*8b26181fSAndroid Build Coastguard Worker uint8 protocol_version; /* negotiated protocol version */
2930*8b26181fSAndroid Build Coastguard Worker int byte_swapped; /* 1 if server byte order is known to be the reverse of ours */
2931*8b26181fSAndroid Build Coastguard Worker struct activehosts *temp, *prev; /* temp var needed to scan he host list chain */
2932*8b26181fSAndroid Build Coastguard Worker
2933*8b26181fSAndroid Build Coastguard Worker *connectinghost = 0; /* just in case */
2934*8b26181fSAndroid Build Coastguard Worker
2935*8b26181fSAndroid Build Coastguard Worker /* Prepare to open a new server socket */
2936*8b26181fSAndroid Build Coastguard Worker memset(&hints, 0, sizeof(struct addrinfo));
2937*8b26181fSAndroid Build Coastguard Worker /* WARNING Currently it supports only ONE socket family among ipv4 and IPv6 */
2938*8b26181fSAndroid Build Coastguard Worker hints.ai_family = AF_INET; /* PF_UNSPEC to have both IPv4 and IPv6 server */
2939*8b26181fSAndroid Build Coastguard Worker hints.ai_flags = AI_PASSIVE; /* Ready to a bind() socket */
2940*8b26181fSAndroid Build Coastguard Worker hints.ai_socktype = SOCK_STREAM;
2941*8b26181fSAndroid Build Coastguard Worker
2942*8b26181fSAndroid Build Coastguard Worker /* Warning: this call can be the first one called by the user. */
2943*8b26181fSAndroid Build Coastguard Worker /* For this reason, we have to initialize the Winsock support. */
2944*8b26181fSAndroid Build Coastguard Worker if (sock_init(errbuf, PCAP_ERRBUF_SIZE) == -1)
2945*8b26181fSAndroid Build Coastguard Worker return (SOCKET)-1;
2946*8b26181fSAndroid Build Coastguard Worker
2947*8b26181fSAndroid Build Coastguard Worker /* Do the work */
2948*8b26181fSAndroid Build Coastguard Worker if ((port == NULL) || (port[0] == 0))
2949*8b26181fSAndroid Build Coastguard Worker {
2950*8b26181fSAndroid Build Coastguard Worker if (sock_initaddress(address, RPCAP_DEFAULT_NETPORT_ACTIVE, &hints, &addrinfo, errbuf, PCAP_ERRBUF_SIZE) == -1)
2951*8b26181fSAndroid Build Coastguard Worker {
2952*8b26181fSAndroid Build Coastguard Worker return (SOCKET)-2;
2953*8b26181fSAndroid Build Coastguard Worker }
2954*8b26181fSAndroid Build Coastguard Worker }
2955*8b26181fSAndroid Build Coastguard Worker else
2956*8b26181fSAndroid Build Coastguard Worker {
2957*8b26181fSAndroid Build Coastguard Worker if (sock_initaddress(address, port, &hints, &addrinfo, errbuf, PCAP_ERRBUF_SIZE) == -1)
2958*8b26181fSAndroid Build Coastguard Worker {
2959*8b26181fSAndroid Build Coastguard Worker return (SOCKET)-2;
2960*8b26181fSAndroid Build Coastguard Worker }
2961*8b26181fSAndroid Build Coastguard Worker }
2962*8b26181fSAndroid Build Coastguard Worker
2963*8b26181fSAndroid Build Coastguard Worker
2964*8b26181fSAndroid Build Coastguard Worker if ((sockmain = sock_open(NULL, addrinfo, SOCKOPEN_SERVER, 1, errbuf, PCAP_ERRBUF_SIZE)) == INVALID_SOCKET)
2965*8b26181fSAndroid Build Coastguard Worker {
2966*8b26181fSAndroid Build Coastguard Worker freeaddrinfo(addrinfo);
2967*8b26181fSAndroid Build Coastguard Worker return (SOCKET)-2;
2968*8b26181fSAndroid Build Coastguard Worker }
2969*8b26181fSAndroid Build Coastguard Worker freeaddrinfo(addrinfo);
2970*8b26181fSAndroid Build Coastguard Worker
2971*8b26181fSAndroid Build Coastguard Worker /* Connection creation */
2972*8b26181fSAndroid Build Coastguard Worker fromlen = sizeof(struct sockaddr_storage);
2973*8b26181fSAndroid Build Coastguard Worker
2974*8b26181fSAndroid Build Coastguard Worker sockctrl = accept(sockmain, (struct sockaddr *) &from, &fromlen);
2975*8b26181fSAndroid Build Coastguard Worker
2976*8b26181fSAndroid Build Coastguard Worker /* We're not using sock_close, since we do not want to send a shutdown */
2977*8b26181fSAndroid Build Coastguard Worker /* (which is not allowed on a non-connected socket) */
2978*8b26181fSAndroid Build Coastguard Worker closesocket(sockmain);
2979*8b26181fSAndroid Build Coastguard Worker sockmain = 0;
2980*8b26181fSAndroid Build Coastguard Worker
2981*8b26181fSAndroid Build Coastguard Worker if (sockctrl == INVALID_SOCKET)
2982*8b26181fSAndroid Build Coastguard Worker {
2983*8b26181fSAndroid Build Coastguard Worker sock_geterrmsg(errbuf, PCAP_ERRBUF_SIZE, "accept() failed");
2984*8b26181fSAndroid Build Coastguard Worker return (SOCKET)-2;
2985*8b26181fSAndroid Build Coastguard Worker }
2986*8b26181fSAndroid Build Coastguard Worker
2987*8b26181fSAndroid Build Coastguard Worker /* Promote to SSL early before any error message may be sent */
2988*8b26181fSAndroid Build Coastguard Worker if (uses_ssl)
2989*8b26181fSAndroid Build Coastguard Worker {
2990*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OPENSSL
2991*8b26181fSAndroid Build Coastguard Worker ssl = ssl_promotion(0, sockctrl, errbuf, PCAP_ERRBUF_SIZE);
2992*8b26181fSAndroid Build Coastguard Worker if (! ssl)
2993*8b26181fSAndroid Build Coastguard Worker {
2994*8b26181fSAndroid Build Coastguard Worker sock_close(sockctrl, NULL, 0);
2995*8b26181fSAndroid Build Coastguard Worker return (SOCKET)-1;
2996*8b26181fSAndroid Build Coastguard Worker }
2997*8b26181fSAndroid Build Coastguard Worker #else
2998*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE, "No TLS support");
2999*8b26181fSAndroid Build Coastguard Worker sock_close(sockctrl, NULL, 0);
3000*8b26181fSAndroid Build Coastguard Worker return (SOCKET)-1;
3001*8b26181fSAndroid Build Coastguard Worker #endif
3002*8b26181fSAndroid Build Coastguard Worker }
3003*8b26181fSAndroid Build Coastguard Worker
3004*8b26181fSAndroid Build Coastguard Worker /* Get the numeric for of the name of the connecting host */
3005*8b26181fSAndroid Build Coastguard Worker if (getnameinfo((struct sockaddr *) &from, fromlen, connectinghost, RPCAP_HOSTLIST_SIZE, NULL, 0, NI_NUMERICHOST))
3006*8b26181fSAndroid Build Coastguard Worker {
3007*8b26181fSAndroid Build Coastguard Worker sock_geterrmsg(errbuf, PCAP_ERRBUF_SIZE,
3008*8b26181fSAndroid Build Coastguard Worker "getnameinfo() failed");
3009*8b26181fSAndroid Build Coastguard Worker rpcap_senderror(sockctrl, ssl, 0, PCAP_ERR_REMOTEACCEPT, errbuf, NULL);
3010*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OPENSSL
3011*8b26181fSAndroid Build Coastguard Worker if (ssl)
3012*8b26181fSAndroid Build Coastguard Worker {
3013*8b26181fSAndroid Build Coastguard Worker // Finish using the SSL handle for the socket.
3014*8b26181fSAndroid Build Coastguard Worker // This must be done *before* the socket is closed.
3015*8b26181fSAndroid Build Coastguard Worker ssl_finish(ssl);
3016*8b26181fSAndroid Build Coastguard Worker }
3017*8b26181fSAndroid Build Coastguard Worker #endif
3018*8b26181fSAndroid Build Coastguard Worker sock_close(sockctrl, NULL, 0);
3019*8b26181fSAndroid Build Coastguard Worker return (SOCKET)-1;
3020*8b26181fSAndroid Build Coastguard Worker }
3021*8b26181fSAndroid Build Coastguard Worker
3022*8b26181fSAndroid Build Coastguard Worker /* checks if the connecting host is among the ones allowed */
3023*8b26181fSAndroid Build Coastguard Worker if (sock_check_hostlist((char *)hostlist, RPCAP_HOSTLIST_SEP, &from, errbuf, PCAP_ERRBUF_SIZE) < 0)
3024*8b26181fSAndroid Build Coastguard Worker {
3025*8b26181fSAndroid Build Coastguard Worker rpcap_senderror(sockctrl, ssl, 0, PCAP_ERR_REMOTEACCEPT, errbuf, NULL);
3026*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OPENSSL
3027*8b26181fSAndroid Build Coastguard Worker if (ssl)
3028*8b26181fSAndroid Build Coastguard Worker {
3029*8b26181fSAndroid Build Coastguard Worker // Finish using the SSL handle for the socket.
3030*8b26181fSAndroid Build Coastguard Worker // This must be done *before* the socket is closed.
3031*8b26181fSAndroid Build Coastguard Worker ssl_finish(ssl);
3032*8b26181fSAndroid Build Coastguard Worker }
3033*8b26181fSAndroid Build Coastguard Worker #endif
3034*8b26181fSAndroid Build Coastguard Worker sock_close(sockctrl, NULL, 0);
3035*8b26181fSAndroid Build Coastguard Worker return (SOCKET)-1;
3036*8b26181fSAndroid Build Coastguard Worker }
3037*8b26181fSAndroid Build Coastguard Worker
3038*8b26181fSAndroid Build Coastguard Worker /*
3039*8b26181fSAndroid Build Coastguard Worker * Send authentication to the remote machine.
3040*8b26181fSAndroid Build Coastguard Worker */
3041*8b26181fSAndroid Build Coastguard Worker if (rpcap_doauth(sockctrl, ssl, &protocol_version, &byte_swapped,
3042*8b26181fSAndroid Build Coastguard Worker auth, errbuf) == -1)
3043*8b26181fSAndroid Build Coastguard Worker {
3044*8b26181fSAndroid Build Coastguard Worker /* Unrecoverable error. */
3045*8b26181fSAndroid Build Coastguard Worker rpcap_senderror(sockctrl, ssl, 0, PCAP_ERR_REMOTEACCEPT, errbuf, NULL);
3046*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OPENSSL
3047*8b26181fSAndroid Build Coastguard Worker if (ssl)
3048*8b26181fSAndroid Build Coastguard Worker {
3049*8b26181fSAndroid Build Coastguard Worker // Finish using the SSL handle for the socket.
3050*8b26181fSAndroid Build Coastguard Worker // This must be done *before* the socket is closed.
3051*8b26181fSAndroid Build Coastguard Worker ssl_finish(ssl);
3052*8b26181fSAndroid Build Coastguard Worker }
3053*8b26181fSAndroid Build Coastguard Worker #endif
3054*8b26181fSAndroid Build Coastguard Worker sock_close(sockctrl, NULL, 0);
3055*8b26181fSAndroid Build Coastguard Worker return (SOCKET)-3;
3056*8b26181fSAndroid Build Coastguard Worker }
3057*8b26181fSAndroid Build Coastguard Worker
3058*8b26181fSAndroid Build Coastguard Worker /* Checks that this host does not already have a cntrl connection in place */
3059*8b26181fSAndroid Build Coastguard Worker
3060*8b26181fSAndroid Build Coastguard Worker /* Initialize pointers */
3061*8b26181fSAndroid Build Coastguard Worker temp = activeHosts;
3062*8b26181fSAndroid Build Coastguard Worker prev = NULL;
3063*8b26181fSAndroid Build Coastguard Worker
3064*8b26181fSAndroid Build Coastguard Worker while (temp)
3065*8b26181fSAndroid Build Coastguard Worker {
3066*8b26181fSAndroid Build Coastguard Worker /* This host already has an active connection in place, so I don't have to update the host list */
3067*8b26181fSAndroid Build Coastguard Worker if (sock_cmpaddr(&temp->host, &from) == 0)
3068*8b26181fSAndroid Build Coastguard Worker return sockctrl;
3069*8b26181fSAndroid Build Coastguard Worker
3070*8b26181fSAndroid Build Coastguard Worker prev = temp;
3071*8b26181fSAndroid Build Coastguard Worker temp = temp->next;
3072*8b26181fSAndroid Build Coastguard Worker }
3073*8b26181fSAndroid Build Coastguard Worker
3074*8b26181fSAndroid Build Coastguard Worker /* The host does not exist in the list; so I have to update the list */
3075*8b26181fSAndroid Build Coastguard Worker if (prev)
3076*8b26181fSAndroid Build Coastguard Worker {
3077*8b26181fSAndroid Build Coastguard Worker prev->next = (struct activehosts *) malloc(sizeof(struct activehosts));
3078*8b26181fSAndroid Build Coastguard Worker temp = prev->next;
3079*8b26181fSAndroid Build Coastguard Worker }
3080*8b26181fSAndroid Build Coastguard Worker else
3081*8b26181fSAndroid Build Coastguard Worker {
3082*8b26181fSAndroid Build Coastguard Worker activeHosts = (struct activehosts *) malloc(sizeof(struct activehosts));
3083*8b26181fSAndroid Build Coastguard Worker temp = activeHosts;
3084*8b26181fSAndroid Build Coastguard Worker }
3085*8b26181fSAndroid Build Coastguard Worker
3086*8b26181fSAndroid Build Coastguard Worker if (temp == NULL)
3087*8b26181fSAndroid Build Coastguard Worker {
3088*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
3089*8b26181fSAndroid Build Coastguard Worker errno, "malloc() failed");
3090*8b26181fSAndroid Build Coastguard Worker rpcap_senderror(sockctrl, ssl, protocol_version, PCAP_ERR_REMOTEACCEPT, errbuf, NULL);
3091*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OPENSSL
3092*8b26181fSAndroid Build Coastguard Worker if (ssl)
3093*8b26181fSAndroid Build Coastguard Worker {
3094*8b26181fSAndroid Build Coastguard Worker // Finish using the SSL handle for the socket.
3095*8b26181fSAndroid Build Coastguard Worker // This must be done *before* the socket is closed.
3096*8b26181fSAndroid Build Coastguard Worker ssl_finish(ssl);
3097*8b26181fSAndroid Build Coastguard Worker }
3098*8b26181fSAndroid Build Coastguard Worker #endif
3099*8b26181fSAndroid Build Coastguard Worker sock_close(sockctrl, NULL, 0);
3100*8b26181fSAndroid Build Coastguard Worker return (SOCKET)-1;
3101*8b26181fSAndroid Build Coastguard Worker }
3102*8b26181fSAndroid Build Coastguard Worker
3103*8b26181fSAndroid Build Coastguard Worker memcpy(&temp->host, &from, fromlen);
3104*8b26181fSAndroid Build Coastguard Worker temp->sockctrl = sockctrl;
3105*8b26181fSAndroid Build Coastguard Worker temp->ssl = ssl;
3106*8b26181fSAndroid Build Coastguard Worker temp->protocol_version = protocol_version;
3107*8b26181fSAndroid Build Coastguard Worker temp->byte_swapped = byte_swapped;
3108*8b26181fSAndroid Build Coastguard Worker temp->next = NULL;
3109*8b26181fSAndroid Build Coastguard Worker
3110*8b26181fSAndroid Build Coastguard Worker return sockctrl;
3111*8b26181fSAndroid Build Coastguard Worker }
3112*8b26181fSAndroid Build Coastguard Worker
pcap_remoteact_accept(const char * address,const char * port,const char * hostlist,char * connectinghost,struct pcap_rmtauth * auth,char * errbuf)3113*8b26181fSAndroid Build Coastguard Worker SOCKET pcap_remoteact_accept(const char *address, const char *port, const char *hostlist, char *connectinghost, struct pcap_rmtauth *auth, char *errbuf)
3114*8b26181fSAndroid Build Coastguard Worker {
3115*8b26181fSAndroid Build Coastguard Worker return pcap_remoteact_accept_ex(address, port, hostlist, connectinghost, auth, 0, errbuf);
3116*8b26181fSAndroid Build Coastguard Worker }
3117*8b26181fSAndroid Build Coastguard Worker
pcap_remoteact_close(const char * host,char * errbuf)3118*8b26181fSAndroid Build Coastguard Worker int pcap_remoteact_close(const char *host, char *errbuf)
3119*8b26181fSAndroid Build Coastguard Worker {
3120*8b26181fSAndroid Build Coastguard Worker struct activehosts *temp, *prev; /* temp var needed to scan the host list chain */
3121*8b26181fSAndroid Build Coastguard Worker struct addrinfo hints, *addrinfo, *ai_next; /* temp var needed to translate between hostname to its address */
3122*8b26181fSAndroid Build Coastguard Worker int retval;
3123*8b26181fSAndroid Build Coastguard Worker
3124*8b26181fSAndroid Build Coastguard Worker temp = activeHosts;
3125*8b26181fSAndroid Build Coastguard Worker prev = NULL;
3126*8b26181fSAndroid Build Coastguard Worker
3127*8b26181fSAndroid Build Coastguard Worker /* retrieve the network address corresponding to 'host' */
3128*8b26181fSAndroid Build Coastguard Worker addrinfo = NULL;
3129*8b26181fSAndroid Build Coastguard Worker memset(&hints, 0, sizeof(struct addrinfo));
3130*8b26181fSAndroid Build Coastguard Worker hints.ai_family = PF_UNSPEC;
3131*8b26181fSAndroid Build Coastguard Worker hints.ai_socktype = SOCK_STREAM;
3132*8b26181fSAndroid Build Coastguard Worker
3133*8b26181fSAndroid Build Coastguard Worker retval = sock_initaddress(host, NULL, &hints, &addrinfo, errbuf,
3134*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE);
3135*8b26181fSAndroid Build Coastguard Worker if (retval != 0)
3136*8b26181fSAndroid Build Coastguard Worker {
3137*8b26181fSAndroid Build Coastguard Worker return -1;
3138*8b26181fSAndroid Build Coastguard Worker }
3139*8b26181fSAndroid Build Coastguard Worker
3140*8b26181fSAndroid Build Coastguard Worker while (temp)
3141*8b26181fSAndroid Build Coastguard Worker {
3142*8b26181fSAndroid Build Coastguard Worker ai_next = addrinfo;
3143*8b26181fSAndroid Build Coastguard Worker while (ai_next)
3144*8b26181fSAndroid Build Coastguard Worker {
3145*8b26181fSAndroid Build Coastguard Worker if (sock_cmpaddr(&temp->host, (struct sockaddr_storage *) ai_next->ai_addr) == 0)
3146*8b26181fSAndroid Build Coastguard Worker {
3147*8b26181fSAndroid Build Coastguard Worker struct rpcap_header header;
3148*8b26181fSAndroid Build Coastguard Worker int status = 0;
3149*8b26181fSAndroid Build Coastguard Worker
3150*8b26181fSAndroid Build Coastguard Worker /* Close this connection */
3151*8b26181fSAndroid Build Coastguard Worker rpcap_createhdr(&header, temp->protocol_version,
3152*8b26181fSAndroid Build Coastguard Worker RPCAP_MSG_CLOSE, 0, 0);
3153*8b26181fSAndroid Build Coastguard Worker
3154*8b26181fSAndroid Build Coastguard Worker /*
3155*8b26181fSAndroid Build Coastguard Worker * Don't check for errors, since we're
3156*8b26181fSAndroid Build Coastguard Worker * just cleaning up.
3157*8b26181fSAndroid Build Coastguard Worker */
3158*8b26181fSAndroid Build Coastguard Worker if (sock_send(temp->sockctrl, temp->ssl,
3159*8b26181fSAndroid Build Coastguard Worker (char *)&header,
3160*8b26181fSAndroid Build Coastguard Worker sizeof(struct rpcap_header), errbuf,
3161*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE) < 0)
3162*8b26181fSAndroid Build Coastguard Worker {
3163*8b26181fSAndroid Build Coastguard Worker /*
3164*8b26181fSAndroid Build Coastguard Worker * Let that error be the one we
3165*8b26181fSAndroid Build Coastguard Worker * report.
3166*8b26181fSAndroid Build Coastguard Worker */
3167*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OPENSSL
3168*8b26181fSAndroid Build Coastguard Worker if (temp->ssl)
3169*8b26181fSAndroid Build Coastguard Worker {
3170*8b26181fSAndroid Build Coastguard Worker // Finish using the SSL handle
3171*8b26181fSAndroid Build Coastguard Worker // for the socket.
3172*8b26181fSAndroid Build Coastguard Worker // This must be done *before*
3173*8b26181fSAndroid Build Coastguard Worker // the socket is closed.
3174*8b26181fSAndroid Build Coastguard Worker ssl_finish(temp->ssl);
3175*8b26181fSAndroid Build Coastguard Worker }
3176*8b26181fSAndroid Build Coastguard Worker #endif
3177*8b26181fSAndroid Build Coastguard Worker (void)sock_close(temp->sockctrl, NULL,
3178*8b26181fSAndroid Build Coastguard Worker 0);
3179*8b26181fSAndroid Build Coastguard Worker status = -1;
3180*8b26181fSAndroid Build Coastguard Worker }
3181*8b26181fSAndroid Build Coastguard Worker else
3182*8b26181fSAndroid Build Coastguard Worker {
3183*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OPENSSL
3184*8b26181fSAndroid Build Coastguard Worker if (temp->ssl)
3185*8b26181fSAndroid Build Coastguard Worker {
3186*8b26181fSAndroid Build Coastguard Worker // Finish using the SSL handle
3187*8b26181fSAndroid Build Coastguard Worker // for the socket.
3188*8b26181fSAndroid Build Coastguard Worker // This must be done *before*
3189*8b26181fSAndroid Build Coastguard Worker // the socket is closed.
3190*8b26181fSAndroid Build Coastguard Worker ssl_finish(temp->ssl);
3191*8b26181fSAndroid Build Coastguard Worker }
3192*8b26181fSAndroid Build Coastguard Worker #endif
3193*8b26181fSAndroid Build Coastguard Worker if (sock_close(temp->sockctrl, errbuf,
3194*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE) == -1)
3195*8b26181fSAndroid Build Coastguard Worker status = -1;
3196*8b26181fSAndroid Build Coastguard Worker }
3197*8b26181fSAndroid Build Coastguard Worker
3198*8b26181fSAndroid Build Coastguard Worker /*
3199*8b26181fSAndroid Build Coastguard Worker * Remove the host from the list of active
3200*8b26181fSAndroid Build Coastguard Worker * hosts.
3201*8b26181fSAndroid Build Coastguard Worker */
3202*8b26181fSAndroid Build Coastguard Worker if (prev)
3203*8b26181fSAndroid Build Coastguard Worker prev->next = temp->next;
3204*8b26181fSAndroid Build Coastguard Worker else
3205*8b26181fSAndroid Build Coastguard Worker activeHosts = temp->next;
3206*8b26181fSAndroid Build Coastguard Worker
3207*8b26181fSAndroid Build Coastguard Worker freeaddrinfo(addrinfo);
3208*8b26181fSAndroid Build Coastguard Worker
3209*8b26181fSAndroid Build Coastguard Worker free(temp);
3210*8b26181fSAndroid Build Coastguard Worker
3211*8b26181fSAndroid Build Coastguard Worker /* To avoid inconsistencies in the number of sock_init() */
3212*8b26181fSAndroid Build Coastguard Worker sock_cleanup();
3213*8b26181fSAndroid Build Coastguard Worker
3214*8b26181fSAndroid Build Coastguard Worker return status;
3215*8b26181fSAndroid Build Coastguard Worker }
3216*8b26181fSAndroid Build Coastguard Worker
3217*8b26181fSAndroid Build Coastguard Worker ai_next = ai_next->ai_next;
3218*8b26181fSAndroid Build Coastguard Worker }
3219*8b26181fSAndroid Build Coastguard Worker prev = temp;
3220*8b26181fSAndroid Build Coastguard Worker temp = temp->next;
3221*8b26181fSAndroid Build Coastguard Worker }
3222*8b26181fSAndroid Build Coastguard Worker
3223*8b26181fSAndroid Build Coastguard Worker if (addrinfo)
3224*8b26181fSAndroid Build Coastguard Worker freeaddrinfo(addrinfo);
3225*8b26181fSAndroid Build Coastguard Worker
3226*8b26181fSAndroid Build Coastguard Worker /* To avoid inconsistencies in the number of sock_init() */
3227*8b26181fSAndroid Build Coastguard Worker sock_cleanup();
3228*8b26181fSAndroid Build Coastguard Worker
3229*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE, "The host you want to close the active connection is not known");
3230*8b26181fSAndroid Build Coastguard Worker return -1;
3231*8b26181fSAndroid Build Coastguard Worker }
3232*8b26181fSAndroid Build Coastguard Worker
pcap_remoteact_cleanup(void)3233*8b26181fSAndroid Build Coastguard Worker void pcap_remoteact_cleanup(void)
3234*8b26181fSAndroid Build Coastguard Worker {
3235*8b26181fSAndroid Build Coastguard Worker # ifdef HAVE_OPENSSL
3236*8b26181fSAndroid Build Coastguard Worker if (ssl_main)
3237*8b26181fSAndroid Build Coastguard Worker {
3238*8b26181fSAndroid Build Coastguard Worker // Finish using the SSL handle for the main active socket.
3239*8b26181fSAndroid Build Coastguard Worker // This must be done *before* the socket is closed.
3240*8b26181fSAndroid Build Coastguard Worker ssl_finish(ssl_main);
3241*8b26181fSAndroid Build Coastguard Worker ssl_main = NULL;
3242*8b26181fSAndroid Build Coastguard Worker }
3243*8b26181fSAndroid Build Coastguard Worker # endif
3244*8b26181fSAndroid Build Coastguard Worker
3245*8b26181fSAndroid Build Coastguard Worker /* Very dirty, but it works */
3246*8b26181fSAndroid Build Coastguard Worker if (sockmain)
3247*8b26181fSAndroid Build Coastguard Worker {
3248*8b26181fSAndroid Build Coastguard Worker closesocket(sockmain);
3249*8b26181fSAndroid Build Coastguard Worker
3250*8b26181fSAndroid Build Coastguard Worker /* To avoid inconsistencies in the number of sock_init() */
3251*8b26181fSAndroid Build Coastguard Worker sock_cleanup();
3252*8b26181fSAndroid Build Coastguard Worker }
3253*8b26181fSAndroid Build Coastguard Worker }
3254*8b26181fSAndroid Build Coastguard Worker
pcap_remoteact_list(char * hostlist,char sep,int size,char * errbuf)3255*8b26181fSAndroid Build Coastguard Worker int pcap_remoteact_list(char *hostlist, char sep, int size, char *errbuf)
3256*8b26181fSAndroid Build Coastguard Worker {
3257*8b26181fSAndroid Build Coastguard Worker struct activehosts *temp; /* temp var needed to scan the host list chain */
3258*8b26181fSAndroid Build Coastguard Worker size_t len;
3259*8b26181fSAndroid Build Coastguard Worker char hoststr[RPCAP_HOSTLIST_SIZE + 1];
3260*8b26181fSAndroid Build Coastguard Worker
3261*8b26181fSAndroid Build Coastguard Worker temp = activeHosts;
3262*8b26181fSAndroid Build Coastguard Worker
3263*8b26181fSAndroid Build Coastguard Worker len = 0;
3264*8b26181fSAndroid Build Coastguard Worker *hostlist = 0;
3265*8b26181fSAndroid Build Coastguard Worker
3266*8b26181fSAndroid Build Coastguard Worker while (temp)
3267*8b26181fSAndroid Build Coastguard Worker {
3268*8b26181fSAndroid Build Coastguard Worker /*int sock_getascii_addrport(const struct sockaddr_storage *sockaddr, char *address, int addrlen, char *port, int portlen, int flags, char *errbuf, int errbuflen) */
3269*8b26181fSAndroid Build Coastguard Worker
3270*8b26181fSAndroid Build Coastguard Worker /* Get the numeric form of the name of the connecting host */
3271*8b26181fSAndroid Build Coastguard Worker if (sock_getascii_addrport((struct sockaddr_storage *) &temp->host, hoststr,
3272*8b26181fSAndroid Build Coastguard Worker RPCAP_HOSTLIST_SIZE, NULL, 0, NI_NUMERICHOST, errbuf, PCAP_ERRBUF_SIZE) != -1)
3273*8b26181fSAndroid Build Coastguard Worker /* if (getnameinfo( (struct sockaddr *) &temp->host, sizeof (struct sockaddr_storage), hoststr, */
3274*8b26181fSAndroid Build Coastguard Worker /* RPCAP_HOSTLIST_SIZE, NULL, 0, NI_NUMERICHOST) ) */
3275*8b26181fSAndroid Build Coastguard Worker {
3276*8b26181fSAndroid Build Coastguard Worker /* sock_geterrmsg(errbuf, PCAP_ERRBUF_SIZE, */
3277*8b26181fSAndroid Build Coastguard Worker /* "getnameinfo() failed"); */
3278*8b26181fSAndroid Build Coastguard Worker return -1;
3279*8b26181fSAndroid Build Coastguard Worker }
3280*8b26181fSAndroid Build Coastguard Worker
3281*8b26181fSAndroid Build Coastguard Worker len = len + strlen(hoststr) + 1 /* the separator */;
3282*8b26181fSAndroid Build Coastguard Worker
3283*8b26181fSAndroid Build Coastguard Worker if ((size < 0) || (len >= (size_t)size))
3284*8b26181fSAndroid Build Coastguard Worker {
3285*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE, "The string you provided is not able to keep "
3286*8b26181fSAndroid Build Coastguard Worker "the hostnames for all the active connections");
3287*8b26181fSAndroid Build Coastguard Worker return -1;
3288*8b26181fSAndroid Build Coastguard Worker }
3289*8b26181fSAndroid Build Coastguard Worker
3290*8b26181fSAndroid Build Coastguard Worker pcap_strlcat(hostlist, hoststr, PCAP_ERRBUF_SIZE);
3291*8b26181fSAndroid Build Coastguard Worker hostlist[len - 1] = sep;
3292*8b26181fSAndroid Build Coastguard Worker hostlist[len] = 0;
3293*8b26181fSAndroid Build Coastguard Worker
3294*8b26181fSAndroid Build Coastguard Worker temp = temp->next;
3295*8b26181fSAndroid Build Coastguard Worker }
3296*8b26181fSAndroid Build Coastguard Worker
3297*8b26181fSAndroid Build Coastguard Worker return 0;
3298*8b26181fSAndroid Build Coastguard Worker }
3299*8b26181fSAndroid Build Coastguard Worker
3300*8b26181fSAndroid Build Coastguard Worker /*
3301*8b26181fSAndroid Build Coastguard Worker * Receive the header of a message.
3302*8b26181fSAndroid Build Coastguard Worker */
rpcap_recv_msg_header(SOCKET sock,SSL * ssl,struct rpcap_header * header,char * errbuf)3303*8b26181fSAndroid Build Coastguard Worker static int rpcap_recv_msg_header(SOCKET sock, SSL *ssl, struct rpcap_header *header, char *errbuf)
3304*8b26181fSAndroid Build Coastguard Worker {
3305*8b26181fSAndroid Build Coastguard Worker int nrecv;
3306*8b26181fSAndroid Build Coastguard Worker
3307*8b26181fSAndroid Build Coastguard Worker nrecv = sock_recv(sock, ssl, (char *) header, sizeof(struct rpcap_header),
3308*8b26181fSAndroid Build Coastguard Worker SOCK_RECEIVEALL_YES|SOCK_EOF_IS_ERROR, errbuf,
3309*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE);
3310*8b26181fSAndroid Build Coastguard Worker if (nrecv == -1)
3311*8b26181fSAndroid Build Coastguard Worker {
3312*8b26181fSAndroid Build Coastguard Worker /* Network error. */
3313*8b26181fSAndroid Build Coastguard Worker return -1;
3314*8b26181fSAndroid Build Coastguard Worker }
3315*8b26181fSAndroid Build Coastguard Worker header->plen = ntohl(header->plen);
3316*8b26181fSAndroid Build Coastguard Worker return 0;
3317*8b26181fSAndroid Build Coastguard Worker }
3318*8b26181fSAndroid Build Coastguard Worker
3319*8b26181fSAndroid Build Coastguard Worker /*
3320*8b26181fSAndroid Build Coastguard Worker * Make sure the protocol version of a received message is what we were
3321*8b26181fSAndroid Build Coastguard Worker * expecting.
3322*8b26181fSAndroid Build Coastguard Worker */
rpcap_check_msg_ver(SOCKET sock,SSL * ssl,uint8 expected_ver,struct rpcap_header * header,char * errbuf)3323*8b26181fSAndroid Build Coastguard Worker static int rpcap_check_msg_ver(SOCKET sock, SSL *ssl, uint8 expected_ver, struct rpcap_header *header, char *errbuf)
3324*8b26181fSAndroid Build Coastguard Worker {
3325*8b26181fSAndroid Build Coastguard Worker /*
3326*8b26181fSAndroid Build Coastguard Worker * Did the server specify the version we negotiated?
3327*8b26181fSAndroid Build Coastguard Worker */
3328*8b26181fSAndroid Build Coastguard Worker if (header->ver != expected_ver)
3329*8b26181fSAndroid Build Coastguard Worker {
3330*8b26181fSAndroid Build Coastguard Worker /*
3331*8b26181fSAndroid Build Coastguard Worker * Discard the rest of the message.
3332*8b26181fSAndroid Build Coastguard Worker */
3333*8b26181fSAndroid Build Coastguard Worker if (rpcap_discard(sock, ssl, header->plen, errbuf) == -1)
3334*8b26181fSAndroid Build Coastguard Worker return -1;
3335*8b26181fSAndroid Build Coastguard Worker
3336*8b26181fSAndroid Build Coastguard Worker /*
3337*8b26181fSAndroid Build Coastguard Worker * Tell our caller that it's not the negotiated version.
3338*8b26181fSAndroid Build Coastguard Worker */
3339*8b26181fSAndroid Build Coastguard Worker if (errbuf != NULL)
3340*8b26181fSAndroid Build Coastguard Worker {
3341*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE,
3342*8b26181fSAndroid Build Coastguard Worker "Server sent us a message with version %u when we were expecting %u",
3343*8b26181fSAndroid Build Coastguard Worker header->ver, expected_ver);
3344*8b26181fSAndroid Build Coastguard Worker }
3345*8b26181fSAndroid Build Coastguard Worker return -1;
3346*8b26181fSAndroid Build Coastguard Worker }
3347*8b26181fSAndroid Build Coastguard Worker return 0;
3348*8b26181fSAndroid Build Coastguard Worker }
3349*8b26181fSAndroid Build Coastguard Worker
3350*8b26181fSAndroid Build Coastguard Worker /*
3351*8b26181fSAndroid Build Coastguard Worker * Check the message type of a received message, which should either be
3352*8b26181fSAndroid Build Coastguard Worker * the expected message type or RPCAP_MSG_ERROR.
3353*8b26181fSAndroid Build Coastguard Worker */
rpcap_check_msg_type(SOCKET sock,SSL * ssl,uint8 request_type,struct rpcap_header * header,uint16 * errcode,char * errbuf)3354*8b26181fSAndroid Build Coastguard Worker static int rpcap_check_msg_type(SOCKET sock, SSL *ssl, uint8 request_type, struct rpcap_header *header, uint16 *errcode, char *errbuf)
3355*8b26181fSAndroid Build Coastguard Worker {
3356*8b26181fSAndroid Build Coastguard Worker const char *request_type_string;
3357*8b26181fSAndroid Build Coastguard Worker const char *msg_type_string;
3358*8b26181fSAndroid Build Coastguard Worker
3359*8b26181fSAndroid Build Coastguard Worker /*
3360*8b26181fSAndroid Build Coastguard Worker * What type of message is it?
3361*8b26181fSAndroid Build Coastguard Worker */
3362*8b26181fSAndroid Build Coastguard Worker if (header->type == RPCAP_MSG_ERROR)
3363*8b26181fSAndroid Build Coastguard Worker {
3364*8b26181fSAndroid Build Coastguard Worker /*
3365*8b26181fSAndroid Build Coastguard Worker * The server reported an error.
3366*8b26181fSAndroid Build Coastguard Worker * Hand that error back to our caller.
3367*8b26181fSAndroid Build Coastguard Worker */
3368*8b26181fSAndroid Build Coastguard Worker *errcode = ntohs(header->value);
3369*8b26181fSAndroid Build Coastguard Worker rpcap_msg_err(sock, ssl, header->plen, errbuf);
3370*8b26181fSAndroid Build Coastguard Worker return -1;
3371*8b26181fSAndroid Build Coastguard Worker }
3372*8b26181fSAndroid Build Coastguard Worker
3373*8b26181fSAndroid Build Coastguard Worker *errcode = 0;
3374*8b26181fSAndroid Build Coastguard Worker
3375*8b26181fSAndroid Build Coastguard Worker /*
3376*8b26181fSAndroid Build Coastguard Worker * For a given request type value, the expected reply type value
3377*8b26181fSAndroid Build Coastguard Worker * is the request type value with ORed with RPCAP_MSG_IS_REPLY.
3378*8b26181fSAndroid Build Coastguard Worker */
3379*8b26181fSAndroid Build Coastguard Worker if (header->type != (request_type | RPCAP_MSG_IS_REPLY))
3380*8b26181fSAndroid Build Coastguard Worker {
3381*8b26181fSAndroid Build Coastguard Worker /*
3382*8b26181fSAndroid Build Coastguard Worker * This isn't a reply to the request we sent.
3383*8b26181fSAndroid Build Coastguard Worker */
3384*8b26181fSAndroid Build Coastguard Worker
3385*8b26181fSAndroid Build Coastguard Worker /*
3386*8b26181fSAndroid Build Coastguard Worker * Discard the rest of the message.
3387*8b26181fSAndroid Build Coastguard Worker */
3388*8b26181fSAndroid Build Coastguard Worker if (rpcap_discard(sock, ssl, header->plen, errbuf) == -1)
3389*8b26181fSAndroid Build Coastguard Worker return -1;
3390*8b26181fSAndroid Build Coastguard Worker
3391*8b26181fSAndroid Build Coastguard Worker /*
3392*8b26181fSAndroid Build Coastguard Worker * Tell our caller about it.
3393*8b26181fSAndroid Build Coastguard Worker */
3394*8b26181fSAndroid Build Coastguard Worker request_type_string = rpcap_msg_type_string(request_type);
3395*8b26181fSAndroid Build Coastguard Worker msg_type_string = rpcap_msg_type_string(header->type);
3396*8b26181fSAndroid Build Coastguard Worker if (errbuf != NULL)
3397*8b26181fSAndroid Build Coastguard Worker {
3398*8b26181fSAndroid Build Coastguard Worker if (request_type_string == NULL)
3399*8b26181fSAndroid Build Coastguard Worker {
3400*8b26181fSAndroid Build Coastguard Worker /* This should not happen. */
3401*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE,
3402*8b26181fSAndroid Build Coastguard Worker "rpcap_check_msg_type called for request message with type %u",
3403*8b26181fSAndroid Build Coastguard Worker request_type);
3404*8b26181fSAndroid Build Coastguard Worker return -1;
3405*8b26181fSAndroid Build Coastguard Worker }
3406*8b26181fSAndroid Build Coastguard Worker if (msg_type_string != NULL)
3407*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE,
3408*8b26181fSAndroid Build Coastguard Worker "%s message received in response to a %s message",
3409*8b26181fSAndroid Build Coastguard Worker msg_type_string, request_type_string);
3410*8b26181fSAndroid Build Coastguard Worker else
3411*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE,
3412*8b26181fSAndroid Build Coastguard Worker "Message of unknown type %u message received in response to a %s request",
3413*8b26181fSAndroid Build Coastguard Worker header->type, request_type_string);
3414*8b26181fSAndroid Build Coastguard Worker }
3415*8b26181fSAndroid Build Coastguard Worker return -1;
3416*8b26181fSAndroid Build Coastguard Worker }
3417*8b26181fSAndroid Build Coastguard Worker
3418*8b26181fSAndroid Build Coastguard Worker return 0;
3419*8b26181fSAndroid Build Coastguard Worker }
3420*8b26181fSAndroid Build Coastguard Worker
3421*8b26181fSAndroid Build Coastguard Worker /*
3422*8b26181fSAndroid Build Coastguard Worker * Receive and process the header of a message.
3423*8b26181fSAndroid Build Coastguard Worker */
rpcap_process_msg_header(SOCKET sock,SSL * ssl,uint8 expected_ver,uint8 request_type,struct rpcap_header * header,char * errbuf)3424*8b26181fSAndroid Build Coastguard Worker static int rpcap_process_msg_header(SOCKET sock, SSL *ssl, uint8 expected_ver, uint8 request_type, struct rpcap_header *header, char *errbuf)
3425*8b26181fSAndroid Build Coastguard Worker {
3426*8b26181fSAndroid Build Coastguard Worker uint16 errcode;
3427*8b26181fSAndroid Build Coastguard Worker
3428*8b26181fSAndroid Build Coastguard Worker if (rpcap_recv_msg_header(sock, ssl, header, errbuf) == -1)
3429*8b26181fSAndroid Build Coastguard Worker {
3430*8b26181fSAndroid Build Coastguard Worker /* Network error. */
3431*8b26181fSAndroid Build Coastguard Worker return -1;
3432*8b26181fSAndroid Build Coastguard Worker }
3433*8b26181fSAndroid Build Coastguard Worker
3434*8b26181fSAndroid Build Coastguard Worker /*
3435*8b26181fSAndroid Build Coastguard Worker * Did the server specify the version we negotiated?
3436*8b26181fSAndroid Build Coastguard Worker */
3437*8b26181fSAndroid Build Coastguard Worker if (rpcap_check_msg_ver(sock, ssl, expected_ver, header, errbuf) == -1)
3438*8b26181fSAndroid Build Coastguard Worker return -1;
3439*8b26181fSAndroid Build Coastguard Worker
3440*8b26181fSAndroid Build Coastguard Worker /*
3441*8b26181fSAndroid Build Coastguard Worker * Check the message type.
3442*8b26181fSAndroid Build Coastguard Worker */
3443*8b26181fSAndroid Build Coastguard Worker return rpcap_check_msg_type(sock, ssl, request_type, header,
3444*8b26181fSAndroid Build Coastguard Worker &errcode, errbuf);
3445*8b26181fSAndroid Build Coastguard Worker }
3446*8b26181fSAndroid Build Coastguard Worker
3447*8b26181fSAndroid Build Coastguard Worker /*
3448*8b26181fSAndroid Build Coastguard Worker * Read data from a message.
3449*8b26181fSAndroid Build Coastguard Worker * If we're trying to read more data that remains, puts an error
3450*8b26181fSAndroid Build Coastguard Worker * message into errmsgbuf and returns -2. Otherwise, tries to read
3451*8b26181fSAndroid Build Coastguard Worker * the data and, if that succeeds, subtracts the amount read from
3452*8b26181fSAndroid Build Coastguard Worker * the number of bytes of data that remains.
3453*8b26181fSAndroid Build Coastguard Worker * Returns 0 on success, logs a message and returns -1 on a network
3454*8b26181fSAndroid Build Coastguard Worker * error.
3455*8b26181fSAndroid Build Coastguard Worker */
rpcap_recv(SOCKET sock,SSL * ssl,void * buffer,size_t toread,uint32 * plen,char * errbuf)3456*8b26181fSAndroid Build Coastguard Worker static int rpcap_recv(SOCKET sock, SSL *ssl, void *buffer, size_t toread, uint32 *plen, char *errbuf)
3457*8b26181fSAndroid Build Coastguard Worker {
3458*8b26181fSAndroid Build Coastguard Worker int nread;
3459*8b26181fSAndroid Build Coastguard Worker
3460*8b26181fSAndroid Build Coastguard Worker if (toread > *plen)
3461*8b26181fSAndroid Build Coastguard Worker {
3462*8b26181fSAndroid Build Coastguard Worker /* The server sent us a bad message */
3463*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE, "Message payload is too short");
3464*8b26181fSAndroid Build Coastguard Worker return -1;
3465*8b26181fSAndroid Build Coastguard Worker }
3466*8b26181fSAndroid Build Coastguard Worker nread = sock_recv(sock, ssl, buffer, toread,
3467*8b26181fSAndroid Build Coastguard Worker SOCK_RECEIVEALL_YES|SOCK_EOF_IS_ERROR, errbuf, PCAP_ERRBUF_SIZE);
3468*8b26181fSAndroid Build Coastguard Worker if (nread == -1)
3469*8b26181fSAndroid Build Coastguard Worker {
3470*8b26181fSAndroid Build Coastguard Worker return -1;
3471*8b26181fSAndroid Build Coastguard Worker }
3472*8b26181fSAndroid Build Coastguard Worker *plen -= nread;
3473*8b26181fSAndroid Build Coastguard Worker return 0;
3474*8b26181fSAndroid Build Coastguard Worker }
3475*8b26181fSAndroid Build Coastguard Worker
3476*8b26181fSAndroid Build Coastguard Worker /*
3477*8b26181fSAndroid Build Coastguard Worker * This handles the RPCAP_MSG_ERROR message.
3478*8b26181fSAndroid Build Coastguard Worker */
rpcap_msg_err(SOCKET sockctrl,SSL * ssl,uint32 plen,char * remote_errbuf)3479*8b26181fSAndroid Build Coastguard Worker static void rpcap_msg_err(SOCKET sockctrl, SSL *ssl, uint32 plen, char *remote_errbuf)
3480*8b26181fSAndroid Build Coastguard Worker {
3481*8b26181fSAndroid Build Coastguard Worker char errbuf[PCAP_ERRBUF_SIZE];
3482*8b26181fSAndroid Build Coastguard Worker
3483*8b26181fSAndroid Build Coastguard Worker if (plen >= PCAP_ERRBUF_SIZE)
3484*8b26181fSAndroid Build Coastguard Worker {
3485*8b26181fSAndroid Build Coastguard Worker /*
3486*8b26181fSAndroid Build Coastguard Worker * Message is too long; just read as much of it as we
3487*8b26181fSAndroid Build Coastguard Worker * can into the buffer provided, and discard the rest.
3488*8b26181fSAndroid Build Coastguard Worker */
3489*8b26181fSAndroid Build Coastguard Worker if (sock_recv(sockctrl, ssl, remote_errbuf, PCAP_ERRBUF_SIZE - 1,
3490*8b26181fSAndroid Build Coastguard Worker SOCK_RECEIVEALL_YES|SOCK_EOF_IS_ERROR, errbuf,
3491*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE) == -1)
3492*8b26181fSAndroid Build Coastguard Worker {
3493*8b26181fSAndroid Build Coastguard Worker // Network error.
3494*8b26181fSAndroid Build Coastguard Worker DIAG_OFF_FORMAT_TRUNCATION
3495*8b26181fSAndroid Build Coastguard Worker snprintf(remote_errbuf, PCAP_ERRBUF_SIZE, "Read of error message from client failed: %s", errbuf);
3496*8b26181fSAndroid Build Coastguard Worker DIAG_ON_FORMAT_TRUNCATION
3497*8b26181fSAndroid Build Coastguard Worker return;
3498*8b26181fSAndroid Build Coastguard Worker }
3499*8b26181fSAndroid Build Coastguard Worker
3500*8b26181fSAndroid Build Coastguard Worker /*
3501*8b26181fSAndroid Build Coastguard Worker * Null-terminate it.
3502*8b26181fSAndroid Build Coastguard Worker */
3503*8b26181fSAndroid Build Coastguard Worker remote_errbuf[PCAP_ERRBUF_SIZE - 1] = '\0';
3504*8b26181fSAndroid Build Coastguard Worker
3505*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
3506*8b26181fSAndroid Build Coastguard Worker /*
3507*8b26181fSAndroid Build Coastguard Worker * If we're not in UTF-8 mode, convert it to the local
3508*8b26181fSAndroid Build Coastguard Worker * code page.
3509*8b26181fSAndroid Build Coastguard Worker */
3510*8b26181fSAndroid Build Coastguard Worker if (!pcap_utf_8_mode)
3511*8b26181fSAndroid Build Coastguard Worker utf_8_to_acp_truncated(remote_errbuf);
3512*8b26181fSAndroid Build Coastguard Worker #endif
3513*8b26181fSAndroid Build Coastguard Worker
3514*8b26181fSAndroid Build Coastguard Worker /*
3515*8b26181fSAndroid Build Coastguard Worker * Throw away the rest.
3516*8b26181fSAndroid Build Coastguard Worker */
3517*8b26181fSAndroid Build Coastguard Worker (void)rpcap_discard(sockctrl, ssl, plen - (PCAP_ERRBUF_SIZE - 1), remote_errbuf);
3518*8b26181fSAndroid Build Coastguard Worker }
3519*8b26181fSAndroid Build Coastguard Worker else if (plen == 0)
3520*8b26181fSAndroid Build Coastguard Worker {
3521*8b26181fSAndroid Build Coastguard Worker /* Empty error string. */
3522*8b26181fSAndroid Build Coastguard Worker remote_errbuf[0] = '\0';
3523*8b26181fSAndroid Build Coastguard Worker }
3524*8b26181fSAndroid Build Coastguard Worker else
3525*8b26181fSAndroid Build Coastguard Worker {
3526*8b26181fSAndroid Build Coastguard Worker if (sock_recv(sockctrl, ssl, remote_errbuf, plen,
3527*8b26181fSAndroid Build Coastguard Worker SOCK_RECEIVEALL_YES|SOCK_EOF_IS_ERROR, errbuf,
3528*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE) == -1)
3529*8b26181fSAndroid Build Coastguard Worker {
3530*8b26181fSAndroid Build Coastguard Worker // Network error.
3531*8b26181fSAndroid Build Coastguard Worker DIAG_OFF_FORMAT_TRUNCATION
3532*8b26181fSAndroid Build Coastguard Worker snprintf(remote_errbuf, PCAP_ERRBUF_SIZE, "Read of error message from client failed: %s", errbuf);
3533*8b26181fSAndroid Build Coastguard Worker DIAG_ON_FORMAT_TRUNCATION
3534*8b26181fSAndroid Build Coastguard Worker return;
3535*8b26181fSAndroid Build Coastguard Worker }
3536*8b26181fSAndroid Build Coastguard Worker
3537*8b26181fSAndroid Build Coastguard Worker /*
3538*8b26181fSAndroid Build Coastguard Worker * Null-terminate it.
3539*8b26181fSAndroid Build Coastguard Worker */
3540*8b26181fSAndroid Build Coastguard Worker remote_errbuf[plen] = '\0';
3541*8b26181fSAndroid Build Coastguard Worker }
3542*8b26181fSAndroid Build Coastguard Worker }
3543*8b26181fSAndroid Build Coastguard Worker
3544*8b26181fSAndroid Build Coastguard Worker /*
3545*8b26181fSAndroid Build Coastguard Worker * Discard data from a connection.
3546*8b26181fSAndroid Build Coastguard Worker * Mostly used to discard wrong-sized messages.
3547*8b26181fSAndroid Build Coastguard Worker * Returns 0 on success, logs a message and returns -1 on a network
3548*8b26181fSAndroid Build Coastguard Worker * error.
3549*8b26181fSAndroid Build Coastguard Worker */
rpcap_discard(SOCKET sock,SSL * ssl,uint32 len,char * errbuf)3550*8b26181fSAndroid Build Coastguard Worker static int rpcap_discard(SOCKET sock, SSL *ssl, uint32 len, char *errbuf)
3551*8b26181fSAndroid Build Coastguard Worker {
3552*8b26181fSAndroid Build Coastguard Worker if (len != 0)
3553*8b26181fSAndroid Build Coastguard Worker {
3554*8b26181fSAndroid Build Coastguard Worker if (sock_discard(sock, ssl, len, errbuf, PCAP_ERRBUF_SIZE) == -1)
3555*8b26181fSAndroid Build Coastguard Worker {
3556*8b26181fSAndroid Build Coastguard Worker // Network error.
3557*8b26181fSAndroid Build Coastguard Worker return -1;
3558*8b26181fSAndroid Build Coastguard Worker }
3559*8b26181fSAndroid Build Coastguard Worker }
3560*8b26181fSAndroid Build Coastguard Worker return 0;
3561*8b26181fSAndroid Build Coastguard Worker }
3562*8b26181fSAndroid Build Coastguard Worker
3563*8b26181fSAndroid Build Coastguard Worker /*
3564*8b26181fSAndroid Build Coastguard Worker * Read bytes into the pcap_t's buffer until we have the specified
3565*8b26181fSAndroid Build Coastguard Worker * number of bytes read or we get an error or interrupt indication.
3566*8b26181fSAndroid Build Coastguard Worker */
rpcap_read_packet_msg(struct pcap_rpcap const * rp,pcap_t * p,size_t size)3567*8b26181fSAndroid Build Coastguard Worker static int rpcap_read_packet_msg(struct pcap_rpcap const *rp, pcap_t *p, size_t size)
3568*8b26181fSAndroid Build Coastguard Worker {
3569*8b26181fSAndroid Build Coastguard Worker u_char *bp;
3570*8b26181fSAndroid Build Coastguard Worker int cc;
3571*8b26181fSAndroid Build Coastguard Worker int bytes_read;
3572*8b26181fSAndroid Build Coastguard Worker
3573*8b26181fSAndroid Build Coastguard Worker bp = p->bp;
3574*8b26181fSAndroid Build Coastguard Worker cc = p->cc;
3575*8b26181fSAndroid Build Coastguard Worker
3576*8b26181fSAndroid Build Coastguard Worker /*
3577*8b26181fSAndroid Build Coastguard Worker * Loop until we have the amount of data requested or we get
3578*8b26181fSAndroid Build Coastguard Worker * an error or interrupt.
3579*8b26181fSAndroid Build Coastguard Worker */
3580*8b26181fSAndroid Build Coastguard Worker while ((size_t)cc < size)
3581*8b26181fSAndroid Build Coastguard Worker {
3582*8b26181fSAndroid Build Coastguard Worker /*
3583*8b26181fSAndroid Build Coastguard Worker * We haven't read all of the packet header yet.
3584*8b26181fSAndroid Build Coastguard Worker * Read what remains, which could be all of it.
3585*8b26181fSAndroid Build Coastguard Worker */
3586*8b26181fSAndroid Build Coastguard Worker bytes_read = sock_recv(rp->rmt_sockdata, rp->data_ssl, bp, size - cc,
3587*8b26181fSAndroid Build Coastguard Worker SOCK_RECEIVEALL_NO|SOCK_EOF_IS_ERROR, p->errbuf,
3588*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE);
3589*8b26181fSAndroid Build Coastguard Worker
3590*8b26181fSAndroid Build Coastguard Worker if (bytes_read == -1)
3591*8b26181fSAndroid Build Coastguard Worker {
3592*8b26181fSAndroid Build Coastguard Worker /*
3593*8b26181fSAndroid Build Coastguard Worker * Network error. Update the read pointer and
3594*8b26181fSAndroid Build Coastguard Worker * byte count, and return an error indication.
3595*8b26181fSAndroid Build Coastguard Worker */
3596*8b26181fSAndroid Build Coastguard Worker p->bp = bp;
3597*8b26181fSAndroid Build Coastguard Worker p->cc = cc;
3598*8b26181fSAndroid Build Coastguard Worker return -1;
3599*8b26181fSAndroid Build Coastguard Worker }
3600*8b26181fSAndroid Build Coastguard Worker if (bytes_read == -3)
3601*8b26181fSAndroid Build Coastguard Worker {
3602*8b26181fSAndroid Build Coastguard Worker /*
3603*8b26181fSAndroid Build Coastguard Worker * Interrupted receive. Update the read
3604*8b26181fSAndroid Build Coastguard Worker * pointer and byte count, and return
3605*8b26181fSAndroid Build Coastguard Worker * an interrupted indication.
3606*8b26181fSAndroid Build Coastguard Worker */
3607*8b26181fSAndroid Build Coastguard Worker p->bp = bp;
3608*8b26181fSAndroid Build Coastguard Worker p->cc = cc;
3609*8b26181fSAndroid Build Coastguard Worker return -3;
3610*8b26181fSAndroid Build Coastguard Worker }
3611*8b26181fSAndroid Build Coastguard Worker if (bytes_read == 0)
3612*8b26181fSAndroid Build Coastguard Worker {
3613*8b26181fSAndroid Build Coastguard Worker /*
3614*8b26181fSAndroid Build Coastguard Worker * EOF - server terminated the connection.
3615*8b26181fSAndroid Build Coastguard Worker * Update the read pointer and byte count, and
3616*8b26181fSAndroid Build Coastguard Worker * return an error indication.
3617*8b26181fSAndroid Build Coastguard Worker */
3618*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
3619*8b26181fSAndroid Build Coastguard Worker "The server terminated the connection.");
3620*8b26181fSAndroid Build Coastguard Worker return -1;
3621*8b26181fSAndroid Build Coastguard Worker }
3622*8b26181fSAndroid Build Coastguard Worker bp += bytes_read;
3623*8b26181fSAndroid Build Coastguard Worker cc += bytes_read;
3624*8b26181fSAndroid Build Coastguard Worker }
3625*8b26181fSAndroid Build Coastguard Worker p->bp = bp;
3626*8b26181fSAndroid Build Coastguard Worker p->cc = cc;
3627*8b26181fSAndroid Build Coastguard Worker return 0;
3628*8b26181fSAndroid Build Coastguard Worker }
3629