var ip = new IPv4("192.168.0.100"); var subnet = new Subnet("/24"); var ipnet = new IPv4Network(ip,subnet); var network = ipnet.network(); //IPv4("192.168.0.0") var broadcast = ipnet.broadcast(); //IPv4("192.168.255.255"); var stopIP = new IPv4("192.168.0.10"); range_ips = []; ipnet.iter(function(ip,index) { range_ips.push(ip); if(ip.equals(stopIP)) return false; }); //check IPs are in a network var is_contained = ipnet.contains(ip) //true; var total = ipnet.count(); //254
/** * @param sourceAddress * the sourceAddress to set * @return this */ public IPv4 setSourceAddress(final String sourceAddress) { this.sourceAddress = IPv4.toIPv4Address(sourceAddress); return this; }
if (ipRequest.getProtocol() != IPv4.PROTOCOL_ICMP) { return null; IPv4 ipReply = new IPv4(); int destAddress = ipRequest.getDestinationAddress(); ipReply.setDestinationAddress(ipRequest.getSourceAddress()); ipReply.setSourceAddress(destAddress); ipReply.setTtl((byte) 64); ipReply.setDiffServ(ipRequest.getDiffServ()); ipReply.setChecksum((short) 0); ipReply.setProtocol(IPv4.PROTOCOL_ICMP); ICMP icmpRequest = (ICMP) ipRequest.getPayload(); ICMP icmpReply = new ICMP(); icmpReply.setIcmpCode(ICMP.CODE_ECHO_REPLY); icmpReply.setChecksum((short) 0); ipReply.setPayload(icmpReply);
((IPv4) this.parent).setProtocol(IPv4.PROTOCOL_UDP); if (this.parent instanceof IPv4) { final IPv4 ipv4 = (IPv4) this.parent; accumulation += (ipv4.getSourceAddress() >> 16 & 0xffff) + (ipv4.getSourceAddress() & 0xffff); accumulation += (ipv4.getDestinationAddress() >> 16 & 0xffff) + (ipv4.getDestinationAddress() & 0xffff); accumulation += ipv4.getProtocol() & 0xff; accumulation += length & 0xffff; } else if (this.parent instanceof IPv6) {
final ARP p = (ARP) pkt; sb.append("\nnw_src: "); sb.append(IPv4.fromIPv4Address(IPv4.toIPv4Address(p .getSenderProtocolAddress()))); sb.append("\nnw_dst: "); sb.append(IPv4.fromIPv4Address(IPv4.toIPv4Address(p .getTargetProtocolAddress()))); } else if (pkt instanceof LLDP) { final IPv4 p = (IPv4) pkt; sb.append("\nnw_src: "); sb.append(IPv4.fromIPv4Address(p.getSourceAddress())); sb.append("\nnw_dst: "); sb.append(IPv4.fromIPv4Address(p.getDestinationAddress())); sb.append("\nnw_tos: "); sb.append(p.getDiffServ()); sb.append("\nnw_proto: "); sb.append(p.getProtocol());
ipSrc = IpAddress.valueOf(ipv4Packet.getSourceAddress()); ipDst = IpAddress.valueOf(ipv4Packet.getDestinationAddress()); protocol = ipv4Packet.getProtocol(); if (protocol == IPv4.PROTOCOL_TCP) { TCP tcpPacket = (TCP) ipv4Packet.getPayload(); portSrc = tcpPacket.getSourcePort(); portDst = tcpPacket.getDestinationPort(); } else if (protocol == IPv4.PROTOCOL_UDP) { UDP udpPacket = (UDP) ipv4Packet.getPayload(); portSrc = udpPacket.getSourcePort(); portDst = udpPacket.getDestinationPort();
@Override public void sendPacketIn(PccId pccId) { TCP tcp = new TCP(); // Set the well known PCEP port. To be used to decide to process/discard the packet while processing. tcp.setDestinationPort(PCEP_PORT); IPv4 ipv4 = new IPv4(); ipv4.setProtocol(IPv4.PROTOCOL_TCP); ipv4.setPayload(tcp); Ethernet eth = new Ethernet(); eth.setEtherType(Ethernet.TYPE_IPV4); eth.setDestinationMACAddress(MacAddress.NONE); eth.setPayload(ipv4); // Get lsrId of the PCEP client from the PCC ID. Session info is based on lsrID. String lsrId = String.valueOf(pccId.ipAddress()); DeviceId pccDeviceId = DeviceId.deviceId(lsrId); InboundPacket inPkt = new DefaultInboundPacket(new ConnectPoint(pccDeviceId, PortNumber.portNumber(PCEP_PORT)), eth, null); packetProviderService.processPacket(new PcepPacketContext(inPkt, null)); } }
@Override public void process(PacketContext context) { // Stop processing if the packet has been handled, since we // can't do any more to it. log.debug("Received trigger for label DB sync."); if (context.isHandled()) { return; } InboundPacket pkt = context.inPacket(); if (pkt == null) { return; } Ethernet ethernet = pkt.parsed(); if (ethernet == null || ethernet.getEtherType() != Ethernet.TYPE_IPV4) { return; } IPv4 ipPacket = (IPv4) ethernet.getPayload(); if (ipPacket == null || ipPacket.getProtocol() != IPv4.PROTOCOL_TCP) { return; } TCP tcp = (TCP) ipPacket.getPayload(); if (tcp == null || tcp.getDestinationPort() != PCEP_PORT) { return; } syncLabelDb(pkt.receivedFrom().deviceId()); } }
private void upStreamPacketProcessor(IPv4 ipPacket, DeviceId deviceId) { IpAddress srcIp = IpAddress.valueOf(ipPacket.getSourceAddress()); IpAddress dstIp = IpAddress.valueOf(ipPacket.getDestinationAddress()); FloatingIp floatingIp = null; Collection<FloatingIp> floatingIps = floatingIpService
checkInput(data, offset, length, HEADER_LENGTH); IPv4 ipv4 = new IPv4(); ipv4.setVersion((byte) (versionByte >> 4 & 0xf)); ipv4.setDiffServ(bb.get()); ipv4.totalLength = bb.getShort(); ipv4.identification = bb.getShort();
((IPv4) this.parent).setProtocol(IPv4.PROTOCOL_ICMP);
/** * Deserializer function for IP packets. * * @return deserializer function */ public static Deserializer<? extends IP> deserializer() { return (data, offset, length) -> { final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); byte version = (byte) (bb.get() >> 4 & 0xf); switch (version) { case 4: return IPv4.deserializer().deserialize(data, offset, length); case 6: return IPv6.deserializer().deserialize(data, offset, length); default: throw new DeserializationException("Invalid IP version"); } }; } }
((IPv4) this.parent).setProtocol(IPv4.PROTOCOL_TCP); if (this.parent instanceof IPv4) { final IPv4 ipv4 = (IPv4) this.parent; accumulation += (ipv4.getSourceAddress() >> 16 & 0xffff) + (ipv4.getSourceAddress() & 0xffff); accumulation += (ipv4.getDestinationAddress() >> 16 & 0xffff) + (ipv4.getDestinationAddress() & 0xffff); accumulation += ipv4.getProtocol() & 0xff; accumulation += length & 0xffff; } else if (this.parent instanceof IPv6) {
/** * @param destinationAddress * the destinationAddress to set * @return this */ public IPv4 setDestinationAddress(final String destinationAddress) { this.destinationAddress = IPv4.toIPv4Address(destinationAddress); return this; }
private void arpresponceProcess(ARP arpPacket, DeviceId deviceId) { MacAddress srcMac = MacAddress .valueOf(arpPacket.getTargetHardwareAddress()); MacAddress dstMac = MacAddress .valueOf(arpPacket.getSenderHardwareAddress()); IpAddress srcIp = IpAddress.valueOf(IPv4 .toIPv4Address(arpPacket.getTargetProtocolAddress())); IpAddress dstIp = IpAddress.valueOf(IPv4 .toIPv4Address(arpPacket.getSenderProtocolAddress())); FloatingIp floatingIp = floatingIpStore.get(srcIp); if (floatingIp == null) { return; } DeviceId deviceIdOfFloatingIp = getDeviceIdOfFloatingIP(floatingIp); if (!deviceId.equals(deviceIdOfFloatingIp)) { return; } if (!downloadSnatRules(deviceId, srcMac, srcIp, dstMac, dstIp, floatingIp)) { return; } }
private void arprequestProcess(ARP arpPacket, DeviceId deviceId) { MacAddress dstMac = MacAddress .valueOf(arpPacket.getSenderHardwareAddress()); IpAddress srcIp = IpAddress.valueOf(IPv4 .toIPv4Address(arpPacket.getTargetProtocolAddress())); IpAddress dstIp = IpAddress.valueOf(IPv4 .toIPv4Address(arpPacket.getSenderProtocolAddress())); FloatingIp floatingIp = floatingIpStore.get(srcIp); if (floatingIp == null) { return; } DeviceId deviceIdOfFloatingIp = getDeviceIdOfFloatingIP(floatingIp); if (!deviceId.equals(deviceIdOfFloatingIp)) { return; } Port exPort = exPortOfDevice.get(deviceId); MacAddress srcMac = MacAddress.valueOf(exPort.annotations() .value(AnnotationKeys.PORT_MAC)); if (!downloadSnatRules(deviceId, srcMac, srcIp, dstMac, dstIp, floatingIp)) { return; } Ethernet ethernet = buildArpResponse(dstIp, dstMac, srcIp, srcMac); if (ethernet != null) { sendPacketOut(deviceId, exPort.number(), ethernet); } }