BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file DHCPIpUdpEncoder.c
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29  
30 #include <limits.h>
31 #include <string.h>
32 #include <stddef.h>
33  
34 #include <misc/ipv4_proto.h>
35 #include <misc/udp_proto.h>
36 #include <misc/byteorder.h>
37  
38 #include <dhcpclient/DHCPIpUdpEncoder.h>
39  
40 #define DHCP_SERVER_PORT 67
41 #define DHCP_CLIENT_PORT 68
42  
43 #define IPUDP_HEADER_SIZE (sizeof(struct ipv4_header) + sizeof(struct udp_header))
44  
45 static void output_handler_recv (DHCPIpUdpEncoder *o, uint8_t *data)
46 {
47 DebugObject_Access(&o->d_obj);
48  
49 // remember output packet
50 o->data = data;
51  
52 // receive payload
53 PacketRecvInterface_Receiver_Recv(o->input, o->data + IPUDP_HEADER_SIZE);
54 }
55  
56 static void input_handler_done (DHCPIpUdpEncoder *o, int data_len)
57 {
58 DebugObject_Access(&o->d_obj);
59  
60 // build IP header
61 struct ipv4_header iph;
62 iph.version4_ihl4 = IPV4_MAKE_VERSION_IHL(sizeof(iph));
63 iph.ds = hton8(0);
64 iph.total_length = hton16(IPUDP_HEADER_SIZE + data_len);
65 iph.identification = hton16(0);
66 iph.flags3_fragmentoffset13 = hton16(0);
67 iph.ttl = hton8(64);
68 iph.protocol = hton8(IPV4_PROTOCOL_UDP);
69 iph.checksum = hton16(0);
70 iph.source_address = hton32(0x00000000);
71 iph.destination_address = hton32(0xFFFFFFFF);
72 iph.checksum = ipv4_checksum(&iph, NULL, 0);
73  
74 // write UDP header
75 struct udp_header udph;
76 udph.source_port = hton16(DHCP_CLIENT_PORT);
77 udph.dest_port = hton16(DHCP_SERVER_PORT);
78 udph.length = hton16(sizeof(udph) + data_len);
79 udph.checksum = hton16(0);
80 udph.checksum = udp_checksum(&udph, o->data + IPUDP_HEADER_SIZE, data_len, iph.source_address, iph.destination_address);
81  
82 // write header
83 memcpy(o->data, &iph, sizeof(iph));
84 memcpy(o->data + sizeof(iph), &udph, sizeof(udph));
85  
86 // finish packet
87 PacketRecvInterface_Done(&o->output, IPUDP_HEADER_SIZE + data_len);
88 }
89  
90 void DHCPIpUdpEncoder_Init (DHCPIpUdpEncoder *o, PacketRecvInterface *input, BPendingGroup *pg)
91 {
92 ASSERT(PacketRecvInterface_GetMTU(input) <= INT_MAX - IPUDP_HEADER_SIZE)
93  
94 // init arguments
95 o->input = input;
96  
97 // init input
98 PacketRecvInterface_Receiver_Init(o->input, (PacketRecvInterface_handler_done)input_handler_done, o);
99  
100 // init output
101 PacketRecvInterface_Init(&o->output, IPUDP_HEADER_SIZE + PacketRecvInterface_GetMTU(o->input), (PacketRecvInterface_handler_recv)output_handler_recv, o, pg);
102  
103 DebugObject_Init(&o->d_obj);
104 }
105  
106 void DHCPIpUdpEncoder_Free (DHCPIpUdpEncoder *o)
107 {
108 DebugObject_Free(&o->d_obj);
109  
110 // free output
111 PacketRecvInterface_Free(&o->output);
112 }
113  
114 PacketRecvInterface * DHCPIpUdpEncoder_GetOutput (DHCPIpUdpEncoder *o)
115 {
116 DebugObject_Access(&o->d_obj);
117  
118 return &o->output;
119 }