Imported Upstream version 878.70.2
[platform/upstream/mdnsresponder.git] / mDNSMacOSX / BonjourTop / source / Frame.cpp
1 //
2 //  Frame.cpp
3 //  TestTB
4 //
5 //  Created by Terrin Eager on 1/19/13.
6 //
7 //
8
9 #include "Frame.h"
10
11 #define EthernetHeaderStart 14
12
13 void Frame::Set(BJ_UINT8* data,BJ_UINT32 len,BJ_UINT64 t)
14 {
15     frameData = data;
16     length = len;
17     frameTime = t;
18 }
19
20 BJ_UINT8* Frame::GetEthernetStart()
21 {
22     //todo Support other media types
23     return frameData;
24 }
25 BJ_UINT8* Frame::GetIPStart()
26 {
27     BJ_UINT8* ether = GetEthernetStart();
28
29     return ether + 14;
30
31 }
32 BJ_UINT8* Frame::GetUDPStart()
33 {
34     BJ_UINT8* ip = GetIPStart();
35
36     BJ_UINT16 nSize = *((__uint16_t*) (ip));
37     BJ_UINT16 nVerison = (nSize&0xf0) >> 4;
38     if (nVerison == 0x4)
39     {
40         m_bCurrentFrameIPversion = 4;
41
42         nSize &= 0x0f;
43         nSize *= 4;
44
45
46         BJ_UINT8 nProtocol = *(ip+9);
47
48         if (nProtocol != 17) // Not UDP
49             return NULL;
50     }
51     else if (nVerison == 0x6)
52     {
53         m_bCurrentFrameIPversion = 6;
54         BJ_UINT8 nProtocol = *(ip+6);
55
56         if (nProtocol != 17) // Not UDP
57             return NULL;
58         nSize = 40;
59
60     }
61
62     return ip+nSize;
63 }
64
65 BJ_UINT8* Frame::GetBonjourStart()
66 {
67     BJ_UINT8* udp = GetUDPStart();
68
69
70     if (udp == NULL)
71         return NULL;
72
73     BJ_UINT16 nSourcePort = *((__uint16_t*)(udp));
74     BJ_UINT16 nDestPort = *((__uint16_t*)(udp+2));
75     BJ_UINT16 nBonjourPort  = htons(5353);
76
77     if (nSourcePort == nBonjourPort && nDestPort == nBonjourPort)
78         return (udp+8);
79     else
80         return NULL;
81
82 }
83
84
85
86 BJIPAddr* Frame::GetSrcIPAddr()
87 {
88     BJ_UINT8* ip = GetIPStart();
89
90     BJ_UINT16 nSize = (__uint16_t) (*ip);
91     BJ_UINT16 nVerison = (nSize&0xf0) >> 4;
92     if (nVerison == 0x4)
93     {
94         m_bCurrentFrameIPversion = 4;
95
96         struct in_addr* ipi_addr;
97
98         ipi_addr = (in_addr*)(ip+12);
99
100         sourceIPAddr.Set(ipi_addr);
101
102     }
103     else if (nVerison == 0x6)
104     {
105         m_bCurrentFrameIPversion = 6;
106         BJ_UINT8* ipi_addr;
107
108         ipi_addr = (ip+8);
109
110         sourceIPAddr.Setv6Raw(ipi_addr);
111
112     }
113
114     return &sourceIPAddr;
115 }
116
117 BJIPAddr* Frame::GetDestIPAddr()
118 {
119     BJ_UINT8* ip = GetIPStart();
120
121     BJ_UINT16 nSize = *((__uint16_t*) (ip));
122     BJ_UINT16 nVerison = (nSize&0xf0) >> 4;
123     if (nVerison == 0x4)
124     {
125         m_bCurrentFrameIPversion = 4;
126
127         struct in_addr* ipi_addr;
128
129         ipi_addr = (in_addr*)(ip+16);
130
131         destIPAddr.Set(ipi_addr);
132
133     }
134     else if (nVerison == 0x6)
135     {
136         m_bCurrentFrameIPversion = 6;
137         struct in6_addr* ipi_addr;
138
139         ipi_addr = (in6_addr*)(ip+24);
140
141         destIPAddr.Set(ipi_addr);
142
143     }
144     return &destIPAddr;
145 }
146
147 BJMACAddr* Frame::GetSrcMACAddr()
148 {
149     sourceMACAddr.Set(GetEthernetStart()+6);
150
151     return &sourceMACAddr;
152 }
153
154 BJMACAddr* Frame::GetDestMACAddr()
155 {
156     destMACAddr.Set(GetEthernetStart());
157
158     return &destMACAddr;
159 }
160
161 void Frame::SetDatalinkType(BJ_DATALINKTYPE datalinkType)
162 {
163     m_datalinkType = datalinkType;
164 }
165
166 BJ_UINT32 Frame::GetLinklayerHeaderLength()
167 {
168     switch (m_datalinkType)
169     {
170         case (BJ_DLT_EN10MB):
171             return EthernetHeaderStart;
172         case (BJ_DLT_IEEE802_11):
173             return Get80211HeaderLength();
174         default:
175             // Default to Ethernet
176             return EthernetHeaderStart;
177     }
178 }
179
180 BJ_UINT32 Frame::Get80211HeaderLength()
181 {
182     // XXX: 802.11 header is tricky since it has no "length" field.
183     // We should look at "FrameControl" and derive the length manually for each frame.
184     BJ_UINT16 * frameControl = (BJ_UINT16*)GetEthernetStart();
185
186     // [SubType] [Type - Ver]
187
188     bool isFrameData = (0x0C & *frameControl) == 0x08;
189     bool isQosData   = ((0xF0 & *frameControl) == 0x80) && isFrameData;
190
191     if (isQosData)
192     {
193         //Standard (24) + QoS (2) + LLC (3) + SNAP (5)
194         return 24 + 2 + 3 + 5;
195     }
196     else
197     {
198         //Standard (24) + LLC (3) + SNAP (5)
199         return 24 + 3 + 5;
200     }
201 }