Beispiel #1
0
        public void Set(RecvPacket p)
        {
            if (_listView.InvokeRequired) {// 別スレッドから呼び出された場合
                _listView.BeginInvoke(new MethodInvoker(() => Set(p)));
            } else {
                var item = _listView.Items.Add(Util.Mac2Str(p.Mac[(int)Sd.Dst]));
                item.SubItems.Add(Util.Mac2Str(p.Mac[(int)Sd.Src]));
                item.SubItems.Add(p.Type.ToString());
                item.SubItems.Add(p.Protocol.ToString());
                item.SubItems.Add(Util.Ip2Str(p.Ip[(int)Sd.Src]));
                item.SubItems.Add(Util.Ip2Str(p.Ip[(int)Sd.Dst]));
                item.SubItems.Add(p.Port[(int)Sd.Src].ToString());
                item.SubItems.Add(p.Port[(int)Sd.Dst].ToString());
                item.SubItems.Add(p.Len.ToString());
                item.SubItems.Add(p.Squence.ToString());
                item.SubItems.Add(p.Ack.ToString());
                item.SubItems.Add(Util.Flg2Str(p.Flg));
                item.Tag = Util.Mac2Str(p.Mac[(int)Sd.Dst]);

                //自動スクロール
                var rect = _listView.ClientRectangle;//ListViewの高さ取得
                var bounds = _listView.Items[0].Bounds;//1行の高さ取得
                var row = rect.Height / bounds.Height;//表示されている行数取得
                var top = _listView.Items.Count - row;//1行目のItemIndex
                if (_listView.TopItem.Index == top) {
                    //最下行が表示されているので、自動スクロールする
                    _listView.EnsureVisible(_listView.Items.Count - 1);
                }
            }
        }
Beispiel #2
0
        public void Set(RecvPacket p)
        {
            if (_listView.InvokeRequired)  // 別スレッドから呼び出された場合
            {
                _listView.BeginInvoke(new MethodInvoker(() => Set(p)));
            }
            else
            {
                var item = _listView.Items.Add(Util.Mac2Str(p.Mac[(int)Sd.Dst]));
                item.SubItems.Add(Util.Mac2Str(p.Mac[(int)Sd.Src]));
                item.SubItems.Add(p.Type.ToString());
                item.SubItems.Add(p.Protocol.ToString());
                item.SubItems.Add(Util.Ip2Str(p.Ip[(int)Sd.Src]));
                item.SubItems.Add(Util.Ip2Str(p.Ip[(int)Sd.Dst]));
                item.SubItems.Add(p.Port[(int)Sd.Src].ToString());
                item.SubItems.Add(p.Port[(int)Sd.Dst].ToString());
                item.SubItems.Add(p.Len.ToString());
                item.SubItems.Add(p.Squence.ToString());
                item.SubItems.Add(p.Ack.ToString());
                item.SubItems.Add(Util.Flg2Str(p.Flg));
                item.Tag = Util.Mac2Str(p.Mac[(int)Sd.Dst]);

                //自動スクロール
                var rect   = _listView.ClientRectangle;   //ListViewの高さ取得
                var bounds = _listView.Items[0].Bounds;   //1行の高さ取得
                var row    = rect.Height / bounds.Height; //表示されている行数取得
                var top    = _listView.Items.Count - row; //1行目のItemIndex
                if (_listView.TopItem.Index == top)
                {
                    //最下行が表示されているので、自動スクロールする
                    _listView.EnsureVisible(_listView.Items.Count - 1);
                }
            }
        }
Beispiel #3
0
        //パケット受信時のイベントハンドラ
        void OnRecv(IntPtr pkt_hdr, IntPtr pkt_data)
        {
            WinPcap.pcap_pkthdr hdr = (WinPcap.pcap_pkthdr)Marshal.PtrToStructure(pkt_hdr, typeof(WinPcap.pcap_pkthdr));
            byte[] data             = new byte[hdr.caplen];
            Marshal.Copy(pkt_data, data, 0, (int)hdr.caplen);
            var recvPacket = new RecvPacket(data);

            if (OnCapture != null)
            {
                OnCapture(recvPacket);
            }
        }
