Example #1
0
            void DumpUDPPacket(UDPHeader pkt) {
                Console.WriteLine("-- UDP Packet Dump --");
                int num_cols = 10;

                for (int pos=0; pos < pkt.payloadLength;pos += num_cols) {                
                    // write hex values
                    for (int col = 0; col < num_cols; col++) {                
                        if (pos+col < pkt.payloadLength) {
                            Console.Write("{0:X2} ",pkt.Data[pos+col]);
                        } else {
                            Console.Write("   ");
                        }
                    }
                    // write ASCII values
                    Console.Write("    ");
                    for (int col = 0; col < num_cols; col++) {      
                        if (pos+col < pkt.payloadLength) {
                            var val = pkt.Data[pos+col];
                            if (val > 31 && val < 127) {
                                Console.Write("{0} ", (char)val);
                            } else {
                                Console.Write(". ");
                            }
                        } else {
                            Console.Write("  ");
                        }
                    }
                    Console.WriteLine("");                    
                }

                
            }
Example #2
0
            private void ParseData(byte[] byteData, int nReceived)
            {
                              
                //Since all protocol packets are encapsulated in the IP datagram
                //so we start by parsing the IP header and see what protocol data
                //is being carried by it
                IPHeader ipHeader = new IPHeader(byteData, nReceived);
           
                // TODO: make this packet capture + event delivery configurable instead of hardcoded

                //Now according to the protocol being carried by the IP datagram we parse 
                //the data field of the datagram
                switch (ipHeader.ProtocolType)
                {
                    case Protocol.TCP:
                        break; // skip TCP packets.

                    case Protocol.UDP:
                        //  IPHeader.Data stores the data being carried by the IP datagram
                        UDPHeader udpHeader = 
                            new UDPHeader(ipHeader.Data,  (int)ipHeader.MessageLength);

                        

                        var ports = new HashSet<string> { "5055", "5056" };
                        
                        if (ports.Contains(udpHeader.DestinationPort) || ports.Contains(udpHeader.SourcePort))
                        {
                            // Console.WriteLine("Albion packet received .. size = " + udpHeader.payloadLength.ToString());
                            // DumpRawPacket(byteData, nReceived);
                            DumpUDPPacket(udpHeader);

                            //  Albion Photon Data       
                            PacketEvent_Info?.Invoke(String.Format("--  Albion UDP Packet, size={0}", ipHeader.MessageLength));
                            PacketEvent_UDP?.Invoke(udpHeader);
                        } else if (udpHeader.DestinationPort == "53" || udpHeader.SourcePort == "53") {
                            //  If the port is equal to 53 then the underlying protocol is DNS
                            //  Note: DNS can use either TCP or UDP thats why the check is done twice               
                            // PacketEvent_Info?.Invoke(String.Format("UDP DNS Packet, size={0}", udpHeader.payloadLength));
                        }                        
                        break;

                    case Protocol.Unknown:
                        break;
                }
            
            }