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
50 #include <sys/types.h> // for select() and such
51 #include <pwd.h> // for password management
55 #include <shadow.h> // for password management
58 #include <pcap.h> // for libpcap/WinPcap calls
61 #include "sockutils.h" // for socket calls
62 #include "portability.h"
63 #include "rpcap-protocol.h"
68 #include <openssl/ssl.h>
72 #define RPCAP_TIMEOUT_INIT 90 /* Initial timeout for RPCAP connections (default: 90 sec) */
73 #define RPCAP_TIMEOUT_RUNTIME 180 /* Run-time timeout for RPCAP connections (default: 3 min) */
74 #define RPCAP_SUSPEND_WRONGAUTH 1 /* If the authentication is wrong, stops 1 sec before accepting a new auth message */
76 // Parameters for the service loop.
79 SOCKET sockctrl_in
; //!< SOCKET ID of the input side of the control connection
80 SOCKET sockctrl_out
; //!< SOCKET ID of the output side 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.
93 SSL
*ctrl_ssl
, *data_ssl
; // optional SSL handlers for sockctrl_out and sockdata.
94 uint8 protocol_version
;
100 // Structure to refer to a thread.
101 // It includes both a Boolean indicating whether we *have* a thread,
102 // and a platform-dependent (UN*X vs. Windows) identifier for the
103 // thread; on Windows, we could use an invalid handle to indicate
104 // that we don't have a thread, but there *is* no portable "no thread"
105 // value for a pthread_t on UN*X.
107 struct thread_handle
{
116 // Locally defined functions
117 static int daemon_msg_err(SOCKET sockctrl_in
, SSL
*, uint32 plen
);
118 static int daemon_msg_auth_req(struct daemon_slpars
*pars
, uint32 plen
);
119 static int daemon_AuthUserPwd(char *username
, char *password
, char *errbuf
);
121 static int daemon_msg_findallif_req(struct daemon_slpars
*pars
, uint32 plen
);
123 static int daemon_msg_open_req(struct daemon_slpars
*pars
, uint32 plen
, char *source
, size_t sourcelen
);
124 static int daemon_msg_startcap_req(struct daemon_slpars
*pars
, uint32 plen
, struct thread_handle
*threaddata
, char *source
, struct session
**sessionp
, struct rpcap_sampling
*samp_param
, int uses_ssl
);
125 static int daemon_msg_endcap_req(struct daemon_slpars
*pars
, struct session
*session
, struct thread_handle
*threaddata
);
127 static int daemon_msg_updatefilter_req(struct daemon_slpars
*pars
, struct session
*session
, uint32 plen
);
128 static int daemon_unpackapplyfilter(SOCKET sockctrl_in
, SSL
*, struct session
*session
, uint32
*plenp
, char *errbuf
);
130 static int daemon_msg_stats_req(struct daemon_slpars
*pars
, struct session
*session
, uint32 plen
, struct pcap_stat
*stats
, unsigned int svrcapt
);
132 static int daemon_msg_setsampling_req(struct daemon_slpars
*pars
, uint32 plen
, struct rpcap_sampling
*samp_param
);
134 static void daemon_seraddr(struct sockaddr_storage
*sockaddrin
, struct rpcap_sockaddr
*sockaddrout
);
136 static unsigned __stdcall
daemon_thrdatamain(void *ptr
);
138 static void *daemon_thrdatamain(void *ptr
);
141 static int rpcapd_recv_msg_header(SOCKET sock
, SSL
*, struct rpcap_header
*headerp
);
142 static int rpcapd_recv(SOCKET sock
, SSL
*, char *buffer
, size_t toread
, uint32
*plen
, char *errmsgbuf
);
143 static int rpcapd_discard(SOCKET sock
, SSL
*, uint32 len
);
144 static void session_close(struct session
*);
147 daemon_serviceloop(SOCKET sockctrl_in
, SOCKET sockctrl_out
,
148 int isactive
, char *passiveClients
, int nullAuthAllowed
, int uses_ssl
)
150 struct daemon_slpars pars
; // service loop parameters
151 char errbuf
[PCAP_ERRBUF_SIZE
+ 1]; // keeps the error string, prior to be printed
152 char errmsgbuf
[PCAP_ERRBUF_SIZE
+ 1]; // buffer for errors to send to the client
156 struct rpcap_header header
; // RPCAP message general header
157 uint32 plen
; // payload length from header
158 int authenticated
= 0; // 1 if the client has successfully authenticated
159 char source
[PCAP_BUF_SIZE
+1]; // keeps the string that contains the interface to open
160 int got_source
= 0; // 1 if we've gotten the source from an open request
161 struct session
*session
= NULL
; // struct session main variable
162 const char *msg_type_string
; // string for message type
163 int client_told_us_to_close
= 0; // 1 if the client told us to close the capture
165 struct thread_handle threaddata
; // 'read from daemon and send to client' thread
167 // needed to save the values of the statistics
168 struct pcap_stat stats
;
169 unsigned int svrcapt
;
171 struct rpcap_sampling samp_param
; // in case sampling has been requested
173 // Structures needed for the select() call
174 fd_set rfds
; // set of socket descriptors we have to check
175 struct timeval tv
; // maximum time the select() can block waiting for data
176 int retval
; // select() return value
178 // We don't have a thread yet.
179 threaddata
.have_thread
= 0;
181 // We *shouldn't* have to initialize the thread indicator
182 // itself, because the compiler *should* realize that we
183 // only use this if have_thread isn't 0, but we *do* have
184 // to do it, because not all compilers *do* realize that.
186 // There is no "invalid thread handle" value for a UN*X
187 // pthread_t, so we just zero it out.
190 threaddata
.thread
= INVALID_HANDLE_VALUE
;
192 memset(&threaddata
.thread
, 0, sizeof(threaddata
.thread
));
195 *errbuf
= 0; // Initialize errbuf
197 // Set parameters structure
198 pars
.sockctrl_in
= sockctrl_in
;
199 pars
.sockctrl_out
= sockctrl_out
;
200 pars
.protocol_version
= 0; // not yet known
201 pars
.isactive
= isactive
; // active mode
202 pars
.nullAuthAllowed
= nullAuthAllowed
;
206 // We have to upgrade to TLS as soon as possible, so that the
207 // whole protocol goes through the encrypted tunnel, including
208 // early error messages.
210 // Even in active mode, the other end has to initiate the TLS
211 // handshake as we still are the server as far as TLS is concerned,
212 // so we don't check isactive.
216 ssl
= ssl_promotion_rw(1, pars
.sockctrl_in
, pars
.sockctrl_out
, errbuf
, PCAP_ERRBUF_SIZE
);
219 rpcapd_log(LOGPRIO_ERROR
, "TLS handshake on control connection failed: %s",
228 // We have a connection.
230 // If it's a passive mode connection, check whether the connecting
231 // host is among the ones allowed.
233 // In either case, we were handed a copy of the host list; free it
234 // as soon as we're done with it.
239 free(passiveClients
);
240 passiveClients
= NULL
;
244 struct sockaddr_storage from
;
248 // Get the address of the other end of the connection.
250 fromlen
= sizeof(struct sockaddr_storage
);
251 if (getpeername(pars
.sockctrl_in
, (struct sockaddr
*)&from
,
254 sock_geterror("getpeername(): ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
255 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
, 0, PCAP_ERR_NETW
, errmsgbuf
, errbuf
) == -1)
256 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
261 // Are they in the list of host/port combinations we allow?
263 host_port_ok
= (sock_check_hostlist(passiveClients
, RPCAP_HOSTLIST_SEP
, &from
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == 0);
264 free(passiveClients
);
265 passiveClients
= NULL
;
269 // Sorry, you're not on the guest list.
271 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
, 0, PCAP_ERR_HOSTNOAUTH
, errmsgbuf
, errbuf
) == -1)
272 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
278 // The client must first authenticate; loop until they send us a
279 // message with a version we support and credentials we accept,
280 // they send us a close message indicating that they're giving up,
281 // or we get a network error or other fatal error.
283 while (!authenticated
)
286 // If we're in active mode, we have to check for the
289 // XXX - do this on *every* trip through the loop?
294 // We do not have to block here
295 tv
.tv_sec
= RPCAP_TIMEOUT_INIT
;
298 FD_SET(pars
.sockctrl_in
, &rfds
);
300 retval
= select((int)pars
.sockctrl_in
+ 1, &rfds
, NULL
, NULL
, &tv
);
303 sock_geterror("select failed: ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
304 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
, 0, PCAP_ERR_NETW
, errmsgbuf
, errbuf
) == -1)
305 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
309 // The timeout has expired
310 // So, this was a fake connection. Drop it down
313 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
, 0, PCAP_ERR_INITTIMEOUT
, "The RPCAP initial timeout has expired", errbuf
) == -1)
314 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
320 // Read the message header from the client.
322 nrecv
= rpcapd_recv_msg_header(pars
.sockctrl_in
, pars
.ssl
, &header
);
330 // Client closed the connection.
337 // Did the client specify a version we can handle?
339 if (!RPCAP_VERSION_IS_SUPPORTED(header
.ver
))
342 // Tell them it's not a valid protocol version.
347 // If RPCAP_MIN_VERSION is 0, no version is too
348 // old, as the oldest supported version is 0,
349 // and there are no negative versions.
351 #if RPCAP_MIN_VERSION != 0
352 if (header
.ver
< RPCAP_MIN_VERSION
)
355 // Their maximum version is too old;
356 // there *is* no version we can both
357 // handle, and they might reject
358 // an error with a version they don't
359 // understand, so reply with the
360 // version they sent. That may
361 // make them retry with that version,
362 // but they'll give up on that
365 reply_version
= header
.ver
;
371 // Their maximum version is too new,
372 // but they might be able to handle
373 // *our* maximum version, so reply
374 // with that version.
376 reply_version
= RPCAP_MAX_VERSION
;
378 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
, reply_version
,
379 PCAP_ERR_WRONGVER
, "RPCAP version number mismatch",
382 // That failed; log a message and give up.
383 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
387 // Discard the rest of the message.
388 if (rpcapd_discard(pars
.sockctrl_in
, pars
.ssl
, plen
) == -1)
394 // Let them try again.
399 // OK, we use the version the client specified.
401 pars
.protocol_version
= header
.ver
;
405 case RPCAP_MSG_AUTH_REQ
:
406 retval
= daemon_msg_auth_req(&pars
, plen
);
409 // Fatal error; a message has
410 // been logged, so just give up.
415 // Non-fatal error; we sent back
416 // an error message, so let them
421 // OK, we're authenticated; we sent back
422 // a reply, so start serving requests.
426 case RPCAP_MSG_CLOSE
:
428 // The client is giving up.
429 // Discard the rest of the message, if
430 // there is anything more.
432 (void)rpcapd_discard(pars
.sockctrl_in
, pars
.ssl
, plen
);
433 // We're done with this client.
436 case RPCAP_MSG_ERROR
:
437 // Log this and close the connection?
438 // XXX - is this what happens in active
439 // mode, where *we* initiate the
440 // connection, and the client gives us
441 // an error message rather than a "let
442 // me log in" message, indicating that
443 // we're not allowed to connect to them?
444 (void)daemon_msg_err(pars
.sockctrl_in
, pars
.ssl
, plen
);
447 case RPCAP_MSG_FINDALLIF_REQ
:
448 case RPCAP_MSG_OPEN_REQ
:
449 case RPCAP_MSG_STARTCAP_REQ
:
450 case RPCAP_MSG_UPDATEFILTER_REQ
:
451 case RPCAP_MSG_STATS_REQ
:
452 case RPCAP_MSG_ENDCAP_REQ
:
453 case RPCAP_MSG_SETSAMPLING_REQ
:
455 // These requests can't be sent until
456 // the client is authenticated.
458 msg_type_string
= rpcap_msg_type_string(header
.type
);
459 if (msg_type_string
!= NULL
)
461 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "%s request sent before authentication was completed", msg_type_string
);
465 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Message of type %u sent before authentication was completed", header
.type
);
467 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
,
468 pars
.protocol_version
, PCAP_ERR_WRONGMSG
,
469 errmsgbuf
, errbuf
) == -1)
471 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
474 // Discard the rest of the message.
475 if (rpcapd_discard(pars
.sockctrl_in
, pars
.ssl
, plen
) == -1)
482 case RPCAP_MSG_PACKET
:
483 case RPCAP_MSG_FINDALLIF_REPLY
:
484 case RPCAP_MSG_OPEN_REPLY
:
485 case RPCAP_MSG_STARTCAP_REPLY
:
486 case RPCAP_MSG_UPDATEFILTER_REPLY
:
487 case RPCAP_MSG_AUTH_REPLY
:
488 case RPCAP_MSG_STATS_REPLY
:
489 case RPCAP_MSG_ENDCAP_REPLY
:
490 case RPCAP_MSG_SETSAMPLING_REPLY
:
492 // These are server-to-client messages.
494 msg_type_string
= rpcap_msg_type_string(header
.type
);
495 if (msg_type_string
!= NULL
)
497 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Server-to-client message %s received from client", msg_type_string
);
501 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Server-to-client message of type %u received from client", header
.type
);
503 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
,
504 pars
.protocol_version
, PCAP_ERR_WRONGMSG
,
505 errmsgbuf
, errbuf
) == -1)
507 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
510 // Discard the rest of the message.
511 if (rpcapd_discard(pars
.sockctrl_in
, pars
.ssl
, plen
) == -1)
520 // Unknown message type.
522 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Unknown message type %u", header
.type
);
523 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
,
524 pars
.protocol_version
, PCAP_ERR_WRONGMSG
,
525 errmsgbuf
, errbuf
) == -1)
527 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
530 // Discard the rest of the message.
531 if (rpcapd_discard(pars
.sockctrl_in
, pars
.ssl
, plen
) == -1)
541 // OK, the client has authenticated itself, and we can start
542 // processing regular requests from it.
546 // We don't have any statistics yet.
558 errbuf
[0] = 0; // clear errbuf
560 // Avoid zombies connections; check if the connection is opens but no commands are performed
561 // from more than RPCAP_TIMEOUT_RUNTIME
563 // - I have to be in normal mode (no active mode)
564 // - if the device is open, I don't have to be in the middle of a capture (session->sockdata)
565 // - if the device is closed, I have always to check if a new command arrives
567 // Be carefully: the capture can have been started, but an error occurred (so session != NULL, but
569 if ((!pars
.isactive
) && ((session
== NULL
) || ((session
!= NULL
) && (session
->sockdata
== 0))))
571 // Check for the initial timeout
573 // We do not have to block here
574 tv
.tv_sec
= RPCAP_TIMEOUT_RUNTIME
;
577 FD_SET(pars
.sockctrl_in
, &rfds
);
579 retval
= select((int)pars
.sockctrl_in
+ 1, &rfds
, NULL
, NULL
, &tv
);
582 sock_geterror("select failed: ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
583 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
,
584 pars
.protocol_version
, PCAP_ERR_NETW
,
585 errmsgbuf
, errbuf
) == -1)
586 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
590 // The timeout has expired
591 // So, this was a fake connection. Drop it down
594 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
,
595 pars
.protocol_version
,
596 PCAP_ERR_INITTIMEOUT
,
597 "The RPCAP initial timeout has expired",
599 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
605 // Read the message header from the client.
607 nrecv
= rpcapd_recv_msg_header(pars
.sockctrl_in
, pars
.ssl
, &header
);
615 // Client closed the connection.
622 // Did the client specify the version we negotiated?
624 // For now, there's only one version.
626 if (header
.ver
!= pars
.protocol_version
)
629 // Tell them it's not the negotiated version.
630 // Send the error message with their version,
631 // so they don't reject it as having the wrong
634 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
,
635 header
.ver
, PCAP_ERR_WRONGVER
,
636 "RPCAP version in message isn't the negotiated version",
639 // That failed; log a message and give up.
640 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
644 // Discard the rest of the message.
645 (void)rpcapd_discard(pars
.sockctrl_in
, pars
.ssl
, plen
);
652 case RPCAP_MSG_ERROR
: // The other endpoint reported an error
654 (void)daemon_msg_err(pars
.sockctrl_in
, pars
.ssl
, plen
);
655 // Do nothing; just exit; the error code is already into the errbuf
656 // XXX - actually exit....
660 case RPCAP_MSG_FINDALLIF_REQ
:
662 if (daemon_msg_findallif_req(&pars
, plen
) == -1)
664 // Fatal error; a message has
665 // been logged, so just give up.
671 case RPCAP_MSG_OPEN_REQ
:
674 // Process the open request, and keep
675 // the source from it, for use later
676 // when the capture is started.
678 // XXX - we don't care if the client sends
679 // us multiple open requests, the last
682 retval
= daemon_msg_open_req(&pars
, plen
, source
, sizeof(source
));
685 // Fatal error; a message has
686 // been logged, so just give up.
693 case RPCAP_MSG_STARTCAP_REQ
:
697 // They never told us what device
699 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
,
700 pars
.protocol_version
,
701 PCAP_ERR_STARTCAPTURE
,
702 "No capture device was specified",
705 // Fatal error; log an
706 // error and give up.
707 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
710 if (rpcapd_discard(pars
.sockctrl_in
, pars
.ssl
, plen
) == -1)
717 if (daemon_msg_startcap_req(&pars
, plen
, &threaddata
, source
, &session
, &samp_param
, uses_ssl
) == -1)
719 // Fatal error; a message has
720 // been logged, so just give up.
726 case RPCAP_MSG_UPDATEFILTER_REQ
:
730 if (daemon_msg_updatefilter_req(&pars
, session
, plen
) == -1)
732 // Fatal error; a message has
733 // been logged, so just give up.
739 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
,
740 pars
.protocol_version
,
741 PCAP_ERR_UPDATEFILTER
,
742 "Device not opened. Cannot update filter",
745 // That failed; log a message and give up.
746 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
753 case RPCAP_MSG_CLOSE
: // The other endpoint close the pcap session
756 // Indicate to our caller that the client
757 // closed the control connection.
758 // This is used only in case of active mode.
760 client_told_us_to_close
= 1;
761 rpcapd_log(LOGPRIO_DEBUG
, "The other end system asked to close the connection.");
765 case RPCAP_MSG_STATS_REQ
:
767 if (daemon_msg_stats_req(&pars
, session
, plen
, &stats
, svrcapt
) == -1)
769 // Fatal error; a message has
770 // been logged, so just give up.
776 case RPCAP_MSG_ENDCAP_REQ
: // The other endpoint close the current capture session
780 // Save statistics (we can need them in the future)
781 if (pcap_stats(session
->fp
, &stats
))
783 svrcapt
= session
->TotCapt
;
793 if (daemon_msg_endcap_req(&pars
, session
, &threaddata
) == -1)
797 // Fatal error; a message has
798 // been logged, so just give up.
806 rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
,
807 pars
.protocol_version
,
809 "Device not opened. Cannot close the capture",
815 case RPCAP_MSG_SETSAMPLING_REQ
:
817 if (daemon_msg_setsampling_req(&pars
, plen
, &samp_param
) == -1)
819 // Fatal error; a message has
820 // been logged, so just give up.
826 case RPCAP_MSG_AUTH_REQ
:
829 // We're already authenticated; you don't
830 // get to reauthenticate.
832 rpcapd_log(LOGPRIO_INFO
, "The client sent an RPCAP_MSG_AUTH_REQ message after authentication was completed");
833 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
,
834 pars
.protocol_version
,
836 "RPCAP_MSG_AUTH_REQ request sent after authentication was completed",
839 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
842 // Discard the rest of the message.
843 if (rpcapd_discard(pars
.sockctrl_in
, pars
.ssl
, plen
) == -1)
850 case RPCAP_MSG_PACKET
:
851 case RPCAP_MSG_FINDALLIF_REPLY
:
852 case RPCAP_MSG_OPEN_REPLY
:
853 case RPCAP_MSG_STARTCAP_REPLY
:
854 case RPCAP_MSG_UPDATEFILTER_REPLY
:
855 case RPCAP_MSG_AUTH_REPLY
:
856 case RPCAP_MSG_STATS_REPLY
:
857 case RPCAP_MSG_ENDCAP_REPLY
:
858 case RPCAP_MSG_SETSAMPLING_REPLY
:
860 // These are server-to-client messages.
862 msg_type_string
= rpcap_msg_type_string(header
.type
);
863 if (msg_type_string
!= NULL
)
865 rpcapd_log(LOGPRIO_INFO
, "The client sent a %s server-to-client message", msg_type_string
);
866 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Server-to-client message %s received from client", msg_type_string
);
870 rpcapd_log(LOGPRIO_INFO
, "The client sent a server-to-client message of type %u", header
.type
);
871 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Server-to-client message of type %u received from client", header
.type
);
873 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
,
874 pars
.protocol_version
, PCAP_ERR_WRONGMSG
,
875 errmsgbuf
, errbuf
) == -1)
877 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
880 // Discard the rest of the message.
881 if (rpcapd_discard(pars
.sockctrl_in
, pars
.ssl
, plen
) == -1)
890 // Unknown message type.
892 rpcapd_log(LOGPRIO_INFO
, "The client sent a message of type %u", header
.type
);
893 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Unknown message type %u", header
.type
);
894 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
,
895 pars
.protocol_version
, PCAP_ERR_WRONGMSG
,
896 errbuf
, errmsgbuf
) == -1)
898 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
901 // Discard the rest of the message.
902 if (rpcapd_discard(pars
.sockctrl_in
, pars
.ssl
, plen
) == -1)
913 // The child thread is about to end
915 // perform pcap_t cleanup, in case it has not been done
918 if (threaddata
.have_thread
)
922 // Tell the data connection thread main capture
923 // loop to break out of that loop.
925 pcap_breakloop(session
->fp
);
928 // If it's currently blocked waiting for packets
929 // to arrive, try to wake it up, so it can see
930 // the "break out of the loop" indication.
932 SetEvent(pcap_getevent(session
->fp
));
935 // Wait for the thread to exit, so we don't close
936 // sockets out from under it.
938 // XXX - have a timeout, so we don't wait forever?
940 WaitForSingleObject(threaddata
.thread
, INFINITE
);
943 // Release the thread handle, as we're done with
946 CloseHandle(threaddata
.thread
);
948 pthread_cancel(threaddata
.thread
);
950 threaddata
.have_thread
= 0;
953 session_close(session
);
965 // Print message and return
966 rpcapd_log(LOGPRIO_DEBUG
, "I'm exiting from the child loop");
967 rpcapd_log(LOGPRIO_DEBUG
, "%s", errbuf
);
969 return client_told_us_to_close
;
973 * This handles the RPCAP_MSG_ERR message.
976 daemon_msg_err(SOCKET sockctrl_in
, SSL
*ssl
, uint32 plen
)
978 char errbuf
[PCAP_ERRBUF_SIZE
];
979 char remote_errbuf
[PCAP_ERRBUF_SIZE
];
981 if (plen
>= PCAP_ERRBUF_SIZE
)
984 * Message is too long; just read as much of it as we
985 * can into the buffer provided, and discard the rest.
987 if (sock_recv(sockctrl_in
, ssl
, remote_errbuf
, PCAP_ERRBUF_SIZE
- 1,
988 SOCK_RECEIVEALL_YES
|SOCK_EOF_IS_ERROR
, errbuf
,
989 PCAP_ERRBUF_SIZE
) == -1)
992 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
995 if (rpcapd_discard(sockctrl_in
, ssl
, plen
- (PCAP_ERRBUF_SIZE
- 1)) == -1)
1002 * Null-terminate it.
1004 remote_errbuf
[PCAP_ERRBUF_SIZE
- 1] = '\0';
1008 /* Empty error string. */
1009 remote_errbuf
[0] = '\0';
1013 if (sock_recv(sockctrl_in
, ssl
, remote_errbuf
, plen
,
1014 SOCK_RECEIVEALL_YES
|SOCK_EOF_IS_ERROR
, errbuf
,
1015 PCAP_ERRBUF_SIZE
) == -1)
1018 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
1023 * Null-terminate it.
1025 remote_errbuf
[plen
] = '\0';
1028 rpcapd_log(LOGPRIO_ERROR
, "Error from client: %s", remote_errbuf
);
1033 * This handles the RPCAP_MSG_AUTH_REQ message.
1034 * It checks if the authentication credentials supplied by the user are valid.
1036 * This function is called if the daemon receives a RPCAP_MSG_AUTH_REQ
1037 * message in its authentication loop. It reads the body of the
1038 * authentication message from the network and checks whether the
1039 * credentials are valid.
1041 * \param sockctrl: the socket for the control connection.
1043 * \param nullAuthAllowed: '1' if the NULL authentication is allowed.
1045 * \param errbuf: a user-allocated buffer in which the error message
1046 * (if one) has to be written. It must be at least PCAP_ERRBUF_SIZE
1049 * \return '0' if everything is fine, '-1' if an unrecoverable error occurred,
1050 * or '-2' if the authentication failed. For errors, an error message is
1051 * returned in the 'errbuf' variable; this gives a message for the
1052 * unrecoverable error or for the authentication failure.
1055 daemon_msg_auth_req(struct daemon_slpars
*pars
, uint32 plen
)
1057 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
1058 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
1059 struct rpcap_header header
; // RPCAP message general header
1061 struct rpcap_auth auth
; // RPCAP authentication header
1063 status
= rpcapd_recv(pars
->sockctrl_in
, pars
->ssl
, (char *) &auth
, sizeof(struct rpcap_auth
), &plen
, errmsgbuf
);
1073 switch (ntohs(auth
.type
))
1075 case RPCAP_RMTAUTH_NULL
:
1077 if (!pars
->nullAuthAllowed
)
1079 // Send the client an error reply.
1080 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Authentication failed; NULL authentication not permitted.");
1086 case RPCAP_RMTAUTH_PWD
:
1088 char *username
, *passwd
;
1089 uint32 usernamelen
, passwdlen
;
1091 usernamelen
= ntohs(auth
.slen1
);
1092 username
= (char *) malloc (usernamelen
+ 1);
1093 if (username
== NULL
)
1095 pcap_fmt_errmsg_for_errno(errmsgbuf
,
1096 PCAP_ERRBUF_SIZE
, errno
, "malloc() failed");
1099 status
= rpcapd_recv(pars
->sockctrl_in
, pars
->ssl
, username
, usernamelen
, &plen
, errmsgbuf
);
1110 username
[usernamelen
] = '\0';
1112 passwdlen
= ntohs(auth
.slen2
);
1113 passwd
= (char *) malloc (passwdlen
+ 1);
1116 pcap_fmt_errmsg_for_errno(errmsgbuf
,
1117 PCAP_ERRBUF_SIZE
, errno
, "malloc() failed");
1121 status
= rpcapd_recv(pars
->sockctrl_in
, pars
->ssl
, passwd
, passwdlen
, &plen
, errmsgbuf
);
1134 passwd
[passwdlen
] = '\0';
1136 if (daemon_AuthUserPwd(username
, passwd
, errmsgbuf
))
1139 // Authentication failed. Let the client
1144 if (rpcap_senderror(pars
->sockctrl_out
, pars
->ssl
,
1145 pars
->protocol_version
,
1146 PCAP_ERR_AUTH
, errmsgbuf
, errbuf
) == -1)
1148 // That failed; log a message and give up.
1149 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1154 // Suspend for 1 second, so that they can't
1155 // hammer us with repeated tries with an
1156 // attack such as a dictionary attack.
1158 // WARNING: this delay is inserted only
1159 // at this point; if the client closes the
1160 // connection and reconnects, the suspension
1161 // time does not have any effect.
1163 sleep_secs(RPCAP_SUSPEND_WRONGAUTH
);
1173 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Authentication type not recognized.");
1177 // The authentication succeeded; let the client know.
1178 rpcap_createhdr(&header
, pars
->protocol_version
, RPCAP_MSG_AUTH_REPLY
, 0, 0);
1180 // Send the ok message back
1181 if (sock_send(pars
->sockctrl_out
, pars
->ssl
, (char *) &header
, sizeof (struct rpcap_header
), errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1183 // That failed; log a messsage and give up.
1184 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1188 // Check if all the data has been read; if not, discard the data in excess
1189 if (rpcapd_discard(pars
->sockctrl_in
, pars
->ssl
, plen
) == -1)
1197 if (rpcap_senderror(pars
->sockctrl_out
, pars
->ssl
, pars
->protocol_version
,
1198 PCAP_ERR_AUTH
, errmsgbuf
, errbuf
) == -1)
1200 // That failed; log a message and give up.
1201 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1206 // Check if all the data has been read; if not, discard the data in excess
1207 if (rpcapd_discard(pars
->sockctrl_in
, pars
->ssl
, plen
) == -1)
1216 daemon_AuthUserPwd(char *username
, char *password
, char *errbuf
)
1220 * Warning: the user which launches the process must have the
1221 * SE_TCB_NAME right.
1222 * This corresponds to have the "Act as part of the Operating System"
1223 * turned on (administrative tools, local security settings, local
1224 * policies, user right assignment)
1225 * However, it seems to me that if you run it as a service, this
1226 * right should be provided by default.
1229 if (LogonUser(username
, ".", password
, LOGON32_LOGON_NETWORK
, LOGON32_PROVIDER_DEFAULT
, &Token
) == 0)
1231 pcap_win32_err_to_str(GetLastError(), errbuf
);
1235 // This call should change the current thread to the selected user.
1236 // I didn't test it.
1237 if (ImpersonateLoggedOnUser(Token
) == 0)
1239 pcap_win32_err_to_str(GetLastError(), errbuf
);
1251 * http://www.unixpapa.com/incnote/passwd.html
1253 * We use the Solaris/Linux shadow password authentication if
1254 * we have getspnam(), otherwise we just do traditional
1255 * authentication, which, on some platforms, might work, even
1256 * with shadow passwords, if we're running as root. Traditional
1257 * authenticaion won't work if we're not running as root, as
1258 * I think these days all UN*Xes either won't return the password
1259 * at all with getpwnam() or will only do so if you're root.
1261 * XXX - perhaps what we *should* be using is PAM, if we have
1262 * it. That might hide all the details of username/password
1263 * authentication, whether it's done with a visible-to-root-
1264 * only password database or some other authentication mechanism,
1267 struct passwd
*user
;
1268 char *user_password
;
1269 #ifdef HAVE_GETSPNAM
1270 struct spwd
*usersp
;
1273 // This call is needed to get the uid
1274 if ((user
= getpwnam(username
)) == NULL
)
1276 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Authentication failed: no such user");
1280 #ifdef HAVE_GETSPNAM
1281 // This call is needed to get the password; otherwise 'x' is returned
1282 if ((usersp
= getspnam(username
)) == NULL
)
1284 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Authentication failed: no such user");
1287 user_password
= usersp
->sp_pwdp
;
1290 * XXX - what about other platforms?
1291 * The unixpapa.com page claims this Just Works on *BSD if you're
1292 * running as root - it's from 2000, so it doesn't indicate whether
1293 * macOS (which didn't come out until 2001, under the name Mac OS
1294 * X) behaves like the *BSDs or not, and might also work on AIX.
1295 * HP-UX does something else.
1297 * Again, hopefully PAM hides all that.
1299 user_password
= user
->pw_passwd
;
1302 if (strcmp(user_password
, (char *) crypt(password
, user_password
)) != 0)
1304 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Authentication failed: password incorrect");
1308 if (setuid(user
->pw_uid
))
1310 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1315 /* if (setgid(user->pw_gid))
1317 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1329 daemon_msg_findallif_req(struct daemon_slpars
*pars
, uint32 plen
)
1331 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
1332 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
1333 char sendbuf
[RPCAP_NETBUF_SIZE
]; // temporary buffer in which data to be sent is buffered
1334 int sendbufidx
= 0; // index which keeps the number of bytes currently buffered
1335 pcap_if_t
*alldevs
= NULL
; // pointer to the header of the interface chain
1336 pcap_if_t
*d
; // temp pointer needed to scan the interface chain
1337 struct pcap_addr
*address
; // pcap structure that keeps a network address of an interface
1338 struct rpcap_findalldevs_if
*findalldevs_if
;// rpcap structure that packet all the data of an interface together
1339 uint16 nif
= 0; // counts the number of interface listed
1341 // Discard the rest of the message; there shouldn't be any payload.
1342 if (rpcapd_discard(pars
->sockctrl_in
, pars
->ssl
, plen
) == -1)
1348 // Retrieve the device list
1349 if (pcap_findalldevs(&alldevs
, errmsgbuf
) == -1)
1352 if (alldevs
== NULL
)
1354 if (rpcap_senderror(pars
->sockctrl_out
, pars
->ssl
, pars
->protocol_version
,
1355 PCAP_ERR_NOREMOTEIF
,
1356 "No interfaces found! Make sure libpcap/WinPcap is properly installed"
1357 " and you have the right to access to the remote device.",
1360 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1366 // checks the number of interfaces and it computes the total length of the payload
1367 for (d
= alldevs
; d
!= NULL
; d
= d
->next
)
1372 plen
+= strlen(d
->description
);
1374 plen
+= strlen(d
->name
);
1376 plen
+= sizeof(struct rpcap_findalldevs_if
);
1378 for (address
= d
->addresses
; address
!= NULL
; address
= address
->next
)
1381 * Send only IPv4 and IPv6 addresses over the wire.
1383 switch (address
->addr
->sa_family
)
1389 plen
+= (sizeof(struct rpcap_sockaddr
) * 4);
1398 // RPCAP findalldevs command
1399 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
,
1400 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
,
1401 PCAP_ERRBUF_SIZE
) == -1)
1404 rpcap_createhdr((struct rpcap_header
*) sendbuf
, pars
->protocol_version
,
1405 RPCAP_MSG_FINDALLIF_REPLY
, nif
, plen
);
1407 // send the interface list
1408 for (d
= alldevs
; d
!= NULL
; d
= d
->next
)
1410 uint16 lname
, ldescr
;
1412 findalldevs_if
= (struct rpcap_findalldevs_if
*) &sendbuf
[sendbufidx
];
1414 if (sock_bufferize(NULL
, sizeof(struct rpcap_findalldevs_if
), NULL
,
1415 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1418 memset(findalldevs_if
, 0, sizeof(struct rpcap_findalldevs_if
));
1420 if (d
->description
) ldescr
= (short) strlen(d
->description
);
1422 if (d
->name
) lname
= (short) strlen(d
->name
);
1425 findalldevs_if
->desclen
= htons(ldescr
);
1426 findalldevs_if
->namelen
= htons(lname
);
1427 findalldevs_if
->flags
= htonl(d
->flags
);
1429 for (address
= d
->addresses
; address
!= NULL
; address
= address
->next
)
1432 * Send only IPv4 and IPv6 addresses over the wire.
1434 switch (address
->addr
->sa_family
)
1440 findalldevs_if
->naddr
++;
1447 findalldevs_if
->naddr
= htons(findalldevs_if
->naddr
);
1449 if (sock_bufferize(d
->name
, lname
, sendbuf
, &sendbufidx
,
1450 RPCAP_NETBUF_SIZE
, SOCKBUF_BUFFERIZE
, errmsgbuf
,
1451 PCAP_ERRBUF_SIZE
) == -1)
1454 if (sock_bufferize(d
->description
, ldescr
, sendbuf
, &sendbufidx
,
1455 RPCAP_NETBUF_SIZE
, SOCKBUF_BUFFERIZE
, errmsgbuf
,
1456 PCAP_ERRBUF_SIZE
) == -1)
1459 // send all addresses
1460 for (address
= d
->addresses
; address
!= NULL
; address
= address
->next
)
1462 struct rpcap_sockaddr
*sockaddr
;
1465 * Send only IPv4 and IPv6 addresses over the wire.
1467 switch (address
->addr
->sa_family
)
1473 sockaddr
= (struct rpcap_sockaddr
*) &sendbuf
[sendbufidx
];
1474 if (sock_bufferize(NULL
, sizeof(struct rpcap_sockaddr
), NULL
,
1475 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1477 daemon_seraddr((struct sockaddr_storage
*) address
->addr
, sockaddr
);
1479 sockaddr
= (struct rpcap_sockaddr
*) &sendbuf
[sendbufidx
];
1480 if (sock_bufferize(NULL
, sizeof(struct rpcap_sockaddr
), NULL
,
1481 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1483 daemon_seraddr((struct sockaddr_storage
*) address
->netmask
, sockaddr
);
1485 sockaddr
= (struct rpcap_sockaddr
*) &sendbuf
[sendbufidx
];
1486 if (sock_bufferize(NULL
, sizeof(struct rpcap_sockaddr
), NULL
,
1487 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1489 daemon_seraddr((struct sockaddr_storage
*) address
->broadaddr
, sockaddr
);
1491 sockaddr
= (struct rpcap_sockaddr
*) &sendbuf
[sendbufidx
];
1492 if (sock_bufferize(NULL
, sizeof(struct rpcap_sockaddr
), NULL
,
1493 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1495 daemon_seraddr((struct sockaddr_storage
*) address
->dstaddr
, sockaddr
);
1504 // We no longer need the device list. Free it.
1505 pcap_freealldevs(alldevs
);
1507 // Send a final command that says "now send it!"
1508 if (sock_send(pars
->sockctrl_out
, pars
->ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1510 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1518 pcap_freealldevs(alldevs
);
1520 if (rpcap_senderror(pars
->sockctrl_out
, pars
->ssl
, pars
->protocol_version
,
1521 PCAP_ERR_FINDALLIF
, errmsgbuf
, errbuf
) == -1)
1523 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1530 \param plen: the length of the current message (needed in order to be able
1531 to discard excess data in the message, if present)
1534 daemon_msg_open_req(struct daemon_slpars
*pars
, uint32 plen
, char *source
, size_t sourcelen
)
1536 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
1537 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
1538 pcap_t
*fp
; // pcap_t main variable
1540 char sendbuf
[RPCAP_NETBUF_SIZE
]; // temporary buffer in which data to be sent is buffered
1541 int sendbufidx
= 0; // index which keeps the number of bytes currently buffered
1542 struct rpcap_openreply
*openreply
; // open reply message
1544 if (plen
> sourcelen
- 1)
1546 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Source string too long");
1550 nread
= sock_recv(pars
->sockctrl_in
, pars
->ssl
, source
, plen
,
1551 SOCK_RECEIVEALL_YES
|SOCK_EOF_IS_ERROR
, errbuf
, PCAP_ERRBUF_SIZE
);
1554 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
1557 source
[nread
] = '\0';
1560 // XXX - make sure it's *not* a URL; we don't support opening
1561 // remote devices here.
1563 // Open the selected device
1564 // This is a fake open, since we do that only to get the needed parameters, then we close the device again
1565 if ((fp
= pcap_open_live(source
,
1566 1500 /* fake snaplen */,
1568 1000 /* fake timeout */,
1569 errmsgbuf
)) == NULL
)
1572 // Now, I can send a RPCAP open reply message
1573 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
, &sendbufidx
,
1574 RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1577 rpcap_createhdr((struct rpcap_header
*) sendbuf
, pars
->protocol_version
,
1578 RPCAP_MSG_OPEN_REPLY
, 0, sizeof(struct rpcap_openreply
));
1580 openreply
= (struct rpcap_openreply
*) &sendbuf
[sendbufidx
];
1582 if (sock_bufferize(NULL
, sizeof(struct rpcap_openreply
), NULL
, &sendbufidx
,
1583 RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1586 memset(openreply
, 0, sizeof(struct rpcap_openreply
));
1587 openreply
->linktype
= htonl(pcap_datalink(fp
));
1589 // We're done with the pcap_t.
1593 if (sock_send(pars
->sockctrl_out
, pars
->ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1595 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1601 if (rpcap_senderror(pars
->sockctrl_out
, pars
->ssl
, pars
->protocol_version
,
1602 PCAP_ERR_OPEN
, errmsgbuf
, errbuf
) == -1)
1604 // That failed; log a message and give up.
1605 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1609 // Check if all the data has been read; if not, discard the data in excess
1610 if (rpcapd_discard(pars
->sockctrl_in
, pars
->ssl
, plen
) == -1)
1618 \param plen: the length of the current message (needed in order to be able
1619 to discard excess data in the message, if present)
1622 daemon_msg_startcap_req(struct daemon_slpars
*pars
, uint32 plen
, struct thread_handle
*threaddata
, char *source
, struct session
**sessionp
, struct rpcap_sampling
*samp_param _U_
, int uses_ssl
)
1624 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
1625 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
1626 char portdata
[PCAP_BUF_SIZE
]; // temp variable needed to derive the data port
1627 char peerhost
[PCAP_BUF_SIZE
]; // temp variable needed to derive the host name of our peer
1628 struct session
*session
= NULL
; // saves state of session
1630 char sendbuf
[RPCAP_NETBUF_SIZE
]; // temporary buffer in which data to be sent is buffered
1631 int sendbufidx
= 0; // index which keeps the number of bytes currently buffered
1633 // socket-related variables
1634 SOCKET sockdata
= INVALID_SOCKET
; // socket descriptor of the data connection
1635 struct addrinfo hints
; // temp, needed to open a socket connection
1636 struct addrinfo
*addrinfo
; // temp, needed to open a socket connection
1637 struct sockaddr_storage saddr
; // temp, needed to retrieve the network data port chosen on the local machine
1638 socklen_t saddrlen
; // temp, needed to retrieve the network data port chosen on the local machine
1639 int ret
; // return value from functions
1642 pthread_attr_t detachedAttribute
; // temp, needed to set the created thread as detached
1645 // RPCAP-related variables
1646 struct rpcap_startcapreq startcapreq
; // start capture request message
1647 struct rpcap_startcapreply
*startcapreply
; // start capture reply message
1648 int serveropen_dp
; // keeps who is going to open the data connection
1652 status
= rpcapd_recv(pars
->sockctrl_in
, pars
->ssl
, (char *) &startcapreq
,
1653 sizeof(struct rpcap_startcapreq
), &plen
, errmsgbuf
);
1663 startcapreq
.flags
= ntohs(startcapreq
.flags
);
1665 // Check that the client does not ask for UDP is the server has been asked
1666 // to enforce encryption, as SSL is not supported yet with UDP:
1667 if (uses_ssl
&& (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_DGRAM
))
1669 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1670 "SSL not supported with UDP forward of remote packets");
1674 // Create a session structure
1675 session
= malloc(sizeof(struct session
));
1676 if (session
== NULL
)
1678 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Can't allocate session structure");
1682 session
->ctrl_ssl
= session
->data_ssl
= NULL
;
1684 // Open the selected device
1685 if ((session
->fp
= pcap_open_live(source
,
1686 ntohl(startcapreq
.snaplen
),
1687 (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_PROMISC
) ? 1 : 0 /* local device, other flags not needed */,
1688 ntohl(startcapreq
.read_timeout
),
1689 errmsgbuf
)) == NULL
)
1693 // Apply sampling parameters
1694 fp
->rmt_samp
.method
= samp_param
->method
;
1695 fp
->rmt_samp
.value
= samp_param
->value
;
1699 We're in active mode if:
1700 - we're using TCP, and the user wants us to be in active mode
1703 serveropen_dp
= (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_SERVEROPEN
) || (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_DGRAM
) || pars
->isactive
;
1706 Gets the sockaddr structure referred to the other peer in the ctrl connection
1708 We need that because:
1709 - if we're in passive mode, we need to know the address family we want to use
1710 (the same used for the ctrl socket)
1711 - if we're in active mode, we need to know the network address of the other host
1712 we want to connect to
1714 saddrlen
= sizeof(struct sockaddr_storage
);
1715 if (getpeername(pars
->sockctrl_in
, (struct sockaddr
*) &saddr
, &saddrlen
) == -1)
1717 sock_geterror("getpeername(): ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
1721 memset(&hints
, 0, sizeof(struct addrinfo
));
1722 hints
.ai_socktype
= (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_DGRAM
) ? SOCK_DGRAM
: SOCK_STREAM
;
1723 hints
.ai_family
= saddr
.ss_family
;
1725 // Now we have to create a new socket to send packets
1726 if (serveropen_dp
) // Data connection is opened by the server toward the client
1728 pcap_snprintf(portdata
, sizeof portdata
, "%d", ntohs(startcapreq
.portdata
));
1730 // Get the name of the other peer (needed to connect to that specific network address)
1731 if (getnameinfo((struct sockaddr
*) &saddr
, saddrlen
, peerhost
,
1732 sizeof(peerhost
), NULL
, 0, NI_NUMERICHOST
))
1734 sock_geterror("getnameinfo(): ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
1738 if (sock_initaddress(peerhost
, portdata
, &hints
, &addrinfo
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1741 if ((sockdata
= sock_open(addrinfo
, SOCKOPEN_CLIENT
, 0, errmsgbuf
, PCAP_ERRBUF_SIZE
)) == INVALID_SOCKET
)
1744 else // Data connection is opened by the client toward the server
1746 hints
.ai_flags
= AI_PASSIVE
;
1748 // Let's the server socket pick up a free network port for us
1749 if (sock_initaddress(NULL
, "0", &hints
, &addrinfo
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1752 if ((sockdata
= sock_open(addrinfo
, SOCKOPEN_SERVER
, 1 /* max 1 connection in queue */, errmsgbuf
, PCAP_ERRBUF_SIZE
)) == INVALID_SOCKET
)
1755 // get the complete sockaddr structure used in the data connection
1756 saddrlen
= sizeof(struct sockaddr_storage
);
1757 if (getsockname(sockdata
, (struct sockaddr
*) &saddr
, &saddrlen
) == -1)
1759 sock_geterror("getsockname(): ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
1763 // Get the local port the system picked up
1764 if (getnameinfo((struct sockaddr
*) &saddr
, saddrlen
, NULL
,
1765 0, portdata
, sizeof(portdata
), NI_NUMERICSERV
))
1767 sock_geterror("getnameinfo(): ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
1772 // addrinfo is no longer used
1773 freeaddrinfo(addrinfo
);
1776 // Needed to send an error on the ctrl connection
1777 session
->sockctrl_out
= pars
->sockctrl_out
;
1778 session
->ctrl_ssl
= pars
->ssl
;
1779 session
->protocol_version
= pars
->protocol_version
;
1781 // Now I can set the filter
1782 ret
= daemon_unpackapplyfilter(pars
->sockctrl_in
, pars
->ssl
, session
, &plen
, errmsgbuf
);
1785 // Fatal error. A message has been logged; just give up.
1790 // Non-fatal error. Send an error message to the client.
1794 // Now, I can send a RPCAP start capture reply message
1795 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
, &sendbufidx
,
1796 RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1799 rpcap_createhdr((struct rpcap_header
*) sendbuf
, pars
->protocol_version
,
1800 RPCAP_MSG_STARTCAP_REPLY
, 0, sizeof(struct rpcap_startcapreply
));
1802 startcapreply
= (struct rpcap_startcapreply
*) &sendbuf
[sendbufidx
];
1804 if (sock_bufferize(NULL
, sizeof(struct rpcap_startcapreply
), NULL
,
1805 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1808 memset(startcapreply
, 0, sizeof(struct rpcap_startcapreply
));
1809 startcapreply
->bufsize
= htonl(pcap_bufsize(session
->fp
));
1813 unsigned short port
= (unsigned short)strtoul(portdata
,NULL
,10);
1814 startcapreply
->portdata
= htons(port
);
1817 if (sock_send(pars
->sockctrl_out
, pars
->ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1819 // That failed; log a message and give up.
1820 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1826 SOCKET socktemp
; // We need another socket, since we're going to accept() a connection
1828 // Connection creation
1829 saddrlen
= sizeof(struct sockaddr_storage
);
1831 socktemp
= accept(sockdata
, (struct sockaddr
*) &saddr
, &saddrlen
);
1833 if (socktemp
== INVALID_SOCKET
)
1835 sock_geterror("accept(): ", errbuf
, PCAP_ERRBUF_SIZE
);
1836 rpcapd_log(LOGPRIO_ERROR
, "Accept of data connection failed: %s",
1841 // Now that I accepted the connection, the server socket is no longer needed
1842 sock_close(sockdata
, NULL
, 0);
1843 sockdata
= socktemp
;
1850 /* In both active or passive cases, wait for the client to initiate the
1851 * TLS handshake. Yes during that time the control socket will not be
1852 * served, but the same was true from the above call to accept(). */
1853 ssl
= ssl_promotion(1, sockdata
, errbuf
, PCAP_ERRBUF_SIZE
);
1856 rpcapd_log(LOGPRIO_ERROR
, "TLS handshake failed: %s", errbuf
);
1861 session
->data_ssl
= ssl
;
1862 session
->sockdata
= sockdata
;
1864 // Now we have to create a new thread to receive packets
1866 threaddata
->thread
= (HANDLE
)_beginthreadex(NULL
, 0, daemon_thrdatamain
,
1867 (void *) session
, 0, NULL
);
1868 if (threaddata
->thread
== 0)
1870 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Error creating the data thread");
1874 /* GV we need this to create the thread as detached. */
1875 /* GV otherwise, the thread handle is not destroyed */
1876 pthread_attr_init(&detachedAttribute
);
1877 pthread_attr_setdetachstate(&detachedAttribute
, PTHREAD_CREATE_DETACHED
);
1878 ret
= pthread_create(&threaddata
->thread
, &detachedAttribute
,
1879 daemon_thrdatamain
, (void *) session
);
1882 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1883 ret
, "Error creating the data thread");
1884 pthread_attr_destroy(&detachedAttribute
);
1887 pthread_attr_destroy(&detachedAttribute
);
1889 threaddata
->have_thread
= 1;
1891 // Check if all the data has been read; if not, discard the data in excess
1892 if (rpcapd_discard(pars
->sockctrl_in
, pars
->ssl
, plen
) == -1)
1895 *sessionp
= session
;
1900 // Not a fatal error, so send the client an error message and
1901 // keep serving client requests.
1906 freeaddrinfo(addrinfo
);
1908 if (threaddata
->have_thread
)
1913 pcap_breakloop(session
->fp
);
1914 SetEvent(pcap_getevent(session
->fp
));
1916 CloseHandle(threaddata
->thread
);
1918 pthread_cancel(threaddata
->thread
);
1920 threaddata
->have_thread
= 0;
1923 if (sockdata
!= INVALID_SOCKET
)
1924 sock_close(sockdata
, NULL
, 0);
1929 pcap_close(session
->fp
);
1931 if (session
->ctrl_ssl
)
1932 SSL_free(session
->ctrl_ssl
);
1933 if (session
->data_ssl
)
1934 SSL_free(session
->data_ssl
);
1939 if (rpcap_senderror(pars
->sockctrl_out
, pars
->ssl
, pars
->protocol_version
,
1940 PCAP_ERR_STARTCAPTURE
, errmsgbuf
, errbuf
) == -1)
1942 // That failed; log a message and give up.
1943 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1947 // Check if all the data has been read; if not, discard the data in excess
1948 if (rpcapd_discard(pars
->sockctrl_in
, pars
->ssl
, plen
) == -1)
1958 // Fatal network error, so don't try to communicate with
1959 // the client, just give up.
1963 if (threaddata
->have_thread
)
1966 if (session
&& session
->fp
)
1969 // Tell the data connection thread main capture
1970 // loop to break out of that loop.
1972 pcap_breakloop(session
->fp
);
1975 // If it's currently blocked waiting for packets
1976 // to arrive, try to wake it up, so it can see
1977 // the "break out of the loop" indication.
1979 SetEvent(pcap_getevent(session
->fp
));
1983 // Wait for the thread to exit, so we don't close
1984 // sockets out from under it.
1986 // XXX - have a timeout, so we don't wait forever?
1988 WaitForSingleObject(threaddata
->thread
, INFINITE
);
1991 // Release the thread handle, as we're done with
1994 CloseHandle(threaddata
->thread
);
1996 pthread_cancel(threaddata
->thread
);
1998 threaddata
->have_thread
= 0;
2001 if (sockdata
!= INVALID_SOCKET
)
2002 sock_close(sockdata
, NULL
, 0);
2007 pcap_close(session
->fp
);
2009 if (session
->ctrl_ssl
)
2010 SSL_free(session
->ctrl_ssl
);
2011 if (session
->data_ssl
)
2012 SSL_free(session
->data_ssl
);
2021 daemon_msg_endcap_req(struct daemon_slpars
*pars
, struct session
*session
, struct thread_handle
*threaddata
)
2023 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2024 struct rpcap_header header
;
2026 if (threaddata
->have_thread
)
2030 // Tell the data connection thread main capture loop to
2031 // break out of that loop.
2033 pcap_breakloop(session
->fp
);
2036 // If it's currently blocked waiting for packets to
2037 // arrive, try to wake it up, so it can see the "break
2038 // out of the loop" indication.
2040 SetEvent(pcap_getevent(session
->fp
));
2043 // Wait for the thread to exit, so we don't close
2044 // sockets out from under it.
2046 // XXX - have a timeout, so we don't wait forever?
2048 WaitForSingleObject(threaddata
->thread
, INFINITE
);
2051 // Release the thread handle, as we're done with
2054 CloseHandle(threaddata
->thread
);
2056 pthread_cancel(threaddata
->thread
);
2058 threaddata
->have_thread
= 0;
2061 session_close(session
);
2063 rpcap_createhdr(&header
, pars
->protocol_version
,
2064 RPCAP_MSG_ENDCAP_REPLY
, 0, 0);
2066 if (sock_send(pars
->sockctrl_out
, pars
->ssl
, (char *) &header
, sizeof(struct rpcap_header
), errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2068 // That failed; log a message and give up.
2069 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2077 daemon_unpackapplyfilter(SOCKET sockctrl_in
, SSL
*ctrl_ssl
, struct session
*session
, uint32
*plenp
, char *errmsgbuf
)
2080 struct rpcap_filter filter
;
2081 struct rpcap_filterbpf_insn insn
;
2082 struct bpf_insn
*bf_insn
;
2083 struct bpf_program bf_prog
;
2086 status
= rpcapd_recv(sockctrl_in
, ctrl_ssl
, (char *) &filter
,
2087 sizeof(struct rpcap_filter
), plenp
, errmsgbuf
);
2097 bf_prog
.bf_len
= ntohl(filter
.nitems
);
2099 if (ntohs(filter
.filtertype
) != RPCAP_UPDATEFILTER_BPF
)
2101 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Only BPF/NPF filters are currently supported");
2105 bf_insn
= (struct bpf_insn
*) malloc (sizeof(struct bpf_insn
) * bf_prog
.bf_len
);
2106 if (bf_insn
== NULL
)
2108 pcap_fmt_errmsg_for_errno(errmsgbuf
, PCAP_ERRBUF_SIZE
,
2109 errno
, "malloc() failed");
2113 bf_prog
.bf_insns
= bf_insn
;
2115 for (i
= 0; i
< bf_prog
.bf_len
; i
++)
2117 status
= rpcapd_recv(sockctrl_in
, ctrl_ssl
, (char *) &insn
,
2118 sizeof(struct rpcap_filterbpf_insn
), plenp
, errmsgbuf
);
2128 bf_insn
->code
= ntohs(insn
.code
);
2129 bf_insn
->jf
= insn
.jf
;
2130 bf_insn
->jt
= insn
.jt
;
2131 bf_insn
->k
= ntohl(insn
.k
);
2137 // XXX - pcap_setfilter() should do the validation for us.
2139 if (bpf_validate(bf_prog
.bf_insns
, bf_prog
.bf_len
) == 0)
2141 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "The filter contains bogus instructions");
2145 if (pcap_setfilter(session
->fp
, &bf_prog
))
2147 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "RPCAP error: %s", pcap_geterr(session
->fp
));
2155 daemon_msg_updatefilter_req(struct daemon_slpars
*pars
, struct session
*session
, uint32 plen
)
2157 char errbuf
[PCAP_ERRBUF_SIZE
];
2158 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
2159 int ret
; // status of daemon_unpackapplyfilter()
2160 struct rpcap_header header
; // keeps the answer to the updatefilter command
2162 ret
= daemon_unpackapplyfilter(pars
->sockctrl_in
, pars
->ssl
, session
, &plen
, errmsgbuf
);
2165 // Fatal error. A message has been logged; just give up.
2170 // Non-fatal error. Send an error reply to the client.
2174 // Check if all the data has been read; if not, discard the data in excess
2175 if (rpcapd_discard(pars
->sockctrl_in
, pars
->ssl
, plen
) == -1)
2181 // A response is needed, otherwise the other host does not know that everything went well
2182 rpcap_createhdr(&header
, pars
->protocol_version
,
2183 RPCAP_MSG_UPDATEFILTER_REPLY
, 0, 0);
2185 if (sock_send(pars
->sockctrl_out
, pars
->ssl
, (char *) &header
, sizeof (struct rpcap_header
), pcap_geterr(session
->fp
), PCAP_ERRBUF_SIZE
))
2187 // That failed; log a messsage and give up.
2188 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2195 if (rpcapd_discard(pars
->sockctrl_in
, pars
->ssl
, plen
) == -1)
2199 rpcap_senderror(pars
->sockctrl_out
, pars
->ssl
, pars
->protocol_version
,
2200 PCAP_ERR_UPDATEFILTER
, errmsgbuf
, NULL
);
2206 \brief Received the sampling parameters from remote host and it stores in the pcap_t structure.
2209 daemon_msg_setsampling_req(struct daemon_slpars
*pars
, uint32 plen
, struct rpcap_sampling
*samp_param
)
2211 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2212 char errmsgbuf
[PCAP_ERRBUF_SIZE
];
2213 struct rpcap_header header
;
2214 struct rpcap_sampling rpcap_samp
;
2217 status
= rpcapd_recv(pars
->sockctrl_in
, pars
->ssl
, (char *) &rpcap_samp
, sizeof(struct rpcap_sampling
), &plen
, errmsgbuf
);
2227 // Save these settings in the pcap_t
2228 samp_param
->method
= rpcap_samp
.method
;
2229 samp_param
->value
= ntohl(rpcap_samp
.value
);
2231 // A response is needed, otherwise the other host does not know that everything went well
2232 rpcap_createhdr(&header
, pars
->protocol_version
,
2233 RPCAP_MSG_SETSAMPLING_REPLY
, 0, 0);
2235 if (sock_send(pars
->sockctrl_out
, pars
->ssl
, (char *) &header
, sizeof (struct rpcap_header
), errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2237 // That failed; log a messsage and give up.
2238 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2242 if (rpcapd_discard(pars
->sockctrl_in
, pars
->ssl
, plen
) == -1)
2250 if (rpcap_senderror(pars
->sockctrl_out
, pars
->ssl
, pars
->protocol_version
,
2251 PCAP_ERR_AUTH
, errmsgbuf
, errbuf
) == -1)
2253 // That failed; log a message and give up.
2254 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2258 // Check if all the data has been read; if not, discard the data in excess
2259 if (rpcapd_discard(pars
->sockctrl_in
, pars
->ssl
, plen
) == -1)
2268 daemon_msg_stats_req(struct daemon_slpars
*pars
, struct session
*session
, uint32 plen
, struct pcap_stat
*stats
, unsigned int svrcapt
)
2270 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2271 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
2272 char sendbuf
[RPCAP_NETBUF_SIZE
]; // temporary buffer in which data to be sent is buffered
2273 int sendbufidx
= 0; // index which keeps the number of bytes currently buffered
2274 struct rpcap_stats
*netstats
; // statistics sent on the network
2276 // Checks that the header does not contain other data; if so, discard it
2277 if (rpcapd_discard(pars
->sockctrl_in
, pars
->ssl
, plen
) == -1)
2283 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
,
2284 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
2287 rpcap_createhdr((struct rpcap_header
*) sendbuf
, pars
->protocol_version
,
2288 RPCAP_MSG_STATS_REPLY
, 0, (uint16
) sizeof(struct rpcap_stats
));
2290 netstats
= (struct rpcap_stats
*) &sendbuf
[sendbufidx
];
2292 if (sock_bufferize(NULL
, sizeof(struct rpcap_stats
), NULL
,
2293 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
2296 if (session
&& session
->fp
)
2298 if (pcap_stats(session
->fp
, stats
) == -1)
2300 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "%s", pcap_geterr(session
->fp
));
2304 netstats
->ifdrop
= htonl(stats
->ps_ifdrop
);
2305 netstats
->ifrecv
= htonl(stats
->ps_recv
);
2306 netstats
->krnldrop
= htonl(stats
->ps_drop
);
2307 netstats
->svrcapt
= htonl(session
->TotCapt
);
2311 // We have to keep compatibility with old applications,
2312 // which ask for statistics also when the capture has
2314 netstats
->ifdrop
= htonl(stats
->ps_ifdrop
);
2315 netstats
->ifrecv
= htonl(stats
->ps_recv
);
2316 netstats
->krnldrop
= htonl(stats
->ps_drop
);
2317 netstats
->svrcapt
= htonl(svrcapt
);
2321 if (sock_send(pars
->sockctrl_out
, pars
->ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2323 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2330 rpcap_senderror(pars
->sockctrl_out
, pars
->ssl
, pars
->protocol_version
,
2331 PCAP_ERR_GETSTATS
, errmsgbuf
, NULL
);
2336 static unsigned __stdcall
2340 daemon_thrdatamain(void *ptr
)
2342 char errbuf
[PCAP_ERRBUF_SIZE
+ 1]; // error buffer
2343 struct session
*session
; // pointer to the struct session for this session
2344 int retval
; // general variable used to keep the return value of other functions
2345 struct rpcap_pkthdr
*net_pkt_header
;// header of the packet
2346 struct pcap_pkthdr
*pkt_header
; // pointer to the buffer that contains the header of the current packet
2347 u_char
*pkt_data
; // pointer to the buffer that contains the current packet
2348 size_t sendbufsize
; // size for the send buffer
2349 char *sendbuf
; // temporary buffer in which data to be sent is buffered
2350 int sendbufidx
; // index which keeps the number of bytes currently buffered
2353 session
= (struct session
*) ptr
;
2355 session
->TotCapt
= 0; // counter which is incremented each time a packet is received
2357 // Initialize errbuf
2358 memset(errbuf
, 0, sizeof(errbuf
));
2361 // We need a buffer large enough to hold a buffer large enough
2362 // for a maximum-size packet for this pcap_t.
2364 if (pcap_snapshot(session
->fp
) < 0)
2367 // The snapshot length is negative.
2368 // This "should not happen".
2370 rpcapd_log(LOGPRIO_ERROR
,
2371 "Unable to allocate the buffer for this child thread: snapshot length of %d is negative",
2372 pcap_snapshot(session
->fp
));
2373 sendbuf
= NULL
; // we can't allocate a buffer, so nothing to free
2377 // size_t is unsigned, and the result of pcap_snapshot() is signed;
2378 // on no platform that we support is int larger than size_t.
2379 // This means that, unless the extra information we prepend to
2380 // a maximum-sized packet is impossibly large, the sum of the
2381 // snapshot length and the size of that extra information will
2384 // So we don't need to make sure that sendbufsize will overflow.
2386 // However, we *do* need to make sure its value fits in an int,
2387 // because sock_send() can't send more than INT_MAX bytes (it could
2388 // do so on 64-bit UN*Xes, but can't do so on Windows, not even
2389 // 64-bit Windows, as the send() buffer size argument is an int
2392 sendbufsize
= sizeof(struct rpcap_header
) + sizeof(struct rpcap_pkthdr
) + pcap_snapshot(session
->fp
);
2393 if (sendbufsize
> INT_MAX
)
2395 rpcapd_log(LOGPRIO_ERROR
,
2396 "Buffer size for this child thread would be larger than %d",
2398 sendbuf
= NULL
; // we haven't allocated a buffer, so nothing to free
2401 sendbuf
= (char *) malloc (sendbufsize
);
2402 if (sendbuf
== NULL
)
2404 rpcapd_log(LOGPRIO_ERROR
,
2405 "Unable to allocate the buffer for this child thread");
2410 // Modify thread params so that it can be killed at any time
2411 retval
= pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, NULL
);
2414 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
2415 retval
, "pthread_setcancelstate");
2416 rpcapd_log(LOGPRIO_ERROR
,
2417 "Can't set cancel state on data thread: %s", errbuf
);
2420 retval
= pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS
, NULL
);
2423 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
2424 retval
, "pthread_setcanceltype");
2425 rpcapd_log(LOGPRIO_ERROR
,
2426 "Can't set cancel type on data thread: %s", errbuf
);
2431 // Retrieve the packets
2432 while ((retval
= pcap_next_ex(session
->fp
, &pkt_header
, (const u_char
**) &pkt_data
)) >= 0) // cast to avoid a compiler warning
2434 if (retval
== 0) // Read timeout elapsed
2439 // Bufferize the general header
2440 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
,
2441 &sendbufidx
, (int)sendbufsize
, SOCKBUF_CHECKONLY
, errbuf
,
2442 PCAP_ERRBUF_SIZE
) == -1)
2444 rpcapd_log(LOGPRIO_ERROR
,
2445 "sock_bufferize() error sending packet message: %s",
2450 rpcap_createhdr((struct rpcap_header
*) sendbuf
,
2451 session
->protocol_version
, RPCAP_MSG_PACKET
, 0,
2452 (uint16
) (sizeof(struct rpcap_pkthdr
) + pkt_header
->caplen
));
2454 net_pkt_header
= (struct rpcap_pkthdr
*) &sendbuf
[sendbufidx
];
2456 // Bufferize the pkt header
2457 if (sock_bufferize(NULL
, sizeof(struct rpcap_pkthdr
), NULL
,
2458 &sendbufidx
, (int)sendbufsize
, SOCKBUF_CHECKONLY
, errbuf
,
2459 PCAP_ERRBUF_SIZE
) == -1)
2461 rpcapd_log(LOGPRIO_ERROR
,
2462 "sock_bufferize() error sending packet message: %s",
2467 net_pkt_header
->caplen
= htonl(pkt_header
->caplen
);
2468 net_pkt_header
->len
= htonl(pkt_header
->len
);
2469 net_pkt_header
->npkt
= htonl(++(session
->TotCapt
));
2471 // This protocol needs to be updated with a new version
2472 // before 2038-01-19 03:14:07 UTC.
2474 net_pkt_header
->timestamp_sec
= htonl((uint32
)pkt_header
->ts
.tv_sec
);
2475 net_pkt_header
->timestamp_usec
= htonl((uint32
)pkt_header
->ts
.tv_usec
);
2477 // Bufferize the pkt data
2478 if (sock_bufferize((char *) pkt_data
, pkt_header
->caplen
,
2479 sendbuf
, &sendbufidx
, (int)sendbufsize
, SOCKBUF_BUFFERIZE
,
2480 errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2482 rpcapd_log(LOGPRIO_ERROR
,
2483 "sock_bufferize() error sending packet message: %s",
2489 // If the client dropped the connection, don't report an
2490 // error, just quit.
2491 status
= sock_send(session
->sockdata
, session
->data_ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
);
2497 // Error other than "client closed the
2498 // connection out from under us"; report
2501 rpcapd_log(LOGPRIO_ERROR
,
2502 "Send of packet to client failed: %s",
2507 // Give up in either case.
2515 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Error reading the packets: %s", pcap_geterr(session
->fp
));
2516 rpcap_senderror(session
->sockctrl_out
, session
->ctrl_ssl
, session
->protocol_version
,
2517 PCAP_ERR_READEX
, errbuf
, NULL
);
2522 session_close(session
);
2530 \brief It serializes a network address.
2532 It accepts a 'sockaddr_storage' structure as input, and it converts it appropriately into a format
2533 that can be used to be sent on the network. Basically, it applies all the hton()
2534 conversion required to the input variable.
2536 \param sockaddrin a 'sockaddr_storage' pointer to the variable that has to be
2537 serialized. This variable can be both a 'sockaddr_in' and 'sockaddr_in6'.
2539 \param sockaddrout an 'rpcap_sockaddr' pointer to the variable that will contain
2540 the serialized data. This variable has to be allocated by the user.
2542 \warning This function supports only AF_INET and AF_INET6 address families.
2545 daemon_seraddr(struct sockaddr_storage
*sockaddrin
, struct rpcap_sockaddr
*sockaddrout
)
2547 memset(sockaddrout
, 0, sizeof(struct sockaddr_storage
));
2549 // There can be the case in which the sockaddrin is not available
2550 if (sockaddrin
== NULL
) return;
2552 // Warning: we support only AF_INET and AF_INET6
2553 switch (sockaddrin
->ss_family
)
2557 struct sockaddr_in
*sockaddrin_ipv4
;
2558 struct rpcap_sockaddr_in
*sockaddrout_ipv4
;
2560 sockaddrin_ipv4
= (struct sockaddr_in
*) sockaddrin
;
2561 sockaddrout_ipv4
= (struct rpcap_sockaddr_in
*) sockaddrout
;
2562 sockaddrout_ipv4
->family
= htons(RPCAP_AF_INET
);
2563 sockaddrout_ipv4
->port
= htons(sockaddrin_ipv4
->sin_port
);
2564 memcpy(&sockaddrout_ipv4
->addr
, &sockaddrin_ipv4
->sin_addr
, sizeof(sockaddrout_ipv4
->addr
));
2565 memset(sockaddrout_ipv4
->zero
, 0, sizeof(sockaddrout_ipv4
->zero
));
2572 struct sockaddr_in6
*sockaddrin_ipv6
;
2573 struct rpcap_sockaddr_in6
*sockaddrout_ipv6
;
2575 sockaddrin_ipv6
= (struct sockaddr_in6
*) sockaddrin
;
2576 sockaddrout_ipv6
= (struct rpcap_sockaddr_in6
*) sockaddrout
;
2577 sockaddrout_ipv6
->family
= htons(RPCAP_AF_INET6
);
2578 sockaddrout_ipv6
->port
= htons(sockaddrin_ipv6
->sin6_port
);
2579 sockaddrout_ipv6
->flowinfo
= htonl(sockaddrin_ipv6
->sin6_flowinfo
);
2580 memcpy(&sockaddrout_ipv6
->addr
, &sockaddrin_ipv6
->sin6_addr
, sizeof(sockaddrout_ipv6
->addr
));
2581 sockaddrout_ipv6
->scope_id
= htonl(sockaddrin_ipv6
->sin6_scope_id
);
2590 \brief Suspends a thread for secs seconds.
2592 void sleep_secs(int secs
)
2597 unsigned secs_remaining
;
2601 secs_remaining
= secs
;
2602 while (secs_remaining
!= 0)
2603 secs_remaining
= sleep(secs_remaining
);
2608 * Read the header of a message.
2611 rpcapd_recv_msg_header(SOCKET sock
, SSL
*ssl
, struct rpcap_header
*headerp
)
2614 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2616 nread
= sock_recv(sock
, ssl
, (char *) headerp
, sizeof(struct rpcap_header
),
2617 SOCK_RECEIVEALL_YES
|SOCK_EOF_ISNT_ERROR
, errbuf
, PCAP_ERRBUF_SIZE
);
2621 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
2626 // Immediate EOF; that's treated like a close message.
2629 headerp
->plen
= ntohl(headerp
->plen
);
2634 * Read data from a message.
2635 * If we're trying to read more data that remains, puts an error
2636 * message into errmsgbuf and returns -2. Otherwise, tries to read
2637 * the data and, if that succeeds, subtracts the amount read from
2638 * the number of bytes of data that remains.
2639 * Returns 0 on success, logs a message and returns -1 on a network
2643 rpcapd_recv(SOCKET sock
, SSL
*ssl
, char *buffer
, size_t toread
, uint32
*plen
, char *errmsgbuf
)
2646 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2650 // Tell the client and continue.
2651 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Message payload is too short");
2654 nread
= sock_recv(sock
, ssl
, buffer
, toread
,
2655 SOCK_RECEIVEALL_YES
|SOCK_EOF_IS_ERROR
, errbuf
, PCAP_ERRBUF_SIZE
);
2658 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
2666 * Discard data from a connection.
2667 * Mostly used to discard wrong-sized messages.
2668 * Returns 0 on success, logs a message and returns -1 on a network
2672 rpcapd_discard(SOCKET sock
, SSL
*ssl
, uint32 len
)
2674 char errbuf
[PCAP_ERRBUF_SIZE
+ 1]; // keeps the error string, prior to be printed
2678 if (sock_discard(sock
, ssl
, len
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2681 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
2689 * Close the socket associated with the session, the optional SSL handle,
2690 * and the underlying packet capture handle. We of course do not touch
2691 * the controlling socket that's also copied into the session.
2693 static void session_close(struct session
*session
)
2696 if (session
->data_ssl
)
2698 SSL_free(session
->data_ssl
); // Must happen *before* the socket is closed
2699 session
->data_ssl
= NULL
;
2703 if (session
->sockdata
)
2705 sock_close(session
->sockdata
, NULL
, 0);
2706 session
->sockdata
= 0;
2709 pcap_close(session
->fp
);