Beispiel #4
0
        public ArpReplyPacket(Log log, RecvPacket recvPacket, byte [] mac)
        {
            EtherHeader etherHeader = new EtherHeader();

            etherHeader.dstMac = new byte[] { 0, 0, 0, 0, 0, 0 };
            etherHeader.srcMac = new byte[] { 0, 0, 0, 0, 0, 0 };

            ArpHeader arpHeader = new ArpHeader();

            arpHeader.srcIp  = new byte[] { 0, 0, 0, 0 };
            arpHeader.dstIp  = new byte[] { 0, 0, 0, 0 };
            arpHeader.srcMac = new byte[] { 0, 0, 0, 0, 0, 0 };
            arpHeader.dstMac = new byte[] { 0, 0, 0, 0, 0, 0 };

            var arpHeaderLen   = Marshal.SizeOf(arpHeader);
            var etherHeaderLen = Marshal.SizeOf(etherHeader);

            Buf = new byte[etherHeaderLen + arpHeaderLen];

            Buffer.BlockCopy(recvPacket.etherHeader.srcMac, 0, etherHeader.dstMac, 0, 6);
            Buffer.BlockCopy(mac, 0, etherHeader.srcMac, 0, 6);
            etherHeader.type = recvPacket.etherHeader.type;


            Buffer.BlockCopy(recvPacket.arpHeader.srcMac, 0, arpHeader.dstMac, 0, 6);
            Buffer.BlockCopy(recvPacket.arpHeader.srcIp, 0, arpHeader.dstIp, 0, 4);
            Buffer.BlockCopy(mac, 0, arpHeader.srcMac, 0, 6);
            Buffer.BlockCopy(recvPacket.arpHeader.dstIp, 0, arpHeader.srcIp, 0, 4);

            arpHeader.code     = 0x0200;//応答
            arpHeader.hwLen    = recvPacket.arpHeader.hwLen;
            arpHeader.hwType   = recvPacket.arpHeader.hwType;
            arpHeader.protoLen = recvPacket.arpHeader.protoLen;
            arpHeader.type     = recvPacket.arpHeader.type;


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

                    Marshal.StructureToPtr(etherHeader, new IntPtr(p + offSet), true);
                    offSet += 14;
                    Marshal.StructureToPtr(arpHeader, new IntPtr(p + offSet), true);
                }
            }
        }
Beispiel #5
0
        public ArpReplyPacket(Log log,RecvPacket recvPacket,byte [] mac)
        {
            EtherHeader etherHeader = new EtherHeader();
            etherHeader.dstMac = new byte[] { 0, 0, 0, 0, 0, 0 };
            etherHeader.srcMac = new byte[] { 0, 0, 0, 0, 0, 0 };

            ArpHeader arpHeader = new ArpHeader();
            arpHeader.srcIp = new byte[] { 0, 0, 0, 0 };
            arpHeader.dstIp = new byte[] { 0, 0, 0, 0 };
            arpHeader.srcMac = new byte[] { 0, 0, 0, 0, 0, 0 };
            arpHeader.dstMac = new byte[] { 0, 0, 0, 0, 0, 0 };

            var arpHeaderLen = Marshal.SizeOf(arpHeader);
            var etherHeaderLen = Marshal.SizeOf(etherHeader);

            Buf = new byte[etherHeaderLen + arpHeaderLen];

            Buffer.BlockCopy(recvPacket.etherHeader.srcMac, 0, etherHeader.dstMac, 0, 6);
            Buffer.BlockCopy(mac, 0, etherHeader.srcMac, 0, 6);
            etherHeader.type = recvPacket.etherHeader.type;

            Buffer.BlockCopy(recvPacket.arpHeader.srcMac, 0, arpHeader.dstMac, 0, 6);
            Buffer.BlockCopy(recvPacket.arpHeader.srcIp, 0, arpHeader.dstIp, 0, 4);
            Buffer.BlockCopy(mac, 0, arpHeader.srcMac, 0, 6);
            Buffer.BlockCopy(recvPacket.arpHeader.dstIp, 0, arpHeader.srcIp, 0, 4);

            arpHeader.code = 0x0200;//応答
            arpHeader.hwLen = recvPacket.arpHeader.hwLen;
            arpHeader.hwType = recvPacket.arpHeader.hwType;
            arpHeader.protoLen = recvPacket.arpHeader.protoLen;
            arpHeader.type = recvPacket.arpHeader.type;

            unsafe {
                fixed (byte* p = Buf) {
                    var offSet=0;
                    Marshal.StructureToPtr(etherHeader, new IntPtr(p + offSet), true);
                    offSet += 14;
                    Marshal.StructureToPtr(arpHeader, new IntPtr(p + offSet), true);
                }
            }
        }
