A good place to start research on packets is at:<br><a href="http://www.networksorcery.com/enp/default1002.htm">http://www.networksorcery.com/enp/default1002.htm</a><br><br>Long story short on all this, start processing the packet from the beginning and shove little pieces into headers as you go.&nbsp; Determining if you have got it in the right place by verifying the data.<br>
<br>I&#39;m sure there are some ready made chunks of code out there for you on some of what you are looking for, but if you start from scratch you need to build headers for each of the protocols you want to play with and start plugging away.&nbsp; Code snippets below are from pascal, convert as needed for c, c++, c#, etc.<br>
<br>So to answer question #1<br>So first you need to determine if it is Ethernet II or if it is Ethernet 802.3.&nbsp; Not sure the best way to do this, but as I&#39;m looking at some code I&#39;ve done in the past I check to see if eth_proto is &lt;=$05DC<br>
<br>Where this is what I had for headers:<br>&nbsp;EthernetII_Header = record<br>&nbsp;&nbsp; eth_dstmac&nbsp;&nbsp;&nbsp; : array[0..5] of Byte;<br>&nbsp;&nbsp; eth_srcmac&nbsp;&nbsp;&nbsp; : array[0..5] of Byte;<br>&nbsp;&nbsp; eth_proto&nbsp;&nbsp;&nbsp;&nbsp; : Word;<br>&nbsp;end;<br><br>&nbsp;Ethernet8023_Header = record<br>
&nbsp;&nbsp; eth_dstmac&nbsp;&nbsp;&nbsp; : array[0..5] of Byte;<br>&nbsp;&nbsp; eth_srcmac&nbsp;&nbsp;&nbsp; : array[0..5] of Byte;<br>&nbsp;&nbsp; eth_len&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Word;<br>&nbsp;end;<br><br>6 bytes for dest mac address, 6 bytes for source mac address, and 2 bytes for what is either a protocol or a length, depending on what type of packet it is going to be.<br>
<br>Assuming it was an IP packet (Ethernet II) instead of an IPX packet (802.3) then you&#39;d start looking at what value is in eth_proto (byte 13 and 14).&nbsp; If that value in byte 13 and 14 is less than or equal to $05DC, then it is Ethernet 802.3.<br>
<br>Table here:<br><a href="http://www.iana.org/assignments/protocol-numbers/">http://www.iana.org/assignments/protocol-numbers/</a><br><br>(you also need to check to see if it is a vlan, and if it is then do other things, but we&#39;ll assume it is not a vlan packet for now).<br>
<br>So assume byte 13 and 14 is 0800, then we have an IP packet.<br>Other values:<br>0806 = arp<br>872d = Cisco Wireless ALan context Control Protocol <br>etc<br><br>Question #2:<br>Anyway, 0800, IP packet, next determine if it is TCP or UDP, for this you&#39;d look at the ip_protocol section in the IP_header after you&#39;ve shoved the next 20 bytes into this:<br>
<br>&nbsp;IP_Header = record<br>&nbsp;&nbsp; ip_verlen&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Byte;<br>&nbsp;&nbsp; ip_tos&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Byte;<br>&nbsp;&nbsp; ip_totallength&nbsp; : Word;<br>&nbsp;&nbsp; ip_id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Word;<br>&nbsp;&nbsp; ip_offset&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Word;<br>&nbsp;&nbsp; ip_ttl&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Byte;<br>&nbsp;&nbsp; ip_protocol&nbsp;&nbsp;&nbsp;&nbsp; : Byte;<br>
&nbsp;&nbsp; ip_checksum&nbsp;&nbsp;&nbsp;&nbsp; : Word;<br>&nbsp;&nbsp; ip_srcaddr&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : array[0..3] of Byte;<br>&nbsp;&nbsp; ip_dstaddr&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : array[0..3] of Byte;<br>&nbsp;end;<br><br>IP may not always be 20 bytes in length since it can have IP Options seen as optional in this pic:<br>

<a href="http://www.networksorcery.com/enp/protocol/ip.htm">http://www.networksorcery.com/enp/protocol/ip.htm</a><br><br>So this is something you need to determine based on processing ip_verlen.&nbsp; This field gives you both the version of IP and the length of the header as I recall (sorry been a long time since I worked on this chunk of code)<br>
<br>Question #3 sorta:<br>Now that we have ip_protocol, we need to look at its value<br>0x11 = UDP<br>
0x59 = OSPF IGP<br>
0x06 = tcp<br>
<br>You can look up others here:<br><a href="http://www.networksorcery.com/enp/protocol/ip.htm#Protocol">http://www.networksorcery.com/enp/protocol/ip.htm#Protocol</a><br><br>Question #4:<br>Depends on the type of packet you were working with:<br>
Telnet/FTP, get to the data section after you&#39;ve parsed through EthernetII, IP, TCP and start looking for a key to go off of<br><br>Ultimately look up the protocol you are interested in and parse little by little. <br>
<br>There is an open source project out there at sourceforge, in C# that you may be able to look at and get some ideas.&nbsp; Haven&#39;t ever looked specifically at its code, but it pulls some creds from different protocols:<br>
<br><a href="http://sourceforge.net/projects/networkminer/">http://sourceforge.net/projects/networkminer/</a><br><br>Eric<br><br><br>