2 * Copyright (c) 2002 - 2003
3 * NetGroup, Politecnico di Torino (Italy)
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 nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <errno.h> // for the errno variable
40 #include <stdlib.h> // for malloc(), free(), ...
41 #include <string.h> // for strlen(), ...
42 #include <limits.h> // for INT_MAX
45 #include <process.h> // for threads
51 #include <sys/types.h> // for select() and such
52 #include <pwd.h> // for password management
56 #include <shadow.h> // for password management
59 #include <pcap.h> // for libpcap/WinPcap calls
62 #include "sockutils.h" // for socket calls
63 #include "portability.h"
64 #include "rpcap-protocol.h"
69 #include <openssl/ssl.h>
74 // Timeout, in seconds, when we're waiting for a client to send us an
75 // authentication request; if they don't send us a request within that
76 // interval, we drop the connection, so we don't stay stuck forever.
78 #define RPCAP_TIMEOUT_INIT 90
81 // Timeout, in seconds, when we're waiting for an authenticated client
82 // to send us a request, if a capture isn't in progress; if they don't
83 // send us a request within that interval, we drop the connection, so
84 // we don't stay stuck forever.
86 #define RPCAP_TIMEOUT_RUNTIME 180
89 // Time, in seconds, that we wait after a failed authentication attempt
90 // before processing the next request; this prevents a client from
91 // rapidly trying different accounts or passwords.
93 #define RPCAP_SUSPEND_WRONGAUTH 1
95 // Parameters for the service loop.
98 SOCKET sockctrl
; //!< SOCKET ID of the control connection
99 SSL
*ssl
; //!< Optional SSL handler for the controlling sockets
100 int isactive
; //!< Not null if the daemon has to run in active mode
101 int nullAuthAllowed
; //!< '1' if we permit NULL authentication, '0' otherwise
105 // Data for a session managed by a thread.
106 // It includes both a Boolean indicating whether we *have* a thread,
107 // and a platform-dependent (UN*X vs. Windows) identifier for the
108 // thread; on Windows, we could use an invalid handle to indicate
109 // that we don't have a thread, but there *is* no portable "no thread"
110 // value for a pthread_t on UN*X.
115 SSL
*ctrl_ssl
, *data_ssl
; // optional SSL handlers for sockctrl and sockdata.
116 uint8 protocol_version
;
118 unsigned int TotCapt
;
127 // Locally defined functions
128 static int daemon_msg_err(SOCKET sockctrl
, SSL
*, uint32 plen
);
129 static int daemon_msg_auth_req(struct daemon_slpars
*pars
, uint32 plen
);
130 static int daemon_AuthUserPwd(char *username
, char *password
, char *errbuf
);
132 static int daemon_msg_findallif_req(uint8 ver
, struct daemon_slpars
*pars
,
135 static int daemon_msg_open_req(uint8 ver
, struct daemon_slpars
*pars
,
136 uint32 plen
, char *source
, size_t sourcelen
);
137 static int daemon_msg_startcap_req(uint8 ver
, struct daemon_slpars
*pars
,
138 uint32 plen
, char *source
, struct session
**sessionp
,
139 struct rpcap_sampling
*samp_param
, int uses_ssl
);
140 static int daemon_msg_endcap_req(uint8 ver
, struct daemon_slpars
*pars
,
141 struct session
*session
);
143 static int daemon_msg_updatefilter_req(uint8 ver
, struct daemon_slpars
*pars
,
144 struct session
*session
, uint32 plen
);
145 static int daemon_unpackapplyfilter(SOCKET sockctrl
, SSL
*, struct session
*session
, uint32
*plenp
, char *errbuf
);
147 static int daemon_msg_stats_req(uint8 ver
, struct daemon_slpars
*pars
,
148 struct session
*session
, uint32 plen
, struct pcap_stat
*stats
,
149 unsigned int svrcapt
);
151 static int daemon_msg_setsampling_req(uint8 ver
, struct daemon_slpars
*pars
,
152 uint32 plen
, struct rpcap_sampling
*samp_param
);
154 static void daemon_seraddr(struct sockaddr_storage
*sockaddrin
, struct rpcap_sockaddr
*sockaddrout
);
156 static unsigned __stdcall
daemon_thrdatamain(void *ptr
);
158 static void *daemon_thrdatamain(void *ptr
);
159 static void noop_handler(int sign
);
162 static int rpcapd_recv_msg_header(SOCKET sock
, SSL
*, struct rpcap_header
*headerp
);
163 static int rpcapd_recv(SOCKET sock
, SSL
*, char *buffer
, size_t toread
, uint32
*plen
, char *errmsgbuf
);
164 static int rpcapd_discard(SOCKET sock
, SSL
*, uint32 len
);
165 static void session_close(struct session
*);
168 // TLS record layer header; used when processing the first message from
169 // the client, in case we aren't doing TLS but they are.
171 struct tls_record_header
{
172 uint8 type
; // ContentType - will be 22, for Handshake
173 uint8 version_major
; // TLS protocol major version
174 uint8 version_injor
; // TLS protocol minor version
175 // This is *not* aligned on a 2-byte boundary; we just
176 // declare it as two bytes. Don't assume any particular
177 // compiler's mechanism for saying "packed"!
178 uint8 length_hi
; // Upper 8 bits of payload length
179 uint8 length_lo
; // Low 8 bits of payload length
182 #define TLS_RECORD_HEADER_LEN 5 // Don't use sizeof in case it's padded
184 #define TLS_RECORD_TYPE_ALERT 21
185 #define TLS_RECORD_TYPE_HANDSHAKE 22
188 // TLS alert message.
192 uint8 alert_description
;
195 #define TLS_ALERT_LEN 2
197 #define TLS_ALERT_LEVEL_FATAL 2
198 #define TLS_ALERT_HANDSHAKE_FAILURE 40
201 daemon_serviceloop(SOCKET sockctrl
, int isactive
, char *passiveClients
,
202 int nullAuthAllowed
, int uses_ssl
)
205 struct tls_record_header tls_header
;
206 struct tls_alert tls_alert
;
207 struct daemon_slpars pars
; // service loop parameters
208 char errbuf
[PCAP_ERRBUF_SIZE
+ 1]; // keeps the error string, prior to be printed
209 char errmsgbuf
[PCAP_ERRBUF_SIZE
+ 1]; // buffer for errors to send to the client
210 int host_port_check_status
;
213 struct rpcap_header header
; // RPCAP message general header
214 uint32 plen
; // payload length from header
215 int authenticated
= 0; // 1 if the client has successfully authenticated
216 char source
[PCAP_BUF_SIZE
+1]; // keeps the string that contains the interface to open
217 int got_source
= 0; // 1 if we've gotten the source from an open request
219 struct sigaction action
;
221 struct session
*session
= NULL
; // struct session main variable
222 const char *msg_type_string
; // string for message type
223 int client_told_us_to_close
= 0; // 1 if the client told us to close the capture
225 // needed to save the values of the statistics
226 struct pcap_stat stats
;
227 unsigned int svrcapt
;
229 struct rpcap_sampling samp_param
; // in case sampling has been requested
231 // Structures needed for the select() call
232 fd_set rfds
; // set of socket descriptors we have to check
233 struct timeval tv
; // maximum time the select() can block waiting for data
234 int retval
; // select() return value
236 *errbuf
= 0; // Initialize errbuf
239 // Peek into the socket to determine whether the client sent us
240 // a TLS handshake message or a non-TLS rpcapd message.
242 // The first byte of an rpcapd request is the version number;
243 // the first byte of a TLS handshake message is 22. The
244 // first request to an rpcapd server must be an authentication
245 // request or a close request, and must have a version number
246 // of 0, so it will be possible to distinguish between an
247 // initial plaintext request to a server and an initial TLS
248 // handshake message.
250 nrecv
= sock_recv(sockctrl
, NULL
, (char *)&first_octet
, 1,
251 SOCK_EOF_ISNT_ERROR
|SOCK_MSG_PEEK
, errbuf
, PCAP_ERRBUF_SIZE
);
255 rpcapd_log(LOGPRIO_ERROR
, "Peek from client failed: %s", errbuf
);
260 // Client closed the connection.
266 // We have to upgrade to TLS as soon as possible, so that the
267 // whole protocol goes through the encrypted tunnel, including
268 // early error messages.
270 // Even in active mode, the other end has to initiate the TLS
271 // handshake as we still are the server as far as TLS is concerned,
272 // so we don't check isactive.
277 // We're expecting a TLS handshake message. If this
278 // isn't one, assume it's a non-TLS rpcapd message.
280 // The first octet of a TLS handshake is
281 // TLS_RECORD_TYPE_HANDSHAKE.
283 if (first_octet
!= TLS_RECORD_TYPE_HANDSHAKE
)
286 // We assume this is a non-TLS rpcapd message.
288 // Read the message header from the client.
290 nrecv
= rpcapd_recv_msg_header(sockctrl
, NULL
, &header
);
298 // Client closed the connection.
303 // Discard the rest of the message.
304 if (rpcapd_discard(sockctrl
, NULL
, plen
) == -1)
311 // Send an authentication error, indicating
312 // that we require TLS.
314 if (rpcap_senderror(sockctrl
, NULL
, header
.ver
,
315 PCAP_ERR_TLS_REQUIRED
,
316 "TLS is required by this server", errbuf
) == -1)
318 // That failed; log a message and give up.
319 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
323 // Shut the session down.
326 ssl
= ssl_promotion(1, sockctrl
, errbuf
, PCAP_ERRBUF_SIZE
);
329 rpcapd_log(LOGPRIO_ERROR
, "TLS handshake on control connection failed: %s",
338 // We're expecting a non-TLS rpcapd message. If this
339 // looks, instead, like a TLS handshake message, send
340 // a TLS handshake_failed alert.
342 // The first octet of a TLS handshake is
343 // TLS_RECORD_TYPE_HANDSHAKE.
345 if (first_octet
== TLS_RECORD_TYPE_HANDSHAKE
)
349 // Read the record header.
351 nrecv
= sock_recv(sockctrl
, ssl
, (char *) &tls_header
,
352 sizeof tls_header
, SOCK_RECEIVEALL_YES
|SOCK_EOF_ISNT_ERROR
,
353 errbuf
, PCAP_ERRBUF_SIZE
);
357 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
365 plen
= (tls_header
.length_hi
<< 8) | tls_header
.length_lo
;
367 // Discard the rest of the message.
368 if (rpcapd_discard(sockctrl
, NULL
, plen
) == -1)
375 // Send a TLS handshake failure alert.
376 // Use the same version the client sent us.
378 tls_header
.type
= TLS_RECORD_TYPE_ALERT
;
379 tls_header
.length_hi
= 0;
380 tls_header
.length_lo
= TLS_ALERT_LEN
;
382 if (sock_send(sockctrl
, NULL
, (char *) &tls_header
,
383 TLS_RECORD_HEADER_LEN
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
385 // That failed; log a messsage and give up.
386 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
390 tls_alert
.alert_level
= TLS_ALERT_LEVEL_FATAL
;
391 tls_alert
.alert_description
= TLS_ALERT_HANDSHAKE_FAILURE
;
392 if (sock_send(sockctrl
, NULL
, (char *) &tls_alert
,
393 TLS_ALERT_LEN
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
395 // That failed; log a messsage and give up.
396 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
406 // Set parameters structure
407 pars
.sockctrl
= sockctrl
;
409 pars
.isactive
= isactive
; // active mode
410 pars
.nullAuthAllowed
= nullAuthAllowed
;
413 // We have a connection.
415 // If it's a passive mode connection, check whether the connecting
416 // host is among the ones allowed.
418 // In either case, we were handed a copy of the host list; free it
419 // as soon as we're done with it.
424 free(passiveClients
);
425 passiveClients
= NULL
;
429 struct sockaddr_storage from
;
433 // Get the address of the other end of the connection.
435 fromlen
= sizeof(struct sockaddr_storage
);
436 if (getpeername(pars
.sockctrl
, (struct sockaddr
*)&from
,
439 sock_geterror("getpeername(): ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
440 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
, 0, PCAP_ERR_NETW
, errmsgbuf
, errbuf
) == -1)
441 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
446 // Are they in the list of host/port combinations we allow?
448 host_port_check_status
= sock_check_hostlist(passiveClients
, RPCAP_HOSTLIST_SEP
, &from
, errmsgbuf
, PCAP_ERRBUF_SIZE
);
449 free(passiveClients
);
450 passiveClients
= NULL
;
451 if (host_port_check_status
< 0)
453 if (host_port_check_status
== -2) {
455 // We got an error; log it.
457 rpcapd_log(LOGPRIO_ERROR
, "%s", errmsgbuf
);
461 // Sorry, we can't let you in.
463 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
, 0, PCAP_ERR_HOSTNOAUTH
, errmsgbuf
, errbuf
) == -1)
464 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
471 // Catch SIGUSR1, but do nothing. We use it to interrupt the
472 // capture thread to break it out of a loop in which it's
473 // blocked waiting for packets to arrive.
475 // We don't want interrupted system calls to restart, so that
476 // the read routine for the pcap_t gets EINTR rather than
477 // restarting if it's blocked in a system call.
479 memset(&action
, 0, sizeof (action
));
480 action
.sa_handler
= noop_handler
;
482 sigemptyset(&action
.sa_mask
);
483 sigaction(SIGUSR1
, &action
, NULL
);
487 // The client must first authenticate; loop until they send us a
488 // message with a version we support and credentials we accept,
489 // they send us a close message indicating that they're giving up,
490 // or we get a network error or other fatal error.
492 while (!authenticated
)
495 // If we're not in active mode, we use select(), with a
496 // timeout, to wait for an authentication request; if
497 // the timeout expires, we drop the connection, so that
498 // a client can't just connect to us and leave us
504 // We do not have to block here
505 tv
.tv_sec
= RPCAP_TIMEOUT_INIT
;
508 FD_SET(pars
.sockctrl
, &rfds
);
510 retval
= select((int)pars
.sockctrl
+ 1, &rfds
, NULL
, NULL
, &tv
);
513 sock_geterror("select failed: ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
514 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
, 0, PCAP_ERR_NETW
, errmsgbuf
, errbuf
) == -1)
515 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
519 // The timeout has expired
520 // So, this was a fake connection. Drop it down
523 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
, 0, PCAP_ERR_INITTIMEOUT
, "The RPCAP initial timeout has expired", errbuf
) == -1)
524 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
530 // Read the message header from the client.
532 nrecv
= rpcapd_recv_msg_header(pars
.sockctrl
, pars
.ssl
, &header
);
540 // Client closed the connection.
547 // While we're in the authentication pharse, all requests
548 // must use version 0.
553 // Send it back to them with their version.
555 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
, header
.ver
,
557 "RPCAP version in requests in the authentication phase must be 0",
560 // That failed; log a message and give up.
561 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
565 // Discard the rest of the message and drop the
567 (void)rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
);
573 case RPCAP_MSG_AUTH_REQ
:
574 retval
= daemon_msg_auth_req(&pars
, plen
);
577 // Fatal error; a message has
578 // been logged, so just give up.
583 // Non-fatal error; we sent back
584 // an error message, so let them
589 // OK, we're authenticated; we sent back
590 // a reply, so start serving requests.
594 case RPCAP_MSG_CLOSE
:
596 // The client is giving up.
597 // Discard the rest of the message, if
598 // there is anything more.
600 (void)rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
);
601 // We're done with this client.
604 case RPCAP_MSG_ERROR
:
605 // Log this and close the connection?
606 // XXX - is this what happens in active
607 // mode, where *we* initiate the
608 // connection, and the client gives us
609 // an error message rather than a "let
610 // me log in" message, indicating that
611 // we're not allowed to connect to them?
612 (void)daemon_msg_err(pars
.sockctrl
, pars
.ssl
, plen
);
615 case RPCAP_MSG_FINDALLIF_REQ
:
616 case RPCAP_MSG_OPEN_REQ
:
617 case RPCAP_MSG_STARTCAP_REQ
:
618 case RPCAP_MSG_UPDATEFILTER_REQ
:
619 case RPCAP_MSG_STATS_REQ
:
620 case RPCAP_MSG_ENDCAP_REQ
:
621 case RPCAP_MSG_SETSAMPLING_REQ
:
623 // These requests can't be sent until
624 // the client is authenticated.
626 msg_type_string
= rpcap_msg_type_string(header
.type
);
627 if (msg_type_string
!= NULL
)
629 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "%s request sent before authentication was completed", msg_type_string
);
633 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Message of type %u sent before authentication was completed", header
.type
);
635 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
636 header
.ver
, PCAP_ERR_WRONGMSG
,
637 errmsgbuf
, errbuf
) == -1)
639 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
642 // Discard the rest of the message.
643 if (rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
) == -1)
650 case RPCAP_MSG_PACKET
:
651 case RPCAP_MSG_FINDALLIF_REPLY
:
652 case RPCAP_MSG_OPEN_REPLY
:
653 case RPCAP_MSG_STARTCAP_REPLY
:
654 case RPCAP_MSG_UPDATEFILTER_REPLY
:
655 case RPCAP_MSG_AUTH_REPLY
:
656 case RPCAP_MSG_STATS_REPLY
:
657 case RPCAP_MSG_ENDCAP_REPLY
:
658 case RPCAP_MSG_SETSAMPLING_REPLY
:
660 // These are server-to-client messages.
662 msg_type_string
= rpcap_msg_type_string(header
.type
);
663 if (msg_type_string
!= NULL
)
665 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Server-to-client message %s received from client", msg_type_string
);
669 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Server-to-client message of type %u received from client", header
.type
);
671 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
672 header
.ver
, PCAP_ERR_WRONGMSG
,
673 errmsgbuf
, errbuf
) == -1)
675 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
678 // Discard the rest of the message.
679 if (rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
) == -1)
688 // Unknown message type.
690 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Unknown message type %u", header
.type
);
691 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
692 header
.ver
, PCAP_ERR_WRONGMSG
,
693 errmsgbuf
, errbuf
) == -1)
695 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
698 // Discard the rest of the message.
699 if (rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
) == -1)
709 // OK, the client has authenticated itself, and we can start
710 // processing regular requests from it.
714 // We don't have any statistics yet.
726 errbuf
[0] = 0; // clear errbuf
728 // Avoid zombies connections; check if the connection is opens but no commands are performed
729 // from more than RPCAP_TIMEOUT_RUNTIME
731 // - I have to be in normal mode (no active mode)
732 // - if the device is open, I don't have to be in the middle of a capture (session->sockdata)
733 // - if the device is closed, I have always to check if a new command arrives
735 // Be carefully: the capture can have been started, but an error occurred (so session != NULL, but
737 if ((!pars
.isactive
) && ((session
== NULL
) || ((session
!= NULL
) && (session
->sockdata
== 0))))
739 // Check for the initial timeout
741 // We do not have to block here
742 tv
.tv_sec
= RPCAP_TIMEOUT_RUNTIME
;
745 FD_SET(pars
.sockctrl
, &rfds
);
747 retval
= select((int)pars
.sockctrl
+ 1, &rfds
, NULL
, NULL
, &tv
);
750 sock_geterror("select failed: ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
751 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
753 errmsgbuf
, errbuf
) == -1)
754 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
758 // The timeout has expired
759 // So, this was a fake connection. Drop it down
762 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
763 0, PCAP_ERR_INITTIMEOUT
,
764 "The RPCAP initial timeout has expired",
766 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
772 // Read the message header from the client.
774 nrecv
= rpcapd_recv_msg_header(pars
.sockctrl
, pars
.ssl
, &header
);
782 // Client closed the connection.
789 // Did the client specify a protocol version that we
792 if (!RPCAP_VERSION_IS_SUPPORTED(header
.ver
))
795 // Tell them it's not a supported version.
796 // Send the error message with their version,
797 // so they don't reject it as having the wrong
800 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
801 header
.ver
, PCAP_ERR_WRONGVER
,
802 "RPCAP version in message isn't supported by the server",
805 // That failed; log a message and give up.
806 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
810 // Discard the rest of the message.
811 (void)rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
);
818 case RPCAP_MSG_ERROR
: // The other endpoint reported an error
820 (void)daemon_msg_err(pars
.sockctrl
, pars
.ssl
, plen
);
821 // Do nothing; just exit; the error code is already into the errbuf
822 // XXX - actually exit....
826 case RPCAP_MSG_FINDALLIF_REQ
:
828 if (daemon_msg_findallif_req(header
.ver
, &pars
, plen
) == -1)
830 // Fatal error; a message has
831 // been logged, so just give up.
837 case RPCAP_MSG_OPEN_REQ
:
840 // Process the open request, and keep
841 // the source from it, for use later
842 // when the capture is started.
844 // XXX - we don't care if the client sends
845 // us multiple open requests, the last
848 retval
= daemon_msg_open_req(header
.ver
, &pars
,
849 plen
, source
, sizeof(source
));
852 // Fatal error; a message has
853 // been logged, so just give up.
860 case RPCAP_MSG_STARTCAP_REQ
:
864 // They never told us what device
866 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
868 PCAP_ERR_STARTCAPTURE
,
869 "No capture device was specified",
872 // Fatal error; log an
873 // error and give up.
874 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
877 if (rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
) == -1)
884 if (daemon_msg_startcap_req(header
.ver
, &pars
,
885 plen
, source
, &session
, &samp_param
,
888 // Fatal error; a message has
889 // been logged, so just give up.
895 case RPCAP_MSG_UPDATEFILTER_REQ
:
899 if (daemon_msg_updatefilter_req(header
.ver
,
900 &pars
, session
, plen
) == -1)
902 // Fatal error; a message has
903 // been logged, so just give up.
909 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
911 PCAP_ERR_UPDATEFILTER
,
912 "Device not opened. Cannot update filter",
915 // That failed; log a message and give up.
916 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
923 case RPCAP_MSG_CLOSE
: // The other endpoint close the pcap session
926 // Indicate to our caller that the client
927 // closed the control connection.
928 // This is used only in case of active mode.
930 client_told_us_to_close
= 1;
931 rpcapd_log(LOGPRIO_DEBUG
, "The other end system asked to close the connection.");
935 case RPCAP_MSG_STATS_REQ
:
937 if (daemon_msg_stats_req(header
.ver
, &pars
,
938 session
, plen
, &stats
, svrcapt
) == -1)
940 // Fatal error; a message has
941 // been logged, so just give up.
947 case RPCAP_MSG_ENDCAP_REQ
: // The other endpoint close the current capture session
951 // Save statistics (we can need them in the future)
952 if (pcap_stats(session
->fp
, &stats
))
954 svrcapt
= session
->TotCapt
;
964 if (daemon_msg_endcap_req(header
.ver
,
965 &pars
, session
) == -1)
969 // Fatal error; a message has
970 // been logged, so just give up.
978 rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
981 "Device not opened. Cannot close the capture",
987 case RPCAP_MSG_SETSAMPLING_REQ
:
989 if (daemon_msg_setsampling_req(header
.ver
,
990 &pars
, plen
, &samp_param
) == -1)
992 // Fatal error; a message has
993 // been logged, so just give up.
999 case RPCAP_MSG_AUTH_REQ
:
1002 // We're already authenticated; you don't
1003 // get to reauthenticate.
1005 rpcapd_log(LOGPRIO_INFO
, "The client sent an RPCAP_MSG_AUTH_REQ message after authentication was completed");
1006 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
1009 "RPCAP_MSG_AUTH_REQ request sent after authentication was completed",
1012 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1015 // Discard the rest of the message.
1016 if (rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
) == -1)
1023 case RPCAP_MSG_PACKET
:
1024 case RPCAP_MSG_FINDALLIF_REPLY
:
1025 case RPCAP_MSG_OPEN_REPLY
:
1026 case RPCAP_MSG_STARTCAP_REPLY
:
1027 case RPCAP_MSG_UPDATEFILTER_REPLY
:
1028 case RPCAP_MSG_AUTH_REPLY
:
1029 case RPCAP_MSG_STATS_REPLY
:
1030 case RPCAP_MSG_ENDCAP_REPLY
:
1031 case RPCAP_MSG_SETSAMPLING_REPLY
:
1033 // These are server-to-client messages.
1035 msg_type_string
= rpcap_msg_type_string(header
.type
);
1036 if (msg_type_string
!= NULL
)
1038 rpcapd_log(LOGPRIO_INFO
, "The client sent a %s server-to-client message", msg_type_string
);
1039 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Server-to-client message %s received from client", msg_type_string
);
1043 rpcapd_log(LOGPRIO_INFO
, "The client sent a server-to-client message of type %u", header
.type
);
1044 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Server-to-client message of type %u received from client", header
.type
);
1046 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
1047 header
.ver
, PCAP_ERR_WRONGMSG
,
1048 errmsgbuf
, errbuf
) == -1)
1050 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1053 // Discard the rest of the message.
1054 if (rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
) == -1)
1063 // Unknown message type.
1065 rpcapd_log(LOGPRIO_INFO
, "The client sent a message of type %u", header
.type
);
1066 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Unknown message type %u", header
.type
);
1067 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
1068 header
.ver
, PCAP_ERR_WRONGMSG
,
1069 errbuf
, errmsgbuf
) == -1)
1071 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1074 // Discard the rest of the message.
1075 if (rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
) == -1)
1086 // The service loop is finishing up.
1087 // If we have a capture session running, close it.
1090 session_close(session
);
1096 // Finish using the SSL handle for the control socket, if we
1097 // have an SSL connection, and close the control socket.
1102 // Finish using the SSL handle for the socket.
1103 // This must be done *before* the socket is closed.
1107 sock_close(sockctrl
, NULL
, 0);
1109 // Print message and return
1110 rpcapd_log(LOGPRIO_DEBUG
, "I'm exiting from the child loop");
1112 return client_told_us_to_close
;
1116 * This handles the RPCAP_MSG_ERR message.
1119 daemon_msg_err(SOCKET sockctrl
, SSL
*ssl
, uint32 plen
)
1121 char errbuf
[PCAP_ERRBUF_SIZE
];
1122 char remote_errbuf
[PCAP_ERRBUF_SIZE
];
1124 if (plen
>= PCAP_ERRBUF_SIZE
)
1127 * Message is too long; just read as much of it as we
1128 * can into the buffer provided, and discard the rest.
1130 if (sock_recv(sockctrl
, ssl
, remote_errbuf
, PCAP_ERRBUF_SIZE
- 1,
1131 SOCK_RECEIVEALL_YES
|SOCK_EOF_IS_ERROR
, errbuf
,
1132 PCAP_ERRBUF_SIZE
) == -1)
1135 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
1138 if (rpcapd_discard(sockctrl
, ssl
, plen
- (PCAP_ERRBUF_SIZE
- 1)) == -1)
1145 * Null-terminate it.
1147 remote_errbuf
[PCAP_ERRBUF_SIZE
- 1] = '\0';
1151 /* Empty error string. */
1152 remote_errbuf
[0] = '\0';
1156 if (sock_recv(sockctrl
, ssl
, remote_errbuf
, plen
,
1157 SOCK_RECEIVEALL_YES
|SOCK_EOF_IS_ERROR
, errbuf
,
1158 PCAP_ERRBUF_SIZE
) == -1)
1161 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
1166 * Null-terminate it.
1168 remote_errbuf
[plen
] = '\0';
1171 rpcapd_log(LOGPRIO_ERROR
, "Error from client: %s", remote_errbuf
);
1176 * This handles the RPCAP_MSG_AUTH_REQ message.
1177 * It checks if the authentication credentials supplied by the user are valid.
1179 * This function is called if the daemon receives a RPCAP_MSG_AUTH_REQ
1180 * message in its authentication loop. It reads the body of the
1181 * authentication message from the network and checks whether the
1182 * credentials are valid.
1184 * \param sockctrl: the socket for the control connection.
1186 * \param nullAuthAllowed: '1' if the NULL authentication is allowed.
1188 * \param errbuf: a user-allocated buffer in which the error message
1189 * (if one) has to be written. It must be at least PCAP_ERRBUF_SIZE
1192 * \return '0' if everything is fine, '-1' if an unrecoverable error occurred,
1193 * or '-2' if the authentication failed. For errors, an error message is
1194 * returned in the 'errbuf' variable; this gives a message for the
1195 * unrecoverable error or for the authentication failure.
1198 daemon_msg_auth_req(struct daemon_slpars
*pars
, uint32 plen
)
1200 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
1201 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
1203 struct rpcap_auth auth
; // RPCAP authentication header
1204 char sendbuf
[RPCAP_NETBUF_SIZE
]; // temporary buffer in which data to be sent is buffered
1205 int sendbufidx
= 0; // index which keeps the number of bytes currently buffered
1206 struct rpcap_authreply
*authreply
; // authentication reply message
1208 status
= rpcapd_recv(pars
->sockctrl
, pars
->ssl
, (char *) &auth
, sizeof(struct rpcap_auth
), &plen
, errmsgbuf
);
1218 switch (ntohs(auth
.type
))
1220 case RPCAP_RMTAUTH_NULL
:
1222 if (!pars
->nullAuthAllowed
)
1224 // Send the client an error reply.
1225 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
,
1226 "Authentication failed; NULL authentication not permitted.");
1227 if (rpcap_senderror(pars
->sockctrl
, pars
->ssl
,
1228 0, PCAP_ERR_AUTH_FAILED
, errmsgbuf
, errbuf
) == -1)
1230 // That failed; log a message and give up.
1231 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1239 case RPCAP_RMTAUTH_PWD
:
1241 char *username
, *passwd
;
1242 uint32 usernamelen
, passwdlen
;
1244 usernamelen
= ntohs(auth
.slen1
);
1245 username
= (char *) malloc (usernamelen
+ 1);
1246 if (username
== NULL
)
1248 pcap_fmt_errmsg_for_errno(errmsgbuf
,
1249 PCAP_ERRBUF_SIZE
, errno
, "malloc() failed");
1252 status
= rpcapd_recv(pars
->sockctrl
, pars
->ssl
, username
, usernamelen
, &plen
, errmsgbuf
);
1263 username
[usernamelen
] = '\0';
1265 passwdlen
= ntohs(auth
.slen2
);
1266 passwd
= (char *) malloc (passwdlen
+ 1);
1269 pcap_fmt_errmsg_for_errno(errmsgbuf
,
1270 PCAP_ERRBUF_SIZE
, errno
, "malloc() failed");
1274 status
= rpcapd_recv(pars
->sockctrl
, pars
->ssl
, passwd
, passwdlen
, &plen
, errmsgbuf
);
1287 passwd
[passwdlen
] = '\0';
1289 if (daemon_AuthUserPwd(username
, passwd
, errmsgbuf
))
1292 // Authentication failed. Let the client
1297 if (rpcap_senderror(pars
->sockctrl
, pars
->ssl
,
1298 0, PCAP_ERR_AUTH_FAILED
, errmsgbuf
, errbuf
) == -1)
1300 // That failed; log a message and give up.
1301 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1306 // Suspend for 1 second, so that they can't
1307 // hammer us with repeated tries with an
1308 // attack such as a dictionary attack.
1310 // WARNING: this delay is inserted only
1311 // at this point; if the client closes the
1312 // connection and reconnects, the suspension
1313 // time does not have any effect.
1315 sleep_secs(RPCAP_SUSPEND_WRONGAUTH
);
1325 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
,
1326 "Authentication type not recognized.");
1327 if (rpcap_senderror(pars
->sockctrl
, pars
->ssl
,
1328 0, PCAP_ERR_AUTH_TYPE_NOTSUP
, errmsgbuf
, errbuf
) == -1)
1330 // That failed; log a message and give up.
1331 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1337 // The authentication succeeded; let the client know.
1338 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
, &sendbufidx
,
1339 RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1342 rpcap_createhdr((struct rpcap_header
*) sendbuf
, 0,
1343 RPCAP_MSG_AUTH_REPLY
, 0, sizeof(struct rpcap_authreply
));
1345 authreply
= (struct rpcap_authreply
*) &sendbuf
[sendbufidx
];
1347 if (sock_bufferize(NULL
, sizeof(struct rpcap_authreply
), NULL
, &sendbufidx
,
1348 RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1352 // Indicate to our peer what versions we support.
1354 memset(authreply
, 0, sizeof(struct rpcap_authreply
));
1355 authreply
->minvers
= RPCAP_MIN_VERSION
;
1356 authreply
->maxvers
= RPCAP_MAX_VERSION
;
1359 if (sock_send(pars
->sockctrl
, pars
->ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1361 // That failed; log a messsage and give up.
1362 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1366 // Check if all the data has been read; if not, discard the data in excess
1367 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
1375 if (rpcap_senderror(pars
->sockctrl
, pars
->ssl
, 0, PCAP_ERR_AUTH
,
1376 errmsgbuf
, errbuf
) == -1)
1378 // That failed; log a message and give up.
1379 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1384 // Check if all the data has been read; if not, discard the data in excess
1385 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
1394 daemon_AuthUserPwd(char *username
, char *password
, char *errbuf
)
1398 * Warning: the user which launches the process must have the
1399 * SE_TCB_NAME right.
1400 * This corresponds to have the "Act as part of the Operating System"
1401 * turned on (administrative tools, local security settings, local
1402 * policies, user right assignment)
1403 * However, it seems to me that if you run it as a service, this
1404 * right should be provided by default.
1406 * XXX - hopefully, this returns errors such as ERROR_LOGON_FAILURE,
1407 * which merely indicates that the user name or password is
1408 * incorrect, not whether it's the user name or the password
1409 * that's incorrect, so a client that's trying to brute-force
1410 * accounts doesn't know whether it's the user name or the
1411 * password that's incorrect, so it doesn't know whether to
1412 * stop trying to log in with a given user name and move on
1413 * to another user name.
1416 if (LogonUser(username
, ".", password
, LOGON32_LOGON_NETWORK
, LOGON32_PROVIDER_DEFAULT
, &Token
) == 0)
1418 pcap_win32_err_to_str(GetLastError(), errbuf
);
1422 // This call should change the current thread to the selected user.
1423 // I didn't test it.
1424 if (ImpersonateLoggedOnUser(Token
) == 0)
1426 pcap_win32_err_to_str(GetLastError(), errbuf
);
1438 * http://www.unixpapa.com/incnote/passwd.html
1440 * We use the Solaris/Linux shadow password authentication if
1441 * we have getspnam(), otherwise we just do traditional
1442 * authentication, which, on some platforms, might work, even
1443 * with shadow passwords, if we're running as root. Traditional
1444 * authenticaion won't work if we're not running as root, as
1445 * I think these days all UN*Xes either won't return the password
1446 * at all with getpwnam() or will only do so if you're root.
1448 * XXX - perhaps what we *should* be using is PAM, if we have
1449 * it. That might hide all the details of username/password
1450 * authentication, whether it's done with a visible-to-root-
1451 * only password database or some other authentication mechanism,
1454 struct passwd
*user
;
1455 char *user_password
;
1456 #ifdef HAVE_GETSPNAM
1457 struct spwd
*usersp
;
1460 // This call is needed to get the uid
1461 if ((user
= getpwnam(username
)) == NULL
)
1463 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Authentication failed: user name or password incorrect");
1467 #ifdef HAVE_GETSPNAM
1468 // This call is needed to get the password; otherwise 'x' is returned
1469 if ((usersp
= getspnam(username
)) == NULL
)
1471 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Authentication failed: user name or password incorrect");
1474 user_password
= usersp
->sp_pwdp
;
1477 * XXX - what about other platforms?
1478 * The unixpapa.com page claims this Just Works on *BSD if you're
1479 * running as root - it's from 2000, so it doesn't indicate whether
1480 * macOS (which didn't come out until 2001, under the name Mac OS
1481 * X) behaves like the *BSDs or not, and might also work on AIX.
1482 * HP-UX does something else.
1484 * Again, hopefully PAM hides all that.
1486 user_password
= user
->pw_passwd
;
1489 if (strcmp(user_password
, (char *) crypt(password
, user_password
)) != 0)
1491 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Authentication failed: user name or password incorrect");
1495 if (setuid(user
->pw_uid
))
1497 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1502 /* if (setgid(user->pw_gid))
1504 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1516 daemon_msg_findallif_req(uint8 ver
, struct daemon_slpars
*pars
, uint32 plen
)
1518 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
1519 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
1520 char sendbuf
[RPCAP_NETBUF_SIZE
]; // temporary buffer in which data to be sent is buffered
1521 int sendbufidx
= 0; // index which keeps the number of bytes currently buffered
1522 pcap_if_t
*alldevs
= NULL
; // pointer to the header of the interface chain
1523 pcap_if_t
*d
; // temp pointer needed to scan the interface chain
1524 struct pcap_addr
*address
; // pcap structure that keeps a network address of an interface
1525 struct rpcap_findalldevs_if
*findalldevs_if
;// rpcap structure that packet all the data of an interface together
1526 uint16 nif
= 0; // counts the number of interface listed
1528 // Discard the rest of the message; there shouldn't be any payload.
1529 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
1535 // Retrieve the device list
1536 if (pcap_findalldevs(&alldevs
, errmsgbuf
) == -1)
1539 if (alldevs
== NULL
)
1541 if (rpcap_senderror(pars
->sockctrl
, pars
->ssl
, ver
,
1542 PCAP_ERR_NOREMOTEIF
,
1543 "No interfaces found! Make sure libpcap/WinPcap is properly installed"
1544 " and you have the right to access to the remote device.",
1547 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1553 // checks the number of interfaces and it computes the total length of the payload
1554 for (d
= alldevs
; d
!= NULL
; d
= d
->next
)
1559 plen
+= strlen(d
->description
);
1561 plen
+= strlen(d
->name
);
1563 plen
+= sizeof(struct rpcap_findalldevs_if
);
1565 for (address
= d
->addresses
; address
!= NULL
; address
= address
->next
)
1568 * Send only IPv4 and IPv6 addresses over the wire.
1570 switch (address
->addr
->sa_family
)
1576 plen
+= (sizeof(struct rpcap_sockaddr
) * 4);
1585 // RPCAP findalldevs command
1586 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
,
1587 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
,
1588 PCAP_ERRBUF_SIZE
) == -1)
1591 rpcap_createhdr((struct rpcap_header
*) sendbuf
, ver
,
1592 RPCAP_MSG_FINDALLIF_REPLY
, nif
, plen
);
1594 // send the interface list
1595 for (d
= alldevs
; d
!= NULL
; d
= d
->next
)
1597 uint16 lname
, ldescr
;
1599 findalldevs_if
= (struct rpcap_findalldevs_if
*) &sendbuf
[sendbufidx
];
1601 if (sock_bufferize(NULL
, sizeof(struct rpcap_findalldevs_if
), NULL
,
1602 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1605 memset(findalldevs_if
, 0, sizeof(struct rpcap_findalldevs_if
));
1607 if (d
->description
) ldescr
= (short) strlen(d
->description
);
1609 if (d
->name
) lname
= (short) strlen(d
->name
);
1612 findalldevs_if
->desclen
= htons(ldescr
);
1613 findalldevs_if
->namelen
= htons(lname
);
1614 findalldevs_if
->flags
= htonl(d
->flags
);
1616 for (address
= d
->addresses
; address
!= NULL
; address
= address
->next
)
1619 * Send only IPv4 and IPv6 addresses over the wire.
1621 switch (address
->addr
->sa_family
)
1627 findalldevs_if
->naddr
++;
1634 findalldevs_if
->naddr
= htons(findalldevs_if
->naddr
);
1636 if (sock_bufferize(d
->name
, lname
, sendbuf
, &sendbufidx
,
1637 RPCAP_NETBUF_SIZE
, SOCKBUF_BUFFERIZE
, errmsgbuf
,
1638 PCAP_ERRBUF_SIZE
) == -1)
1641 if (sock_bufferize(d
->description
, ldescr
, sendbuf
, &sendbufidx
,
1642 RPCAP_NETBUF_SIZE
, SOCKBUF_BUFFERIZE
, errmsgbuf
,
1643 PCAP_ERRBUF_SIZE
) == -1)
1646 // send all addresses
1647 for (address
= d
->addresses
; address
!= NULL
; address
= address
->next
)
1649 struct rpcap_sockaddr
*sockaddr
;
1652 * Send only IPv4 and IPv6 addresses over the wire.
1654 switch (address
->addr
->sa_family
)
1660 sockaddr
= (struct rpcap_sockaddr
*) &sendbuf
[sendbufidx
];
1661 if (sock_bufferize(NULL
, sizeof(struct rpcap_sockaddr
), NULL
,
1662 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1664 daemon_seraddr((struct sockaddr_storage
*) address
->addr
, sockaddr
);
1666 sockaddr
= (struct rpcap_sockaddr
*) &sendbuf
[sendbufidx
];
1667 if (sock_bufferize(NULL
, sizeof(struct rpcap_sockaddr
), NULL
,
1668 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1670 daemon_seraddr((struct sockaddr_storage
*) address
->netmask
, sockaddr
);
1672 sockaddr
= (struct rpcap_sockaddr
*) &sendbuf
[sendbufidx
];
1673 if (sock_bufferize(NULL
, sizeof(struct rpcap_sockaddr
), NULL
,
1674 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1676 daemon_seraddr((struct sockaddr_storage
*) address
->broadaddr
, sockaddr
);
1678 sockaddr
= (struct rpcap_sockaddr
*) &sendbuf
[sendbufidx
];
1679 if (sock_bufferize(NULL
, sizeof(struct rpcap_sockaddr
), NULL
,
1680 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1682 daemon_seraddr((struct sockaddr_storage
*) address
->dstaddr
, sockaddr
);
1691 // We no longer need the device list. Free it.
1692 pcap_freealldevs(alldevs
);
1694 // Send a final command that says "now send it!"
1695 if (sock_send(pars
->sockctrl
, pars
->ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1697 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1705 pcap_freealldevs(alldevs
);
1707 if (rpcap_senderror(pars
->sockctrl
, pars
->ssl
, ver
,
1708 PCAP_ERR_FINDALLIF
, errmsgbuf
, errbuf
) == -1)
1710 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1717 \param plen: the length of the current message (needed in order to be able
1718 to discard excess data in the message, if present)
1721 daemon_msg_open_req(uint8 ver
, struct daemon_slpars
*pars
, uint32 plen
,
1722 char *source
, size_t sourcelen
)
1724 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
1725 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
1726 pcap_t
*fp
; // pcap_t main variable
1728 char sendbuf
[RPCAP_NETBUF_SIZE
]; // temporary buffer in which data to be sent is buffered
1729 int sendbufidx
= 0; // index which keeps the number of bytes currently buffered
1730 struct rpcap_openreply
*openreply
; // open reply message
1732 if (plen
> sourcelen
- 1)
1734 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Source string too long");
1738 nread
= sock_recv(pars
->sockctrl
, pars
->ssl
, source
, plen
,
1739 SOCK_RECEIVEALL_YES
|SOCK_EOF_IS_ERROR
, errbuf
, PCAP_ERRBUF_SIZE
);
1742 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
1745 source
[nread
] = '\0';
1748 // XXX - make sure it's *not* a URL; we don't support opening
1749 // remote devices here.
1751 // Open the selected device
1752 // This is a fake open, since we do that only to get the needed parameters, then we close the device again
1753 if ((fp
= pcap_open_live(source
,
1754 1500 /* fake snaplen */,
1756 1000 /* fake timeout */,
1757 errmsgbuf
)) == NULL
)
1760 // Now, I can send a RPCAP open reply message
1761 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
, &sendbufidx
,
1762 RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1765 rpcap_createhdr((struct rpcap_header
*) sendbuf
, ver
,
1766 RPCAP_MSG_OPEN_REPLY
, 0, sizeof(struct rpcap_openreply
));
1768 openreply
= (struct rpcap_openreply
*) &sendbuf
[sendbufidx
];
1770 if (sock_bufferize(NULL
, sizeof(struct rpcap_openreply
), NULL
, &sendbufidx
,
1771 RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1774 memset(openreply
, 0, sizeof(struct rpcap_openreply
));
1775 openreply
->linktype
= htonl(pcap_datalink(fp
));
1777 * This is always 0 for live captures; we no longer support it
1778 * as something we read from capture files and supply to
1779 * clients, but we have to send it over the wire, as open
1780 * replies are expected to have 8 bytes of payload by
1783 openreply
->tzoff
= 0;
1785 // We're done with the pcap_t.
1789 if (sock_send(pars
->sockctrl
, pars
->ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1791 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1797 if (rpcap_senderror(pars
->sockctrl
, pars
->ssl
, ver
, PCAP_ERR_OPEN
,
1798 errmsgbuf
, errbuf
) == -1)
1800 // That failed; log a message and give up.
1801 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1805 // Check if all the data has been read; if not, discard the data in excess
1806 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
1814 \param plen: the length of the current message (needed in order to be able
1815 to discard excess data in the message, if present)
1818 daemon_msg_startcap_req(uint8 ver
, struct daemon_slpars
*pars
, uint32 plen
,
1819 char *source
, struct session
**sessionp
,
1820 struct rpcap_sampling
*samp_param _U_
, int uses_ssl
)
1822 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
1823 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
1824 char portdata
[PCAP_BUF_SIZE
]; // temp variable needed to derive the data port
1825 char peerhost
[PCAP_BUF_SIZE
]; // temp variable needed to derive the host name of our peer
1826 struct session
*session
= NULL
; // saves state of session
1828 char sendbuf
[RPCAP_NETBUF_SIZE
]; // temporary buffer in which data to be sent is buffered
1829 int sendbufidx
= 0; // index which keeps the number of bytes currently buffered
1831 // socket-related variables
1832 struct addrinfo hints
; // temp, needed to open a socket connection
1833 struct addrinfo
*addrinfo
; // temp, needed to open a socket connection
1834 struct sockaddr_storage saddr
; // temp, needed to retrieve the network data port chosen on the local machine
1835 socklen_t saddrlen
; // temp, needed to retrieve the network data port chosen on the local machine
1836 int ret
; // return value from functions
1838 // RPCAP-related variables
1839 struct rpcap_startcapreq startcapreq
; // start capture request message
1840 struct rpcap_startcapreply
*startcapreply
; // start capture reply message
1841 int serveropen_dp
; // keeps who is going to open the data connection
1845 status
= rpcapd_recv(pars
->sockctrl
, pars
->ssl
, (char *) &startcapreq
,
1846 sizeof(struct rpcap_startcapreq
), &plen
, errmsgbuf
);
1856 startcapreq
.flags
= ntohs(startcapreq
.flags
);
1858 // Check that the client does not ask for UDP is the server has been asked
1859 // to enforce encryption, as SSL is not supported yet with UDP:
1860 if (uses_ssl
&& (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_DGRAM
))
1862 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1863 "SSL not supported with UDP forward of remote packets");
1867 // Create a session structure
1868 session
= malloc(sizeof(struct session
));
1869 if (session
== NULL
)
1871 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Can't allocate session structure");
1875 session
->sockdata
= INVALID_SOCKET
;
1876 session
->ctrl_ssl
= session
->data_ssl
= NULL
;
1877 // We don't have a thread yet.
1878 session
->have_thread
= 0;
1880 // We *shouldn't* have to initialize the thread indicator
1881 // itself, because the compiler *should* realize that we
1882 // only use this if have_thread isn't 0, but we *do* have
1883 // to do it, because not all compilers *do* realize that.
1885 // There is no "invalid thread handle" value for a UN*X
1886 // pthread_t, so we just zero it out.
1889 session
->thread
= INVALID_HANDLE_VALUE
;
1891 memset(&session
->thread
, 0, sizeof(session
->thread
));
1894 // Open the selected device
1895 if ((session
->fp
= pcap_open_live(source
,
1896 ntohl(startcapreq
.snaplen
),
1897 (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_PROMISC
) ? 1 : 0 /* local device, other flags not needed */,
1898 ntohl(startcapreq
.read_timeout
),
1899 errmsgbuf
)) == NULL
)
1903 // Apply sampling parameters
1904 fp
->rmt_samp
.method
= samp_param
->method
;
1905 fp
->rmt_samp
.value
= samp_param
->value
;
1909 We're in active mode if:
1910 - we're using TCP, and the user wants us to be in active mode
1913 serveropen_dp
= (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_SERVEROPEN
) || (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_DGRAM
) || pars
->isactive
;
1916 Gets the sockaddr structure referred to the other peer in the ctrl connection
1918 We need that because:
1919 - if we're in passive mode, we need to know the address family we want to use
1920 (the same used for the ctrl socket)
1921 - if we're in active mode, we need to know the network address of the other host
1922 we want to connect to
1924 saddrlen
= sizeof(struct sockaddr_storage
);
1925 if (getpeername(pars
->sockctrl
, (struct sockaddr
*) &saddr
, &saddrlen
) == -1)
1927 sock_geterror("getpeername(): ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
1931 memset(&hints
, 0, sizeof(struct addrinfo
));
1932 hints
.ai_socktype
= (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_DGRAM
) ? SOCK_DGRAM
: SOCK_STREAM
;
1933 hints
.ai_family
= saddr
.ss_family
;
1935 // Now we have to create a new socket to send packets
1936 if (serveropen_dp
) // Data connection is opened by the server toward the client
1938 pcap_snprintf(portdata
, sizeof portdata
, "%d", ntohs(startcapreq
.portdata
));
1940 // Get the name of the other peer (needed to connect to that specific network address)
1941 if (getnameinfo((struct sockaddr
*) &saddr
, saddrlen
, peerhost
,
1942 sizeof(peerhost
), NULL
, 0, NI_NUMERICHOST
))
1944 sock_geterror("getnameinfo(): ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
1948 if (sock_initaddress(peerhost
, portdata
, &hints
, &addrinfo
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1951 if ((session
->sockdata
= sock_open(addrinfo
, SOCKOPEN_CLIENT
, 0, errmsgbuf
, PCAP_ERRBUF_SIZE
)) == INVALID_SOCKET
)
1954 else // Data connection is opened by the client toward the server
1956 hints
.ai_flags
= AI_PASSIVE
;
1958 // Let's the server socket pick up a free network port for us
1959 if (sock_initaddress(NULL
, "0", &hints
, &addrinfo
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1962 if ((session
->sockdata
= sock_open(addrinfo
, SOCKOPEN_SERVER
, 1 /* max 1 connection in queue */, errmsgbuf
, PCAP_ERRBUF_SIZE
)) == INVALID_SOCKET
)
1965 // get the complete sockaddr structure used in the data connection
1966 saddrlen
= sizeof(struct sockaddr_storage
);
1967 if (getsockname(session
->sockdata
, (struct sockaddr
*) &saddr
, &saddrlen
) == -1)
1969 sock_geterror("getsockname(): ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
1973 // Get the local port the system picked up
1974 if (getnameinfo((struct sockaddr
*) &saddr
, saddrlen
, NULL
,
1975 0, portdata
, sizeof(portdata
), NI_NUMERICSERV
))
1977 sock_geterror("getnameinfo(): ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
1982 // addrinfo is no longer used
1983 freeaddrinfo(addrinfo
);
1986 // Needed to send an error on the ctrl connection
1987 session
->sockctrl
= pars
->sockctrl
;
1988 session
->ctrl_ssl
= pars
->ssl
;
1989 session
->protocol_version
= ver
;
1991 // Now I can set the filter
1992 ret
= daemon_unpackapplyfilter(pars
->sockctrl
, pars
->ssl
, session
, &plen
, errmsgbuf
);
1995 // Fatal error. A message has been logged; just give up.
2000 // Non-fatal error. Send an error message to the client.
2004 // Now, I can send a RPCAP start capture reply message
2005 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
, &sendbufidx
,
2006 RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
2009 rpcap_createhdr((struct rpcap_header
*) sendbuf
, ver
,
2010 RPCAP_MSG_STARTCAP_REPLY
, 0, sizeof(struct rpcap_startcapreply
));
2012 startcapreply
= (struct rpcap_startcapreply
*) &sendbuf
[sendbufidx
];
2014 if (sock_bufferize(NULL
, sizeof(struct rpcap_startcapreply
), NULL
,
2015 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
2018 memset(startcapreply
, 0, sizeof(struct rpcap_startcapreply
));
2019 startcapreply
->bufsize
= htonl(pcap_bufsize(session
->fp
));
2023 unsigned short port
= (unsigned short)strtoul(portdata
,NULL
,10);
2024 startcapreply
->portdata
= htons(port
);
2027 if (sock_send(pars
->sockctrl
, pars
->ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2029 // That failed; log a message and give up.
2030 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2036 SOCKET socktemp
; // We need another socket, since we're going to accept() a connection
2038 // Connection creation
2039 saddrlen
= sizeof(struct sockaddr_storage
);
2041 socktemp
= accept(session
->sockdata
, (struct sockaddr
*) &saddr
, &saddrlen
);
2043 if (socktemp
== INVALID_SOCKET
)
2045 sock_geterror("accept(): ", errbuf
, PCAP_ERRBUF_SIZE
);
2046 rpcapd_log(LOGPRIO_ERROR
, "Accept of data connection failed: %s",
2051 // Now that I accepted the connection, the server socket is no longer needed
2052 sock_close(session
->sockdata
, NULL
, 0);
2053 session
->sockdata
= socktemp
;
2060 /* In both active or passive cases, wait for the client to initiate the
2061 * TLS handshake. Yes during that time the control socket will not be
2062 * served, but the same was true from the above call to accept(). */
2063 ssl
= ssl_promotion(1, session
->sockdata
, errbuf
, PCAP_ERRBUF_SIZE
);
2066 rpcapd_log(LOGPRIO_ERROR
, "TLS handshake failed: %s", errbuf
);
2071 session
->data_ssl
= ssl
;
2073 // Now we have to create a new thread to receive packets
2075 session
->thread
= (HANDLE
)_beginthreadex(NULL
, 0, daemon_thrdatamain
,
2076 (void *) session
, 0, NULL
);
2077 if (session
->thread
== 0)
2079 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Error creating the data thread");
2083 ret
= pthread_create(&session
->thread
, NULL
, daemon_thrdatamain
,
2087 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
2088 ret
, "Error creating the data thread");
2092 session
->have_thread
= 1;
2094 // Check if all the data has been read; if not, discard the data in excess
2095 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
2098 *sessionp
= session
;
2103 // Not a fatal error, so send the client an error message and
2104 // keep serving client requests.
2109 freeaddrinfo(addrinfo
);
2113 session_close(session
);
2117 if (rpcap_senderror(pars
->sockctrl
, pars
->ssl
, ver
,
2118 PCAP_ERR_STARTCAPTURE
, errmsgbuf
, errbuf
) == -1)
2120 // That failed; log a message and give up.
2121 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2125 // Check if all the data has been read; if not, discard the data in excess
2126 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
2136 // Fatal network error, so don't try to communicate with
2137 // the client, just give up.
2143 session_close(session
);
2151 daemon_msg_endcap_req(uint8 ver
, struct daemon_slpars
*pars
,
2152 struct session
*session
)
2154 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2155 struct rpcap_header header
;
2157 session_close(session
);
2159 rpcap_createhdr(&header
, ver
, RPCAP_MSG_ENDCAP_REPLY
, 0, 0);
2161 if (sock_send(pars
->sockctrl
, pars
->ssl
, (char *) &header
, sizeof(struct rpcap_header
), errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2163 // That failed; log a message and give up.
2164 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2172 daemon_unpackapplyfilter(SOCKET sockctrl
, SSL
*ctrl_ssl
, struct session
*session
, uint32
*plenp
, char *errmsgbuf
)
2175 struct rpcap_filter filter
;
2176 struct rpcap_filterbpf_insn insn
;
2177 struct bpf_insn
*bf_insn
;
2178 struct bpf_program bf_prog
;
2181 status
= rpcapd_recv(sockctrl
, ctrl_ssl
, (char *) &filter
,
2182 sizeof(struct rpcap_filter
), plenp
, errmsgbuf
);
2192 bf_prog
.bf_len
= ntohl(filter
.nitems
);
2194 if (ntohs(filter
.filtertype
) != RPCAP_UPDATEFILTER_BPF
)
2196 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Only BPF/NPF filters are currently supported");
2200 bf_insn
= (struct bpf_insn
*) malloc (sizeof(struct bpf_insn
) * bf_prog
.bf_len
);
2201 if (bf_insn
== NULL
)
2203 pcap_fmt_errmsg_for_errno(errmsgbuf
, PCAP_ERRBUF_SIZE
,
2204 errno
, "malloc() failed");
2208 bf_prog
.bf_insns
= bf_insn
;
2210 for (i
= 0; i
< bf_prog
.bf_len
; i
++)
2212 status
= rpcapd_recv(sockctrl
, ctrl_ssl
, (char *) &insn
,
2213 sizeof(struct rpcap_filterbpf_insn
), plenp
, errmsgbuf
);
2223 bf_insn
->code
= ntohs(insn
.code
);
2224 bf_insn
->jf
= insn
.jf
;
2225 bf_insn
->jt
= insn
.jt
;
2226 bf_insn
->k
= ntohl(insn
.k
);
2232 // XXX - pcap_setfilter() should do the validation for us.
2234 if (bpf_validate(bf_prog
.bf_insns
, bf_prog
.bf_len
) == 0)
2236 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "The filter contains bogus instructions");
2240 if (pcap_setfilter(session
->fp
, &bf_prog
))
2242 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "RPCAP error: %s", pcap_geterr(session
->fp
));
2250 daemon_msg_updatefilter_req(uint8 ver
, struct daemon_slpars
*pars
,
2251 struct session
*session
, uint32 plen
)
2253 char errbuf
[PCAP_ERRBUF_SIZE
];
2254 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
2255 int ret
; // status of daemon_unpackapplyfilter()
2256 struct rpcap_header header
; // keeps the answer to the updatefilter command
2258 ret
= daemon_unpackapplyfilter(pars
->sockctrl
, pars
->ssl
, session
, &plen
, errmsgbuf
);
2261 // Fatal error. A message has been logged; just give up.
2266 // Non-fatal error. Send an error reply to the client.
2270 // Check if all the data has been read; if not, discard the data in excess
2271 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
2277 // A response is needed, otherwise the other host does not know that everything went well
2278 rpcap_createhdr(&header
, ver
, RPCAP_MSG_UPDATEFILTER_REPLY
, 0, 0);
2280 if (sock_send(pars
->sockctrl
, pars
->ssl
, (char *) &header
, sizeof (struct rpcap_header
), pcap_geterr(session
->fp
), PCAP_ERRBUF_SIZE
))
2282 // That failed; log a messsage and give up.
2283 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2290 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
2294 rpcap_senderror(pars
->sockctrl
, pars
->ssl
, ver
, PCAP_ERR_UPDATEFILTER
,
2301 \brief Received the sampling parameters from remote host and it stores in the pcap_t structure.
2304 daemon_msg_setsampling_req(uint8 ver
, struct daemon_slpars
*pars
, uint32 plen
,
2305 struct rpcap_sampling
*samp_param
)
2307 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2308 char errmsgbuf
[PCAP_ERRBUF_SIZE
];
2309 struct rpcap_header header
;
2310 struct rpcap_sampling rpcap_samp
;
2313 status
= rpcapd_recv(pars
->sockctrl
, pars
->ssl
, (char *) &rpcap_samp
, sizeof(struct rpcap_sampling
), &plen
, errmsgbuf
);
2323 // Save these settings in the pcap_t
2324 samp_param
->method
= rpcap_samp
.method
;
2325 samp_param
->value
= ntohl(rpcap_samp
.value
);
2327 // A response is needed, otherwise the other host does not know that everything went well
2328 rpcap_createhdr(&header
, ver
, RPCAP_MSG_SETSAMPLING_REPLY
, 0, 0);
2330 if (sock_send(pars
->sockctrl
, pars
->ssl
, (char *) &header
, sizeof (struct rpcap_header
), errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2332 // That failed; log a messsage and give up.
2333 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2337 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
2345 if (rpcap_senderror(pars
->sockctrl
, pars
->ssl
, ver
, PCAP_ERR_SETSAMPLING
,
2346 errmsgbuf
, errbuf
) == -1)
2348 // That failed; log a message and give up.
2349 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2353 // Check if all the data has been read; if not, discard the data in excess
2354 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
2363 daemon_msg_stats_req(uint8 ver
, struct daemon_slpars
*pars
,
2364 struct session
*session
, uint32 plen
, struct pcap_stat
*stats
,
2365 unsigned int svrcapt
)
2367 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2368 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
2369 char sendbuf
[RPCAP_NETBUF_SIZE
]; // temporary buffer in which data to be sent is buffered
2370 int sendbufidx
= 0; // index which keeps the number of bytes currently buffered
2371 struct rpcap_stats
*netstats
; // statistics sent on the network
2373 // Checks that the header does not contain other data; if so, discard it
2374 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
2380 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
,
2381 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
2384 rpcap_createhdr((struct rpcap_header
*) sendbuf
, ver
,
2385 RPCAP_MSG_STATS_REPLY
, 0, (uint16
) sizeof(struct rpcap_stats
));
2387 netstats
= (struct rpcap_stats
*) &sendbuf
[sendbufidx
];
2389 if (sock_bufferize(NULL
, sizeof(struct rpcap_stats
), NULL
,
2390 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
2393 if (session
&& session
->fp
)
2395 if (pcap_stats(session
->fp
, stats
) == -1)
2397 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "%s", pcap_geterr(session
->fp
));
2401 netstats
->ifdrop
= htonl(stats
->ps_ifdrop
);
2402 netstats
->ifrecv
= htonl(stats
->ps_recv
);
2403 netstats
->krnldrop
= htonl(stats
->ps_drop
);
2404 netstats
->svrcapt
= htonl(session
->TotCapt
);
2408 // We have to keep compatibility with old applications,
2409 // which ask for statistics also when the capture has
2411 netstats
->ifdrop
= htonl(stats
->ps_ifdrop
);
2412 netstats
->ifrecv
= htonl(stats
->ps_recv
);
2413 netstats
->krnldrop
= htonl(stats
->ps_drop
);
2414 netstats
->svrcapt
= htonl(svrcapt
);
2418 if (sock_send(pars
->sockctrl
, pars
->ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2420 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2427 rpcap_senderror(pars
->sockctrl
, pars
->ssl
, ver
, PCAP_ERR_GETSTATS
,
2433 static unsigned __stdcall
2437 daemon_thrdatamain(void *ptr
)
2439 char errbuf
[PCAP_ERRBUF_SIZE
+ 1]; // error buffer
2440 struct session
*session
; // pointer to the struct session for this session
2441 int retval
; // general variable used to keep the return value of other functions
2442 struct rpcap_pkthdr
*net_pkt_header
;// header of the packet
2443 struct pcap_pkthdr
*pkt_header
; // pointer to the buffer that contains the header of the current packet
2444 u_char
*pkt_data
; // pointer to the buffer that contains the current packet
2445 size_t sendbufsize
; // size for the send buffer
2446 char *sendbuf
; // temporary buffer in which data to be sent is buffered
2447 int sendbufidx
; // index which keeps the number of bytes currently buffered
2450 sigset_t sigusr1
; // signal set with just SIGUSR1
2453 session
= (struct session
*) ptr
;
2455 session
->TotCapt
= 0; // counter which is incremented each time a packet is received
2457 // Initialize errbuf
2458 memset(errbuf
, 0, sizeof(errbuf
));
2461 // We need a buffer large enough to hold a buffer large enough
2462 // for a maximum-size packet for this pcap_t.
2464 if (pcap_snapshot(session
->fp
) < 0)
2467 // The snapshot length is negative.
2468 // This "should not happen".
2470 rpcapd_log(LOGPRIO_ERROR
,
2471 "Unable to allocate the buffer for this child thread: snapshot length of %d is negative",
2472 pcap_snapshot(session
->fp
));
2473 sendbuf
= NULL
; // we can't allocate a buffer, so nothing to free
2477 // size_t is unsigned, and the result of pcap_snapshot() is signed;
2478 // on no platform that we support is int larger than size_t.
2479 // This means that, unless the extra information we prepend to
2480 // a maximum-sized packet is impossibly large, the sum of the
2481 // snapshot length and the size of that extra information will
2484 // So we don't need to make sure that sendbufsize will overflow.
2486 // However, we *do* need to make sure its value fits in an int,
2487 // because sock_send() can't send more than INT_MAX bytes (it could
2488 // do so on 64-bit UN*Xes, but can't do so on Windows, not even
2489 // 64-bit Windows, as the send() buffer size argument is an int
2492 sendbufsize
= sizeof(struct rpcap_header
) + sizeof(struct rpcap_pkthdr
) + pcap_snapshot(session
->fp
);
2493 if (sendbufsize
> INT_MAX
)
2495 rpcapd_log(LOGPRIO_ERROR
,
2496 "Buffer size for this child thread would be larger than %d",
2498 sendbuf
= NULL
; // we haven't allocated a buffer, so nothing to free
2501 sendbuf
= (char *) malloc (sendbufsize
);
2502 if (sendbuf
== NULL
)
2504 rpcapd_log(LOGPRIO_ERROR
,
2505 "Unable to allocate the buffer for this child thread");
2511 // Set the signal set to include just SIGUSR1, and block that
2512 // signal; we only want it unblocked when we're reading
2513 // packets - we dn't want any other system calls, such as
2514 // ones being used to send to the client or to log messages,
2515 // to be interrupted.
2517 sigemptyset(&sigusr1
);
2518 sigaddset(&sigusr1
, SIGUSR1
);
2519 pthread_sigmask(SIG_BLOCK
, &sigusr1
, NULL
);
2522 // Retrieve the packets
2527 // Unblock SIGUSR1 while we might be waiting for packets.
2529 pthread_sigmask(SIG_UNBLOCK
, &sigusr1
, NULL
);
2531 retval
= pcap_next_ex(session
->fp
, &pkt_header
, (const u_char
**) &pkt_data
); // cast to avoid a compiler warning
2534 // Now block it again.
2536 pthread_sigmask(SIG_BLOCK
, &sigusr1
, NULL
);
2540 if (retval
== 0) // Read timeout elapsed
2545 // Bufferize the general header
2546 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
,
2547 &sendbufidx
, (int)sendbufsize
, SOCKBUF_CHECKONLY
, errbuf
,
2548 PCAP_ERRBUF_SIZE
) == -1)
2550 rpcapd_log(LOGPRIO_ERROR
,
2551 "sock_bufferize() error sending packet message: %s",
2556 rpcap_createhdr((struct rpcap_header
*) sendbuf
,
2557 session
->protocol_version
, RPCAP_MSG_PACKET
, 0,
2558 (uint16
) (sizeof(struct rpcap_pkthdr
) + pkt_header
->caplen
));
2560 net_pkt_header
= (struct rpcap_pkthdr
*) &sendbuf
[sendbufidx
];
2562 // Bufferize the pkt header
2563 if (sock_bufferize(NULL
, sizeof(struct rpcap_pkthdr
), NULL
,
2564 &sendbufidx
, (int)sendbufsize
, SOCKBUF_CHECKONLY
, errbuf
,
2565 PCAP_ERRBUF_SIZE
) == -1)
2567 rpcapd_log(LOGPRIO_ERROR
,
2568 "sock_bufferize() error sending packet message: %s",
2573 net_pkt_header
->caplen
= htonl(pkt_header
->caplen
);
2574 net_pkt_header
->len
= htonl(pkt_header
->len
);
2575 net_pkt_header
->npkt
= htonl(++(session
->TotCapt
));
2577 // This protocol needs to be updated with a new version
2578 // before 2038-01-19 03:14:07 UTC.
2580 net_pkt_header
->timestamp_sec
= htonl((uint32
)pkt_header
->ts
.tv_sec
);
2581 net_pkt_header
->timestamp_usec
= htonl((uint32
)pkt_header
->ts
.tv_usec
);
2583 // Bufferize the pkt data
2584 if (sock_bufferize((char *) pkt_data
, pkt_header
->caplen
,
2585 sendbuf
, &sendbufidx
, (int)sendbufsize
, SOCKBUF_BUFFERIZE
,
2586 errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2588 rpcapd_log(LOGPRIO_ERROR
,
2589 "sock_bufferize() error sending packet message: %s",
2595 // If the client dropped the connection, don't report an
2596 // error, just quit.
2597 status
= sock_send(session
->sockdata
, session
->data_ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
);
2603 // Error other than "client closed the
2604 // connection out from under us"; report
2607 rpcapd_log(LOGPRIO_ERROR
,
2608 "Send of packet to client failed: %s",
2613 // Give up in either case.
2619 if (retval
< 0 && retval
!= PCAP_ERROR_BREAK
)
2622 // Failed with an error other than "we were told to break
2623 // out of the loop".
2625 // The latter just means that the client told us to stop
2626 // capturing, so there's no error to report.
2628 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Error reading the packets: %s", pcap_geterr(session
->fp
));
2629 rpcap_senderror(session
->sockctrl
, session
->ctrl_ssl
, session
->protocol_version
,
2630 PCAP_ERR_READEX
, errbuf
, NULL
);
2635 // The main thread will clean up the session structure.
2644 // Do-nothing handler for SIGUSR1; the sole purpose of SIGUSR1 is to
2645 // interrupt the data thread if it's blocked in a system call waiting
2646 // for packets to arrive.
2648 static void noop_handler(int sign _U_
)
2654 \brief It serializes a network address.
2656 It accepts a 'sockaddr_storage' structure as input, and it converts it appropriately into a format
2657 that can be used to be sent on the network. Basically, it applies all the hton()
2658 conversion required to the input variable.
2660 \param sockaddrin a 'sockaddr_storage' pointer to the variable that has to be
2661 serialized. This variable can be both a 'sockaddr_in' and 'sockaddr_in6'.
2663 \param sockaddrout an 'rpcap_sockaddr' pointer to the variable that will contain
2664 the serialized data. This variable has to be allocated by the user.
2666 \warning This function supports only AF_INET and AF_INET6 address families.
2669 daemon_seraddr(struct sockaddr_storage
*sockaddrin
, struct rpcap_sockaddr
*sockaddrout
)
2671 memset(sockaddrout
, 0, sizeof(struct sockaddr_storage
));
2673 // There can be the case in which the sockaddrin is not available
2674 if (sockaddrin
== NULL
) return;
2676 // Warning: we support only AF_INET and AF_INET6
2677 switch (sockaddrin
->ss_family
)
2681 struct sockaddr_in
*sockaddrin_ipv4
;
2682 struct rpcap_sockaddr_in
*sockaddrout_ipv4
;
2684 sockaddrin_ipv4
= (struct sockaddr_in
*) sockaddrin
;
2685 sockaddrout_ipv4
= (struct rpcap_sockaddr_in
*) sockaddrout
;
2686 sockaddrout_ipv4
->family
= htons(RPCAP_AF_INET
);
2687 sockaddrout_ipv4
->port
= htons(sockaddrin_ipv4
->sin_port
);
2688 memcpy(&sockaddrout_ipv4
->addr
, &sockaddrin_ipv4
->sin_addr
, sizeof(sockaddrout_ipv4
->addr
));
2689 memset(sockaddrout_ipv4
->zero
, 0, sizeof(sockaddrout_ipv4
->zero
));
2696 struct sockaddr_in6
*sockaddrin_ipv6
;
2697 struct rpcap_sockaddr_in6
*sockaddrout_ipv6
;
2699 sockaddrin_ipv6
= (struct sockaddr_in6
*) sockaddrin
;
2700 sockaddrout_ipv6
= (struct rpcap_sockaddr_in6
*) sockaddrout
;
2701 sockaddrout_ipv6
->family
= htons(RPCAP_AF_INET6
);
2702 sockaddrout_ipv6
->port
= htons(sockaddrin_ipv6
->sin6_port
);
2703 sockaddrout_ipv6
->flowinfo
= htonl(sockaddrin_ipv6
->sin6_flowinfo
);
2704 memcpy(&sockaddrout_ipv6
->addr
, &sockaddrin_ipv6
->sin6_addr
, sizeof(sockaddrout_ipv6
->addr
));
2705 sockaddrout_ipv6
->scope_id
= htonl(sockaddrin_ipv6
->sin6_scope_id
);
2714 \brief Suspends a thread for secs seconds.
2716 void sleep_secs(int secs
)
2721 unsigned secs_remaining
;
2725 secs_remaining
= secs
;
2726 while (secs_remaining
!= 0)
2727 secs_remaining
= sleep(secs_remaining
);
2732 * Read the header of a message.
2735 rpcapd_recv_msg_header(SOCKET sock
, SSL
*ssl
, struct rpcap_header
*headerp
)
2738 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2740 nread
= sock_recv(sock
, ssl
, (char *) headerp
, sizeof(struct rpcap_header
),
2741 SOCK_RECEIVEALL_YES
|SOCK_EOF_ISNT_ERROR
, errbuf
, PCAP_ERRBUF_SIZE
);
2745 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
2750 // Immediate EOF; that's treated like a close message.
2753 headerp
->plen
= ntohl(headerp
->plen
);
2758 * Read data from a message.
2759 * If we're trying to read more data that remains, puts an error
2760 * message into errmsgbuf and returns -2. Otherwise, tries to read
2761 * the data and, if that succeeds, subtracts the amount read from
2762 * the number of bytes of data that remains.
2763 * Returns 0 on success, logs a message and returns -1 on a network
2767 rpcapd_recv(SOCKET sock
, SSL
*ssl
, char *buffer
, size_t toread
, uint32
*plen
, char *errmsgbuf
)
2770 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2774 // Tell the client and continue.
2775 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Message payload is too short");
2778 nread
= sock_recv(sock
, ssl
, buffer
, toread
,
2779 SOCK_RECEIVEALL_YES
|SOCK_EOF_IS_ERROR
, errbuf
, PCAP_ERRBUF_SIZE
);
2782 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
2790 * Discard data from a connection.
2791 * Mostly used to discard wrong-sized messages.
2792 * Returns 0 on success, logs a message and returns -1 on a network
2796 rpcapd_discard(SOCKET sock
, SSL
*ssl
, uint32 len
)
2798 char errbuf
[PCAP_ERRBUF_SIZE
+ 1]; // keeps the error string, prior to be printed
2802 if (sock_discard(sock
, ssl
, len
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2805 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
2813 // Shut down any packet-capture thread associated with the session,
2814 // close the SSL handle for the data socket if we have one, close
2815 // the data socket if we have one, and close the underlying packet
2816 // capture handle if we have one.
2818 // We do not, of course, touch the controlling socket that's also
2819 // copied into the session, as the service loop might still use it.
2821 static void session_close(struct session
*session
)
2823 if (session
->have_thread
)
2826 // Tell the data connection thread main capture loop to
2827 // break out of that loop.
2829 // This may be sufficient to wake up a blocked thread,
2830 // but it's not guaranteed to be sufficient.
2832 pcap_breakloop(session
->fp
);
2836 // Set the event on which a read would block, so that,
2837 // if it's currently blocked waiting for packets to
2838 // arrive, it'll wake up, so it can see the "break
2839 // out of the loop" indication. (pcap_breakloop()
2840 // might do this, but older versions don't. Setting
2841 // it twice should, at worst, cause an extra wakeup,
2842 // which shouldn't be a problem.)
2844 // XXX - what about modules other than NPF?
2846 SetEvent(pcap_getevent(session
->fp
));
2849 // Wait for the thread to exit, so we don't close
2850 // sockets out from under it.
2852 // XXX - have a timeout, so we don't wait forever?
2854 WaitForSingleObject(session
->thread
, INFINITE
);
2857 // Release the thread handle, as we're done with
2860 CloseHandle(session
->thread
);
2861 session
->have_thread
= 0;
2862 session
->thread
= INVALID_HANDLE_VALUE
;
2865 // Send a SIGUSR1 signal to the thread, so that, if
2866 // it's currently blocked waiting for packets to arrive,
2867 // it'll wake up (we've turned off SA_RESTART for
2868 // SIGUSR1, so that the system call in which it's blocked
2869 // should return EINTR rather than restarting).
2871 pthread_kill(session
->thread
, SIGUSR1
);
2874 // Wait for the thread to exit, so we don't close
2875 // sockets out from under it.
2877 // XXX - have a timeout, so we don't wait forever?
2879 pthread_join(session
->thread
, NULL
);
2880 session
->have_thread
= 0;
2881 memset(&session
->thread
, 0, sizeof(session
->thread
));
2886 if (session
->data_ssl
)
2888 // Finish using the SSL handle for the socket.
2889 // This must be done *before* the socket is closed.
2890 ssl_finish(session
->data_ssl
);
2891 session
->data_ssl
= NULL
;
2895 if (session
->sockdata
!= INVALID_SOCKET
)
2897 sock_close(session
->sockdata
, NULL
, 0);
2898 session
->sockdata
= INVALID_SOCKET
;
2903 pcap_close(session
->fp
);