/// <summary> Fetch the urgent pointer.</summary> public virtual int getUrgentPointer() { if (!_urgentPointerSet) { _urgentPointer = ArrayHelper.extractInteger(Bytes, _ipOffset + TCPFields_Fields.TCP_URG_POS, TCPFields_Fields.TCP_URG_LEN); _urgentPointerSet = true; } return(_urgentPointer); }
/// <summary> Convert captured packet data into an object.</summary> public static Packet dataToPacket(int linkType, byte[] bytes, Timeval tv) { int ethProtocol; // retrieve the length of the headers associated with this link layer type. // this length is the offset to the header embedded in the packet. int lLen = LinkLayer.getLinkLayerLength(linkType); // extract the protocol code for the type of header embedded in the // link-layer of the packet int offset = LinkLayer.getProtoOffset(linkType); if (offset == -1) { // if there is no embedded protocol, assume IP? ethProtocol = EthernetPacket.EtherType.IP; } else { ethProtocol = ArrayHelper.extractInteger(bytes, offset, EthernetFields_Fields.ETH_CODE_LEN); } // try to recognize the ethernet type.. switch (ethProtocol) { // arp case EthernetPacket.EtherType.ARP: return(new ARPPacket(lLen, bytes, tv)); case EthernetPacket.EtherType.IPV6: case EthernetPacket.EtherType.IP: // ethernet level code is recognized as IP, figure out what kind.. int ipProtocol = IPProtocol.extractProtocol(lLen, bytes); switch (ipProtocol) { case (int)IPProtocol.IPProtocolType.ICMP: return(new ICMPPacket(lLen, bytes, tv)); case (int)IPProtocol.IPProtocolType.IGMP: return(new IGMPPacket(lLen, bytes, tv)); case (int)IPProtocol.IPProtocolType.TCP: return(new TCPPacket(lLen, bytes, tv)); case (int)IPProtocol.IPProtocolType.UDP: return(new UDPPacket(lLen, bytes, tv)); // unidentified ip.. default: return(new IPPacket(lLen, bytes, tv)); } // ethernet level code not recognized, default to anonymous packet.. default: return(new EthernetPacket(lLen, bytes, tv)); } }
/// <summary> /// Sets the data section of this udp packet /// </summary> /// <param name="data">the data bytes</param> public void SetData(byte[] data) { if (IPVersion != IPVersions.IPv4) { throw new System.NotImplementedException("IPVersion of " + IPVersion + " is unrecognized"); } byte[] headers = ArrayHelper.copy(Bytes, 0, UDPFields_Fields.UDP_HEADER_LEN + IPHeaderLength + EthernetHeaderLength); byte[] newBytes = ArrayHelper.join(headers, data); this.Bytes = newBytes; UDPLength = Bytes.Length - IPHeaderLength - EthernetHeaderLength; //update ip total length length IPTotalLength = IPHeaderLength + UDPFields_Fields.UDP_HEADER_LEN + data.Length; //update also offset and pcap header OnOffsetChanged(); }
public int ComputeTransportLayerChecksum(int checksumOffset, bool update, bool pseudoIPHeader) { // copy the tcp section with data byte[] dataToChecksum = IPData; // reset the checksum field (checksum is calculated when this field is // zeroed) ArrayHelper.insertLong(dataToChecksum, 0, checksumOffset, 2); if (pseudoIPHeader) { dataToChecksum = AttachPseudoIPHeader(dataToChecksum); } // compute the one's complement sum of the tcp header int cs = ChecksumUtils.OnesComplementSum(dataToChecksum); if (update) { SetTransportLayerChecksum(cs, checksumOffset); } return(cs); }
/// <summary> Computes the UDP checksum, optionally updating the UDP checksum header. /// /// </summary> /// <param name="update">Specifies whether or not to update the UDP checksum header /// after computing the checksum. A value of true indicates the /// header should be updated, a value of false indicates it should /// not be updated. /// </param> /// <returns> The computed UDP checksum. /// </returns> public int ComputeUDPChecksum(bool update) { if (IPVersion != IPVersions.IPv4) { throw new System.NotImplementedException("IPVersion of " + IPVersion + " is unrecognized"); } // copy the udp section with data byte[] udp = IPData; // reset the checksum field (checksum is calculated when this field is // zeroed) ArrayHelper.insertLong(udp, 0, UDPFields_Fields.UDP_CSUM_POS, UDPFields_Fields.UDP_CSUM_LEN); //pseudo ip header should be attached to the udp+data udp = AttachPseudoIPHeader(udp); // compute the one's complement sum of the udp header int cs = ChecksumUtils.OnesComplementSum(udp); if (update) { UDPChecksum = cs; } return(cs); }
/// <summary> Sets the urgent pointer. /// /// </summary> /// <param name="pointer">The urgent pointer value. /// </param> public void setUrgentPointer(int pointer) { ArrayHelper.insertLong(Bytes, pointer, _ipOffset + TCPFields_Fields.TCP_URG_POS, TCPFields_Fields.TCP_URG_LEN); _urgentPointerSet = false; }
/// <summary> Sets the IP header checksum.</summary> protected internal virtual void SetChecksum(int cs, int checkSumOffset) { ArrayHelper.insertLong(Bytes, cs, checkSumOffset, 2); }
/// <summary> Fetch the header checksum.</summary> public virtual int GetTransportLayerChecksum(int pos) { return(ArrayHelper.extractInteger(Bytes, pos, 2)); }
/// <summary> Convert captured packet data into an object.</summary> public static Packet dataToPacket(LinkLayers linkType, byte[] bytes, Timeval tv) { EthernetPacketType ethProtocol; // retrieve the length of the headers associated with this link layer type. // this length is the offset to the header embedded in the packet. int byteOffsetToEthernetPayload = LinkLayer.LinkLayerLength(linkType); // extract the protocol code for the type of header embedded in the // link-layer of the packet int offset = LinkLayer.ProtocolOffset(linkType); if (offset == -1) { // if there is no embedded protocol, assume IpV4 ethProtocol = EthernetPacketType.IPv4; } else { ethProtocol = (EthernetPacketType)ArrayHelper.extractInteger(bytes, offset, EthernetFields_Fields.ETH_CODE_LEN); } string errorString; Packet parsedPacket = null; try { // try to recognize the ethernet type.. switch (ethProtocol) { // arp case EthernetPacketType.ARP: parsedPacket = new ARPPacket(byteOffsetToEthernetPayload, bytes, tv); break; case EthernetPacketType.IPv6: case EthernetPacketType.IPv4: try { // ethernet level code is recognized as IP, figure out what kind.. int ipProtocol = IPProtocol.extractProtocol(byteOffsetToEthernetPayload, bytes); switch (ipProtocol) { case (int)IPProtocol.IPProtocolType.ICMP: parsedPacket = new ICMPPacket(byteOffsetToEthernetPayload, bytes, tv); break; case (int)IPProtocol.IPProtocolType.IGMP: parsedPacket = new IGMPPacket(byteOffsetToEthernetPayload, bytes, tv); break; case (int)IPProtocol.IPProtocolType.TCP: parsedPacket = new TCPPacket(byteOffsetToEthernetPayload, bytes, tv); break; case (int)IPProtocol.IPProtocolType.UDP: parsedPacket = new UDPPacket(byteOffsetToEthernetPayload, bytes, tv); break; // unidentified ip.. default: parsedPacket = new IPPacket(byteOffsetToEthernetPayload, bytes, tv); break; } // check that the parsed packet is valid if (!parsedPacket.IsValid(out errorString)) { throw new PcapException(errorString); } else { return(parsedPacket); } } catch { // error parsing the specific ip packet type, parse as a generic IPPacket parsedPacket = new IPPacket(byteOffsetToEthernetPayload, bytes, tv); // check that the parsed packet is valid if (!parsedPacket.IsValid(out errorString)) { throw new PcapException(errorString); } else { return(parsedPacket); } } // ethernet level code not recognized, default to anonymous packet.. default: parsedPacket = new EthernetPacket(byteOffsetToEthernetPayload, bytes, tv); break; } return(parsedPacket); } catch { // we know we have at least an ethernet packet, so return that return(new EthernetPacket(byteOffsetToEthernetPayload, bytes, tv)); } }