Пример #1
0
        byte[] GetBytes(IpHeader p)
        {
            IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IpHeader)));

            Marshal.StructureToPtr(p, ptr, false);
            byte[] ih = new byte[Marshal.SizeOf(typeof(IpHeader))];
            Marshal.Copy(ptr, ih, 0, Marshal.SizeOf(typeof(IpHeader)));
            Marshal.FreeHGlobal(ptr);
            return(ih);
        }
Пример #2
0
        public RecvPacket(byte [] b)
        {
            etherHeader = new EtherHeader();
            ipHeader = new IpHeader();
            tcpHeader = new TcpHeader();
            arpHeader = new ArpHeader();

            Mac = new List<byte[]> { null,null};
            Ip = new List<byte[]> { null, null };
            Port = new List<ushort> { 0, 0 };
            for (int i = 0; i < 2; i++) {
                Mac[i] = new byte[] { 0, 0, 0, 0, 0, 0 };
                Ip[i] = new byte[] { 0, 0, 0, 0 };
            }

            this.Buf = new byte[b.Length];
            Buffer.BlockCopy(b,0,Buf,0,b.Length);

            unsafe {
                int offSet = 0;
                fixed (byte* p = b) {
                    // Etherヘッダ処理
                    int etherHeaderSize = Marshal.SizeOf(etherHeader);
                    if (offSet + etherHeaderSize > b.Length) //  受信バイト数超過
                        return;
                    etherHeader = (EtherHeader)Marshal.PtrToStructure((IntPtr)(p + offSet), typeof(EtherHeader));
                    offSet += etherHeaderSize;

                    // Ether dst mac
                    Buffer.BlockCopy(etherHeader.dstMac, 0, Mac[(int)Sd.Dst], 0, 6);
                    Buffer.BlockCopy(etherHeader.srcMac, 0, Mac[(int)Sd.Src], 0, 6);

                    // Ether type
                    Type = (PType)Util.htons(etherHeader.type);

                    if (Type == PType.IP) {//IP

                        // IPヘッダ処理
                        int ipHeaderSize = Marshal.SizeOf(ipHeader);

                        if (offSet + ipHeaderSize > b.Length) //  受信バイト数超過
                            return;
                        ipHeader = (IpHeader)Marshal.PtrToStructure((IntPtr)(p + offSet), typeof(IpHeader));
                        var ipHeaderLen  = (ipHeader.verLen & 0x0F) * 4;
                        offSet += ipHeaderLen;

                        Len = Util.htons(ipHeader.totalLen);
                        Len -= (ushort)ipHeaderLen;

                        // プロトコル
                        Protocol = (Protocol)ipHeader.protocol;

                        // 送信元 IPアドレス
                        Buffer.BlockCopy(ipHeader.srcIp, 0, Ip[(int)Sd.Src], 0, 4);
                        // 送信先 IPアドレス
                        Buffer.BlockCopy(ipHeader.dstIp, 0, Ip[(int)Sd.Dst], 0, 4);

                        if (ipHeader.protocol == 6) { // TCP

                            // TCPヘッダ処理
                            int tcpHeaderSize = Marshal.SizeOf(tcpHeader);

                            if (offSet + tcpHeaderSize > b.Length) //  受信バイト数超過
                                return;
                            tcpHeader = (TcpHeader)Marshal.PtrToStructure((IntPtr)(p + offSet), typeof(TcpHeader));

                            var l = tcpHeader.offset >> 4;
                            var hedderLen = l*4;
                            Len -= (ushort)hedderLen;

                            if (Len != 0) {
                                Data = new byte[Len];
                                for (int i = 0; i < Len; i++) {
                                    Data[i] = (byte)*(p + offSet + hedderLen + i);
                                }
                            }

                            // 送信元ポート
                            Port[(int)Sd.Src] = Util.htons(tcpHeader.srcPort);
                            // 送信先ポート
                            Port[(int)Sd.Dst] = Util.htons(tcpHeader.dstPort);

                            Squence = Util.htons(tcpHeader.squence);
                            Ack = Util.htons(tcpHeader.ack);

                            Flg = tcpHeader.flg;
                        }
                    } else if (Type == PType.ARP) { //ARP
                        // Arpヘッダ処理
                        int arpHeaderSize = Marshal.SizeOf(arpHeader);
                        if (offSet + etherHeaderSize > b.Length) //  受信バイト数超過
                            return;
                        arpHeader = (ArpHeader)Marshal.PtrToStructure((IntPtr)(p + offSet), typeof(ArpHeader));
                        return;
                    } else {
                        return; // 未対応
                    }
                }
            }
        }
