/***************************************************************************1* RawData.cc -- The RawData Class represents a network packet payload. It *2* is essentially a single buffer that may contain either random data or *3* caller supplied data. This class can be used, for example, to be linked *4* to a UDP datagram. *5* *6***********************IMPORTANT NMAP LICENSE TERMS************************7*8* The Nmap Security Scanner is (C) 1996-2025 Nmap Software LLC ("The Nmap9* Project"). Nmap is also a registered trademark of the Nmap Project.10*11* This program is distributed under the terms of the Nmap Public Source12* License (NPSL). The exact license text applying to a particular Nmap13* release or source code control revision is contained in the LICENSE14* file distributed with that version of Nmap or source code control15* revision. More Nmap copyright/legal information is available from16* https://nmap.org/book/man-legal.html, and further information on the17* NPSL license itself can be found at https://nmap.org/npsl/ . This18* header summarizes some key points from the Nmap license, but is no19* substitute for the actual license text.20*21* Nmap is generally free for end users to download and use themselves,22* including commercial use. It is available from https://nmap.org.23*24* The Nmap license generally prohibits companies from using and25* redistributing Nmap in commercial products, but we sell a special Nmap26* OEM Edition with a more permissive license and special features for27* this purpose. See https://nmap.org/oem/28*29* If you have received a written Nmap license agreement or contract30* stating terms other than these (such as an Nmap OEM license), you may31* choose to use and redistribute Nmap under those terms instead.32*33* The official Nmap Windows builds include the Npcap software34* (https://npcap.com) for packet capture and transmission. It is under35* separate license terms which forbid redistribution without special36* permission. So the official Nmap Windows builds may not be redistributed37* without special permission (such as an Nmap OEM license).38*39* Source is provided to this software because we believe users have a40* right to know exactly what a program is going to do before they run it.41* This also allows you to audit the software for security holes.42*43* Source code also allows you to port Nmap to new platforms, fix bugs, and44* add new features. You are highly encouraged to submit your changes as a45* Github PR or by email to the [email protected] mailing list for possible46* incorporation into the main distribution. Unless you specify otherwise, it47* is understood that you are offering us very broad rights to use your48* submissions as described in the Nmap Public Source License Contributor49* Agreement. This is important because we fund the project by selling licenses50* with various terms, and also because the inability to relicense code has51* caused devastating problems for other Free Software projects (such as KDE52* and NASM).53*54* The free version of Nmap is distributed in the hope that it will be55* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of56* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Warranties,57* indemnification and commercial support are all available through the58* Npcap OEM program--see https://nmap.org/oem/59*60***************************************************************************/61/* This code was originally part of the Nping tool. */6263#include "RawData.h"6465/******************************************************************************/66/* CONTRUCTORS, DESTRUCTORS AND INITIALIZATION METHODS */67/******************************************************************************/68RawData::RawData(){69this->reset();70} /* End of RawData contructor */717273RawData::~RawData(){74if(this->data!=NULL){75free(this->data);76this->data=NULL;77}78} /* End of RawData destructor */798081/** Sets every attribute to its default value */82void RawData::reset(){83this->data=NULL;84this->length=0;85} /* End of reset() */868788/******************************************************************************/89/* PacketElement:: OVERWRITTEN METHODS */90/******************************************************************************/9192u8 * RawData::getBufferPointer(){93return this->getBufferPointer(NULL);94} /* End of getBufferPointer() */959697u8 * RawData::getBufferPointer(int *mylen){98if(mylen!=NULL)99*mylen=this->length;100return this->data;101} /* End of getBufferPointer() */102103104/** Added for consistency with the rest of classes of the PacketElement family. */105int RawData::storeRecvData(const u8 *buf, size_t len){106return this->store(buf, len);107} /* End of storeRecvData() */108109110/* Returns a protocol identifier. This is used by packet parsing funtions111* that return linked lists of PacketElement objects, to determine the protocol112* the object represents. */113int RawData::protocol_id() const {114return HEADER_TYPE_RAW_DATA;115} /* End of protocol_id() */116117118/** Determines if the data stored in the object after an storeRecvData() call119* is valid and safe to use. This mainly checks the length of the data but may120* also test the value of certain protocol fields to ensure their correctness.121* @return the length, in bytes, of the header, if its found to be valid or122* OP_FAILURE (-1) otherwise. */123int RawData::validate(){124return this->length;125} /* End of validate() */126127128/** Prints the contents of the header and calls print() on the next protocol129* header in the chain (if there is any).130* @return OP_SUCCESS on success and OP_FAILURE in case of error. */131int RawData::print(FILE *output, int detail) const {132fprintf(output, "Payload[");133fprintf(output, "%d byte%s]", this->length, (this->length!=1)? "s":"");134if(this->next!=NULL){135print_separator(output, detail);136next->print(output, detail);137}138return OP_SUCCESS;139} /* End of print() */140141142/******************************************************************************/143/* PROTOCOL-SPECIFIC METHODS */144/******************************************************************************/145146int RawData::store(const u8 *buf, size_t len){147/* If buffer had already been set, try to reuse it. */148if(this->data!=NULL){149if( this->length >= (int)len ){150memcpy(this->data, buf, len);151this->length=(int)len;152return OP_SUCCESS;153}else{154free(this->data);155}156}157if( (this->data=(u8 *)calloc(len, sizeof(u8)))==NULL )158return OP_FAILURE;159memcpy(this->data, buf, len);160this->length=(int)len;161return OP_SUCCESS;162} /* End of store() */163164165int RawData::store(const char *str){166if(str==NULL)167return OP_FAILURE;168else169return this->store((const u8*)str, strlen(str));170} /* End of store() */171172173174175