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>
73 #define RPCAP_TIMEOUT_INIT 90 /* Initial timeout for RPCAP connections (default: 90 sec) */
74 #define RPCAP_TIMEOUT_RUNTIME 180 /* Run-time timeout for RPCAP connections (default: 3 min) */
75 #define RPCAP_SUSPEND_WRONGAUTH 1 /* If the authentication is wrong, stops 1 sec before accepting a new auth message */
77 // Parameters for the service loop.
80 SOCKET sockctrl
; //!< SOCKET ID of the control connection
81 SSL
*ssl
; //!< Optional SSL handler for the controlling sockets
82 uint8 protocol_version
; //!< negotiated protocol version
83 int isactive
; //!< Not null if the daemon has to run in active mode
84 int nullAuthAllowed
; //!< '1' if we permit NULL authentication, '0' otherwise
88 // Data for a session managed by a thread.
89 // It includes both a Boolean indicating whether we *have* a thread,
90 // and a platform-dependent (UN*X vs. Windows) identifier for the
91 // thread; on Windows, we could use an invalid handle to indicate
92 // that we don't have a thread, but there *is* no portable "no thread"
93 // value for a pthread_t on UN*X.
98 SSL
*ctrl_ssl
, *data_ssl
; // optional SSL handlers for sockctrl and sockdata.
99 uint8 protocol_version
;
101 unsigned int TotCapt
;
110 // Locally defined functions
111 static int daemon_msg_err(SOCKET sockctrl
, SSL
*, uint32 plen
);
112 static int daemon_msg_auth_req(struct daemon_slpars
*pars
, uint32 plen
);
113 static int daemon_AuthUserPwd(char *username
, char *password
, char *errbuf
);
115 static int daemon_msg_findallif_req(struct daemon_slpars
*pars
, uint32 plen
);
117 static int daemon_msg_open_req(struct daemon_slpars
*pars
, uint32 plen
, char *source
, size_t sourcelen
);
118 static int daemon_msg_startcap_req(struct daemon_slpars
*pars
, uint32 plen
, char *source
, struct session
**sessionp
, struct rpcap_sampling
*samp_param
, int uses_ssl
);
119 static int daemon_msg_endcap_req(struct daemon_slpars
*pars
, struct session
*session
);
121 static int daemon_msg_updatefilter_req(struct daemon_slpars
*pars
, struct session
*session
, uint32 plen
);
122 static int daemon_unpackapplyfilter(SOCKET sockctrl
, SSL
*, struct session
*session
, uint32
*plenp
, char *errbuf
);
124 static int daemon_msg_stats_req(struct daemon_slpars
*pars
, struct session
*session
, uint32 plen
, struct pcap_stat
*stats
, unsigned int svrcapt
);
126 static int daemon_msg_setsampling_req(struct daemon_slpars
*pars
, uint32 plen
, struct rpcap_sampling
*samp_param
);
128 static void daemon_seraddr(struct sockaddr_storage
*sockaddrin
, struct rpcap_sockaddr
*sockaddrout
);
130 static unsigned __stdcall
daemon_thrdatamain(void *ptr
);
132 static void *daemon_thrdatamain(void *ptr
);
133 static void noop_handler(int sign
);
136 static int rpcapd_recv_msg_header(SOCKET sock
, SSL
*, struct rpcap_header
*headerp
);
137 static int rpcapd_recv(SOCKET sock
, SSL
*, char *buffer
, size_t toread
, uint32
*plen
, char *errmsgbuf
);
138 static int rpcapd_discard(SOCKET sock
, SSL
*, uint32 len
);
139 static void session_close(struct session
*);
142 // TLS record layer header; used when processing the first message from
143 // the client, in case we aren't doing TLS but they are.
145 struct tls_record_header
{
146 uint8 type
; // ContentType - will be 22, for Handshake
147 uint8 version_major
; // TLS protocol major version
148 uint8 version_injor
; // TLS protocol minor version
149 // This is *not* aligned on a 2-byte boundary; we just
150 // declare it as two bytes. Don't assume any particular
151 // compiler's mechanism for saying "packed"!
152 uint8 length_hi
; // Upper 8 bits of payload length
153 uint8 length_lo
; // Low 8 bits of payload length
156 #define TLS_RECORD_HEADER_LEN 5 // Don't use sizeof in case it's padded
158 #define TLS_RECORD_TYPE_ALERT 21
159 #define TLS_RECORD_TYPE_HANDSHAKE 22
162 // TLS alert message.
166 uint8 alert_description
;
169 #define TLS_ALERT_LEN 2
171 #define TLS_ALERT_LEVEL_FATAL 2
172 #define TLS_ALERT_HANDSHAKE_FAILURE 40
175 daemon_serviceloop(SOCKET sockctrl
, int isactive
, char *passiveClients
,
176 int nullAuthAllowed
, int uses_ssl
)
179 struct tls_record_header tls_header
;
180 struct tls_alert tls_alert
;
181 struct daemon_slpars pars
; // service loop parameters
182 char errbuf
[PCAP_ERRBUF_SIZE
+ 1]; // keeps the error string, prior to be printed
183 char errmsgbuf
[PCAP_ERRBUF_SIZE
+ 1]; // buffer for errors to send to the client
184 int host_port_check_status
;
187 struct rpcap_header header
; // RPCAP message general header
188 uint32 plen
; // payload length from header
189 int authenticated
= 0; // 1 if the client has successfully authenticated
190 char source
[PCAP_BUF_SIZE
+1]; // keeps the string that contains the interface to open
191 int got_source
= 0; // 1 if we've gotten the source from an open request
193 struct sigaction action
;
195 struct session
*session
= NULL
; // struct session main variable
196 const char *msg_type_string
; // string for message type
197 int client_told_us_to_close
= 0; // 1 if the client told us to close the capture
199 // needed to save the values of the statistics
200 struct pcap_stat stats
;
201 unsigned int svrcapt
;
203 struct rpcap_sampling samp_param
; // in case sampling has been requested
205 // Structures needed for the select() call
206 fd_set rfds
; // set of socket descriptors we have to check
207 struct timeval tv
; // maximum time the select() can block waiting for data
208 int retval
; // select() return value
210 *errbuf
= 0; // Initialize errbuf
213 // Peek into the socket to determine whether the client sent us
214 // a TLS handshake message or a non-TLS rpcapd message.
216 // The first byte of an rpcapd request is the version number;
217 // the first byte of a TLS handshake message is 22. We
218 // permanently reserve an rpcapd version number of 22, so that
219 // we can distinguish between rpcapd messages and TLS handshake
220 // messages; if we ever get to rpcapd version 21, and want to
221 // introduce a new version after that, we'll jump to version 23.
223 nrecv
= sock_recv(sockctrl
, NULL
, (char *)&first_octet
, 1,
224 SOCK_EOF_ISNT_ERROR
|SOCK_MSG_PEEK
, errbuf
, PCAP_ERRBUF_SIZE
);
228 rpcapd_log(LOGPRIO_ERROR
, "Peek from client failed: %s", errbuf
);
233 // Client closed the connection.
239 // We have to upgrade to TLS as soon as possible, so that the
240 // whole protocol goes through the encrypted tunnel, including
241 // early error messages.
243 // Even in active mode, the other end has to initiate the TLS
244 // handshake as we still are the server as far as TLS is concerned,
245 // so we don't check isactive.
250 // We're expecting a TLS handshake message. If this
251 // isn't one, assume it's a non-TLS rpcapd message.
253 // The first octet of a TLS handshake is
254 // TLS_RECORD_TYPE_HANDSHAKE.
256 if (first_octet
!= TLS_RECORD_TYPE_HANDSHAKE
)
259 // We assume this is a non-TLS rpcapd message.
261 // Read the message header from the client.
263 nrecv
= rpcapd_recv_msg_header(sockctrl
, NULL
, &header
);
271 // Client closed the connection.
276 // Discard the rest of the message.
277 if (rpcapd_discard(sockctrl
, NULL
, plen
) == -1)
284 // Send an authentication error, indicating
285 // that we require TLS.
287 if (rpcap_senderror(sockctrl
, NULL
, header
.ver
,
288 PCAP_ERR_AUTH
, "TLS is required by this server",
291 // That failed; log a message and give up.
292 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
296 // Shut the session down.
299 ssl
= ssl_promotion(1, sockctrl
, errbuf
, PCAP_ERRBUF_SIZE
);
302 rpcapd_log(LOGPRIO_ERROR
, "TLS handshake on control connection failed: %s",
311 // We're expecting a non-TLS rpcapd message. If this
312 // looks, instead, like a TLS handshake message, send
313 // a TLS handshake_failed alert.
315 // The first octet of a TLS handshake is
316 // TLS_RECORD_TYPE_HANDSHAKE.
318 if (first_octet
== TLS_RECORD_TYPE_HANDSHAKE
)
322 // Read the record header.
324 nrecv
= sock_recv(sockctrl
, ssl
, (char *) &tls_header
,
325 sizeof tls_header
, SOCK_RECEIVEALL_YES
|SOCK_EOF_ISNT_ERROR
,
326 errbuf
, PCAP_ERRBUF_SIZE
);
330 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
338 plen
= (tls_header
.length_hi
<< 8) | tls_header
.length_lo
;
340 // Discard the rest of the message.
341 if (rpcapd_discard(sockctrl
, NULL
, plen
) == -1)
348 // Send a TLS handshake failure alert.
349 // Use the same version the client sent us.
351 tls_header
.type
= TLS_RECORD_TYPE_ALERT
;
352 tls_header
.length_hi
= 0;
353 tls_header
.length_lo
= TLS_ALERT_LEN
;
355 if (sock_send(sockctrl
, NULL
, (char *) &tls_header
,
356 TLS_RECORD_HEADER_LEN
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
358 // That failed; log a messsage and give up.
359 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
363 tls_alert
.alert_level
= TLS_ALERT_LEVEL_FATAL
;
364 tls_alert
.alert_description
= TLS_ALERT_HANDSHAKE_FAILURE
;
365 if (sock_send(sockctrl
, NULL
, (char *) &tls_alert
,
366 TLS_ALERT_LEN
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
368 // That failed; log a messsage and give up.
369 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
379 // Set parameters structure
380 pars
.sockctrl
= sockctrl
;
382 pars
.protocol_version
= 0; // not yet known
383 pars
.isactive
= isactive
; // active mode
384 pars
.nullAuthAllowed
= nullAuthAllowed
;
387 // We have a connection.
389 // If it's a passive mode connection, check whether the connecting
390 // host is among the ones allowed.
392 // In either case, we were handed a copy of the host list; free it
393 // as soon as we're done with it.
398 free(passiveClients
);
399 passiveClients
= NULL
;
403 struct sockaddr_storage from
;
407 // Get the address of the other end of the connection.
409 fromlen
= sizeof(struct sockaddr_storage
);
410 if (getpeername(pars
.sockctrl
, (struct sockaddr
*)&from
,
413 sock_geterror("getpeername(): ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
414 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
, 0, PCAP_ERR_NETW
, errmsgbuf
, errbuf
) == -1)
415 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
420 // Are they in the list of host/port combinations we allow?
422 host_port_check_status
= sock_check_hostlist(passiveClients
, RPCAP_HOSTLIST_SEP
, &from
, errmsgbuf
, PCAP_ERRBUF_SIZE
);
423 free(passiveClients
);
424 passiveClients
= NULL
;
425 if (host_port_check_status
< 0)
427 if (host_port_check_status
== -2) {
429 // We got an error; log it.
431 rpcapd_log(LOGPRIO_ERROR
, "%s", errmsgbuf
);
435 // Sorry, we can't let you in.
437 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
, 0, PCAP_ERR_HOSTNOAUTH
, errmsgbuf
, errbuf
) == -1)
438 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
445 // Catch SIGUSR1, but do nothing. We use it to interrupt the
446 // capture thread to break it out of a loop in which it's
447 // blocked waiting for packets to arrive.
449 // We don't want interrupted system calls to restart, so that
450 // the read routine for the pcap_t gets EINTR rather than
451 // restarting if it's blocked in a system call.
453 memset(&action
, 0, sizeof (action
));
454 action
.sa_handler
= noop_handler
;
456 sigemptyset(&action
.sa_mask
);
457 sigaction(SIGUSR1
, &action
, NULL
);
461 // The client must first authenticate; loop until they send us a
462 // message with a version we support and credentials we accept,
463 // they send us a close message indicating that they're giving up,
464 // or we get a network error or other fatal error.
466 while (!authenticated
)
469 // If we're in active mode, we have to check for the
472 // XXX - do this on *every* trip through the loop?
477 // We do not have to block here
478 tv
.tv_sec
= RPCAP_TIMEOUT_INIT
;
481 FD_SET(pars
.sockctrl
, &rfds
);
483 retval
= select((int)pars
.sockctrl
+ 1, &rfds
, NULL
, NULL
, &tv
);
486 sock_geterror("select failed: ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
487 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
, 0, PCAP_ERR_NETW
, errmsgbuf
, errbuf
) == -1)
488 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
492 // The timeout has expired
493 // So, this was a fake connection. Drop it down
496 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
, 0, PCAP_ERR_INITTIMEOUT
, "The RPCAP initial timeout has expired", errbuf
) == -1)
497 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
503 // Read the message header from the client.
505 nrecv
= rpcapd_recv_msg_header(pars
.sockctrl
, pars
.ssl
, &header
);
513 // Client closed the connection.
520 // Did the client specify a version we can handle?
522 if (!RPCAP_VERSION_IS_SUPPORTED(header
.ver
))
525 // Tell them it's not a valid protocol version.
530 // If RPCAP_MIN_VERSION is 0, no version is too
531 // old, as the oldest supported version is 0,
532 // and there are no negative versions.
534 #if RPCAP_MIN_VERSION != 0
535 if (header
.ver
< RPCAP_MIN_VERSION
)
538 // Their maximum version is too old;
539 // there *is* no version we can both
540 // handle, and they might reject
541 // an error with a version they don't
542 // understand, so reply with the
543 // version they sent. That may
544 // make them retry with that version,
545 // but they'll give up on that
548 reply_version
= header
.ver
;
554 // Their maximum version is too new,
555 // but they might be able to handle
556 // *our* maximum version, so reply
557 // with that version.
559 reply_version
= RPCAP_MAX_VERSION
;
561 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
, reply_version
,
562 PCAP_ERR_WRONGVER
, "RPCAP version number mismatch",
565 // That failed; log a message and give up.
566 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
570 // Discard the rest of the message.
571 if (rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
) == -1)
577 // Let them try again.
582 // OK, we use the version the client specified.
584 pars
.protocol_version
= header
.ver
;
588 case RPCAP_MSG_AUTH_REQ
:
589 retval
= daemon_msg_auth_req(&pars
, plen
);
592 // Fatal error; a message has
593 // been logged, so just give up.
598 // Non-fatal error; we sent back
599 // an error message, so let them
604 // OK, we're authenticated; we sent back
605 // a reply, so start serving requests.
609 case RPCAP_MSG_CLOSE
:
611 // The client is giving up.
612 // Discard the rest of the message, if
613 // there is anything more.
615 (void)rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
);
616 // We're done with this client.
619 case RPCAP_MSG_ERROR
:
620 // Log this and close the connection?
621 // XXX - is this what happens in active
622 // mode, where *we* initiate the
623 // connection, and the client gives us
624 // an error message rather than a "let
625 // me log in" message, indicating that
626 // we're not allowed to connect to them?
627 (void)daemon_msg_err(pars
.sockctrl
, pars
.ssl
, plen
);
630 case RPCAP_MSG_FINDALLIF_REQ
:
631 case RPCAP_MSG_OPEN_REQ
:
632 case RPCAP_MSG_STARTCAP_REQ
:
633 case RPCAP_MSG_UPDATEFILTER_REQ
:
634 case RPCAP_MSG_STATS_REQ
:
635 case RPCAP_MSG_ENDCAP_REQ
:
636 case RPCAP_MSG_SETSAMPLING_REQ
:
638 // These requests can't be sent until
639 // the client is authenticated.
641 msg_type_string
= rpcap_msg_type_string(header
.type
);
642 if (msg_type_string
!= NULL
)
644 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "%s request sent before authentication was completed", msg_type_string
);
648 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Message of type %u sent before authentication was completed", header
.type
);
650 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
651 pars
.protocol_version
, PCAP_ERR_WRONGMSG
,
652 errmsgbuf
, errbuf
) == -1)
654 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
657 // Discard the rest of the message.
658 if (rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
) == -1)
665 case RPCAP_MSG_PACKET
:
666 case RPCAP_MSG_FINDALLIF_REPLY
:
667 case RPCAP_MSG_OPEN_REPLY
:
668 case RPCAP_MSG_STARTCAP_REPLY
:
669 case RPCAP_MSG_UPDATEFILTER_REPLY
:
670 case RPCAP_MSG_AUTH_REPLY
:
671 case RPCAP_MSG_STATS_REPLY
:
672 case RPCAP_MSG_ENDCAP_REPLY
:
673 case RPCAP_MSG_SETSAMPLING_REPLY
:
675 // These are server-to-client messages.
677 msg_type_string
= rpcap_msg_type_string(header
.type
);
678 if (msg_type_string
!= NULL
)
680 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Server-to-client message %s received from client", msg_type_string
);
684 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Server-to-client message of type %u received from client", header
.type
);
686 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
687 pars
.protocol_version
, PCAP_ERR_WRONGMSG
,
688 errmsgbuf
, errbuf
) == -1)
690 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
693 // Discard the rest of the message.
694 if (rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
) == -1)
703 // Unknown message type.
705 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Unknown message type %u", header
.type
);
706 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
707 pars
.protocol_version
, PCAP_ERR_WRONGMSG
,
708 errmsgbuf
, errbuf
) == -1)
710 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
713 // Discard the rest of the message.
714 if (rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
) == -1)
724 // OK, the client has authenticated itself, and we can start
725 // processing regular requests from it.
729 // We don't have any statistics yet.
741 errbuf
[0] = 0; // clear errbuf
743 // Avoid zombies connections; check if the connection is opens but no commands are performed
744 // from more than RPCAP_TIMEOUT_RUNTIME
746 // - I have to be in normal mode (no active mode)
747 // - if the device is open, I don't have to be in the middle of a capture (session->sockdata)
748 // - if the device is closed, I have always to check if a new command arrives
750 // Be carefully: the capture can have been started, but an error occurred (so session != NULL, but
752 if ((!pars
.isactive
) && ((session
== NULL
) || ((session
!= NULL
) && (session
->sockdata
== 0))))
754 // Check for the initial timeout
756 // We do not have to block here
757 tv
.tv_sec
= RPCAP_TIMEOUT_RUNTIME
;
760 FD_SET(pars
.sockctrl
, &rfds
);
762 retval
= select((int)pars
.sockctrl
+ 1, &rfds
, NULL
, NULL
, &tv
);
765 sock_geterror("select failed: ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
766 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
767 pars
.protocol_version
, PCAP_ERR_NETW
,
768 errmsgbuf
, errbuf
) == -1)
769 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
773 // The timeout has expired
774 // So, this was a fake connection. Drop it down
777 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
778 pars
.protocol_version
,
779 PCAP_ERR_INITTIMEOUT
,
780 "The RPCAP initial timeout has expired",
782 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
788 // Read the message header from the client.
790 nrecv
= rpcapd_recv_msg_header(pars
.sockctrl
, pars
.ssl
, &header
);
798 // Client closed the connection.
805 // Did the client specify the version we negotiated?
807 // For now, there's only one version.
809 if (header
.ver
!= pars
.protocol_version
)
812 // Tell them it's not the negotiated version.
813 // Send the error message with their version,
814 // so they don't reject it as having the wrong
817 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
818 header
.ver
, PCAP_ERR_WRONGVER
,
819 "RPCAP version in message isn't the negotiated version",
822 // That failed; log a message and give up.
823 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
827 // Discard the rest of the message.
828 (void)rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
);
835 case RPCAP_MSG_ERROR
: // The other endpoint reported an error
837 (void)daemon_msg_err(pars
.sockctrl
, pars
.ssl
, plen
);
838 // Do nothing; just exit; the error code is already into the errbuf
839 // XXX - actually exit....
843 case RPCAP_MSG_FINDALLIF_REQ
:
845 if (daemon_msg_findallif_req(&pars
, plen
) == -1)
847 // Fatal error; a message has
848 // been logged, so just give up.
854 case RPCAP_MSG_OPEN_REQ
:
857 // Process the open request, and keep
858 // the source from it, for use later
859 // when the capture is started.
861 // XXX - we don't care if the client sends
862 // us multiple open requests, the last
865 retval
= daemon_msg_open_req(&pars
, plen
, source
, sizeof(source
));
868 // Fatal error; a message has
869 // been logged, so just give up.
876 case RPCAP_MSG_STARTCAP_REQ
:
880 // They never told us what device
882 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
883 pars
.protocol_version
,
884 PCAP_ERR_STARTCAPTURE
,
885 "No capture device was specified",
888 // Fatal error; log an
889 // error and give up.
890 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
893 if (rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
) == -1)
900 if (daemon_msg_startcap_req(&pars
, plen
, source
, &session
, &samp_param
, uses_ssl
) == -1)
902 // Fatal error; a message has
903 // been logged, so just give up.
909 case RPCAP_MSG_UPDATEFILTER_REQ
:
913 if (daemon_msg_updatefilter_req(&pars
, session
, plen
) == -1)
915 // Fatal error; a message has
916 // been logged, so just give up.
922 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
923 pars
.protocol_version
,
924 PCAP_ERR_UPDATEFILTER
,
925 "Device not opened. Cannot update filter",
928 // That failed; log a message and give up.
929 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
936 case RPCAP_MSG_CLOSE
: // The other endpoint close the pcap session
939 // Indicate to our caller that the client
940 // closed the control connection.
941 // This is used only in case of active mode.
943 client_told_us_to_close
= 1;
944 rpcapd_log(LOGPRIO_DEBUG
, "The other end system asked to close the connection.");
948 case RPCAP_MSG_STATS_REQ
:
950 if (daemon_msg_stats_req(&pars
, session
, plen
, &stats
, svrcapt
) == -1)
952 // Fatal error; a message has
953 // been logged, so just give up.
959 case RPCAP_MSG_ENDCAP_REQ
: // The other endpoint close the current capture session
963 // Save statistics (we can need them in the future)
964 if (pcap_stats(session
->fp
, &stats
))
966 svrcapt
= session
->TotCapt
;
976 if (daemon_msg_endcap_req(&pars
, session
) == -1)
980 // Fatal error; a message has
981 // been logged, so just give up.
989 rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
990 pars
.protocol_version
,
992 "Device not opened. Cannot close the capture",
998 case RPCAP_MSG_SETSAMPLING_REQ
:
1000 if (daemon_msg_setsampling_req(&pars
, plen
, &samp_param
) == -1)
1002 // Fatal error; a message has
1003 // been logged, so just give up.
1009 case RPCAP_MSG_AUTH_REQ
:
1012 // We're already authenticated; you don't
1013 // get to reauthenticate.
1015 rpcapd_log(LOGPRIO_INFO
, "The client sent an RPCAP_MSG_AUTH_REQ message after authentication was completed");
1016 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
1017 pars
.protocol_version
,
1019 "RPCAP_MSG_AUTH_REQ request sent after authentication was completed",
1022 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1025 // Discard the rest of the message.
1026 if (rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
) == -1)
1033 case RPCAP_MSG_PACKET
:
1034 case RPCAP_MSG_FINDALLIF_REPLY
:
1035 case RPCAP_MSG_OPEN_REPLY
:
1036 case RPCAP_MSG_STARTCAP_REPLY
:
1037 case RPCAP_MSG_UPDATEFILTER_REPLY
:
1038 case RPCAP_MSG_AUTH_REPLY
:
1039 case RPCAP_MSG_STATS_REPLY
:
1040 case RPCAP_MSG_ENDCAP_REPLY
:
1041 case RPCAP_MSG_SETSAMPLING_REPLY
:
1043 // These are server-to-client messages.
1045 msg_type_string
= rpcap_msg_type_string(header
.type
);
1046 if (msg_type_string
!= NULL
)
1048 rpcapd_log(LOGPRIO_INFO
, "The client sent a %s server-to-client message", msg_type_string
);
1049 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Server-to-client message %s received from client", msg_type_string
);
1053 rpcapd_log(LOGPRIO_INFO
, "The client sent a server-to-client message of type %u", header
.type
);
1054 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Server-to-client message of type %u received from client", header
.type
);
1056 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
1057 pars
.protocol_version
, PCAP_ERR_WRONGMSG
,
1058 errmsgbuf
, errbuf
) == -1)
1060 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1063 // Discard the rest of the message.
1064 if (rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
) == -1)
1073 // Unknown message type.
1075 rpcapd_log(LOGPRIO_INFO
, "The client sent a message of type %u", header
.type
);
1076 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Unknown message type %u", header
.type
);
1077 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
1078 pars
.protocol_version
, PCAP_ERR_WRONGMSG
,
1079 errbuf
, errmsgbuf
) == -1)
1081 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1084 // Discard the rest of the message.
1085 if (rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
) == -1)
1096 // The service loop is finishing up.
1097 // If we have a capture session running, close it.
1100 session_close(session
);
1106 // Finish using the SSL handle for the control socket, if we
1107 // have an SSL connection, and close the control socket.
1112 // Finish using the SSL handle for the socket.
1113 // This must be done *before* the socket is closed.
1117 sock_close(sockctrl
, NULL
, 0);
1119 // Print message and return
1120 rpcapd_log(LOGPRIO_DEBUG
, "I'm exiting from the child loop");
1122 return client_told_us_to_close
;
1126 * This handles the RPCAP_MSG_ERR message.
1129 daemon_msg_err(SOCKET sockctrl
, SSL
*ssl
, uint32 plen
)
1131 char errbuf
[PCAP_ERRBUF_SIZE
];
1132 char remote_errbuf
[PCAP_ERRBUF_SIZE
];
1134 if (plen
>= PCAP_ERRBUF_SIZE
)
1137 * Message is too long; just read as much of it as we
1138 * can into the buffer provided, and discard the rest.
1140 if (sock_recv(sockctrl
, ssl
, remote_errbuf
, PCAP_ERRBUF_SIZE
- 1,
1141 SOCK_RECEIVEALL_YES
|SOCK_EOF_IS_ERROR
, errbuf
,
1142 PCAP_ERRBUF_SIZE
) == -1)
1145 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
1148 if (rpcapd_discard(sockctrl
, ssl
, plen
- (PCAP_ERRBUF_SIZE
- 1)) == -1)
1155 * Null-terminate it.
1157 remote_errbuf
[PCAP_ERRBUF_SIZE
- 1] = '\0';
1161 /* Empty error string. */
1162 remote_errbuf
[0] = '\0';
1166 if (sock_recv(sockctrl
, ssl
, remote_errbuf
, plen
,
1167 SOCK_RECEIVEALL_YES
|SOCK_EOF_IS_ERROR
, errbuf
,
1168 PCAP_ERRBUF_SIZE
) == -1)
1171 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
1176 * Null-terminate it.
1178 remote_errbuf
[plen
] = '\0';
1181 rpcapd_log(LOGPRIO_ERROR
, "Error from client: %s", remote_errbuf
);
1186 * This handles the RPCAP_MSG_AUTH_REQ message.
1187 * It checks if the authentication credentials supplied by the user are valid.
1189 * This function is called if the daemon receives a RPCAP_MSG_AUTH_REQ
1190 * message in its authentication loop. It reads the body of the
1191 * authentication message from the network and checks whether the
1192 * credentials are valid.
1194 * \param sockctrl: the socket for the control connection.
1196 * \param nullAuthAllowed: '1' if the NULL authentication is allowed.
1198 * \param errbuf: a user-allocated buffer in which the error message
1199 * (if one) has to be written. It must be at least PCAP_ERRBUF_SIZE
1202 * \return '0' if everything is fine, '-1' if an unrecoverable error occurred,
1203 * or '-2' if the authentication failed. For errors, an error message is
1204 * returned in the 'errbuf' variable; this gives a message for the
1205 * unrecoverable error or for the authentication failure.
1208 daemon_msg_auth_req(struct daemon_slpars
*pars
, uint32 plen
)
1210 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
1211 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
1212 struct rpcap_header header
; // RPCAP message general header
1214 struct rpcap_auth auth
; // RPCAP authentication header
1216 status
= rpcapd_recv(pars
->sockctrl
, pars
->ssl
, (char *) &auth
, sizeof(struct rpcap_auth
), &plen
, errmsgbuf
);
1226 switch (ntohs(auth
.type
))
1228 case RPCAP_RMTAUTH_NULL
:
1230 if (!pars
->nullAuthAllowed
)
1232 // Send the client an error reply.
1233 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Authentication failed; NULL authentication not permitted.");
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 pars
->protocol_version
,
1299 PCAP_ERR_AUTH
, errmsgbuf
, errbuf
) == -1)
1301 // That failed; log a message and give up.
1302 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1307 // Suspend for 1 second, so that they can't
1308 // hammer us with repeated tries with an
1309 // attack such as a dictionary attack.
1311 // WARNING: this delay is inserted only
1312 // at this point; if the client closes the
1313 // connection and reconnects, the suspension
1314 // time does not have any effect.
1316 sleep_secs(RPCAP_SUSPEND_WRONGAUTH
);
1326 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Authentication type not recognized.");
1330 // The authentication succeeded; let the client know.
1331 rpcap_createhdr(&header
, pars
->protocol_version
, RPCAP_MSG_AUTH_REPLY
, 0, 0);
1333 // Send the ok message back
1334 if (sock_send(pars
->sockctrl
, pars
->ssl
, (char *) &header
, sizeof (struct rpcap_header
), errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1336 // That failed; log a messsage and give up.
1337 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1341 // Check if all the data has been read; if not, discard the data in excess
1342 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
1350 if (rpcap_senderror(pars
->sockctrl
, pars
->ssl
, pars
->protocol_version
,
1351 PCAP_ERR_AUTH
, errmsgbuf
, errbuf
) == -1)
1353 // That failed; log a message and give up.
1354 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1359 // Check if all the data has been read; if not, discard the data in excess
1360 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
1369 daemon_AuthUserPwd(char *username
, char *password
, char *errbuf
)
1373 * Warning: the user which launches the process must have the
1374 * SE_TCB_NAME right.
1375 * This corresponds to have the "Act as part of the Operating System"
1376 * turned on (administrative tools, local security settings, local
1377 * policies, user right assignment)
1378 * However, it seems to me that if you run it as a service, this
1379 * right should be provided by default.
1382 if (LogonUser(username
, ".", password
, LOGON32_LOGON_NETWORK
, LOGON32_PROVIDER_DEFAULT
, &Token
) == 0)
1384 pcap_win32_err_to_str(GetLastError(), errbuf
);
1388 // This call should change the current thread to the selected user.
1389 // I didn't test it.
1390 if (ImpersonateLoggedOnUser(Token
) == 0)
1392 pcap_win32_err_to_str(GetLastError(), errbuf
);
1404 * http://www.unixpapa.com/incnote/passwd.html
1406 * We use the Solaris/Linux shadow password authentication if
1407 * we have getspnam(), otherwise we just do traditional
1408 * authentication, which, on some platforms, might work, even
1409 * with shadow passwords, if we're running as root. Traditional
1410 * authenticaion won't work if we're not running as root, as
1411 * I think these days all UN*Xes either won't return the password
1412 * at all with getpwnam() or will only do so if you're root.
1414 * XXX - perhaps what we *should* be using is PAM, if we have
1415 * it. That might hide all the details of username/password
1416 * authentication, whether it's done with a visible-to-root-
1417 * only password database or some other authentication mechanism,
1420 struct passwd
*user
;
1421 char *user_password
;
1422 #ifdef HAVE_GETSPNAM
1423 struct spwd
*usersp
;
1426 // This call is needed to get the uid
1427 if ((user
= getpwnam(username
)) == NULL
)
1429 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Authentication failed: no such user");
1433 #ifdef HAVE_GETSPNAM
1434 // This call is needed to get the password; otherwise 'x' is returned
1435 if ((usersp
= getspnam(username
)) == NULL
)
1437 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Authentication failed: no such user");
1440 user_password
= usersp
->sp_pwdp
;
1443 * XXX - what about other platforms?
1444 * The unixpapa.com page claims this Just Works on *BSD if you're
1445 * running as root - it's from 2000, so it doesn't indicate whether
1446 * macOS (which didn't come out until 2001, under the name Mac OS
1447 * X) behaves like the *BSDs or not, and might also work on AIX.
1448 * HP-UX does something else.
1450 * Again, hopefully PAM hides all that.
1452 user_password
= user
->pw_passwd
;
1455 if (strcmp(user_password
, (char *) crypt(password
, user_password
)) != 0)
1457 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Authentication failed: password incorrect");
1461 if (setuid(user
->pw_uid
))
1463 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1468 /* if (setgid(user->pw_gid))
1470 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1482 daemon_msg_findallif_req(struct daemon_slpars
*pars
, uint32 plen
)
1484 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
1485 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
1486 char sendbuf
[RPCAP_NETBUF_SIZE
]; // temporary buffer in which data to be sent is buffered
1487 int sendbufidx
= 0; // index which keeps the number of bytes currently buffered
1488 pcap_if_t
*alldevs
= NULL
; // pointer to the header of the interface chain
1489 pcap_if_t
*d
; // temp pointer needed to scan the interface chain
1490 struct pcap_addr
*address
; // pcap structure that keeps a network address of an interface
1491 struct rpcap_findalldevs_if
*findalldevs_if
;// rpcap structure that packet all the data of an interface together
1492 uint16 nif
= 0; // counts the number of interface listed
1494 // Discard the rest of the message; there shouldn't be any payload.
1495 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
1501 // Retrieve the device list
1502 if (pcap_findalldevs(&alldevs
, errmsgbuf
) == -1)
1505 if (alldevs
== NULL
)
1507 if (rpcap_senderror(pars
->sockctrl
, pars
->ssl
, pars
->protocol_version
,
1508 PCAP_ERR_NOREMOTEIF
,
1509 "No interfaces found! Make sure libpcap/WinPcap is properly installed"
1510 " and you have the right to access to the remote device.",
1513 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1519 // checks the number of interfaces and it computes the total length of the payload
1520 for (d
= alldevs
; d
!= NULL
; d
= d
->next
)
1525 plen
+= strlen(d
->description
);
1527 plen
+= strlen(d
->name
);
1529 plen
+= sizeof(struct rpcap_findalldevs_if
);
1531 for (address
= d
->addresses
; address
!= NULL
; address
= address
->next
)
1534 * Send only IPv4 and IPv6 addresses over the wire.
1536 switch (address
->addr
->sa_family
)
1542 plen
+= (sizeof(struct rpcap_sockaddr
) * 4);
1551 // RPCAP findalldevs command
1552 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
,
1553 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
,
1554 PCAP_ERRBUF_SIZE
) == -1)
1557 rpcap_createhdr((struct rpcap_header
*) sendbuf
, pars
->protocol_version
,
1558 RPCAP_MSG_FINDALLIF_REPLY
, nif
, plen
);
1560 // send the interface list
1561 for (d
= alldevs
; d
!= NULL
; d
= d
->next
)
1563 uint16 lname
, ldescr
;
1565 findalldevs_if
= (struct rpcap_findalldevs_if
*) &sendbuf
[sendbufidx
];
1567 if (sock_bufferize(NULL
, sizeof(struct rpcap_findalldevs_if
), NULL
,
1568 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1571 memset(findalldevs_if
, 0, sizeof(struct rpcap_findalldevs_if
));
1573 if (d
->description
) ldescr
= (short) strlen(d
->description
);
1575 if (d
->name
) lname
= (short) strlen(d
->name
);
1578 findalldevs_if
->desclen
= htons(ldescr
);
1579 findalldevs_if
->namelen
= htons(lname
);
1580 findalldevs_if
->flags
= htonl(d
->flags
);
1582 for (address
= d
->addresses
; address
!= NULL
; address
= address
->next
)
1585 * Send only IPv4 and IPv6 addresses over the wire.
1587 switch (address
->addr
->sa_family
)
1593 findalldevs_if
->naddr
++;
1600 findalldevs_if
->naddr
= htons(findalldevs_if
->naddr
);
1602 if (sock_bufferize(d
->name
, lname
, sendbuf
, &sendbufidx
,
1603 RPCAP_NETBUF_SIZE
, SOCKBUF_BUFFERIZE
, errmsgbuf
,
1604 PCAP_ERRBUF_SIZE
) == -1)
1607 if (sock_bufferize(d
->description
, ldescr
, sendbuf
, &sendbufidx
,
1608 RPCAP_NETBUF_SIZE
, SOCKBUF_BUFFERIZE
, errmsgbuf
,
1609 PCAP_ERRBUF_SIZE
) == -1)
1612 // send all addresses
1613 for (address
= d
->addresses
; address
!= NULL
; address
= address
->next
)
1615 struct rpcap_sockaddr
*sockaddr
;
1618 * Send only IPv4 and IPv6 addresses over the wire.
1620 switch (address
->addr
->sa_family
)
1626 sockaddr
= (struct rpcap_sockaddr
*) &sendbuf
[sendbufidx
];
1627 if (sock_bufferize(NULL
, sizeof(struct rpcap_sockaddr
), NULL
,
1628 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1630 daemon_seraddr((struct sockaddr_storage
*) address
->addr
, sockaddr
);
1632 sockaddr
= (struct rpcap_sockaddr
*) &sendbuf
[sendbufidx
];
1633 if (sock_bufferize(NULL
, sizeof(struct rpcap_sockaddr
), NULL
,
1634 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1636 daemon_seraddr((struct sockaddr_storage
*) address
->netmask
, sockaddr
);
1638 sockaddr
= (struct rpcap_sockaddr
*) &sendbuf
[sendbufidx
];
1639 if (sock_bufferize(NULL
, sizeof(struct rpcap_sockaddr
), NULL
,
1640 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1642 daemon_seraddr((struct sockaddr_storage
*) address
->broadaddr
, sockaddr
);
1644 sockaddr
= (struct rpcap_sockaddr
*) &sendbuf
[sendbufidx
];
1645 if (sock_bufferize(NULL
, sizeof(struct rpcap_sockaddr
), NULL
,
1646 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1648 daemon_seraddr((struct sockaddr_storage
*) address
->dstaddr
, sockaddr
);
1657 // We no longer need the device list. Free it.
1658 pcap_freealldevs(alldevs
);
1660 // Send a final command that says "now send it!"
1661 if (sock_send(pars
->sockctrl
, pars
->ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1663 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1671 pcap_freealldevs(alldevs
);
1673 if (rpcap_senderror(pars
->sockctrl
, pars
->ssl
, pars
->protocol_version
,
1674 PCAP_ERR_FINDALLIF
, errmsgbuf
, errbuf
) == -1)
1676 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1683 \param plen: the length of the current message (needed in order to be able
1684 to discard excess data in the message, if present)
1687 daemon_msg_open_req(struct daemon_slpars
*pars
, uint32 plen
, char *source
, size_t sourcelen
)
1689 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
1690 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
1691 pcap_t
*fp
; // pcap_t main variable
1693 char sendbuf
[RPCAP_NETBUF_SIZE
]; // temporary buffer in which data to be sent is buffered
1694 int sendbufidx
= 0; // index which keeps the number of bytes currently buffered
1695 struct rpcap_openreply
*openreply
; // open reply message
1697 if (plen
> sourcelen
- 1)
1699 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Source string too long");
1703 nread
= sock_recv(pars
->sockctrl
, pars
->ssl
, source
, plen
,
1704 SOCK_RECEIVEALL_YES
|SOCK_EOF_IS_ERROR
, errbuf
, PCAP_ERRBUF_SIZE
);
1707 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
1710 source
[nread
] = '\0';
1713 // XXX - make sure it's *not* a URL; we don't support opening
1714 // remote devices here.
1716 // Open the selected device
1717 // This is a fake open, since we do that only to get the needed parameters, then we close the device again
1718 if ((fp
= pcap_open_live(source
,
1719 1500 /* fake snaplen */,
1721 1000 /* fake timeout */,
1722 errmsgbuf
)) == NULL
)
1725 // Now, I can send a RPCAP open reply message
1726 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
, &sendbufidx
,
1727 RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1730 rpcap_createhdr((struct rpcap_header
*) sendbuf
, pars
->protocol_version
,
1731 RPCAP_MSG_OPEN_REPLY
, 0, sizeof(struct rpcap_openreply
));
1733 openreply
= (struct rpcap_openreply
*) &sendbuf
[sendbufidx
];
1735 if (sock_bufferize(NULL
, sizeof(struct rpcap_openreply
), NULL
, &sendbufidx
,
1736 RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1739 memset(openreply
, 0, sizeof(struct rpcap_openreply
));
1740 openreply
->linktype
= htonl(pcap_datalink(fp
));
1742 * This is always 0 for live captures; we no longer support it
1743 * as something we read from capture files and supply to
1744 * clients, but we have to send it over the wire, as open
1745 * replies are expected to have 8 bytes of payload by
1748 openreply
->tzoff
= 0;
1750 // We're done with the pcap_t.
1754 if (sock_send(pars
->sockctrl
, pars
->ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1756 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1762 if (rpcap_senderror(pars
->sockctrl
, pars
->ssl
, pars
->protocol_version
,
1763 PCAP_ERR_OPEN
, errmsgbuf
, errbuf
) == -1)
1765 // That failed; log a message and give up.
1766 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1770 // Check if all the data has been read; if not, discard the data in excess
1771 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
1779 \param plen: the length of the current message (needed in order to be able
1780 to discard excess data in the message, if present)
1783 daemon_msg_startcap_req(struct daemon_slpars
*pars
, uint32 plen
, char *source
, struct session
**sessionp
, struct rpcap_sampling
*samp_param _U_
, int uses_ssl
)
1785 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
1786 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
1787 char portdata
[PCAP_BUF_SIZE
]; // temp variable needed to derive the data port
1788 char peerhost
[PCAP_BUF_SIZE
]; // temp variable needed to derive the host name of our peer
1789 struct session
*session
= NULL
; // saves state of session
1791 char sendbuf
[RPCAP_NETBUF_SIZE
]; // temporary buffer in which data to be sent is buffered
1792 int sendbufidx
= 0; // index which keeps the number of bytes currently buffered
1794 // socket-related variables
1795 struct addrinfo hints
; // temp, needed to open a socket connection
1796 struct addrinfo
*addrinfo
; // temp, needed to open a socket connection
1797 struct sockaddr_storage saddr
; // temp, needed to retrieve the network data port chosen on the local machine
1798 socklen_t saddrlen
; // temp, needed to retrieve the network data port chosen on the local machine
1799 int ret
; // return value from functions
1801 // RPCAP-related variables
1802 struct rpcap_startcapreq startcapreq
; // start capture request message
1803 struct rpcap_startcapreply
*startcapreply
; // start capture reply message
1804 int serveropen_dp
; // keeps who is going to open the data connection
1808 status
= rpcapd_recv(pars
->sockctrl
, pars
->ssl
, (char *) &startcapreq
,
1809 sizeof(struct rpcap_startcapreq
), &plen
, errmsgbuf
);
1819 startcapreq
.flags
= ntohs(startcapreq
.flags
);
1821 // Check that the client does not ask for UDP is the server has been asked
1822 // to enforce encryption, as SSL is not supported yet with UDP:
1823 if (uses_ssl
&& (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_DGRAM
))
1825 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1826 "SSL not supported with UDP forward of remote packets");
1830 // Create a session structure
1831 session
= malloc(sizeof(struct session
));
1832 if (session
== NULL
)
1834 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Can't allocate session structure");
1838 session
->sockdata
= INVALID_SOCKET
;
1839 session
->ctrl_ssl
= session
->data_ssl
= NULL
;
1840 // We don't have a thread yet.
1841 session
->have_thread
= 0;
1843 // We *shouldn't* have to initialize the thread indicator
1844 // itself, because the compiler *should* realize that we
1845 // only use this if have_thread isn't 0, but we *do* have
1846 // to do it, because not all compilers *do* realize that.
1848 // There is no "invalid thread handle" value for a UN*X
1849 // pthread_t, so we just zero it out.
1852 session
->thread
= INVALID_HANDLE_VALUE
;
1854 memset(&session
->thread
, 0, sizeof(session
->thread
));
1857 // Open the selected device
1858 if ((session
->fp
= pcap_open_live(source
,
1859 ntohl(startcapreq
.snaplen
),
1860 (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_PROMISC
) ? 1 : 0 /* local device, other flags not needed */,
1861 ntohl(startcapreq
.read_timeout
),
1862 errmsgbuf
)) == NULL
)
1866 // Apply sampling parameters
1867 fp
->rmt_samp
.method
= samp_param
->method
;
1868 fp
->rmt_samp
.value
= samp_param
->value
;
1872 We're in active mode if:
1873 - we're using TCP, and the user wants us to be in active mode
1876 serveropen_dp
= (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_SERVEROPEN
) || (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_DGRAM
) || pars
->isactive
;
1879 Gets the sockaddr structure referred to the other peer in the ctrl connection
1881 We need that because:
1882 - if we're in passive mode, we need to know the address family we want to use
1883 (the same used for the ctrl socket)
1884 - if we're in active mode, we need to know the network address of the other host
1885 we want to connect to
1887 saddrlen
= sizeof(struct sockaddr_storage
);
1888 if (getpeername(pars
->sockctrl
, (struct sockaddr
*) &saddr
, &saddrlen
) == -1)
1890 sock_geterror("getpeername(): ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
1894 memset(&hints
, 0, sizeof(struct addrinfo
));
1895 hints
.ai_socktype
= (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_DGRAM
) ? SOCK_DGRAM
: SOCK_STREAM
;
1896 hints
.ai_family
= saddr
.ss_family
;
1898 // Now we have to create a new socket to send packets
1899 if (serveropen_dp
) // Data connection is opened by the server toward the client
1901 pcap_snprintf(portdata
, sizeof portdata
, "%d", ntohs(startcapreq
.portdata
));
1903 // Get the name of the other peer (needed to connect to that specific network address)
1904 if (getnameinfo((struct sockaddr
*) &saddr
, saddrlen
, peerhost
,
1905 sizeof(peerhost
), NULL
, 0, NI_NUMERICHOST
))
1907 sock_geterror("getnameinfo(): ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
1911 if (sock_initaddress(peerhost
, portdata
, &hints
, &addrinfo
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1914 if ((session
->sockdata
= sock_open(addrinfo
, SOCKOPEN_CLIENT
, 0, errmsgbuf
, PCAP_ERRBUF_SIZE
)) == INVALID_SOCKET
)
1917 else // Data connection is opened by the client toward the server
1919 hints
.ai_flags
= AI_PASSIVE
;
1921 // Let's the server socket pick up a free network port for us
1922 if (sock_initaddress(NULL
, "0", &hints
, &addrinfo
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1925 if ((session
->sockdata
= sock_open(addrinfo
, SOCKOPEN_SERVER
, 1 /* max 1 connection in queue */, errmsgbuf
, PCAP_ERRBUF_SIZE
)) == INVALID_SOCKET
)
1928 // get the complete sockaddr structure used in the data connection
1929 saddrlen
= sizeof(struct sockaddr_storage
);
1930 if (getsockname(session
->sockdata
, (struct sockaddr
*) &saddr
, &saddrlen
) == -1)
1932 sock_geterror("getsockname(): ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
1936 // Get the local port the system picked up
1937 if (getnameinfo((struct sockaddr
*) &saddr
, saddrlen
, NULL
,
1938 0, portdata
, sizeof(portdata
), NI_NUMERICSERV
))
1940 sock_geterror("getnameinfo(): ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
1945 // addrinfo is no longer used
1946 freeaddrinfo(addrinfo
);
1949 // Needed to send an error on the ctrl connection
1950 session
->sockctrl
= pars
->sockctrl
;
1951 session
->ctrl_ssl
= pars
->ssl
;
1952 session
->protocol_version
= pars
->protocol_version
;
1954 // Now I can set the filter
1955 ret
= daemon_unpackapplyfilter(pars
->sockctrl
, pars
->ssl
, session
, &plen
, errmsgbuf
);
1958 // Fatal error. A message has been logged; just give up.
1963 // Non-fatal error. Send an error message to the client.
1967 // Now, I can send a RPCAP start capture reply message
1968 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
, &sendbufidx
,
1969 RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1972 rpcap_createhdr((struct rpcap_header
*) sendbuf
, pars
->protocol_version
,
1973 RPCAP_MSG_STARTCAP_REPLY
, 0, sizeof(struct rpcap_startcapreply
));
1975 startcapreply
= (struct rpcap_startcapreply
*) &sendbuf
[sendbufidx
];
1977 if (sock_bufferize(NULL
, sizeof(struct rpcap_startcapreply
), NULL
,
1978 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1981 memset(startcapreply
, 0, sizeof(struct rpcap_startcapreply
));
1982 startcapreply
->bufsize
= htonl(pcap_bufsize(session
->fp
));
1986 unsigned short port
= (unsigned short)strtoul(portdata
,NULL
,10);
1987 startcapreply
->portdata
= htons(port
);
1990 if (sock_send(pars
->sockctrl
, pars
->ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1992 // That failed; log a message and give up.
1993 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1999 SOCKET socktemp
; // We need another socket, since we're going to accept() a connection
2001 // Connection creation
2002 saddrlen
= sizeof(struct sockaddr_storage
);
2004 socktemp
= accept(session
->sockdata
, (struct sockaddr
*) &saddr
, &saddrlen
);
2006 if (socktemp
== INVALID_SOCKET
)
2008 sock_geterror("accept(): ", errbuf
, PCAP_ERRBUF_SIZE
);
2009 rpcapd_log(LOGPRIO_ERROR
, "Accept of data connection failed: %s",
2014 // Now that I accepted the connection, the server socket is no longer needed
2015 sock_close(session
->sockdata
, NULL
, 0);
2016 session
->sockdata
= socktemp
;
2023 /* In both active or passive cases, wait for the client to initiate the
2024 * TLS handshake. Yes during that time the control socket will not be
2025 * served, but the same was true from the above call to accept(). */
2026 ssl
= ssl_promotion(1, session
->sockdata
, errbuf
, PCAP_ERRBUF_SIZE
);
2029 rpcapd_log(LOGPRIO_ERROR
, "TLS handshake failed: %s", errbuf
);
2034 session
->data_ssl
= ssl
;
2036 // Now we have to create a new thread to receive packets
2038 session
->thread
= (HANDLE
)_beginthreadex(NULL
, 0, daemon_thrdatamain
,
2039 (void *) session
, 0, NULL
);
2040 if (session
->thread
== 0)
2042 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Error creating the data thread");
2046 ret
= pthread_create(&session
->thread
, NULL
, daemon_thrdatamain
,
2050 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
2051 ret
, "Error creating the data thread");
2055 session
->have_thread
= 1;
2057 // Check if all the data has been read; if not, discard the data in excess
2058 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
2061 *sessionp
= session
;
2066 // Not a fatal error, so send the client an error message and
2067 // keep serving client requests.
2072 freeaddrinfo(addrinfo
);
2076 session_close(session
);
2080 if (rpcap_senderror(pars
->sockctrl
, pars
->ssl
, pars
->protocol_version
,
2081 PCAP_ERR_STARTCAPTURE
, errmsgbuf
, errbuf
) == -1)
2083 // That failed; log a message and give up.
2084 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2088 // Check if all the data has been read; if not, discard the data in excess
2089 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
2099 // Fatal network error, so don't try to communicate with
2100 // the client, just give up.
2106 session_close(session
);
2114 daemon_msg_endcap_req(struct daemon_slpars
*pars
, struct session
*session
)
2116 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2117 struct rpcap_header header
;
2119 session_close(session
);
2121 rpcap_createhdr(&header
, pars
->protocol_version
,
2122 RPCAP_MSG_ENDCAP_REPLY
, 0, 0);
2124 if (sock_send(pars
->sockctrl
, pars
->ssl
, (char *) &header
, sizeof(struct rpcap_header
), errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2126 // That failed; log a message and give up.
2127 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2135 daemon_unpackapplyfilter(SOCKET sockctrl
, SSL
*ctrl_ssl
, struct session
*session
, uint32
*plenp
, char *errmsgbuf
)
2138 struct rpcap_filter filter
;
2139 struct rpcap_filterbpf_insn insn
;
2140 struct bpf_insn
*bf_insn
;
2141 struct bpf_program bf_prog
;
2144 status
= rpcapd_recv(sockctrl
, ctrl_ssl
, (char *) &filter
,
2145 sizeof(struct rpcap_filter
), plenp
, errmsgbuf
);
2155 bf_prog
.bf_len
= ntohl(filter
.nitems
);
2157 if (ntohs(filter
.filtertype
) != RPCAP_UPDATEFILTER_BPF
)
2159 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Only BPF/NPF filters are currently supported");
2163 bf_insn
= (struct bpf_insn
*) malloc (sizeof(struct bpf_insn
) * bf_prog
.bf_len
);
2164 if (bf_insn
== NULL
)
2166 pcap_fmt_errmsg_for_errno(errmsgbuf
, PCAP_ERRBUF_SIZE
,
2167 errno
, "malloc() failed");
2171 bf_prog
.bf_insns
= bf_insn
;
2173 for (i
= 0; i
< bf_prog
.bf_len
; i
++)
2175 status
= rpcapd_recv(sockctrl
, ctrl_ssl
, (char *) &insn
,
2176 sizeof(struct rpcap_filterbpf_insn
), plenp
, errmsgbuf
);
2186 bf_insn
->code
= ntohs(insn
.code
);
2187 bf_insn
->jf
= insn
.jf
;
2188 bf_insn
->jt
= insn
.jt
;
2189 bf_insn
->k
= ntohl(insn
.k
);
2195 // XXX - pcap_setfilter() should do the validation for us.
2197 if (bpf_validate(bf_prog
.bf_insns
, bf_prog
.bf_len
) == 0)
2199 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "The filter contains bogus instructions");
2203 if (pcap_setfilter(session
->fp
, &bf_prog
))
2205 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "RPCAP error: %s", pcap_geterr(session
->fp
));
2213 daemon_msg_updatefilter_req(struct daemon_slpars
*pars
, struct session
*session
, uint32 plen
)
2215 char errbuf
[PCAP_ERRBUF_SIZE
];
2216 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
2217 int ret
; // status of daemon_unpackapplyfilter()
2218 struct rpcap_header header
; // keeps the answer to the updatefilter command
2220 ret
= daemon_unpackapplyfilter(pars
->sockctrl
, pars
->ssl
, session
, &plen
, errmsgbuf
);
2223 // Fatal error. A message has been logged; just give up.
2228 // Non-fatal error. Send an error reply to the client.
2232 // Check if all the data has been read; if not, discard the data in excess
2233 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
2239 // A response is needed, otherwise the other host does not know that everything went well
2240 rpcap_createhdr(&header
, pars
->protocol_version
,
2241 RPCAP_MSG_UPDATEFILTER_REPLY
, 0, 0);
2243 if (sock_send(pars
->sockctrl
, pars
->ssl
, (char *) &header
, sizeof (struct rpcap_header
), pcap_geterr(session
->fp
), PCAP_ERRBUF_SIZE
))
2245 // That failed; log a messsage and give up.
2246 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2253 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
2257 rpcap_senderror(pars
->sockctrl
, pars
->ssl
, pars
->protocol_version
,
2258 PCAP_ERR_UPDATEFILTER
, errmsgbuf
, NULL
);
2264 \brief Received the sampling parameters from remote host and it stores in the pcap_t structure.
2267 daemon_msg_setsampling_req(struct daemon_slpars
*pars
, uint32 plen
, struct rpcap_sampling
*samp_param
)
2269 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2270 char errmsgbuf
[PCAP_ERRBUF_SIZE
];
2271 struct rpcap_header header
;
2272 struct rpcap_sampling rpcap_samp
;
2275 status
= rpcapd_recv(pars
->sockctrl
, pars
->ssl
, (char *) &rpcap_samp
, sizeof(struct rpcap_sampling
), &plen
, errmsgbuf
);
2285 // Save these settings in the pcap_t
2286 samp_param
->method
= rpcap_samp
.method
;
2287 samp_param
->value
= ntohl(rpcap_samp
.value
);
2289 // A response is needed, otherwise the other host does not know that everything went well
2290 rpcap_createhdr(&header
, pars
->protocol_version
,
2291 RPCAP_MSG_SETSAMPLING_REPLY
, 0, 0);
2293 if (sock_send(pars
->sockctrl
, pars
->ssl
, (char *) &header
, sizeof (struct rpcap_header
), errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2295 // That failed; log a messsage and give up.
2296 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2300 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
2308 if (rpcap_senderror(pars
->sockctrl
, pars
->ssl
, pars
->protocol_version
,
2309 PCAP_ERR_AUTH
, errmsgbuf
, errbuf
) == -1)
2311 // That failed; log a message and give up.
2312 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2316 // Check if all the data has been read; if not, discard the data in excess
2317 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
2326 daemon_msg_stats_req(struct daemon_slpars
*pars
, struct session
*session
, uint32 plen
, struct pcap_stat
*stats
, unsigned int svrcapt
)
2328 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2329 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
2330 char sendbuf
[RPCAP_NETBUF_SIZE
]; // temporary buffer in which data to be sent is buffered
2331 int sendbufidx
= 0; // index which keeps the number of bytes currently buffered
2332 struct rpcap_stats
*netstats
; // statistics sent on the network
2334 // Checks that the header does not contain other data; if so, discard it
2335 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
2341 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
,
2342 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
2345 rpcap_createhdr((struct rpcap_header
*) sendbuf
, pars
->protocol_version
,
2346 RPCAP_MSG_STATS_REPLY
, 0, (uint16
) sizeof(struct rpcap_stats
));
2348 netstats
= (struct rpcap_stats
*) &sendbuf
[sendbufidx
];
2350 if (sock_bufferize(NULL
, sizeof(struct rpcap_stats
), NULL
,
2351 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
2354 if (session
&& session
->fp
)
2356 if (pcap_stats(session
->fp
, stats
) == -1)
2358 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "%s", pcap_geterr(session
->fp
));
2362 netstats
->ifdrop
= htonl(stats
->ps_ifdrop
);
2363 netstats
->ifrecv
= htonl(stats
->ps_recv
);
2364 netstats
->krnldrop
= htonl(stats
->ps_drop
);
2365 netstats
->svrcapt
= htonl(session
->TotCapt
);
2369 // We have to keep compatibility with old applications,
2370 // which ask for statistics also when the capture has
2372 netstats
->ifdrop
= htonl(stats
->ps_ifdrop
);
2373 netstats
->ifrecv
= htonl(stats
->ps_recv
);
2374 netstats
->krnldrop
= htonl(stats
->ps_drop
);
2375 netstats
->svrcapt
= htonl(svrcapt
);
2379 if (sock_send(pars
->sockctrl
, pars
->ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2381 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2388 rpcap_senderror(pars
->sockctrl
, pars
->ssl
, pars
->protocol_version
,
2389 PCAP_ERR_GETSTATS
, errmsgbuf
, NULL
);
2394 static unsigned __stdcall
2398 daemon_thrdatamain(void *ptr
)
2400 char errbuf
[PCAP_ERRBUF_SIZE
+ 1]; // error buffer
2401 struct session
*session
; // pointer to the struct session for this session
2402 int retval
; // general variable used to keep the return value of other functions
2403 struct rpcap_pkthdr
*net_pkt_header
;// header of the packet
2404 struct pcap_pkthdr
*pkt_header
; // pointer to the buffer that contains the header of the current packet
2405 u_char
*pkt_data
; // pointer to the buffer that contains the current packet
2406 size_t sendbufsize
; // size for the send buffer
2407 char *sendbuf
; // temporary buffer in which data to be sent is buffered
2408 int sendbufidx
; // index which keeps the number of bytes currently buffered
2411 sigset_t sigusr1
; // signal set with just SIGUSR1
2414 session
= (struct session
*) ptr
;
2416 session
->TotCapt
= 0; // counter which is incremented each time a packet is received
2418 // Initialize errbuf
2419 memset(errbuf
, 0, sizeof(errbuf
));
2422 // We need a buffer large enough to hold a buffer large enough
2423 // for a maximum-size packet for this pcap_t.
2425 if (pcap_snapshot(session
->fp
) < 0)
2428 // The snapshot length is negative.
2429 // This "should not happen".
2431 rpcapd_log(LOGPRIO_ERROR
,
2432 "Unable to allocate the buffer for this child thread: snapshot length of %d is negative",
2433 pcap_snapshot(session
->fp
));
2434 sendbuf
= NULL
; // we can't allocate a buffer, so nothing to free
2438 // size_t is unsigned, and the result of pcap_snapshot() is signed;
2439 // on no platform that we support is int larger than size_t.
2440 // This means that, unless the extra information we prepend to
2441 // a maximum-sized packet is impossibly large, the sum of the
2442 // snapshot length and the size of that extra information will
2445 // So we don't need to make sure that sendbufsize will overflow.
2447 // However, we *do* need to make sure its value fits in an int,
2448 // because sock_send() can't send more than INT_MAX bytes (it could
2449 // do so on 64-bit UN*Xes, but can't do so on Windows, not even
2450 // 64-bit Windows, as the send() buffer size argument is an int
2453 sendbufsize
= sizeof(struct rpcap_header
) + sizeof(struct rpcap_pkthdr
) + pcap_snapshot(session
->fp
);
2454 if (sendbufsize
> INT_MAX
)
2456 rpcapd_log(LOGPRIO_ERROR
,
2457 "Buffer size for this child thread would be larger than %d",
2459 sendbuf
= NULL
; // we haven't allocated a buffer, so nothing to free
2462 sendbuf
= (char *) malloc (sendbufsize
);
2463 if (sendbuf
== NULL
)
2465 rpcapd_log(LOGPRIO_ERROR
,
2466 "Unable to allocate the buffer for this child thread");
2472 // Set the signal set to include just SIGUSR1, and block that
2473 // signal; we only want it unblocked when we're reading
2474 // packets - we dn't want any other system calls, such as
2475 // ones being used to send to the client or to log messages,
2476 // to be interrupted.
2478 sigemptyset(&sigusr1
);
2479 sigaddset(&sigusr1
, SIGUSR1
);
2480 pthread_sigmask(SIG_BLOCK
, &sigusr1
, NULL
);
2483 // Retrieve the packets
2488 // Unblock SIGUSR1 while we might be waiting for packets.
2490 pthread_sigmask(SIG_UNBLOCK
, &sigusr1
, NULL
);
2492 retval
= pcap_next_ex(session
->fp
, &pkt_header
, (const u_char
**) &pkt_data
); // cast to avoid a compiler warning
2495 // Now block it again.
2497 pthread_sigmask(SIG_BLOCK
, &sigusr1
, NULL
);
2501 if (retval
== 0) // Read timeout elapsed
2506 // Bufferize the general header
2507 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
,
2508 &sendbufidx
, (int)sendbufsize
, SOCKBUF_CHECKONLY
, errbuf
,
2509 PCAP_ERRBUF_SIZE
) == -1)
2511 rpcapd_log(LOGPRIO_ERROR
,
2512 "sock_bufferize() error sending packet message: %s",
2517 rpcap_createhdr((struct rpcap_header
*) sendbuf
,
2518 session
->protocol_version
, RPCAP_MSG_PACKET
, 0,
2519 (uint16
) (sizeof(struct rpcap_pkthdr
) + pkt_header
->caplen
));
2521 net_pkt_header
= (struct rpcap_pkthdr
*) &sendbuf
[sendbufidx
];
2523 // Bufferize the pkt header
2524 if (sock_bufferize(NULL
, sizeof(struct rpcap_pkthdr
), NULL
,
2525 &sendbufidx
, (int)sendbufsize
, SOCKBUF_CHECKONLY
, errbuf
,
2526 PCAP_ERRBUF_SIZE
) == -1)
2528 rpcapd_log(LOGPRIO_ERROR
,
2529 "sock_bufferize() error sending packet message: %s",
2534 net_pkt_header
->caplen
= htonl(pkt_header
->caplen
);
2535 net_pkt_header
->len
= htonl(pkt_header
->len
);
2536 net_pkt_header
->npkt
= htonl(++(session
->TotCapt
));
2538 // This protocol needs to be updated with a new version
2539 // before 2038-01-19 03:14:07 UTC.
2541 net_pkt_header
->timestamp_sec
= htonl((uint32
)pkt_header
->ts
.tv_sec
);
2542 net_pkt_header
->timestamp_usec
= htonl((uint32
)pkt_header
->ts
.tv_usec
);
2544 // Bufferize the pkt data
2545 if (sock_bufferize((char *) pkt_data
, pkt_header
->caplen
,
2546 sendbuf
, &sendbufidx
, (int)sendbufsize
, SOCKBUF_BUFFERIZE
,
2547 errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2549 rpcapd_log(LOGPRIO_ERROR
,
2550 "sock_bufferize() error sending packet message: %s",
2556 // If the client dropped the connection, don't report an
2557 // error, just quit.
2558 status
= sock_send(session
->sockdata
, session
->data_ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
);
2564 // Error other than "client closed the
2565 // connection out from under us"; report
2568 rpcapd_log(LOGPRIO_ERROR
,
2569 "Send of packet to client failed: %s",
2574 // Give up in either case.
2580 if (retval
< 0 && retval
!= PCAP_ERROR_BREAK
)
2583 // Failed with an error other than "we were told to break
2584 // out of the loop".
2586 // The latter just means that the client told us to stop
2587 // capturing, so there's no error to report.
2589 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Error reading the packets: %s", pcap_geterr(session
->fp
));
2590 rpcap_senderror(session
->sockctrl
, session
->ctrl_ssl
, session
->protocol_version
,
2591 PCAP_ERR_READEX
, errbuf
, NULL
);
2596 // The main thread will clean up the session structure.
2605 // Do-nothing handler for SIGUSR1; the sole purpose of SIGUSR1 is to
2606 // interrupt the data thread if it's blocked in a system call waiting
2607 // for packets to arrive.
2609 static void noop_handler(int sign _U_
)
2615 \brief It serializes a network address.
2617 It accepts a 'sockaddr_storage' structure as input, and it converts it appropriately into a format
2618 that can be used to be sent on the network. Basically, it applies all the hton()
2619 conversion required to the input variable.
2621 \param sockaddrin a 'sockaddr_storage' pointer to the variable that has to be
2622 serialized. This variable can be both a 'sockaddr_in' and 'sockaddr_in6'.
2624 \param sockaddrout an 'rpcap_sockaddr' pointer to the variable that will contain
2625 the serialized data. This variable has to be allocated by the user.
2627 \warning This function supports only AF_INET and AF_INET6 address families.
2630 daemon_seraddr(struct sockaddr_storage
*sockaddrin
, struct rpcap_sockaddr
*sockaddrout
)
2632 memset(sockaddrout
, 0, sizeof(struct sockaddr_storage
));
2634 // There can be the case in which the sockaddrin is not available
2635 if (sockaddrin
== NULL
) return;
2637 // Warning: we support only AF_INET and AF_INET6
2638 switch (sockaddrin
->ss_family
)
2642 struct sockaddr_in
*sockaddrin_ipv4
;
2643 struct rpcap_sockaddr_in
*sockaddrout_ipv4
;
2645 sockaddrin_ipv4
= (struct sockaddr_in
*) sockaddrin
;
2646 sockaddrout_ipv4
= (struct rpcap_sockaddr_in
*) sockaddrout
;
2647 sockaddrout_ipv4
->family
= htons(RPCAP_AF_INET
);
2648 sockaddrout_ipv4
->port
= htons(sockaddrin_ipv4
->sin_port
);
2649 memcpy(&sockaddrout_ipv4
->addr
, &sockaddrin_ipv4
->sin_addr
, sizeof(sockaddrout_ipv4
->addr
));
2650 memset(sockaddrout_ipv4
->zero
, 0, sizeof(sockaddrout_ipv4
->zero
));
2657 struct sockaddr_in6
*sockaddrin_ipv6
;
2658 struct rpcap_sockaddr_in6
*sockaddrout_ipv6
;
2660 sockaddrin_ipv6
= (struct sockaddr_in6
*) sockaddrin
;
2661 sockaddrout_ipv6
= (struct rpcap_sockaddr_in6
*) sockaddrout
;
2662 sockaddrout_ipv6
->family
= htons(RPCAP_AF_INET6
);
2663 sockaddrout_ipv6
->port
= htons(sockaddrin_ipv6
->sin6_port
);
2664 sockaddrout_ipv6
->flowinfo
= htonl(sockaddrin_ipv6
->sin6_flowinfo
);
2665 memcpy(&sockaddrout_ipv6
->addr
, &sockaddrin_ipv6
->sin6_addr
, sizeof(sockaddrout_ipv6
->addr
));
2666 sockaddrout_ipv6
->scope_id
= htonl(sockaddrin_ipv6
->sin6_scope_id
);
2675 \brief Suspends a thread for secs seconds.
2677 void sleep_secs(int secs
)
2682 unsigned secs_remaining
;
2686 secs_remaining
= secs
;
2687 while (secs_remaining
!= 0)
2688 secs_remaining
= sleep(secs_remaining
);
2693 * Read the header of a message.
2696 rpcapd_recv_msg_header(SOCKET sock
, SSL
*ssl
, struct rpcap_header
*headerp
)
2699 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2701 nread
= sock_recv(sock
, ssl
, (char *) headerp
, sizeof(struct rpcap_header
),
2702 SOCK_RECEIVEALL_YES
|SOCK_EOF_ISNT_ERROR
, errbuf
, PCAP_ERRBUF_SIZE
);
2706 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
2711 // Immediate EOF; that's treated like a close message.
2714 headerp
->plen
= ntohl(headerp
->plen
);
2719 * Read data from a message.
2720 * If we're trying to read more data that remains, puts an error
2721 * message into errmsgbuf and returns -2. Otherwise, tries to read
2722 * the data and, if that succeeds, subtracts the amount read from
2723 * the number of bytes of data that remains.
2724 * Returns 0 on success, logs a message and returns -1 on a network
2728 rpcapd_recv(SOCKET sock
, SSL
*ssl
, char *buffer
, size_t toread
, uint32
*plen
, char *errmsgbuf
)
2731 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2735 // Tell the client and continue.
2736 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Message payload is too short");
2739 nread
= sock_recv(sock
, ssl
, buffer
, toread
,
2740 SOCK_RECEIVEALL_YES
|SOCK_EOF_IS_ERROR
, errbuf
, PCAP_ERRBUF_SIZE
);
2743 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
2751 * Discard data from a connection.
2752 * Mostly used to discard wrong-sized messages.
2753 * Returns 0 on success, logs a message and returns -1 on a network
2757 rpcapd_discard(SOCKET sock
, SSL
*ssl
, uint32 len
)
2759 char errbuf
[PCAP_ERRBUF_SIZE
+ 1]; // keeps the error string, prior to be printed
2763 if (sock_discard(sock
, ssl
, len
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2766 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
2774 // Shut down any packet-capture thread associated with the session,
2775 // close the SSL handle for the data socket if we have one, close
2776 // the data socket if we have one, and close the underlying packet
2777 // capture handle if we have one.
2779 // We do not, of course, touch the controlling socket that's also
2780 // copied into the session, as the service loop might still use it.
2782 static void session_close(struct session
*session
)
2784 if (session
->have_thread
)
2787 // Tell the data connection thread main capture loop to
2788 // break out of that loop.
2790 // This may be sufficient to wake up a blocked thread,
2791 // but it's not guaranteed to be sufficient.
2793 pcap_breakloop(session
->fp
);
2797 // Set the event on which a read would block, so that,
2798 // if it's currently blocked waiting for packets to
2799 // arrive, it'll wake up, so it can see the "break
2800 // out of the loop" indication. (pcap_breakloop()
2801 // might do this, but older versions don't. Setting
2802 // it twice should, at worst, cause an extra wakeup,
2803 // which shouldn't be a problem.)
2805 // XXX - what about modules other than NPF?
2807 SetEvent(pcap_getevent(session
->fp
));
2810 // Wait for the thread to exit, so we don't close
2811 // sockets out from under it.
2813 // XXX - have a timeout, so we don't wait forever?
2815 WaitForSingleObject(session
->thread
, INFINITE
);
2818 // Release the thread handle, as we're done with
2821 CloseHandle(session
->thread
);
2822 session
->have_thread
= 0;
2823 session
->thread
= INVALID_HANDLE_VALUE
;
2826 // Send a SIGUSR1 signal to the thread, so that, if
2827 // it's currently blocked waiting for packets to arrive,
2828 // it'll wake up (we've turned off SA_RESTART for
2829 // SIGUSR1, so that the system call in which it's blocked
2830 // should return EINTR rather than restarting).
2832 pthread_kill(session
->thread
, SIGUSR1
);
2835 // Wait for the thread to exit, so we don't close
2836 // sockets out from under it.
2838 // XXX - have a timeout, so we don't wait forever?
2840 pthread_join(session
->thread
, NULL
);
2841 session
->have_thread
= 0;
2842 memset(&session
->thread
, 0, sizeof(session
->thread
));
2847 if (session
->data_ssl
)
2849 // Finish using the SSL handle for the socket.
2850 // This must be done *before* the socket is closed.
2851 ssl_finish(session
->data_ssl
);
2852 session
->data_ssl
= NULL
;
2856 if (session
->sockdata
!= INVALID_SOCKET
)
2858 sock_close(session
->sockdata
, NULL
, 0);
2859 session
->sockdata
= INVALID_SOCKET
;
2864 pcap_close(session
->fp
);