Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
nmap
GitHub Repository: nmap/nmap
Path: blob/master/libnetutil/ARPHeader.cc
3733 views
1
/***************************************************************************
2
* ARPHeader.cc -- The ARPHeader Class represents an ARP packet. It *
3
* contains methods to set any header field. In general, these methods do *
4
* error checkings and byte order conversion. *
5
* *
6
***********************IMPORTANT NMAP LICENSE TERMS************************
7
*
8
* The Nmap Security Scanner is (C) 1996-2025 Nmap Software LLC ("The Nmap
9
* Project"). Nmap is also a registered trademark of the Nmap Project.
10
*
11
* This program is distributed under the terms of the Nmap Public Source
12
* License (NPSL). The exact license text applying to a particular Nmap
13
* release or source code control revision is contained in the LICENSE
14
* file distributed with that version of Nmap or source code control
15
* revision. More Nmap copyright/legal information is available from
16
* https://nmap.org/book/man-legal.html, and further information on the
17
* NPSL license itself can be found at https://nmap.org/npsl/ . This
18
* header summarizes some key points from the Nmap license, but is no
19
* 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 and
25
* redistributing Nmap in commercial products, but we sell a special Nmap
26
* OEM Edition with a more permissive license and special features for
27
* this purpose. See https://nmap.org/oem/
28
*
29
* If you have received a written Nmap license agreement or contract
30
* stating terms other than these (such as an Nmap OEM license), you may
31
* choose to use and redistribute Nmap under those terms instead.
32
*
33
* The official Nmap Windows builds include the Npcap software
34
* (https://npcap.com) for packet capture and transmission. It is under
35
* separate license terms which forbid redistribution without special
36
* permission. So the official Nmap Windows builds may not be redistributed
37
* without special permission (such as an Nmap OEM license).
38
*
39
* Source is provided to this software because we believe users have a
40
* 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, and
44
* add new features. You are highly encouraged to submit your changes as a
45
* Github PR or by email to the [email protected] mailing list for possible
46
* incorporation into the main distribution. Unless you specify otherwise, it
47
* is understood that you are offering us very broad rights to use your
48
* submissions as described in the Nmap Public Source License Contributor
49
* Agreement. This is important because we fund the project by selling licenses
50
* with various terms, and also because the inability to relicense code has
51
* caused devastating problems for other Free Software projects (such as KDE
52
* and NASM).
53
*
54
* The free version of Nmap is distributed in the hope that it will be
55
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
56
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Warranties,
57
* indemnification and commercial support are all available through the
58
* Npcap OEM program--see https://nmap.org/oem/
59
*
60
***************************************************************************/
61
/* This code was originally part of the Nping tool. */
62
63
#include "ARPHeader.h"
64
65
/******************************************************************************/
66
/* CONTRUCTORS, DESTRUCTORS AND INITIALIZATION METHODS */
67
/******************************************************************************/
68
ARPHeader::ARPHeader() {
69
this->reset();
70
} /* End of ARPHeader constructor */
71
72
73
ARPHeader::~ARPHeader() {
74
75
} /* End of ARPHeader destructor */
76
77
78
/** Sets every attribute to its default value */
79
void ARPHeader::reset(){
80
memset (&this->h, 0, sizeof(nping_arp_hdr_t));
81
this->length=ARP_HEADER_LEN;
82
} /* End of reset() */
83
84
85
/******************************************************************************/
86
/* PacketElement:: OVERWRITTEN METHODS */
87
/******************************************************************************/
88
89
/** @warning This method is essential for the superclass getBinaryBuffer()
90
* method to work. Do NOT change a thing unless you know what you're doing */
91
u8 *ARPHeader::getBufferPointer(){
92
return (u8*)(&h);
93
} /* End of getBufferPointer() */
94
95
96
/** Stores supplied packet in the internal buffer so the information
97
* can be accessed using the standard get & set methods.
98
* @warning The ARPHeader class is able to hold a maximum of 28 bytes.
99
* If the supplied buffer is longer than that, only the first 28 bytes will be
100
* stored in the internal buffer.
101
* @warning Supplied len MUST be at least 28 bytes (ARP header length).
102
* @return OP_SUCCESS on success and OP_FAILURE in case of error */
103
int ARPHeader::storeRecvData(const u8 *buf, size_t len){
104
if(buf==NULL || len<ARP_HEADER_LEN){
105
return OP_FAILURE;
106
}else{
107
this->reset(); /* Re-init the object, just in case the caller had used it already */
108
this->length=ARP_HEADER_LEN;
109
memcpy(&(this->h), buf, ARP_HEADER_LEN);
110
}
111
return OP_SUCCESS;
112
} /* End of storeRecvData() */
113
114
115
/* Returns a protocol identifier. This is used by packet parsing funtions
116
* that return linked lists of PacketElement objects, to determine the protocol
117
* the object represents. */
118
int ARPHeader::protocol_id() const {
119
return HEADER_TYPE_ARP;
120
} /* End of protocol_id() */
121
122
123
/** Determines if the data stored in the object after an storeRecvData() call
124
* is valid and safe to use. This mainly checks the length of the data but may
125
* also test the value of certain protocol fields to ensure their correctness.
126
* @return the length, in bytes, of the header, if its found to be valid or
127
* OP_FAILURE (-1) otherwise. */
128
int ARPHeader::validate(){
129
if( this->length!=ARP_HEADER_LEN)
130
return OP_FAILURE;
131
else
132
return ARP_HEADER_LEN;
133
} /* End of validate() */
134
135
136
/** Prints the contents of the header and calls print() on the next protocol
137
* header in the chain (if there is any).
138
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
139
int ARPHeader::print(FILE *output, int detail) const {
140
fprintf(output, "ARP[]");
141
if(this->next!=NULL){
142
print_separator(output, detail);
143
next->print(output, detail);
144
}
145
return OP_SUCCESS;
146
} /* End of print() */
147
148
149
/******************************************************************************/
150
/* PROTOCOL-SPECIFIC METHODS */
151
/******************************************************************************/
152
153
/** Sets HardwareType.
154
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
155
int ARPHeader::setHardwareType(u16 val){
156
this->h.ar_hrd=htons(val);
157
return OP_SUCCESS;
158
} /* End of setHardwareType() */
159
160
161
/** Sets HardwareType to ETHERNET.
162
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
163
int ARPHeader::setHardwareType(){
164
this->h.ar_hrd=htons(HDR_ETH10MB);
165
return OP_SUCCESS;
166
} /* End of setHardwareType() */
167
168
169
/** Returns value of attribute h.ar_hrd */
170
u16 ARPHeader::getHardwareType(){
171
return ntohs(this->h.ar_hrd);
172
} /* End of getHardwareType() */
173
174
175
/** Sets ProtocolType.
176
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
177
int ARPHeader::setProtocolType(u16 val){
178
this->h.ar_pro=htons(val);
179
return OP_SUCCESS;
180
} /* End of setProtocolType() */
181
182
183
/** Sets ProtocolType.
184
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
185
int ARPHeader::setProtocolType(){
186
this->h.ar_pro=htons(0x0800); /* DEFAULT: IPv4 */
187
return OP_SUCCESS;
188
} /* End of setProtocolType() */
189
190
191
/** Returns value of attribute h.ar_pro */
192
u16 ARPHeader::getProtocolType(){
193
return ntohs(this->h.ar_pro);
194
} /* End of getProtocolType() */
195
196
197
/** Sets HwAddrLen.
198
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
199
int ARPHeader::setHwAddrLen(u8 val){
200
this->h.ar_hln=val;
201
return OP_SUCCESS;
202
} /* End of setHwAddrLen() */
203
204
205
/** Sets HwAddrLen.
206
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
207
int ARPHeader::setHwAddrLen(){
208
this->h.ar_hln=ETH_ADDRESS_LEN;
209
return OP_SUCCESS;
210
} /* End of setHwAddrLen() */
211
212
213
/** Returns value of attribute h.ar_hln */
214
u8 ARPHeader::getHwAddrLen(){
215
return this->h.ar_hln;
216
} /* End of getHwAddrLen() */
217
218
219
/** Sets ProtoAddrLen.
220
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
221
int ARPHeader::setProtoAddrLen(u8 val){
222
this->h.ar_pln=val;
223
return OP_SUCCESS;
224
} /* End of setProtoAddrLen() */
225
226
227
/** Sets ProtoAddrLen.
228
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
229
int ARPHeader::setProtoAddrLen(){
230
this->h.ar_pln=IPv4_ADDRESS_LEN; /* DEFAULT: IPv4 */
231
return OP_SUCCESS;
232
} /* End of setProtoAddrLen() */
233
234
235
/** Returns value of attribute h.ar_pln */
236
u8 ARPHeader::getProtoAddrLen(){
237
return this->h.ar_pln;
238
} /* End of getProtoAddrLen() */
239
240
241
/** Sets OpCode.
242
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
243
int ARPHeader::setOpCode(u16 val){
244
this->h.ar_op=htons(val);
245
return OP_SUCCESS;
246
} /* End of setOpCode() */
247
248
249
/** Returns value of attribute h.ar_op */
250
u16 ARPHeader::getOpCode(){
251
return ntohs(this->h.ar_op);
252
} /* End of getOpCode() */
253
254
255
/** Sets SenderMAC.
256
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
257
int ARPHeader::setSenderMAC(const u8 * val){
258
if(val==NULL)
259
return OP_FAILURE;
260
memcpy(this->h.data, val, ETH_ADDRESS_LEN);
261
return OP_SUCCESS;
262
} /* End of setSenderMAC() */
263
264
265
/** Returns value of attribute h.ar_sha */
266
u8 * ARPHeader::getSenderMAC(){
267
return this->h.data;
268
} /* End of getSenderMAC() */
269
270
271
/** Sets SenderIP.
272
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
273
int ARPHeader::setSenderIP(struct in_addr val){
274
memcpy(this->h.data+6, &val.s_addr, 4);
275
return OP_SUCCESS;
276
} /* End of setSenderIP() */
277
278
279
/** Returns value of attribute h.ar_sip */
280
u32 ARPHeader::getSenderIP(){
281
u32 *p = (u32 *)(this->h.data+6);
282
return *p;
283
} /* End of getSenderIP() */
284
285
286
/** Sets TargetMAC.
287
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
288
int ARPHeader::setTargetMAC(u8 * val){
289
if(val==NULL)
290
return OP_FAILURE;
291
memcpy(this->h.data+10, val, ETH_ADDRESS_LEN);
292
return OP_SUCCESS;
293
} /* End of setTargetMAC() */
294
295
296
/** Returns value of attribute h.ar_tha */
297
u8 * ARPHeader::getTargetMAC(){
298
return this->h.data+10;
299
} /* End of getTargetMAC() */
300
301
302
/** Sets TargetIP.
303
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
304
int ARPHeader::setTargetIP(struct in_addr val){
305
memcpy(this->h.data+16, &val.s_addr, 4);
306
return OP_SUCCESS;
307
} /* End of setTargetIP() */
308
309
310
/** Returns value of attribute h.ar_tip */
311
u32 ARPHeader::getTargetIP(){
312
u32 *p = (u32 *)(this->h.data+16);
313
return *p;
314
} /* End of getTargetIP() */
315
316