Пример #3
0
        public SendPacket(Log log, RecvPacket recvPacket, short ident, uint squence, uint ack, byte flg, byte [] data)
        {
            _log = log;

            var dataLen        = data.Length;
            var etherHeaderLen = 14;
            var ipHeaderLen    = 20;
            var tcpHeaderLen   = 20;

            Buf = new byte[etherHeaderLen + ipHeaderLen + tcpHeaderLen + dataLen];

            EtherHeader etherHeader = new EtherHeader();

            etherHeader.dstMac = new byte[] { 0, 0, 0, 0, 0, 0 };
            etherHeader.srcMac = new byte[] { 0, 0, 0, 0, 0, 0 };
            Buffer.BlockCopy(recvPacket.etherHeader.srcMac, 0, etherHeader.dstMac, 0, 6);
            Buffer.BlockCopy(recvPacket.etherHeader.dstMac, 0, etherHeader.srcMac, 0, 6);
            etherHeader.type = recvPacket.etherHeader.type;

            IpHeader ipHeader = new IpHeader();

            ipHeader.srcIp = new byte[] { 0, 0, 0, 0 };
            ipHeader.dstIp = new byte[] { 0, 0, 0, 0 };
            Buffer.BlockCopy(recvPacket.ipHeader.srcIp, 0, ipHeader.dstIp, 0, 4);
            Buffer.BlockCopy(recvPacket.ipHeader.dstIp, 0, ipHeader.srcIp, 0, 4);
            ipHeader.verLen   = recvPacket.ipHeader.verLen;
            ipHeader.flags    = recvPacket.ipHeader.flags;
            ipHeader.ident    = ident;
            ipHeader.protocol = recvPacket.ipHeader.protocol;
            ipHeader.ttl      = recvPacket.ipHeader.ttl;
            ipHeader.totalLen = Util.htons((ushort)(ipHeaderLen + tcpHeaderLen + dataLen));//IP/TCPヘッダのみ

            TcpHeader tcpHeader = new TcpHeader();

            tcpHeader.urgent  = recvPacket.tcpHeader.urgent;
            tcpHeader.window  = 0x0020;
            tcpHeader.srcPort = recvPacket.tcpHeader.dstPort;
            tcpHeader.dstPort = recvPacket.tcpHeader.srcPort;
            tcpHeader.ack     = Util.htons(ack);
            tcpHeader.squence = Util.htons(squence);
            tcpHeader.offset  = 0x50;// TcpHeaderLen=20 byte
            tcpHeader.flg     = flg;

            //チェックサム計算方法
            //http://ja.wikipedia.org/wiki/Transmission_Control_Protocol


            //IPチェックサム
            var b = new byte[ipHeaderLen];

            Buffer.BlockCopy(GetBytes(ipHeader), 0, b, 0, ipHeaderLen);
            ipHeader.checkSum = (short)Util.htons(CreateChecksum(b, 0, ipHeaderLen));


            //TCPチェックサム
            //擬似ヘッダ + TcpHeader + TCPデータ
            b = new byte[12 + tcpHeaderLen + dataLen];//擬似ヘッダ+TcpHeader
            Buffer.BlockCopy(GetBytes(tcpHeader), 0, b, 12, tcpHeaderLen);
            Buffer.BlockCopy(data, 0, b, 32, dataLen);

            //擬似ヘッダ編集
            Buffer.BlockCopy(recvPacket.ipHeader.srcIp, 0, b, 0, 4);
            Buffer.BlockCopy(recvPacket.ipHeader.dstIp, 0, b, 4, 4);
            b[9] = 6;//TCP
            int size = tcpHeaderLen + dataLen;

            b[10] = (byte)((size & 0xFF00) >> 8);
            b[11] = (byte)(size & 0x00FF);

            tcpHeader.checkSum = (short)Util.htons(CreateChecksum(b, 0, b.Length));

            unsafe
            {
                fixed(byte *p = Buf)
                {
                    var offSet = 0;

                    Marshal.StructureToPtr(etherHeader, new IntPtr(p + offSet), true);
                    offSet += 14;
                    Marshal.StructureToPtr(ipHeader, new IntPtr(p + offSet), true);
                    offSet += 20;
                    Marshal.StructureToPtr(tcpHeader, new IntPtr(p + offSet), true);
                }

                if (dataLen > 0)
                {
                    Buffer.BlockCopy(data, 0, Buf, 54, dataLen);
                }
            }
        }
