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.
38 #include <errno.h> // for the errno variable
39 #include <stdlib.h> // for malloc(), free(), ...
40 #include <string.h> // for strlen(), ...
43 #include <process.h> // for threads
48 #include <sys/types.h> // for select() and such
49 #include <pwd.h> // for password management
53 #include <shadow.h> // for password management
56 #include <pcap.h> // for libpcap/WinPcap calls
59 #include "sockutils.h" // for socket calls
60 #include "portability.h"
61 #include "rpcap-protocol.h"
65 #define RPCAP_TIMEOUT_INIT 90 /* Initial timeout for RPCAP connections (default: 90 sec) */
66 #define RPCAP_TIMEOUT_RUNTIME 180 /* Run-time timeout for RPCAP connections (default: 3 min) */
67 #define RPCAP_SUSPEND_WRONGAUTH 1 /* If the authentication is wrong, stops 1 sec before accepting a new auth message */
70 * Data for a session managed by a thread.
75 uint8 protocol_version
;
80 // Locally defined functions
81 static int daemon_msg_err(SOCKET sockctrl
, uint32 plen
);
82 static int daemon_msg_auth_req(SOCKET sockctrl
, uint8 ver
, uint32 plen
, int nullAuthAllowed
);
83 static int daemon_AuthUserPwd(char *username
, char *password
, char *errbuf
);
85 static int daemon_msg_findallif_req(struct daemon_slpars
*pars
, uint32 plen
);
87 static int daemon_msg_open_req(struct daemon_slpars
*pars
, uint32 plen
, char *source
, size_t sourcelen
);
89 static int daemon_msg_startcap_req(struct daemon_slpars
*pars
, uint32 plen
, int *have_thread
, HANDLE
*threaddata
, char *source
, struct session
**sessionp
, struct rpcap_sampling
*samp_param
);
90 static int daemon_msg_endcap_req(struct daemon_slpars
*pars
, struct session
*session
, int *have_thread
, HANDLE threaddata
);
92 static int daemon_msg_startcap_req(struct daemon_slpars
*pars
, uint32 plen
, int *have_thread
, pthread_t
*threaddata
, char *source
, struct session
**sessionp
, struct rpcap_sampling
*samp_param
);
93 static int daemon_msg_endcap_req(struct daemon_slpars
*pars
, struct session
*session
, int *have_thread
, pthread_t threaddata
);
96 static int daemon_msg_updatefilter_req(struct daemon_slpars
*pars
, struct session
*session
, uint32 plen
);
97 static int daemon_unpackapplyfilter(SOCKET sockctrl
, struct session
*session
, uint32
*plenp
, char *errbuf
);
99 static int daemon_msg_stats_req(struct daemon_slpars
*pars
, struct session
*session
, uint32 plen
, struct pcap_stat
*stats
, unsigned int svrcapt
);
101 static int daemon_msg_setsampling_req(struct daemon_slpars
*pars
, uint32 plen
, struct rpcap_sampling
*samp_param
);
103 static void daemon_seraddr(struct sockaddr_storage
*sockaddrin
, struct rpcap_sockaddr
*sockaddrout
);
105 static unsigned __stdcall
daemon_thrdatamain(void *ptr
);
107 static void *daemon_thrdatamain(void *ptr
);
110 static int rpcapd_recv_msg_header(SOCKET sock
, struct rpcap_header
*headerp
);
111 static int rpcapd_recv(SOCKET sock
, char *buffer
, size_t toread
, uint32
*plen
, char *errmsgbuf
);
112 static int rpcapd_discard(SOCKET sock
, uint32 len
);
115 \brief Main serving function
116 This function is the one which does the job. It is the main() of the child
117 thread, which is created as soon as a new connection is accepted.
119 \param ptr: a void pointer that keeps the reference of the 'pthread_chain'
120 value corresponding to this thread. This variable is casted into a 'pthread_chain'
121 value in order to retrieve the socket we're currently using, the thread ID, and
122 some pointers to the previous and next elements into this struct.
127 unsigned __stdcall
daemon_serviceloop(void *ptr
)
129 void *daemon_serviceloop(void *ptr
)
132 char errbuf
[PCAP_ERRBUF_SIZE
+ 1]; // keeps the error string, prior to be printed
133 char errmsgbuf
[PCAP_ERRBUF_SIZE
+ 1]; // buffer for errors to send to the client
135 struct rpcap_header header
; // RPCAP message general header
136 uint32 plen
; // payload length from header
137 int authenticated
= 0; // 1 if the client has successfully authenticated
138 char source
[PCAP_BUF_SIZE
+1]; // keeps the string that contains the interface to open
139 int got_source
= 0; // 1 if we've gotten the source from an open request
140 struct session
*session
= NULL
; // struct session main variable
141 struct daemon_slpars
*pars
; // parameters related to the present daemon loop
142 const char *msg_type_string
; // string for message type
144 int have_thread
= 0; // 1 if threaddata refers to a thread we've created
146 HANDLE threaddata
; // handle to the 'read from daemon and send to client' thread
148 pthread_t threaddata
; // handle to the 'read from daemon and send to client' thread
151 // needed to save the values of the statistics
152 struct pcap_stat stats
;
153 unsigned int svrcapt
;
155 struct rpcap_sampling samp_param
; // in case sampling has been requested
157 // Structures needed for the select() call
158 fd_set rfds
; // set of socket descriptors we have to check
159 struct timeval tv
; // maximum time the select() can block waiting for data
160 int retval
; // select() return value
162 pars
= (struct daemon_slpars
*) ptr
;
164 *errbuf
= 0; // Initialize errbuf
167 // If we're in active mode, this is not a separate thread
168 if (! pars
->isactive
)
170 // Modify thread params so that it can be killed at any time
171 if (pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, NULL
))
173 if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS
, NULL
))
179 // The client must first authenticate; loop until they send us a
180 // message with a version we support and credentials we accept,
181 // they send us a close message indicating that they're giving up,
182 // or we get a network error or other fatal error.
184 while (!authenticated
)
187 // If we're in active mode, we have to check for the
190 // XXX - do this on *every* trip through the loop?
195 // We do not have to block here
196 tv
.tv_sec
= RPCAP_TIMEOUT_INIT
;
199 FD_SET(pars
->sockctrl
, &rfds
);
201 retval
= select(pars
->sockctrl
+ 1, &rfds
, NULL
, NULL
, &tv
);
204 sock_geterror("select failed: ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
205 if (rpcap_senderror(pars
->sockctrl
, 0, PCAP_ERR_NETW
, errmsgbuf
, errbuf
) == -1)
206 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
210 // The timeout has expired
211 // So, this was a fake connection. Drop it down
214 if (rpcap_senderror(pars
->sockctrl
, 0, PCAP_ERR_INITTIMEOUT
, "The RPCAP initial timeout has expired", errbuf
) == -1)
215 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
221 // Read the message header from the client.
223 nrecv
= rpcapd_recv_msg_header(pars
->sockctrl
, &header
);
231 // Client closed the connection.
238 // Did the client specify a version we can handle?
240 if (!RPCAP_VERSION_IS_SUPPORTED(header
.ver
))
243 // Tell them it's not a valid protocol version.
248 // If RPCAP_MIN_VERSION is 0, no version is too
249 // old, as the oldest supported version is 0,
250 // and there are no negative versions.
252 #if RPCAP_MIN_VERSION != 0
253 if (header
.ver
< RPCAP_MIN_VERSION
)
256 // Their maximum version is too old;
257 // there *is* no version we can both
258 // handle, and they might reject
259 // an error with a version they don't
260 // understand, so reply with the
261 // version they sent. That may
262 // make them retry with that version,
263 // but they'll give up on that
266 reply_version
= header
.ver
;
272 // Their maximum version is too new,
273 // but they might be able to handle
274 // *our* maximum version, so reply
275 // with that version.
277 reply_version
= RPCAP_MAX_VERSION
;
279 if (rpcap_senderror(pars
->sockctrl
, reply_version
,
280 PCAP_ERR_WRONGVER
, "RPCAP version number mismatch",
283 // That failed; log a message and give up.
284 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
288 // Discard the rest of the message.
289 if (rpcapd_discard(pars
->sockctrl
, plen
) == -1)
295 // Let them try again.
300 // OK, we use the version the client specified.
302 pars
->protocol_version
= header
.ver
;
306 case RPCAP_MSG_AUTH_REQ
:
307 retval
= daemon_msg_auth_req(pars
->sockctrl
, pars
->protocol_version
, plen
, pars
->nullAuthAllowed
);
310 // Fatal error; a message has
311 // been logged, so just give up.
316 // Non-fatal error; we sent back
317 // an error message, so let them
322 // OK, we're authenticated; we sent back
323 // a reply, so start serving requests.
327 case RPCAP_MSG_CLOSE
:
329 // The client is giving up.
330 // Discard the rest of the message, if
331 // there is anything more.
333 (void)rpcapd_discard(pars
->sockctrl
, plen
);
334 // We're done with this client.
337 case RPCAP_MSG_ERROR
:
338 // Log this and close the connection?
339 // XXX - is this what happens in active
340 // mode, where *we* initiate the
341 // connection, and the client gives us
342 // an error message rather than a "let
343 // me log in" message, indicating that
344 // we're not allowed to connect to them?
345 (void)daemon_msg_err(pars
->sockctrl
, plen
);
348 case RPCAP_MSG_FINDALLIF_REQ
:
349 case RPCAP_MSG_OPEN_REQ
:
350 case RPCAP_MSG_STARTCAP_REQ
:
351 case RPCAP_MSG_UPDATEFILTER_REQ
:
352 case RPCAP_MSG_STATS_REQ
:
353 case RPCAP_MSG_ENDCAP_REQ
:
354 case RPCAP_MSG_SETSAMPLING_REQ
:
356 // These requests can't be sent until
357 // the client is authenticated.
359 msg_type_string
= rpcap_msg_type_string(header
.type
);
360 if (msg_type_string
!= NULL
)
362 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "%s request sent before authentication was completed", msg_type_string
);
366 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Message of type %u sent before authentication was completed", header
.type
);
368 if (rpcap_senderror(pars
->sockctrl
,
369 pars
->protocol_version
, PCAP_ERR_WRONGMSG
,
370 errmsgbuf
, errbuf
) == -1)
372 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
375 // Discard the rest of the message.
376 if (rpcapd_discard(pars
->sockctrl
, plen
) == -1)
383 case RPCAP_MSG_PACKET
:
384 case RPCAP_MSG_FINDALLIF_REPLY
:
385 case RPCAP_MSG_OPEN_REPLY
:
386 case RPCAP_MSG_STARTCAP_REPLY
:
387 case RPCAP_MSG_UPDATEFILTER_REPLY
:
388 case RPCAP_MSG_AUTH_REPLY
:
389 case RPCAP_MSG_STATS_REPLY
:
390 case RPCAP_MSG_ENDCAP_REPLY
:
391 case RPCAP_MSG_SETSAMPLING_REPLY
:
393 // These are server-to-client messages.
395 msg_type_string
= rpcap_msg_type_string(header
.type
);
396 if (msg_type_string
!= NULL
)
398 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Server-to-client message %s received from client", msg_type_string
);
402 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Server-to-client message of type %u received from client", header
.type
);
404 if (rpcap_senderror(pars
->sockctrl
,
405 pars
->protocol_version
, PCAP_ERR_WRONGMSG
,
406 errmsgbuf
, errbuf
) == -1)
408 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
411 // Discard the rest of the message.
412 if (rpcapd_discard(pars
->sockctrl
, plen
) == -1)
421 // Unknown message type.
423 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Unknown message type %u", header
.type
);
424 if (rpcap_senderror(pars
->sockctrl
,
425 pars
->protocol_version
, PCAP_ERR_WRONGMSG
,
426 errmsgbuf
, errbuf
) == -1)
428 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
431 // Discard the rest of the message.
432 if (rpcapd_discard(pars
->sockctrl
, plen
) == -1)
442 // OK, the client has authenticated itself, and we can start
443 // processing regular requests from it.
447 // We don't have any statistics yet.
459 errbuf
[0] = 0; // clear errbuf
461 // Avoid zombies connections; check if the connection is opens but no commands are performed
462 // from more than RPCAP_TIMEOUT_RUNTIME
464 // - I have to be in normal mode (no active mode)
465 // - if the device is open, I don't have to be in the middle of a capture (session->sockdata)
466 // - if the device is closed, I have always to check if a new command arrives
468 // Be carefully: the capture can have been started, but an error occurred (so session != NULL, but
470 if ((!pars
->isactive
) && ((session
== NULL
) || ((session
!= NULL
) && (session
->sockdata
== 0))))
472 // Check for the initial timeout
474 // We do not have to block here
475 tv
.tv_sec
= RPCAP_TIMEOUT_RUNTIME
;
478 FD_SET(pars
->sockctrl
, &rfds
);
480 retval
= select(pars
->sockctrl
+ 1, &rfds
, NULL
, NULL
, &tv
);
483 sock_geterror("select failed: ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
484 if (rpcap_senderror(pars
->sockctrl
,
485 pars
->protocol_version
, PCAP_ERR_NETW
,
486 errmsgbuf
, errbuf
) == -1)
487 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
491 // The timeout has expired
492 // So, this was a fake connection. Drop it down
495 if (rpcap_senderror(pars
->sockctrl
,
496 pars
->protocol_version
,
497 PCAP_ERR_INITTIMEOUT
,
498 "The RPCAP initial timeout has expired",
500 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
506 // Read the message header from the client.
508 nrecv
= rpcapd_recv_msg_header(pars
->sockctrl
, &header
);
516 // Client closed the connection.
523 // Did the client specify the version we negotiated?
525 // For now, there's only one version.
527 if (header
.ver
!= pars
->protocol_version
)
530 // Tell them it's not the negotiated version.
531 // Send the error message with their version,
532 // so they don't reject it as having the wrong
535 if (rpcap_senderror(pars
->sockctrl
,
536 header
.ver
, PCAP_ERR_WRONGVER
,
537 "RPCAP version in message isn't the negotiated version",
540 // That failed; log a message and give up.
541 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
545 // Discard the rest of the message.
546 (void)rpcapd_discard(pars
->sockctrl
, plen
);
553 case RPCAP_MSG_ERROR
: // The other endpoint reported an error
555 (void)daemon_msg_err(pars
->sockctrl
, plen
);
556 // Do nothing; just exit; the error code is already into the errbuf
557 // XXX - actually exit....
561 case RPCAP_MSG_FINDALLIF_REQ
:
563 if (daemon_msg_findallif_req(pars
, plen
) == -1)
565 // Fatal error; a message has
566 // been logged, so just give up.
572 case RPCAP_MSG_OPEN_REQ
:
575 // Process the open request, and keep
576 // the source from it, for use later
577 // when the capture is started.
579 // XXX - we don't care if the client sends
580 // us multiple open requests, the last
583 retval
= daemon_msg_open_req(pars
, plen
, source
, sizeof(source
));
586 // Fatal error; a message has
587 // been logged, so just give up.
594 case RPCAP_MSG_STARTCAP_REQ
:
598 // They never told us what device
600 if (rpcap_senderror(pars
->sockctrl
,
601 pars
->protocol_version
,
602 PCAP_ERR_STARTCAPTURE
,
603 "No capture device was specified",
606 // Fatal error; log an
607 // error and give up.
608 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
611 if (rpcapd_discard(pars
->sockctrl
, plen
) == -1)
618 if (daemon_msg_startcap_req(pars
, plen
, &have_thread
, &threaddata
, source
, &session
, &samp_param
) == -1)
620 // Fatal error; a message has
621 // been logged, so just give up.
627 case RPCAP_MSG_UPDATEFILTER_REQ
:
631 if (daemon_msg_updatefilter_req(pars
, session
, plen
) == -1)
633 // Fatal error; a message has
634 // been logged, so just give up.
640 if (rpcap_senderror(pars
->sockctrl
,
641 pars
->protocol_version
,
642 PCAP_ERR_UPDATEFILTER
,
643 "Device not opened. Cannot update filter",
646 // That failed; log a message and give up.
647 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
654 case RPCAP_MSG_CLOSE
: // The other endpoint close the pcap session
656 // signal to the main that the user closed the control connection
657 // This is used only in case of active mode
658 pars
->activeclose
= 1;
659 SOCK_MESSAGE("The other end system asked to close the connection.");
663 case RPCAP_MSG_STATS_REQ
:
665 if (daemon_msg_stats_req(pars
, session
, plen
, &stats
, svrcapt
) == -1)
667 // Fatal error; a message has
668 // been logged, so just give up.
674 case RPCAP_MSG_ENDCAP_REQ
: // The other endpoint close the current capture session
678 // Save statistics (we can need them in the future)
679 if (pcap_stats(session
->fp
, &stats
))
681 svrcapt
= session
->TotCapt
;
691 if (daemon_msg_endcap_req(pars
, session
, &have_thread
, threaddata
) == -1)
695 // Fatal error; a message has
696 // been logged, so just give up.
704 rpcap_senderror(pars
->sockctrl
,
705 pars
->protocol_version
,
707 "Device not opened. Cannot close the capture",
713 case RPCAP_MSG_SETSAMPLING_REQ
:
715 if (daemon_msg_setsampling_req(pars
, plen
, &samp_param
) == -1)
717 // Fatal error; a message has
718 // been logged, so just give up.
724 case RPCAP_MSG_AUTH_REQ
:
727 // We're already authenticated; you don't
728 // get to reauthenticate.
730 rpcapd_log(LOGPRIO_INFO
, "The client sent an RPCAP_MSG_AUTH_REQ message after authentication was completed");
731 if (rpcap_senderror(pars
->sockctrl
,
732 pars
->protocol_version
,
734 "RPCAP_MSG_AUTH_REQ request sent after authentication was completed",
737 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
740 // Discard the rest of the message.
741 if (rpcapd_discard(pars
->sockctrl
, plen
) == -1)
748 case RPCAP_MSG_PACKET
:
749 case RPCAP_MSG_FINDALLIF_REPLY
:
750 case RPCAP_MSG_OPEN_REPLY
:
751 case RPCAP_MSG_STARTCAP_REPLY
:
752 case RPCAP_MSG_UPDATEFILTER_REPLY
:
753 case RPCAP_MSG_AUTH_REPLY
:
754 case RPCAP_MSG_STATS_REPLY
:
755 case RPCAP_MSG_ENDCAP_REPLY
:
756 case RPCAP_MSG_SETSAMPLING_REPLY
:
758 // These are server-to-client messages.
760 msg_type_string
= rpcap_msg_type_string(header
.type
);
761 if (msg_type_string
!= NULL
)
763 rpcapd_log(LOGPRIO_INFO
, "The client sent a %s server-to-client message", msg_type_string
);
764 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Server-to-client message %s received from client", msg_type_string
);
768 rpcapd_log(LOGPRIO_INFO
, "The client sent a server-to-client message of type %u", header
.type
);
769 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Server-to-client message of type %u received from client", header
.type
);
771 if (rpcap_senderror(pars
->sockctrl
,
772 pars
->protocol_version
, PCAP_ERR_WRONGMSG
,
773 errmsgbuf
, errbuf
) == -1)
775 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
778 // Discard the rest of the message.
779 if (rpcapd_discard(pars
->sockctrl
, plen
) == -1)
788 // Unknown message type.
790 rpcapd_log(LOGPRIO_INFO
, "The client sent a message of type %u", header
.type
);
791 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Unknown message type %u", header
.type
);
792 if (rpcap_senderror(pars
->sockctrl
,
793 pars
->protocol_version
, PCAP_ERR_WRONGMSG
,
794 errbuf
, errmsgbuf
) == -1)
796 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
799 // Discard the rest of the message.
800 if (rpcapd_discard(pars
->sockctrl
, plen
) == -1)
811 // The child thread is about to end
813 // perform pcap_t cleanup, in case it has not been done
820 // Tell the data connection thread main capture
821 // loop to break out of that loop.
823 pcap_breakloop(session
->fp
);
826 // If it's currently blocked waiting for packets
827 // to arrive, try to wake it up, so it can see
828 // the "break out of the loop" indication.
830 SetEvent(pcap_getevent(session
->fp
));
833 // Wait for the thread to exit, so we don't close
834 // sockets out from under it.
836 // XXX - have a timeout, so we don't wait forever?
838 WaitForSingleObject(threaddata
, INFINITE
);
841 // Release the thread handle, as we're done with
844 CloseHandle(threaddata
);
846 pthread_cancel(threaddata
);
850 if (session
->sockdata
)
852 sock_close(session
->sockdata
, NULL
, 0);
853 session
->sockdata
= 0;
855 pcap_close(session
->fp
);
860 // Print message and exit
861 SOCK_MESSAGE("I'm exiting from the child loop");
862 SOCK_MESSAGE(errbuf
);
867 sock_close(pars
->sockctrl
, NULL
, 0);
875 * This handles the RPCAP_MSG_ERR message.
878 daemon_msg_err(SOCKET sockctrl
, uint32 plen
)
880 char errbuf
[PCAP_ERRBUF_SIZE
];
881 char remote_errbuf
[PCAP_ERRBUF_SIZE
];
883 if (plen
>= PCAP_ERRBUF_SIZE
)
886 * Message is too long; just read as much of it as we
887 * can into the buffer provided, and discard the rest.
889 if (sock_recv(sockctrl
, remote_errbuf
, PCAP_ERRBUF_SIZE
- 1,
890 SOCK_RECEIVEALL_YES
|SOCK_EOF_IS_ERROR
, errbuf
,
891 PCAP_ERRBUF_SIZE
) == -1)
894 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
897 if (rpcapd_discard(sockctrl
, plen
- (PCAP_ERRBUF_SIZE
- 1)) == -1)
906 remote_errbuf
[PCAP_ERRBUF_SIZE
- 1] = '\0';
910 /* Empty error string. */
911 remote_errbuf
[0] = '\0';
915 if (sock_recv(sockctrl
, remote_errbuf
, plen
,
916 SOCK_RECEIVEALL_YES
|SOCK_EOF_IS_ERROR
, errbuf
,
917 PCAP_ERRBUF_SIZE
) == -1)
920 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
927 remote_errbuf
[plen
] = '\0';
930 rpcapd_log(LOGPRIO_ERROR
, "Error from client: %s", remote_errbuf
);
935 * This handles the RPCAP_MSG_AUTH_REQ message.
936 * It checks if the authentication credentials supplied by the user are valid.
938 * This function is called if the daemon receives a RPCAP_MSG_AUTH_REQ
939 * message in its authentication loop. It reads the body of the
940 * authentication message from the network and checks whether the
941 * credentials are valid.
943 * \param sockctrl: the socket for the control connection.
945 * \param nullAuthAllowed: '1' if the NULL authentication is allowed.
947 * \param errbuf: a user-allocated buffer in which the error message
948 * (if one) has to be written. It must be at least PCAP_ERRBUF_SIZE
951 * \return '0' if everything is fine, '-1' if an unrecoverable error occurred,
952 * or '-2' if the authentication failed. For errors, an error message is
953 * returned in the 'errbuf' variable; this gives a message for the
954 * unrecoverable error or for the authentication failure.
957 daemon_msg_auth_req(SOCKET sockctrl
, uint8 ver
, uint32 plen
, int nullAuthAllowed
)
959 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
960 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
961 struct rpcap_header header
; // RPCAP message general header
963 struct rpcap_auth auth
; // RPCAP authentication header
965 status
= rpcapd_recv(sockctrl
, (char *) &auth
, sizeof(struct rpcap_auth
), &plen
, errmsgbuf
);
975 switch (ntohs(auth
.type
))
977 case RPCAP_RMTAUTH_NULL
:
979 if (!nullAuthAllowed
)
981 // Send the client an error reply.
982 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Authentication failed; NULL authentication not permitted.");
988 case RPCAP_RMTAUTH_PWD
:
990 char *username
, *passwd
;
991 uint32 usernamelen
, passwdlen
;
993 usernamelen
= ntohs(auth
.slen1
);
994 username
= (char *) malloc (usernamelen
+ 1);
995 if (username
== NULL
)
997 pcap_fmt_errmsg_for_errno(errmsgbuf
,
998 PCAP_ERRBUF_SIZE
, errno
, "malloc() failed");
1001 status
= rpcapd_recv(sockctrl
, username
, usernamelen
, &plen
, errmsgbuf
);
1012 username
[usernamelen
] = '\0';
1014 passwdlen
= ntohs(auth
.slen2
);
1015 passwd
= (char *) malloc (passwdlen
+ 1);
1018 pcap_fmt_errmsg_for_errno(errmsgbuf
,
1019 PCAP_ERRBUF_SIZE
, errno
, "malloc() failed");
1023 status
= rpcapd_recv(sockctrl
, passwd
, passwdlen
, &plen
, errmsgbuf
);
1036 passwd
[passwdlen
] = '\0';
1038 if (daemon_AuthUserPwd(username
, passwd
, errmsgbuf
))
1041 // Authentication failed. Let the client
1046 if (rpcap_senderror(sockctrl
, ver
,
1047 PCAP_ERR_AUTH
, errmsgbuf
, errbuf
) == -1)
1049 // That failed; log a message and give up.
1050 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1055 // Suspend for 1 second, so that they can't
1056 // hammer us with repeated tries with an
1057 // attack such as a dictionary attack.
1059 // WARNING: this delay is inserted only
1060 // at this point; if the client closes the
1061 // connection and reconnects, the suspension
1062 // time does not have any effect.
1064 sleep_secs(RPCAP_SUSPEND_WRONGAUTH
);
1074 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Authentication type not recognized.");
1078 // The authentication succeeded; let the client know.
1079 rpcap_createhdr(&header
, ver
, RPCAP_MSG_AUTH_REPLY
, 0, 0);
1081 // Send the ok message back
1082 if (sock_send(sockctrl
, (char *) &header
, sizeof (struct rpcap_header
), errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1084 // That failed; log a messsage and give up.
1085 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1089 // Check if all the data has been read; if not, discard the data in excess
1090 if (rpcapd_discard(sockctrl
, plen
) == -1)
1098 if (rpcap_senderror(sockctrl
, ver
, PCAP_ERR_AUTH
, errmsgbuf
,
1101 // That failed; log a message and give up.
1102 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1107 // Check if all the data has been read; if not, discard the data in excess
1108 if (rpcapd_discard(sockctrl
, plen
) == -1)
1117 daemon_AuthUserPwd(char *username
, char *password
, char *errbuf
)
1121 * Warning: the user which launches the process must have the
1122 * SE_TCB_NAME right.
1123 * This corresponds to have the "Act as part of the Operating System"
1124 * turned on (administrative tools, local security settings, local
1125 * policies, user right assignment)
1126 * However, it seems to me that if you run it as a service, this
1127 * right should be provided by default.
1130 if (LogonUser(username
, ".", password
, LOGON32_LOGON_NETWORK
, LOGON32_PROVIDER_DEFAULT
, &Token
) == 0)
1134 error
= GetLastError();
1135 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
, NULL
, error
, 0, errbuf
,
1136 PCAP_ERRBUF_SIZE
, NULL
);
1141 // This call should change the current thread to the selected user.
1142 // I didn't test it.
1143 if (ImpersonateLoggedOnUser(Token
) == 0)
1147 error
= GetLastError();
1148 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
, NULL
, error
, 0, errbuf
,
1149 PCAP_ERRBUF_SIZE
, NULL
);
1162 * http://www.unixpapa.com/incnote/passwd.html
1164 * We use the Solaris/Linux shadow password authentication if
1165 * we have getspnam(), otherwise we just do traditional
1166 * authentication, which, on some platforms, might work, even
1167 * with shadow passwords, if we're running as root. Traditional
1168 * authenticaion won't work if we're not running as root, as
1169 * I think these days all UN*Xes either won't return the password
1170 * at all with getpwnam() or will only do so if you're root.
1172 * XXX - perhaps what we *should* be using is PAM, if we have
1173 * it. That might hide all the details of username/password
1174 * authentication, whether it's done with a visible-to-root-
1175 * only password database or some other authentication mechanism,
1178 struct passwd
*user
;
1179 char *user_password
;
1180 #ifdef HAVE_GETSPNAM
1181 struct spwd
*usersp
;
1184 // This call is needed to get the uid
1185 if ((user
= getpwnam(username
)) == NULL
)
1187 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Authentication failed: no such user");
1191 #ifdef HAVE_GETSPNAM
1192 // This call is needed to get the password; otherwise 'x' is returned
1193 if ((usersp
= getspnam(username
)) == NULL
)
1195 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Authentication failed: no such user");
1198 user_password
= usersp
->sp_pwdp
;
1201 * XXX - what about other platforms?
1202 * The unixpapa.com page claims this Just Works on *BSD if you're
1203 * running as root - it's from 2000, so it doesn't indicate whether
1204 * macOS (which didn't come out until 2001, under the name Mac OS
1205 * X) behaves like the *BSDs or not, and might also work on AIX.
1206 * HP-UX does something else.
1208 * Again, hopefully PAM hides all that.
1210 user_password
= user
->pw_passwd
;
1213 if (strcmp(user_password
, (char *) crypt(password
, user_password
)) != 0)
1215 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Authentication failed: password incorrect");
1219 if (setuid(user
->pw_uid
))
1221 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1226 /* if (setgid(user->pw_gid))
1228 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1240 daemon_msg_findallif_req(struct daemon_slpars
*pars
, uint32 plen
)
1242 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
1243 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
1244 char sendbuf
[RPCAP_NETBUF_SIZE
]; // temporary buffer in which data to be sent is buffered
1245 int sendbufidx
= 0; // index which keeps the number of bytes currently buffered
1246 pcap_if_t
*alldevs
= NULL
; // pointer to the header of the interface chain
1247 pcap_if_t
*d
; // temp pointer needed to scan the interface chain
1248 struct pcap_addr
*address
; // pcap structure that keeps a network address of an interface
1249 struct rpcap_findalldevs_if
*findalldevs_if
;// rpcap structure that packet all the data of an interface together
1250 uint16 nif
= 0; // counts the number of interface listed
1252 // Discard the rest of the message; there shouldn't be any payload.
1253 if (rpcapd_discard(pars
->sockctrl
, plen
) == -1)
1259 // Retrieve the device list
1260 if (pcap_findalldevs(&alldevs
, errmsgbuf
) == -1)
1263 if (alldevs
== NULL
)
1265 if (rpcap_senderror(pars
->sockctrl
, pars
->protocol_version
,
1266 PCAP_ERR_NOREMOTEIF
,
1267 "No interfaces found! Make sure libpcap/WinPcap is properly installed"
1268 " and you have the right to access to the remote device.",
1271 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1277 // checks the number of interfaces and it computes the total length of the payload
1278 for (d
= alldevs
; d
!= NULL
; d
= d
->next
)
1283 plen
+= strlen(d
->description
);
1285 plen
+= strlen(d
->name
);
1287 plen
+= sizeof(struct rpcap_findalldevs_if
);
1289 for (address
= d
->addresses
; address
!= NULL
; address
= address
->next
)
1292 * Send only IPv4 and IPv6 addresses over the wire.
1294 switch (address
->addr
->sa_family
)
1300 plen
+= (sizeof(struct rpcap_sockaddr
) * 4);
1309 // RPCAP findalldevs command
1310 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
,
1311 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
,
1312 PCAP_ERRBUF_SIZE
) == -1)
1315 rpcap_createhdr((struct rpcap_header
*) sendbuf
, pars
->protocol_version
,
1316 RPCAP_MSG_FINDALLIF_REPLY
, nif
, plen
);
1318 // send the interface list
1319 for (d
= alldevs
; d
!= NULL
; d
= d
->next
)
1321 uint16 lname
, ldescr
;
1323 findalldevs_if
= (struct rpcap_findalldevs_if
*) &sendbuf
[sendbufidx
];
1325 if (sock_bufferize(NULL
, sizeof(struct rpcap_findalldevs_if
), NULL
,
1326 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1329 memset(findalldevs_if
, 0, sizeof(struct rpcap_findalldevs_if
));
1331 if (d
->description
) ldescr
= (short) strlen(d
->description
);
1333 if (d
->name
) lname
= (short) strlen(d
->name
);
1336 findalldevs_if
->desclen
= htons(ldescr
);
1337 findalldevs_if
->namelen
= htons(lname
);
1338 findalldevs_if
->flags
= htonl(d
->flags
);
1340 for (address
= d
->addresses
; address
!= NULL
; address
= address
->next
)
1343 * Send only IPv4 and IPv6 addresses over the wire.
1345 switch (address
->addr
->sa_family
)
1351 findalldevs_if
->naddr
++;
1358 findalldevs_if
->naddr
= htons(findalldevs_if
->naddr
);
1360 if (sock_bufferize(d
->name
, lname
, sendbuf
, &sendbufidx
,
1361 RPCAP_NETBUF_SIZE
, SOCKBUF_BUFFERIZE
, errmsgbuf
,
1362 PCAP_ERRBUF_SIZE
) == -1)
1365 if (sock_bufferize(d
->description
, ldescr
, sendbuf
, &sendbufidx
,
1366 RPCAP_NETBUF_SIZE
, SOCKBUF_BUFFERIZE
, errmsgbuf
,
1367 PCAP_ERRBUF_SIZE
) == -1)
1370 // send all addresses
1371 for (address
= d
->addresses
; address
!= NULL
; address
= address
->next
)
1373 struct rpcap_sockaddr
*sockaddr
;
1376 * Send only IPv4 and IPv6 addresses over the wire.
1378 switch (address
->addr
->sa_family
)
1384 sockaddr
= (struct rpcap_sockaddr
*) &sendbuf
[sendbufidx
];
1385 if (sock_bufferize(NULL
, sizeof(struct rpcap_sockaddr
), NULL
,
1386 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1388 daemon_seraddr((struct sockaddr_storage
*) address
->addr
, sockaddr
);
1390 sockaddr
= (struct rpcap_sockaddr
*) &sendbuf
[sendbufidx
];
1391 if (sock_bufferize(NULL
, sizeof(struct rpcap_sockaddr
), NULL
,
1392 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1394 daemon_seraddr((struct sockaddr_storage
*) address
->netmask
, sockaddr
);
1396 sockaddr
= (struct rpcap_sockaddr
*) &sendbuf
[sendbufidx
];
1397 if (sock_bufferize(NULL
, sizeof(struct rpcap_sockaddr
), NULL
,
1398 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1400 daemon_seraddr((struct sockaddr_storage
*) address
->broadaddr
, sockaddr
);
1402 sockaddr
= (struct rpcap_sockaddr
*) &sendbuf
[sendbufidx
];
1403 if (sock_bufferize(NULL
, sizeof(struct rpcap_sockaddr
), NULL
,
1404 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1406 daemon_seraddr((struct sockaddr_storage
*) address
->dstaddr
, sockaddr
);
1415 // We no longer need the device list. Free it.
1416 pcap_freealldevs(alldevs
);
1418 // Send a final command that says "now send it!"
1419 if (sock_send(pars
->sockctrl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1421 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1429 pcap_freealldevs(alldevs
);
1431 if (rpcap_senderror(pars
->sockctrl
, pars
->protocol_version
,
1432 PCAP_ERR_FINDALLIF
, errmsgbuf
, errbuf
) == -1)
1434 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1441 \param plen: the length of the current message (needed in order to be able
1442 to discard excess data in the message, if present)
1445 daemon_msg_open_req(struct daemon_slpars
*pars
, uint32 plen
, char *source
, size_t sourcelen
)
1447 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
1448 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
1449 pcap_t
*fp
; // pcap_t main variable
1451 char sendbuf
[RPCAP_NETBUF_SIZE
]; // temporary buffer in which data to be sent is buffered
1452 int sendbufidx
= 0; // index which keeps the number of bytes currently buffered
1453 struct rpcap_openreply
*openreply
; // open reply message
1455 if (plen
> sourcelen
- 1)
1457 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Source string too long");
1461 nread
= sock_recv(pars
->sockctrl
, source
, plen
,
1462 SOCK_RECEIVEALL_YES
|SOCK_EOF_IS_ERROR
, errbuf
, PCAP_ERRBUF_SIZE
);
1465 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
1468 source
[nread
] = '\0';
1471 // XXX - make sure it's *not* a URL; we don't support opening
1472 // remote devices here.
1474 // Open the selected device
1475 // This is a fake open, since we do that only to get the needed parameters, then we close the device again
1476 if ((fp
= pcap_open_live(source
,
1477 1500 /* fake snaplen */,
1479 1000 /* fake timeout */,
1480 errmsgbuf
)) == NULL
)
1483 // Now, I can send a RPCAP open reply message
1484 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
, &sendbufidx
,
1485 RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1488 rpcap_createhdr((struct rpcap_header
*) sendbuf
, pars
->protocol_version
,
1489 RPCAP_MSG_OPEN_REPLY
, 0, sizeof(struct rpcap_openreply
));
1491 openreply
= (struct rpcap_openreply
*) &sendbuf
[sendbufidx
];
1493 if (sock_bufferize(NULL
, sizeof(struct rpcap_openreply
), NULL
, &sendbufidx
,
1494 RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1497 memset(openreply
, 0, sizeof(struct rpcap_openreply
));
1498 openreply
->linktype
= htonl(pcap_datalink(fp
));
1499 openreply
->tzoff
= 0; /* This is always 0 for live captures */
1501 // We're done with the pcap_t.
1505 if (sock_send(pars
->sockctrl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1507 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1513 if (rpcap_senderror(pars
->sockctrl
, pars
->protocol_version
,
1514 PCAP_ERR_OPEN
, errmsgbuf
, errbuf
) == -1)
1516 // That failed; log a message and give up.
1517 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1521 // Check if all the data has been read; if not, discard the data in excess
1522 if (rpcapd_discard(pars
->sockctrl
, plen
) == -1)
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)
1535 daemon_msg_startcap_req(struct daemon_slpars
*pars
, uint32 plen
, int *have_thread
, HANDLE
*threaddata
, char *source
, struct session
**sessionp
, struct rpcap_sampling
*samp_param
)
1537 daemon_msg_startcap_req(struct daemon_slpars
*pars
, uint32 plen
, int *have_thread
, pthread_t
*threaddata
, char *source
, struct session
**sessionp
, struct rpcap_sampling
*samp_param
)
1540 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
1541 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
1542 char portdata
[PCAP_BUF_SIZE
]; // temp variable needed to derive the data port
1543 char peerhost
[PCAP_BUF_SIZE
]; // temp variable needed to derive the host name of our peer
1544 struct session
*session
= NULL
; // saves state of session
1546 char sendbuf
[RPCAP_NETBUF_SIZE
]; // temporary buffer in which data to be sent is buffered
1547 int sendbufidx
= 0; // index which keeps the number of bytes currently buffered
1549 // socket-related variables
1550 SOCKET sockdata
= INVALID_SOCKET
; // socket descriptor of the data connection
1551 struct addrinfo hints
; // temp, needed to open a socket connection
1552 struct addrinfo
*addrinfo
; // temp, needed to open a socket connection
1553 struct sockaddr_storage saddr
; // temp, needed to retrieve the network data port chosen on the local machine
1554 socklen_t saddrlen
; // temp, needed to retrieve the network data port chosen on the local machine
1555 int ret
; // return value from functions
1559 pthread_attr_t detachedAttribute
; // temp, needed to set the created thread as detached
1562 // RPCAP-related variables
1563 struct rpcap_startcapreq startcapreq
; // start capture request message
1564 struct rpcap_startcapreply
*startcapreply
; // start capture reply message
1565 int serveropen_dp
; // keeps who is going to open the data connection
1569 status
= rpcapd_recv(pars
->sockctrl
, (char *) &startcapreq
,
1570 sizeof(struct rpcap_startcapreq
), &plen
, errmsgbuf
);
1580 startcapreq
.flags
= ntohs(startcapreq
.flags
);
1582 // Create a session structure
1583 session
= malloc(sizeof(struct session
));
1584 if (session
== NULL
)
1586 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Can't allocate session structure");
1590 // Open the selected device
1591 if ((session
->fp
= pcap_open_live(source
,
1592 ntohl(startcapreq
.snaplen
),
1593 (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_PROMISC
) ? 1 : 0 /* local device, other flags not needed */,
1594 ntohl(startcapreq
.read_timeout
),
1595 errmsgbuf
)) == NULL
)
1599 // Apply sampling parameters
1600 fp
->rmt_samp
.method
= samp_param
->method
;
1601 fp
->rmt_samp
.value
= samp_param
->value
;
1605 We're in active mode if:
1606 - we're using TCP, and the user wants us to be in active mode
1609 serveropen_dp
= (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_SERVEROPEN
) || (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_DGRAM
) || pars
->isactive
;
1612 Gets the sockaddr structure referred to the other peer in the ctrl connection
1614 We need that because:
1615 - if we're in passive mode, we need to know the address family we want to use
1616 (the same used for the ctrl socket)
1617 - if we're in active mode, we need to know the network address of the other host
1618 we want to connect to
1620 saddrlen
= sizeof(struct sockaddr_storage
);
1621 if (getpeername(pars
->sockctrl
, (struct sockaddr
*) &saddr
, &saddrlen
) == -1)
1623 sock_geterror("getpeername(): ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
1627 memset(&hints
, 0, sizeof(struct addrinfo
));
1628 hints
.ai_socktype
= (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_DGRAM
) ? SOCK_DGRAM
: SOCK_STREAM
;
1629 hints
.ai_family
= saddr
.ss_family
;
1631 // Now we have to create a new socket to send packets
1632 if (serveropen_dp
) // Data connection is opened by the server toward the client
1634 pcap_snprintf(portdata
, sizeof portdata
, "%d", ntohs(startcapreq
.portdata
));
1636 // Get the name of the other peer (needed to connect to that specific network address)
1637 if (getnameinfo((struct sockaddr
*) &saddr
, saddrlen
, peerhost
,
1638 sizeof(peerhost
), NULL
, 0, NI_NUMERICHOST
))
1640 sock_geterror("getnameinfo(): ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
1644 if (sock_initaddress(peerhost
, portdata
, &hints
, &addrinfo
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1647 if ((sockdata
= sock_open(addrinfo
, SOCKOPEN_CLIENT
, 0, errmsgbuf
, PCAP_ERRBUF_SIZE
)) == INVALID_SOCKET
)
1650 else // Data connection is opened by the client toward the server
1652 hints
.ai_flags
= AI_PASSIVE
;
1654 // Let's the server socket pick up a free network port for us
1655 if (sock_initaddress(NULL
, "0", &hints
, &addrinfo
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1658 if ((sockdata
= sock_open(addrinfo
, SOCKOPEN_SERVER
, 1 /* max 1 connection in queue */, errmsgbuf
, PCAP_ERRBUF_SIZE
)) == INVALID_SOCKET
)
1661 // get the complete sockaddr structure used in the data connection
1662 saddrlen
= sizeof(struct sockaddr_storage
);
1663 if (getsockname(sockdata
, (struct sockaddr
*) &saddr
, &saddrlen
) == -1)
1665 sock_geterror("getsockname(): ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
1669 // Get the local port the system picked up
1670 if (getnameinfo((struct sockaddr
*) &saddr
, saddrlen
, NULL
,
1671 0, portdata
, sizeof(portdata
), NI_NUMERICSERV
))
1673 sock_geterror("getnameinfo(): ", errmsgbuf
, PCAP_ERRBUF_SIZE
);
1678 // addrinfo is no longer used
1679 freeaddrinfo(addrinfo
);
1682 // Needed to send an error on the ctrl connection
1683 session
->sockctrl
= pars
->sockctrl
;
1684 session
->protocol_version
= pars
->protocol_version
;
1686 // Now I can set the filter
1687 ret
= daemon_unpackapplyfilter(pars
->sockctrl
, session
, &plen
, errmsgbuf
);
1690 // Fatal error. A message has been logged; just give up.
1695 // Non-fatal error. Send an error message to the client.
1699 // Now, I can send a RPCAP start capture reply message
1700 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
, &sendbufidx
,
1701 RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1704 rpcap_createhdr((struct rpcap_header
*) sendbuf
, pars
->protocol_version
,
1705 RPCAP_MSG_STARTCAP_REPLY
, 0, sizeof(struct rpcap_startcapreply
));
1707 startcapreply
= (struct rpcap_startcapreply
*) &sendbuf
[sendbufidx
];
1709 if (sock_bufferize(NULL
, sizeof(struct rpcap_startcapreply
), NULL
,
1710 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1713 memset(startcapreply
, 0, sizeof(struct rpcap_startcapreply
));
1714 startcapreply
->bufsize
= htonl(pcap_bufsize(session
->fp
));
1718 unsigned short port
= (unsigned short)strtoul(portdata
,NULL
,10);
1719 startcapreply
->portdata
= htons(port
);
1722 if (sock_send(pars
->sockctrl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1724 // That failed; log a message and give up.
1725 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1731 SOCKET socktemp
; // We need another socket, since we're going to accept() a connection
1733 // Connection creation
1734 saddrlen
= sizeof(struct sockaddr_storage
);
1736 socktemp
= accept(sockdata
, (struct sockaddr
*) &saddr
, &saddrlen
);
1738 if (socktemp
== INVALID_SOCKET
)
1740 sock_geterror("accept(): ", errbuf
, PCAP_ERRBUF_SIZE
);
1741 rpcapd_log(LOGPRIO_ERROR
, "Accept of data connection failed: %s",
1746 // Now that I accepted the connection, the server socket is no longer needed
1747 sock_close(sockdata
, NULL
, 0);
1748 sockdata
= socktemp
;
1751 session
->sockdata
= sockdata
;
1753 // Now we have to create a new thread to receive packets
1755 *threaddata
= (HANDLE
)_beginthreadex(NULL
, 0, daemon_thrdatamain
,
1756 (void *) session
, 0, NULL
);
1757 if (*threaddata
== 0)
1759 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Error creating the data thread");
1763 /* GV we need this to create the thread as detached. */
1764 /* GV otherwise, the thread handle is not destroyed */
1765 pthread_attr_init(&detachedAttribute
);
1766 pthread_attr_setdetachstate(&detachedAttribute
, PTHREAD_CREATE_DETACHED
);
1767 ret
= pthread_create(threaddata
, &detachedAttribute
,
1768 daemon_thrdatamain
, (void *) session
);
1771 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1772 ret
, "Error creating the data thread");
1773 pthread_attr_destroy(&detachedAttribute
);
1776 pthread_attr_destroy(&detachedAttribute
);
1780 // Check if all the data has been read; if not, discard the data in excess
1781 if (rpcapd_discard(pars
->sockctrl
, plen
) == -1)
1784 *sessionp
= session
;
1789 // Not a fatal error, so send the client an error message and
1790 // keep serving client requests.
1795 freeaddrinfo(addrinfo
);
1802 pcap_breakloop(session
->fp
);
1803 SetEvent(pcap_getevent(session
->fp
));
1805 CloseHandle(*threaddata
);
1807 pthread_cancel(*threaddata
);
1812 if (sockdata
!= INVALID_SOCKET
)
1813 sock_close(sockdata
, NULL
, 0);
1818 pcap_close(session
->fp
);
1822 if (rpcap_senderror(pars
->sockctrl
, pars
->protocol_version
,
1823 PCAP_ERR_STARTCAPTURE
, errmsgbuf
, errbuf
) == -1)
1825 // That failed; log a message and give up.
1826 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1830 // Check if all the data has been read; if not, discard the data in excess
1831 if (rpcapd_discard(pars
->sockctrl
, plen
) == -1)
1841 // Fatal network error, so don't try to communicate with
1842 // the client, just give up.
1849 if (session
&& session
->fp
)
1852 // Tell the data connection thread main capture
1853 // loop to break out of that loop.
1855 pcap_breakloop(session
->fp
);
1858 // If it's currently blocked waiting for packets
1859 // to arrive, try to wake it up, so it can see
1860 // the "break out of the loop" indication.
1862 SetEvent(pcap_getevent(session
->fp
));
1866 // Wait for the thread to exit, so we don't close
1867 // sockets out from under it.
1869 // XXX - have a timeout, so we don't wait forever?
1871 WaitForSingleObject(*threaddata
, INFINITE
);
1874 // Release the thread handle, as we're done with
1877 CloseHandle(*threaddata
);
1879 pthread_cancel(*threaddata
);
1884 if (sockdata
!= INVALID_SOCKET
)
1885 sock_close(sockdata
, NULL
, 0);
1890 pcap_close(session
->fp
);
1899 daemon_msg_endcap_req(struct daemon_slpars
*pars
, struct session
*session
, int *have_thread
, HANDLE threaddata
)
1901 daemon_msg_endcap_req(struct daemon_slpars
*pars
, struct session
*session
, int *have_thread
, pthread_t threaddata
)
1904 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
1905 struct rpcap_header header
;
1911 // Tell the data connection thread main capture loop to
1912 // break out of that loop.
1914 pcap_breakloop(session
->fp
);
1917 // If it's currently blocked waiting for packets to
1918 // arrive, try to wake it up, so it can see the "break
1919 // out of the loop" indication.
1921 SetEvent(pcap_getevent(session
->fp
));
1924 // Wait for the thread to exit, so we don't close
1925 // sockets out from under it.
1927 // XXX - have a timeout, so we don't wait forever?
1929 WaitForSingleObject(threaddata
, INFINITE
);
1932 // Release the thread handle, as we're done with
1935 CloseHandle(threaddata
);
1937 pthread_cancel(threaddata
);
1941 if (session
->sockdata
)
1943 sock_close(session
->sockdata
, NULL
, 0);
1944 session
->sockdata
= 0;
1947 pcap_close(session
->fp
);
1949 rpcap_createhdr(&header
, pars
->protocol_version
,
1950 RPCAP_MSG_ENDCAP_REPLY
, 0, 0);
1952 if (sock_send(pars
->sockctrl
, (char *) &header
, sizeof(struct rpcap_header
), errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1954 // That failed; log a message and give up.
1955 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1963 daemon_unpackapplyfilter(SOCKET sockctrl
, struct session
*session
, uint32
*plenp
, char *errmsgbuf
)
1966 struct rpcap_filter filter
;
1967 struct rpcap_filterbpf_insn insn
;
1968 struct bpf_insn
*bf_insn
;
1969 struct bpf_program bf_prog
;
1972 status
= rpcapd_recv(sockctrl
, (char *) &filter
,
1973 sizeof(struct rpcap_filter
), plenp
, errmsgbuf
);
1983 bf_prog
.bf_len
= ntohl(filter
.nitems
);
1985 if (ntohs(filter
.filtertype
) != RPCAP_UPDATEFILTER_BPF
)
1987 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Only BPF/NPF filters are currently supported");
1991 bf_insn
= (struct bpf_insn
*) malloc (sizeof(struct bpf_insn
) * bf_prog
.bf_len
);
1992 if (bf_insn
== NULL
)
1994 pcap_fmt_errmsg_for_errno(errmsgbuf
, PCAP_ERRBUF_SIZE
,
1995 errno
, "malloc() failed");
1999 bf_prog
.bf_insns
= bf_insn
;
2001 for (i
= 0; i
< bf_prog
.bf_len
; i
++)
2003 status
= rpcapd_recv(sockctrl
, (char *) &insn
,
2004 sizeof(struct rpcap_filterbpf_insn
), plenp
, errmsgbuf
);
2014 bf_insn
->code
= ntohs(insn
.code
);
2015 bf_insn
->jf
= insn
.jf
;
2016 bf_insn
->jt
= insn
.jt
;
2017 bf_insn
->k
= ntohl(insn
.k
);
2022 if (bpf_validate(bf_prog
.bf_insns
, bf_prog
.bf_len
) == 0)
2024 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "The filter contains bogus instructions");
2028 if (pcap_setfilter(session
->fp
, &bf_prog
))
2030 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "RPCAP error: %s", pcap_geterr(session
->fp
));
2038 daemon_msg_updatefilter_req(struct daemon_slpars
*pars
, struct session
*session
, uint32 plen
)
2040 char errbuf
[PCAP_ERRBUF_SIZE
];
2041 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
2042 int ret
; // status of daemon_unpackapplyfilter()
2043 struct rpcap_header header
; // keeps the answer to the updatefilter command
2045 ret
= daemon_unpackapplyfilter(pars
->sockctrl
, session
, &plen
, errmsgbuf
);
2048 // Fatal error. A message has been logged; just give up.
2053 // Non-fatal error. Send an error reply to the client.
2057 // Check if all the data has been read; if not, discard the data in excess
2058 if (rpcapd_discard(pars
->sockctrl
, plen
) == -1)
2064 // A response is needed, otherwise the other host does not know that everything went well
2065 rpcap_createhdr(&header
, pars
->protocol_version
,
2066 RPCAP_MSG_UPDATEFILTER_REPLY
, 0, 0);
2068 if (sock_send(pars
->sockctrl
, (char *) &header
, sizeof (struct rpcap_header
), pcap_geterr(session
->fp
), PCAP_ERRBUF_SIZE
))
2070 // That failed; log a messsage and give up.
2071 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2078 if (rpcapd_discard(pars
->sockctrl
, plen
) == -1)
2082 rpcap_senderror(pars
->sockctrl
, pars
->protocol_version
,
2083 PCAP_ERR_UPDATEFILTER
, errmsgbuf
, NULL
);
2089 \brief Received the sampling parameters from remote host and it stores in the pcap_t structure.
2092 daemon_msg_setsampling_req(struct daemon_slpars
*pars
, uint32 plen
, struct rpcap_sampling
*samp_param
)
2094 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2095 char errmsgbuf
[PCAP_ERRBUF_SIZE
];
2096 struct rpcap_header header
;
2097 struct rpcap_sampling rpcap_samp
;
2100 status
= rpcapd_recv(pars
->sockctrl
, (char *) &rpcap_samp
, sizeof(struct rpcap_sampling
), &plen
, errmsgbuf
);
2110 // Save these settings in the pcap_t
2111 samp_param
->method
= rpcap_samp
.method
;
2112 samp_param
->value
= ntohl(rpcap_samp
.value
);
2114 // A response is needed, otherwise the other host does not know that everything went well
2115 rpcap_createhdr(&header
, pars
->protocol_version
,
2116 RPCAP_MSG_SETSAMPLING_REPLY
, 0, 0);
2118 if (sock_send(pars
->sockctrl
, (char *) &header
, sizeof (struct rpcap_header
), errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2120 // That failed; log a messsage and give up.
2121 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2125 if (rpcapd_discard(pars
->sockctrl
, plen
) == -1)
2133 if (rpcap_senderror(pars
->sockctrl
, pars
->protocol_version
,
2134 PCAP_ERR_AUTH
, errmsgbuf
, errbuf
) == -1)
2136 // That failed; log a message and give up.
2137 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2141 // Check if all the data has been read; if not, discard the data in excess
2142 if (rpcapd_discard(pars
->sockctrl
, plen
) == -1)
2151 daemon_msg_stats_req(struct daemon_slpars
*pars
, struct session
*session
, uint32 plen
, struct pcap_stat
*stats
, unsigned int svrcapt
)
2153 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2154 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
2155 char sendbuf
[RPCAP_NETBUF_SIZE
]; // temporary buffer in which data to be sent is buffered
2156 int sendbufidx
= 0; // index which keeps the number of bytes currently buffered
2157 struct rpcap_stats
*netstats
; // statistics sent on the network
2159 // Checks that the header does not contain other data; if so, discard it
2160 if (rpcapd_discard(pars
->sockctrl
, plen
) == -1)
2166 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
,
2167 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
2170 rpcap_createhdr((struct rpcap_header
*) sendbuf
, pars
->protocol_version
,
2171 RPCAP_MSG_STATS_REPLY
, 0, (uint16
) sizeof(struct rpcap_stats
));
2173 netstats
= (struct rpcap_stats
*) &sendbuf
[sendbufidx
];
2175 if (sock_bufferize(NULL
, sizeof(struct rpcap_stats
), NULL
,
2176 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
2179 if (session
&& session
->fp
)
2181 if (pcap_stats(session
->fp
, stats
) == -1)
2183 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "%s", pcap_geterr(session
->fp
));
2187 netstats
->ifdrop
= htonl(stats
->ps_ifdrop
);
2188 netstats
->ifrecv
= htonl(stats
->ps_recv
);
2189 netstats
->krnldrop
= htonl(stats
->ps_drop
);
2190 netstats
->svrcapt
= htonl(session
->TotCapt
);
2194 // We have to keep compatibility with old applications,
2195 // which ask for statistics also when the capture has
2197 netstats
->ifdrop
= htonl(stats
->ps_ifdrop
);
2198 netstats
->ifrecv
= htonl(stats
->ps_recv
);
2199 netstats
->krnldrop
= htonl(stats
->ps_drop
);
2200 netstats
->svrcapt
= htonl(svrcapt
);
2204 if (sock_send(pars
->sockctrl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2206 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2213 rpcap_senderror(pars
->sockctrl
, pars
->protocol_version
,
2214 PCAP_ERR_GETSTATS
, errmsgbuf
, NULL
);
2219 static unsigned __stdcall
2223 daemon_thrdatamain(void *ptr
)
2225 char errbuf
[PCAP_ERRBUF_SIZE
+ 1]; // error buffer
2226 struct session
*session
; // pointer to the struct session for this session
2227 int retval
; // general variable used to keep the return value of other functions
2228 struct rpcap_pkthdr
*net_pkt_header
;// header of the packet
2229 struct pcap_pkthdr
*pkt_header
; // pointer to the buffer that contains the header of the current packet
2230 u_char
*pkt_data
; // pointer to the buffer that contains the current packet
2231 size_t sendbufsize
; // size for the send buffer
2232 char *sendbuf
; // temporary buffer in which data to be sent is buffered
2233 int sendbufidx
; // index which keeps the number of bytes currently buffered
2236 session
= (struct session
*) ptr
;
2238 session
->TotCapt
= 0; // counter which is incremented each time a packet is received
2240 // Initialize errbuf
2241 memset(errbuf
, 0, sizeof(errbuf
));
2244 // We need a buffer large enough to hold a buffer large enough
2245 // for a maximum-size packet for this pcap_t.
2247 sendbufsize
= sizeof(struct rpcap_header
) + sizeof(struct rpcap_pkthdr
) + pcap_snapshot(session
->fp
);
2248 sendbuf
= (char *) malloc (sendbufsize
);
2249 if (sendbuf
== NULL
)
2251 rpcapd_log(LOGPRIO_ERROR
,
2252 "Unable to allocate the buffer for this child thread");
2257 // Modify thread params so that it can be killed at any time
2258 retval
= pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, NULL
);
2261 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
2262 retval
, "pthread_setcancelstate");
2263 rpcapd_log(LOGPRIO_ERROR
,
2264 "Can't set cancel state on data thread: %s", errbuf
);
2267 retval
= pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS
, NULL
);
2270 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
2271 retval
, "pthread_setcanceltype");
2272 rpcapd_log(LOGPRIO_ERROR
,
2273 "Can't set cancel type on data thread: %s", errbuf
);
2278 // Retrieve the packets
2279 while ((retval
= pcap_next_ex(session
->fp
, &pkt_header
, (const u_char
**) &pkt_data
)) >= 0) // cast to avoid a compiler warning
2281 if (retval
== 0) // Read timeout elapsed
2286 // Bufferize the general header
2287 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
,
2288 &sendbufidx
, sendbufsize
, SOCKBUF_CHECKONLY
, errbuf
,
2289 PCAP_ERRBUF_SIZE
) == -1)
2291 rpcapd_log(LOGPRIO_ERROR
,
2292 "sock_bufferize() error sending packet message: %s",
2297 rpcap_createhdr((struct rpcap_header
*) sendbuf
,
2298 session
->protocol_version
, RPCAP_MSG_PACKET
, 0,
2299 (uint16
) (sizeof(struct rpcap_pkthdr
) + pkt_header
->caplen
));
2301 net_pkt_header
= (struct rpcap_pkthdr
*) &sendbuf
[sendbufidx
];
2303 // Bufferize the pkt header
2304 if (sock_bufferize(NULL
, sizeof(struct rpcap_pkthdr
), NULL
,
2305 &sendbufidx
, sendbufsize
, SOCKBUF_CHECKONLY
, errbuf
,
2306 PCAP_ERRBUF_SIZE
) == -1)
2308 rpcapd_log(LOGPRIO_ERROR
,
2309 "sock_bufferize() error sending packet message: %s",
2314 net_pkt_header
->caplen
= htonl(pkt_header
->caplen
);
2315 net_pkt_header
->len
= htonl(pkt_header
->len
);
2316 net_pkt_header
->npkt
= htonl(++(session
->TotCapt
));
2317 net_pkt_header
->timestamp_sec
= htonl(pkt_header
->ts
.tv_sec
);
2318 net_pkt_header
->timestamp_usec
= htonl(pkt_header
->ts
.tv_usec
);
2320 // Bufferize the pkt data
2321 if (sock_bufferize((char *) pkt_data
, pkt_header
->caplen
,
2322 sendbuf
, &sendbufidx
, sendbufsize
, SOCKBUF_BUFFERIZE
,
2323 errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2325 rpcapd_log(LOGPRIO_ERROR
,
2326 "sock_bufferize() error sending packet message: %s",
2332 // If the client dropped the connection, don't report an
2333 // error, just quit.
2334 status
= sock_send(session
->sockdata
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
);
2340 // Error other than "client closed the
2341 // connection out from under us"; report
2344 rpcapd_log(LOGPRIO_ERROR
,
2345 "Send of packet to client failed: %s",
2350 // Give up in either case.
2358 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Error reading the packets: %s", pcap_geterr(session
->fp
));
2359 rpcap_senderror(session
->sockctrl
, session
->protocol_version
,
2360 PCAP_ERR_READEX
, errbuf
, NULL
);
2365 closesocket(session
->sockdata
);
2366 session
->sockdata
= 0;
2374 \brief It serializes a network address.
2376 It accepts a 'sockaddr_storage' structure as input, and it converts it appropriately into a format
2377 that can be used to be sent on the network. Basically, it applies all the hton()
2378 conversion required to the input variable.
2380 \param sockaddrin a 'sockaddr_storage' pointer to the variable that has to be
2381 serialized. This variable can be both a 'sockaddr_in' and 'sockaddr_in6'.
2383 \param sockaddrout an 'rpcap_sockaddr' pointer to the variable that will contain
2384 the serialized data. This variable has to be allocated by the user.
2386 \warning This function supports only AF_INET and AF_INET6 address families.
2389 daemon_seraddr(struct sockaddr_storage
*sockaddrin
, struct rpcap_sockaddr
*sockaddrout
)
2391 memset(sockaddrout
, 0, sizeof(struct sockaddr_storage
));
2393 // There can be the case in which the sockaddrin is not available
2394 if (sockaddrin
== NULL
) return;
2396 // Warning: we support only AF_INET and AF_INET6
2397 switch (sockaddrin
->ss_family
)
2401 struct sockaddr_in
*sockaddrin_ipv4
;
2402 struct rpcap_sockaddr_in
*sockaddrout_ipv4
;
2404 sockaddrin_ipv4
= (struct sockaddr_in
*) sockaddrin
;
2405 sockaddrout_ipv4
= (struct rpcap_sockaddr_in
*) sockaddrout
;
2406 sockaddrout_ipv4
->family
= htons(RPCAP_AF_INET
);
2407 sockaddrout_ipv4
->port
= htons(sockaddrin_ipv4
->sin_port
);
2408 memcpy(&sockaddrout_ipv4
->addr
, &sockaddrin_ipv4
->sin_addr
, sizeof(sockaddrout_ipv4
->addr
));
2409 memset(sockaddrout_ipv4
->zero
, 0, sizeof(sockaddrout_ipv4
->zero
));
2416 struct sockaddr_in6
*sockaddrin_ipv6
;
2417 struct rpcap_sockaddr_in6
*sockaddrout_ipv6
;
2419 sockaddrin_ipv6
= (struct sockaddr_in6
*) sockaddrin
;
2420 sockaddrout_ipv6
= (struct rpcap_sockaddr_in6
*) sockaddrout
;
2421 sockaddrout_ipv6
->family
= htons(RPCAP_AF_INET6
);
2422 sockaddrout_ipv6
->port
= htons(sockaddrin_ipv6
->sin6_port
);
2423 sockaddrout_ipv6
->flowinfo
= htonl(sockaddrin_ipv6
->sin6_flowinfo
);
2424 memcpy(&sockaddrout_ipv6
->addr
, &sockaddrin_ipv6
->sin6_addr
, sizeof(sockaddrout_ipv6
->addr
));
2425 sockaddrout_ipv6
->scope_id
= htonl(sockaddrin_ipv6
->sin6_scope_id
);
2434 \brief Suspends a thread for secs seconds.
2436 void sleep_secs(int secs
)
2441 unsigned secs_remaining
;
2445 secs_remaining
= secs
;
2446 while (secs_remaining
!= 0)
2447 secs_remaining
= sleep(secs_remaining
);
2452 * Read the header of a message.
2455 rpcapd_recv_msg_header(SOCKET sock
, struct rpcap_header
*headerp
)
2458 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2460 nread
= sock_recv(sock
, (char *) headerp
, sizeof(struct rpcap_header
),
2461 SOCK_RECEIVEALL_YES
|SOCK_EOF_ISNT_ERROR
, errbuf
, PCAP_ERRBUF_SIZE
);
2465 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
2470 // Immediate EOF; that's treated like a close message.
2473 headerp
->plen
= ntohl(headerp
->plen
);
2478 * Read data from a message.
2479 * If we're trying to read more data that remains, puts an error
2480 * message into errmsgbuf and returns -2. Otherwise, tries to read
2481 * the data and, if that succeeds, subtracts the amount read from
2482 * the number of bytes of data that remains.
2483 * Returns 0 on success, logs a message and returns -1 on a network
2487 rpcapd_recv(SOCKET sock
, char *buffer
, size_t toread
, uint32
*plen
, char *errmsgbuf
)
2490 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2494 // Tell the client and continue.
2495 pcap_snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Message payload is too short");
2498 nread
= sock_recv(sock
, buffer
, toread
,
2499 SOCK_RECEIVEALL_YES
|SOCK_EOF_IS_ERROR
, errbuf
, PCAP_ERRBUF_SIZE
);
2502 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
2510 * Discard data from a connection.
2511 * Mostly used to discard wrong-sized messages.
2512 * Returns 0 on success, logs a message and returns -1 on a network
2516 rpcapd_discard(SOCKET sock
, uint32 len
)
2518 char errbuf
[PCAP_ERRBUF_SIZE
+ 1]; // keeps the error string, prior to be printed
2522 if (sock_discard(sock
, len
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2525 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);