Пример #1
0
        /// <summary>
        /// Write IP and UDP headers and payload data into a
        /// byte array.
        /// </summary>
        /// <param name="pkt">Array of bytes representing
        /// packet to be sent.</param>
        /// <param name="offset">Offset of IP Header within
        /// packet.</param>
        /// <param name="ipHeader">IP header to be written
        /// to packet.</param>
        /// <param name="udpHeader">UDP header to be written
        /// to packet.</param>
        /// <param name="payload">Payload of UDP Packet.</param>
        /// <param name="payloadOffset">The offset of start
        /// of the payload data within the payload
        /// array.</param>

        /// <param name="payloadLength">The size of the payload data.</param>
        public static void WriteUdpPacket(byte[] !pkt,
                                          int offset,
                                          IPFormat.IPHeader !ipHeader,
                                          ref UdpHeader udpHeader,
                                          byte[] payload,
                                          int payloadOffset,
                                          int payloadLength)
        {
            int udpStart = IPFormat.WriteIPHeader(pkt, offset, ipHeader);
            int udpEnd   = WriteUdpHeader(pkt, udpStart, ref udpHeader);

            if (pkt != payload || udpEnd != payloadOffset)
            {
                Array.Copy(payload, payloadOffset,
                           pkt, udpEnd, payloadLength);
            }
            SetUdpChecksum(pkt, offset, ref udpHeader);
        }
Пример #2
0
        // a helper method to write the TCP segment (without data)
        public static void WriteTcpSegment(byte[] !pktData,
                                           ushort localPort,
                                           ushort destPort,
                                           uint ackSeq,
                                           uint seq,
                                           ushort wnd,
                                           IPv4 sIP,
                                           IPv4 dIP,
                                           ushort dataLen,
                                           bool isAck,
                                           bool isSyn,
                                           bool isRst,
                                           bool isFin,
                                           bool withOptions)
        {
            // now create the IP,TCP headers!
            int start = EthernetFormat.Size;

            // prepare it...
            TcpFormat.TcpHeader tcpHeader = new TcpFormat.TcpHeader();
            tcpHeader.sourcePort = localPort;
            tcpHeader.destPort   = destPort;
            if (withOptions)
            {
                tcpHeader.off_res1 = 0x60;
            }
            else
            {
                tcpHeader.off_res1 = 0x50;
            }

            tcpHeader.ackSeq = ackSeq;
            tcpHeader.seq    = seq;
            tcpHeader.window = wnd;

            if (isFin)
            {
                TcpFormat.SetFIN(ref tcpHeader);
            }
            if (isSyn)
            {
                TcpFormat.SetSYN(ref tcpHeader);
            }
            if (isAck)
            {
                TcpFormat.SetACK(ref tcpHeader);
            }
            if (isRst)
            {
                TcpFormat.SetReset(ref tcpHeader);
            }

            IPFormat.IPHeader ipHeader = new IPFormat.IPHeader();
            ipHeader.SetDefaults(IPFormat.Protocol.TCP);
            ipHeader.totalLength = (ushort)(IPFormat.Size + TcpFormat.Size + dataLen);
            ipHeader.srcAddr     = sIP;
            ipHeader.destAddr    = dIP;
            IPFormat.SetDontFragBit(ipHeader);

            // write the ip header + ip checksum
            start = IPFormat.WriteIPHeader(pktData, start, ipHeader);

            // write TCP header, no checksum
            start = TcpFormat.WriteTcpHeader(pktData, start, ref tcpHeader);

            // calc the checksum...
            TcpFormat.SetTcpChecksum(pktData, EthernetFormat.Size, ref tcpHeader);
        }