Пример #4
0
        public SendPacket(Log log,RecvPacket recvPacket,short ident,uint squence,uint ack,byte flg,byte [] data)
        {
            _log = log;

            var dataLen = data.Length;
            var etherHeaderLen = 14;
            var ipHeaderLen = 20;
            var tcpHeaderLen = 20;

            Buf = new byte[etherHeaderLen + ipHeaderLen + tcpHeaderLen + dataLen];

            EtherHeader etherHeader = new EtherHeader();
            etherHeader.dstMac = new byte[] { 0, 0, 0, 0, 0, 0 };
            etherHeader.srcMac = new byte[] { 0, 0, 0, 0, 0, 0 };
            Buffer.BlockCopy(recvPacket.etherHeader.srcMac,0,etherHeader.dstMac,0,6);
            Buffer.BlockCopy(recvPacket.etherHeader.dstMac,0,etherHeader.srcMac,0,6);
            etherHeader.type = recvPacket.etherHeader.type;

            IpHeader ipHeader = new IpHeader();
            ipHeader.srcIp = new byte[] { 0, 0, 0, 0 };
            ipHeader.dstIp = new byte[] { 0, 0, 0, 0 };
            Buffer.BlockCopy(recvPacket.ipHeader.srcIp, 0, ipHeader.dstIp, 0, 4);
            Buffer.BlockCopy(recvPacket.ipHeader.dstIp, 0, ipHeader.srcIp, 0, 4);
            ipHeader.verLen = recvPacket.ipHeader.verLen;
            ipHeader.flags = recvPacket.ipHeader.flags;
            ipHeader.ident = ident;
            ipHeader.protocol = recvPacket.ipHeader.protocol;
            ipHeader.ttl = recvPacket.ipHeader.ttl;
            ipHeader.totalLen = Util.htons((ushort)(ipHeaderLen + tcpHeaderLen + dataLen));//IP/TCPヘッダのみ

            TcpHeader tcpHeader = new TcpHeader();
            tcpHeader.urgent = recvPacket.tcpHeader.urgent;
            tcpHeader.window = 0x0020;
            tcpHeader.srcPort = recvPacket.tcpHeader.dstPort;
            tcpHeader.dstPort = recvPacket.tcpHeader.srcPort;
            tcpHeader.ack =  Util.htons(ack);
            tcpHeader.squence = Util.htons(squence);
            tcpHeader.offset = 0x50;// TcpHeaderLen=20 byte
            tcpHeader.flg = flg;

            //チェックサム計算方法
            //http://ja.wikipedia.org/wiki/Transmission_Control_Protocol

            //IPチェックサム
            var b = new byte[ipHeaderLen];
            Buffer.BlockCopy(GetBytes(ipHeader), 0, b, 0, ipHeaderLen);
            ipHeader.checkSum = (short)Util.htons(CreateChecksum(b, 0, ipHeaderLen));

            //TCPチェックサム
            //擬似ヘッダ + TcpHeader + TCPデータ
            b = new byte[12 + tcpHeaderLen + dataLen];//擬似ヘッダ+TcpHeader
            Buffer.BlockCopy(GetBytes(tcpHeader), 0, b, 12, tcpHeaderLen);
            Buffer.BlockCopy(data, 0, b, 32, dataLen);

            //擬似ヘッダ編集
            Buffer.BlockCopy(recvPacket.ipHeader.srcIp, 0, b, 0, 4);
            Buffer.BlockCopy(recvPacket.ipHeader.dstIp, 0, b, 4, 4);
            b[9]=6;//TCP
            int size = tcpHeaderLen + dataLen;
            b[10] = (byte)((size & 0xFF00) >> 8);
            b[11] = (byte)(size & 0x00FF);

            tcpHeader.checkSum = (short)Util.htons(CreateChecksum(b, 0, b.Length));

            unsafe {
                fixed (byte* p = Buf) {
                    var offSet=0;
                    Marshal.StructureToPtr(etherHeader, new IntPtr(p + offSet), true);
                    offSet += 14;
                    Marshal.StructureToPtr(ipHeader, new IntPtr(p + offSet), true);
                    offSet += 20;
                    Marshal.StructureToPtr(tcpHeader, new IntPtr(p + offSet), true);
                }
                if(dataLen>0)
                    Buffer.BlockCopy(data, 0,Buf, 54, dataLen);
            }
        }
Пример #5
0
 byte[] GetBytes(IpHeader p)
 {
     IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IpHeader)));
     Marshal.StructureToPtr(p, ptr, false);
     byte[] ih = new byte[Marshal.SizeOf(typeof(IpHeader))];
     Marshal.Copy(ptr, ih, 0, Marshal.SizeOf(typeof(IpHeader)));
     Marshal.FreeHGlobal(ptr);
     return ih;
 }
