Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
nmap
GitHub Repository: nmap/nmap
Path: blob/master/libnetutil/RawData.cc
3733 views
1
/***************************************************************************
2
* RawData.cc -- The RawData Class represents a network packet payload. It *
3
* is essentially a single buffer that may contain either random data or *
4
* caller supplied data. This class can be used, for example, to be linked *
5
* to a UDP datagram. *
6
* *
7
***********************IMPORTANT NMAP LICENSE TERMS************************
8
*
9
* The Nmap Security Scanner is (C) 1996-2025 Nmap Software LLC ("The Nmap
10
* Project"). Nmap is also a registered trademark of the Nmap Project.
11
*
12
* This program is distributed under the terms of the Nmap Public Source
13
* License (NPSL). The exact license text applying to a particular Nmap
14
* release or source code control revision is contained in the LICENSE
15
* file distributed with that version of Nmap or source code control
16
* revision. More Nmap copyright/legal information is available from
17
* https://nmap.org/book/man-legal.html, and further information on the
18
* NPSL license itself can be found at https://nmap.org/npsl/ . This
19
* header summarizes some key points from the Nmap license, but is no
20
* substitute for the actual license text.
21
*
22
* Nmap is generally free for end users to download and use themselves,
23
* including commercial use. It is available from https://nmap.org.
24
*
25
* The Nmap license generally prohibits companies from using and
26
* redistributing Nmap in commercial products, but we sell a special Nmap
27
* OEM Edition with a more permissive license and special features for
28
* this purpose. See https://nmap.org/oem/
29
*
30
* If you have received a written Nmap license agreement or contract
31
* stating terms other than these (such as an Nmap OEM license), you may
32
* choose to use and redistribute Nmap under those terms instead.
33
*
34
* The official Nmap Windows builds include the Npcap software
35
* (https://npcap.com) for packet capture and transmission. It is under
36
* separate license terms which forbid redistribution without special
37
* permission. So the official Nmap Windows builds may not be redistributed
38
* without special permission (such as an Nmap OEM license).
39
*
40
* Source is provided to this software because we believe users have a
41
* right to know exactly what a program is going to do before they run it.
42
* This also allows you to audit the software for security holes.
43
*
44
* Source code also allows you to port Nmap to new platforms, fix bugs, and
45
* add new features. You are highly encouraged to submit your changes as a
46
* Github PR or by email to the [email protected] mailing list for possible
47
* incorporation into the main distribution. Unless you specify otherwise, it
48
* is understood that you are offering us very broad rights to use your
49
* submissions as described in the Nmap Public Source License Contributor
50
* Agreement. This is important because we fund the project by selling licenses
51
* with various terms, and also because the inability to relicense code has
52
* caused devastating problems for other Free Software projects (such as KDE
53
* and NASM).
54
*
55
* The free version of Nmap is distributed in the hope that it will be
56
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
57
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Warranties,
58
* indemnification and commercial support are all available through the
59
* Npcap OEM program--see https://nmap.org/oem/
60
*
61
***************************************************************************/
62
/* This code was originally part of the Nping tool. */
63
64
#include "RawData.h"
65
66
/******************************************************************************/
67
/* CONTRUCTORS, DESTRUCTORS AND INITIALIZATION METHODS */
68
/******************************************************************************/
69
RawData::RawData(){
70
this->reset();
71
} /* End of RawData contructor */
72
73
74
RawData::~RawData(){
75
if(this->data!=NULL){
76
free(this->data);
77
this->data=NULL;
78
}
79
} /* End of RawData destructor */
80
81
82
/** Sets every attribute to its default value */
83
void RawData::reset(){
84
this->data=NULL;
85
this->length=0;
86
} /* End of reset() */
87
88
89
/******************************************************************************/
90
/* PacketElement:: OVERWRITTEN METHODS */
91
/******************************************************************************/
92
93
u8 * RawData::getBufferPointer(){
94
return this->getBufferPointer(NULL);
95
} /* End of getBufferPointer() */
96
97
98
u8 * RawData::getBufferPointer(int *mylen){
99
if(mylen!=NULL)
100
*mylen=this->length;
101
return this->data;
102
} /* End of getBufferPointer() */
103
104
105
/** Added for consistency with the rest of classes of the PacketElement family. */
106
int RawData::storeRecvData(const u8 *buf, size_t len){
107
return this->store(buf, len);
108
} /* End of storeRecvData() */
109
110
111
/* Returns a protocol identifier. This is used by packet parsing funtions
112
* that return linked lists of PacketElement objects, to determine the protocol
113
* the object represents. */
114
int RawData::protocol_id() const {
115
return HEADER_TYPE_RAW_DATA;
116
} /* End of protocol_id() */
117
118
119
/** Determines if the data stored in the object after an storeRecvData() call
120
* is valid and safe to use. This mainly checks the length of the data but may
121
* also test the value of certain protocol fields to ensure their correctness.
122
* @return the length, in bytes, of the header, if its found to be valid or
123
* OP_FAILURE (-1) otherwise. */
124
int RawData::validate(){
125
return this->length;
126
} /* End of validate() */
127
128
129
/** Prints the contents of the header and calls print() on the next protocol
130
* header in the chain (if there is any).
131
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
132
int RawData::print(FILE *output, int detail) const {
133
fprintf(output, "Payload[");
134
fprintf(output, "%d byte%s]", this->length, (this->length!=1)? "s":"");
135
if(this->next!=NULL){
136
print_separator(output, detail);
137
next->print(output, detail);
138
}
139
return OP_SUCCESS;
140
} /* End of print() */
141
142
143
/******************************************************************************/
144
/* PROTOCOL-SPECIFIC METHODS */
145
/******************************************************************************/
146
147
int RawData::store(const u8 *buf, size_t len){
148
/* If buffer had already been set, try to reuse it. */
149
if(this->data!=NULL){
150
if( this->length >= (int)len ){
151
memcpy(this->data, buf, len);
152
this->length=(int)len;
153
return OP_SUCCESS;
154
}else{
155
free(this->data);
156
}
157
}
158
if( (this->data=(u8 *)calloc(len, sizeof(u8)))==NULL )
159
return OP_FAILURE;
160
memcpy(this->data, buf, len);
161
this->length=(int)len;
162
return OP_SUCCESS;
163
} /* End of store() */
164
165
166
int RawData::store(const char *str){
167
if(str==NULL)
168
return OP_FAILURE;
169
else
170
return this->store((const u8*)str, strlen(str));
171
} /* End of store() */
172
173
174
175