Debian Codegraph @

protolib/examples/vifLan.cpp

raw
1 #include "protoApp.h"
2 #include "protoVif.h"
3 #include "protoPktIP.h"
4 #include "protoPktETH.h"
5 #include "protoPktARP.h"
6
7 #include "protoList.h"
8
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <ctype.h> // for "isspace()"
13
14
15 class VifItem : public ProtoList::Item
16 {
17 public:
18 VifItem(ProtoVif* theVif);
19 ~VifItem();
20
21 ProtoVif* GetVif()
22 {return vif;}
23
24 private:
25 ProtoVif* vif;
26 }; // end class VifItem
27
28 class VifList : public ProtoListTemplate<VifItem> {};
29
30
31 VifItem::VifItem(ProtoVif* theVif)
32 : vif(theVif)
33 {
34 }
35
36 VifItem::~VifItem()
37 {
38 if (NULL != vif)
39 {
40 delete vif;
41 vif = NULL;
42 }
43 }
44
45 /**
46 * @class VifLanApp
47 *
48 * @brief Example using protoVif
49 *
50 */
51 class VifLanApp : public ProtoApp
52 {
53 public:
54 VifLanApp();
55 ~VifLanApp();
56
57 bool OnStartup(int argc, const char*const* argv);
58 bool ProcessCommands(int argc, const char*const* argv) {return false;}
59 void OnShutdown();
60
61 private:
62 enum CmdType {CMD_INVALID, CMD_ARG, CMD_NOARG};
63 static const char* const CMD_LIST[];
64 static CmdType GetCmdType(const char* string);
65 bool OnCommand(const char* cmd, const char* val);
66 void Usage();
67
68 void PeekPkt(ProtoPktETH& ethPkt, bool inbound);
69
70 void OnOutboundPkt(ProtoChannel& theChannel,
71 ProtoChannel::Notification notifyType);
72 void OnInboundPkt(ProtoChannel& theChannel,
73 ProtoChannel::Notification notifyType);
74
75 VifList vif_list;
76
77 }; // end class VifLanApp
78
79 void VifLanApp::Usage()
80 {
81 fprintf(stderr, "Usage: vifLan <vifName1> <vifName2> ...\n");
82 }
83
84 const char* const VifLanApp::CMD_LIST[] =
85 {
86 "-help", // print help info an exit
87 "+debug", // <debugLevel>
88 NULL
89 };
90
91 /**
92 * This macro creates our ProtoApp derived application instance
93 */
94 PROTO_INSTANTIATE_APP(VifLanApp)
95
96 VifLanApp::VifLanApp()
97 {
98 }
99
100 VifLanApp::~VifLanApp()
101 {
102 }
103
104 VifLanApp::CmdType VifLanApp::GetCmdType(const char* cmd)
105 {
106 if (!cmd) return CMD_INVALID;
107 unsigned int len = strlen(cmd);
108 bool matched = false;
109 CmdType type = CMD_INVALID;
110 const char* const* nextCmd = CMD_LIST;
111 while (*nextCmd)
112 {
113 if (!strncmp(cmd, *nextCmd+1, len))
114 {
115 if (matched)
116 {
117 // ambiguous command (command should match only once)
118 return CMD_INVALID;
119 }
120 else
121 {
122 matched = true;
123 if ('+' == *nextCmd[0])
124 type = CMD_ARG;
125 else
126 type = CMD_NOARG;
127 }
128 }
129 nextCmd++;
130 }
131 return type;
132 } // end VifLanApp::GetCmdType()
133
134
135 bool VifLanApp::OnStartup(int argc, const char*const* argv)
136 {
137 ProtoAddress invalidAddr;
138 for (int i = 1; i < argc; i++)
139 {
140 // Create virtual interface with given name
141 ProtoVif* vif = ProtoVif::Create();
142 if (NULL == vif)
143 {
144 PLOG(PL_ERROR, "VifLanApp::OnStartup() new ProtoVif error: %s\n", GetErrorString());
145 vif_list.Destroy();
146 return false;
147 }
148 vif->SetNotifier(static_cast<ProtoChannel::Notifier*>(&dispatcher));
149 vif->SetListener(this,&VifLanApp::OnOutboundPkt);
150 // Open virtual interface with no address assigned
151 if (!vif->Open(argv[i], invalidAddr, 0))
152 {
153 PLOG(PL_ERROR,"VifLanApp::OnStartup() ProtoVif::Open() error\n");
154 delete vif;
155 vif_list.Destroy();
156 return false;
157 }
158 VifItem* vifItem = new VifItem(vif);
159 if (NULL == vifItem)
160 {
161 PLOG(PL_ERROR, "VifLanApp::OnStartup() new VifItem error: %s\n", GetErrorString());
162 delete vif;
163 vif_list.Destroy();
164 return false;
165 }
166 vif_list.Append(*vifItem);
167 }
168 return true;
169 } // end VifLanApp::OnStartup()
170
171 void VifLanApp::OnShutdown()
172 {
173 // Closes and deletes all ProtoVifs in vif_list
174 vif_list.Destroy();
175 PLOG(PL_INFO, "vifLan: Done.\n");
176
177 } // end VifLanApp::OnShutdown()
178
179
180 /**
181 * @note We offset the buffer here by 2 bytes since
182 * Ethernet header is 14 bytes
183 * (i.e. not a multiple of 4 (sizeof(UINT32))
184 * This gives us a properly aligned buffer for
185 * 32-bit aligned IP packets
186 */
187 void VifLanApp::OnOutboundPkt(ProtoChannel& theChannel,
188 ProtoChannel::Notification notifyType)
189 {
190 const int BUFFER_MAX = 4096;
191 UINT32 alignedBuffer[BUFFER_MAX/sizeof(UINT32)];
192 // offset by 2-bytes so IP content is 32-bit aligned
193 UINT16* ethBuffer = ((UINT16*)alignedBuffer) + 1;
194 unsigned int numBytes = BUFFER_MAX - 2;
195 ProtoVif& vif = static_cast<ProtoVif&>(theChannel);
196 while (vif.Read((char*)ethBuffer, numBytes))
197 {
198 if (numBytes == 0) break; // no more packets to receive
199
200
201
202
203 ProtoPktETH ethPkt((UINT32*)ethBuffer, BUFFER_MAX - 2);
204 if (!ethPkt.InitFromBuffer(numBytes))
205 {
206 PLOG(PL_ERROR, "VifExampleApp::OnOutboundPkt() error: bad Ether frame\n");
207 continue;
208 }
209 // Uncomment this to get printout of transmitted packets
210 //PeekPkt(ethPkt, false);
211
212 // Iterate through the vif_list and "output" this packet to
213 // the _other_ interfaces on our virtual LAN
214 VifList::Iterator iterator(vif_list);
215 VifItem* nextItem;
216 while (NULL != (nextItem = iterator.GetNextItem()))
217 {
218 ProtoVif* nextVif = nextItem->GetVif();
219 if (&vif == nextVif) continue;
220 if (!nextVif->Write((char*)ethBuffer, numBytes))
221 {
222 PLOG(PL_ERROR, "VifLanApp::OnOutboundPkt() error: error writing packet\n");
223 }
224 }
225 numBytes = BUFFER_MAX;
226 }
227 } // end VifLanApp::OnOutboundPkt
228
229
230 // Method useful for debugging
231 void VifLanApp::PeekPkt(ProtoPktETH& ethPkt, bool inbound)
232 {
233 switch (ethPkt.GetType())
234 {
235 case ProtoPktETH::IP:
236 case ProtoPktETH::IPv6:
237 {
238 unsigned int payloadLength = ethPkt.GetPayloadLength();
239 ProtoPktIP ipPkt;
240 if (!ipPkt.InitFromBuffer(payloadLength, (UINT32*)ethPkt.AccessPayload(), payloadLength))
241 {
242 PLOG(PL_ERROR, "vifLan::PeekPkt() error: bad %sbound IP packet\n",
243 inbound ? "in" : "out");
244 break;
245 }
246 ProtoAddress dstAddr;
247 ProtoAddress srcAddr;
248 switch (ipPkt.GetVersion())
249 {
250 case 4:
251 {
252 ProtoPktIPv4 ip4Pkt(ipPkt);
253 ip4Pkt.GetDstAddr(dstAddr);
254 ip4Pkt.GetSrcAddr(srcAddr);
255 break;
256 }
257 case 6:
258 {
259 ProtoPktIPv6 ip6Pkt(ipPkt);
260 ip6Pkt.GetDstAddr(dstAddr);
261 ip6Pkt.GetSrcAddr(srcAddr);
262 break;
263 }
264 default:
265 {
266 PLOG(PL_ERROR,"VifLanApp::PeekPkt() Error: Invalid IP pkt version.\n");
267 break;
268 }
269 }
270 PLOG(PL_ALWAYS, "VifLanApp::PeekPkt() %sbound packet IP dst>%s ",
271 inbound ? "in" : "out", dstAddr.GetHostString());
272 PLOG(PL_ALWAYS," src>%s length>%d\n", srcAddr.GetHostString(), ipPkt.GetLength());
273 break;
274 }
275 case ProtoPktETH::ARP:
276 {
277 ProtoPktARP arp;
278 if (!arp.InitFromBuffer((UINT32*)ethPkt.AccessPayload(), ethPkt.GetPayloadLength()))
279 {
280 PLOG(PL_ERROR, "VifLanApp::PeekPkt() received bad ARP packet?\n");
281 break;
282 }
283 PLOG(PL_ALWAYS,"VifLanApp::PeekPkt() %sbound ARP ",
284 inbound ? "in" : "out");
285 switch(arp.GetOpcode())
286 {
287 case ProtoPktARP::ARP_REQ:
288 PLOG(PL_ERROR, "request ");
289 break;
290 case ProtoPktARP::ARP_REP:
291 PLOG(PL_ERROR, "reply ");
292 break;
293 default:
294 PLOG(PL_ERROR, "??? ");
295 break;
296 }
297 ProtoAddress addr;
298 arp.GetSenderHardwareAddress(addr);
299 PLOG(PL_ALWAYS, "from eth:%s ", addr.GetHostString());
300 arp.GetSenderProtocolAddress(addr);
301 PLOG(PL_ALWAYS, "ip:%s ", addr.GetHostString());
302 arp.GetTargetProtocolAddress(addr);
303 PLOG(PL_ALWAYS, "for ip:%s ", addr.GetHostString());
304 if (ProtoPktARP::ARP_REP == arp.GetOpcode())
305 {
306
307 arp.GetTargetHardwareAddress(addr);
308 PLOG(PL_ERROR, "eth:%s ", addr.GetHostString());
309 }
310 PLOG(PL_ALWAYS, "\n");
311 break;
312 }
313 default:
314 PLOG(PL_ERROR, "VifLanApp::PeekPkt() unknown %s packet type\n", inbound ? "inbound" : "outbound");
315 break;
316 }
317 } // end VifLanApp::PeekPkt()