/***************************************************************************1* ARPHeader.cc -- The ARPHeader Class represents an ARP packet. It *2* contains methods to set any header field. In general, these methods do *3* error checkings and byte order conversion. *4* *5***********************IMPORTANT NMAP LICENSE TERMS************************6*7* The Nmap Security Scanner is (C) 1996-2025 Nmap Software LLC ("The Nmap8* Project"). Nmap is also a registered trademark of the Nmap Project.9*10* This program is distributed under the terms of the Nmap Public Source11* License (NPSL). The exact license text applying to a particular Nmap12* release or source code control revision is contained in the LICENSE13* file distributed with that version of Nmap or source code control14* revision. More Nmap copyright/legal information is available from15* https://nmap.org/book/man-legal.html, and further information on the16* NPSL license itself can be found at https://nmap.org/npsl/ . This17* header summarizes some key points from the Nmap license, but is no18* substitute for the actual license text.19*20* Nmap is generally free for end users to download and use themselves,21* including commercial use. It is available from https://nmap.org.22*23* The Nmap license generally prohibits companies from using and24* redistributing Nmap in commercial products, but we sell a special Nmap25* OEM Edition with a more permissive license and special features for26* this purpose. See https://nmap.org/oem/27*28* If you have received a written Nmap license agreement or contract29* stating terms other than these (such as an Nmap OEM license), you may30* choose to use and redistribute Nmap under those terms instead.31*32* The official Nmap Windows builds include the Npcap software33* (https://npcap.com) for packet capture and transmission. It is under34* separate license terms which forbid redistribution without special35* permission. So the official Nmap Windows builds may not be redistributed36* without special permission (such as an Nmap OEM license).37*38* Source is provided to this software because we believe users have a39* right to know exactly what a program is going to do before they run it.40* This also allows you to audit the software for security holes.41*42* Source code also allows you to port Nmap to new platforms, fix bugs, and43* add new features. You are highly encouraged to submit your changes as a44* Github PR or by email to the [email protected] mailing list for possible45* incorporation into the main distribution. Unless you specify otherwise, it46* is understood that you are offering us very broad rights to use your47* submissions as described in the Nmap Public Source License Contributor48* Agreement. This is important because we fund the project by selling licenses49* with various terms, and also because the inability to relicense code has50* caused devastating problems for other Free Software projects (such as KDE51* and NASM).52*53* The free version of Nmap is distributed in the hope that it will be54* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of55* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Warranties,56* indemnification and commercial support are all available through the57* Npcap OEM program--see https://nmap.org/oem/58*59***************************************************************************/60/* This code was originally part of the Nping tool. */6162#include "ARPHeader.h"6364/******************************************************************************/65/* CONTRUCTORS, DESTRUCTORS AND INITIALIZATION METHODS */66/******************************************************************************/67ARPHeader::ARPHeader() {68this->reset();69} /* End of ARPHeader constructor */707172ARPHeader::~ARPHeader() {7374} /* End of ARPHeader destructor */757677/** Sets every attribute to its default value */78void ARPHeader::reset(){79memset (&this->h, 0, sizeof(nping_arp_hdr_t));80this->length=ARP_HEADER_LEN;81} /* End of reset() */828384/******************************************************************************/85/* PacketElement:: OVERWRITTEN METHODS */86/******************************************************************************/8788/** @warning This method is essential for the superclass getBinaryBuffer()89* method to work. Do NOT change a thing unless you know what you're doing */90u8 *ARPHeader::getBufferPointer(){91return (u8*)(&h);92} /* End of getBufferPointer() */939495/** Stores supplied packet in the internal buffer so the information96* can be accessed using the standard get & set methods.97* @warning The ARPHeader class is able to hold a maximum of 28 bytes.98* If the supplied buffer is longer than that, only the first 28 bytes will be99* stored in the internal buffer.100* @warning Supplied len MUST be at least 28 bytes (ARP header length).101* @return OP_SUCCESS on success and OP_FAILURE in case of error */102int ARPHeader::storeRecvData(const u8 *buf, size_t len){103if(buf==NULL || len<ARP_HEADER_LEN){104return OP_FAILURE;105}else{106this->reset(); /* Re-init the object, just in case the caller had used it already */107this->length=ARP_HEADER_LEN;108memcpy(&(this->h), buf, ARP_HEADER_LEN);109}110return OP_SUCCESS;111} /* End of storeRecvData() */112113114/* Returns a protocol identifier. This is used by packet parsing funtions115* that return linked lists of PacketElement objects, to determine the protocol116* the object represents. */117int ARPHeader::protocol_id() const {118return HEADER_TYPE_ARP;119} /* End of protocol_id() */120121122/** Determines if the data stored in the object after an storeRecvData() call123* is valid and safe to use. This mainly checks the length of the data but may124* also test the value of certain protocol fields to ensure their correctness.125* @return the length, in bytes, of the header, if its found to be valid or126* OP_FAILURE (-1) otherwise. */127int ARPHeader::validate(){128if( this->length!=ARP_HEADER_LEN)129return OP_FAILURE;130else131return ARP_HEADER_LEN;132} /* End of validate() */133134135/** Prints the contents of the header and calls print() on the next protocol136* header in the chain (if there is any).137* @return OP_SUCCESS on success and OP_FAILURE in case of error. */138int ARPHeader::print(FILE *output, int detail) const {139fprintf(output, "ARP[]");140if(this->next!=NULL){141print_separator(output, detail);142next->print(output, detail);143}144return OP_SUCCESS;145} /* End of print() */146147148/******************************************************************************/149/* PROTOCOL-SPECIFIC METHODS */150/******************************************************************************/151152/** Sets HardwareType.153* @return OP_SUCCESS on success and OP_FAILURE in case of error. */154int ARPHeader::setHardwareType(u16 val){155this->h.ar_hrd=htons(val);156return OP_SUCCESS;157} /* End of setHardwareType() */158159160/** Sets HardwareType to ETHERNET.161* @return OP_SUCCESS on success and OP_FAILURE in case of error. */162int ARPHeader::setHardwareType(){163this->h.ar_hrd=htons(HDR_ETH10MB);164return OP_SUCCESS;165} /* End of setHardwareType() */166167168/** Returns value of attribute h.ar_hrd */169u16 ARPHeader::getHardwareType(){170return ntohs(this->h.ar_hrd);171} /* End of getHardwareType() */172173174/** Sets ProtocolType.175* @return OP_SUCCESS on success and OP_FAILURE in case of error. */176int ARPHeader::setProtocolType(u16 val){177this->h.ar_pro=htons(val);178return OP_SUCCESS;179} /* End of setProtocolType() */180181182/** Sets ProtocolType.183* @return OP_SUCCESS on success and OP_FAILURE in case of error. */184int ARPHeader::setProtocolType(){185this->h.ar_pro=htons(0x0800); /* DEFAULT: IPv4 */186return OP_SUCCESS;187} /* End of setProtocolType() */188189190/** Returns value of attribute h.ar_pro */191u16 ARPHeader::getProtocolType(){192return ntohs(this->h.ar_pro);193} /* End of getProtocolType() */194195196/** Sets HwAddrLen.197* @return OP_SUCCESS on success and OP_FAILURE in case of error. */198int ARPHeader::setHwAddrLen(u8 val){199this->h.ar_hln=val;200return OP_SUCCESS;201} /* End of setHwAddrLen() */202203204/** Sets HwAddrLen.205* @return OP_SUCCESS on success and OP_FAILURE in case of error. */206int ARPHeader::setHwAddrLen(){207this->h.ar_hln=ETH_ADDRESS_LEN;208return OP_SUCCESS;209} /* End of setHwAddrLen() */210211212/** Returns value of attribute h.ar_hln */213u8 ARPHeader::getHwAddrLen(){214return this->h.ar_hln;215} /* End of getHwAddrLen() */216217218/** Sets ProtoAddrLen.219* @return OP_SUCCESS on success and OP_FAILURE in case of error. */220int ARPHeader::setProtoAddrLen(u8 val){221this->h.ar_pln=val;222return OP_SUCCESS;223} /* End of setProtoAddrLen() */224225226/** Sets ProtoAddrLen.227* @return OP_SUCCESS on success and OP_FAILURE in case of error. */228int ARPHeader::setProtoAddrLen(){229this->h.ar_pln=IPv4_ADDRESS_LEN; /* DEFAULT: IPv4 */230return OP_SUCCESS;231} /* End of setProtoAddrLen() */232233234/** Returns value of attribute h.ar_pln */235u8 ARPHeader::getProtoAddrLen(){236return this->h.ar_pln;237} /* End of getProtoAddrLen() */238239240/** Sets OpCode.241* @return OP_SUCCESS on success and OP_FAILURE in case of error. */242int ARPHeader::setOpCode(u16 val){243this->h.ar_op=htons(val);244return OP_SUCCESS;245} /* End of setOpCode() */246247248/** Returns value of attribute h.ar_op */249u16 ARPHeader::getOpCode(){250return ntohs(this->h.ar_op);251} /* End of getOpCode() */252253254/** Sets SenderMAC.255* @return OP_SUCCESS on success and OP_FAILURE in case of error. */256int ARPHeader::setSenderMAC(const u8 * val){257if(val==NULL)258return OP_FAILURE;259memcpy(this->h.data, val, ETH_ADDRESS_LEN);260return OP_SUCCESS;261} /* End of setSenderMAC() */262263264/** Returns value of attribute h.ar_sha */265u8 * ARPHeader::getSenderMAC(){266return this->h.data;267} /* End of getSenderMAC() */268269270/** Sets SenderIP.271* @return OP_SUCCESS on success and OP_FAILURE in case of error. */272int ARPHeader::setSenderIP(struct in_addr val){273memcpy(this->h.data+6, &val.s_addr, 4);274return OP_SUCCESS;275} /* End of setSenderIP() */276277278/** Returns value of attribute h.ar_sip */279u32 ARPHeader::getSenderIP(){280u32 *p = (u32 *)(this->h.data+6);281return *p;282} /* End of getSenderIP() */283284285/** Sets TargetMAC.286* @return OP_SUCCESS on success and OP_FAILURE in case of error. */287int ARPHeader::setTargetMAC(u8 * val){288if(val==NULL)289return OP_FAILURE;290memcpy(this->h.data+10, val, ETH_ADDRESS_LEN);291return OP_SUCCESS;292} /* End of setTargetMAC() */293294295/** Returns value of attribute h.ar_tha */296u8 * ARPHeader::getTargetMAC(){297return this->h.data+10;298} /* End of getTargetMAC() */299300301/** Sets TargetIP.302* @return OP_SUCCESS on success and OP_FAILURE in case of error. */303int ARPHeader::setTargetIP(struct in_addr val){304memcpy(this->h.data+16, &val.s_addr, 4);305return OP_SUCCESS;306} /* End of setTargetIP() */307308309/** Returns value of attribute h.ar_tip */310u32 ARPHeader::getTargetIP(){311u32 *p = (u32 *)(this->h.data+16);312return *p;313} /* End of getTargetIP() */314315316