2 * Copyright (c) 2002 - 2003
3 * NetGroup, Politecnico di Torino (Italy)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the Politecnico di Torino nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <errno.h> // for the errno variable
40 #include <stdlib.h> // for malloc(), free(), ...
41 #include <string.h> // for strlen(), ...
42 #include <limits.h> // for INT_MAX
45 #include <process.h> // for threads
51 #include <sys/types.h> // for select() and such
52 #include <pwd.h> // for password management
56 #include <shadow.h> // for password management
59 #include <pcap.h> // for libpcap/WinPcap calls
62 #include "sockutils.h" // for socket calls
63 #include "portability.h"
64 #include "rpcap-protocol.h"
69 #include <openssl/ssl.h>
74 // Timeout, in seconds, when we're waiting for a client to send us an
75 // authentication request; if they don't send us a request within that
76 // interval, we drop the connection, so we don't stay stuck forever.
78 #define RPCAP_TIMEOUT_INIT 90
81 // Timeout, in seconds, when we're waiting for an authenticated client
82 // to send us a request, if a capture isn't in progress; if they don't
83 // send us a request within that interval, we drop the connection, so
84 // we don't stay stuck forever.
86 #define RPCAP_TIMEOUT_RUNTIME 180
89 // Time, in seconds, that we wait after a failed authentication attempt
90 // before processing the next request; this prevents a client from
91 // rapidly trying different accounts or passwords.
93 #define RPCAP_SUSPEND_WRONGAUTH 1
95 // Parameters for the service loop.
98 SOCKET sockctrl
; //!< SOCKET ID of the control connection
99 SSL
*ssl
; //!< Optional SSL handler for the controlling sockets
100 int isactive
; //!< Not null if the daemon has to run in active mode
101 int nullAuthAllowed
; //!< '1' if we permit NULL authentication, '0' otherwise
105 // Data for a session managed by a thread.
106 // It includes both a Boolean indicating whether we *have* a thread,
107 // and a platform-dependent (UN*X vs. Windows) identifier for the
108 // thread; on Windows, we could use an invalid handle to indicate
109 // that we don't have a thread, but there *is* no portable "no thread"
110 // value for a pthread_t on UN*X.
115 SSL
*ctrl_ssl
, *data_ssl
; // optional SSL handlers for sockctrl and sockdata.
116 uint8 protocol_version
;
118 unsigned int TotCapt
;
127 // Locally defined functions
128 static int daemon_msg_err(SOCKET sockctrl
, SSL
*, uint32 plen
);
129 static int daemon_msg_auth_req(struct daemon_slpars
*pars
, uint32 plen
);
130 static int daemon_AuthUserPwd(char *username
, char *password
, char *errbuf
);
132 static int daemon_msg_findallif_req(uint8 ver
, struct daemon_slpars
*pars
,
135 static int daemon_msg_open_req(uint8 ver
, struct daemon_slpars
*pars
,
136 uint32 plen
, char *source
, size_t sourcelen
);
137 static int daemon_msg_startcap_req(uint8 ver
, struct daemon_slpars
*pars
,
138 uint32 plen
, char *source
, struct session
**sessionp
,
139 struct rpcap_sampling
*samp_param
, int uses_ssl
);
140 static int daemon_msg_endcap_req(uint8 ver
, struct daemon_slpars
*pars
,
141 struct session
*session
);
143 static int daemon_msg_updatefilter_req(uint8 ver
, struct daemon_slpars
*pars
,
144 struct session
*session
, uint32 plen
);
145 static int daemon_unpackapplyfilter(SOCKET sockctrl
, SSL
*, struct session
*session
, uint32
*plenp
, char *errbuf
);
147 static int daemon_msg_stats_req(uint8 ver
, struct daemon_slpars
*pars
,
148 struct session
*session
, uint32 plen
, struct pcap_stat
*stats
,
149 unsigned int svrcapt
);
151 static int daemon_msg_setsampling_req(uint8 ver
, struct daemon_slpars
*pars
,
152 uint32 plen
, struct rpcap_sampling
*samp_param
);
154 static void daemon_seraddr(struct sockaddr_storage
*sockaddrin
, struct rpcap_sockaddr
*sockaddrout
);
156 static unsigned __stdcall
daemon_thrdatamain(void *ptr
);
158 static void *daemon_thrdatamain(void *ptr
);
159 static void noop_handler(int sign
);
162 static int rpcapd_recv_msg_header(SOCKET sock
, SSL
*, struct rpcap_header
*headerp
);
163 static int rpcapd_recv(SOCKET sock
, SSL
*, char *buffer
, size_t toread
, uint32
*plen
, char *errmsgbuf
);
164 static int rpcapd_discard(SOCKET sock
, SSL
*, uint32 len
);
165 static void session_close(struct session
*);
168 // TLS record layer header; used when processing the first message from
169 // the client, in case we aren't doing TLS but they are.
171 struct tls_record_header
{
172 uint8 type
; // ContentType - will be 22, for Handshake
173 uint8 version_major
; // TLS protocol major version
174 uint8 version_injor
; // TLS protocol minor version
175 // This is *not* aligned on a 2-byte boundary; we just
176 // declare it as two bytes. Don't assume any particular
177 // compiler's mechanism for saying "packed"!
178 uint8 length_hi
; // Upper 8 bits of payload length
179 uint8 length_lo
; // Low 8 bits of payload length
182 #define TLS_RECORD_HEADER_LEN 5 // Don't use sizeof in case it's padded
184 #define TLS_RECORD_TYPE_ALERT 21
185 #define TLS_RECORD_TYPE_HANDSHAKE 22
188 // TLS alert message.
192 uint8 alert_description
;
195 #define TLS_ALERT_LEN 2
197 #define TLS_ALERT_LEVEL_FATAL 2
198 #define TLS_ALERT_HANDSHAKE_FAILURE 40
200 static int is_url(const char *source
);
203 * Maximum sizes for fixed-bit-width values.
206 #define UINT16_MAX 65535U
210 #define UINT32_MAX 4294967295U
214 daemon_serviceloop(SOCKET sockctrl
, int isactive
, char *passiveClients
,
215 int nullAuthAllowed
, int uses_ssl
)
218 struct tls_record_header tls_header
;
219 struct tls_alert tls_alert
;
220 struct daemon_slpars pars
; // service loop parameters
221 char errbuf
[PCAP_ERRBUF_SIZE
+ 1]; // keeps the error string, prior to be printed
222 char errmsgbuf
[PCAP_ERRBUF_SIZE
+ 1]; // buffer for errors to send to the client
223 int host_port_check_status
;
226 struct rpcap_header header
; // RPCAP message general header
227 uint32 plen
; // payload length from header
228 int authenticated
= 0; // 1 if the client has successfully authenticated
229 char source
[PCAP_BUF_SIZE
+1]; // keeps the string that contains the interface to open
230 int got_source
= 0; // 1 if we've gotten the source from an open request
232 struct sigaction action
;
234 struct session
*session
= NULL
; // struct session main variable
235 const char *msg_type_string
; // string for message type
236 int client_told_us_to_close
= 0; // 1 if the client told us to close the capture
238 // needed to save the values of the statistics
239 struct pcap_stat stats
;
240 unsigned int svrcapt
;
242 struct rpcap_sampling samp_param
; // in case sampling has been requested
244 // Structures needed for the select() call
245 fd_set rfds
; // set of socket descriptors we have to check
246 struct timeval tv
; // maximum time the select() can block waiting for data
247 int retval
; // select() return value
249 *errbuf
= 0; // Initialize errbuf
252 // Peek into the socket to determine whether the client sent us
253 // a TLS handshake message or a non-TLS rpcapd message.
255 // The first byte of an rpcapd request is the version number;
256 // the first byte of a TLS handshake message is 22. The
257 // first request to an rpcapd server must be an authentication
258 // request or a close request, and must have a version number
259 // of 0, so it will be possible to distinguish between an
260 // initial plaintext request to a server and an initial TLS
261 // handshake message.
263 nrecv
= sock_recv(sockctrl
, NULL
, (char *)&first_octet
, 1,
264 SOCK_EOF_ISNT_ERROR
|SOCK_MSG_PEEK
, errbuf
, PCAP_ERRBUF_SIZE
);
268 rpcapd_log(LOGPRIO_ERROR
, "Peek from client failed: %s", errbuf
);
273 // Client closed the connection.
279 // We have to upgrade to TLS as soon as possible, so that the
280 // whole protocol goes through the encrypted tunnel, including
281 // early error messages.
283 // Even in active mode, the other end has to initiate the TLS
284 // handshake as we still are the server as far as TLS is concerned,
285 // so we don't check isactive.
290 // We're expecting a TLS handshake message. If this
291 // isn't one, assume it's a non-TLS rpcapd message.
293 // The first octet of a TLS handshake is
294 // TLS_RECORD_TYPE_HANDSHAKE.
296 if (first_octet
!= TLS_RECORD_TYPE_HANDSHAKE
)
299 // We assume this is a non-TLS rpcapd message.
301 // Read the message header from the client.
303 nrecv
= rpcapd_recv_msg_header(sockctrl
, NULL
, &header
);
311 // Client closed the connection.
316 // Discard the rest of the message.
317 if (rpcapd_discard(sockctrl
, NULL
, plen
) == -1)
324 // Send an authentication error, indicating
325 // that we require TLS.
327 if (rpcap_senderror(sockctrl
, NULL
, header
.ver
,
328 PCAP_ERR_TLS_REQUIRED
,
329 "TLS is required by this server", errbuf
) == -1)
331 // That failed; log a message and give up.
332 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
336 // Shut the session down.
339 ssl
= ssl_promotion(1, sockctrl
, errbuf
, PCAP_ERRBUF_SIZE
);
342 rpcapd_log(LOGPRIO_ERROR
, "TLS handshake on control connection failed: %s",
351 // We're expecting a non-TLS rpcapd message. If this
352 // looks, instead, like a TLS handshake message, send
353 // a TLS handshake_failed alert.
355 // The first octet of a TLS handshake is
356 // TLS_RECORD_TYPE_HANDSHAKE.
358 if (first_octet
== TLS_RECORD_TYPE_HANDSHAKE
)
362 // Read the record header.
364 nrecv
= sock_recv(sockctrl
, ssl
, (char *) &tls_header
,
365 sizeof tls_header
, SOCK_RECEIVEALL_YES
|SOCK_EOF_ISNT_ERROR
,
366 errbuf
, PCAP_ERRBUF_SIZE
);
370 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
378 plen
= (tls_header
.length_hi
<< 8U) | tls_header
.length_lo
;
380 // Discard the rest of the message.
381 if (rpcapd_discard(sockctrl
, NULL
, plen
) == -1)
388 // Send a TLS handshake failure alert.
389 // Use the same version the client sent us.
391 tls_header
.type
= TLS_RECORD_TYPE_ALERT
;
392 tls_header
.length_hi
= 0;
393 tls_header
.length_lo
= TLS_ALERT_LEN
;
395 if (sock_send(sockctrl
, NULL
, (char *) &tls_header
,
396 TLS_RECORD_HEADER_LEN
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
398 // That failed; log a message and give up.
399 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
403 tls_alert
.alert_level
= TLS_ALERT_LEVEL_FATAL
;
404 tls_alert
.alert_description
= TLS_ALERT_HANDSHAKE_FAILURE
;
405 if (sock_send(sockctrl
, NULL
, (char *) &tls_alert
,
406 TLS_ALERT_LEN
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
408 // That failed; log a message and give up.
409 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
419 // Set parameters structure
420 pars
.sockctrl
= sockctrl
;
422 pars
.isactive
= isactive
; // active mode
423 pars
.nullAuthAllowed
= nullAuthAllowed
;
426 // We have a connection.
428 // If it's a passive mode connection, check whether the connecting
429 // host is among the ones allowed.
431 // In either case, we were handed a copy of the host list; free it
432 // as soon as we're done with it.
437 free(passiveClients
);
438 passiveClients
= NULL
;
442 struct sockaddr_storage from
;
446 // Get the address of the other end of the connection.
448 fromlen
= sizeof(struct sockaddr_storage
);
449 if (getpeername(pars
.sockctrl
, (struct sockaddr
*)&from
,
452 sock_geterror("getpeername()", errmsgbuf
, PCAP_ERRBUF_SIZE
);
453 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
, 0, PCAP_ERR_NETW
, errmsgbuf
, errbuf
) == -1)
454 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
459 // Are they in the list of host/port combinations we allow?
461 host_port_check_status
= sock_check_hostlist(passiveClients
, RPCAP_HOSTLIST_SEP
, &from
, errmsgbuf
, PCAP_ERRBUF_SIZE
);
462 free(passiveClients
);
463 passiveClients
= NULL
;
464 if (host_port_check_status
< 0)
466 if (host_port_check_status
== -2) {
468 // We got an error; log it.
470 rpcapd_log(LOGPRIO_ERROR
, "%s", errmsgbuf
);
474 // Sorry, we can't let you in.
476 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
, 0, PCAP_ERR_HOSTNOAUTH
, errmsgbuf
, errbuf
) == -1)
477 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
484 // Catch SIGUSR1, but do nothing. We use it to interrupt the
485 // capture thread to break it out of a loop in which it's
486 // blocked waiting for packets to arrive.
488 // We don't want interrupted system calls to restart, so that
489 // the read routine for the pcap_t gets EINTR rather than
490 // restarting if it's blocked in a system call.
492 memset(&action
, 0, sizeof (action
));
493 action
.sa_handler
= noop_handler
;
495 sigemptyset(&action
.sa_mask
);
496 sigaction(SIGUSR1
, &action
, NULL
);
500 // The client must first authenticate; loop until they send us a
501 // message with a version we support and credentials we accept,
502 // they send us a close message indicating that they're giving up,
503 // or we get a network error or other fatal error.
505 while (!authenticated
)
508 // If we're not in active mode, we use select(), with a
509 // timeout, to wait for an authentication request; if
510 // the timeout expires, we drop the connection, so that
511 // a client can't just connect to us and leave us
517 // We do not have to block here
518 tv
.tv_sec
= RPCAP_TIMEOUT_INIT
;
521 FD_SET(pars
.sockctrl
, &rfds
);
523 retval
= select((int)pars
.sockctrl
+ 1, &rfds
, NULL
, NULL
, &tv
);
526 sock_geterror("select() failed", errmsgbuf
, PCAP_ERRBUF_SIZE
);
527 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
, 0, PCAP_ERR_NETW
, errmsgbuf
, errbuf
) == -1)
528 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
532 // The timeout has expired
533 // So, this was a fake connection. Drop it down
536 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
, 0, PCAP_ERR_INITTIMEOUT
, "The RPCAP initial timeout has expired", errbuf
) == -1)
537 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
543 // Read the message header from the client.
545 nrecv
= rpcapd_recv_msg_header(pars
.sockctrl
, pars
.ssl
, &header
);
553 // Client closed the connection.
560 // While we're in the authentication pharse, all requests
561 // must use version 0.
566 // Send it back to them with their version.
568 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
, header
.ver
,
570 "RPCAP version in requests in the authentication phase must be 0",
573 // That failed; log a message and give up.
574 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
578 // Discard the rest of the message and drop the
580 (void)rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
);
586 case RPCAP_MSG_AUTH_REQ
:
587 retval
= daemon_msg_auth_req(&pars
, plen
);
590 // Fatal error; a message has
591 // been logged, so just give up.
596 // Non-fatal error; we sent back
597 // an error message, so let them
602 // OK, we're authenticated; we sent back
603 // a reply, so start serving requests.
607 case RPCAP_MSG_CLOSE
:
609 // The client is giving up.
610 // Discard the rest of the message, if
611 // there is anything more.
613 (void)rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
);
614 // We're done with this client.
617 case RPCAP_MSG_ERROR
:
618 // Log this and close the connection?
619 // XXX - is this what happens in active
620 // mode, where *we* initiate the
621 // connection, and the client gives us
622 // an error message rather than a "let
623 // me log in" message, indicating that
624 // we're not allowed to connect to them?
625 (void)daemon_msg_err(pars
.sockctrl
, pars
.ssl
, plen
);
628 case RPCAP_MSG_FINDALLIF_REQ
:
629 case RPCAP_MSG_OPEN_REQ
:
630 case RPCAP_MSG_STARTCAP_REQ
:
631 case RPCAP_MSG_UPDATEFILTER_REQ
:
632 case RPCAP_MSG_STATS_REQ
:
633 case RPCAP_MSG_ENDCAP_REQ
:
634 case RPCAP_MSG_SETSAMPLING_REQ
:
636 // These requests can't be sent until
637 // the client is authenticated.
639 msg_type_string
= rpcap_msg_type_string(header
.type
);
640 if (msg_type_string
!= NULL
)
642 snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "%s request sent before authentication was completed", msg_type_string
);
646 snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Message of type %u sent before authentication was completed", header
.type
);
648 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
649 header
.ver
, PCAP_ERR_WRONGMSG
,
650 errmsgbuf
, errbuf
) == -1)
652 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
655 // Discard the rest of the message.
656 if (rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
) == -1)
663 case RPCAP_MSG_PACKET
:
664 case RPCAP_MSG_FINDALLIF_REPLY
:
665 case RPCAP_MSG_OPEN_REPLY
:
666 case RPCAP_MSG_STARTCAP_REPLY
:
667 case RPCAP_MSG_UPDATEFILTER_REPLY
:
668 case RPCAP_MSG_AUTH_REPLY
:
669 case RPCAP_MSG_STATS_REPLY
:
670 case RPCAP_MSG_ENDCAP_REPLY
:
671 case RPCAP_MSG_SETSAMPLING_REPLY
:
673 // These are server-to-client messages.
675 msg_type_string
= rpcap_msg_type_string(header
.type
);
676 if (msg_type_string
!= NULL
)
678 snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Server-to-client message %s received from client", msg_type_string
);
682 snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Server-to-client message of type %u received from client", header
.type
);
684 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
685 header
.ver
, PCAP_ERR_WRONGMSG
,
686 errmsgbuf
, errbuf
) == -1)
688 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
691 // Discard the rest of the message.
692 if (rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
) == -1)
701 // Unknown message type.
703 snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Unknown message type %u", header
.type
);
704 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
705 header
.ver
, PCAP_ERR_WRONGMSG
,
706 errmsgbuf
, errbuf
) == -1)
708 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
711 // Discard the rest of the message.
712 if (rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
) == -1)
722 // OK, the client has authenticated itself, and we can start
723 // processing regular requests from it.
727 // We don't have any statistics yet.
739 errbuf
[0] = 0; // clear errbuf
741 // Avoid zombies connections; check if the connection is opens but no commands are performed
742 // from more than RPCAP_TIMEOUT_RUNTIME
744 // - I have to be in normal mode (no active mode)
745 // - if the device is open, I don't have to be in the middle of a capture (session->sockdata)
746 // - if the device is closed, I have always to check if a new command arrives
748 // Be carefully: the capture can have been started, but an error occurred (so session != NULL, but
750 if ((!pars
.isactive
) && (session
== NULL
|| session
->sockdata
== 0))
752 // Check for the initial timeout
754 // We do not have to block here
755 tv
.tv_sec
= RPCAP_TIMEOUT_RUNTIME
;
758 FD_SET(pars
.sockctrl
, &rfds
);
759 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
762 retval
= select((int)pars
.sockctrl
+ 1, &rfds
, NULL
, NULL
, &tv
);
766 sock_geterror("select() failed", errmsgbuf
, PCAP_ERRBUF_SIZE
);
767 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
769 errmsgbuf
, errbuf
) == -1)
770 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
774 // The timeout has expired
775 // So, this was a fake connection. Drop it down
778 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
779 0, PCAP_ERR_INITTIMEOUT
,
780 "The RPCAP initial timeout has expired",
782 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
788 // Read the message header from the client.
790 nrecv
= rpcapd_recv_msg_header(pars
.sockctrl
, pars
.ssl
, &header
);
798 // Client closed the connection.
805 // Did the client specify a protocol version that we
808 if (!RPCAP_VERSION_IS_SUPPORTED(header
.ver
))
811 // Tell them it's not a supported version.
812 // Send the error message with their version,
813 // so they don't reject it as having the wrong
816 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
817 header
.ver
, PCAP_ERR_WRONGVER
,
818 "RPCAP version in message isn't supported by the server",
821 // That failed; log a message and give up.
822 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
826 // Discard the rest of the message.
827 (void)rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
);
834 case RPCAP_MSG_ERROR
: // The other endpoint reported an error
836 (void)daemon_msg_err(pars
.sockctrl
, pars
.ssl
, plen
);
837 // Do nothing; just exit; the error code is already into the errbuf
838 // XXX - actually exit....
842 case RPCAP_MSG_FINDALLIF_REQ
:
844 if (daemon_msg_findallif_req(header
.ver
, &pars
, plen
) == -1)
846 // Fatal error; a message has
847 // been logged, so just give up.
853 case RPCAP_MSG_OPEN_REQ
:
856 // Process the open request, and keep
857 // the source from it, for use later
858 // when the capture is started.
860 // XXX - we don't care if the client sends
861 // us multiple open requests, the last
864 retval
= daemon_msg_open_req(header
.ver
, &pars
,
865 plen
, source
, sizeof(source
));
868 // Fatal error; a message has
869 // been logged, so just give up.
876 case RPCAP_MSG_STARTCAP_REQ
:
880 // They never told us what device
882 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
884 PCAP_ERR_STARTCAPTURE
,
885 "No capture device was specified",
888 // Fatal error; log an
889 // error and give up.
890 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
893 if (rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
) == -1)
900 if (daemon_msg_startcap_req(header
.ver
, &pars
,
901 plen
, source
, &session
, &samp_param
,
904 // Fatal error; a message has
905 // been logged, so just give up.
911 case RPCAP_MSG_UPDATEFILTER_REQ
:
915 if (daemon_msg_updatefilter_req(header
.ver
,
916 &pars
, session
, plen
) == -1)
918 // Fatal error; a message has
919 // been logged, so just give up.
925 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
927 PCAP_ERR_UPDATEFILTER
,
928 "Device not opened. Cannot update filter",
931 // That failed; log a message and give up.
932 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
939 case RPCAP_MSG_CLOSE
: // The other endpoint close the pcap session
942 // Indicate to our caller that the client
943 // closed the control connection.
944 // This is used only in case of active mode.
946 client_told_us_to_close
= 1;
947 rpcapd_log(LOGPRIO_DEBUG
, "The other end system asked to close the connection.");
951 case RPCAP_MSG_STATS_REQ
:
953 if (daemon_msg_stats_req(header
.ver
, &pars
,
954 session
, plen
, &stats
, svrcapt
) == -1)
956 // Fatal error; a message has
957 // been logged, so just give up.
963 case RPCAP_MSG_ENDCAP_REQ
: // The other endpoint close the current capture session
967 // Save statistics (we can need them in the future)
968 if (pcap_stats(session
->fp
, &stats
))
970 svrcapt
= session
->TotCapt
;
980 if (daemon_msg_endcap_req(header
.ver
,
981 &pars
, session
) == -1)
985 // Fatal error; a message has
986 // been logged, so just give up.
994 rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
997 "Device not opened. Cannot close the capture",
1003 case RPCAP_MSG_SETSAMPLING_REQ
:
1005 if (daemon_msg_setsampling_req(header
.ver
,
1006 &pars
, plen
, &samp_param
) == -1)
1008 // Fatal error; a message has
1009 // been logged, so just give up.
1015 case RPCAP_MSG_AUTH_REQ
:
1018 // We're already authenticated; you don't
1019 // get to reauthenticate.
1021 rpcapd_log(LOGPRIO_INFO
, "The client sent an RPCAP_MSG_AUTH_REQ message after authentication was completed");
1022 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
1025 "RPCAP_MSG_AUTH_REQ request sent after authentication was completed",
1028 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1031 // Discard the rest of the message.
1032 if (rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
) == -1)
1039 case RPCAP_MSG_PACKET
:
1040 case RPCAP_MSG_FINDALLIF_REPLY
:
1041 case RPCAP_MSG_OPEN_REPLY
:
1042 case RPCAP_MSG_STARTCAP_REPLY
:
1043 case RPCAP_MSG_UPDATEFILTER_REPLY
:
1044 case RPCAP_MSG_AUTH_REPLY
:
1045 case RPCAP_MSG_STATS_REPLY
:
1046 case RPCAP_MSG_ENDCAP_REPLY
:
1047 case RPCAP_MSG_SETSAMPLING_REPLY
:
1049 // These are server-to-client messages.
1051 msg_type_string
= rpcap_msg_type_string(header
.type
);
1052 if (msg_type_string
!= NULL
)
1054 rpcapd_log(LOGPRIO_INFO
, "The client sent a %s server-to-client message", msg_type_string
);
1055 snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Server-to-client message %s received from client", msg_type_string
);
1059 rpcapd_log(LOGPRIO_INFO
, "The client sent a server-to-client message of type %u", header
.type
);
1060 snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Server-to-client message of type %u received from client", header
.type
);
1062 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
1063 header
.ver
, PCAP_ERR_WRONGMSG
,
1064 errmsgbuf
, errbuf
) == -1)
1066 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1069 // Discard the rest of the message.
1070 if (rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
) == -1)
1079 // Unknown message type.
1081 rpcapd_log(LOGPRIO_INFO
, "The client sent a message of type %u", header
.type
);
1082 snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Unknown message type %u", header
.type
);
1083 if (rpcap_senderror(pars
.sockctrl
, pars
.ssl
,
1084 header
.ver
, PCAP_ERR_WRONGMSG
,
1085 errbuf
, errmsgbuf
) == -1)
1087 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1090 // Discard the rest of the message.
1091 if (rpcapd_discard(pars
.sockctrl
, pars
.ssl
, plen
) == -1)
1102 // The service loop is finishing up.
1103 // If we have a capture session running, close it.
1106 session_close(session
);
1111 if (passiveClients
) {
1112 free(passiveClients
);
1115 // Finish using the SSL handle for the control socket, if we
1116 // have an SSL connection, and close the control socket.
1121 // Finish using the SSL handle for the socket.
1122 // This must be done *before* the socket is closed.
1126 sock_close(sockctrl
, NULL
, 0);
1128 // Print message and return
1129 rpcapd_log(LOGPRIO_DEBUG
, "I'm exiting from the child loop");
1131 return client_told_us_to_close
;
1135 * This handles the RPCAP_MSG_ERR message.
1138 daemon_msg_err(SOCKET sockctrl
, SSL
*ssl
, uint32 plen
)
1140 char errbuf
[PCAP_ERRBUF_SIZE
];
1141 char remote_errbuf
[PCAP_ERRBUF_SIZE
];
1143 if (plen
>= PCAP_ERRBUF_SIZE
)
1146 * Message is too long; just read as much of it as we
1147 * can into the buffer provided, and discard the rest.
1149 if (sock_recv(sockctrl
, ssl
, remote_errbuf
, PCAP_ERRBUF_SIZE
- 1,
1150 SOCK_RECEIVEALL_YES
|SOCK_EOF_IS_ERROR
, errbuf
,
1151 PCAP_ERRBUF_SIZE
) == -1)
1154 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
1157 if (rpcapd_discard(sockctrl
, ssl
, plen
- (PCAP_ERRBUF_SIZE
- 1)) == -1)
1164 * Null-terminate it.
1166 remote_errbuf
[PCAP_ERRBUF_SIZE
- 1] = '\0';
1170 /* Empty error string. */
1171 remote_errbuf
[0] = '\0';
1175 if (sock_recv(sockctrl
, ssl
, remote_errbuf
, plen
,
1176 SOCK_RECEIVEALL_YES
|SOCK_EOF_IS_ERROR
, errbuf
,
1177 PCAP_ERRBUF_SIZE
) == -1)
1180 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
1185 * Null-terminate it.
1187 remote_errbuf
[plen
] = '\0';
1190 rpcapd_log(LOGPRIO_ERROR
, "Error from client: %s", remote_errbuf
);
1195 * This handles the RPCAP_MSG_AUTH_REQ message.
1196 * It checks if the authentication credentials supplied by the user are valid.
1198 * This function is called if the daemon receives a RPCAP_MSG_AUTH_REQ
1199 * message in its authentication loop. It reads the body of the
1200 * authentication message from the network and checks whether the
1201 * credentials are valid.
1203 * \param sockctrl: the socket for the control connection.
1205 * \param nullAuthAllowed: '1' if the NULL authentication is allowed.
1207 * \param errbuf: a user-allocated buffer in which the error message
1208 * (if one) has to be written. It must be at least PCAP_ERRBUF_SIZE
1211 * \return '0' if everything is fine, '-1' if an unrecoverable error occurred,
1212 * or '-2' if the authentication failed. For errors, an error message is
1213 * returned in the 'errbuf' variable; this gives a message for the
1214 * unrecoverable error or for the authentication failure.
1217 daemon_msg_auth_req(struct daemon_slpars
*pars
, uint32 plen
)
1219 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
1220 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
1222 struct rpcap_auth auth
; // RPCAP authentication header
1223 char sendbuf
[RPCAP_NETBUF_SIZE
]; // temporary buffer in which data to be sent is buffered
1224 int sendbufidx
= 0; // index which keeps the number of bytes currently buffered
1225 struct rpcap_authreply
*authreply
; // authentication reply message
1227 status
= rpcapd_recv(pars
->sockctrl
, pars
->ssl
, (char *) &auth
, sizeof(struct rpcap_auth
), &plen
, errmsgbuf
);
1237 switch (ntohs(auth
.type
))
1239 case RPCAP_RMTAUTH_NULL
:
1241 if (!pars
->nullAuthAllowed
)
1243 // Send the client an error reply.
1244 snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
,
1245 "Authentication failed; NULL authentication not permitted.");
1246 if (rpcap_senderror(pars
->sockctrl
, pars
->ssl
,
1247 0, PCAP_ERR_AUTH_FAILED
, errmsgbuf
, errbuf
) == -1)
1249 // That failed; log a message and give up.
1250 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1258 case RPCAP_RMTAUTH_PWD
:
1260 char *username
, *passwd
;
1261 uint32 usernamelen
, passwdlen
;
1263 usernamelen
= ntohs(auth
.slen1
);
1264 username
= (char *) malloc (usernamelen
+ 1);
1265 if (username
== NULL
)
1267 pcap_fmt_errmsg_for_errno(errmsgbuf
,
1268 PCAP_ERRBUF_SIZE
, errno
, "malloc() failed");
1271 status
= rpcapd_recv(pars
->sockctrl
, pars
->ssl
, username
, usernamelen
, &plen
, errmsgbuf
);
1282 username
[usernamelen
] = '\0';
1284 passwdlen
= ntohs(auth
.slen2
);
1285 passwd
= (char *) malloc (passwdlen
+ 1);
1288 pcap_fmt_errmsg_for_errno(errmsgbuf
,
1289 PCAP_ERRBUF_SIZE
, errno
, "malloc() failed");
1293 status
= rpcapd_recv(pars
->sockctrl
, pars
->ssl
, passwd
, passwdlen
, &plen
, errmsgbuf
);
1306 passwd
[passwdlen
] = '\0';
1308 if (daemon_AuthUserPwd(username
, passwd
, errmsgbuf
))
1311 // Authentication failed. Let the client
1316 if (rpcap_senderror(pars
->sockctrl
, pars
->ssl
,
1317 0, PCAP_ERR_AUTH_FAILED
, errmsgbuf
, errbuf
) == -1)
1319 // That failed; log a message and give up.
1320 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1325 // Suspend for 1 second, so that they can't
1326 // hammer us with repeated tries with an
1327 // attack such as a dictionary attack.
1329 // WARNING: this delay is inserted only
1330 // at this point; if the client closes the
1331 // connection and reconnects, the suspension
1332 // time does not have any effect.
1334 sleep_secs(RPCAP_SUSPEND_WRONGAUTH
);
1344 snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
,
1345 "Authentication type not recognized.");
1346 if (rpcap_senderror(pars
->sockctrl
, pars
->ssl
,
1347 0, PCAP_ERR_AUTH_TYPE_NOTSUP
, errmsgbuf
, errbuf
) == -1)
1349 // That failed; log a message and give up.
1350 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1356 // The authentication succeeded; let the client know.
1357 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
, &sendbufidx
,
1358 RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1361 rpcap_createhdr((struct rpcap_header
*) sendbuf
, 0,
1362 RPCAP_MSG_AUTH_REPLY
, 0, sizeof(struct rpcap_authreply
));
1364 authreply
= (struct rpcap_authreply
*) &sendbuf
[sendbufidx
];
1366 if (sock_bufferize(NULL
, sizeof(struct rpcap_authreply
), NULL
, &sendbufidx
,
1367 RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1371 // Indicate to our peer what versions we support.
1373 memset(authreply
, 0, sizeof(struct rpcap_authreply
));
1374 authreply
->minvers
= RPCAP_MIN_VERSION
;
1375 authreply
->maxvers
= RPCAP_MAX_VERSION
;
1378 if (sock_send(pars
->sockctrl
, pars
->ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1380 // That failed; log a message and give up.
1381 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1385 // Check if all the data has been read; if not, discard the data in excess
1386 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
1394 if (rpcap_senderror(pars
->sockctrl
, pars
->ssl
, 0, PCAP_ERR_AUTH
,
1395 errmsgbuf
, errbuf
) == -1)
1397 // That failed; log a message and give up.
1398 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1403 // Check if all the data has been read; if not, discard the data in excess
1404 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
1413 daemon_AuthUserPwd(char *username
, char *password
, char *errbuf
)
1417 * Warning: the user which launches the process must have the
1418 * SE_TCB_NAME right.
1419 * This corresponds to have the "Act as part of the Operating System"
1420 * turned on (administrative tools, local security settings, local
1421 * policies, user right assignment)
1422 * However, it seems to me that if you run it as a service, this
1423 * right should be provided by default.
1425 * XXX - hopefully, this returns errors such as ERROR_LOGON_FAILURE,
1426 * which merely indicates that the user name or password is
1427 * incorrect, not whether it's the user name or the password
1428 * that's incorrect, so a client that's trying to brute-force
1429 * accounts doesn't know whether it's the user name or the
1430 * password that's incorrect, so it doesn't know whether to
1431 * stop trying to log in with a given user name and move on
1432 * to another user name.
1436 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to log
1438 if (LogonUser(username
, ".", password
, LOGON32_LOGON_NETWORK
, LOGON32_PROVIDER_DEFAULT
, &Token
) == 0)
1440 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Authentication failed");
1441 error
= GetLastError();
1442 if (error
!= ERROR_LOGON_FAILURE
)
1444 // Some error other than an authentication error;
1446 pcap_fmt_errmsg_for_win32_err(errmsgbuf
,
1447 PCAP_ERRBUF_SIZE
, error
, "LogonUser() failed");
1448 rpcapd_log(LOGPRIO_ERROR
, "%s", errmsgbuf
);
1453 // This call should change the current thread to the selected user.
1454 // I didn't test it.
1455 if (ImpersonateLoggedOnUser(Token
) == 0)
1457 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Authentication failed");
1458 pcap_fmt_errmsg_for_win32_err(errmsgbuf
, PCAP_ERRBUF_SIZE
,
1459 GetLastError(), "ImpersonateLoggedOnUser() failed");
1460 rpcapd_log(LOGPRIO_ERROR
, "%s", errmsgbuf
);
1472 * https://www.unixpapa.com/incnote/passwd.html
1474 * We use the Solaris/Linux shadow password authentication if
1475 * we have getspnam(), otherwise we just do traditional
1476 * authentication, which, on some platforms, might work, even
1477 * with shadow passwords, if we're running as root. Traditional
1478 * authenticaion won't work if we're not running as root, as
1479 * I think these days all UN*Xes either won't return the password
1480 * at all with getpwnam() or will only do so if you're root.
1482 * XXX - perhaps what we *should* be using is PAM, if we have
1483 * it. That might hide all the details of username/password
1484 * authentication, whether it's done with a visible-to-root-
1485 * only password database or some other authentication mechanism,
1489 struct passwd
*user
;
1490 char *user_password
;
1491 #ifdef HAVE_GETSPNAM
1492 struct spwd
*usersp
;
1494 char *crypt_password
;
1496 // This call is needed to get the uid
1497 if ((user
= getpwnam(username
)) == NULL
)
1499 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Authentication failed");
1503 #ifdef HAVE_GETSPNAM
1504 // This call is needed to get the password; otherwise 'x' is returned
1505 if ((usersp
= getspnam(username
)) == NULL
)
1507 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Authentication failed");
1510 user_password
= usersp
->sp_pwdp
;
1513 * XXX - what about other platforms?
1514 * The unixpapa.com page claims this Just Works on *BSD if you're
1515 * running as root - it's from 2000, so it doesn't indicate whether
1516 * macOS (which didn't come out until 2001, under the name Mac OS
1517 * X) behaves like the *BSDs or not, and might also work on AIX.
1518 * HP-UX does something else.
1520 * Again, hopefully PAM hides all that.
1522 user_password
= user
->pw_passwd
;
1526 // The Single UNIX Specification says that if crypt() fails it
1527 // sets errno, but some implementatons that haven't been run
1528 // through the SUS test suite might not do so.
1531 crypt_password
= crypt(password
, user_password
);
1532 if (crypt_password
== NULL
)
1535 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Authentication failed");
1538 // It didn't set errno.
1539 rpcapd_log(LOGPRIO_ERROR
, "crypt() failed");
1543 rpcapd_log(LOGPRIO_ERROR
, "crypt() failed: %s",
1548 if (strcmp(user_password
, crypt_password
) != 0)
1550 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Authentication failed");
1554 if (setuid(user
->pw_uid
))
1557 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1559 rpcapd_log(LOGPRIO_ERROR
, "setuid() failed: %s",
1564 /* if (setgid(user->pw_gid))
1567 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1569 rpcapd_log(LOGPRIO_ERROR, "setgid() failed: %s",
1581 * Make sure that the reply length won't overflow 32 bits if we add the
1582 * specified amount to it. If it won't, add that amount to it.
1584 * We check whether replylen + itemlen > UINT32_MAX, but subtract itemlen
1585 * from both sides, to prevent overflow.
1587 #define CHECK_AND_INCREASE_REPLY_LEN(itemlen) \
1588 if (replylen > UINT32_MAX - (itemlen)) { \
1589 pcap_strlcpy(errmsgbuf, "Reply length doesn't fit in 32 bits", \
1590 sizeof (errmsgbuf)); \
1593 replylen += (uint32)(itemlen)
1596 daemon_msg_findallif_req(uint8 ver
, struct daemon_slpars
*pars
, uint32 plen
)
1598 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
1599 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
1600 char sendbuf
[RPCAP_NETBUF_SIZE
]; // temporary buffer in which data to be sent is buffered
1601 int sendbufidx
= 0; // index which keeps the number of bytes currently buffered
1602 pcap_if_t
*alldevs
= NULL
; // pointer to the header of the interface chain
1603 pcap_if_t
*d
; // temp pointer needed to scan the interface chain
1604 struct pcap_addr
*address
; // pcap structure that keeps a network address of an interface
1605 uint32 replylen
; // length of reply payload
1606 uint16 nif
= 0; // counts the number of interface listed
1608 // Discard the rest of the message; there shouldn't be any payload.
1609 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
1615 // Retrieve the device list
1616 if (pcap_findalldevs(&alldevs
, errmsgbuf
) == -1)
1619 if (alldevs
== NULL
)
1621 if (rpcap_senderror(pars
->sockctrl
, pars
->ssl
, ver
,
1622 PCAP_ERR_NOREMOTEIF
,
1623 "No interfaces found! Make sure libpcap/WinPcap is properly installed"
1624 " and you have the right to access to the remote device.",
1627 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1633 // This checks the number of interfaces and computes the total
1634 // length of the payload.
1636 for (d
= alldevs
; d
!= NULL
; d
= d
->next
)
1640 if (d
->description
) {
1641 size_t stringlen
= strlen(d
->description
);
1642 if (stringlen
> UINT16_MAX
) {
1643 pcap_strlcpy(errmsgbuf
,
1644 "Description length doesn't fit in 16 bits",
1645 sizeof (errmsgbuf
));
1648 CHECK_AND_INCREASE_REPLY_LEN(stringlen
);
1651 size_t stringlen
= strlen(d
->name
);
1652 if (stringlen
> UINT16_MAX
) {
1653 pcap_strlcpy(errmsgbuf
,
1654 "Name length doesn't fit in 16 bits",
1655 sizeof (errmsgbuf
));
1658 CHECK_AND_INCREASE_REPLY_LEN(stringlen
);
1661 CHECK_AND_INCREASE_REPLY_LEN(sizeof(struct rpcap_findalldevs_if
));
1663 uint16_t naddrs
= 0;
1664 for (address
= d
->addresses
; address
!= NULL
; address
= address
->next
)
1667 * Send only IPv4 and IPv6 addresses over the wire.
1669 switch (address
->addr
->sa_family
)
1675 CHECK_AND_INCREASE_REPLY_LEN(sizeof(struct rpcap_sockaddr
) * 4);
1676 if (naddrs
== UINT16_MAX
) {
1677 pcap_strlcpy(errmsgbuf
,
1678 "Number of interfaces doesn't fit in 16 bits",
1679 sizeof (errmsgbuf
));
1691 // RPCAP findalldevs reply
1692 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
,
1693 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
,
1694 PCAP_ERRBUF_SIZE
) == -1)
1697 rpcap_createhdr((struct rpcap_header
*) sendbuf
, ver
,
1698 RPCAP_MSG_FINDALLIF_REPLY
, nif
, replylen
);
1700 // send the interface list
1701 for (d
= alldevs
; d
!= NULL
; d
= d
->next
)
1703 uint16 lname
, ldescr
;
1705 // Note: the findalldevs_if entries are *not* neatly
1706 // aligned on 4-byte boundaries, because they're
1707 // preceded by strings that aren't padded to 4-byte
1708 // boundaries, so we cannot just cast output buffer
1709 // boundaries to struct rpcap_findalldevs_if pointers
1710 // and store into them - we must fill in a structure and
1711 // then copy the structure to the buffer, as not all
1712 // systems support unaligned access (some, such as
1713 // SPARC, crash; others, such as Arm, may just ignore
1714 // the lower-order bits).
1715 struct rpcap_findalldevs_if findalldevs_if
;
1718 * We've already established that the string lengths
1722 ldescr
= (uint16
) strlen(d
->description
);
1726 lname
= (uint16
) strlen(d
->name
);
1730 findalldevs_if
.desclen
= htons(ldescr
);
1731 findalldevs_if
.namelen
= htons(lname
);
1732 findalldevs_if
.flags
= htonl(d
->flags
);
1734 uint16_t naddrs
= 0;
1735 for (address
= d
->addresses
; address
!= NULL
; address
= address
->next
)
1738 * Send only IPv4 and IPv6 addresses over the wire.
1740 switch (address
->addr
->sa_family
)
1753 findalldevs_if
.naddr
= htons(naddrs
);
1754 findalldevs_if
.dummy
= 0;
1756 if (sock_bufferize(&findalldevs_if
, sizeof(struct rpcap_findalldevs_if
), sendbuf
,
1757 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_BUFFERIZE
, errmsgbuf
,
1758 PCAP_ERRBUF_SIZE
) == -1)
1761 if (sock_bufferize(d
->name
, lname
, sendbuf
, &sendbufidx
,
1762 RPCAP_NETBUF_SIZE
, SOCKBUF_BUFFERIZE
, errmsgbuf
,
1763 PCAP_ERRBUF_SIZE
) == -1)
1766 if (sock_bufferize(d
->description
, ldescr
, sendbuf
, &sendbufidx
,
1767 RPCAP_NETBUF_SIZE
, SOCKBUF_BUFFERIZE
, errmsgbuf
,
1768 PCAP_ERRBUF_SIZE
) == -1)
1771 // send all addresses
1772 for (address
= d
->addresses
; address
!= NULL
; address
= address
->next
)
1774 struct rpcap_sockaddr
*sockaddr
;
1777 * Send only IPv4 and IPv6 addresses over the wire.
1779 switch (address
->addr
->sa_family
)
1785 sockaddr
= (struct rpcap_sockaddr
*) &sendbuf
[sendbufidx
];
1786 if (sock_bufferize(NULL
, sizeof(struct rpcap_sockaddr
), NULL
,
1787 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1789 daemon_seraddr((struct sockaddr_storage
*) address
->addr
, sockaddr
);
1791 sockaddr
= (struct rpcap_sockaddr
*) &sendbuf
[sendbufidx
];
1792 if (sock_bufferize(NULL
, sizeof(struct rpcap_sockaddr
), NULL
,
1793 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1795 daemon_seraddr((struct sockaddr_storage
*) address
->netmask
, sockaddr
);
1797 sockaddr
= (struct rpcap_sockaddr
*) &sendbuf
[sendbufidx
];
1798 if (sock_bufferize(NULL
, sizeof(struct rpcap_sockaddr
), NULL
,
1799 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1801 daemon_seraddr((struct sockaddr_storage
*) address
->broadaddr
, sockaddr
);
1803 sockaddr
= (struct rpcap_sockaddr
*) &sendbuf
[sendbufidx
];
1804 if (sock_bufferize(NULL
, sizeof(struct rpcap_sockaddr
), NULL
,
1805 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1807 daemon_seraddr((struct sockaddr_storage
*) address
->dstaddr
, sockaddr
);
1816 // We no longer need the device list. Free it.
1817 pcap_freealldevs(alldevs
);
1819 // Send a final command that says "now send it!"
1820 if (sock_send(pars
->sockctrl
, pars
->ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1822 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1830 pcap_freealldevs(alldevs
);
1832 if (rpcap_senderror(pars
->sockctrl
, pars
->ssl
, ver
,
1833 PCAP_ERR_FINDALLIF
, errmsgbuf
, errbuf
) == -1)
1835 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1842 \param plen: the length of the current message (needed in order to be able
1843 to discard excess data in the message, if present)
1846 daemon_msg_open_req(uint8 ver
, struct daemon_slpars
*pars
, uint32 plen
,
1847 char *source
, size_t sourcelen
)
1849 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
1850 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
1851 pcap_t
*fp
; // pcap_t main variable
1853 char sendbuf
[RPCAP_NETBUF_SIZE
]; // temporary buffer in which data to be sent is buffered
1854 int sendbufidx
= 0; // index which keeps the number of bytes currently buffered
1855 struct rpcap_openreply
*openreply
; // open reply message
1857 if (plen
> sourcelen
- 1)
1859 snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Source string too long");
1863 nread
= sock_recv(pars
->sockctrl
, pars
->ssl
, source
, plen
,
1864 SOCK_RECEIVEALL_YES
|SOCK_EOF_IS_ERROR
, errbuf
, PCAP_ERRBUF_SIZE
);
1867 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
1870 source
[nread
] = '\0';
1873 // Is this a URL rather than a device?
1874 // If so, reject it.
1877 snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Source string refers to a remote device");
1881 // Open the selected device
1882 // This is a fake open, since we do that only to get the needed parameters, then we close the device again
1883 if ((fp
= pcap_open_live(source
,
1884 1500 /* fake snaplen */,
1886 1000 /* fake timeout */,
1887 errmsgbuf
)) == NULL
)
1890 // Now, I can send a RPCAP open reply message
1891 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
, &sendbufidx
,
1892 RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1895 rpcap_createhdr((struct rpcap_header
*) sendbuf
, ver
,
1896 RPCAP_MSG_OPEN_REPLY
, 0, sizeof(struct rpcap_openreply
));
1898 openreply
= (struct rpcap_openreply
*) &sendbuf
[sendbufidx
];
1900 if (sock_bufferize(NULL
, sizeof(struct rpcap_openreply
), NULL
, &sendbufidx
,
1901 RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
1904 memset(openreply
, 0, sizeof(struct rpcap_openreply
));
1905 openreply
->linktype
= htonl(pcap_datalink(fp
));
1907 * This is always 0 for live captures; we no longer support it
1908 * as something we read from capture files and supply to
1909 * clients, but we have to send it over the wire, as open
1910 * replies are expected to have 8 bytes of payload by
1913 openreply
->tzoff
= 0;
1915 // We're done with the pcap_t.
1919 if (sock_send(pars
->sockctrl
, pars
->ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
1921 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1927 if (rpcap_senderror(pars
->sockctrl
, pars
->ssl
, ver
, PCAP_ERR_OPEN
,
1928 errmsgbuf
, errbuf
) == -1)
1930 // That failed; log a message and give up.
1931 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
1935 // Check if all the data has been read; if not, discard the data in excess
1936 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
1944 \param plen: the length of the current message (needed in order to be able
1945 to discard excess data in the message, if present)
1948 daemon_msg_startcap_req(uint8 ver
, struct daemon_slpars
*pars
, uint32 plen
,
1949 char *source
, struct session
**sessionp
,
1950 struct rpcap_sampling
*samp_param _U_
, int uses_ssl
)
1952 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
1953 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
1954 char portdata
[PCAP_BUF_SIZE
]; // temp variable needed to derive the data port
1955 char peerhost
[PCAP_BUF_SIZE
]; // temp variable needed to derive the host name of our peer
1956 struct session
*session
= NULL
; // saves state of session
1958 char sendbuf
[RPCAP_NETBUF_SIZE
]; // temporary buffer in which data to be sent is buffered
1959 int sendbufidx
= 0; // index which keeps the number of bytes currently buffered
1961 // socket-related variables
1962 struct addrinfo hints
; // temp, needed to open a socket connection
1963 struct addrinfo
*addrinfo
; // temp, needed to open a socket connection
1964 struct sockaddr_storage saddr
; // temp, needed to retrieve the network data port chosen on the local machine
1965 socklen_t saddrlen
; // temp, needed to retrieve the network data port chosen on the local machine
1966 int ret
; // return value from functions
1968 // RPCAP-related variables
1969 struct rpcap_startcapreq startcapreq
; // start capture request message
1970 struct rpcap_startcapreply
*startcapreply
; // start capture reply message
1971 int serveropen_dp
; // keeps who is going to open the data connection
1975 status
= rpcapd_recv(pars
->sockctrl
, pars
->ssl
, (char *) &startcapreq
,
1976 sizeof(struct rpcap_startcapreq
), &plen
, errmsgbuf
);
1986 startcapreq
.flags
= ntohs(startcapreq
.flags
);
1988 // Check that the client does not ask for UDP is the server has been asked
1989 // to enforce encryption, as SSL is not supported yet with UDP:
1990 if (uses_ssl
&& (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_DGRAM
))
1992 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1993 "SSL not supported with UDP forward of remote packets");
1997 // Create a session structure
1998 session
= malloc(sizeof(struct session
));
1999 if (session
== NULL
)
2001 snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Can't allocate session structure");
2005 session
->sockdata
= INVALID_SOCKET
;
2006 session
->ctrl_ssl
= session
->data_ssl
= NULL
;
2007 // We don't have a thread yet.
2008 session
->have_thread
= 0;
2010 // We *shouldn't* have to initialize the thread indicator
2011 // itself, because the compiler *should* realize that we
2012 // only use this if have_thread isn't 0, but we *do* have
2013 // to do it, because not all compilers *do* realize that.
2015 // There is no "invalid thread handle" value for a UN*X
2016 // pthread_t, so we just zero it out.
2019 session
->thread
= INVALID_HANDLE_VALUE
;
2021 memset(&session
->thread
, 0, sizeof(session
->thread
));
2024 // Open the selected device
2025 if ((session
->fp
= pcap_open_live(source
,
2026 ntohl(startcapreq
.snaplen
),
2027 (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_PROMISC
) ? 1 : 0 /* local device, other flags not needed */,
2028 ntohl(startcapreq
.read_timeout
),
2029 errmsgbuf
)) == NULL
)
2033 // Apply sampling parameters
2034 fp
->rmt_samp
.method
= samp_param
->method
;
2035 fp
->rmt_samp
.value
= samp_param
->value
;
2039 We're in active mode if:
2040 - we're using TCP, and the user wants us to be in active mode
2043 serveropen_dp
= (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_SERVEROPEN
) || (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_DGRAM
) || pars
->isactive
;
2046 Gets the sockaddr structure referred to the other peer in the ctrl connection
2048 We need that because:
2049 - if we're in passive mode, we need to know the address family we want to use
2050 (the same used for the ctrl socket)
2051 - if we're in active mode, we need to know the network address of the other host
2052 we want to connect to
2054 saddrlen
= sizeof(struct sockaddr_storage
);
2055 if (getpeername(pars
->sockctrl
, (struct sockaddr
*) &saddr
, &saddrlen
) == -1)
2057 sock_geterror("getpeername()", errmsgbuf
, PCAP_ERRBUF_SIZE
);
2061 memset(&hints
, 0, sizeof(struct addrinfo
));
2062 hints
.ai_socktype
= (startcapreq
.flags
& RPCAP_STARTCAPREQ_FLAG_DGRAM
) ? SOCK_DGRAM
: SOCK_STREAM
;
2063 hints
.ai_family
= saddr
.ss_family
;
2065 // Now we have to create a new socket to send packets
2066 if (serveropen_dp
) // Data connection is opened by the server toward the client
2068 snprintf(portdata
, sizeof portdata
, "%d", ntohs(startcapreq
.portdata
));
2070 // Get the name of the other peer (needed to connect to that specific network address)
2071 if (getnameinfo((struct sockaddr
*) &saddr
, saddrlen
, peerhost
,
2072 sizeof(peerhost
), NULL
, 0, NI_NUMERICHOST
))
2074 sock_geterror("getnameinfo()", errmsgbuf
, PCAP_ERRBUF_SIZE
);
2078 if (sock_initaddress(peerhost
, portdata
, &hints
, &addrinfo
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
2081 if ((session
->sockdata
= sock_open(addrinfo
, SOCKOPEN_CLIENT
, 0, errmsgbuf
, PCAP_ERRBUF_SIZE
)) == INVALID_SOCKET
)
2084 else // Data connection is opened by the client toward the server
2086 hints
.ai_flags
= AI_PASSIVE
;
2088 // Let's the server socket pick up a free network port for us
2089 if (sock_initaddress(NULL
, "0", &hints
, &addrinfo
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
2092 if ((session
->sockdata
= sock_open(addrinfo
, SOCKOPEN_SERVER
, 1 /* max 1 connection in queue */, errmsgbuf
, PCAP_ERRBUF_SIZE
)) == INVALID_SOCKET
)
2095 // get the complete sockaddr structure used in the data connection
2096 saddrlen
= sizeof(struct sockaddr_storage
);
2097 if (getsockname(session
->sockdata
, (struct sockaddr
*) &saddr
, &saddrlen
) == -1)
2099 sock_geterror("getsockname()", errmsgbuf
, PCAP_ERRBUF_SIZE
);
2103 // Get the local port the system picked up
2104 if (getnameinfo((struct sockaddr
*) &saddr
, saddrlen
, NULL
,
2105 0, portdata
, sizeof(portdata
), NI_NUMERICSERV
))
2107 sock_geterror("getnameinfo()", errmsgbuf
, PCAP_ERRBUF_SIZE
);
2112 // addrinfo is no longer used
2113 freeaddrinfo(addrinfo
);
2116 // Needed to send an error on the ctrl connection
2117 session
->sockctrl
= pars
->sockctrl
;
2118 session
->ctrl_ssl
= pars
->ssl
;
2119 session
->protocol_version
= ver
;
2121 // Now I can set the filter
2122 ret
= daemon_unpackapplyfilter(pars
->sockctrl
, pars
->ssl
, session
, &plen
, errmsgbuf
);
2125 // Fatal error. A message has been logged; just give up.
2130 // Non-fatal error. Send an error message to the client.
2134 // Now, I can send a RPCAP start capture reply message
2135 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
, &sendbufidx
,
2136 RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
2139 rpcap_createhdr((struct rpcap_header
*) sendbuf
, ver
,
2140 RPCAP_MSG_STARTCAP_REPLY
, 0, sizeof(struct rpcap_startcapreply
));
2142 startcapreply
= (struct rpcap_startcapreply
*) &sendbuf
[sendbufidx
];
2144 if (sock_bufferize(NULL
, sizeof(struct rpcap_startcapreply
), NULL
,
2145 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
2148 memset(startcapreply
, 0, sizeof(struct rpcap_startcapreply
));
2149 startcapreply
->bufsize
= htonl(pcap_bufsize(session
->fp
));
2153 unsigned short port
= (unsigned short)strtoul(portdata
,NULL
,10);
2154 startcapreply
->portdata
= htons(port
);
2157 if (sock_send(pars
->sockctrl
, pars
->ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2159 // That failed; log a message and give up.
2160 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2166 SOCKET socktemp
; // We need another socket, since we're going to accept() a connection
2168 // Connection creation
2169 saddrlen
= sizeof(struct sockaddr_storage
);
2171 socktemp
= accept(session
->sockdata
, (struct sockaddr
*) &saddr
, &saddrlen
);
2173 if (socktemp
== INVALID_SOCKET
)
2175 sock_geterror("accept()", errbuf
, PCAP_ERRBUF_SIZE
);
2176 rpcapd_log(LOGPRIO_ERROR
, "Accept of data connection failed: %s",
2181 // Now that I accepted the connection, the server socket is no longer needed
2182 sock_close(session
->sockdata
, NULL
, 0);
2183 session
->sockdata
= socktemp
;
2190 /* In both active or passive cases, wait for the client to initiate the
2191 * TLS handshake. Yes during that time the control socket will not be
2192 * served, but the same was true from the above call to accept(). */
2193 ssl
= ssl_promotion(1, session
->sockdata
, errbuf
, PCAP_ERRBUF_SIZE
);
2196 rpcapd_log(LOGPRIO_ERROR
, "TLS handshake failed: %s", errbuf
);
2201 session
->data_ssl
= ssl
;
2203 // Now we have to create a new thread to receive packets
2205 session
->thread
= (HANDLE
)_beginthreadex(NULL
, 0, daemon_thrdatamain
,
2206 (void *) session
, 0, NULL
);
2207 if (session
->thread
== 0)
2209 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Error creating the data thread");
2213 ret
= pthread_create(&session
->thread
, NULL
, daemon_thrdatamain
,
2217 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
2218 ret
, "Error creating the data thread");
2222 session
->have_thread
= 1;
2224 // Check if all the data has been read; if not, discard the data in excess
2225 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
2228 *sessionp
= session
;
2233 // Not a fatal error, so send the client an error message and
2234 // keep serving client requests.
2239 freeaddrinfo(addrinfo
);
2243 session_close(session
);
2247 if (rpcap_senderror(pars
->sockctrl
, pars
->ssl
, ver
,
2248 PCAP_ERR_STARTCAPTURE
, errmsgbuf
, errbuf
) == -1)
2250 // That failed; log a message and give up.
2251 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2255 // Check if all the data has been read; if not, discard the data in excess
2256 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
2266 // Fatal network error, so don't try to communicate with
2267 // the client, just give up.
2273 session_close(session
);
2281 daemon_msg_endcap_req(uint8 ver
, struct daemon_slpars
*pars
,
2282 struct session
*session
)
2284 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2285 struct rpcap_header header
;
2287 session_close(session
);
2289 rpcap_createhdr(&header
, ver
, RPCAP_MSG_ENDCAP_REPLY
, 0, 0);
2291 if (sock_send(pars
->sockctrl
, pars
->ssl
, (char *) &header
, sizeof(struct rpcap_header
), errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2293 // That failed; log a message and give up.
2294 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2302 // We impose a limit on the filter program size, so that, on Windows,
2303 // where there's only one server process with multiple threads, it's
2304 // harder to eat all the server address space by sending larger filter
2305 // programs. (This isn't an issue on UN*X, where there are multiple
2306 // server processes, one per client connection.)
2308 // We pick a value that limits each filter to 64K; that value is twice
2309 // the in-kernel limit for Linux and 16 times the in-kernel limit for
2312 // It also prevents an overflow on 32-bit platforms when calculating
2313 // the total size of the filter program. (It's not an issue on 64-bit
2314 // platforms with a 64-bit size_t, as the filter size is 32 bits.)
2316 #define RPCAP_BPF_MAXINSNS 8192
2319 daemon_unpackapplyfilter(SOCKET sockctrl
, SSL
*ctrl_ssl
, struct session
*session
, uint32
*plenp
, char *errmsgbuf
)
2322 struct rpcap_filter filter
;
2323 struct rpcap_filterbpf_insn insn
;
2324 struct bpf_insn
*bf_insn
;
2325 struct bpf_program bf_prog
;
2328 status
= rpcapd_recv(sockctrl
, ctrl_ssl
, (char *) &filter
,
2329 sizeof(struct rpcap_filter
), plenp
, errmsgbuf
);
2339 bf_prog
.bf_len
= ntohl(filter
.nitems
);
2341 if (ntohs(filter
.filtertype
) != RPCAP_UPDATEFILTER_BPF
)
2343 snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Only BPF/NPF filters are currently supported");
2347 if (bf_prog
.bf_len
> RPCAP_BPF_MAXINSNS
)
2349 snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
,
2350 "Filter program is larger than the maximum size of %d instructions",
2351 RPCAP_BPF_MAXINSNS
);
2354 bf_insn
= (struct bpf_insn
*) malloc (sizeof(struct bpf_insn
) * bf_prog
.bf_len
);
2355 if (bf_insn
== NULL
)
2357 pcap_fmt_errmsg_for_errno(errmsgbuf
, PCAP_ERRBUF_SIZE
,
2358 errno
, "malloc() failed");
2362 bf_prog
.bf_insns
= bf_insn
;
2364 for (i
= 0; i
< bf_prog
.bf_len
; i
++)
2366 status
= rpcapd_recv(sockctrl
, ctrl_ssl
, (char *) &insn
,
2367 sizeof(struct rpcap_filterbpf_insn
), plenp
, errmsgbuf
);
2377 bf_insn
->code
= ntohs(insn
.code
);
2378 bf_insn
->jf
= insn
.jf
;
2379 bf_insn
->jt
= insn
.jt
;
2380 bf_insn
->k
= ntohl(insn
.k
);
2386 // XXX - pcap_setfilter() should do the validation for us.
2388 if (bpf_validate(bf_prog
.bf_insns
, bf_prog
.bf_len
) == 0)
2390 snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "The filter contains bogus instructions");
2394 if (pcap_setfilter(session
->fp
, &bf_prog
))
2396 snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "RPCAP error: %s", pcap_geterr(session
->fp
));
2404 daemon_msg_updatefilter_req(uint8 ver
, struct daemon_slpars
*pars
,
2405 struct session
*session
, uint32 plen
)
2407 char errbuf
[PCAP_ERRBUF_SIZE
];
2408 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
2409 int ret
; // status of daemon_unpackapplyfilter()
2410 struct rpcap_header header
; // keeps the answer to the updatefilter command
2412 ret
= daemon_unpackapplyfilter(pars
->sockctrl
, pars
->ssl
, session
, &plen
, errmsgbuf
);
2415 // Fatal error. A message has been logged; just give up.
2420 // Non-fatal error. Send an error reply to the client.
2424 // Check if all the data has been read; if not, discard the data in excess
2425 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
2431 // A response is needed, otherwise the other host does not know that everything went well
2432 rpcap_createhdr(&header
, ver
, RPCAP_MSG_UPDATEFILTER_REPLY
, 0, 0);
2434 if (sock_send(pars
->sockctrl
, pars
->ssl
, (char *) &header
, sizeof (struct rpcap_header
), errbuf
, PCAP_ERRBUF_SIZE
))
2436 // That failed; log a message and give up.
2437 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2444 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
2448 rpcap_senderror(pars
->sockctrl
, pars
->ssl
, ver
, PCAP_ERR_UPDATEFILTER
,
2455 \brief Received the sampling parameters from remote host and it stores in the pcap_t structure.
2458 daemon_msg_setsampling_req(uint8 ver
, struct daemon_slpars
*pars
, uint32 plen
,
2459 struct rpcap_sampling
*samp_param
)
2461 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2462 char errmsgbuf
[PCAP_ERRBUF_SIZE
];
2463 struct rpcap_header header
;
2464 struct rpcap_sampling rpcap_samp
;
2467 status
= rpcapd_recv(pars
->sockctrl
, pars
->ssl
, (char *) &rpcap_samp
, sizeof(struct rpcap_sampling
), &plen
, errmsgbuf
);
2477 // Save these settings in the pcap_t
2478 samp_param
->method
= rpcap_samp
.method
;
2479 samp_param
->value
= ntohl(rpcap_samp
.value
);
2481 // A response is needed, otherwise the other host does not know that everything went well
2482 rpcap_createhdr(&header
, ver
, RPCAP_MSG_SETSAMPLING_REPLY
, 0, 0);
2484 if (sock_send(pars
->sockctrl
, pars
->ssl
, (char *) &header
, sizeof (struct rpcap_header
), errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2486 // That failed; log a message and give up.
2487 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2491 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
2499 if (rpcap_senderror(pars
->sockctrl
, pars
->ssl
, ver
, PCAP_ERR_SETSAMPLING
,
2500 errmsgbuf
, errbuf
) == -1)
2502 // That failed; log a message and give up.
2503 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2507 // Check if all the data has been read; if not, discard the data in excess
2508 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
2517 daemon_msg_stats_req(uint8 ver
, struct daemon_slpars
*pars
,
2518 struct session
*session
, uint32 plen
, struct pcap_stat
*stats
,
2519 unsigned int svrcapt
)
2521 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2522 char errmsgbuf
[PCAP_ERRBUF_SIZE
]; // buffer for errors to send to the client
2523 char sendbuf
[RPCAP_NETBUF_SIZE
]; // temporary buffer in which data to be sent is buffered
2524 int sendbufidx
= 0; // index which keeps the number of bytes currently buffered
2525 struct rpcap_stats
*netstats
; // statistics sent on the network
2527 // Checks that the header does not contain other data; if so, discard it
2528 if (rpcapd_discard(pars
->sockctrl
, pars
->ssl
, plen
) == -1)
2534 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
,
2535 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
2538 rpcap_createhdr((struct rpcap_header
*) sendbuf
, ver
,
2539 RPCAP_MSG_STATS_REPLY
, 0, (uint16
) sizeof(struct rpcap_stats
));
2541 netstats
= (struct rpcap_stats
*) &sendbuf
[sendbufidx
];
2543 if (sock_bufferize(NULL
, sizeof(struct rpcap_stats
), NULL
,
2544 &sendbufidx
, RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errmsgbuf
, PCAP_ERRBUF_SIZE
) == -1)
2547 if (session
&& session
->fp
)
2549 if (pcap_stats(session
->fp
, stats
) == -1)
2551 snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "%s", pcap_geterr(session
->fp
));
2555 netstats
->ifdrop
= htonl(stats
->ps_ifdrop
);
2556 netstats
->ifrecv
= htonl(stats
->ps_recv
);
2557 netstats
->krnldrop
= htonl(stats
->ps_drop
);
2558 netstats
->svrcapt
= htonl(session
->TotCapt
);
2562 // We have to keep compatibility with old applications,
2563 // which ask for statistics also when the capture has
2565 netstats
->ifdrop
= htonl(stats
->ps_ifdrop
);
2566 netstats
->ifrecv
= htonl(stats
->ps_recv
);
2567 netstats
->krnldrop
= htonl(stats
->ps_drop
);
2568 netstats
->svrcapt
= htonl(svrcapt
);
2572 if (sock_send(pars
->sockctrl
, pars
->ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2574 rpcapd_log(LOGPRIO_ERROR
, "Send to client failed: %s", errbuf
);
2581 rpcap_senderror(pars
->sockctrl
, pars
->ssl
, ver
, PCAP_ERR_GETSTATS
,
2587 static unsigned __stdcall
2591 daemon_thrdatamain(void *ptr
)
2593 char errbuf
[PCAP_ERRBUF_SIZE
+ 1]; // error buffer
2594 struct session
*session
; // pointer to the struct session for this session
2595 int retval
; // general variable used to keep the return value of other functions
2596 struct rpcap_pkthdr
*net_pkt_header
;// header of the packet
2597 struct pcap_pkthdr
*pkt_header
; // pointer to the buffer that contains the header of the current packet
2598 u_char
*pkt_data
; // pointer to the buffer that contains the current packet
2599 size_t sendbufsize
; // size for the send buffer
2600 char *sendbuf
; // temporary buffer in which data to be sent is buffered
2601 int sendbufidx
; // index which keeps the number of bytes currently buffered
2604 sigset_t sigusr1
; // signal set with just SIGUSR1
2607 session
= (struct session
*) ptr
;
2609 session
->TotCapt
= 0; // counter which is incremented each time a packet is received
2611 // Initialize errbuf
2612 memset(errbuf
, 0, sizeof(errbuf
));
2615 // We need a buffer large enough to hold a buffer large enough
2616 // for a maximum-size packet for this pcap_t.
2618 if (pcap_snapshot(session
->fp
) < 0)
2621 // The snapshot length is negative.
2622 // This "should not happen".
2624 rpcapd_log(LOGPRIO_ERROR
,
2625 "Unable to allocate the buffer for this child thread: snapshot length of %d is negative",
2626 pcap_snapshot(session
->fp
));
2627 sendbuf
= NULL
; // we can't allocate a buffer, so nothing to free
2631 // size_t is unsigned, and the result of pcap_snapshot() is signed;
2632 // on no platform that we support is int larger than size_t.
2633 // This means that, unless the extra information we prepend to
2634 // a maximum-sized packet is impossibly large, the sum of the
2635 // snapshot length and the size of that extra information will
2638 // So we don't need to make sure that sendbufsize will overflow.
2640 // However, we *do* need to make sure its value fits in an int,
2641 // because sock_send() can't send more than INT_MAX bytes (it could
2642 // do so on 64-bit UN*Xes, but can't do so on Windows, not even
2643 // 64-bit Windows, as the send() buffer size argument is an int
2646 sendbufsize
= sizeof(struct rpcap_header
) + sizeof(struct rpcap_pkthdr
) + pcap_snapshot(session
->fp
);
2647 if (sendbufsize
> INT_MAX
)
2649 rpcapd_log(LOGPRIO_ERROR
,
2650 "Buffer size for this child thread would be larger than %d",
2652 sendbuf
= NULL
; // we haven't allocated a buffer, so nothing to free
2655 sendbuf
= (char *) malloc (sendbufsize
);
2656 if (sendbuf
== NULL
)
2658 rpcapd_log(LOGPRIO_ERROR
,
2659 "Unable to allocate the buffer for this child thread");
2665 // Set the signal set to include just SIGUSR1, and block that
2666 // signal; we only want it unblocked when we're reading
2667 // packets - we dn't want any other system calls, such as
2668 // ones being used to send to the client or to log messages,
2669 // to be interrupted.
2671 sigemptyset(&sigusr1
);
2672 sigaddset(&sigusr1
, SIGUSR1
);
2673 pthread_sigmask(SIG_BLOCK
, &sigusr1
, NULL
);
2676 // Retrieve the packets
2681 // Unblock SIGUSR1 while we might be waiting for packets.
2683 pthread_sigmask(SIG_UNBLOCK
, &sigusr1
, NULL
);
2685 retval
= pcap_next_ex(session
->fp
, &pkt_header
, (const u_char
**) &pkt_data
); // cast to avoid a compiler warning
2688 // Now block it again.
2690 pthread_sigmask(SIG_BLOCK
, &sigusr1
, NULL
);
2694 if (retval
== 0) // Read timeout elapsed
2699 // Bufferize the general header
2700 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
,
2701 &sendbufidx
, (int)sendbufsize
, SOCKBUF_CHECKONLY
, errbuf
,
2702 PCAP_ERRBUF_SIZE
) == -1)
2704 rpcapd_log(LOGPRIO_ERROR
,
2705 "sock_bufferize() error sending packet message: %s",
2710 rpcap_createhdr((struct rpcap_header
*) sendbuf
,
2711 session
->protocol_version
, RPCAP_MSG_PACKET
, 0,
2712 (uint16
) (sizeof(struct rpcap_pkthdr
) + pkt_header
->caplen
));
2714 net_pkt_header
= (struct rpcap_pkthdr
*) &sendbuf
[sendbufidx
];
2716 // Bufferize the pkt header
2717 if (sock_bufferize(NULL
, sizeof(struct rpcap_pkthdr
), NULL
,
2718 &sendbufidx
, (int)sendbufsize
, SOCKBUF_CHECKONLY
, errbuf
,
2719 PCAP_ERRBUF_SIZE
) == -1)
2721 rpcapd_log(LOGPRIO_ERROR
,
2722 "sock_bufferize() error sending packet message: %s",
2727 net_pkt_header
->caplen
= htonl(pkt_header
->caplen
);
2728 net_pkt_header
->len
= htonl(pkt_header
->len
);
2729 net_pkt_header
->npkt
= htonl(++(session
->TotCapt
));
2731 // This protocol needs to be updated with a new version
2732 // before 2038-01-19 03:14:07 UTC.
2734 net_pkt_header
->timestamp_sec
= htonl((uint32
)pkt_header
->ts
.tv_sec
);
2735 net_pkt_header
->timestamp_usec
= htonl((uint32
)pkt_header
->ts
.tv_usec
);
2737 // Bufferize the pkt data
2738 if (sock_bufferize((char *) pkt_data
, pkt_header
->caplen
,
2739 sendbuf
, &sendbufidx
, (int)sendbufsize
, SOCKBUF_BUFFERIZE
,
2740 errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2742 rpcapd_log(LOGPRIO_ERROR
,
2743 "sock_bufferize() error sending packet message: %s",
2749 // If the client dropped the connection, don't report an
2750 // error, just quit.
2751 status
= sock_send(session
->sockdata
, session
->data_ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
);
2757 // Error other than "client closed the
2758 // connection out from under us"; report
2761 rpcapd_log(LOGPRIO_ERROR
,
2762 "Send of packet to client failed: %s",
2767 // Give up in either case.
2773 if (retval
< 0 && retval
!= PCAP_ERROR_BREAK
)
2776 // Failed with an error other than "we were told to break
2777 // out of the loop".
2779 // The latter just means that the client told us to stop
2780 // capturing, so there's no error to report.
2782 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Error reading the packets: %s", pcap_geterr(session
->fp
));
2783 rpcap_senderror(session
->sockctrl
, session
->ctrl_ssl
, session
->protocol_version
,
2784 PCAP_ERR_READEX
, errbuf
, NULL
);
2789 // The main thread will clean up the session structure.
2798 // Do-nothing handler for SIGUSR1; the sole purpose of SIGUSR1 is to
2799 // interrupt the data thread if it's blocked in a system call waiting
2800 // for packets to arrive.
2802 static void noop_handler(int sign _U_
)
2808 \brief It serializes a network address.
2810 It accepts a 'sockaddr_storage' structure as input, and it converts it appropriately into a format
2811 that can be used to be sent on the network. Basically, it applies all the hton()
2812 conversion required to the input variable.
2814 \param sockaddrin a 'sockaddr_storage' pointer to the variable that has to be
2815 serialized. This variable can be both a 'sockaddr_in' and 'sockaddr_in6'.
2817 \param sockaddrout an 'rpcap_sockaddr' pointer to the variable that will contain
2818 the serialized data. This variable has to be allocated by the user.
2820 \warning This function supports only AF_INET and AF_INET6 address families.
2823 daemon_seraddr(struct sockaddr_storage
*sockaddrin
, struct rpcap_sockaddr
*sockaddrout
)
2825 memset(sockaddrout
, 0, sizeof(struct sockaddr_storage
));
2827 // There can be the case in which the sockaddrin is not available
2828 if (sockaddrin
== NULL
) return;
2830 // Warning: we support only AF_INET and AF_INET6
2832 // Note: as noted above, the output structures are not
2833 // neatly aligned on 4-byte boundaries, so we must fill
2834 // in an aligned structure and then copy it to the output
2835 // buffer with memcpy().
2836 switch (sockaddrin
->ss_family
)
2840 struct sockaddr_in
*sockaddrin_ipv4
;
2841 struct rpcap_sockaddr_in sockaddrout_ipv4
;
2843 sockaddrin_ipv4
= (struct sockaddr_in
*) sockaddrin
;
2845 sockaddrout_ipv4
.family
= htons(RPCAP_AF_INET
);
2846 sockaddrout_ipv4
.port
= htons(sockaddrin_ipv4
->sin_port
);
2847 memcpy(&sockaddrout_ipv4
.addr
, &sockaddrin_ipv4
->sin_addr
, sizeof(sockaddrout_ipv4
.addr
));
2848 memset(sockaddrout_ipv4
.zero
, 0, sizeof(sockaddrout_ipv4
.zero
));
2849 memcpy(sockaddrout
, &sockaddrout_ipv4
, sizeof(struct rpcap_sockaddr_in
));
2856 struct sockaddr_in6
*sockaddrin_ipv6
;
2857 struct rpcap_sockaddr_in6 sockaddrout_ipv6
;
2859 sockaddrin_ipv6
= (struct sockaddr_in6
*) sockaddrin
;
2861 sockaddrout_ipv6
.family
= htons(RPCAP_AF_INET6
);
2862 sockaddrout_ipv6
.port
= htons(sockaddrin_ipv6
->sin6_port
);
2863 sockaddrout_ipv6
.flowinfo
= htonl(sockaddrin_ipv6
->sin6_flowinfo
);
2864 memcpy(&sockaddrout_ipv6
.addr
, &sockaddrin_ipv6
->sin6_addr
, sizeof(sockaddrout_ipv6
.addr
));
2865 sockaddrout_ipv6
.scope_id
= htonl(sockaddrin_ipv6
->sin6_scope_id
);
2866 memcpy(sockaddrout
, &sockaddrout_ipv6
, sizeof(struct rpcap_sockaddr_in6
));
2875 \brief Suspends a thread for secs seconds.
2877 void sleep_secs(int secs
)
2879 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
2883 unsigned secs_remaining
;
2887 secs_remaining
= secs
;
2888 while (secs_remaining
!= 0)
2889 secs_remaining
= sleep(secs_remaining
);
2895 * Read the header of a message.
2898 rpcapd_recv_msg_header(SOCKET sock
, SSL
*ssl
, struct rpcap_header
*headerp
)
2901 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2903 nread
= sock_recv(sock
, ssl
, (char *) headerp
, sizeof(struct rpcap_header
),
2904 SOCK_RECEIVEALL_YES
|SOCK_EOF_ISNT_ERROR
, errbuf
, PCAP_ERRBUF_SIZE
);
2908 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
2913 // Immediate EOF; that's treated like a close message.
2916 headerp
->plen
= ntohl(headerp
->plen
);
2921 * Read data from a message.
2922 * If we're trying to read more data that remains, puts an error
2923 * message into errmsgbuf and returns -2. Otherwise, tries to read
2924 * the data and, if that succeeds, subtracts the amount read from
2925 * the number of bytes of data that remains.
2926 * Returns 0 on success, logs a message and returns -1 on a network
2930 rpcapd_recv(SOCKET sock
, SSL
*ssl
, char *buffer
, size_t toread
, uint32
*plen
, char *errmsgbuf
)
2933 char errbuf
[PCAP_ERRBUF_SIZE
]; // buffer for network errors
2937 // Tell the client and continue.
2938 snprintf(errmsgbuf
, PCAP_ERRBUF_SIZE
, "Message payload is too short");
2941 nread
= sock_recv(sock
, ssl
, buffer
, toread
,
2942 SOCK_RECEIVEALL_YES
|SOCK_EOF_IS_ERROR
, errbuf
, PCAP_ERRBUF_SIZE
);
2945 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
2953 * Discard data from a connection.
2954 * Mostly used to discard wrong-sized messages.
2955 * Returns 0 on success, logs a message and returns -1 on a network
2959 rpcapd_discard(SOCKET sock
, SSL
*ssl
, uint32 len
)
2961 char errbuf
[PCAP_ERRBUF_SIZE
+ 1]; // keeps the error string, prior to be printed
2965 if (sock_discard(sock
, ssl
, len
, errbuf
, PCAP_ERRBUF_SIZE
) == -1)
2968 rpcapd_log(LOGPRIO_ERROR
, "Read from client failed: %s", errbuf
);
2976 // Shut down any packet-capture thread associated with the session,
2977 // close the SSL handle for the data socket if we have one, close
2978 // the data socket if we have one, and close the underlying packet
2979 // capture handle if we have one.
2981 // We do not, of course, touch the controlling socket that's also
2982 // copied into the session, as the service loop might still use it.
2984 static void session_close(struct session
*session
)
2986 if (session
->have_thread
)
2989 // Tell the data connection thread main capture loop to
2990 // break out of that loop.
2992 // This may be sufficient to wake up a blocked thread,
2993 // but it's not guaranteed to be sufficient.
2995 pcap_breakloop(session
->fp
);
2999 // Set the event on which a read would block, so that,
3000 // if it's currently blocked waiting for packets to
3001 // arrive, it'll wake up, so it can see the "break
3002 // out of the loop" indication. (pcap_breakloop()
3003 // might do this, but older versions don't. Setting
3004 // it twice should, at worst, cause an extra wakeup,
3005 // which shouldn't be a problem.)
3007 // XXX - what about modules other than NPF?
3009 SetEvent(pcap_getevent(session
->fp
));
3012 // Wait for the thread to exit, so we don't close
3013 // sockets out from under it.
3015 // XXX - have a timeout, so we don't wait forever?
3017 WaitForSingleObject(session
->thread
, INFINITE
);
3020 // Release the thread handle, as we're done with
3023 CloseHandle(session
->thread
);
3024 session
->have_thread
= 0;
3025 session
->thread
= INVALID_HANDLE_VALUE
;
3028 // Send a SIGUSR1 signal to the thread, so that, if
3029 // it's currently blocked waiting for packets to arrive,
3030 // it'll wake up (we've turned off SA_RESTART for
3031 // SIGUSR1, so that the system call in which it's blocked
3032 // should return EINTR rather than restarting).
3034 pthread_kill(session
->thread
, SIGUSR1
);
3037 // Wait for the thread to exit, so we don't close
3038 // sockets out from under it.
3040 // XXX - have a timeout, so we don't wait forever?
3042 pthread_join(session
->thread
, NULL
);
3043 session
->have_thread
= 0;
3044 memset(&session
->thread
, 0, sizeof(session
->thread
));
3049 if (session
->data_ssl
)
3051 // Finish using the SSL handle for the socket.
3052 // This must be done *before* the socket is closed.
3053 ssl_finish(session
->data_ssl
);
3054 session
->data_ssl
= NULL
;
3058 if (session
->sockdata
!= INVALID_SOCKET
)
3060 sock_close(session
->sockdata
, NULL
, 0);
3061 session
->sockdata
= INVALID_SOCKET
;
3066 pcap_close(session
->fp
);
3072 // Check whether a capture source string is a URL or not.
3073 // This includes URLs that refer to a local device; a scheme, followed
3074 // by ://, followed by *another* scheme and ://, is just silly, and
3075 // anybody who supplies that will get an error.
3078 is_url(const char *source
)
3085 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
3087 * hier-part = "//" authority path-abempty
3092 * authority = [ userinfo "@" ] host [ ":" port ]
3094 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
3096 * Step 1: look for the ":" at the end of the scheme.
3097 * A colon in the source is *NOT* sufficient to indicate that
3098 * this is a URL, as interface names on some platforms might
3099 * include colons (e.g., I think some Solaris interfaces
3102 colonp
= strchr(source
, ':');
3106 * The source is the device to open. It's not a URL.
3112 * All schemes must have "//" after them, i.e. we only support
3113 * hier-part = "//" authority path-abempty, not
3114 * hier-part = path-absolute
3115 * hier-part = path-rootless
3116 * hier-part = path-empty
3118 * We need that in order to distinguish between a local device
3119 * name that happens to contain a colon and a URI.
3121 if (strncmp(colonp
+ 1, "//", 2) != 0)
3124 * The source is the device to open. It's not a URL.