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
, SSL
*ssl
, int isactive
, int nullAuthAllowed
, int uses_ssl
)
149 struct daemon_slpars pars
; // service loop parameters
150 char errbuf
[PCAP_ERRBUF_SIZE
+ 1]; // keeps the error string, prior to be printed
151 char errmsgbuf
[PCAP_ERRBUF_SIZE
+ 1]; // buffer for errors to send to the client
153 struct rpcap_header header
; // RPCAP message general header
154 uint32 plen
; // payload length from header
155 int authenticated
= 0; // 1 if the client has successfully authenticated
156 char source
[PCAP_BUF_SIZE
+1]; // keeps the string that contains the interface to open
157 int got_source
= 0; // 1 if we've gotten the source from an open request
158 struct session
*session
= NULL
; // struct session main variable
159 const char *msg_type_string
; // string for message type
160 int client_told_us_to_close
= 0; // 1 if the client told us to close the capture
162 struct thread_handle threaddata
; // 'read from daemon and send to client' thread
164 // needed to save the values of the statistics
165 struct pcap_stat stats
;
166 unsigned int svrcapt
;
168 struct rpcap_sampling samp_param
; // in case sampling has been requested
170 // Structures needed for the select() call
171 fd_set rfds
; // set of socket descriptors we have to check
172 struct timeval tv
; // maximum time the select() can block waiting for data
173 int retval
; // select() return value
175 // Set parameters structure
176 pars
.sockctrl_in
= sockctrl_in
;
177 pars
.sockctrl_out
= sockctrl_out
;
179 pars
.protocol_version
= 0; // not yet known
180 pars
.isactive
= isactive
; // active mode
181 pars
.nullAuthAllowed
= nullAuthAllowed
;
183 // We don't have a thread yet.
184 threaddata
.have_thread
= 0;
186 // We *shouldn't* have to initialize the thread indicator
187 // itself, because the compiler *should* realize that we
188 // only use this if have_thread isn't 0, but we *do* have
189 // to do it, because not all compilers *do* realize that.
191 // There is no "invalid thread handle" value for a UN*X
192 // pthread_t, so we just zero it out.
195 threaddata
.thread
= INVALID_HANDLE_VALUE
;
197 memset(&threaddata
.thread
, 0, sizeof(threaddata
.thread
));
200 *errbuf
= 0; // Initialize errbuf
203 // The client must first authenticate; loop until they send us a
204 // message with a version we support and credentials we accept,
205 // they send us a close message indicating that they're giving up,
206 // or we get a network error or other fatal error.
208 while (!authenticated
)
211 // If we're in active mode, we have to check for the
214 // XXX - do this on *every* trip through the loop?
219 // We do not have to block here
220 tv
.tv_sec
= RPCAP_TIMEOUT_INIT
;
223 FD_SET(pars
.sockctrl_in
, &rfds
);
225 retval
= select((int)pars
.sockctrl_in
+ 1, &rfds
, NULL
, NULL
, &tv
);
228 sock_geterror("select failed: ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
229 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
, 0, PCAP_ERR_NETW
, errmsgbuf
, errbuf
) == -1)
230 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
234 // The timeout has expired
235 // So, this was a fake connection. Drop it down
238 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
, 0, PCAP_ERR_INITTIMEOUT
, "The RPCAP initial timeout has expired", errbuf
) == -1)
239 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
245 // Read the message header from the client.
247 nrecv
= rpcapd_recv_msg_header(pars
.sockctrl_in
, pars
.ssl
, &header
);
255 // Client closed the connection.
262 // Did the client specify a version we can handle?
264 if (!RPCAP_VERSION_IS_SUPPORTED(header
.ver
))
267 // Tell them it's not a valid protocol version.
272 // If RPCAP_MIN_VERSION is 0, no version is too
273 // old, as the oldest supported version is 0,
274 // and there are no negative versions.
276 #if RPCAP_MIN_VERSION != 0
277 if (header
.ver
< RPCAP_MIN_VERSION
)
280 // Their maximum version is too old;
281 // there *is* no version we can both
282 // handle, and they might reject
283 // an error with a version they don't
284 // understand, so reply with the
285 // version they sent. That may
286 // make them retry with that version,
287 // but they'll give up on that
290 reply_version
= header
.ver
;
296 // Their maximum version is too new,
297 // but they might be able to handle
298 // *our* maximum version, so reply
299 // with that version.
301 reply_version
= RPCAP_MAX_VERSION
;
303 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
, reply_version
,
304 PCAP_ERR_WRONGVER
, "RPCAP version number mismatch",
307 // That failed; log a message and give up.
308 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
312 // Discard the rest of the message.
313 if (rpcapd_discard(pars
.sockctrl_in
, pars
.ssl
, plen
) == -1)
319 // Let them try again.
324 // OK, we use the version the client specified.
326 pars
.protocol_version
= header
.ver
;
330 case RPCAP_MSG_AUTH_REQ
:
331 retval
= daemon_msg_auth_req(&pars
, plen
);
334 // Fatal error; a message has
335 // been logged, so just give up.
340 // Non-fatal error; we sent back
341 // an error message, so let them
346 // OK, we're authenticated; we sent back
347 // a reply, so start serving requests.
351 case RPCAP_MSG_CLOSE
:
353 // The client is giving up.
354 // Discard the rest of the message, if
355 // there is anything more.
357 (void)rpcapd_discard(pars
.sockctrl_in
, pars
.ssl
, plen
);
358 // We're done with this client.
361 case RPCAP_MSG_ERROR
:
362 // Log this and close the connection?
363 // XXX - is this what happens in active
364 // mode, where *we* initiate the
365 // connection, and the client gives us
366 // an error message rather than a "let
367 // me log in" message, indicating that
368 // we're not allowed to connect to them?
369 (void)daemon_msg_err(pars
.sockctrl_in
, pars
.ssl
, plen
);
372 case RPCAP_MSG_FINDALLIF_REQ
:
373 case RPCAP_MSG_OPEN_REQ
:
374 case RPCAP_MSG_STARTCAP_REQ
:
375 case RPCAP_MSG_UPDATEFILTER_REQ
:
376 case RPCAP_MSG_STATS_REQ
:
377 case RPCAP_MSG_ENDCAP_REQ
:
378 case RPCAP_MSG_SETSAMPLING_REQ
:
380 // These requests can't be sent until
381 // the client is authenticated.
383 msg_type_string
= rpcap_msg_type_string(header
.type
);
384 if (msg_type_string
!= NULL
)
386 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "%s request sent before authentication was completed", msg_type_string
);
390 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Message of type %u sent before authentication was completed", header
.type
);
392 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
,
393 pars
.protocol_version
, PCAP_ERR_WRONGMSG
,
394 errmsgbuf
, errbuf
) == -1)
396 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
399 // Discard the rest of the message.
400 if (rpcapd_discard(pars
.sockctrl_in
, pars
.ssl
, plen
) == -1)
407 case RPCAP_MSG_PACKET
:
408 case RPCAP_MSG_FINDALLIF_REPLY
:
409 case RPCAP_MSG_OPEN_REPLY
:
410 case RPCAP_MSG_STARTCAP_REPLY
:
411 case RPCAP_MSG_UPDATEFILTER_REPLY
:
412 case RPCAP_MSG_AUTH_REPLY
:
413 case RPCAP_MSG_STATS_REPLY
:
414 case RPCAP_MSG_ENDCAP_REPLY
:
415 case RPCAP_MSG_SETSAMPLING_REPLY
:
417 // These are server-to-client messages.
419 msg_type_string
= rpcap_msg_type_string(header
.type
);
420 if (msg_type_string
!= NULL
)
422 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Server-to-client message %s received from client", msg_type_string
);
426 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Server-to-client message of type %u received from client", header
.type
);
428 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
,
429 pars
.protocol_version
, PCAP_ERR_WRONGMSG
,
430 errmsgbuf
, errbuf
) == -1)
432 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
435 // Discard the rest of the message.
436 if (rpcapd_discard(pars
.sockctrl_in
, pars
.ssl
, plen
) == -1)
445 // Unknown message type.
447 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Unknown message type %u", header
.type
);
448 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
,
449 pars
.protocol_version
, PCAP_ERR_WRONGMSG
,
450 errmsgbuf
, errbuf
) == -1)
452 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
455 // Discard the rest of the message.
456 if (rpcapd_discard(pars
.sockctrl_in
, pars
.ssl
, plen
) == -1)
466 // OK, the client has authenticated itself, and we can start
467 // processing regular requests from it.
471 // We don't have any statistics yet.
483 errbuf
[0] = 0; // clear errbuf
485 // Avoid zombies connections; check if the connection is opens but no commands are performed
486 // from more than RPCAP_TIMEOUT_RUNTIME
488 // - I have to be in normal mode (no active mode)
489 // - if the device is open, I don't have to be in the middle of a capture (session->sockdata)
490 // - if the device is closed, I have always to check if a new command arrives
492 // Be carefully: the capture can have been started, but an error occurred (so session != NULL, but
494 if ((!pars
.isactive
) && ((session
== NULL
) || ((session
!= NULL
) && (session
->sockdata
== 0))))
496 // Check for the initial timeout
498 // We do not have to block here
499 tv
.tv_sec
= RPCAP_TIMEOUT_RUNTIME
;
502 FD_SET(pars
.sockctrl_in
, &rfds
);
504 retval
= select((int)pars
.sockctrl_in
+ 1, &rfds
, NULL
, NULL
, &tv
);
507 sock_geterror("select failed: ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
508 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
,
509 pars
.protocol_version
, PCAP_ERR_NETW
,
510 errmsgbuf
, errbuf
) == -1)
511 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
515 // The timeout has expired
516 // So, this was a fake connection. Drop it down
519 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
,
520 pars
.protocol_version
,
521 PCAP_ERR_INITTIMEOUT
,
522 "The RPCAP initial timeout has expired",
524 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
530 // Read the message header from the client.
532 nrecv
= rpcapd_recv_msg_header(pars
.sockctrl_in
, pars
.ssl
, &header
);
540 // Client closed the connection.
547 // Did the client specify the version we negotiated?
549 // For now, there's only one version.
551 if (header
.ver
!= pars
.protocol_version
)
554 // Tell them it's not the negotiated version.
555 // Send the error message with their version,
556 // so they don't reject it as having the wrong
559 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
,
560 header
.ver
, PCAP_ERR_WRONGVER
,
561 "RPCAP version in message isn't the negotiated version",
564 // That failed; log a message and give up.
565 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
569 // Discard the rest of the message.
570 (void)rpcapd_discard(pars
.sockctrl_in
, pars
.ssl
, plen
);
577 case RPCAP_MSG_ERROR
: // The other endpoint reported an error
579 (void)daemon_msg_err(pars
.sockctrl_in
, pars
.ssl
, plen
);
580 // Do nothing; just exit; the error code is already into the errbuf
581 // XXX - actually exit....
585 case RPCAP_MSG_FINDALLIF_REQ
:
587 if (daemon_msg_findallif_req(&pars
, plen
) == -1)
589 // Fatal error; a message has
590 // been logged, so just give up.
596 case RPCAP_MSG_OPEN_REQ
:
599 // Process the open request, and keep
600 // the source from it, for use later
601 // when the capture is started.
603 // XXX - we don't care if the client sends
604 // us multiple open requests, the last
607 retval
= daemon_msg_open_req(&pars
, plen
, source
, sizeof(source
));
610 // Fatal error; a message has
611 // been logged, so just give up.
618 case RPCAP_MSG_STARTCAP_REQ
:
622 // They never told us what device
624 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
,
625 pars
.protocol_version
,
626 PCAP_ERR_STARTCAPTURE
,
627 "No capture device was specified",
630 // Fatal error; log an
631 // error and give up.
632 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
635 if (rpcapd_discard(pars
.sockctrl_in
, pars
.ssl
, plen
) == -1)
642 if (daemon_msg_startcap_req(&pars
, plen
, &threaddata
, source
, &session
, &samp_param
, uses_ssl
) == -1)
644 // Fatal error; a message has
645 // been logged, so just give up.
651 case RPCAP_MSG_UPDATEFILTER_REQ
:
655 if (daemon_msg_updatefilter_req(&pars
, session
, plen
) == -1)
657 // Fatal error; a message has
658 // been logged, so just give up.
664 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
,
665 pars
.protocol_version
,
666 PCAP_ERR_UPDATEFILTER
,
667 "Device not opened. Cannot update filter",
670 // That failed; log a message and give up.
671 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
678 case RPCAP_MSG_CLOSE
: // The other endpoint close the pcap session
681 // Indicate to our caller that the client
682 // closed the control connection.
683 // This is used only in case of active mode.
685 client_told_us_to_close
= 1;
686 SOCK_DEBUG_MESSAGE("The other end system asked to close the connection.");
690 case RPCAP_MSG_STATS_REQ
:
692 if (daemon_msg_stats_req(&pars
, session
, plen
, &stats
, svrcapt
) == -1)
694 // Fatal error; a message has
695 // been logged, so just give up.
701 case RPCAP_MSG_ENDCAP_REQ
: // The other endpoint close the current capture session
705 // Save statistics (we can need them in the future)
706 if (pcap_stats(session
->fp
, &stats
))
708 svrcapt
= session
->TotCapt
;
718 if (daemon_msg_endcap_req(&pars
, session
, &threaddata
) == -1)
722 // Fatal error; a message has
723 // been logged, so just give up.
731 rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
,
732 pars
.protocol_version
,
734 "Device not opened. Cannot close the capture",
740 case RPCAP_MSG_SETSAMPLING_REQ
:
742 if (daemon_msg_setsampling_req(&pars
, plen
, &samp_param
) == -1)
744 // Fatal error; a message has
745 // been logged, so just give up.
751 case RPCAP_MSG_AUTH_REQ
:
754 // We're already authenticated; you don't
755 // get to reauthenticate.
757 rpcapd_log(LOGPRIO_INFO
, "The client sent an RPCAP_MSG_AUTH_REQ message after authentication was completed");
758 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
,
759 pars
.protocol_version
,
761 "RPCAP_MSG_AUTH_REQ request sent after authentication was completed",
764 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
767 // Discard the rest of the message.
768 if (rpcapd_discard(pars
.sockctrl_in
, pars
.ssl
, plen
) == -1)
775 case RPCAP_MSG_PACKET
:
776 case RPCAP_MSG_FINDALLIF_REPLY
:
777 case RPCAP_MSG_OPEN_REPLY
:
778 case RPCAP_MSG_STARTCAP_REPLY
:
779 case RPCAP_MSG_UPDATEFILTER_REPLY
:
780 case RPCAP_MSG_AUTH_REPLY
:
781 case RPCAP_MSG_STATS_REPLY
:
782 case RPCAP_MSG_ENDCAP_REPLY
:
783 case RPCAP_MSG_SETSAMPLING_REPLY
:
785 // These are server-to-client messages.
787 msg_type_string
= rpcap_msg_type_string(header
.type
);
788 if (msg_type_string
!= NULL
)
790 rpcapd_log(LOGPRIO_INFO
, "The client sent a %s server-to-client message", msg_type_string
);
791 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Server-to-client message %s received from client", msg_type_string
);
795 rpcapd_log(LOGPRIO_INFO
, "The client sent a server-to-client message of type %u", header
.type
);
796 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Server-to-client message of type %u received from client", header
.type
);
798 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
,
799 pars
.protocol_version
, PCAP_ERR_WRONGMSG
,
800 errmsgbuf
, errbuf
) == -1)
802 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
805 // Discard the rest of the message.
806 if (rpcapd_discard(pars
.sockctrl_in
, pars
.ssl
, plen
) == -1)
815 // Unknown message type.
817 rpcapd_log(LOGPRIO_INFO
, "The client sent a message of type %u", header
.type
);
818 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Unknown message type %u", header
.type
);
819 if (rpcap_senderror(pars
.sockctrl_out
, pars
.ssl
,
820 pars
.protocol_version
, PCAP_ERR_WRONGMSG
,
821 errbuf
, errmsgbuf
) == -1)
823 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
826 // Discard the rest of the message.
827 if (rpcapd_discard(pars
.sockctrl_in
, pars
.ssl
, plen
) == -1)
838 // The child thread is about to end
840 // perform pcap_t cleanup, in case it has not been done
843 if (threaddata
.have_thread
)
847 // Tell the data connection thread main capture
848 // loop to break out of that loop.
850 pcap_breakloop(session
->fp
);
853 // If it's currently blocked waiting for packets
854 // to arrive, try to wake it up, so it can see
855 // the "break out of the loop" indication.
857 SetEvent(pcap_getevent(session
->fp
));
860 // Wait for the thread to exit, so we don't close
861 // sockets out from under it.
863 // XXX - have a timeout, so we don't wait forever?
865 WaitForSingleObject(threaddata
.thread
, INFINITE
);
868 // Release the thread handle, as we're done with
871 CloseHandle(threaddata
.thread
);
873 pthread_cancel(threaddata
.thread
);
875 threaddata
.have_thread
= 0;
878 session_close(session
);
883 // Print message and return
884 SOCK_DEBUG_MESSAGE("I'm exiting from the child loop");
885 SOCK_DEBUG_MESSAGE(errbuf
);
887 return client_told_us_to_close
;
891 * This handles the RPCAP_MSG_ERR message.
894 daemon_msg_err(SOCKET sockctrl_in
, SSL
*ssl
, uint32 plen
)
896 char errbuf
[PCAP_ERRBUF_SIZE
];
897 char remote_errbuf
[PCAP_ERRBUF_SIZE
];
899 if (plen
>= PCAP_ERRBUF_SIZE
)
902 * Message is too long; just read as much of it as we
903 * can into the buffer provided, and discard the rest.
905 if (sock_recv(sockctrl_in
, ssl
, remote_errbuf
, PCAP_ERRBUF_SIZE
- 1,
906 SOCK_RECEIVEALL_YES
|SOCK_EOF_IS_ERROR
, errbuf
,
907 PCAP_ERRBUF_SIZE
) == -1)
910 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
913 if (rpcapd_discard(sockctrl_in
, ssl
, plen
- (PCAP_ERRBUF_SIZE
- 1)) == -1)
922 remote_errbuf
[PCAP_ERRBUF_SIZE
- 1] = '\0';
926 /* Empty error string. */
927 remote_errbuf
[0] = '\0';
931 if (sock_recv(sockctrl_in
, ssl
, remote_errbuf
, plen
,
932 SOCK_RECEIVEALL_YES
|SOCK_EOF_IS_ERROR
, errbuf
,
933 PCAP_ERRBUF_SIZE
) == -1)
936 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
943 remote_errbuf
[plen
] = '\0';
946 rpcapd_log(LOGPRIO_ERROR
, "Error from client: %s", remote_errbuf
);
951 * This handles the RPCAP_MSG_AUTH_REQ message.
952 * It checks if the authentication credentials supplied by the user are valid.
954 * This function is called if the daemon receives a RPCAP_MSG_AUTH_REQ
955 * message in its authentication loop. It reads the body of the
956 * authentication message from the network and checks whether the
957 * credentials are valid.
959 * \param sockctrl: the socket for the control connection.
961 * \param nullAuthAllowed: '1' if the NULL authentication is allowed.
963 * \param errbuf: a user-allocated buffer in which the error message
964 * (if one) has to be written. It must be at least PCAP_ERRBUF_SIZE
967 * \return '0' if everything is fine, '-1' if an unrecoverable error occurred,
968 * or '-2' if the authentication failed. For errors, an error message is
969 * returned in the 'errbuf' variable; this gives a message for the
970 * unrecoverable error or for the authentication failure.
973 daemon_msg_auth_req(struct daemon_slpars
*pars
, uint32 plen
)
975 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
976 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
977 struct rpcap_header header
; // RPCAP message general header
979 struct rpcap_auth auth
; // RPCAP authentication header
981 status
= rpcapd_recv(pars
->sockctrl_in
, pars
->ssl
, (char *) &auth
, sizeof(struct rpcap_auth
), &plen
, errmsgbuf
);
991 switch (ntohs(auth
.type
))
993 case RPCAP_RMTAUTH_NULL
:
995 if (!pars
->nullAuthAllowed
)
997 // Send the client an error reply.
998 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Authentication failed; NULL authentication not permitted.");
1004 case RPCAP_RMTAUTH_PWD
:
1006 char *username
, *passwd
;
1007 uint32 usernamelen
, passwdlen
;
1009 usernamelen
= ntohs(auth
.slen1
);
1010 username
= (char *) malloc (usernamelen
+ 1);
1011 if (username
== NULL
)
1013 pcap_fmt_errmsg_for_errno(errmsgbuf
,
1014 PCAP_ERRBUF_SIZE
, errno
, "malloc() failed");
1017 status
= rpcapd_recv(pars
->sockctrl_in
, pars
->ssl
, username
, usernamelen
, &plen
, errmsgbuf
);
1028 username
[usernamelen
] = '\0';
1030 passwdlen
= ntohs(auth
.slen2
);
1031 passwd
= (char *) malloc (passwdlen
+ 1);
1034 pcap_fmt_errmsg_for_errno(errmsgbuf
,
1035 PCAP_ERRBUF_SIZE
, errno
, "malloc() failed");
1039 status
= rpcapd_recv(pars
->sockctrl_in
, pars
->ssl
, passwd
, passwdlen
, &plen
, errmsgbuf
);
1052 passwd
[passwdlen
] = '\0';
1054 if (daemon_AuthUserPwd(username
, passwd
, errmsgbuf
))
1057 // Authentication failed. Let the client
1062 if (rpcap_senderror(pars
->sockctrl_out
, pars
->ssl
,
1063 pars
->protocol_version
,
1064 PCAP_ERR_AUTH
, errmsgbuf
, errbuf
) == -1)
1066 // That failed; log a message and give up.
1067 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1072 // Suspend for 1 second, so that they can't
1073 // hammer us with repeated tries with an
1074 // attack such as a dictionary attack.
1076 // WARNING: this delay is inserted only
1077 // at this point; if the client closes the
1078 // connection and reconnects, the suspension
1079 // time does not have any effect.
1081 sleep_secs(RPCAP_SUSPEND_WRONGAUTH
);
1091 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Authentication type not recognized.");
1095 // The authentication succeeded; let the client know.
1096 rpcap_createhdr(&header
, pars
->protocol_version
, RPCAP_MSG_AUTH_REPLY
, 0, 0);
1098 // Send the ok message back
1099 if (sock_send(pars
->sockctrl_out
, pars
->ssl
, (char *) &header
, sizeof (struct rpcap_header
), errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1101 // That failed; log a messsage and give up.
1102 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1106 // Check if all the data has been read; if not, discard the data in excess
1107 if (rpcapd_discard(pars
->sockctrl_in
, pars
->ssl
, plen
) == -1)
1115 if (rpcap_senderror(pars
->sockctrl_out
, pars
->ssl
, pars
->protocol_version
,
1116 PCAP_ERR_AUTH
, errmsgbuf
, errbuf
) == -1)
1118 // That failed; log a message and give up.
1119 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1124 // Check if all the data has been read; if not, discard the data in excess
1125 if (rpcapd_discard(pars
->sockctrl_in
, pars
->ssl
, plen
) == -1)
1134 daemon_AuthUserPwd(char *username
, char *password
, char *errbuf
)
1138 * Warning: the user which launches the process must have the
1139 * SE_TCB_NAME right.
1140 * This corresponds to have the "Act as part of the Operating System"
1141 * turned on (administrative tools, local security settings, local
1142 * policies, user right assignment)
1143 * However, it seems to me that if you run it as a service, this
1144 * right should be provided by default.
1147 if (LogonUser(username
, ".", password
, LOGON32_LOGON_NETWORK
, LOGON32_PROVIDER_DEFAULT
, &Token
) == 0)
1151 error
= GetLastError();
1152 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM
, NULL
, error
, 0, errbuf
,
1153 PCAP_ERRBUF_SIZE
, NULL
);
1158 // This call should change the current thread to the selected user.
1159 // I didn't test it.
1160 if (ImpersonateLoggedOnUser(Token
) == 0)
1164 error
= GetLastError();
1165 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM
, NULL
, error
, 0, errbuf
,
1166 PCAP_ERRBUF_SIZE
, NULL
);
1179 * http://www.unixpapa.com/incnote/passwd.html
1181 * We use the Solaris/Linux shadow password authentication if
1182 * we have getspnam(), otherwise we just do traditional
1183 * authentication, which, on some platforms, might work, even
1184 * with shadow passwords, if we're running as root. Traditional
1185 * authenticaion won't work if we're not running as root, as
1186 * I think these days all UN*Xes either won't return the password
1187 * at all with getpwnam() or will only do so if you're root.
1189 * XXX - perhaps what we *should* be using is PAM, if we have
1190 * it. That might hide all the details of username/password
1191 * authentication, whether it's done with a visible-to-root-
1192 * only password database or some other authentication mechanism,
1195 struct passwd
*user
;
1196 char *user_password
;
1197 #ifdef HAVE_GETSPNAM
1198 struct spwd
*usersp
;
1201 // This call is needed to get the uid
1202 if ((user
= getpwnam(username
)) == NULL
)
1204 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Authentication failed: no such user");
1208 #ifdef HAVE_GETSPNAM
1209 // This call is needed to get the password; otherwise 'x' is returned
1210 if ((usersp
= getspnam(username
)) == NULL
)
1212 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Authentication failed: no such user");
1215 user_password
= usersp
->sp_pwdp
;
1218 * XXX - what about other platforms?
1219 * The unixpapa.com page claims this Just Works on *BSD if you're
1220 * running as root - it's from 2000, so it doesn't indicate whether
1221 * macOS (which didn't come out until 2001, under the name Mac OS
1222 * X) behaves like the *BSDs or not, and might also work on AIX.
1223 * HP-UX does something else.
1225 * Again, hopefully PAM hides all that.
1227 user_password
= user
->pw_passwd
;
1230 if (strcmp(user_password
, (char *) crypt(password
, user_password
)) != 0)
1232 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Authentication failed: password incorrect");
1236 if (setuid(user
->pw_uid
))
1238 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1243 /* if (setgid(user->pw_gid))
1245 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1257 daemon_msg_findallif_req(struct daemon_slpars
*pars
, uint32 plen
)
1259 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
1260 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
1261 char sendbuf
[RPCAP_NETBUF_SIZE
]; // temporary buffer in which data to be sent is buffered
1262 int sendbufidx
= 0; // index which keeps the number of bytes currently buffered
1263 pcap_if_t
*alldevs
= NULL
; // pointer to the header of the interface chain
1264 pcap_if_t
*d
; // temp pointer needed to scan the interface chain
1265 struct pcap_addr
*address
; // pcap structure that keeps a network address of an interface
1266 struct rpcap_findalldevs_if
*findalldevs_if
;// rpcap structure that packet all the data of an interface together
1267 uint16 nif
= 0; // counts the number of interface listed
1269 // Discard the rest of the message; there shouldn't be any payload.
1270 if (rpcapd_discard(pars
->sockctrl_in
, pars
->ssl
, plen
) == -1)
1276 // Retrieve the device list
1277 if (pcap_findalldevs(&alldevs
, errmsgbuf
) == -1)
1280 if (alldevs
== NULL
)
1282 if (rpcap_senderror(pars
->sockctrl_out
, pars
->ssl
, pars
->protocol_version
,
1283 PCAP_ERR_NOREMOTEIF
,
1284 "No interfaces found! Make sure libpcap/WinPcap is properly installed"
1285 " and you have the right to access to the remote device.",
1288 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1294 // checks the number of interfaces and it computes the total length of the payload
1295 for (d
= alldevs
; d
!= NULL
; d
= d
->next
)
1300 plen
+= strlen(d
->description
);
1302 plen
+= strlen(d
->name
);
1304 plen
+= sizeof(struct rpcap_findalldevs_if
);
1306 for (address
= d
->addresses
; address
!= NULL
; address
= address
->next
)
1309 * Send only IPv4 and IPv6 addresses over the wire.
1311 switch (address
->addr
->sa_family
)
1317 plen
+= (sizeof(struct rpcap_sockaddr
) * 4);
1326 // RPCAP findalldevs command
1327 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
,
1328 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
,
1329 PCAP_ERRBUF_SIZE
) == -1)
1332 rpcap_createhdr((struct rpcap_header
*) sendbuf
, pars
->protocol_version
,
1333 RPCAP_MSG_FINDALLIF_REPLY
, nif
, plen
);
1335 // send the interface list
1336 for (d
= alldevs
; d
!= NULL
; d
= d
->next
)
1338 uint16 lname
, ldescr
;
1340 findalldevs_if
= (struct rpcap_findalldevs_if
*) &sendbuf
[sendbufidx
];
1342 if (sock_bufferize(NULL
, sizeof(struct rpcap_findalldevs_if
), NULL
,
1343 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1346 memset(findalldevs_if
, 0, sizeof(struct rpcap_findalldevs_if
));
1348 if (d
->description
) ldescr
= (short) strlen(d
->description
);
1350 if (d
->name
) lname
= (short) strlen(d
->name
);
1353 findalldevs_if
->desclen
= htons(ldescr
);
1354 findalldevs_if
->namelen
= htons(lname
);
1355 findalldevs_if
->flags
= htonl(d
->flags
);
1357 for (address
= d
->addresses
; address
!= NULL
; address
= address
->next
)
1360 * Send only IPv4 and IPv6 addresses over the wire.
1362 switch (address
->addr
->sa_family
)
1368 findalldevs_if
->naddr
++;
1375 findalldevs_if
->naddr
= htons(findalldevs_if
->naddr
);
1377 if (sock_bufferize(d
->name
, lname
, sendbuf
, &sendbufidx
,
1378 RPCAP_NETBUF_SIZE
, SOCKBUF_BUFFERIZE
, errmsgbuf
,
1379 PCAP_ERRBUF_SIZE
) == -1)
1382 if (sock_bufferize(d
->description
, ldescr
, sendbuf
, &sendbufidx
,
1383 RPCAP_NETBUF_SIZE
, SOCKBUF_BUFFERIZE
, errmsgbuf
,
1384 PCAP_ERRBUF_SIZE
) == -1)
1387 // send all addresses
1388 for (address
= d
->addresses
; address
!= NULL
; address
= address
->next
)
1390 struct rpcap_sockaddr
*sockaddr
;
1393 * Send only IPv4 and IPv6 addresses over the wire.
1395 switch (address
->addr
->sa_family
)
1401 sockaddr
= (struct rpcap_sockaddr
*) &sendbuf
[sendbufidx
];
1402 if (sock_bufferize(NULL
, sizeof(struct rpcap_sockaddr
), NULL
,
1403 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1405 daemon_seraddr((struct sockaddr_storage
*) address
->addr
, sockaddr
);
1407 sockaddr
= (struct rpcap_sockaddr
*) &sendbuf
[sendbufidx
];
1408 if (sock_bufferize(NULL
, sizeof(struct rpcap_sockaddr
), NULL
,
1409 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1411 daemon_seraddr((struct sockaddr_storage
*) address
->netmask
, sockaddr
);
1413 sockaddr
= (struct rpcap_sockaddr
*) &sendbuf
[sendbufidx
];
1414 if (sock_bufferize(NULL
, sizeof(struct rpcap_sockaddr
), NULL
,
1415 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1417 daemon_seraddr((struct sockaddr_storage
*) address
->broadaddr
, sockaddr
);
1419 sockaddr
= (struct rpcap_sockaddr
*) &sendbuf
[sendbufidx
];
1420 if (sock_bufferize(NULL
, sizeof(struct rpcap_sockaddr
), NULL
,
1421 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1423 daemon_seraddr((struct sockaddr_storage
*) address
->dstaddr
, sockaddr
);
1432 // We no longer need the device list. Free it.
1433 pcap_freealldevs(alldevs
);
1435 // Send a final command that says "now send it!"
1436 if (sock_send(pars
->sockctrl_out
, pars
->ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1438 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1446 pcap_freealldevs(alldevs
);
1448 if (rpcap_senderror(pars
->sockctrl_out
, pars
->ssl
, pars
->protocol_version
,
1449 PCAP_ERR_FINDALLIF
, errmsgbuf
, errbuf
) == -1)
1451 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1458 \param plen: the length of the current message (needed in order to be able
1459 to discard excess data in the message, if present)
1462 daemon_msg_open_req(struct daemon_slpars
*pars
, uint32 plen
, char *source
, size_t sourcelen
)
1464 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
1465 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
1466 pcap_t
*fp
; // pcap_t main variable
1468 char sendbuf
[RPCAP_NETBUF_SIZE
]; // temporary buffer in which data to be sent is buffered
1469 int sendbufidx
= 0; // index which keeps the number of bytes currently buffered
1470 struct rpcap_openreply
*openreply
; // open reply message
1472 if (plen
> sourcelen
- 1)
1474 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Source string too long");
1478 nread
= sock_recv(pars
->sockctrl_in
, pars
->ssl
, source
, plen
,
1479 SOCK_RECEIVEALL_YES
|SOCK_EOF_IS_ERROR
, errbuf
, PCAP_ERRBUF_SIZE
);
1482 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
1485 source
[nread
] = '\0';
1488 // XXX - make sure it's *not* a URL; we don't support opening
1489 // remote devices here.
1491 // Open the selected device
1492 // This is a fake open, since we do that only to get the needed parameters, then we close the device again
1493 if ((fp
= pcap_open_live(source
,
1494 1500 /* fake snaplen */,
1496 1000 /* fake timeout */,
1497 errmsgbuf
)) == NULL
)
1500 // Now, I can send a RPCAP open reply message
1501 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
, &sendbufidx
,
1502 RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1505 rpcap_createhdr((struct rpcap_header
*) sendbuf
, pars
->protocol_version
,
1506 RPCAP_MSG_OPEN_REPLY
, 0, sizeof(struct rpcap_openreply
));
1508 openreply
= (struct rpcap_openreply
*) &sendbuf
[sendbufidx
];
1510 if (sock_bufferize(NULL
, sizeof(struct rpcap_openreply
), NULL
, &sendbufidx
,
1511 RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1514 memset(openreply
, 0, sizeof(struct rpcap_openreply
));
1515 openreply
->linktype
= htonl(pcap_datalink(fp
));
1517 // We're done with the pcap_t.
1521 if (sock_send(pars
->sockctrl_out
, pars
->ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1523 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1529 if (rpcap_senderror(pars
->sockctrl_out
, pars
->ssl
, pars
->protocol_version
,
1530 PCAP_ERR_OPEN
, errmsgbuf
, errbuf
) == -1)
1532 // That failed; log a message and give up.
1533 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1537 // Check if all the data has been read; if not, discard the data in excess
1538 if (rpcapd_discard(pars
->sockctrl_in
, pars
->ssl
, plen
) == -1)
1546 \param plen: the length of the current message (needed in order to be able
1547 to discard excess data in the message, if present)
1550 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
)
1552 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
1553 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
1554 char portdata
[PCAP_BUF_SIZE
]; // temp variable needed to derive the data port
1555 char peerhost
[PCAP_BUF_SIZE
]; // temp variable needed to derive the host name of our peer
1556 struct session
*session
= NULL
; // saves state of session
1558 char sendbuf
[RPCAP_NETBUF_SIZE
]; // temporary buffer in which data to be sent is buffered
1559 int sendbufidx
= 0; // index which keeps the number of bytes currently buffered
1561 // socket-related variables
1562 SOCKET sockdata
= INVALID_SOCKET
; // socket descriptor of the data connection
1563 struct addrinfo hints
; // temp, needed to open a socket connection
1564 struct addrinfo
*addrinfo
; // temp, needed to open a socket connection
1565 struct sockaddr_storage saddr
; // temp, needed to retrieve the network data port chosen on the local machine
1566 socklen_t saddrlen
; // temp, needed to retrieve the network data port chosen on the local machine
1567 int ret
; // return value from functions
1570 pthread_attr_t detachedAttribute
; // temp, needed to set the created thread as detached
1573 // RPCAP-related variables
1574 struct rpcap_startcapreq startcapreq
; // start capture request message
1575 struct rpcap_startcapreply
*startcapreply
; // start capture reply message
1576 int serveropen_dp
; // keeps who is going to open the data connection
1580 status
= rpcapd_recv(pars
->sockctrl_in
, pars
->ssl
, (char *) &startcapreq
,
1581 sizeof(struct rpcap_startcapreq
), &plen
, errmsgbuf
);
1591 startcapreq
.flags
= ntohs(startcapreq
.flags
);
1593 // Check that the client does not ask for UDP is the server has been asked
1594 // to enforce encryption, as SSL is not supported yet with UDP:
1595 if (uses_ssl
&& (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_DGRAM
))
1597 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1598 "SSL not supported with UDP forward of remote packets");
1602 // Create a session structure
1603 session
= malloc(sizeof(struct session
));
1604 if (session
== NULL
)
1606 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Can't allocate session structure");
1610 session
->ctrl_ssl
= session
->data_ssl
= NULL
;
1612 // Open the selected device
1613 if ((session
->fp
= pcap_open_live(source
,
1614 ntohl(startcapreq
.snaplen
),
1615 (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_PROMISC
) ? 1 : 0 /* local device, other flags not needed */,
1616 ntohl(startcapreq
.read_timeout
),
1617 errmsgbuf
)) == NULL
)
1621 // Apply sampling parameters
1622 fp
->rmt_samp
.method
= samp_param
->method
;
1623 fp
->rmt_samp
.value
= samp_param
->value
;
1627 We're in active mode if:
1628 - we're using TCP, and the user wants us to be in active mode
1631 serveropen_dp
= (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_SERVEROPEN
) || (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_DGRAM
) || pars
->isactive
;
1634 Gets the sockaddr structure referred to the other peer in the ctrl connection
1636 We need that because:
1637 - if we're in passive mode, we need to know the address family we want to use
1638 (the same used for the ctrl socket)
1639 - if we're in active mode, we need to know the network address of the other host
1640 we want to connect to
1642 saddrlen
= sizeof(struct sockaddr_storage
);
1643 if (getpeername(pars
->sockctrl_in
, (struct sockaddr
*) &saddr
, &saddrlen
) == -1)
1645 sock_geterror("getpeername(): ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
1649 memset(&hints
, 0, sizeof(struct addrinfo
));
1650 hints
.ai_socktype
= (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_DGRAM
) ? SOCK_DGRAM
: SOCK_STREAM
;
1651 hints
.ai_family
= saddr
.ss_family
;
1653 // Now we have to create a new socket to send packets
1654 if (serveropen_dp
) // Data connection is opened by the server toward the client
1656 pcap_snprintf(portdata
, sizeof portdata
, "%d", ntohs(startcapreq
.portdata
));
1658 // Get the name of the other peer (needed to connect to that specific network address)
1659 if (getnameinfo((struct sockaddr
*) &saddr
, saddrlen
, peerhost
,
1660 sizeof(peerhost
), NULL
, 0, NI_NUMERICHOST
))
1662 sock_geterror("getnameinfo(): ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
1666 if (sock_initaddress(peerhost
, portdata
, &hints
, &addrinfo
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1669 if ((sockdata
= sock_open(addrinfo
, SOCKOPEN_CLIENT
, 0, errmsgbuf
, PCAP_ERRBUF_SIZE
)) == INVALID_SOCKET
)
1672 else // Data connection is opened by the client toward the server
1674 hints
.ai_flags
= AI_PASSIVE
;
1676 // Let's the server socket pick up a free network port for us
1677 if (sock_initaddress(NULL
, "0", &hints
, &addrinfo
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1680 if ((sockdata
= sock_open(addrinfo
, SOCKOPEN_SERVER
, 1 /* max 1 connection in queue */, errmsgbuf
, PCAP_ERRBUF_SIZE
)) == INVALID_SOCKET
)
1683 // get the complete sockaddr structure used in the data connection
1684 saddrlen
= sizeof(struct sockaddr_storage
);
1685 if (getsockname(sockdata
, (struct sockaddr
*) &saddr
, &saddrlen
) == -1)
1687 sock_geterror("getsockname(): ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
1691 // Get the local port the system picked up
1692 if (getnameinfo((struct sockaddr
*) &saddr
, saddrlen
, NULL
,
1693 0, portdata
, sizeof(portdata
), NI_NUMERICSERV
))
1695 sock_geterror("getnameinfo(): ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
1700 // addrinfo is no longer used
1701 freeaddrinfo(addrinfo
);
1704 // Needed to send an error on the ctrl connection
1705 session
->sockctrl_out
= pars
->sockctrl_out
;
1706 session
->ctrl_ssl
= pars
->ssl
;
1707 session
->protocol_version
= pars
->protocol_version
;
1709 // Now I can set the filter
1710 ret
= daemon_unpackapplyfilter(pars
->sockctrl_in
, pars
->ssl
, session
, &plen
, errmsgbuf
);
1713 // Fatal error. A message has been logged; just give up.
1718 // Non-fatal error. Send an error message to the client.
1722 // Now, I can send a RPCAP start capture reply message
1723 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
, &sendbufidx
,
1724 RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1727 rpcap_createhdr((struct rpcap_header
*) sendbuf
, pars
->protocol_version
,
1728 RPCAP_MSG_STARTCAP_REPLY
, 0, sizeof(struct rpcap_startcapreply
));
1730 startcapreply
= (struct rpcap_startcapreply
*) &sendbuf
[sendbufidx
];
1732 if (sock_bufferize(NULL
, sizeof(struct rpcap_startcapreply
), NULL
,
1733 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1736 memset(startcapreply
, 0, sizeof(struct rpcap_startcapreply
));
1737 startcapreply
->bufsize
= htonl(pcap_bufsize(session
->fp
));
1741 unsigned short port
= (unsigned short)strtoul(portdata
,NULL
,10);
1742 startcapreply
->portdata
= htons(port
);
1745 if (sock_send(pars
->sockctrl_out
, pars
->ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1747 // That failed; log a message and give up.
1748 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1754 SOCKET socktemp
; // We need another socket, since we're going to accept() a connection
1756 // Connection creation
1757 saddrlen
= sizeof(struct sockaddr_storage
);
1759 socktemp
= accept(sockdata
, (struct sockaddr
*) &saddr
, &saddrlen
);
1761 if (socktemp
== INVALID_SOCKET
)
1763 sock_geterror("accept(): ", errbuf
, PCAP_ERRBUF_SIZE
);
1764 rpcapd_log(LOGPRIO_ERROR
, "Accept of data connection failed: %s",
1769 // Now that I accepted the connection, the server socket is no longer needed
1770 sock_close(sockdata
, NULL
, 0);
1771 sockdata
= socktemp
;
1778 /* In both active or passive cases, wait for the client to initiate the
1779 * TLS handshake. Yes during that time the control socket will not be
1780 * served, but the same was true from the above call to accept(). */
1781 ssl
= ssl_promotion(1, sockdata
, errbuf
, PCAP_ERRBUF_SIZE
);
1784 rpcapd_log(LOGPRIO_ERROR
, "TLS handshake failed: %s", errbuf
);
1789 session
->data_ssl
= ssl
;
1790 session
->sockdata
= sockdata
;
1792 // Now we have to create a new thread to receive packets
1794 threaddata
->thread
= (HANDLE
)_beginthreadex(NULL
, 0, daemon_thrdatamain
,
1795 (void *) session
, 0, NULL
);
1796 if (threaddata
->thread
== 0)
1798 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Error creating the data thread");
1802 /* GV we need this to create the thread as detached. */
1803 /* GV otherwise, the thread handle is not destroyed */
1804 pthread_attr_init(&detachedAttribute
);
1805 pthread_attr_setdetachstate(&detachedAttribute
, PTHREAD_CREATE_DETACHED
);
1806 ret
= pthread_create(&threaddata
->thread
, &detachedAttribute
,
1807 daemon_thrdatamain
, (void *) session
);
1810 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1811 ret
, "Error creating the data thread");
1812 pthread_attr_destroy(&detachedAttribute
);
1815 pthread_attr_destroy(&detachedAttribute
);
1817 threaddata
->have_thread
= 1;
1819 // Check if all the data has been read; if not, discard the data in excess
1820 if (rpcapd_discard(pars
->sockctrl_in
, pars
->ssl
, plen
) == -1)
1823 *sessionp
= session
;
1828 // Not a fatal error, so send the client an error message and
1829 // keep serving client requests.
1834 freeaddrinfo(addrinfo
);
1836 if (threaddata
->have_thread
)
1841 pcap_breakloop(session
->fp
);
1842 SetEvent(pcap_getevent(session
->fp
));
1844 CloseHandle(threaddata
->thread
);
1846 pthread_cancel(threaddata
->thread
);
1848 threaddata
->have_thread
= 0;
1851 if (sockdata
!= INVALID_SOCKET
)
1852 sock_close(sockdata
, NULL
, 0);
1857 pcap_close(session
->fp
);
1859 if (session
->ctrl_ssl
)
1860 SSL_free(session
->ctrl_ssl
);
1861 if (session
->data_ssl
)
1862 SSL_free(session
->data_ssl
);
1867 if (rpcap_senderror(pars
->sockctrl_out
, pars
->ssl
, pars
->protocol_version
,
1868 PCAP_ERR_STARTCAPTURE
, errmsgbuf
, errbuf
) == -1)
1870 // That failed; log a message and give up.
1871 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1875 // Check if all the data has been read; if not, discard the data in excess
1876 if (rpcapd_discard(pars
->sockctrl_in
, pars
->ssl
, plen
) == -1)
1886 // Fatal network error, so don't try to communicate with
1887 // the client, just give up.
1891 if (threaddata
->have_thread
)
1894 if (session
&& session
->fp
)
1897 // Tell the data connection thread main capture
1898 // loop to break out of that loop.
1900 pcap_breakloop(session
->fp
);
1903 // If it's currently blocked waiting for packets
1904 // to arrive, try to wake it up, so it can see
1905 // the "break out of the loop" indication.
1907 SetEvent(pcap_getevent(session
->fp
));
1911 // Wait for the thread to exit, so we don't close
1912 // sockets out from under it.
1914 // XXX - have a timeout, so we don't wait forever?
1916 WaitForSingleObject(threaddata
->thread
, INFINITE
);
1919 // Release the thread handle, as we're done with
1922 CloseHandle(threaddata
->thread
);
1924 pthread_cancel(threaddata
->thread
);
1926 threaddata
->have_thread
= 0;
1929 if (sockdata
!= INVALID_SOCKET
)
1930 sock_close(sockdata
, NULL
, 0);
1935 pcap_close(session
->fp
);
1937 if (session
->ctrl_ssl
)
1938 SSL_free(session
->ctrl_ssl
);
1939 if (session
->data_ssl
)
1940 SSL_free(session
->data_ssl
);
1949 daemon_msg_endcap_req(struct daemon_slpars
*pars
, struct session
*session
, struct thread_handle
*threaddata
)
1951 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
1952 struct rpcap_header header
;
1954 if (threaddata
->have_thread
)
1958 // Tell the data connection thread main capture loop to
1959 // break out of that loop.
1961 pcap_breakloop(session
->fp
);
1964 // If it's currently blocked waiting for packets to
1965 // arrive, try to wake it up, so it can see the "break
1966 // out of the loop" indication.
1968 SetEvent(pcap_getevent(session
->fp
));
1971 // Wait for the thread to exit, so we don't close
1972 // sockets out from under it.
1974 // XXX - have a timeout, so we don't wait forever?
1976 WaitForSingleObject(threaddata
->thread
, INFINITE
);
1979 // Release the thread handle, as we're done with
1982 CloseHandle(threaddata
->thread
);
1984 pthread_cancel(threaddata
->thread
);
1986 threaddata
->have_thread
= 0;
1989 session_close(session
);
1991 rpcap_createhdr(&header
, pars
->protocol_version
,
1992 RPCAP_MSG_ENDCAP_REPLY
, 0, 0);
1994 if (sock_send(pars
->sockctrl_out
, pars
->ssl
, (char *) &header
, sizeof(struct rpcap_header
), errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1996 // That failed; log a message and give up.
1997 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2005 daemon_unpackapplyfilter(SOCKET sockctrl_in
, SSL
*ctrl_ssl
, struct session
*session
, uint32
*plenp
, char *errmsgbuf
)
2008 struct rpcap_filter filter
;
2009 struct rpcap_filterbpf_insn insn
;
2010 struct bpf_insn
*bf_insn
;
2011 struct bpf_program bf_prog
;
2014 status
= rpcapd_recv(sockctrl_in
, ctrl_ssl
, (char *) &filter
,
2015 sizeof(struct rpcap_filter
), plenp
, errmsgbuf
);
2025 bf_prog
.bf_len
= ntohl(filter
.nitems
);
2027 if (ntohs(filter
.filtertype
) != RPCAP_UPDATEFILTER_BPF
)
2029 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Only BPF/NPF filters are currently supported");
2033 bf_insn
= (struct bpf_insn
*) malloc (sizeof(struct bpf_insn
) * bf_prog
.bf_len
);
2034 if (bf_insn
== NULL
)
2036 pcap_fmt_errmsg_for_errno(errmsgbuf
, PCAP_ERRBUF_SIZE
,
2037 errno
, "malloc() failed");
2041 bf_prog
.bf_insns
= bf_insn
;
2043 for (i
= 0; i
< bf_prog
.bf_len
; i
++)
2045 status
= rpcapd_recv(sockctrl_in
, ctrl_ssl
, (char *) &insn
,
2046 sizeof(struct rpcap_filterbpf_insn
), plenp
, errmsgbuf
);
2056 bf_insn
->code
= ntohs(insn
.code
);
2057 bf_insn
->jf
= insn
.jf
;
2058 bf_insn
->jt
= insn
.jt
;
2059 bf_insn
->k
= ntohl(insn
.k
);
2065 // XXX - pcap_setfilter() should do the validation for us.
2067 if (bpf_validate(bf_prog
.bf_insns
, bf_prog
.bf_len
) == 0)
2069 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "The filter contains bogus instructions");
2073 if (pcap_setfilter(session
->fp
, &bf_prog
))
2075 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "RPCAP error: %s", pcap_geterr(session
->fp
));
2083 daemon_msg_updatefilter_req(struct daemon_slpars
*pars
, struct session
*session
, uint32 plen
)
2085 char errbuf
[PCAP_ERRBUF_SIZE
];
2086 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
2087 int ret
; // status of daemon_unpackapplyfilter()
2088 struct rpcap_header header
; // keeps the answer to the updatefilter command
2090 ret
= daemon_unpackapplyfilter(pars
->sockctrl_in
, pars
->ssl
, session
, &plen
, errmsgbuf
);
2093 // Fatal error. A message has been logged; just give up.
2098 // Non-fatal error. Send an error reply to the client.
2102 // Check if all the data has been read; if not, discard the data in excess
2103 if (rpcapd_discard(pars
->sockctrl_in
, pars
->ssl
, plen
) == -1)
2109 // A response is needed, otherwise the other host does not know that everything went well
2110 rpcap_createhdr(&header
, pars
->protocol_version
,
2111 RPCAP_MSG_UPDATEFILTER_REPLY
, 0, 0);
2113 if (sock_send(pars
->sockctrl_out
, pars
->ssl
, (char *) &header
, sizeof (struct rpcap_header
), pcap_geterr(session
->fp
), PCAP_ERRBUF_SIZE
))
2115 // That failed; log a messsage and give up.
2116 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2123 if (rpcapd_discard(pars
->sockctrl_in
, pars
->ssl
, plen
) == -1)
2127 rpcap_senderror(pars
->sockctrl_out
, pars
->ssl
, pars
->protocol_version
,
2128 PCAP_ERR_UPDATEFILTER
, errmsgbuf
, NULL
);
2134 \brief Received the sampling parameters from remote host and it stores in the pcap_t structure.
2137 daemon_msg_setsampling_req(struct daemon_slpars
*pars
, uint32 plen
, struct rpcap_sampling
*samp_param
)
2139 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2140 char errmsgbuf
[PCAP_ERRBUF_SIZE
];
2141 struct rpcap_header header
;
2142 struct rpcap_sampling rpcap_samp
;
2145 status
= rpcapd_recv(pars
->sockctrl_in
, pars
->ssl
, (char *) &rpcap_samp
, sizeof(struct rpcap_sampling
), &plen
, errmsgbuf
);
2155 // Save these settings in the pcap_t
2156 samp_param
->method
= rpcap_samp
.method
;
2157 samp_param
->value
= ntohl(rpcap_samp
.value
);
2159 // A response is needed, otherwise the other host does not know that everything went well
2160 rpcap_createhdr(&header
, pars
->protocol_version
,
2161 RPCAP_MSG_SETSAMPLING_REPLY
, 0, 0);
2163 if (sock_send(pars
->sockctrl_out
, pars
->ssl
, (char *) &header
, sizeof (struct rpcap_header
), errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2165 // That failed; log a messsage and give up.
2166 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2170 if (rpcapd_discard(pars
->sockctrl_in
, pars
->ssl
, plen
) == -1)
2178 if (rpcap_senderror(pars
->sockctrl_out
, pars
->ssl
, pars
->protocol_version
,
2179 PCAP_ERR_AUTH
, errmsgbuf
, errbuf
) == -1)
2181 // That failed; log a message and give up.
2182 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2186 // Check if all the data has been read; if not, discard the data in excess
2187 if (rpcapd_discard(pars
->sockctrl_in
, pars
->ssl
, plen
) == -1)
2196 daemon_msg_stats_req(struct daemon_slpars
*pars
, struct session
*session
, uint32 plen
, struct pcap_stat
*stats
, unsigned int svrcapt
)
2198 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2199 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
2200 char sendbuf
[RPCAP_NETBUF_SIZE
]; // temporary buffer in which data to be sent is buffered
2201 int sendbufidx
= 0; // index which keeps the number of bytes currently buffered
2202 struct rpcap_stats
*netstats
; // statistics sent on the network
2204 // Checks that the header does not contain other data; if so, discard it
2205 if (rpcapd_discard(pars
->sockctrl_in
, pars
->ssl
, plen
) == -1)
2211 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
,
2212 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
2215 rpcap_createhdr((struct rpcap_header
*) sendbuf
, pars
->protocol_version
,
2216 RPCAP_MSG_STATS_REPLY
, 0, (uint16
) sizeof(struct rpcap_stats
));
2218 netstats
= (struct rpcap_stats
*) &sendbuf
[sendbufidx
];
2220 if (sock_bufferize(NULL
, sizeof(struct rpcap_stats
), NULL
,
2221 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
2224 if (session
&& session
->fp
)
2226 if (pcap_stats(session
->fp
, stats
) == -1)
2228 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "%s", pcap_geterr(session
->fp
));
2232 netstats
->ifdrop
= htonl(stats
->ps_ifdrop
);
2233 netstats
->ifrecv
= htonl(stats
->ps_recv
);
2234 netstats
->krnldrop
= htonl(stats
->ps_drop
);
2235 netstats
->svrcapt
= htonl(session
->TotCapt
);
2239 // We have to keep compatibility with old applications,
2240 // which ask for statistics also when the capture has
2242 netstats
->ifdrop
= htonl(stats
->ps_ifdrop
);
2243 netstats
->ifrecv
= htonl(stats
->ps_recv
);
2244 netstats
->krnldrop
= htonl(stats
->ps_drop
);
2245 netstats
->svrcapt
= htonl(svrcapt
);
2249 if (sock_send(pars
->sockctrl_out
, pars
->ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2251 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2258 rpcap_senderror(pars
->sockctrl_out
, pars
->ssl
, pars
->protocol_version
,
2259 PCAP_ERR_GETSTATS
, errmsgbuf
, NULL
);
2264 static unsigned __stdcall
2268 daemon_thrdatamain(void *ptr
)
2270 char errbuf
[PCAP_ERRBUF_SIZE
+ 1]; // error buffer
2271 struct session
*session
; // pointer to the struct session for this session
2272 int retval
; // general variable used to keep the return value of other functions
2273 struct rpcap_pkthdr
*net_pkt_header
;// header of the packet
2274 struct pcap_pkthdr
*pkt_header
; // pointer to the buffer that contains the header of the current packet
2275 u_char
*pkt_data
; // pointer to the buffer that contains the current packet
2276 size_t sendbufsize
; // size for the send buffer
2277 char *sendbuf
; // temporary buffer in which data to be sent is buffered
2278 int sendbufidx
; // index which keeps the number of bytes currently buffered
2281 session
= (struct session
*) ptr
;
2283 session
->TotCapt
= 0; // counter which is incremented each time a packet is received
2285 // Initialize errbuf
2286 memset(errbuf
, 0, sizeof(errbuf
));
2289 // We need a buffer large enough to hold a buffer large enough
2290 // for a maximum-size packet for this pcap_t.
2292 if (pcap_snapshot(session
->fp
) < 0)
2295 // The snapshot length is negative.
2296 // This "should not happen".
2298 rpcapd_log(LOGPRIO_ERROR
,
2299 "Unable to allocate the buffer for this child thread: snapshot length of %d is negative",
2300 pcap_snapshot(session
->fp
));
2301 sendbuf
= NULL
; // we can't allocate a buffer, so nothing to free
2305 // size_t is unsigned, and the result of pcap_snapshot() is signed;
2306 // on no platform that we support is int larger than size_t.
2307 // This means that, unless the extra information we prepend to
2308 // a maximum-sized packet is impossibly large, the sum of the
2309 // snapshot length and the size of that extra information will
2312 // So we don't need to make sure that sendbufsize will overflow.
2314 // However, we *do* need to make sure its value fits in an int,
2315 // because sock_send() can't send more than INT_MAX bytes (it could
2316 // do so on 64-bit UN*Xes, but can't do so on Windows, not even
2317 // 64-bit Windows, as the send() buffer size argument is an int
2320 sendbufsize
= sizeof(struct rpcap_header
) + sizeof(struct rpcap_pkthdr
) + pcap_snapshot(session
->fp
);
2321 if (sendbufsize
> INT_MAX
)
2323 rpcapd_log(LOGPRIO_ERROR
,
2324 "Buffer size for this child thread would be larger than %d",
2326 sendbuf
= NULL
; // we haven't allocated a buffer, so nothing to free
2329 sendbuf
= (char *) malloc (sendbufsize
);
2330 if (sendbuf
== NULL
)
2332 rpcapd_log(LOGPRIO_ERROR
,
2333 "Unable to allocate the buffer for this child thread");
2338 // Modify thread params so that it can be killed at any time
2339 retval
= pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, NULL
);
2342 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
2343 retval
, "pthread_setcancelstate");
2344 rpcapd_log(LOGPRIO_ERROR
,
2345 "Can't set cancel state on data thread: %s", errbuf
);
2348 retval
= pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS
, NULL
);
2351 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
2352 retval
, "pthread_setcanceltype");
2353 rpcapd_log(LOGPRIO_ERROR
,
2354 "Can't set cancel type on data thread: %s", errbuf
);
2359 // Retrieve the packets
2360 while ((retval
= pcap_next_ex(session
->fp
, &pkt_header
, (const u_char
**) &pkt_data
)) >= 0) // cast to avoid a compiler warning
2362 if (retval
== 0) // Read timeout elapsed
2367 // Bufferize the general header
2368 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
,
2369 &sendbufidx
, (int)sendbufsize
, SOCKBUF_CHECKONLY
, errbuf
,
2370 PCAP_ERRBUF_SIZE
) == -1)
2372 rpcapd_log(LOGPRIO_ERROR
,
2373 "sock_bufferize() error sending packet message: %s",
2378 rpcap_createhdr((struct rpcap_header
*) sendbuf
,
2379 session
->protocol_version
, RPCAP_MSG_PACKET
, 0,
2380 (uint16
) (sizeof(struct rpcap_pkthdr
) + pkt_header
->caplen
));
2382 net_pkt_header
= (struct rpcap_pkthdr
*) &sendbuf
[sendbufidx
];
2384 // Bufferize the pkt header
2385 if (sock_bufferize(NULL
, sizeof(struct rpcap_pkthdr
), NULL
,
2386 &sendbufidx
, (int)sendbufsize
, SOCKBUF_CHECKONLY
, errbuf
,
2387 PCAP_ERRBUF_SIZE
) == -1)
2389 rpcapd_log(LOGPRIO_ERROR
,
2390 "sock_bufferize() error sending packet message: %s",
2395 net_pkt_header
->caplen
= htonl(pkt_header
->caplen
);
2396 net_pkt_header
->len
= htonl(pkt_header
->len
);
2397 net_pkt_header
->npkt
= htonl(++(session
->TotCapt
));
2399 // This protocol needs to be updated with a new version
2400 // before 2038-01-19 03:14:07 UTC.
2402 net_pkt_header
->timestamp_sec
= htonl((uint32
)pkt_header
->ts
.tv_sec
);
2403 net_pkt_header
->timestamp_usec
= htonl((uint32
)pkt_header
->ts
.tv_usec
);
2405 // Bufferize the pkt data
2406 if (sock_bufferize((char *) pkt_data
, pkt_header
->caplen
,
2407 sendbuf
, &sendbufidx
, (int)sendbufsize
, SOCKBUF_BUFFERIZE
,
2408 errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2410 rpcapd_log(LOGPRIO_ERROR
,
2411 "sock_bufferize() error sending packet message: %s",
2417 // If the client dropped the connection, don't report an
2418 // error, just quit.
2419 status
= sock_send(session
->sockdata
, session
->data_ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
);
2425 // Error other than "client closed the
2426 // connection out from under us"; report
2429 rpcapd_log(LOGPRIO_ERROR
,
2430 "Send of packet to client failed: %s",
2435 // Give up in either case.
2443 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Error reading the packets: %s", pcap_geterr(session
->fp
));
2444 rpcap_senderror(session
->sockctrl_out
, session
->ctrl_ssl
, session
->protocol_version
,
2445 PCAP_ERR_READEX
, errbuf
, NULL
);
2450 session_close(session
);
2458 \brief It serializes a network address.
2460 It accepts a 'sockaddr_storage' structure as input, and it converts it appropriately into a format
2461 that can be used to be sent on the network. Basically, it applies all the hton()
2462 conversion required to the input variable.
2464 \param sockaddrin a 'sockaddr_storage' pointer to the variable that has to be
2465 serialized. This variable can be both a 'sockaddr_in' and 'sockaddr_in6'.
2467 \param sockaddrout an 'rpcap_sockaddr' pointer to the variable that will contain
2468 the serialized data. This variable has to be allocated by the user.
2470 \warning This function supports only AF_INET and AF_INET6 address families.
2473 daemon_seraddr(struct sockaddr_storage
*sockaddrin
, struct rpcap_sockaddr
*sockaddrout
)
2475 memset(sockaddrout
, 0, sizeof(struct sockaddr_storage
));
2477 // There can be the case in which the sockaddrin is not available
2478 if (sockaddrin
== NULL
) return;
2480 // Warning: we support only AF_INET and AF_INET6
2481 switch (sockaddrin
->ss_family
)
2485 struct sockaddr_in
*sockaddrin_ipv4
;
2486 struct rpcap_sockaddr_in
*sockaddrout_ipv4
;
2488 sockaddrin_ipv4
= (struct sockaddr_in
*) sockaddrin
;
2489 sockaddrout_ipv4
= (struct rpcap_sockaddr_in
*) sockaddrout
;
2490 sockaddrout_ipv4
->family
= htons(RPCAP_AF_INET
);
2491 sockaddrout_ipv4
->port
= htons(sockaddrin_ipv4
->sin_port
);
2492 memcpy(&sockaddrout_ipv4
->addr
, &sockaddrin_ipv4
->sin_addr
, sizeof(sockaddrout_ipv4
->addr
));
2493 memset(sockaddrout_ipv4
->zero
, 0, sizeof(sockaddrout_ipv4
->zero
));
2500 struct sockaddr_in6
*sockaddrin_ipv6
;
2501 struct rpcap_sockaddr_in6
*sockaddrout_ipv6
;
2503 sockaddrin_ipv6
= (struct sockaddr_in6
*) sockaddrin
;
2504 sockaddrout_ipv6
= (struct rpcap_sockaddr_in6
*) sockaddrout
;
2505 sockaddrout_ipv6
->family
= htons(RPCAP_AF_INET6
);
2506 sockaddrout_ipv6
->port
= htons(sockaddrin_ipv6
->sin6_port
);
2507 sockaddrout_ipv6
->flowinfo
= htonl(sockaddrin_ipv6
->sin6_flowinfo
);
2508 memcpy(&sockaddrout_ipv6
->addr
, &sockaddrin_ipv6
->sin6_addr
, sizeof(sockaddrout_ipv6
->addr
));
2509 sockaddrout_ipv6
->scope_id
= htonl(sockaddrin_ipv6
->sin6_scope_id
);
2518 \brief Suspends a thread for secs seconds.
2520 void sleep_secs(int secs
)
2525 unsigned secs_remaining
;
2529 secs_remaining
= secs
;
2530 while (secs_remaining
!= 0)
2531 secs_remaining
= sleep(secs_remaining
);
2536 * Read the header of a message.
2539 rpcapd_recv_msg_header(SOCKET sock
, SSL
*ssl
, struct rpcap_header
*headerp
)
2542 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2544 nread
= sock_recv(sock
, ssl
, (char *) headerp
, sizeof(struct rpcap_header
),
2545 SOCK_RECEIVEALL_YES
|SOCK_EOF_ISNT_ERROR
, errbuf
, PCAP_ERRBUF_SIZE
);
2549 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
2554 // Immediate EOF; that's treated like a close message.
2557 headerp
->plen
= ntohl(headerp
->plen
);
2562 * Read data from a message.
2563 * If we're trying to read more data that remains, puts an error
2564 * message into errmsgbuf and returns -2. Otherwise, tries to read
2565 * the data and, if that succeeds, subtracts the amount read from
2566 * the number of bytes of data that remains.
2567 * Returns 0 on success, logs a message and returns -1 on a network
2571 rpcapd_recv(SOCKET sock
, SSL
*ssl
, char *buffer
, size_t toread
, uint32
*plen
, char *errmsgbuf
)
2574 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2578 // Tell the client and continue.
2579 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Message payload is too short");
2582 nread
= sock_recv(sock
, ssl
, buffer
, toread
,
2583 SOCK_RECEIVEALL_YES
|SOCK_EOF_IS_ERROR
, errbuf
, PCAP_ERRBUF_SIZE
);
2586 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
2594 * Discard data from a connection.
2595 * Mostly used to discard wrong-sized messages.
2596 * Returns 0 on success, logs a message and returns -1 on a network
2600 rpcapd_discard(SOCKET sock
, SSL
*ssl
, uint32 len
)
2602 char errbuf
[PCAP_ERRBUF_SIZE
+ 1]; // keeps the error string, prior to be printed
2606 if (sock_discard(sock
, ssl
, len
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2609 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
2617 * Close the socket associated with the session, the optional SSL handle,
2618 * and the underlying packet capture handle. We of course do not touch
2619 * the controlling socket that's also copied into the session.
2621 static void session_close(struct session
*session
)
2624 if (session
->data_ssl
)
2626 SSL_free(session
->data_ssl
); // Must happen *before* the socket is closed
2627 session
->data_ssl
= NULL
;
2631 if (session
->sockdata
)
2633 sock_close(session
->sockdata
, NULL
, 0);
2634 session
->sockdata
= 0;
2637 pcap_close(session
->fp
);