]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/libpcap/rpcap-protocol.c
ident(1): Normalizing date format
[FreeBSD/FreeBSD.git] / contrib / libpcap / rpcap-protocol.c
1 /*
2  * Copyright (c) 2002 - 2005 NetGroup, Politecnico di Torino (Italy)
3  * Copyright (c) 2005 - 2008 CACE Technologies, Davis (California)
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
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
18  * permission.
19  *
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.
31  *
32  */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include <string.h>             /* for strlen(), ... */
39 #include <stdlib.h>             /* for malloc(), free(), ... */
40 #include <stdarg.h>             /* for functions with variable number of arguments */
41 #include <errno.h>              /* for the errno variable */
42 #include "sockutils.h"
43 #include "portability.h"
44 #include "rpcap-protocol.h"
45 #include <pcap/pcap.h>
46
47 /*
48  * This file contains functions used both by the rpcap client and the
49  * rpcap daemon.
50  */
51
52 /*
53  * This function sends a RPCAP error to our peer.
54  *
55  * It has to be called when the main program detects an error.
56  * It will send to our peer the 'buffer' specified by the user.
57  * This function *does not* request a RPCAP CLOSE connection. A CLOSE
58  * command must be sent explicitly by the program, since we do not know
59  * whether the error can be recovered in some way or if it is a
60  * non-recoverable one.
61  *
62  * \param sock: the socket we are currently using.
63  *
64  * \param ver: the protocol version we want to put in the reply.
65  *
66  * \param errcode: a integer which tells the other party the type of error
67  * we had.
68  *
69  * \param error: an user-allocated (and '0' terminated) buffer that contains
70  * the error description that has to be transmitted to our peer. The
71  * error message cannot be longer than PCAP_ERRBUF_SIZE.
72  *
73  * \param errbuf: a pointer to a user-allocated buffer (of size
74  * PCAP_ERRBUF_SIZE) that will contain the error message (in case there
75  * is one). It could be network problem.
76  *
77  * \return '0' if everything is fine, '-1' if some errors occurred. The
78  * error message is returned in the 'errbuf' variable.
79  */
80 int
81 rpcap_senderror(SOCKET sock, uint8 ver, unsigned short errcode, const char *error, char *errbuf)
82 {
83         char sendbuf[RPCAP_NETBUF_SIZE];        /* temporary buffer in which data to be sent is buffered */
84         int sendbufidx = 0;                     /* index which keeps the number of bytes currently buffered */
85         uint16 length;
86
87         length = (uint16)strlen(error);
88
89         if (length > PCAP_ERRBUF_SIZE)
90                 length = PCAP_ERRBUF_SIZE;
91
92         rpcap_createhdr((struct rpcap_header *) sendbuf, ver, RPCAP_MSG_ERROR, errcode, length);
93
94         if (sock_bufferize(NULL, sizeof(struct rpcap_header), NULL, &sendbufidx,
95                 RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errbuf, PCAP_ERRBUF_SIZE))
96                 return -1;
97
98         if (sock_bufferize(error, length, sendbuf, &sendbufidx,
99                 RPCAP_NETBUF_SIZE, SOCKBUF_BUFFERIZE, errbuf, PCAP_ERRBUF_SIZE))
100                 return -1;
101
102         if (sock_send(sock, sendbuf, sendbufidx, errbuf, PCAP_ERRBUF_SIZE) < 0)
103                 return -1;
104
105         return 0;
106 }
107
108 /*
109  * This function fills in a structure of type rpcap_header.
110  *
111  * It is provided just because the creation of an rpcap header is a common
112  * task. It accepts all the values that appears into an rpcap_header, and
113  * it puts them in place using the proper hton() calls.
114  *
115  * \param header: a pointer to a user-allocated buffer which will contain
116  * the serialized header, ready to be sent on the network.
117  *
118  * \param ver: a value (in the host byte order) which will be placed into the
119  * header.ver field and that represents the protocol version number of the
120  * current message.
121  *
122  * \param type: a value (in the host byte order) which will be placed into the
123  * header.type field and that represents the type of the current message.
124  *
125  * \param value: a value (in the host byte order) which will be placed into
126  * the header.value field and that has a message-dependent meaning.
127  *
128  * \param length: a value (in the host by order) which will be placed into
129  * the header.length field, representing the payload length of the message.
130  *
131  * \return Nothing. The serialized header is returned into the 'header'
132  * variable.
133  */
134 void
135 rpcap_createhdr(struct rpcap_header *header, uint8 ver, uint8 type, uint16 value, uint32 length)
136 {
137         memset(header, 0, sizeof(struct rpcap_header));
138
139         header->ver = ver;
140         header->type = type;
141         header->value = htons(value);
142         header->plen = htonl(length);
143 }
144
145 /*
146  * Convert a message type to a string containing the type name.
147  */
148 static const char *requests[] =
149 {
150         NULL,                           /* not a valid message type */
151         "RPCAP_MSG_ERROR",
152         "RPCAP_MSG_FINDALLIF_REQ",
153         "RPCAP_MSG_OPEN_REQ",
154         "RPCAP_MSG_STARTCAP_REQ",
155         "RPCAP_MSG_UPDATEFILTER_REQ",
156         "RPCAP_MSG_CLOSE",
157         "RPCAP_MSG_PACKET",
158         "RPCAP_MSG_AUTH_REQ",
159         "RPCAP_MSG_STATS_REQ",
160         "RPCAP_MSG_ENDCAP_REQ",
161         "RPCAP_MSG_SETSAMPLING_REQ",
162 };
163 #define NUM_REQ_TYPES   (sizeof requests / sizeof requests[0])
164
165 static const char *replies[] =
166 {
167         NULL,
168         NULL,                   /* this would be a reply to RPCAP_MSG_ERROR */
169         "RPCAP_MSG_FINDALLIF_REPLY",
170         "RPCAP_MSG_OPEN_REPLY",
171         "RPCAP_MSG_STARTCAP_REPLY",
172         "RPCAP_MSG_UPDATEFILTER_REPLY",
173         NULL,                   /* this would be a reply to RPCAP_MSG_CLOSE */
174         NULL,                   /* this would be a reply to RPCAP_MSG_PACKET */
175         "RPCAP_MSG_AUTH_REPLY",
176         "RPCAP_MSG_STATS_REPLY",
177         "RPCAP_MSG_ENDCAP_REPLY",
178         "RPCAP_MSG_SETSAMPLING_REPLY",
179 };
180 #define NUM_REPLY_TYPES (sizeof replies / sizeof replies[0])
181
182 const char *
183 rpcap_msg_type_string(uint8 type)
184 {
185         if (type & RPCAP_MSG_IS_REPLY) {
186                 type &= ~RPCAP_MSG_IS_REPLY;
187                 if (type >= NUM_REPLY_TYPES)
188                         return NULL;
189                 return replies[type];
190         } else {
191                 if (type >= NUM_REQ_TYPES)
192                         return NULL;
193                 return requests[type];
194         }
195 }