Beispiel #6
0
        public Session(RecvPacket recvPacket,Log log)
        {
            lock (this) {

                Life = true;
                Accept = false;
                _recvPacket = recvPacket;
                _log = log;

                _ident = (short)_rnd.Next(500);//生成

                //squenceは生成する
                _squence = (uint)_rnd.Next(99999);
                //相手のsquence+dataLen+(1)でackを初期化
                _ack = recvPacket.Squence + recvPacket.Len;
                if (recvPacket.Len == 0)
                    _ack++;

                Log(string.Format("Create ({0})", Util.Flg2Str(recvPacket.Flg)));

                Send(0x12, new byte[0]);// SYN/ACK
            }
        }
Beispiel #7
0
        public Session(RecvPacket recvPacket, Log log)
        {
            lock (this) {
                Life        = true;
                Accept      = false;
                _recvPacket = recvPacket;
                _log        = log;

                _ident = (short)_rnd.Next(500);//生成

                //squenceは生成する
                _squence = (uint)_rnd.Next(99999);
                //相手のsquence+dataLen+(1)でackを初期化
                _ack = recvPacket.Squence + recvPacket.Len;
                if (recvPacket.Len == 0)
                {
                    _ack++;
                }

                Log(string.Format("Create ({0})", Util.Flg2Str(recvPacket.Flg)));

                Send(0x12, new byte[0]);// SYN/ACK
            }
        }
Beispiel #8
0
        public bool Append(RecvPacket recvPacket)
        {
            if (!Life)
                return false;

            lock (this) {

                Log(string.Format("Recv ({0}) srcPort={1} len={2} {3}", Util.Flg2Str(recvPacket.Flg), recvPacket.Port[(int)Sd.Src],recvPacket.Len, (recvPacket.Len == 0) ? "" : Encoding.ASCII.GetString(recvPacket.Data)));

                //関係ないパケットは処理しない
                for (int i = 0; i < 2; i++) {
                    if (Util.Mac2Str(recvPacket.Mac[i]) != Util.Mac2Str(_recvPacket.Mac[i])) {
                        return false;
                    }
                    if (Util.Ip2Str(recvPacket.Ip[i]) != Util.Ip2Str(_recvPacket.Ip[i])) {
                        return false;
                    }
                    if (recvPacket.Port[i] != _recvPacket.Port[i]) {
                        return false;
                    }
                }

                if (Util.FIN(recvPacket.Flg)) {

                    //Log(string.Format("Recv ({0})", Util.Flg2Str(recvPacket.Flg)));
                    Send(0x10,new byte[0]);//ACK

                    Life = false;
                } else {

                    //Log(string.Format("Recv ({0}) len={1} {2}", Util.Flg2Str(recvPacket.Flg), recvPacket.Len, (recvPacket.Len==0)?"":Encoding.ASCII.GetString(recvPacket.Data)));

                    if (recvPacket.Len != 0) {

                        if (recvPacket.Len != 0) {
                            var s = Encoding.ASCII.GetString(recvPacket.Data);
                        }

                        //既に受信されているバッファを退避
                        var tmp = new byte[_buffer.Length];
                        Buffer.BlockCopy(_buffer, 0, tmp, 0, _buffer.Length);

                        _buffer = new byte[tmp.Length + recvPacket.Len];//新しいサイズを確保
                        Buffer.BlockCopy(tmp, 0, _buffer, 0, tmp.Length);//既存のデータを戻す
                        Buffer.BlockCopy(recvPacket.Data, 0, _buffer, tmp.Length, recvPacket.Len);//新しいデータを追加
                    }

                    //相手のackでsquenceを初期化する
                    _squence = recvPacket.Ack;
                    //相手のsquence+dataLen+1でackを初期化
                    _ack = recvPacket.Squence + recvPacket.Len;
                    if (recvPacket.Len == 0)
                        _ack++;
                }
                return true;
            }
        }
