1335640Shselasky/* 2335640Shselasky * Copyright (c) 2002 - 2005 NetGroup, Politecnico di Torino (Italy) 3335640Shselasky * Copyright (c) 2005 - 2008 CACE Technologies, Davis (California) 4335640Shselasky * All rights reserved. 5335640Shselasky * 6335640Shselasky * Redistribution and use in source and binary forms, with or without 7335640Shselasky * modification, are permitted provided that the following conditions 8335640Shselasky * are met: 9335640Shselasky * 10335640Shselasky * 1. Redistributions of source code must retain the above copyright 11335640Shselasky * notice, this list of conditions and the following disclaimer. 12335640Shselasky * 2. Redistributions in binary form must reproduce the above copyright 13335640Shselasky * notice, this list of conditions and the following disclaimer in the 14335640Shselasky * documentation and/or other materials provided with the distribution. 15335640Shselasky * 3. Neither the name of the Politecnico di Torino, CACE Technologies 16335640Shselasky * nor the names of its contributors may be used to endorse or promote 17335640Shselasky * products derived from this software without specific prior written 18335640Shselasky * permission. 19335640Shselasky * 20335640Shselasky * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21335640Shselasky * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22335640Shselasky * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23335640Shselasky * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24335640Shselasky * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25335640Shselasky * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26335640Shselasky * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27335640Shselasky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28335640Shselasky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29335640Shselasky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30335640Shselasky * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31335640Shselasky * 32335640Shselasky */ 33335640Shselasky 34335640Shselasky#ifdef HAVE_CONFIG_H 35335640Shselasky#include <config.h> 36335640Shselasky#endif 37335640Shselasky 38335640Shselasky#include <string.h> /* for strlen(), ... */ 39335640Shselasky#include <stdlib.h> /* for malloc(), free(), ... */ 40335640Shselasky#include <stdarg.h> /* for functions with variable number of arguments */ 41335640Shselasky#include <errno.h> /* for the errno variable */ 42335640Shselasky#include "sockutils.h" 43335640Shselasky#include "portability.h" 44335640Shselasky#include "rpcap-protocol.h" 45335640Shselasky#include <pcap/pcap.h> 46335640Shselasky 47335640Shselasky/* 48335640Shselasky * This file contains functions used both by the rpcap client and the 49335640Shselasky * rpcap daemon. 50335640Shselasky */ 51335640Shselasky 52335640Shselasky/* 53335640Shselasky * This function sends a RPCAP error to our peer. 54335640Shselasky * 55335640Shselasky * It has to be called when the main program detects an error. 56335640Shselasky * It will send to our peer the 'buffer' specified by the user. 57335640Shselasky * This function *does not* request a RPCAP CLOSE connection. A CLOSE 58335640Shselasky * command must be sent explicitly by the program, since we do not know 59335640Shselasky * whether the error can be recovered in some way or if it is a 60335640Shselasky * non-recoverable one. 61335640Shselasky * 62335640Shselasky * \param sock: the socket we are currently using. 63335640Shselasky * 64335640Shselasky * \param ver: the protocol version we want to put in the reply. 65335640Shselasky * 66335640Shselasky * \param errcode: a integer which tells the other party the type of error 67335640Shselasky * we had. 68335640Shselasky * 69335640Shselasky * \param error: an user-allocated (and '0' terminated) buffer that contains 70335640Shselasky * the error description that has to be transmitted to our peer. The 71335640Shselasky * error message cannot be longer than PCAP_ERRBUF_SIZE. 72335640Shselasky * 73335640Shselasky * \param errbuf: a pointer to a user-allocated buffer (of size 74335640Shselasky * PCAP_ERRBUF_SIZE) that will contain the error message (in case there 75335640Shselasky * is one). It could be network problem. 76335640Shselasky * 77335640Shselasky * \return '0' if everything is fine, '-1' if some errors occurred. The 78335640Shselasky * error message is returned in the 'errbuf' variable. 79335640Shselasky */ 80335640Shselaskyint 81335640Shselaskyrpcap_senderror(SOCKET sock, uint8 ver, unsigned short errcode, const char *error, char *errbuf) 82335640Shselasky{ 83335640Shselasky char sendbuf[RPCAP_NETBUF_SIZE]; /* temporary buffer in which data to be sent is buffered */ 84335640Shselasky int sendbufidx = 0; /* index which keeps the number of bytes currently buffered */ 85335640Shselasky uint16 length; 86335640Shselasky 87335640Shselasky length = (uint16)strlen(error); 88335640Shselasky 89335640Shselasky if (length > PCAP_ERRBUF_SIZE) 90335640Shselasky length = PCAP_ERRBUF_SIZE; 91335640Shselasky 92335640Shselasky rpcap_createhdr((struct rpcap_header *) sendbuf, ver, RPCAP_MSG_ERROR, errcode, length); 93335640Shselasky 94335640Shselasky if (sock_bufferize(NULL, sizeof(struct rpcap_header), NULL, &sendbufidx, 95335640Shselasky RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errbuf, PCAP_ERRBUF_SIZE)) 96335640Shselasky return -1; 97335640Shselasky 98335640Shselasky if (sock_bufferize(error, length, sendbuf, &sendbufidx, 99335640Shselasky RPCAP_NETBUF_SIZE, SOCKBUF_BUFFERIZE, errbuf, PCAP_ERRBUF_SIZE)) 100335640Shselasky return -1; 101335640Shselasky 102335640Shselasky if (sock_send(sock, sendbuf, sendbufidx, errbuf, PCAP_ERRBUF_SIZE) < 0) 103335640Shselasky return -1; 104335640Shselasky 105335640Shselasky return 0; 106335640Shselasky} 107335640Shselasky 108335640Shselasky/* 109335640Shselasky * This function fills in a structure of type rpcap_header. 110335640Shselasky * 111335640Shselasky * It is provided just because the creation of an rpcap header is a common 112335640Shselasky * task. It accepts all the values that appears into an rpcap_header, and 113335640Shselasky * it puts them in place using the proper hton() calls. 114335640Shselasky * 115335640Shselasky * \param header: a pointer to a user-allocated buffer which will contain 116335640Shselasky * the serialized header, ready to be sent on the network. 117335640Shselasky * 118335640Shselasky * \param ver: a value (in the host byte order) which will be placed into the 119335640Shselasky * header.ver field and that represents the protocol version number of the 120335640Shselasky * current message. 121335640Shselasky * 122335640Shselasky * \param type: a value (in the host byte order) which will be placed into the 123335640Shselasky * header.type field and that represents the type of the current message. 124335640Shselasky * 125335640Shselasky * \param value: a value (in the host byte order) which will be placed into 126335640Shselasky * the header.value field and that has a message-dependent meaning. 127335640Shselasky * 128335640Shselasky * \param length: a value (in the host by order) which will be placed into 129335640Shselasky * the header.length field, representing the payload length of the message. 130335640Shselasky * 131335640Shselasky * \return Nothing. The serialized header is returned into the 'header' 132335640Shselasky * variable. 133335640Shselasky */ 134335640Shselaskyvoid 135335640Shselaskyrpcap_createhdr(struct rpcap_header *header, uint8 ver, uint8 type, uint16 value, uint32 length) 136335640Shselasky{ 137335640Shselasky memset(header, 0, sizeof(struct rpcap_header)); 138335640Shselasky 139335640Shselasky header->ver = ver; 140335640Shselasky header->type = type; 141335640Shselasky header->value = htons(value); 142335640Shselasky header->plen = htonl(length); 143335640Shselasky} 144335640Shselasky 145335640Shselasky/* 146335640Shselasky * Convert a message type to a string containing the type name. 147335640Shselasky */ 148335640Shselaskystatic const char *requests[] = 149335640Shselasky{ 150335640Shselasky NULL, /* not a valid message type */ 151335640Shselasky "RPCAP_MSG_ERROR", 152335640Shselasky "RPCAP_MSG_FINDALLIF_REQ", 153335640Shselasky "RPCAP_MSG_OPEN_REQ", 154335640Shselasky "RPCAP_MSG_STARTCAP_REQ", 155335640Shselasky "RPCAP_MSG_UPDATEFILTER_REQ", 156335640Shselasky "RPCAP_MSG_CLOSE", 157335640Shselasky "RPCAP_MSG_PACKET", 158335640Shselasky "RPCAP_MSG_AUTH_REQ", 159335640Shselasky "RPCAP_MSG_STATS_REQ", 160335640Shselasky "RPCAP_MSG_ENDCAP_REQ", 161335640Shselasky "RPCAP_MSG_SETSAMPLING_REQ", 162335640Shselasky}; 163335640Shselasky#define NUM_REQ_TYPES (sizeof requests / sizeof requests[0]) 164335640Shselasky 165335640Shselaskystatic const char *replies[] = 166335640Shselasky{ 167335640Shselasky NULL, 168335640Shselasky NULL, /* this would be a reply to RPCAP_MSG_ERROR */ 169335640Shselasky "RPCAP_MSG_FINDALLIF_REPLY", 170335640Shselasky "RPCAP_MSG_OPEN_REPLY", 171335640Shselasky "RPCAP_MSG_STARTCAP_REPLY", 172335640Shselasky "RPCAP_MSG_UPDATEFILTER_REPLY", 173335640Shselasky NULL, /* this would be a reply to RPCAP_MSG_CLOSE */ 174335640Shselasky NULL, /* this would be a reply to RPCAP_MSG_PACKET */ 175335640Shselasky "RPCAP_MSG_AUTH_REPLY", 176335640Shselasky "RPCAP_MSG_STATS_REPLY", 177335640Shselasky "RPCAP_MSG_ENDCAP_REPLY", 178335640Shselasky "RPCAP_MSG_SETSAMPLING_REPLY", 179335640Shselasky}; 180335640Shselasky#define NUM_REPLY_TYPES (sizeof replies / sizeof replies[0]) 181335640Shselasky 182335640Shselaskyconst char * 183335640Shselaskyrpcap_msg_type_string(uint8 type) 184335640Shselasky{ 185335640Shselasky if (type & RPCAP_MSG_IS_REPLY) { 186335640Shselasky type &= ~RPCAP_MSG_IS_REPLY; 187335640Shselasky if (type >= NUM_REPLY_TYPES) 188335640Shselasky return NULL; 189335640Shselasky return replies[type]; 190335640Shselasky } else { 191335640Shselasky if (type >= NUM_REQ_TYPES) 192335640Shselasky return NULL; 193335640Shselasky return requests[type]; 194335640Shselasky } 195335640Shselasky} 196