KLALB V3.4

This commit is contained in:
Administrator
2025-11-15 11:08:16 +08:00
parent 306bfd36f7
commit 8842566ee2
160 changed files with 18540 additions and 4512 deletions
@@ -27,8 +27,12 @@ import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import org.kne.cloud.network.ByteBufferAllocator;
import org.kne.cloud.network.ipv6.IPv6NetworkLink;
import org.kne.cloud.network.ipv6.Inet6AddressGroup;
import org.kne.cloud.network.ipv6.Neighbor;
@@ -69,7 +73,7 @@ public class KLALBRoutingProtocol extends Thread{
public static final long HOTSOPT_TIMEOUT = 1000000000;
public static final int HOTSOPT_REPORT_INTERVAL = 50000000;
public static final int HOTSOPT_REPORT_INTERVAL = 1000000;
private SRv6Router router;
public SRv6Router getRouter() {
return router;
@@ -83,15 +87,27 @@ public class KLALBRoutingProtocol extends Thread{
private long floodTimer=System.nanoTime();
private long floodTimer2=System.nanoTime();
private long requestTimer=System.nanoTime();
KLALBVirtualRawSocket ds = null;
private KLALBVirtualRawSocket ds = null;
private ReentrantLock sendLock=new ReentrantLock();
private Set<BiConsumer<SocketAddress,JsonDataPacket>>receivers=new CopyOnWriteArraySet<BiConsumer<SocketAddress,JsonDataPacket>>();
public void addReceiver(BiConsumer<SocketAddress,JsonDataPacket> rec) {
receivers.add(rec);
}
public void removeReceiver(BiConsumer<SocketAddress,JsonDataPacket> rec) {
receivers.remove(rec);
}
@Override
public void run() {
Thread.currentThread().setName("KLALB路由协议接收线程");
getSelfRouterInfo();
try{
ds=new KLALBVirtualRawSocket(router,router.getLocator().getAddress(), DEFAULT_PROTOCOL_NUMBER);
ds=new KLALBVirtualRawSocket(router.getinLoopback(),router.getLocator().getAddress(), DEFAULT_PROTOCOL_NUMBER);
new Thread(()->{
Thread.currentThread().setName("KLALB路由协议接收线程");
Thread.currentThread().setName("KLALB路由协议发送线程");
while(true) {
try {
@@ -115,25 +131,29 @@ public class KLALBRoutingProtocol extends Thread{
}*/
long cur=System.nanoTime();
if(cur-floodTimer>60000000000L) {
if(cur-floodTimer>20000000000L) {
if(debug)
System.out.println("update60");
floodTimer=cur;
floodPacket(ds, null, new RouterInfoPacket(selfRouterInfo,true,-1));
floodPacket( null, new RouterInfoPacket(selfRouterInfo,true,-1));
}
if(cur-floodTimer2>1000000000L) {
if(debug)
System.out.println("update1");
floodTimer2=cur;
floodPacket(ds, null, new RouterInfoPacket(selfRouterInfo,true,router.getASN()));
floodPacket(null, new RouterInfoPacket(selfRouterInfo,true,router.getASN()));
}
//Set<Inet6Address> requestSet=new HashSet<>();
for (Iterator<Entry<Inet6Address, RouterInfo>> iterator = netmap.entrySet().iterator(); iterator.hasNext();) {
Entry<Inet6Address, RouterInfo> type = (Entry<Inet6Address, RouterInfo>) iterator.next();
RouterInfo val=type.getValue();
if(val.checkTimeOut()) {
long timeout=30000000000L;
if(val.getAsn()==router.getASN()) {
timeout=2000000000L;
}
if(val.checkTimeOut(timeout)) {
iterator.remove();
noticeUpdate();
}
@@ -197,21 +217,20 @@ public class KLALBRoutingProtocol extends Thread{
while(true) {
try {
byte[]ca=new byte[65535];
byte[]ca=ByteBufferAllocator.allocateArray(65535);
DatagramPacket dgp=new DatagramPacket(ca, ca.length);
ds.receive(dgp);
//System.out.println(Arrays.toString( Arrays.copyOf( dgp.getData(),dgp.getLength())));
ByteArrayInputStream bi=new ByteArrayInputStream(dgp.getData(),0,dgp.getLength());
KLALBRoutingProtocolPacket kp=KLALBRoutingProtocolPacket.readKLALBPacketFromChannel(Channels.newChannel(bi));
//System.out.println(kp);
switch(kp.getType()) {
case KLALBRoutingProtocolPacket.RINFO_REQ:
RouterInfo rifr=netmap.get(router.getLocator().getAddress());
if(debug)
System.out.println("get Request from:"+dgp.getSocketAddress());
if(rifr!=null) {
writePacket(ds, new RouterInfoPacket( rifr,false,-1), dgp.getSocketAddress());
writePacket( new RouterInfoPacket( rifr,false,-1), dgp.getSocketAddress());
if(debug)
System.out.println("response router info:"+rifr);
}else {
@@ -233,7 +252,7 @@ public class KLALBRoutingProtocol extends Thread{
netmap.put(rif.getLocator().getAddress(), rif);
noticeUpdate();
if(rifp.isFlood()) {
floodPacket(ds, dgp.getSocketAddress(), rifp);
floodPacket( dgp.getSocketAddress(), rifp);
}
@@ -244,6 +263,10 @@ public class KLALBRoutingProtocol extends Thread{
}
computeShortestPathIfUpdated();
break;
case KLALBRoutingProtocolPacket.JSON_DATA:
JsonDataPacket data=(JsonDataPacket) kp;
receivers.forEach((v)->{v.accept(dgp.getSocketAddress(),data);});
break;
default:
break;
}
@@ -260,43 +283,44 @@ public class KLALBRoutingProtocol extends Thread{
}
}
private void floodPacket(KLALBVirtualRawSocket ds2,SocketAddress except,RouterInfoPacket rifp) throws IOException {
private void floodPacket(SocketAddress except,RouterInfoPacket rifp) throws IOException {
long asn=rifp.getASN();
List<IPv6NetworkLink>links=router.getLinkTabel();
Object[] nls=links.toArray();
for(int i=0;i<nls.length;i++) {
IPv6NetworkLink nl=(IPv6NetworkLink) nls[i];
if(!nl.isLoopBack()) {
List<Neighbor>neis=nl.getNeighborsInfo();
for (Iterator<Neighbor> iteratorx = neis.iterator(); iteratorx.hasNext();) {
Neighbor addresses = (Neighbor) iteratorx.next();
List<Neighbor>ln= router.getNeighbors();
for(Neighbor addresses :ln) {
if(addresses.getLocator()==null)
continue;
InetSocketAddress isa=new InetSocketAddress(addresses.getLocator().getAddress(),DEFAULT_PORT);
if(!isa.equals(except)) {
if(asn==-1) {
writePacket(ds2, rifp, isa);
writePacket( rifp, isa);
}else{
RouterInfo nif= netmap.get(isa.getAddress());
if(nif!=null&&nif.getAsn()==asn) {
writePacket(ds2, rifp, isa);
writePacket( rifp, isa);
}
}
}
}
}
}
}
private void writePacket(KLALBVirtualRawSocket ds2,KLALBRoutingProtocolPacket pkt,SocketAddress dest) throws IOException {
public void sendJsonPacketToAddress(JsonDataPacket pkt,SocketAddress dest) throws IOException {
writePacket(pkt,dest);
}
private void writePacket(KLALBRoutingProtocolPacket pkt,SocketAddress dest) throws IOException {
ByteArrayOutputStream bos=new ByteArrayOutputStream(65535);
KLALBRoutingProtocolPacket.writeKLALBPacketToChannel(Channels.newChannel(bos),pkt);
byte[]to=bos.toByteArray();
DatagramPacket dgpx=new DatagramPacket(to,to.length);
dgpx.setSocketAddress(dest);
sendLock.lock();
try {
ds2.send(dgpx);
ds.send(dgpx);
}catch(BindException e) {
e.printStackTrace();
System.err.println("Cannot assign:"+dest);
}finally {
sendLock.unlock();
}
}
@@ -550,7 +574,7 @@ public class KLALBRoutingProtocol extends Thread{
}
}
}
NeighborInfo ni=new NeighborInfo(nl.getAddressGroup(),addresses.getAddress(),addresses.getLocator(),updelay ,downdelay,updelayMin ,downdelayMin ,uploadspeed ,downloadspeed,uploadspeedmax ,downloadspeedmax);
NeighborInfo ni=new NeighborInfo(nl.getAddressGroups().get(0),addresses.getAddress(),addresses.getLocator(),updelay ,downdelay,updelayMin ,downdelayMin ,uploadspeed ,downloadspeed,uploadspeedmax ,downloadspeedmax);
ni.setContext(addresses);
ri.getNeighborAddresses().add(ni);
@@ -624,9 +648,9 @@ public class KLALBRoutingProtocol extends Thread{
hat.refreshTimeout();
}
if(hat.checkReportTime()) {
System.out.println("hotspot address:"+sourceAddress);
//System.out.println("hotspot address:"+sourceAddress);
try {
writePacket(ds, new RouterInfoPacket( netmap.get(router.getLocator().getAddress()),false,-1), new InetSocketAddress(sourceAddress, DEFAULT_PORT));
writePacket( new RouterInfoPacket( netmap.get(router.getLocator().getAddress()),false,-1), new InetSocketAddress(sourceAddress, DEFAULT_PORT));
} catch (IOException e) {
e.printStackTrace();
}