Compare commits
No commits in common. "497f55c40cb72d86097fd62eb7c7d6a6cd579fd2" and "a9afea0603edbd7639ef0eb827046abceacfff66" have entirely different histories.
497f55c40c
...
a9afea0603
2
pom.xml
2
pom.xml
@ -18,7 +18,7 @@
|
||||
<dependency>
|
||||
<groupId>io.netty</groupId>
|
||||
<artifactId>netty-all</artifactId>
|
||||
<version>4.1.110.Final</version>
|
||||
<version>4.1.68.Final</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
|
@ -38,7 +38,6 @@ public class RIPService {
|
||||
scheduler.scheduleAtFixedRate(() -> {
|
||||
RIPPacket packet = createRipResponsePacket(Config.RIP_VERSION);
|
||||
client.sendRipPacket(packet);
|
||||
System.out.println("客户端 发送了一次消息");
|
||||
}, 0, 10, TimeUnit.SECONDS);
|
||||
System.out.println("客户端已经启动");
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package cn.x47.config;
|
||||
|
||||
public class Config {
|
||||
// 设置协议版本,1 表示 RIP v1,2 表示 RIP v2
|
||||
public static final byte RIP_VERSION = 1;
|
||||
public static final byte RIP_VERSION = 2;
|
||||
public static final boolean startServer = true;
|
||||
public static final boolean startClient = false;
|
||||
public static final boolean startClient = true;
|
||||
}
|
||||
|
@ -29,22 +29,26 @@ public class RIPPacketDecoder extends MessageToMessageDecoder<DatagramPacket> {
|
||||
while (buf.readableBytes() >= 20) {
|
||||
RIPEntry entry = new RIPEntry();
|
||||
entry.setAddressFamily(buf.readShort());
|
||||
if (ripPacket.getVersion() == 2) {
|
||||
entry.setRouteTag(buf.readShort());
|
||||
|
||||
} else {
|
||||
entry.setRouteTag((short) 0);
|
||||
}
|
||||
byte[] ipBytes = new byte[4];
|
||||
buf.readBytes(ipBytes);
|
||||
entry.setIpAddress(InetAddress.getByAddress(ipBytes));
|
||||
|
||||
if (ripPacket.getVersion() == 2) {
|
||||
buf.readBytes(ipBytes);
|
||||
entry.setSubnetMask(InetAddress.getByAddress(ipBytes));
|
||||
buf.readBytes(ipBytes);
|
||||
entry.setNextHop(InetAddress.getByAddress(ipBytes));
|
||||
if (ripPacket.getVersion() == 1) {
|
||||
} else {
|
||||
// 对于 RIP v1,子网掩码和下一跳需要推断或设为默认值
|
||||
entry.setSubnetMask(InetAddress.getByName("255.255.255.0")); // 示例,实际应根据 IP 类推断
|
||||
entry.setNextHop(packet.sender().getAddress());
|
||||
}
|
||||
entry.setMetric((int) buf.readUnsignedInt());
|
||||
entry.setMetric(buf.readInt());
|
||||
entries.add(entry);
|
||||
}
|
||||
ripPacket.setEntries(entries);
|
||||
|
@ -11,7 +11,6 @@ public class RIPServerHandler extends SimpleChannelInboundHandler<RIPPacket> {
|
||||
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, RIPPacket msg) throws Exception {
|
||||
System.out.println(msg);
|
||||
if (msg.getCommand() == 2) { // Response
|
||||
for (RIPEntry entry : msg.getEntries()) {
|
||||
// 更新本地路由表
|
||||
|
@ -11,6 +11,9 @@ import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.DatagramChannel;
|
||||
import io.netty.channel.socket.nio.NioDatagramChannel;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
|
||||
public class RIPClient {
|
||||
|
||||
@ -33,7 +36,8 @@ public class RIPClient {
|
||||
|
||||
public void sendRipPacket(RIPPacket packet) {
|
||||
try {
|
||||
ChannelFuture future = bootstrap.bind(0).sync();
|
||||
ChannelFuture future = bootstrap.bind(0)
|
||||
.sync();
|
||||
future.channel().writeAndFlush(packet).sync();
|
||||
future.channel().close();
|
||||
} catch (InterruptedException e) {
|
||||
|
@ -4,9 +4,7 @@ package cn.x47.service;
|
||||
import cn.x47.handle.RIPPacketDecoder;
|
||||
import cn.x47.handle.RIPServerHandler;
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.channel.ChannelFuture;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.ChannelOption;
|
||||
import io.netty.channel.*;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.DatagramChannel;
|
||||
import io.netty.channel.socket.nio.NioDatagramChannel;
|
||||
|
Loading…
x
Reference in New Issue
Block a user