Beispiel #9
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);
                }
            }
        }
Beispiel #10
0
        void capture_OnCapture(RecvPacket recvPacket)
        {
            lock (this) {
                //************************************************************
                //ARPパケット処理
                //************************************************************
                if (_arpReplay)  //ARP応答処理
                {
                    if (recvPacket.Type == PType.ARP)
                    {
                        if (recvPacket.arpHeader.code == 0x0100)  //要求
                        {
                            var ip = Util.Ip2Str(recvPacket.arpHeader.dstIp);
                            foreach (var a in _arpReplyList)
                            {
                                if (ip == a)
                                {
                                    var arpReplyPacket = new ArpReplyPacket(_log, recvPacket, _myMac);

                                    WinPcap.Send(arpReplyPacket.Buf);
                                    _log.Set(string.Format("ARP Replay {0}", Util.Ip2Str(recvPacket.arpHeader.dstIp)));
                                    return;
                                }
                            }
                        }
                    }
                }
                //************************************************************
                //TCPパケット処理
                //************************************************************
                //宛先MAC確認(Etherヘッダ)
                var mac = Util.Mac2Str(recvPacket.Mac[(int)Sd.Dst]);
                if (mac.ToUpper() != Util.Mac2Str(_myMac))
                {
                    if (mac.ToUpper() != Adapter.Mac.ToUpper())
                    {
                        return;
                    }
                }
                //プロトコル確認(IPヘッダ)
                if (recvPacket.ipHeader.protocol != 0x06)
                {
                    return;
                }
                //ポート番号確認(TCPヘッダ)
                if (recvPacket.Port[(int)Sd.Dst] != _port)
                {
                    return;
                }


                if (recvPacket.Flg == 0x02)  //SYNパケット到着
                //新しいセッションの開始
                {
                    _ar.Add(new Session(recvPacket, _log));
                }
                else    //それ以外のパケット
                //当該セッションに追加
                {
                    for (int i = 0; i < _ar.Count; i++)
                    {
                        if (_ar[i].Append(recvPacket))
                        {
                            break;
                        }
                    }
                }

                //終了したセッションの清掃
                for (int i = _ar.Count - 1; i >= 0; i--)
                {
                    if (!_ar[i].Life)
                    {
                        _log.Set(string.Format("ar.RemoteAt(0)", i));
                        _ar.RemoveAt(i);
                    }
                }
            }
        }