Пример #6
0
        public RecvPacket(byte [] b)
        {
            etherHeader = new EtherHeader();
            ipHeader    = new IpHeader();
            tcpHeader   = new TcpHeader();
            arpHeader   = new ArpHeader();

            Mac = new List <byte[]> {
                null, null
            };
            Ip = new List <byte[]> {
                null, null
            };
            Port = new List <ushort> {
                0, 0
            };
            for (int i = 0; i < 2; i++)
            {
                Mac[i] = new byte[] { 0, 0, 0, 0, 0, 0 };
                Ip[i]  = new byte[] { 0, 0, 0, 0 };
            }

            this.Buf = new byte[b.Length];
            Buffer.BlockCopy(b, 0, Buf, 0, b.Length);

            unsafe {
                int offSet = 0;
                fixed(byte *p = b)
                {
                    // Etherヘッダ処理
                    int etherHeaderSize = Marshal.SizeOf(etherHeader);

                    if (offSet + etherHeaderSize > b.Length) //  受信バイト数超過
                    {
                        return;
                    }
                    etherHeader = (EtherHeader)Marshal.PtrToStructure((IntPtr)(p + offSet), typeof(EtherHeader));
                    offSet     += etherHeaderSize;

                    // Ether dst mac
                    Buffer.BlockCopy(etherHeader.dstMac, 0, Mac[(int)Sd.Dst], 0, 6);
                    Buffer.BlockCopy(etherHeader.srcMac, 0, Mac[(int)Sd.Src], 0, 6);

                    // Ether type
                    Type = (PType)Util.htons(etherHeader.type);

                    if (Type == PType.IP)  //IP

                    // IPヘッダ処理
                    {
                        int ipHeaderSize = Marshal.SizeOf(ipHeader);

                        if (offSet + ipHeaderSize > b.Length) //  受信バイト数超過
                        {
                            return;
                        }
                        ipHeader = (IpHeader)Marshal.PtrToStructure((IntPtr)(p + offSet), typeof(IpHeader));
                        var ipHeaderLen = (ipHeader.verLen & 0x0F) * 4;
                        offSet += ipHeaderLen;

                        Len  = Util.htons(ipHeader.totalLen);
                        Len -= (ushort)ipHeaderLen;

                        // プロトコル
                        Protocol = (Protocol)ipHeader.protocol;

                        // 送信元 IPアドレス
                        Buffer.BlockCopy(ipHeader.srcIp, 0, Ip[(int)Sd.Src], 0, 4);
                        // 送信先 IPアドレス
                        Buffer.BlockCopy(ipHeader.dstIp, 0, Ip[(int)Sd.Dst], 0, 4);

                        if (ipHeader.protocol == 6)   // TCP

                        // TCPヘッダ処理
                        {
                            int tcpHeaderSize = Marshal.SizeOf(tcpHeader);

                            if (offSet + tcpHeaderSize > b.Length) //  受信バイト数超過
                            {
                                return;
                            }
                            tcpHeader = (TcpHeader)Marshal.PtrToStructure((IntPtr)(p + offSet), typeof(TcpHeader));

                            var l         = tcpHeader.offset >> 4;
                            var hedderLen = l * 4;
                            Len -= (ushort)hedderLen;

                            if (Len != 0)
                            {
                                Data = new byte[Len];
                                for (int i = 0; i < Len; i++)
                                {
                                    Data[i] = (byte)*(p + offSet + hedderLen + i);
                                }
                            }

                            // 送信元ポート
                            Port[(int)Sd.Src] = Util.htons(tcpHeader.srcPort);
                            // 送信先ポート
                            Port[(int)Sd.Dst] = Util.htons(tcpHeader.dstPort);

                            Squence = Util.htons(tcpHeader.squence);
                            Ack     = Util.htons(tcpHeader.ack);

                            Flg = tcpHeader.flg;
                        }
                    }
                    else if (Type == PType.ARP)     //ARP
                    // Arpヘッダ処理
                    {
                        int arpHeaderSize = Marshal.SizeOf(arpHeader);
                        if (offSet + etherHeaderSize > b.Length) //  受信バイト数超過
                        {
                            return;
                        }
                        arpHeader = (ArpHeader)Marshal.PtrToStructure((IntPtr)(p + offSet), typeof(ArpHeader));
                        return;
                    }
                    else
                    {
                        return; // 未対応
                    }
                }
            }
        }