- 2023-10-10 10:11:36
- 6852 热度
- 0 评论
之前写了一篇关于 SNMP 修改SNMP4J消息内容 的博客,但是当时只是针对UDP的,后来应用到TCP上后发现有一些问题,现在说一下解决方法
上一篇连接地址:http://cuisuqiang.iteye.com/blog/1584391
使用TCP发送时,由于Socket不知道数据流的长度,所以要增加数据包长度到包上,这才是真正的需求,是我上次理解错了
增加头信息与上一次一样,直接在发送时增加即可
找到类:DefaultTcpTransportMapping
找到发送方法:
public void sendMessage(Address address, byte[] message)
在发送时增加消息头
/** * Sends a SNMP message to the supplied address. * @param address * an <code>TcpAddress</code>. A <code>ClassCastException</code> is thrown * if <code>address</code> is not a <code>TcpAddress</code> instance. * @param message byte[] * the message to sent. * @throws IOException */ public void sendMessage(Address address, byte[] message) throws java.io.IOException { if (server == null) { listen(); } // TODO 增加头字段 short length = (short)(message.length + 2); byte[] btlength = shortToByte(length); byte[] relmess = new byte[length]; System.arraycopy(btlength, 0, relmess, 0, 2); System.arraycopy(message, 0, relmess, 2, message.length); serverThread.sendMessage(address, relmess); }
但是在接收时就遇到一个问题,底层接收后会直接验证数据流,如果你增加头信息,就不算是标准SNMP包了,SNMP4J就不会接收不会处理
在类:DefaultTcpTransportMapping 中有一个内部类:
class ServerThread implements WorkerTask
方法
private void readMessage(SelectionKey sk, SocketChannel readChannel, TcpAddress incomingAddress) throws IOException
提供对于数据流的处理,处理后将数据流返回到上层进行解析
在方法中有这样有这样的代码:
ByteBuffer byteBuffer = ByteBuffer.wrap(buf); byteBuffer.limit(messageLengthDecoder.getMinHeaderLength());
MessageLengthDecoder 接口用于标识SNMP数据包的一些属性解析方法,包括最小头字节和获得 MessageLength 对象
如果数据小于最小字节,那么直接抛弃不解析
同样在TCP发送类中有一个内部类来实现这个接口:
public static class SnmpMesssageLengthDecoder implements MessageLengthDecoder { public int getMinHeaderLength() { return MIN_SNMP_HEADER_LENGTH; } public MessageLength getMessageLength(ByteBuffer buf) throws IOException { MutableByte type = new MutableByte(); BERInputStream is = new BERInputStream(buf); int ml = BER.decodeHeader(is, type); int hl = (int)is.getPosition(); MessageLength messageLength = new MessageLength(hl, ml); return messageLength; } }
由于我们增加两个字节的长度,所以要重新写一个实现的接口
// TODO 自己定义的头解析 public static class SnmpMesssageLengthDecoderMe implements MessageLengthDecoder { public int getMinHeaderLength() { return MIN_SNMP_HEADER_LENGTH + 2; } public MessageLength getMessageLength(ByteBuffer buf) throws IOException { int hl = 4; int ml = decodeHeader(buf); MessageLength messageLength = new MessageLength(hl, ml); return messageLength; } public int decodeHeader(ByteBuffer is){ byte[] bt = is.array(); byte[] len = new byte[]{ bt[0], bt[1] }; return byteToShort(len) - 4; } /** * @功能 字节的转换与短整型 * @return 短整型 */ public synchronized static short byteToShort(byte[] b) { short s = 0; short s0 = (short) (b[1] & 0xff);// 最低位 short s1 = (short) (b[0] & 0xff); s1 <<= 8; s = (short) (s0 | s1); return s; } }
长度就是原来的 +2 ,MessageLength 的属性有两个,按照我的写法写可以解决问题,但是我不明白
int ml = BER.decodeHeader(is, type);
的含义,希望有明白的给说明一下
消息的长度实际上就是我们定义的头表示的长度 -4
这样流读取方法才能把我们发送的数据流完整读取出来
上次在转发到上层解析时的方法也要修改,因为现在修改的只是正常读取到所有的流,而返给上层的数据流是要截取掉这两个字节的
@SuppressWarnings("static-access") private void dispatchMessage(TcpAddress incomingAddress, ByteBuffer byteBuffer, long bytesRead) { byteBuffer.flip(); if (logger.isDebugEnabled()) { logger.debug("Received message from " + incomingAddress + " with length " + bytesRead + ": " + new OctetString(byteBuffer.array(), 0, (int)bytesRead).toHexString()); } ByteBuffer bis; if (isAsyncMsgProcessingSupported()) { byte[] bytes = new byte[(int)bytesRead - 2]; System.arraycopy(byteBuffer.array(), 2, bytes, 0, (int)bytesRead - 2); bis = ByteBuffer.wrap(bytes); } else { byte[] bytes = new byte[(int)bytesRead - 2]; System.arraycopy(byteBuffer.array(), 2, bytes, 0, (int)bytesRead - 2); bis = ByteBuffer.wrap(bytes); } // TODO 调用解析数据流 fireProcessMessage(incomingAddress, bis); }
这样就可以正常解析增加两个字节的数据包
0 评论
留下评论
热门标签
- Spring(403)
- Boot(208)
- Spring Boot(187)
- Java(82)
- Cloud(82)
- Spring Cloud(82)
- Security(60)
- Spring Security(54)
- Boot2(51)
- Spring Boot2(51)
- Redis(31)
- SQL(29)
- Mysql(25)
- IDE(24)
- Dalston(24)
- mongoDB(22)
- MVC(22)
- JDBC(22)
- IDEA(22)
- Web(21)
- CLI(20)
- Alibaba(19)
- SpringMVC(19)
- SpringBoot(17)
- Docker(17)
- Eclipse(16)
- Vue(16)
- Git(16)
- JPA(15)
- Apache(15)
- ORA(15)
- Oracle(14)
- jdk(14)
- Tomcat(14)
- Linux(14)
- HTTP(14)
- Mybatis(14)
- XML(13)
- JdbcTemplate(13)
- OAuth(13)
- Nacos(13)
- Pro(13)
- JSON(12)
- OAuth2(12)
- Data(12)
- int(11)
- Myeclipse(11)
- stream(11)
- not(10)
- Bug(10)
- maven(9)
- Map(9)
- Hystrix(9)
- ast(9)
- session(8)
- Window(8)
- Swagger(8)
- APP(8)
- Bit(8)
- API(8)
- Cache(7)
- File(7)
- IntelliJ(7)
- mail(7)
- windows(7)
- too(7)
- HTML(7)
- Github(7)
- JavaMail(7)
- Log4J(6)
- pushlet(6)
- apt(6)
- Freemarker(6)
- read(6)
- WebFlux(6)
- JSP(6)
- Bean(6)
- error(6)
- Server(6)
- nginx(6)
- jar(6)
- ueditor(6)
- ehcache(6)
- UDP(6)
- RabbitMQ(6)
- star(6)
- and(6)
- Excel(6)
- string(5)
- script(5)
- Syntaxhighlighter(5)
- Tool(5)
- Controller(5)
- swagger2(5)
- ldquo(5)
- input(5)
- Servlet(5)
- Config(5)
- discuz(5)
- Emlog(5)