Beispiel #11
0
        public bool Append(RecvPacket recvPacket)
        {
            if (!Life)
            {
                return(false);
            }

            lock (this) {
                Log(string.Format("Recv ({0}) srcPort={1} len={2} {3}", Util.Flg2Str(recvPacket.Flg), recvPacket.Port[(int)Sd.Src], recvPacket.Len, (recvPacket.Len == 0) ? "" : Encoding.ASCII.GetString(recvPacket.Data)));

                //関係ないパケットは処理しない
                for (int i = 0; i < 2; i++)
                {
                    if (Util.Mac2Str(recvPacket.Mac[i]) != Util.Mac2Str(_recvPacket.Mac[i]))
                    {
                        return(false);
                    }
                    if (Util.Ip2Str(recvPacket.Ip[i]) != Util.Ip2Str(_recvPacket.Ip[i]))
                    {
                        return(false);
                    }
                    if (recvPacket.Port[i] != _recvPacket.Port[i])
                    {
                        return(false);
                    }
                }


                if (Util.FIN(recvPacket.Flg))
                {
                    //Log(string.Format("Recv ({0})", Util.Flg2Str(recvPacket.Flg)));
                    Send(0x10, new byte[0]);//ACK

                    Life = false;
                }
                else
                {
                    //Log(string.Format("Recv ({0}) len={1} {2}", Util.Flg2Str(recvPacket.Flg), recvPacket.Len, (recvPacket.Len==0)?"":Encoding.ASCII.GetString(recvPacket.Data)));

                    if (recvPacket.Len != 0)
                    {
                        if (recvPacket.Len != 0)
                        {
                            var s = Encoding.ASCII.GetString(recvPacket.Data);
                        }

                        //既に受信されているバッファを退避
                        var tmp = new byte[_buffer.Length];
                        Buffer.BlockCopy(_buffer, 0, tmp, 0, _buffer.Length);

                        _buffer = new byte[tmp.Length + recvPacket.Len];                           //新しいサイズを確保
                        Buffer.BlockCopy(tmp, 0, _buffer, 0, tmp.Length);                          //既存のデータを戻す
                        Buffer.BlockCopy(recvPacket.Data, 0, _buffer, tmp.Length, recvPacket.Len); //新しいデータを追加
                    }


                    //相手のackでsquenceを初期化する
                    _squence = recvPacket.Ack;
                    //相手のsquence+dataLen+1でackを初期化
                    _ack = recvPacket.Squence + recvPacket.Len;
                    if (recvPacket.Len == 0)
                    {
                        _ack++;
                    }
                }
                return(true);
            }
        }
Beispiel #12
0
        void capture_OnCapture(RecvPacket recvPacket)
        {
            lock (this) {
                //************************************************************
                //ARPパケット処理
                //************************************************************
                if (_arpReplay) {//ARP応答処理
                    if (recvPacket.Type == PType.ARP) {
                        if (recvPacket.arpHeader.code == 0x0100) {//要求
                            var ip = Util.Ip2Str(recvPacket.arpHeader.dstIp);
                            foreach (var a in _arpReplyList) {
                                if (ip == a) {
                                    var arpReplyPacket = new ArpReplyPacket(_log, recvPacket, _myMac);

                                    WinPcap.Send(arpReplyPacket.Buf);
                                    _log.Set(string.Format("ARP Replay {0}", Util.Ip2Str(recvPacket.arpHeader.dstIp)));
                                    return;
                                }
                            }
                        }
                    }
                }
                //************************************************************
                //TCPパケット処理
                //************************************************************
                //宛先MAC確認(Etherヘッダ)
                var mac = Util.Mac2Str(recvPacket.Mac[(int)Sd.Dst]);
                if (mac.ToUpper() != Util.Mac2Str(_myMac)) {
                    if (mac.ToUpper() != Adapter.Mac.ToUpper()) {
                        return;
                    }
                }
                //プロトコル確認(IPヘッダ)
                if (recvPacket.ipHeader.protocol != 0x06) {
                    return;
                }
                //ポート番号確認(TCPヘッダ)
                if (recvPacket.Port[(int)Sd.Dst] != _port)
                    return;

                if (recvPacket.Flg == 0x02) {//SYNパケット到着
                    //新しいセッションの開始
                    _ar.Add(new Session(recvPacket, _log));

                } else {//それ以外のパケット
                    //当該セッションに追加
                    for (int i = 0; i < _ar.Count; i++) {
                        if (_ar[i].Append(recvPacket)) {
                            break;
                        }
                    }
                }

                //終了したセッションの清掃
                for (int i = _ar.Count - 1; i >= 0; i--) {
                    if (!_ar[i].Life) {
                        _log.Set(string.Format("ar.RemoteAt(0)", i));
                        _ar.RemoveAt(i);
                    }
                }
            }
        }
Beispiel #13
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);
            }
        }
Beispiel #14
0
 void _capture_OnCapture(RecvPacket p)
 {
     _captureView.Set(p);
 }
Beispiel #15
0
 void _capture_OnCapture(RecvPacket p)
 {
     _captureView.Set(p);
 }