1 
2 /***************************************************************************
3  * UDPHeader.h -- The UDPHeader Class represents a UDP packet. It contains *
4  * methods to set the different header fields. These methods tipically     *
5  * perform the necessary error checks and byte order conversions.          *
6  *                                                                         *
7  ***********************IMPORTANT NMAP LICENSE TERMS************************
8  *                                                                         *
9  * The Nmap Security Scanner is (C) 1996-2020 Insecure.Com 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 header  *
19  * summarizes some key points from the Nmap license, but is no substitute  *
20  * 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.org) 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              *
38  * redistributed without special permission (such as an Nmap OEM           *
39  * license).                                                               *
40  *                                                                         *
41  * Source is provided to this software because we believe users have a     *
42  * right to know exactly what a program is going to do before they run it. *
43  * This also allows you to audit the software for security holes.          *
44  *                                                                         *
45  * Source code also allows you to port Nmap to new platforms, fix bugs,    *
46  * and add new features.  You are highly encouraged to submit your         *
47  * changes as a Github PR or by email to the dev@nmap.org mailing list     *
48  * for possible incorporation into the main distribution. Unless you       *
49  * specify otherwise, it is understood that you are offering us very       *
50  * broad rights to use your submissions as described in the Nmap Public    *
51  * Source License Contributor Agreement. This is important because we      *
52  * fund the project by selling licenses with various terms, and also       *
53  * because the inability to relicense code has caused devastating          *
54  * problems for other Free Software projects (such as KDE and NASM).       *
55  *                                                                         *
56  * The free version of Nmap is distributed in the hope that it will be     *
57  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of  *
58  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Warranties,        *
59  * indemnification and commercial support are all available through the    *
60  * Npcap OEM program--see https://nmap.org/oem.                            *
61  *                                                                         *
62  ***************************************************************************/
63 /* This code was originally part of the Nping tool.                        */
64 
65 #ifndef UDPHEADER_H
66 #define UDPHEADER_H 1
67 
68 #include "TransportLayerElement.h"
69 
70 #define UDP_HEADER_LEN 8
71 
72 /* Default header values */
73 #define UDP_DEFAULT_SPORT 53
74 #define UDP_DEFAULT_DPORT 53
75 
76 class UDPHeader : public TransportLayerElement {
77 
78     private:
79         /*
80         0                   1                   2                   3
81         0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
82         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
83         |          Source Port          |       Destination Port        |
84         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
85         |            Length             |           Checksum            |
86         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
87         */
88         struct nping_udp_hdr{
89           u16 uh_sport;
90           u16 uh_dport;
91           u16 uh_ulen;
92           u16 uh_sum;
93         }__attribute__((__packed__));
94 
95         typedef struct nping_udp_hdr nping_udp_hdr_t;
96 
97         nping_udp_hdr_t h;
98 
99     public:
100 
101         UDPHeader();
102         ~UDPHeader();
103         void reset();
104         u8 *getBufferPointer();
105         int storeRecvData(const u8 *buf, size_t len);
106         int protocol_id() const;
107         int validate();
108         int print(FILE *output, int detail) const;
109 
110         int setSourcePort(u16 p);
111         u16 getSourcePort() const;
112 
113         int setDestinationPort(u16 p);
114         u16 getDestinationPort() const;
115 
116         int setTotalLength();
117         int setTotalLength(u16 l);
118         u16 getTotalLength() const;
119 
120         int setSum(struct in_addr source, struct in_addr destination);
121         int setSum(u16 s);
122         int setSum();
123         int setSumRandom();
124         int setSumRandom(struct in_addr source, struct in_addr destination);
125         u16 getSum() const;
126 
127 }; /* End of class UDPHeader */
128 
129 
130 #endif
131