]>
The Tcpdump Group git mirrors - libpcap/blob - rpcap-protocol.c
2 * Copyright (c) 2002 - 2005 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (c) 2005 - 2008 CACE Technologies, Davis (California)
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, CACE Technologies
16 * nor the names of its contributors may be used to endorse or promote
17 * products derived from this software without specific prior written
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <string.h> /* for strlen(), ... */
37 #include <stdlib.h> /* for malloc(), free(), ... */
38 #include <stdarg.h> /* for functions with variable number of arguments */
39 #include <stdint.h> /* for intN_t and uintN_t types */
40 #include <errno.h> /* for the errno variable */
41 #include "sockutils.h"
42 #include "portability.h"
43 #include "rpcap-protocol.h"
44 #include <pcap/pcap.h>
47 * This file contains functions used both by the rpcap client and the
52 * This function sends a RPCAP error to our peer.
54 * It has to be called when the main program detects an error.
55 * It will send to our peer the 'buffer' specified by the user.
56 * This function *does not* request a RPCAP CLOSE connection. A CLOSE
57 * command must be sent explicitly by the program, since we do not know
58 * whether the error can be recovered in some way or if it is a
59 * non-recoverable one.
61 * \param sock: the socket we are currently using.
63 * \param ssl: if compiled with openssl, the optional ssl handler to use with the above socket.
65 * \param ver: the protocol version we want to put in the reply.
67 * \param errcode: a integer which tells the other party the type of error
70 * \param error: an user-allocated (and '0' terminated) buffer that contains
71 * the error description that has to be transmitted to our peer. The
72 * error message cannot be longer than PCAP_ERRBUF_SIZE.
74 * \param errbuf: a pointer to a user-allocated buffer (of size
75 * PCAP_ERRBUF_SIZE) that will contain the error message (in case there
76 * is one). It could be network problem.
78 * \return '0' if everything is fine, '-1' if some errors occurred. The
79 * error message is returned in the 'errbuf' variable.
82 rpcap_senderror(PCAP_SOCKET sock
, SSL
*ssl
, uint8_t ver
, unsigned short errcode
, const char *error
, char *errbuf
)
84 char sendbuf
[RPCAP_NETBUF_SIZE
]; /* temporary buffer in which data to be sent is buffered */
85 int sendbufidx
= 0; /* index which keeps the number of bytes currently buffered */
88 length
= (uint16_t)strlen(error
);
90 if (length
> PCAP_ERRBUF_SIZE
)
91 length
= PCAP_ERRBUF_SIZE
;
93 rpcap_createhdr((struct rpcap_header
*) sendbuf
, ver
, RPCAP_MSG_ERROR
, errcode
, length
);
95 if (sock_bufferize(NULL
, sizeof(struct rpcap_header
), NULL
, &sendbufidx
,
96 RPCAP_NETBUF_SIZE
, SOCKBUF_CHECKONLY
, errbuf
, PCAP_ERRBUF_SIZE
))
99 if (sock_bufferize(error
, length
, sendbuf
, &sendbufidx
,
100 RPCAP_NETBUF_SIZE
, SOCKBUF_BUFFERIZE
, errbuf
, PCAP_ERRBUF_SIZE
))
103 if (sock_send(sock
, ssl
, sendbuf
, sendbufidx
, errbuf
, PCAP_ERRBUF_SIZE
) < 0)
110 * This function fills in a structure of type rpcap_header.
112 * It is provided just because the creation of an rpcap header is a common
113 * task. It accepts all the values that appears into an rpcap_header, and
114 * it puts them in place using the proper hton() calls.
116 * \param header: a pointer to a user-allocated buffer which will contain
117 * the serialized header, ready to be sent on the network.
119 * \param ver: a value (in the host byte order) which will be placed into the
120 * header.ver field and that represents the protocol version number of the
123 * \param type: a value (in the host byte order) which will be placed into the
124 * header.type field and that represents the type of the current message.
126 * \param value: a value (in the host byte order) which will be placed into
127 * the header.value field and that has a message-dependent meaning.
129 * \param length: a value (in the host by order) which will be placed into
130 * the header.length field, representing the payload length of the message.
132 * \return Nothing. The serialized header is returned into the 'header'
136 rpcap_createhdr(struct rpcap_header
*header
, uint8_t ver
, uint8_t type
, uint16_t value
, uint32_t length
)
138 memset(header
, 0, sizeof(struct rpcap_header
));
142 header
->value
= htons(value
);
143 header
->plen
= htonl(length
);
147 * Convert a message type to a string containing the type name.
149 static const char *requests
[] =
151 NULL
, /* not a valid message type */
153 "RPCAP_MSG_FINDALLIF_REQ",
154 "RPCAP_MSG_OPEN_REQ",
155 "RPCAP_MSG_STARTCAP_REQ",
156 "RPCAP_MSG_UPDATEFILTER_REQ",
159 "RPCAP_MSG_AUTH_REQ",
160 "RPCAP_MSG_STATS_REQ",
161 "RPCAP_MSG_ENDCAP_REQ",
162 "RPCAP_MSG_SETSAMPLING_REQ",
164 #define NUM_REQ_TYPES (sizeof requests / sizeof requests[0])
166 static const char *replies
[] =
169 NULL
, /* this would be a reply to RPCAP_MSG_ERROR */
170 "RPCAP_MSG_FINDALLIF_REPLY",
171 "RPCAP_MSG_OPEN_REPLY",
172 "RPCAP_MSG_STARTCAP_REPLY",
173 "RPCAP_MSG_UPDATEFILTER_REPLY",
174 NULL
, /* this would be a reply to RPCAP_MSG_CLOSE */
175 NULL
, /* this would be a reply to RPCAP_MSG_PACKET */
176 "RPCAP_MSG_AUTH_REPLY",
177 "RPCAP_MSG_STATS_REPLY",
178 "RPCAP_MSG_ENDCAP_REPLY",
179 "RPCAP_MSG_SETSAMPLING_REPLY",
181 #define NUM_REPLY_TYPES (sizeof replies / sizeof replies[0])
184 rpcap_msg_type_string(uint8_t type
)
186 if (type
& RPCAP_MSG_IS_REPLY
) {
187 type
&= ~RPCAP_MSG_IS_REPLY
;
188 if (type
>= NUM_REPLY_TYPES
)
190 return replies
[type
];
192 if (type
>= NUM_REQ_TYPES
)
194 return requests
[type
];