2 * Copyright (c) 2002 - 2005 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (c) 2005 - 2008 CACE Technologies, Davis (California)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the Politecnico di Torino, CACE Technologies
16 * nor the names of its contributors may be used to endorse or promote
17 * products derived from this software without specific prior written
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <string.h> /* for strlen(), ... */
39 #include <stdlib.h> /* for malloc(), free(), ... */
40 #include <stdarg.h> /* for functions with variable number of arguments */
41 #include <errno.h> /* for the errno variable */
43 #include "sockutils.h"
44 #include "rpcap-protocol.h"
45 #include "pcap-rpcap-int.h"
50 * This file keeps all the new functions that are needed for the RPCAP protocol.
51 * Almost all the pcap functions need to be modified in order to become compatible
52 * with the RPCAP protocol. However, you can find here only the ones that are completely new.
54 * This file keeps also the functions that are 'private', i.e. are needed by the RPCAP
55 * protocol but are not exported to the user.
57 * \warning All the RPCAP functions that are allowed to return a buffer containing
58 * the error description can return max PCAP_ERRBUF_SIZE characters.
59 * However there is no guarantees that the string will be zero-terminated.
60 * Best practice is to define the errbuf variable as a char of size 'PCAP_ERRBUF_SIZE+1'
61 * and to insert manually a NULL character at the end of the buffer. This will
62 * guarantee that no buffer overflows occur even if we use the printf() to show
63 * the error on the screen.
66 #define PCAP_STATS_STANDARD 0 /* Used by pcap_stats_rpcap to see if we want standard or extended statistics */
68 #define PCAP_STATS_EX 1 /* Used by pcap_stats_rpcap to see if we want standard or extended statistics */
71 /* Keeps a list of all the opened connections in the active mode. */
72 struct activehosts
*activeHosts
;
75 * Private data for capturing remotely using the rpcap protocol.
79 * This is '1' if we're the network client; it is needed by several
80 * functions (such as pcap_setfilter()) to know whether they have
81 * to use the socket or have to open the local adapter.
85 SOCKET rmt_sockctrl
; /* socket ID of the socket used for the control connection */
86 SOCKET rmt_sockdata
; /* socket ID of the socket used for the data connection */
87 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() */
88 int rmt_capstarted
; /* 'true' if the capture is already started (needed to knoe if we have to call the pcap_startcapture() */
89 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. */
91 unsigned int TotNetDrops
; /* keeps the number of packets that have been dropped by the network */
94 * This keeps the number of packets that have been received by the
97 * Packets dropped by the kernel buffer are not counted in this
98 * variable. It is always equal to (TotAccepted - TotDrops),
99 * except for the case of remote capture, in which we have also
100 * packets in flight, i.e. that have been transmitted by the remote
101 * host, but that have not been received (yet) from the client.
102 * In this case, (TotAccepted - TotDrops - TotNetDrops) gives a
103 * wrong result, since this number does not corresponds always to
104 * the number of packet received by the application. For this reason,
105 * in the remote capture we need another variable that takes into
106 * account of the number of packets actually received by the
109 unsigned int TotCapt
;
111 struct pcap_stat stat
;
113 struct pcap
*next
; /* list of open pcaps that need stuff cleared on close */
116 /****************************************************
118 * Locally defined functions *
120 ****************************************************/
121 static int rpcap_checkver(SOCKET sock
, struct rpcap_header
*header
, char *errbuf
);
122 static struct pcap_stat
*rpcap_stats_rpcap(pcap_t
*p
, struct pcap_stat
*ps
, int mode
);
123 static int pcap_pack_bpffilter(pcap_t
*fp
, char *sendbuf
, int *sendbufidx
, struct bpf_program
*prog
);
124 static int pcap_createfilter_norpcappkt(pcap_t
*fp
, struct bpf_program
*prog
);
125 static int pcap_updatefilter_remote(pcap_t
*fp
, struct bpf_program
*prog
);
126 static void pcap_save_current_filter_rpcap(pcap_t
*fp
, const char *filter
);
127 static int pcap_setfilter_rpcap(pcap_t
*fp
, struct bpf_program
*prog
);
128 static int pcap_setsampling_remote(pcap_t
*p
);
130 /****************************************************
134 ****************************************************/
137 * \ingroup remote_pri_func
139 * \brief It traslates (i.e. de-serializes) a 'rpcap_sockaddr'
140 * structure from the network byte order to a 'sockaddr_in" or
141 * 'sockaddr_in6' structure in the host byte order.
143 * It accepts an 'rpcap_sockaddr' structure as it is received from the
144 * network, and checks the address family field against various values
145 * to see whether it looks like an IPv4 address, an IPv6 address, or
146 * neither of those. It checks for multiple values in order to try
147 * to handle older rpcap daemons that sent the native OS's 'sockaddr_in'
148 * or 'sockaddr_in6' structures over the wire with some members
149 * byte-swapped, and to handle the fact that AF_INET6 has different
150 * values on different OSes.
152 * For IPv4 addresses, it converts the address family to host byte
153 * order from network byte order and puts it into the structure,
154 * sets the length if a sockaddr structure has a length, converts the
155 * port number to host byte order from network byte order and puts
156 * it into the structure, copies over the IPv4 address, and zeroes
157 * out the zero padding.
159 * For IPv6 addresses, it converts the address family to host byte
160 * order from network byte order and puts it into the structure,
161 * sets the length if a sockaddr structure has a length, converts the
162 * port number and flow information to host byte order from network
163 * byte order and puts them into the structure, copies over the IPv6
164 * address, and converts the scope ID to host byte order from network
165 * byte order and puts it into the structure.
167 * The function will allocate the 'sockaddrout' variable according to the
168 * address family in use. In case the address does not belong to the
169 * AF_INET nor AF_INET6 families, 'sockaddrout' is not allocated and a
170 * NULL pointer is returned. This usually happens because that address
171 * does not exist on the other host, or is of an address family other
172 * than AF_INET or AF_INET6, so the RPCAP daemon sent a 'sockaddr_storage'
173 * structure containing all 'zero' values.
175 * Older RPCAPDs sent the addresses over the wire in the OS's native
176 * structure format. For most OSes, this looks like the over-the-wire
177 * format, but might have a different value for AF_INET6 than the value
178 * on the machine receiving the reply. For OSes with the newer BSD-style
179 * sockaddr structures, this has, instead of a 2-byte address family,
180 * a 1-byte structure length followed by a 1-byte address family. The
181 * RPCAPD code would put the address family in network byte order before
182 * sending it; that would set it to 0 on a little-endian machine, as
183 * htons() of any value between 1 and 255 would result in a value > 255,
184 * with its lower 8 bits zero, so putting that back into a 1-byte field
187 * Therefore, for older RPCAPDs running on an OS with newer BSD-style
188 * sockaddr structures, the family field, if treated as a big-endian
189 * (network byte order) 16-bit field, would be:
191 * (length << 8) | family if sent by a big-endian machine
192 * (length << 8) if sent by a little-endian machine
194 * For current RPCAPDs, and for older RPCAPDs running on an OS with
195 * older BSD-style sockaddr structures, the family field, if treated
196 * as a big-endian 16-bit field, would just contain the family.
198 * \param sockaddrin: a 'rpcap_sockaddr' pointer to the variable that has
199 * to be de-serialized.
201 * \param sockaddrout: a 'sockaddr_storage' pointer to the variable that will contain
202 * the de-serialized data. The structure returned can be either a 'sockaddr_in' or 'sockaddr_in6'.
203 * This variable will be allocated automatically inside this function.
205 * \param errbuf: a pointer to a user-allocated buffer (of size PCAP_ERRBUF_SIZE)
206 * that will contain the error message (in case there is one).
208 * \return '0' if everything is fine, '-1' if some errors occurred. Basically, the error
209 * can be only the fact that the malloc() failed to allocate memory.
210 * The error message is returned in the 'errbuf' variable, while the deserialized address
211 * is returned into the 'sockaddrout' variable.
213 * \warning This function supports only AF_INET and AF_INET6 address families.
215 * \warning The sockaddrout (if not NULL) must be deallocated by the user.
219 * Possible IPv4 family values other than the designated over-the-wire value,
220 * which is 2 (because everybody uses 2 for AF_INET4).
222 #define SOCKADDR_IN_LEN 16 /* length of struct sockaddr_in */
223 #define SOCKADDR_IN6_LEN 28 /* length of struct sockaddr_in6 */
224 #define NEW_BSD_AF_INET_BE ((SOCKADDR_IN_LEN << 8) | 2)
225 #define NEW_BSD_AF_INET_LE (SOCKADDR_IN_LEN << 8)
228 * Possible IPv6 family values other than the designated over-the-wire value,
229 * which is 23 (because that's what Windows uses, and most RPCAP servers
230 * out there are probably running Windows, as WinPcap includes the server
231 * but few if any UN*Xes build and ship it).
233 * The new BSD sockaddr structure format was in place before 4.4-Lite, so
234 * all the free-software BSDs use it.
236 #define NEW_BSD_AF_INET6_BSD_BE ((SOCKADDR_IN6_LEN << 8) | 24) /* NetBSD, OpenBSD, BSD/OS */
237 #define NEW_BSD_AF_INET6_FREEBSD_BE ((SOCKADDR_IN6_LEN << 8) | 28) /* FreeBSD, DragonFly BSD */
238 #define NEW_BSD_AF_INET6_DARWIN_BE ((SOCKADDR_IN6_LEN << 8) | 30) /* macOS, iOS, anything else Darwin-based */
239 #define NEW_BSD_AF_INET6_LE (SOCKADDR_IN6_LEN << 8)
240 #define LINUX_AF_INET6 10
241 #define HPUX_AF_INET6 22
242 #define AIX_AF_INET6 24
243 #define SOLARIS_AF_INET6 26
245 int rpcap_deseraddr(struct rpcap_sockaddr
*sockaddrin
, struct sockaddr_storage
**sockaddrout
, char *errbuf
)
247 /* Warning: we support only AF_INET and AF_INET6 */
248 switch (ntohs(sockaddrin
->family
))
251 case NEW_BSD_AF_INET_BE
:
252 case NEW_BSD_AF_INET_LE
:
254 struct rpcap_sockaddr_in
*sockaddrin_ipv4
;
255 struct sockaddr_in
*sockaddrout_ipv4
;
257 (*sockaddrout
) = (struct sockaddr_storage
*) malloc(sizeof(struct sockaddr_in
));
258 if ((*sockaddrout
) == NULL
)
260 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "malloc() failed: %s", pcap_strerror(errno
));
263 sockaddrin_ipv4
= (struct rpcap_sockaddr_in
*) sockaddrin
;
264 sockaddrout_ipv4
= (struct sockaddr_in
*) (*sockaddrout
);
265 sockaddrout_ipv4
->sin_family
= AF_INET
;
266 sockaddrout_ipv4
->sin_port
= ntohs(sockaddrin_ipv4
->port
);
267 memcpy(&sockaddrout_ipv4
->sin_addr
, &sockaddrin_ipv4
->addr
, sizeof(sockaddrout_ipv4
->sin_addr
));
268 memset(sockaddrout_ipv4
->sin_zero
, 0, sizeof(sockaddrout_ipv4
->sin_zero
));
274 case NEW_BSD_AF_INET6_BSD_BE
:
275 case NEW_BSD_AF_INET6_FREEBSD_BE
:
276 case NEW_BSD_AF_INET6_DARWIN_BE
:
277 case NEW_BSD_AF_INET6_LE
:
281 case SOLARIS_AF_INET6
:
283 struct rpcap_sockaddr_in6
*sockaddrin_ipv6
;
284 struct sockaddr_in6
*sockaddrout_ipv6
;
286 (*sockaddrout
) = (struct sockaddr_storage
*) malloc(sizeof(struct sockaddr_in6
));
287 if ((*sockaddrout
) == NULL
)
289 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "malloc() failed: %s", pcap_strerror(errno
));
292 sockaddrin_ipv6
= (struct rpcap_sockaddr_in6
*) sockaddrin
;
293 sockaddrout_ipv6
= (struct sockaddr_in6
*) (*sockaddrout
);
294 sockaddrout_ipv6
->sin6_family
= AF_INET6
;
295 sockaddrout_ipv6
->sin6_port
= ntohs(sockaddrin_ipv6
->port
);
296 sockaddrout_ipv6
->sin6_flowinfo
= ntohl(sockaddrin_ipv6
->flowinfo
);
297 memcpy(&sockaddrout_ipv6
->sin6_addr
, &sockaddrin_ipv6
->addr
, sizeof(sockaddrout_ipv6
->sin6_addr
));
298 sockaddrout_ipv6
->sin6_scope_id
= ntohl(sockaddrin_ipv6
->scope_id
);
305 * It is neither AF_INET nor AF_INET6 (or, if the OS doesn't
306 * support AF_INET6, it's not AF_INET).
314 /* \ingroup remote_pri_func
316 * \brief It reads a packet from the network socket. This does not make use of
317 * callback (hence the "nocb" string into its name).
319 * This function is called by the several pcap_next_ex() when they detect that
320 * we have a remote capture and they are the client side. In that case, they need
321 * to read packets from the socket.
323 * Parameters and return values are exactly the same of the pcap_next_ex().
325 * \warning By choice, this function does not make use of semaphores. A smarter
326 * implementation should put a semaphore into the data thread, and a signal will
327 * be raised as soon as there is data into the socket buffer.
328 * However this is complicated and it does not bring any advantages when reading
329 * from the network, in which network delays can be much more important than
330 * these optimizations. Therefore, we chose the following approach:
331 * - the 'timeout' chosen by the user is split in two (half on the server side,
332 * with the usual meaning, and half on the client side)
333 * - this function checks for packets; if there are no packets, it waits for
334 * timeout/2 and then it checks again. If packets are still missing, it returns,
335 * otherwise it reads packets.
337 static int pcap_read_nocb_remote(pcap_t
*p
, struct pcap_pkthdr
**pkt_header
, u_char
**pkt_data
)
339 struct pcap_rpcap
*pr
= p
->priv
; /* structure used when doing a remote live capture */
340 struct rpcap_header
*header
; /* general header according to the RPCAP format */
341 struct rpcap_pkthdr
*net_pkt_header
; /* header of the packet */
342 char netbuf
[RPCAP_NETBUF_SIZE
]; /* size of the network buffer in which the packet is copied, just for UDP */
343 uint32 totread
; /* number of bytes (of payload) currently read from the network (referred to the current pkt) */
345 int retval
; /* generic return value */
347 /* Structures needed for the select() call */
348 struct timeval tv
; /* maximum time the select() can block waiting for data */
349 fd_set rfds
; /* set of socket descriptors we have to check */
352 * Define the packet buffer timeout, to be used in the select()
353 * 'timeout', in pcap_t, is in milliseconds; we have to convert it into sec and microsec
355 tv
.tv_sec
= p
->opt
.timeout
/ 1000;
356 tv
.tv_usec
= (p
->opt
.timeout
- tv
.tv_sec
* 1000) * 1000;
358 /* Watch out sockdata to see if it has input */
362 * 'fp->rmt_sockdata' has always to be set before calling the select(),
363 * since it is cleared by the select()
365 FD_SET(pr
->rmt_sockdata
, &rfds
);
367 retval
= select((int) pr
->rmt_sockdata
+ 1, &rfds
, NULL
, NULL
, &tv
);
370 sock_geterror("select(): ", p
->errbuf
, PCAP_ERRBUF_SIZE
);
374 /* There is no data waiting, so return '0' */
379 * data is here; so, let's copy it into the user buffer.
380 * I'm going to read a new packet; so I reset the number of bytes (payload only) read
385 * We have to define 'header' as a pointer to a larger buffer,
386 * because in case of UDP we have to read all the message within a single call
388 header
= (struct rpcap_header
*) netbuf
;
389 net_pkt_header
= (struct rpcap_pkthdr
*) (netbuf
+ sizeof(struct rpcap_header
));
391 if (pr
->rmt_flags
& PCAP_OPENFLAG_DATATX_UDP
)
393 /* Read the entire message from the network */
394 if (sock_recv(pr
->rmt_sockdata
, netbuf
, RPCAP_NETBUF_SIZE
, SOCK_RECEIVEALL_NO
, p
->errbuf
, PCAP_ERRBUF_SIZE
) == -1)
399 if (sock_recv(pr
->rmt_sockdata
, netbuf
, sizeof(struct rpcap_header
), SOCK_RECEIVEALL_YES
, p
->errbuf
, PCAP_ERRBUF_SIZE
) == -1)
403 /* Checks if the message is correct */
404 retval
= rpcap_checkmsg(p
->errbuf
, pr
->rmt_sockdata
, header
, RPCAP_MSG_PACKET
, 0);
406 if (retval
!= RPCAP_MSG_PACKET
) /* the message is not the one expected */
410 case -3: /* Unrecoverable network error */
411 return -1; /* Do nothing; just exit from here; the error code is already into the errbuf */
413 case -2: /* The other endpoint sent a message that is not allowed here */
414 case -1: /* The other endpoint has a version number that is not compatible with our */
415 return 0; /* Return 'no packets received' */
418 SOCK_ASSERT("Internal error", 1);
419 return 0; /* Return 'no packets received' */
423 /* In case of TCP, read the remaining of the packet from the socket */
424 if (!(pr
->rmt_flags
& PCAP_OPENFLAG_DATATX_UDP
))
426 /* Read the RPCAP packet header from the network */
427 nread
= sock_recv(pr
->rmt_sockdata
, (char *)net_pkt_header
,
428 sizeof(struct rpcap_pkthdr
), SOCK_RECEIVEALL_YES
,
429 p
->errbuf
, PCAP_ERRBUF_SIZE
);
435 if ((ntohl(net_pkt_header
->caplen
) + sizeof(struct pcap_pkthdr
)) <= p
->bufsize
)
437 /* Initialize returned structures */
438 *pkt_header
= (struct pcap_pkthdr
*) p
->buffer
;
439 *pkt_data
= (u_char
*)p
->buffer
+ sizeof(struct pcap_pkthdr
);
441 (*pkt_header
)->caplen
= ntohl(net_pkt_header
->caplen
);
442 (*pkt_header
)->len
= ntohl(net_pkt_header
->len
);
443 (*pkt_header
)->ts
.tv_sec
= ntohl(net_pkt_header
->timestamp_sec
);
444 (*pkt_header
)->ts
.tv_usec
= ntohl(net_pkt_header
->timestamp_usec
);
447 * I don't update the counter of the packets dropped by the network since we're using TCP,
448 * therefore no packets are dropped. Just update the number of packets received correctly
452 /* Copies the packet into the data buffer */
453 if (pr
->rmt_flags
& PCAP_OPENFLAG_DATATX_UDP
)
458 * In case of UDP the packet has already been read, we have to copy it into 'buffer'.
459 * Another option should be to declare 'netbuf' as 'static'. However this prevents
460 * using several pcap instances within the same process (because the static buffer is shared among
463 memcpy(*pkt_data
, netbuf
+ sizeof(struct rpcap_header
) + sizeof(struct rpcap_pkthdr
), (*pkt_header
)->caplen
);
465 /* We're using UDP, so we need to update the counter of the packets dropped by the network */
466 npkt
= ntohl(net_pkt_header
->npkt
);
468 if (pr
->TotCapt
!= npkt
)
470 pr
->TotNetDrops
+= (npkt
- pr
->TotCapt
);
476 /* In case of TCP, read the remaining of the packet from the socket */
477 nread
= sock_recv(pr
->rmt_sockdata
, *pkt_data
,
478 (*pkt_header
)->caplen
, SOCK_RECEIVEALL_YES
,
479 p
->errbuf
, PCAP_ERRBUF_SIZE
);
484 /* Checks if all the data has been read; if not, discard the data in excess */
485 /* This check has to be done only on TCP connections */
486 if (totread
!= ntohl(header
->plen
))
487 sock_discard(pr
->rmt_sockdata
, ntohl(header
->plen
) - totread
, NULL
, 0);
490 /* Packet read successfully */
495 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Received a packet that is larger than the internal buffer size.");
500 /* \ingroup remote_pri_func
502 * \brief It reads a packet from the network socket.
504 * This function is called by the several pcap_read() when they detect that
505 * we have a remote capture and they are the client side. In that case, they need
506 * to read packets from the socket.
508 * This function relies on the pcap_read_nocb_remote to deliver packets. The
509 * difference, here, is that as soon as a packet is read, it is delivered
510 * to the application by means of a callback function.
512 * Parameters and return values are exactly the same of the pcap_read().
514 static int pcap_read_rpcap(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
516 struct pcap_rpcap
*pr
= p
->priv
; /* structure used when doing a remote live capture */
517 struct pcap_pkthdr
*pkt_header
;
522 * If this is client-side, and we haven't already started
523 * the capture, start it now.
525 if (pr
->rmt_clientside
)
527 /* We are on an remote capture */
528 if (!pr
->rmt_capstarted
)
531 * The capture isn't started yet, so try to
534 if (pcap_startcapture_remote(p
))
539 while (n
< cnt
|| PACKET_COUNT_IS_UNLIMITED(cnt
))
542 * Has "pcap_breakloop()" been called?
546 * Yes - clear the flag that indicates that it
547 * has, and return PCAP_ERROR_BREAK to indicate
548 * that we were told to break out of the loop.
551 return (PCAP_ERROR_BREAK
);
557 if (pcap_read_nocb_remote(p
, &pkt_header
, &pkt_data
) == 1)
559 (*callback
)(user
, pkt_header
, pkt_data
);
568 /* \ingroup remote_pri_func
570 * \brief It sends a CLOSE command to the capture server.
572 * This function is called when the user wants to close a pcap_t adapter.
573 * In case we're capturing from the network, it sends a command to the other
574 * peer that says 'ok, let's stop capturing'.
575 * This function is called automatically when the user calls the pcap_close().
577 * Parameters and return values are exactly the same of the pcap_close().
579 * \warning Since we're closing the connection, we do not check for errors.
581 static void pcap_cleanup_rpcap(pcap_t
*fp
)
583 struct pcap_rpcap
*pr
= fp
->priv
; /* structure used when doing a remote live capture */
584 struct rpcap_header header
; /* header of the RPCAP packet */
585 struct activehosts
*temp
; /* temp var needed to scan the host list chain, to detect if we're in active mode */
586 int active
= 0; /* active mode or not? */
588 /* detect if we're in active mode */
592 if (temp
->sockctrl
== pr
->rmt_sockctrl
)
602 rpcap_createhdr(&header
, RPCAP_MSG_CLOSE
, 0, 0);
604 /* I don't check for errors, since I'm going to close everything */
605 sock_send(pr
->rmt_sockctrl
, (char *)&header
, sizeof(struct rpcap_header
), NULL
, 0);
609 rpcap_createhdr(&header
, RPCAP_MSG_ENDCAP_REQ
, 0, 0);
611 /* I don't check for errors, since I'm going to close everything */
612 sock_send(pr
->rmt_sockctrl
, (char *)&header
, sizeof(struct rpcap_header
), NULL
, 0);
614 /* wait for the answer */
615 /* Don't check what we got, since the present libpcap does not uses this pcap_t anymore */
616 sock_recv(pr
->rmt_sockctrl
, (char *)&header
, sizeof(struct rpcap_header
), SOCK_RECEIVEALL_YES
, NULL
, 0);
618 if (ntohl(header
.plen
) != 0)
619 sock_discard(pr
->rmt_sockctrl
, ntohl(header
.plen
), NULL
, 0);
622 if (pr
->rmt_sockdata
)
624 sock_close(pr
->rmt_sockdata
, NULL
, 0);
625 pr
->rmt_sockdata
= 0;
628 if ((!active
) && (pr
->rmt_sockctrl
))
629 sock_close(pr
->rmt_sockctrl
, NULL
, 0);
631 pr
->rmt_sockctrl
= 0;
633 if (pr
->currentfilter
)
635 free(pr
->currentfilter
);
636 pr
->currentfilter
= NULL
;
639 /* To avoid inconsistencies in the number of sock_init() */
643 /* \ingroup remote_pri_func
645 * \brief It retrieves network statistics from the other peer.
647 * This function is just a void cointainer, since the work is done by the rpcap_stats_rpcap().
648 * See that funcion for more details.
650 * Parameters and return values are exactly the same of the pcap_stats().
652 static int pcap_stats_rpcap(pcap_t
*p
, struct pcap_stat
*ps
)
654 struct pcap_stat
*retval
;
656 retval
= rpcap_stats_rpcap(p
, ps
, PCAP_STATS_STANDARD
);
665 /* \ingroup remote_pri_func
667 * \brief It retrieves network statistics from the other peer.
669 * This function is just a void cointainer, since the work is done by the rpcap_stats_rpcap().
670 * See that funcion for more details.
672 * Parameters and return values are exactly the same of the pcap_stats_ex().
674 static struct pcap_stat
*pcap_stats_ex_rpcap(pcap_t
*p
, int *pcap_stat_size
)
676 *pcap_stat_size
= sizeof (p
->stat
);
678 /* PCAP_STATS_EX (third param) means 'extended pcap_stats()' */
679 return (rpcap_stats_rpcap(p
, &(p
->stat
), PCAP_STATS_EX
));
683 /* \ingroup remote_pri_func
685 * \brief It retrieves network statistics from the other peer.
687 * This function can be called in two modes:
688 * - PCAP_STATS_STANDARD: if we want just standard statistics (i.e. the pcap_stats() )
689 * - PCAP_STATS_EX: if we want extended statistics (i.e. the pcap_stats_ex() )
691 * This 'mode' parameter is needed because in the standard pcap_stats() the variable that keeps the
692 * statistics is allocated by the user. Unfortunately, this structure has been extended in order
693 * to keep new stats. However, if the user has a smaller structure and it passes it to the pcap_stats,
694 * thid function will try to fill in more data than the size of the structure, so that the application
695 * goes in memory overflow.
696 * So, we need to know it we have to copy just the standard fields, or the extended fields as well.
698 * In case we want to copy the extended fields as well, the problem of memory overflow does no
699 * longer exist because the structure pcap_stat is no longer allocated by the program;
700 * it is allocated by the library instead.
702 * \param p: the pcap_t structure related to the current instance.
704 * \param ps: a 'pcap_stat' structure, needed for compatibility with pcap_stat(), in which
705 * the structure is allocated by the user. In case of pcap_stats_ex, this structure and the
706 * function return value point to the same variable.
708 * \param mode: one of PCAP_STATS_STANDARD or PCAP_STATS_EX.
710 * \return The structure that keeps the statistics, or NULL in case of error.
711 * The error string is placed in the pcap_t structure.
713 static struct pcap_stat
*rpcap_stats_rpcap(pcap_t
*p
, struct pcap_stat
*ps
, int mode
)
715 struct pcap_rpcap
*pr
= p
->priv
; /* structure used when doing a remote live capture */
716 struct rpcap_header header
; /* header of the RPCAP packet */
717 struct rpcap_stats netstats
; /* statistics sent on the network */
718 uint32 totread
= 0; /* number of bytes of the payload read from the socket */
720 int retval
; /* temp variable which stores functions return value */
723 * If the capture has still to start, we cannot ask statistics to the other peer,
724 * so we return a fake number
726 if (!pr
->rmt_capstarted
)
731 #if defined(_WIN32) && defined(HAVE_REMOTE)
732 if (mode
== PCAP_STATS_EX
)
738 #endif /* _WIN32 && HAVE_REMOTE */
743 rpcap_createhdr(&header
, RPCAP_MSG_STATS_REQ
, 0, 0);
745 /* Send the PCAP_STATS command */
746 if (sock_send(pr
->rmt_sockctrl
, (char *)&header
, sizeof(struct rpcap_header
), p
->errbuf
, PCAP_ERRBUF_SIZE
))
749 /* Receive the RPCAP stats reply message */
750 if (sock_recv(pr
->rmt_sockctrl
, (char *)&header
, sizeof(struct rpcap_header
), SOCK_RECEIVEALL_YES
, p
->errbuf
, PCAP_ERRBUF_SIZE
) == -1)
753 /* Checks if the message is correct */
754 retval
= rpcap_checkmsg(p
->errbuf
, pr
->rmt_sockctrl
, &header
, RPCAP_MSG_STATS_REPLY
, RPCAP_MSG_ERROR
, 0);
756 if (retval
!= RPCAP_MSG_STATS_REPLY
) /* the message is not the one expected */
760 case -3: /* Unrecoverable network error */
761 case -2: /* The other endpoint send a message that is not allowed here */
762 case -1: /* The other endpoint has a version number that is not compatible with our */
765 case RPCAP_MSG_ERROR
: /* The other endpoint reported an error */
766 /* Update totread, since the rpcap_checkmsg() already purged the buffer */
767 totread
= ntohl(header
.plen
);
769 /* Do nothing; just exit; the error code is already into the errbuf */
773 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Internal error");
778 nread
= sock_recv(pr
->rmt_sockctrl
, (char *)&netstats
,
779 sizeof(struct rpcap_stats
), SOCK_RECEIVEALL_YES
,
780 p
->errbuf
, PCAP_ERRBUF_SIZE
);
785 ps
->ps_drop
= ntohl(netstats
.krnldrop
);
786 ps
->ps_ifdrop
= ntohl(netstats
.ifdrop
);
787 ps
->ps_recv
= ntohl(netstats
.ifrecv
);
788 #if defined(_WIN32) && defined(HAVE_REMOTE)
789 if (mode
== PCAP_STATS_EX
)
791 ps
->ps_capt
= pr
->TotCapt
;
792 ps
->ps_netdrop
= pr
->TotNetDrops
;
793 ps
->ps_sent
= ntohl(netstats
.svrcapt
);
795 #endif /* _WIN32 && HAVE_REMOTE */
797 /* Checks if all the data has been read; if not, discard the data in excess */
798 if (totread
!= ntohl(header
.plen
))
800 if (sock_discard(pr
->rmt_sockctrl
, ntohl(header
.plen
) - totread
, NULL
, 0) == 1)
807 if (totread
!= ntohl(header
.plen
))
808 sock_discard(pr
->rmt_sockctrl
, ntohl(header
.plen
) - totread
, NULL
, 0);
813 /* \ingroup remote_pri_func
815 * \brief It opens a remote adapter by opening an RPCAP connection and so on.
817 * This function does basically the job of pcap_open_live() for a remote interface.
818 * In other words, we have a pcap_read for win32, which reads packets from NPF,
819 * another for LINUX, and so on. Now, we have a pcap_opensource_remote() as well.
820 * The difference, here, is the capture thread does not start until the
821 * pcap_startcapture_remote() is called.
823 * This is because, in remote capture, we cannot start capturing data as soon as the
824 * 'open adapter' command is sent. Suppose the remote adapter is already overloaded;
825 * if we start a capture (which, by default, has a NULL filter) the new traffic can
826 * saturate the network.
828 * Instead, we want to "open" the adapter, then send a "start capture" command only
829 * when we're ready to start the capture.
830 * This function does this job: it sends an "open adapter" command (according to the
831 * RPCAP protocol), but it does not start the capture.
833 * Since the other libpcap functions do not share this way of life, we have to make
834 * some dirty things in order to make everyting working.
836 * \param fp: A pointer to a pcap_t structure that has been previously created with
837 * \ref pcap_create().
838 * \param source: see pcap_open().
839 * \param auth: see pcap_open().
841 * \return 0 in case of success, -1 otherwise. In case of success, the pcap_t pointer in fp can be
842 * used as a parameter to the following calls (pcap_compile() and so on). In case of
843 * problems, fp->errbuf contains a text explanation of error.
845 * \warning In case we call the pcap_compile() and the capture is not started, the filter
846 * will be saved into the pcap_t structure, and it will be sent to the other host later
847 * (when the pcap_startcapture_remote() is called).
849 int pcap_opensource_remote(pcap_t
*fp
, struct pcap_rmtauth
*auth
)
851 char host
[PCAP_BUF_SIZE
], ctrlport
[PCAP_BUF_SIZE
], iface
[PCAP_BUF_SIZE
];
853 char sendbuf
[RPCAP_NETBUF_SIZE
]; /* temporary buffer in which data to be sent is buffered */
854 int sendbufidx
= 0; /* index which keeps the number of bytes currently buffered */
855 uint32 totread
= 0; /* number of bytes of the payload read from the socket */
857 int retval
; /* store the return value of the functions */
858 int active
= 0; /* '1' if we're in active mode */
860 /* socket-related variables */
861 struct addrinfo hints
; /* temp, needed to open a socket connection */
862 struct addrinfo
*addrinfo
; /* temp, needed to open a socket connection */
863 SOCKET sockctrl
= 0; /* socket descriptor of the control connection */
865 /* RPCAP-related variables */
866 struct rpcap_header header
; /* header of the RPCAP packet */
867 struct rpcap_openreply openreply
; /* open reply message */
869 struct pcap_rpcap
*pr
= fp
->priv
; /* structure used when doing a remote live capture */
872 * determine the type of the source (NULL, file, local, remote)
873 * You must have a valid source string even if we're in active mode, because otherwise
874 * the call to the following function will fail.
876 if (pcap_parsesrcstr(fp
->opt
.device
, &retval
, host
, ctrlport
, iface
, fp
->errbuf
) == -1)
879 if (retval
!= PCAP_SRC_IFREMOTE
)
881 pcap_snprintf(fp
->errbuf
, PCAP_ERRBUF_SIZE
, "This function is able to open only remote interfaces");
888 * Warning: this call can be the first one called by the user.
889 * For this reason, we have to initialize the WinSock support.
891 if (sock_init(fp
->errbuf
, PCAP_ERRBUF_SIZE
) == -1)
894 sockctrl
= rpcap_remoteact_getsock(host
, &active
, fp
->errbuf
);
895 if (sockctrl
== INVALID_SOCKET
)
901 * We're not in active mode; let's try to open a new
902 * control connection.
904 memset(&hints
, 0, sizeof(struct addrinfo
));
905 hints
.ai_family
= PF_UNSPEC
;
906 hints
.ai_socktype
= SOCK_STREAM
;
908 if (ctrlport
[0] == 0)
910 /* the user chose not to specify the port */
911 if (sock_initaddress(host
, RPCAP_DEFAULT_NETPORT
, &hints
, &addrinfo
, fp
->errbuf
, PCAP_ERRBUF_SIZE
) == -1)
916 /* the user chose not to specify the port */
917 if (sock_initaddress(host
, ctrlport
, &hints
, &addrinfo
, fp
->errbuf
, PCAP_ERRBUF_SIZE
) == -1)
921 if ((sockctrl
= sock_open(addrinfo
, SOCKOPEN_CLIENT
, 0, fp
->errbuf
, PCAP_ERRBUF_SIZE
)) == INVALID_SOCKET
)
924 freeaddrinfo(addrinfo
);
927 if (rpcap_sendauth(sockctrl
, auth
, fp
->errbuf
) == -1)
932 * Now it's time to start playing with the RPCAP protocol
933 * RPCAP open command: create the request message
935 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
,
936 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, fp
->errbuf
, PCAP_ERRBUF_SIZE
))
939 rpcap_createhdr((struct rpcap_header
*) sendbuf
, RPCAP_MSG_OPEN_REQ
, 0, (uint32
) strlen(iface
));
941 if (sock_bufferize(iface
, (int) strlen(iface
), sendbuf
, &sendbufidx
,
942 RPCAP_NETBUF_SIZE
, SOCKBUF_BUFFERIZE
, fp
->errbuf
, PCAP_ERRBUF_SIZE
))
945 if (sock_send(sockctrl
, sendbuf
, sendbufidx
, fp
->errbuf
, PCAP_ERRBUF_SIZE
))
948 /* Receive the RPCAP open reply message */
949 if (sock_recv(sockctrl
, (char *)&header
, sizeof(struct rpcap_header
), SOCK_RECEIVEALL_YES
, fp
->errbuf
, PCAP_ERRBUF_SIZE
) == -1)
952 /* Checks if the message is correct */
953 retval
= rpcap_checkmsg(fp
->errbuf
, sockctrl
, &header
, RPCAP_MSG_OPEN_REPLY
, RPCAP_MSG_ERROR
, 0);
955 if (retval
!= RPCAP_MSG_OPEN_REPLY
) /* the message is not the one expected */
959 case -3: /* Unrecoverable network error */
960 case -2: /* The other endpoint send a message that is not allowed here */
961 case -1: /* The other endpoint has a version number that is not compatible with our */
964 case RPCAP_MSG_ERROR
: /* The other endpoint reported an error */
965 /* Update totread, since the rpcap_checkmsg() already purged the buffer */
966 totread
= ntohl(header
.plen
);
967 /* Do nothing; just exit; the error code is already into the errbuf */
971 pcap_snprintf(fp
->errbuf
, PCAP_ERRBUF_SIZE
, "Internal error");
976 nread
= sock_recv(sockctrl
, (char *)&openreply
,
977 sizeof(struct rpcap_openreply
), SOCK_RECEIVEALL_YES
,
978 fp
->errbuf
, PCAP_ERRBUF_SIZE
);
983 /* Set proper fields into the pcap_t struct */
984 fp
->linktype
= ntohl(openreply
.linktype
);
985 fp
->tzoff
= ntohl(openreply
.tzoff
);
986 pr
->rmt_sockctrl
= sockctrl
;
987 pr
->rmt_clientside
= 1;
989 /* This code is duplicated from the end of this function */
990 fp
->read_op
= pcap_read_rpcap
;
991 fp
->save_current_filter_op
= pcap_save_current_filter_rpcap
;
992 fp
->setfilter_op
= pcap_setfilter_rpcap
;
993 fp
->getnonblock_op
= NULL
; /* This is not implemented in remote capture */
994 fp
->setnonblock_op
= NULL
; /* This is not implemented in remote capture */
995 fp
->stats_op
= pcap_stats_rpcap
;
997 fp
->stats_ex_op
= pcap_stats_ex_rpcap
;
999 fp
->cleanup_op
= pcap_cleanup_rpcap
;
1001 /* Checks if all the data has been read; if not, discard the data in excess */
1002 if (totread
!= ntohl(header
.plen
))
1004 if (sock_discard(sockctrl
, ntohl(header
.plen
) - totread
, NULL
, 0) == 1)
1011 * When the connection has been established, we have to close it. So, at the
1012 * beginning of this function, if an error occur we return immediately with
1013 * a return NULL; when the connection is established, we have to come here
1014 * ('goto error;') in order to close everything properly.
1016 * Checks if all the data has been read; if not, discard the data in excess
1018 if (totread
!= ntohl(header
.plen
))
1019 sock_discard(sockctrl
, ntohl(header
.plen
) - totread
, NULL
, 0);
1022 freeaddrinfo(addrinfo
);
1025 sock_close(sockctrl
, NULL
, 0);
1030 /* \ingroup remote_pri_func
1032 * \brief It starts a remote capture.
1034 * This function is required since the RPCAP protocol decouples the 'open' from the
1035 * 'start capture' functions.
1036 * This function takes all the parameters needed (which have been stored into the pcap_t structure)
1037 * and sends them to the server.
1038 * If everything is fine, it creates a new child thread that reads data from the network
1039 * and puts the data into the user buffer.
1040 * The pcap_read() will read data from the user buffer, as usual.
1042 * The remote capture acts like a new "kernel", which puts packets directly into
1043 * the buffer pointed by pcap_t.
1044 * In fact, this function does not rely on a kernel that reads packets and puts them
1045 * into the user buffer; it has to do that on its own.
1047 * \param fp: the pcap_t descriptor of the device currently open.
1049 * \return '0' if everything is fine, '-1' otherwise. The error message (if one)
1050 * is returned into the 'errbuf' field of the pcap_t structure.
1052 int pcap_startcapture_remote(pcap_t
*fp
)
1054 struct pcap_rpcap
*pr
= fp
->priv
; /* structure used when doing a remote live capture */
1055 char sendbuf
[RPCAP_NETBUF_SIZE
]; /* temporary buffer in which data to be sent is buffered */
1056 int sendbufidx
= 0; /* index which keeps the number of bytes currently buffered */
1057 char portdata
[PCAP_BUF_SIZE
]; /* temp variable needed to keep the network port for the data connection */
1058 uint32 totread
= 0; /* number of bytes of the payload read from the socket */
1060 int retval
; /* store the return value of the functions */
1061 int active
= 0; /* '1' if we're in active mode */
1062 struct activehosts
*temp
; /* temp var needed to scan the host list chain, to detect if we're in active mode */
1063 char host
[INET6_ADDRSTRLEN
+ 1]; /* numeric name of the other host */
1065 /* socket-related variables*/
1066 struct addrinfo hints
; /* temp, needed to open a socket connection */
1067 struct addrinfo
*addrinfo
; /* temp, needed to open a socket connection */
1068 SOCKET sockdata
= 0; /* socket descriptor of the data connection */
1069 struct sockaddr_storage saddr
; /* temp, needed to retrieve the network data port chosen on the local machine */
1070 socklen_t saddrlen
; /* temp, needed to retrieve the network data port chosen on the local machine */
1071 int ai_family
; /* temp, keeps the address family used by the control connection */
1073 /* RPCAP-related variables*/
1074 struct rpcap_header header
; /* header of the RPCAP packet */
1075 struct rpcap_startcapreq
*startcapreq
; /* start capture request message */
1076 struct rpcap_startcapreply startcapreply
; /* start capture reply message */
1078 /* Variables related to the buffer setting */
1081 int sockbufsize
= 0;
1084 * Let's check if sampling has been required.
1085 * If so, let's set it first
1087 if (pcap_setsampling_remote(fp
) != 0)
1090 /* detect if we're in active mode */
1094 if (temp
->sockctrl
== pr
->rmt_sockctrl
)
1105 * Gets the complete sockaddr structure used in the ctrl connection
1106 * This is needed to get the address family of the control socket
1107 * Tip: I cannot save the ai_family of the ctrl sock in the pcap_t struct,
1108 * since the ctrl socket can already be open in case of active mode;
1109 * so I would have to call getpeername() anyway
1111 saddrlen
= sizeof(struct sockaddr_storage
);
1112 if (getpeername(pr
->rmt_sockctrl
, (struct sockaddr
*) &saddr
, &saddrlen
) == -1)
1114 sock_geterror("getsockname(): ", fp
->errbuf
, PCAP_ERRBUF_SIZE
);
1117 ai_family
= ((struct sockaddr_storage
*) &saddr
)->ss_family
;
1119 /* Get the numeric address of the remote host we are connected to */
1120 if (getnameinfo((struct sockaddr
*) &saddr
, saddrlen
, host
,
1121 sizeof(host
), NULL
, 0, NI_NUMERICHOST
))
1123 sock_geterror("getnameinfo(): ", fp
->errbuf
, PCAP_ERRBUF_SIZE
);
1128 * Data connection is opened by the server toward the client if:
1129 * - we're using TCP, and the user wants us to be in active mode
1132 if ((active
) || (pr
->rmt_flags
& PCAP_OPENFLAG_DATATX_UDP
))
1135 * We have to create a new socket to receive packets
1136 * We have to do that immediately, since we have to tell the other
1137 * end which network port we picked up
1139 memset(&hints
, 0, sizeof(struct addrinfo
));
1140 /* TEMP addrinfo is NULL in case of active */
1141 hints
.ai_family
= ai_family
; /* Use the same address family of the control socket */
1142 hints
.ai_socktype
= (pr
->rmt_flags
& PCAP_OPENFLAG_DATATX_UDP
) ? SOCK_DGRAM
: SOCK_STREAM
;
1143 hints
.ai_flags
= AI_PASSIVE
; /* Data connection is opened by the server toward the client */
1145 /* Let's the server pick up a free network port for us */
1146 if (sock_initaddress(NULL
, "0", &hints
, &addrinfo
, fp
->errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1149 if ((sockdata
= sock_open(addrinfo
, SOCKOPEN_SERVER
,
1150 1 /* max 1 connection in queue */, fp
->errbuf
, PCAP_ERRBUF_SIZE
)) == INVALID_SOCKET
)
1153 /* addrinfo is no longer used */
1154 freeaddrinfo(addrinfo
);
1157 /* get the complete sockaddr structure used in the data connection */
1158 saddrlen
= sizeof(struct sockaddr_storage
);
1159 if (getsockname(sockdata
, (struct sockaddr
*) &saddr
, &saddrlen
) == -1)
1161 sock_geterror("getsockname(): ", fp
->errbuf
, PCAP_ERRBUF_SIZE
);
1165 /* Get the local port the system picked up */
1166 if (getnameinfo((struct sockaddr
*) &saddr
, saddrlen
, NULL
,
1167 0, portdata
, sizeof(portdata
), NI_NUMERICSERV
))
1169 sock_geterror("getnameinfo(): ", fp
->errbuf
, PCAP_ERRBUF_SIZE
);
1175 * Now it's time to start playing with the RPCAP protocol
1176 * RPCAP start capture command: create the request message
1178 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
,
1179 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, fp
->errbuf
, PCAP_ERRBUF_SIZE
))
1182 rpcap_createhdr((struct rpcap_header
*) sendbuf
, RPCAP_MSG_STARTCAP_REQ
, 0,
1183 sizeof(struct rpcap_startcapreq
) + sizeof(struct rpcap_filter
) + fp
->fcode
.bf_len
* sizeof(struct rpcap_filterbpf_insn
));
1185 /* Fill the structure needed to open an adapter remotely */
1186 startcapreq
= (struct rpcap_startcapreq
*) &sendbuf
[sendbufidx
];
1188 if (sock_bufferize(NULL
, sizeof(struct rpcap_startcapreq
), NULL
,
1189 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, fp
->errbuf
, PCAP_ERRBUF_SIZE
))
1192 memset(startcapreq
, 0, sizeof(struct rpcap_startcapreq
));
1194 /* By default, apply half the timeout on one side, half of the other */
1195 fp
->opt
.timeout
= fp
->opt
.timeout
/ 2;
1196 startcapreq
->read_timeout
= htonl(fp
->opt
.timeout
);
1198 /* portdata on the openreq is meaningful only if we're in active mode */
1199 if ((active
) || (pr
->rmt_flags
& PCAP_OPENFLAG_DATATX_UDP
))
1201 sscanf(portdata
, "%d", (int *)&(startcapreq
->portdata
)); /* cast to avoid a compiler warning */
1202 startcapreq
->portdata
= htons(startcapreq
->portdata
);
1205 startcapreq
->snaplen
= htonl(fp
->snapshot
);
1206 startcapreq
->flags
= 0;
1208 if (pr
->rmt_flags
& PCAP_OPENFLAG_PROMISCUOUS
)
1209 startcapreq
->flags
|= RPCAP_STARTCAPREQ_FLAG_PROMISC
;
1210 if (pr
->rmt_flags
& PCAP_OPENFLAG_DATATX_UDP
)
1211 startcapreq
->flags
|= RPCAP_STARTCAPREQ_FLAG_DGRAM
;
1213 startcapreq
->flags
|= RPCAP_STARTCAPREQ_FLAG_SERVEROPEN
;
1215 startcapreq
->flags
= htons(startcapreq
->flags
);
1217 /* Pack the capture filter */
1218 if (pcap_pack_bpffilter(fp
, &sendbuf
[sendbufidx
], &sendbufidx
, &fp
->fcode
))
1221 if (sock_send(pr
->rmt_sockctrl
, sendbuf
, sendbufidx
, fp
->errbuf
, PCAP_ERRBUF_SIZE
))
1224 /* Receive the RPCAP start capture reply message */
1225 if (sock_recv(pr
->rmt_sockctrl
, (char *)&header
, sizeof(struct rpcap_header
), SOCK_RECEIVEALL_YES
, fp
->errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1228 /* Checks if the message is correct */
1229 retval
= rpcap_checkmsg(fp
->errbuf
, pr
->rmt_sockctrl
, &header
, RPCAP_MSG_STARTCAP_REPLY
, RPCAP_MSG_ERROR
, 0);
1231 if (retval
!= RPCAP_MSG_STARTCAP_REPLY
) /* the message is not the one expected */
1235 case -3: /* Unrecoverable network error */
1236 case -2: /* The other endpoint send a message that is not allowed here */
1237 case -1: /* The other endpoint has a version number that is not compatible with our */
1240 case RPCAP_MSG_ERROR
: /* The other endpoint reported an error */
1241 /* Update totread, since the rpcap_checkmsg() already purged the buffer */
1242 totread
= ntohl(header
.plen
);
1243 /* Do nothing; just exit; the error code is already into the errbuf */
1247 pcap_snprintf(fp
->errbuf
, PCAP_ERRBUF_SIZE
, "Internal error");
1252 nread
= sock_recv(pr
->rmt_sockctrl
, (char *)&startcapreply
,
1253 sizeof(struct rpcap_startcapreply
), SOCK_RECEIVEALL_YES
,
1254 fp
->errbuf
, PCAP_ERRBUF_SIZE
);
1260 * In case of UDP data stream, the connection is always opened by the daemon
1261 * So, this case is already covered by the code above.
1262 * Now, we have still to handle TCP connections, because:
1263 * - if we're in active mode, we have to wait for a remote connection
1264 * - if we're in passive more, we have to start a connection
1266 * We have to do he job in two steps because in case we're opening a TCP connection, we have
1267 * to tell the port we're using to the remote side; in case we're accepting a TCP
1268 * connection, we have to wait this info from the remote side.
1270 if (!(pr
->rmt_flags
& PCAP_OPENFLAG_DATATX_UDP
))
1274 memset(&hints
, 0, sizeof(struct addrinfo
));
1275 hints
.ai_family
= ai_family
; /* Use the same address family of the control socket */
1276 hints
.ai_socktype
= (pr
->rmt_flags
& PCAP_OPENFLAG_DATATX_UDP
) ? SOCK_DGRAM
: SOCK_STREAM
;
1277 pcap_snprintf(portdata
, PCAP_BUF_SIZE
, "%d", ntohs(startcapreply
.portdata
));
1279 /* Let's the server pick up a free network port for us */
1280 if (sock_initaddress(host
, portdata
, &hints
, &addrinfo
, fp
->errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1283 if ((sockdata
= sock_open(addrinfo
, SOCKOPEN_CLIENT
, 0, fp
->errbuf
, PCAP_ERRBUF_SIZE
)) == INVALID_SOCKET
)
1286 /* addrinfo is no longer used */
1287 freeaddrinfo(addrinfo
);
1292 SOCKET socktemp
; /* We need another socket, since we're going to accept() a connection */
1294 /* Connection creation */
1295 saddrlen
= sizeof(struct sockaddr_storage
);
1297 socktemp
= accept(sockdata
, (struct sockaddr
*) &saddr
, &saddrlen
);
1301 sock_geterror("accept(): ", fp
->errbuf
, PCAP_ERRBUF_SIZE
);
1305 /* Now that I accepted the connection, the server socket is no longer needed */
1306 sock_close(sockdata
, fp
->errbuf
, PCAP_ERRBUF_SIZE
);
1307 sockdata
= socktemp
;
1311 /* Let's save the socket of the data connection */
1312 pr
->rmt_sockdata
= sockdata
;
1314 /* Allocates WinPcap/libpcap user buffer, which is a socket buffer in case of a remote capture */
1315 /* It has the same size of the one used on the other side of the connection */
1316 fp
->bufsize
= ntohl(startcapreply
.bufsize
);
1318 /* Let's get the actual size of the socket buffer */
1319 itemp
= sizeof(sockbufsize
);
1321 res
= getsockopt(sockdata
, SOL_SOCKET
, SO_RCVBUF
, (char *)&sockbufsize
, &itemp
);
1324 sock_geterror("pcap_startcapture_remote()", fp
->errbuf
, PCAP_ERRBUF_SIZE
);
1325 SOCK_ASSERT(fp
->errbuf
, 1);
1329 * Warning: on some kernels (e.g. Linux), the size of the user buffer does not take
1330 * into account the pcap_header and such, and it is set equal to the snaplen.
1331 * In my view, this is wrong (the meaning of the bufsize became a bit strange).
1332 * So, here bufsize is the whole size of the user buffer.
1333 * In case the bufsize returned is too small, let's adjust it accordingly.
1335 if (fp
->bufsize
<= (u_int
) fp
->snapshot
)
1336 fp
->bufsize
+= sizeof(struct pcap_pkthdr
);
1338 /* if the current socket buffer is smaller than the desired one */
1339 if ((u_int
) sockbufsize
< fp
->bufsize
)
1341 /* Loop until the buffer size is OK or the original socket buffer size is larger than this one */
1344 res
= setsockopt(sockdata
, SOL_SOCKET
, SO_RCVBUF
, (char *)&(fp
->bufsize
), sizeof(fp
->bufsize
));
1350 * If something goes wrong, half the buffer size (checking that it does not become smaller than
1355 if ((u_int
) sockbufsize
>= fp
->bufsize
)
1357 fp
->bufsize
= sockbufsize
;
1364 * Let's allocate the packet; this is required in order to put the packet somewhere when
1365 * extracting data from the socket
1366 * Since buffering has already been done in the socket buffer, here we need just a buffer,
1367 * whose size is equal to the pcap header plus the snapshot length
1369 fp
->bufsize
= fp
->snapshot
+ sizeof(struct pcap_pkthdr
);
1371 fp
->buffer
= (u_char
*)malloc(fp
->bufsize
);
1372 if (fp
->buffer
== NULL
)
1374 pcap_snprintf(fp
->errbuf
, PCAP_ERRBUF_SIZE
, "malloc: %s", pcap_strerror(errno
));
1378 /* Checks if all the data has been read; if not, discard the data in excess */
1379 if (totread
!= ntohl(header
.plen
))
1381 if (sock_discard(pr
->rmt_sockctrl
, ntohl(header
.plen
) - totread
, NULL
, 0) == 1)
1386 * In case the user does not want to capture RPCAP packets, let's update the filter
1387 * We have to update it here (instead of sending it into the 'StartCapture' message
1388 * because when we generate the 'start capture' we do not know (yet) all the ports
1389 * we're currently using.
1391 if (pr
->rmt_flags
& PCAP_OPENFLAG_NOCAPTURE_RPCAP
)
1393 struct bpf_program fcode
;
1395 if (pcap_createfilter_norpcappkt(fp
, &fcode
) == -1)
1398 /* We cannot use 'pcap_setfilter_rpcap' because formally the capture has not been started yet */
1399 /* (the 'pr->rmt_capstarted' variable will be updated some lines below) */
1400 if (pcap_updatefilter_remote(fp
, &fcode
) == -1)
1403 pcap_freecode(&fcode
);
1406 pr
->rmt_capstarted
= 1;
1411 * When the connection has been established, we have to close it. So, at the
1412 * beginning of this function, if an error occur we return immediately with
1413 * a return NULL; when the connection is established, we have to come here
1414 * ('goto error;') in order to close everything properly.
1416 * Checks if all the data has been read; if not, discard the data in excess
1418 if (totread
!= ntohl(header
.plen
))
1419 sock_discard(pr
->rmt_sockctrl
, ntohl(header
.plen
) - totread
, NULL
, 0);
1421 if ((sockdata
) && (sockdata
!= -1)) /* we can be here because sockdata said 'error' */
1422 sock_close(sockdata
, NULL
, 0);
1425 sock_close(pr
->rmt_sockctrl
, NULL
, 0);
1428 * We do not have to call pcap_close() here, because this function is always called
1429 * by the user in case something bad happens
1443 * \brief Takes a bpf program and sends it to the other host.
1445 * This function can be called in two cases:
1446 * - the pcap_startcapture() is called (we have to send the filter along with
1447 * the 'start capture' command)
1448 * - we want to udpate the filter during a capture (i.e. the pcap_setfilter()
1449 * is called when the capture is still on)
1451 * This function serializes the filter into the sending buffer ('sendbuf', passed
1452 * as a parameter) and return back. It does not send anything on the network.
1454 * \param fp: the pcap_t descriptor of the device currently opened.
1456 * \param sendbuf: the buffer on which the serialized data has to copied.
1458 * \param sendbufidx: it is used to return the abounf of bytes copied into the buffer.
1460 * \param prog: the bpf program we have to copy.
1462 * \return '0' if everything is fine, '-1' otherwise. The error message (if one)
1463 * is returned into the 'errbuf' field of the pcap_t structure.
1465 static int pcap_pack_bpffilter(pcap_t
*fp
, char *sendbuf
, int *sendbufidx
, struct bpf_program
*prog
)
1467 struct rpcap_filter
*filter
;
1468 struct rpcap_filterbpf_insn
*insn
;
1469 struct bpf_insn
*bf_insn
;
1470 struct bpf_program fake_prog
; /* To be used just in case the user forgot to set a filter */
1473 if (prog
->bf_len
== 0) /* No filters have been specified; so, let's apply a "fake" filter */
1475 if (pcap_compile(fp
, &fake_prog
, NULL
/* buffer */, 1, 0) == -1)
1481 filter
= (struct rpcap_filter
*) sendbuf
;
1483 if (sock_bufferize(NULL
, sizeof(struct rpcap_filter
), NULL
, sendbufidx
,
1484 RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, fp
->errbuf
, PCAP_ERRBUF_SIZE
))
1487 filter
->filtertype
= htons(RPCAP_UPDATEFILTER_BPF
);
1488 filter
->nitems
= htonl((int32
)prog
->bf_len
);
1490 if (sock_bufferize(NULL
, prog
->bf_len
* sizeof(struct rpcap_filterbpf_insn
),
1491 NULL
, sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, fp
->errbuf
, PCAP_ERRBUF_SIZE
))
1494 insn
= (struct rpcap_filterbpf_insn
*) (filter
+ 1);
1495 bf_insn
= prog
->bf_insns
;
1497 for (i
= 0; i
< prog
->bf_len
; i
++)
1499 insn
->code
= htons(bf_insn
->code
);
1500 insn
->jf
= bf_insn
->jf
;
1501 insn
->jt
= bf_insn
->jt
;
1502 insn
->k
= htonl(bf_insn
->k
);
1511 /* \ingroup remote_pri_func
1513 * \brief Update a filter on a remote host.
1515 * This function is called when the user wants to update a filter.
1516 * In case we're capturing from the network, it sends the filter to the other peer.
1517 * This function is *not* called automatically when the user calls the pcap_setfilter().
1518 * There will be two cases:
1519 * - the capture is already on: in this case, pcap_setfilter() calls pcap_updatefilter_remote()
1520 * - the capture has not started yet: in this case, pcap_setfilter() stores the filter into
1521 * the pcap_t structure, and then the filter is sent with the pcap_startcap().
1523 * Parameters and return values are exactly the same of the pcap_setfilter().
1525 * \warning This function *does not* clear the packet currently into the buffers. Therefore,
1526 * the user has to expect to receive some packets that are related to the previous filter.
1527 * If you want to discard all the packets before applying a new filter, you have to close
1528 * the current capture session and start a new one.
1530 static int pcap_updatefilter_remote(pcap_t
*fp
, struct bpf_program
*prog
)
1532 struct pcap_rpcap
*pr
= fp
->priv
; /* structure used when doing a remote live capture */
1533 int retval
; /* general variable used to keep the return value of other functions */
1534 char sendbuf
[RPCAP_NETBUF_SIZE
]; /* temporary buffer in which data to be sent is buffered */
1535 int sendbufidx
= 0; /* index which keeps the number of bytes currently buffered */
1536 struct rpcap_header header
; /* To keep the reply message */
1538 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
, &sendbufidx
,
1539 RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, fp
->errbuf
, PCAP_ERRBUF_SIZE
))
1542 rpcap_createhdr((struct rpcap_header
*) sendbuf
, RPCAP_MSG_UPDATEFILTER_REQ
, 0,
1543 sizeof(struct rpcap_filter
) + prog
->bf_len
* sizeof(struct rpcap_filterbpf_insn
));
1545 if (pcap_pack_bpffilter(fp
, &sendbuf
[sendbufidx
], &sendbufidx
, prog
))
1548 if (sock_send(pr
->rmt_sockctrl
, sendbuf
, sendbufidx
, fp
->errbuf
, PCAP_ERRBUF_SIZE
))
1551 /* Waits for the answer */
1552 if (sock_recv(pr
->rmt_sockctrl
, (char *)&header
, sizeof(struct rpcap_header
), SOCK_RECEIVEALL_YES
, fp
->errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1555 /* Checks if the message is correct */
1556 retval
= rpcap_checkmsg(fp
->errbuf
, pr
->rmt_sockctrl
, &header
, RPCAP_MSG_UPDATEFILTER_REPLY
, 0);
1558 if (retval
!= RPCAP_MSG_UPDATEFILTER_REPLY
) /* the message is not the one expected */
1562 case -3: /* Unrecoverable network error */
1563 case -2: /* The other endpoint sent a message that is not allowed here */
1564 case -1: /* The other endpoint has a version number that is not compatible with our */
1565 /* Do nothing; just exit from here; the error code is already into the errbuf */
1569 SOCK_ASSERT("Internal error", 0);
1574 if (ntohl(header
.plen
) != 0) /* the message has an unexpected size */
1576 if (sock_discard(pr
->rmt_sockctrl
, ntohl(header
.plen
), fp
->errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1584 pcap_save_current_filter_rpcap(pcap_t
*fp
, const char *filter
)
1586 struct pcap_rpcap
*pr
= fp
->priv
; /* structure used when doing a remote live capture */
1590 * - We are on an remote capture
1591 * - we do not want to capture RPCAP traffic
1593 * If so, we have to save the current filter, because we have to
1594 * add some piece of stuff later
1596 if (pr
->rmt_clientside
&&
1597 (pr
->rmt_flags
& PCAP_OPENFLAG_NOCAPTURE_RPCAP
))
1599 if (pr
->currentfilter
)
1600 free(pr
->currentfilter
);
1605 pr
->currentfilter
= strdup(filter
);
1610 * \ingroup remote_pri_func
1612 * \brief Send a filter to a remote host.
1614 * This function is called when the user wants to set a filter.
1615 * In case we're capturing from the network, it sends the filter to the other peer.
1616 * This function is called automatically when the user calls the pcap_setfilter().
1618 * Parameters and return values are exactly the same of the pcap_setfilter().
1620 static int pcap_setfilter_rpcap(pcap_t
*fp
, struct bpf_program
*prog
)
1622 struct pcap_rpcap
*pr
= fp
->priv
; /* structure used when doing a remote live capture */
1624 if (!pr
->rmt_capstarted
)
1626 /* copy filter into the pcap_t structure */
1627 if (install_bpf_program(fp
, prog
) == -1)
1632 /* we have to update a filter during run-time */
1633 if (pcap_updatefilter_remote(fp
, prog
))
1640 * \ingroup remote_pri_func
1642 * \brief Update the current filter in order not to capture rpcap packets.
1644 * This function is called *only* when the user wants exclude RPCAP packets
1645 * related to the current session from the captured packets.
1647 * \return '0' if everything is fine, '-1' otherwise. The error message (if one)
1648 * is returned into the 'errbuf' field of the pcap_t structure.
1650 static int pcap_createfilter_norpcappkt(pcap_t
*fp
, struct bpf_program
*prog
)
1652 struct pcap_rpcap
*pr
= fp
->priv
; /* structure used when doing a remote live capture */
1655 /* We do not want to capture our RPCAP traffic. So, let's update the filter */
1656 if (pr
->rmt_flags
& PCAP_OPENFLAG_NOCAPTURE_RPCAP
)
1658 struct sockaddr_storage saddr
; /* temp, needed to retrieve the network data port chosen on the local machine */
1659 socklen_t saddrlen
; /* temp, needed to retrieve the network data port chosen on the local machine */
1660 char myaddress
[128];
1661 char myctrlport
[128];
1662 char mydataport
[128];
1663 char peeraddress
[128];
1664 char peerctrlport
[128];
1666 const int newstringsize
= 1024;
1667 size_t currentfiltersize
;
1669 /* Get the name/port of the other peer */
1670 saddrlen
= sizeof(struct sockaddr_storage
);
1671 if (getpeername(pr
->rmt_sockctrl
, (struct sockaddr
*) &saddr
, &saddrlen
) == -1)
1673 sock_geterror("getpeername(): ", fp
->errbuf
, PCAP_ERRBUF_SIZE
);
1677 if (getnameinfo((struct sockaddr
*) &saddr
, saddrlen
, peeraddress
,
1678 sizeof(peeraddress
), peerctrlport
, sizeof(peerctrlport
), NI_NUMERICHOST
| NI_NUMERICSERV
))
1680 sock_geterror("getnameinfo(): ", fp
->errbuf
, PCAP_ERRBUF_SIZE
);
1684 /* We cannot check the data port, because this is available only in case of TCP sockets */
1685 /* Get the name/port of the current host */
1686 if (getsockname(pr
->rmt_sockctrl
, (struct sockaddr
*) &saddr
, &saddrlen
) == -1)
1688 sock_geterror("getsockname(): ", fp
->errbuf
, PCAP_ERRBUF_SIZE
);
1692 /* Get the local port the system picked up */
1693 if (getnameinfo((struct sockaddr
*) &saddr
, saddrlen
, myaddress
,
1694 sizeof(myaddress
), myctrlport
, sizeof(myctrlport
), NI_NUMERICHOST
| NI_NUMERICSERV
))
1696 sock_geterror("getnameinfo(): ", fp
->errbuf
, PCAP_ERRBUF_SIZE
);
1700 /* Let's now check the data port */
1701 if (getsockname(pr
->rmt_sockdata
, (struct sockaddr
*) &saddr
, &saddrlen
) == -1)
1703 sock_geterror("getsockname(): ", fp
->errbuf
, PCAP_ERRBUF_SIZE
);
1707 /* Get the local port the system picked up */
1708 if (getnameinfo((struct sockaddr
*) &saddr
, saddrlen
, NULL
, 0, mydataport
, sizeof(mydataport
), NI_NUMERICSERV
))
1710 sock_geterror("getnameinfo(): ", fp
->errbuf
, PCAP_ERRBUF_SIZE
);
1714 currentfiltersize
= pr
->currentfilter
? strlen(pr
->currentfilter
) : 0;
1716 newfilter
= (char *)malloc(currentfiltersize
+ newstringsize
+ 1);
1718 if (currentfiltersize
)
1720 pcap_snprintf(newfilter
, currentfiltersize
+ newstringsize
,
1721 "(%s) and not (host %s and host %s and port %s and port %s) and not (host %s and host %s and port %s)",
1722 pr
->currentfilter
, myaddress
, peeraddress
, myctrlport
, peerctrlport
, myaddress
, peeraddress
, mydataport
);
1726 pcap_snprintf(newfilter
, currentfiltersize
+ newstringsize
,
1727 "not (host %s and host %s and port %s and port %s) and not (host %s and host %s and port %s)",
1728 myaddress
, peeraddress
, myctrlport
, peerctrlport
, myaddress
, peeraddress
, mydataport
);
1731 newfilter
[currentfiltersize
+ newstringsize
] = 0;
1734 * This is only an hack to prevent the save_current_filter
1735 * routine, which will be called when we call pcap_compile(),
1736 * from saving the modified filter.
1738 pr
->rmt_clientside
= 0;
1740 if (pcap_compile(fp
, prog
, newfilter
, 1, 0) == -1)
1743 /* Undo the hack. */
1744 pr
->rmt_clientside
= 1;
1753 * \ingroup remote_pri_func
1755 * \brief Set sampling parameters in the remote host.
1757 * This function is called when the user wants to set activate sampling on the remote host.
1759 * Sampling parameters are defined into the 'pcap_t' structure.
1761 * \param p: the pcap_t descriptor of the device currently opened.
1763 * \return '0' if everything is OK, '-1' is something goes wrong. The error message is returned
1764 * in the 'errbuf' member of the pcap_t structure.
1766 static int pcap_setsampling_remote(pcap_t
*p
)
1768 struct pcap_rpcap
*pr
= p
->priv
; /* structure used when doing a remote live capture */
1769 int retval
; /* general variable used to keep the return value of other functions */
1770 char sendbuf
[RPCAP_NETBUF_SIZE
];/* temporary buffer in which data to be sent is buffered */
1771 int sendbufidx
= 0; /* index which keeps the number of bytes currently buffered */
1772 struct rpcap_header header
; /* To keep the reply message */
1773 struct rpcap_sampling
*sampling_pars
; /* Structure that is needed to send sampling parameters to the remote host */
1775 /* If no samping is requested, return 'ok' */
1776 if (p
->rmt_samp
.method
== PCAP_SAMP_NOSAMP
)
1779 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
,
1780 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, p
->errbuf
, PCAP_ERRBUF_SIZE
))
1783 rpcap_createhdr((struct rpcap_header
*) sendbuf
, RPCAP_MSG_SETSAMPLING_REQ
, 0, sizeof(struct rpcap_sampling
));
1785 /* Fill the structure needed to open an adapter remotely */
1786 sampling_pars
= (struct rpcap_sampling
*) &sendbuf
[sendbufidx
];
1788 if (sock_bufferize(NULL
, sizeof(struct rpcap_sampling
), NULL
,
1789 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, p
->errbuf
, PCAP_ERRBUF_SIZE
))
1792 memset(sampling_pars
, 0, sizeof(struct rpcap_sampling
));
1794 sampling_pars
->method
= p
->rmt_samp
.method
;
1795 sampling_pars
->value
= htonl(p
->rmt_samp
.value
);
1797 if (sock_send(pr
->rmt_sockctrl
, sendbuf
, sendbufidx
, p
->errbuf
, PCAP_ERRBUF_SIZE
))
1800 /* Waits for the answer */
1801 if (sock_recv(pr
->rmt_sockctrl
, (char *)&header
, sizeof(struct rpcap_header
), SOCK_RECEIVEALL_YES
, p
->errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1804 /* Checks if the message is correct */
1805 retval
= rpcap_checkmsg(p
->errbuf
, pr
->rmt_sockctrl
, &header
, RPCAP_MSG_SETSAMPLING_REPLY
, 0);
1807 if (retval
!= RPCAP_MSG_SETSAMPLING_REPLY
) /* the message is not the one expected */
1811 case -3: /* Unrecoverable network error */
1812 case -2: /* The other endpoint sent a message that is not allowed here */
1813 case -1: /* The other endpoint has a version number that is not compatible with our */
1814 case RPCAP_MSG_ERROR
:
1815 /* Do nothing; just exit from here; the error code is already into the errbuf */
1819 SOCK_ASSERT("Internal error", 0);
1824 if (ntohl(header
.plen
) != 0) /* the message has an unexpected size */
1826 if (sock_discard(pr
->rmt_sockctrl
, ntohl(header
.plen
), p
->errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1834 /*********************************************************
1836 * Miscellaneous functions *
1838 *********************************************************/
1840 /* \ingroup remote_pri_func
1841 * \brief It sends a RPCAP error to the other peer.
1843 * This function has to be called when the main program detects an error. This function
1844 * will send on the other peer the 'buffer' specified by the user.
1845 * This function *does not* request a RPCAP CLOSE connection. A CLOSE command must be sent
1846 * explicitly by the program, since we do not know it the error can be recovered in some
1847 * way or it is a non-recoverable one.
1849 * \param sock: the socket we are currently using.
1851 * \param error: an user-allocated (and '0' terminated) buffer that contains the error
1852 * description that has to be transmitted on the other peer. The error message cannot
1853 * be longer than PCAP_ERRBUF_SIZE.
1855 * \param errcode: a integer which tells the other party the type of error we had;
1856 * currently is is not too much used.
1858 * \param errbuf: a pointer to a user-allocated buffer (of size PCAP_ERRBUF_SIZE)
1859 * that will contain the error message (in case there is one). It could be network problem.
1861 * \return '0' if everything is fine, '-1' if some errors occurred. The error message is returned
1862 * in the 'errbuf' variable.
1864 int rpcap_senderror(SOCKET sock
, char *error
, unsigned short errcode
, char *errbuf
)
1866 char sendbuf
[RPCAP_NETBUF_SIZE
]; /* temporary buffer in which data to be sent is buffered */
1867 int sendbufidx
= 0; /* index which keeps the number of bytes currently buffered */
1870 length
= (uint16
)strlen(error
);
1872 if (length
> PCAP_ERRBUF_SIZE
)
1873 length
= PCAP_ERRBUF_SIZE
;
1875 rpcap_createhdr((struct rpcap_header
*) sendbuf
, RPCAP_MSG_ERROR
, errcode
, length
);
1877 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
, &sendbufidx
,
1878 RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errbuf
, PCAP_ERRBUF_SIZE
))
1881 if (sock_bufferize(error
, length
, sendbuf
, &sendbufidx
,
1882 RPCAP_NETBUF_SIZE
, SOCKBUF_BUFFERIZE
, errbuf
, PCAP_ERRBUF_SIZE
))
1885 if (sock_send(sock
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
))
1891 /* \ingroup remote_pri_func
1892 * \brief Sends the authentication message.
1894 * It sends the authentication parameters on the control socket.
1895 * This function is required in order to open the connection with the other end party.
1897 * \param sock: the socket we are currently using.
1899 * \param auth: authentication parameters that have to be sent.
1901 * \param errbuf: a pointer to a user-allocated buffer (of size PCAP_ERRBUF_SIZE)
1902 * that will contain the error message (in case there is one). It could be network problem
1903 * of the fact that the authorization failed.
1905 * \return '0' if everything is fine, '-1' if some errors occurred. The error message is returned
1906 * in the 'errbuf' variable.
1907 * The error message could be also 'the authentication failed'.
1909 int rpcap_sendauth(SOCKET sock
, struct pcap_rmtauth
*auth
, char *errbuf
)
1911 char sendbuf
[RPCAP_NETBUF_SIZE
]; /* temporary buffer in which data that has to be sent is buffered */
1912 int sendbufidx
= 0; /* index which keeps the number of bytes currently buffered */
1913 uint16 length
; /* length of the payload of this message */
1914 struct rpcap_auth
*rpauth
;
1916 struct rpcap_header header
;
1917 int retval
; /* temp variable which stores functions return value */
1922 auth_type
= auth
->type
;
1926 case RPCAP_RMTAUTH_NULL
:
1927 length
= sizeof(struct rpcap_auth
);
1930 case RPCAP_RMTAUTH_PWD
:
1931 length
= sizeof(struct rpcap_auth
);
1934 str_length
= strlen(auth
->username
);
1935 if (str_length
> 65535)
1937 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "User name is too long (> 65535 bytes)");
1940 length
+= (uint16
)str_length
;
1944 str_length
= strlen(auth
->password
);
1945 if (str_length
> 65535)
1947 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Password is too long (> 65535 bytes)");
1950 length
+= (uint16
)str_length
;
1955 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Authentication type not recognized.");
1961 auth_type
= RPCAP_RMTAUTH_NULL
;
1962 length
= sizeof(struct rpcap_auth
);
1966 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
,
1967 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errbuf
, PCAP_ERRBUF_SIZE
))
1970 rpcap_createhdr((struct rpcap_header
*) sendbuf
, RPCAP_MSG_AUTH_REQ
, 0, length
);
1972 rpauth
= (struct rpcap_auth
*) &sendbuf
[sendbufidx
];
1974 if (sock_bufferize(NULL
, sizeof(struct rpcap_auth
), NULL
,
1975 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errbuf
, PCAP_ERRBUF_SIZE
))
1978 memset(rpauth
, 0, sizeof(struct rpcap_auth
));
1980 rpauth
->type
= htons(auth_type
);
1982 if (auth_type
== RPCAP_RMTAUTH_PWD
)
1985 rpauth
->slen1
= (uint16
)strlen(auth
->username
);
1989 if (sock_bufferize(auth
->username
, rpauth
->slen1
, sendbuf
,
1990 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_BUFFERIZE
, errbuf
, PCAP_ERRBUF_SIZE
))
1994 rpauth
->slen2
= (uint16
)strlen(auth
->password
);
1998 if (sock_bufferize(auth
->password
, rpauth
->slen2
, sendbuf
,
1999 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_BUFFERIZE
, errbuf
, PCAP_ERRBUF_SIZE
))
2002 rpauth
->slen1
= htons(rpauth
->slen1
);
2003 rpauth
->slen2
= htons(rpauth
->slen2
);
2006 if (sock_send(sock
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
))
2009 if (sock_recv(sock
, (char *)&header
, sizeof(struct rpcap_header
), SOCK_RECEIVEALL_YES
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2012 retval
= rpcap_checkmsg(errbuf
, sock
, &header
, RPCAP_MSG_AUTH_REPLY
, RPCAP_MSG_ERROR
, 0);
2014 if (retval
!= RPCAP_MSG_AUTH_REPLY
) /* the message is not the one expected */
2018 case -3: /* Unrecoverable network error */
2019 case -2: /* The other endpoint sent a message that is not allowed here */
2020 case -1: /* The other endpoint has a version number that is not compatible with our */
2021 /* Do nothing; just exit from here; the error code is already into the errbuf */
2024 case RPCAP_MSG_ERROR
:
2028 SOCK_ASSERT("Internal error", 0);
2033 if (ntohl(header
.plen
))
2035 if (sock_discard(sock
, ntohl(header
.plen
), errbuf
, PCAP_ERRBUF_SIZE
))
2042 /* \ingroup remote_pri_func
2043 * \brief Creates a structure of type rpcap_header.
2045 * This function is provided just because the creation of an rpcap header is quite a common
2046 * task. It accepts all the values that appears into an rpcap_header, and it puts them in
2047 * place using the proper hton() calls.
2049 * \param header: a pointer to a user-allocated buffer which will contain the serialized
2050 * header, ready to be sent on the network.
2052 * \param type: a value (in the host by order) which will be placed into the header.type
2053 * field and that represents the type of the current message.
2055 * \param value: a value (in the host by order) which will be placed into the header.value
2056 * field and that has a message-dependent meaning.
2058 * \param length: a value (in the host by order) which will be placed into the header.length
2059 * field and that represents the payload length of the message.
2061 * \return Nothing. The serialized header is returned into the 'header' variable.
2063 void rpcap_createhdr(struct rpcap_header
*header
, uint8 type
, uint16 value
, uint32 length
)
2065 memset(header
, 0, sizeof(struct rpcap_header
));
2067 header
->ver
= RPCAP_VERSION
;
2068 header
->type
= type
;
2069 header
->value
= htons(value
);
2070 header
->plen
= htonl(length
);
2073 /* ingroup remote_pri_func
2074 * \brief Checks if the header of the received message is correct.
2076 * This function is a way to easily check if the message received, in a certain
2077 * state of the RPCAP protocol Finite State Machine, is valid. This function accepts,
2078 * as a parameter, the list of message types that are allowed in a certain situation,
2079 * and it returns the one which occurs.
2081 * \param errbuf: a pointer to a user-allocated buffer (of size PCAP_ERRBUF_SIZE)
2082 * that will contain the error message (in case there is one). It could be either problem
2083 * occurred inside this function (e.g. a network problem in case it tries to send an
2084 * error on the other peer and the send() call fails), an error message which has been
2085 * sent to us from the other party, or a version error (the message receive has a version
2086 * number that is incompatible with our).
2088 * \param sock: the socket that has to be used to receive data. This function can
2089 * read data from socket in case the version contained into the message is not compatible
2090 * with our. In that case, all the message is purged from the socket, so that the following
2091 * recv() calls will return a new message.
2093 * \param header: a pointer to and 'rpcap_header' structure that keeps the data received from
2094 * the network (still in network byte order) and that has to be checked.
2096 * \param first: this function has a variable number of parameters. From this point on,
2097 * all the messages that are valid in this context must be passed as parameters.
2098 * The message type list must be terminated with a '0' value, the null message type,
2099 * which means 'no more types to check'. The RPCAP protocol does not define anything with
2100 * message type equal to zero, so there is no ambiguity in using this value as a list terminator.
2102 * \return The message type of the message that has been detected. In case of errors (e.g. the
2103 * header contains a type that is not listed among the allowed types), this function will
2104 * return the following codes:
2105 * - (-1) if the version is incompatible.
2106 * - (-2) if the code is not among the one listed into the parameters list
2107 * - (-3) if a network error (connection reset, ...)
2108 * - RPCAP_MSG_ERROR if the message is an error message (it follow that the RPCAP_MSG_ERROR
2109 * could not be present in the allowed message-types list, because this function checks
2110 * for errors anyway)
2112 * In case either the version is incompatible or nothing matches (i.e. it returns '-1' or '-2'),
2113 * it discards the message body (i.e. it reads the remaining part of the message from the
2114 * network and it discards it) so that the application is ready to receive a new message.
2116 int rpcap_checkmsg(char *errbuf
, SOCKET sock
, struct rpcap_header
*header
, uint8 first
, ...)
2122 va_start(ap
, first
);
2124 /* Check if the present version of the protocol can handle this message */
2125 if (rpcap_checkver(sock
, header
, errbuf
))
2127 SOCK_ASSERT(errbuf
, 1);
2138 * The message matches with one of the types listed
2139 * There is no need of conversions since both values are uint8
2141 * Check if the other side reported an error.
2142 * If yes, it retrieves it and it returns it back to the caller
2144 if (header
->type
== RPCAP_MSG_ERROR
)
2146 len
= ntohl(header
->plen
);
2148 if (len
>= PCAP_ERRBUF_SIZE
)
2150 if (sock_recv(sock
, errbuf
, PCAP_ERRBUF_SIZE
- 1, SOCK_RECEIVEALL_YES
, errbuf
, PCAP_ERRBUF_SIZE
))
2153 sock_discard(sock
, len
- (PCAP_ERRBUF_SIZE
- 1), NULL
, 0);
2155 /* Put '\0' at the end of the string */
2156 errbuf
[PCAP_ERRBUF_SIZE
- 1] = 0;
2160 if (sock_recv(sock
, errbuf
, len
, SOCK_RECEIVEALL_YES
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2163 /* Put '\0' at the end of the string */
2168 return header
->type
;
2171 if (header
->type
== type
)
2174 return header
->type
;
2177 /* get next argument */
2178 type
= va_arg(ap
, int);
2181 /* we already have an error, so please discard this one */
2182 sock_discard(sock
, ntohl(header
->plen
), NULL
, 0);
2184 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "The other endpoint sent a message that is not allowed here.");
2185 SOCK_ASSERT(errbuf
, 1);
2191 /* \ingroup remote_pri_func
2192 * \brief Checks if the version contained into the message is compatible with
2193 * the one handled by this implementation.
2195 * Right now, this function does not have any sophisticated task: if the versions
2196 * are different, it returns -1 and it discards the message.
2197 * It is expected that in the future this message will become more complex.
2199 * \param sock: the socket that has to be used to receive data. This function can
2200 * read data from socket in case the version contained into the message is not compatible
2201 * with our. In that case, all the message is purged from the socket, so that the following
2202 * recv() calls will return a new (clean) message.
2204 * \param header: a pointer to and 'rpcap_header' structure that keeps the data received from
2205 * the network (still in network byte order) and that has to be checked.
2207 * \param errbuf: a pointer to a user-allocated buffer (of size PCAP_ERRBUF_SIZE)
2208 * that will contain the error message (in case there is one). The error message is
2209 * "incompatible version".
2211 * \return '0' if everything is fine, '-1' if some errors occurred. The error message is returned
2212 * in the 'errbuf' variable.
2214 static int rpcap_checkver(SOCKET sock
, struct rpcap_header
*header
, char *errbuf
)
2217 * This is a sample function.
2219 * In the real world, you have to check the type code,
2220 * and decide accordingly.
2222 if (header
->ver
!= RPCAP_VERSION
)
2224 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Incompatible version number: message discarded.");
2226 /* we already have an error, so please discard this one */
2227 sock_discard(sock
, ntohl(header
->plen
), NULL
, 0);
2234 /* \ingroup remote_pri_func
2236 * \brief It returns the socket currently used for this active connection
2237 * (active mode only) and provides an indication of whether this connection
2238 * is in active mode or not.
2240 * This function is just for internal use; it returns the socket ID of the
2241 * active connection currently opened.
2243 * \param host: a string that keeps the host name of the host for which we
2244 * want to get the socket ID for that active connection.
2246 * \param isactive: a pointer to an int that is set to 1 if there's an
2247 * active connection to that host and 0 otherwise.
2249 * \param errbuf: a pointer to a user-allocated buffer (of size
2250 * PCAP_ERRBUF_SIZE) that will contain the error message (in case
2253 * \return the socket identifier if everything is fine, '0' if this host
2254 * is not in the active host list. An indication of whether this host
2255 * is in the active host list is returned into the isactive variable.
2256 * It returns 'INVALID_SOCKET' in case of error. The error message is
2257 * returned into the errbuf variable.
2259 SOCKET
rpcap_remoteact_getsock(const char *host
, int *isactive
, char *errbuf
)
2261 struct activehosts
*temp
; /* temp var needed to scan the host list chain */
2262 struct addrinfo hints
, *addrinfo
, *ai_next
; /* temp var needed to translate between hostname to its address */
2265 /* retrieve the network address corresponding to 'host' */
2267 memset(&hints
, 0, sizeof(struct addrinfo
));
2268 hints
.ai_family
= PF_UNSPEC
;
2269 hints
.ai_socktype
= SOCK_STREAM
;
2271 retval
= getaddrinfo(host
, "0", &hints
, &addrinfo
);
2274 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "getaddrinfo() %s", gai_strerror(retval
));
2276 return INVALID_SOCKET
;
2286 if (sock_cmpaddr(&temp
->host
, (struct sockaddr_storage
*) ai_next
->ai_addr
) == 0)
2289 return (temp
->sockctrl
);
2292 ai_next
= ai_next
->ai_next
;
2298 freeaddrinfo(addrinfo
);
2301 * The host for which you want to get the socket ID does not have an
2302 * active connection.
2308 pcap_t
*pcap_open_rpcap(const char *source
, int snaplen
, int flags
, int read_timeout
, struct pcap_rmtauth
*auth
, char *errbuf
)
2312 struct pcap_rpcap
*pr
; /* structure used when doing a remote live capture */
2315 fp
= pcap_create_common(errbuf
, sizeof (struct pcap_rpcap
));
2320 source_str
= strdup(source
);
2321 if (source_str
== NULL
) {
2322 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
2323 "malloc: %s", pcap_strerror(errno
));
2326 fp
->opt
.device
= source_str
;
2327 fp
->snapshot
= snaplen
;
2328 fp
->opt
.timeout
= read_timeout
;
2330 pr
->rmt_flags
= flags
;
2332 result
= pcap_opensource_remote(fp
, auth
);
2343 /* String identifier to be used in the pcap_findalldevs_ex() */
2344 #define PCAP_TEXT_SOURCE_ADAPTER "Network adapter"
2345 /* String identifier to be used in the pcap_findalldevs_ex() */
2346 #define PCAP_TEXT_SOURCE_ON_REMOTE_HOST "on remote node"
2349 freeaddr(struct pcap_addr
*addr
)
2352 free(addr
->netmask
);
2353 free(addr
->broadaddr
);
2354 free(addr
->dstaddr
);
2359 pcap_findalldevs_ex_remote(char *source
, struct pcap_rmtauth
*auth
, pcap_if_t
**alldevs
, char *errbuf
)
2361 SOCKET sockctrl
; /* socket descriptor of the control connection */
2362 uint32 totread
= 0; /* number of bytes of the payload read from the socket */
2364 struct addrinfo hints
; /* temp variable needed to resolve hostnames into to socket representation */
2365 struct addrinfo
*addrinfo
; /* temp variable needed to resolve hostnames into to socket representation */
2366 struct rpcap_header header
; /* structure that keeps the general header of the rpcap protocol */
2367 int i
, j
; /* temp variables */
2368 int retval
; /* store the return value of the functions */
2369 int nif
; /* Number of interfaces listed */
2370 int active
= 0; /* 'true' if we the other end-party is in active mode */
2372 char host
[PCAP_BUF_SIZE
], port
[PCAP_BUF_SIZE
];
2373 char tmpstring
[PCAP_BUF_SIZE
+ 1]; /* Needed to convert names and descriptions from 'old' syntax to the 'new' one */
2374 pcap_if_t
*dev
; /* Previous device into the pcap_if_t chain */
2376 /* Retrieve the needed data for getting adapter list */
2377 if (pcap_parsesrcstr(source
, &type
, host
, port
, NULL
, errbuf
) == -1)
2380 /* Warning: this call can be the first one called by the user. */
2381 /* For this reason, we have to initialize the WinSock support. */
2382 if (sock_init(errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2385 /* Check for active mode */
2386 sockctrl
= rpcap_remoteact_getsock(host
, &active
, errbuf
);
2387 if (sockctrl
== INVALID_SOCKET
)
2392 * We're not in active mode; let's try to open a new
2393 * control connection.
2397 memset(&hints
, 0, sizeof(struct addrinfo
));
2398 hints
.ai_family
= PF_UNSPEC
;
2399 hints
.ai_socktype
= SOCK_STREAM
;
2403 /* the user chose not to specify the port */
2404 if (sock_initaddress(host
, RPCAP_DEFAULT_NETPORT
, &hints
, &addrinfo
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2409 if (sock_initaddress(host
, port
, &hints
, &addrinfo
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2413 if ((sockctrl
= sock_open(addrinfo
, SOCKOPEN_CLIENT
, 0, errbuf
, PCAP_ERRBUF_SIZE
)) == -1)
2416 /* addrinfo is no longer used */
2417 freeaddrinfo(addrinfo
);
2420 if (rpcap_sendauth(sockctrl
, auth
, errbuf
) == -1)
2422 sock_close(sockctrl
, NULL
, 0);
2427 /* RPCAP findalldevs command */
2428 rpcap_createhdr(&header
, RPCAP_MSG_FINDALLIF_REQ
, 0, 0);
2430 if (sock_send(sockctrl
, (char *)&header
, sizeof(struct rpcap_header
), errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2433 if (sock_recv(sockctrl
, (char *)&header
, sizeof(struct rpcap_header
), SOCK_RECEIVEALL_YES
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2436 /* Checks if the message is correct */
2437 retval
= rpcap_checkmsg(errbuf
, sockctrl
, &header
, RPCAP_MSG_FINDALLIF_REPLY
, RPCAP_MSG_ERROR
, 0);
2439 if (retval
!= RPCAP_MSG_FINDALLIF_REPLY
) /* the message is not the one expected */
2443 case -3: /* Unrecoverable network error */
2444 case -2: /* The other endpoint send a message that is not allowed here */
2445 case -1: /* The other endpoint has a version number that is not compatible with our */
2448 case RPCAP_MSG_ERROR
: /* The other endpoint reported an error */
2453 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Internal error");
2459 sock_close(sockctrl
, NULL
, 0);
2464 /* read the number of interfaces */
2465 nif
= ntohs(header
.value
);
2467 /* loop until all interfaces have been received */
2468 for (i
= 0; i
< nif
; i
++)
2470 struct rpcap_findalldevs_if findalldevs_if
;
2471 char tmpstring2
[PCAP_BUF_SIZE
+ 1]; /* Needed to convert names and descriptions from 'old' syntax to the 'new' one */
2473 struct pcap_addr
*addr
, *prevaddr
;
2475 tmpstring2
[PCAP_BUF_SIZE
] = 0;
2477 /* receive the findalldevs structure from remote host */
2478 nread
= sock_recv(sockctrl
, (char *)&findalldevs_if
,
2479 sizeof(struct rpcap_findalldevs_if
), SOCK_RECEIVEALL_YES
,
2480 errbuf
, PCAP_ERRBUF_SIZE
);
2485 findalldevs_if
.namelen
= ntohs(findalldevs_if
.namelen
);
2486 findalldevs_if
.desclen
= ntohs(findalldevs_if
.desclen
);
2487 findalldevs_if
.naddr
= ntohs(findalldevs_if
.naddr
);
2489 /* allocate the main structure */
2492 (*alldevs
) = (pcap_if_t
*)malloc(sizeof(pcap_if_t
));
2497 dev
->next
= (pcap_if_t
*)malloc(sizeof(pcap_if_t
));
2501 /* check that the malloc() didn't fail */
2504 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "malloc() failed: %s", pcap_strerror(errno
));
2508 /* Initialize the structure to 'zero' */
2509 memset(dev
, 0, sizeof(pcap_if_t
));
2511 /* allocate mem for name and description */
2512 if (findalldevs_if
.namelen
)
2515 if (findalldevs_if
.namelen
>= sizeof(tmpstring
))
2517 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Interface name too long");
2521 /* Retrieve adapter name */
2522 nread
= sock_recv(sockctrl
, tmpstring
,
2523 findalldevs_if
.namelen
, SOCK_RECEIVEALL_YES
,
2524 errbuf
, PCAP_ERRBUF_SIZE
);
2529 tmpstring
[findalldevs_if
.namelen
] = 0;
2531 /* Create the new device identifier */
2532 if (pcap_createsrcstr(tmpstring2
, PCAP_SRC_IFREMOTE
, host
, port
, tmpstring
, errbuf
) == -1)
2535 stringlen
= strlen(tmpstring2
);
2537 dev
->name
= (char *)malloc(stringlen
+ 1);
2538 if (dev
->name
== NULL
)
2540 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "malloc() failed: %s", pcap_strerror(errno
));
2544 /* Copy the new device name into the correct memory location */
2545 strlcpy(dev
->name
, tmpstring2
, stringlen
+ 1);
2548 if (findalldevs_if
.desclen
)
2550 if (findalldevs_if
.desclen
>= sizeof(tmpstring
))
2552 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Interface description too long");
2556 /* Retrieve adapter description */
2557 nread
= sock_recv(sockctrl
, tmpstring
,
2558 findalldevs_if
.desclen
, SOCK_RECEIVEALL_YES
,
2559 errbuf
, PCAP_ERRBUF_SIZE
);
2564 tmpstring
[findalldevs_if
.desclen
] = 0;
2566 pcap_snprintf(tmpstring2
, sizeof(tmpstring2
) - 1, "%s '%s' %s %s", PCAP_TEXT_SOURCE_ADAPTER
,
2567 tmpstring
, PCAP_TEXT_SOURCE_ON_REMOTE_HOST
, host
);
2569 stringlen
= strlen(tmpstring2
);
2571 dev
->description
= (char *)malloc(stringlen
+ 1);
2573 if (dev
->description
== NULL
)
2575 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "malloc() failed: %s", pcap_strerror(errno
));
2579 /* Copy the new device description into the correct memory location */
2580 strlcpy(dev
->description
, tmpstring2
, stringlen
+ 1);
2583 dev
->flags
= ntohl(findalldevs_if
.flags
);
2586 /* loop until all addresses have been received */
2587 for (j
= 0; j
< findalldevs_if
.naddr
; j
++)
2589 struct rpcap_findalldevs_ifaddr ifaddr
;
2591 /* Retrieve the interface addresses */
2592 nread
= sock_recv(sockctrl
, (char *)&ifaddr
,
2593 sizeof(struct rpcap_findalldevs_ifaddr
),
2594 SOCK_RECEIVEALL_YES
, errbuf
, PCAP_ERRBUF_SIZE
);
2600 * Deserialize all the address components.
2602 addr
= (struct pcap_addr
*) malloc(sizeof(struct pcap_addr
));
2605 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "malloc() failed: %s", pcap_strerror(errno
));
2610 addr
->netmask
= NULL
;
2611 addr
->broadaddr
= NULL
;
2612 addr
->dstaddr
= NULL
;
2614 if (rpcap_deseraddr(&ifaddr
.addr
,
2615 (struct sockaddr_storage
**) &addr
->addr
, errbuf
) == -1)
2620 if (rpcap_deseraddr(&ifaddr
.netmask
,
2621 (struct sockaddr_storage
**) &addr
->netmask
, errbuf
) == -1)
2626 if (rpcap_deseraddr(&ifaddr
.broadaddr
,
2627 (struct sockaddr_storage
**) &addr
->broadaddr
, errbuf
) == -1)
2632 if (rpcap_deseraddr(&ifaddr
.dstaddr
,
2633 (struct sockaddr_storage
**) &addr
->dstaddr
, errbuf
) == -1)
2639 if ((addr
->addr
== NULL
) && (addr
->netmask
== NULL
) &&
2640 (addr
->broadaddr
== NULL
) && (addr
->dstaddr
== NULL
))
2643 * None of the addresses are IPv4 or IPv6
2644 * addresses, so throw this entry away.
2651 * Add this entry to the list.
2653 if (prevaddr
== NULL
)
2655 dev
->addresses
= addr
;
2659 prevaddr
->next
= addr
;
2666 /* Checks if all the data has been read; if not, discard the data in excess */
2667 if (totread
!= ntohl(header
.plen
))
2669 if (sock_discard(sockctrl
, ntohl(header
.plen
) - totread
, errbuf
, PCAP_ERRBUF_SIZE
) == 1)
2673 /* Control connection has to be closed only in case the remote machine is in passive mode */
2676 /* DO not send RPCAP_CLOSE, since we did not open a pcap_t; no need to free resources */
2677 if (sock_close(sockctrl
, errbuf
, PCAP_ERRBUF_SIZE
))
2681 /* To avoid inconsistencies in the number of sock_init() */
2688 * In case there has been an error, I don't want to overwrite it with a new one
2689 * if the following call fails. I want to return always the original error.
2691 * Take care: this connection can already be closed when we try to close it.
2692 * This happens because a previous error in the rpcapd, which requested to
2693 * closed the connection. In that case, we already recognized that into the
2694 * rpspck_isheaderok() and we already acknowledged the closing.
2695 * In that sense, this call is useless here (however it is needed in case
2696 * the client generates the error).
2698 * Checks if all the data has been read; if not, discard the data in excess
2700 if (totread
!= ntohl(header
.plen
))
2702 if (sock_discard(sockctrl
, ntohl(header
.plen
) - totread
, NULL
, 0) == 1)
2706 /* Control connection has to be closed only in case the remote machine is in passive mode */
2708 sock_close(sockctrl
, NULL
, 0);
2710 /* To avoid inconsistencies in the number of sock_init() */