Compare commits

..

No commits in common. "497f55c40cb72d86097fd62eb7c7d6a6cd579fd2" and "a9afea0603edbd7639ef0eb827046abceacfff66" have entirely different histories.

7 changed files with 21 additions and 17 deletions

View File

@ -18,7 +18,7 @@
<dependency> <dependency>
<groupId>io.netty</groupId> <groupId>io.netty</groupId>
<artifactId>netty-all</artifactId> <artifactId>netty-all</artifactId>
<version>4.1.110.Final</version> <version>4.1.68.Final</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>

View File

@ -38,7 +38,6 @@ public class RIPService {
scheduler.scheduleAtFixedRate(() -> { scheduler.scheduleAtFixedRate(() -> {
RIPPacket packet = createRipResponsePacket(Config.RIP_VERSION); RIPPacket packet = createRipResponsePacket(Config.RIP_VERSION);
client.sendRipPacket(packet); client.sendRipPacket(packet);
System.out.println("客户端 发送了一次消息");
}, 0, 10, TimeUnit.SECONDS); }, 0, 10, TimeUnit.SECONDS);
System.out.println("客户端已经启动"); System.out.println("客户端已经启动");
} }

View File

@ -2,7 +2,7 @@ package cn.x47.config;
public class Config { public class Config {
// 设置协议版本1 表示 RIP v12 表示 RIP v2 // 设置协议版本1 表示 RIP v12 表示 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 startServer = true;
public static final boolean startClient = false; public static final boolean startClient = true;
} }

View File

@ -29,22 +29,26 @@ public class RIPPacketDecoder extends MessageToMessageDecoder<DatagramPacket> {
while (buf.readableBytes() >= 20) { while (buf.readableBytes() >= 20) {
RIPEntry entry = new RIPEntry(); RIPEntry entry = new RIPEntry();
entry.setAddressFamily(buf.readShort()); entry.setAddressFamily(buf.readShort());
if (ripPacket.getVersion() == 2) {
entry.setRouteTag(buf.readShort()); entry.setRouteTag(buf.readShort());
} else {
entry.setRouteTag((short) 0);
}
byte[] ipBytes = new byte[4]; byte[] ipBytes = new byte[4];
buf.readBytes(ipBytes); buf.readBytes(ipBytes);
entry.setIpAddress(InetAddress.getByAddress(ipBytes)); entry.setIpAddress(InetAddress.getByAddress(ipBytes));
if (ripPacket.getVersion() == 2) {
buf.readBytes(ipBytes); buf.readBytes(ipBytes);
entry.setSubnetMask(InetAddress.getByAddress(ipBytes)); entry.setSubnetMask(InetAddress.getByAddress(ipBytes));
buf.readBytes(ipBytes); buf.readBytes(ipBytes);
entry.setNextHop(InetAddress.getByAddress(ipBytes)); entry.setNextHop(InetAddress.getByAddress(ipBytes));
if (ripPacket.getVersion() == 1) { } else {
// 对于 RIP v1子网掩码和下一跳需要推断或设为默认值 // 对于 RIP v1子网掩码和下一跳需要推断或设为默认值
entry.setSubnetMask(InetAddress.getByName("255.255.255.0")); // 示例实际应根据 IP 类推断 entry.setSubnetMask(InetAddress.getByName("255.255.255.0")); // 示例实际应根据 IP 类推断
entry.setNextHop(packet.sender().getAddress()); entry.setNextHop(packet.sender().getAddress());
} }
entry.setMetric((int) buf.readUnsignedInt()); entry.setMetric(buf.readInt());
entries.add(entry); entries.add(entry);
} }
ripPacket.setEntries(entries); ripPacket.setEntries(entries);

View File

@ -11,7 +11,6 @@ public class RIPServerHandler extends SimpleChannelInboundHandler<RIPPacket> {
@Override @Override
protected void channelRead0(ChannelHandlerContext ctx, RIPPacket msg) throws Exception { protected void channelRead0(ChannelHandlerContext ctx, RIPPacket msg) throws Exception {
System.out.println(msg);
if (msg.getCommand() == 2) { // Response if (msg.getCommand() == 2) { // Response
for (RIPEntry entry : msg.getEntries()) { for (RIPEntry entry : msg.getEntries()) {
// 更新本地路由表 // 更新本地路由表

View File

@ -11,6 +11,9 @@ import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.DatagramChannel; import io.netty.channel.socket.DatagramChannel;
import io.netty.channel.socket.nio.NioDatagramChannel; import io.netty.channel.socket.nio.NioDatagramChannel;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class RIPClient { public class RIPClient {
@ -33,7 +36,8 @@ public class RIPClient {
public void sendRipPacket(RIPPacket packet) { public void sendRipPacket(RIPPacket packet) {
try { try {
ChannelFuture future = bootstrap.bind(0).sync(); ChannelFuture future = bootstrap.bind(0)
.sync();
future.channel().writeAndFlush(packet).sync(); future.channel().writeAndFlush(packet).sync();
future.channel().close(); future.channel().close();
} catch (InterruptedException e) { } catch (InterruptedException e) {

View File

@ -4,9 +4,7 @@ package cn.x47.service;
import cn.x47.handle.RIPPacketDecoder; import cn.x47.handle.RIPPacketDecoder;
import cn.x47.handle.RIPServerHandler; import cn.x47.handle.RIPServerHandler;
import io.netty.bootstrap.Bootstrap; import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture; import io.netty.channel.*;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.DatagramChannel; import io.netty.channel.socket.DatagramChannel;
import io.netty.channel.socket.nio.NioDatagramChannel; import io.netty.channel.socket.nio.NioDatagramChannel;