List of usage examples for io.netty.buffer ByteBuf getUnsignedByte
public abstract short getUnsignedByte(int index);
From source file:com.vethrfolnir.game.network.mu.crypt.MuEncoder.java
License:Open Source License
public static ByteBuf EncodePacket(ByteBuf buff, int serial) { int header = GetHeaderSize(buff); int packetSize = GetPacketSize(buff); int contentSize = packetSize - header; int encodedSize = (((contentSize / 8) + (((contentSize % 8) > 0) ? 1 : 0)) * 11) + header; int size = header; int originalHead = buff.getUnsignedByte(0); ByteBuf out = alloc.heapBuffer(encodedSize, encodedSize); //buff.writerIndex(buff.writerIndex() + 1); short[] Contents = new short[contentSize + 1]; Contents[0] = (short) serial; // XXX: Check this buff.readerIndex(header - 1);/* w w w. j av a 2s.c o m*/ buff.setByte(header - 1, serial); MuCryptUtils.readAsUByteArray(buff, Contents); //System.out.println("Encoding: "+PrintData.printData(Contents)); size += EncodeBuffer(Contents, out, header, (contentSize + 1)); out.writerIndex(0); // Header out.writeByte(originalHead); // Size write switch (originalHead) { case 0xc3: out.writeByte(size); break; case 0xC4: out.writeByte(size >> 8); out.writeByte(size & 0xFF); break; } out.writerIndex(size); return out; }
From source file:com.vethrfolnir.game.network.mu.MuCyperDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (!in.isReadable()) { MuClient client = ctx.channel().attr(MuClient.ClientKey).get(); _log.warn("Client[" + client + "] sent an empty packet!"); return; //XXX: is it critical? }// ww w.ja v a 2s . c o m if (in.readableBytes() < 3) { return; // come back later } in.markReaderIndex(); int opcode = in.readUnsignedByte(); int lengthAt = 0; switch (opcode) { case 0xc1: case 0xc3: lengthAt = 1; break; case 0xc2: case 0xc4: lengthAt = 2; break; } //in.markReaderIndex(); int rez = lengthAt > 1 ? in.readShort() : in.readUnsignedByte(); in.resetReaderIndex(); //System.out.println("1 Size[point="+(lengthAt > 1 ? "Short" : "Unsigned byte")+"]: "+rez+" opcode "+Integer.toHexString(opcode & 0xFF)); if (in.readableBytes() < rez) { in.resetReaderIndex(); return; } int header = in.getUnsignedByte(0); if (header == 0xC1 || header == 0xC2) { ByteBuf buff = ctx.alloc().heapBuffer(rez); in.readBytes(buff); out.add(DecodeXor32(buff)); } else { out.add(DecodePacket(in)); } }
From source file:com.vethrfolnir.login.network.mu.ClientChannelHandler.java
License:Open Source License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf buff = (ByteBuf) msg; switch (buff.getUnsignedByte(0)) { case 0xC1:/*from w w w.ja v a2 s . c o m*/ case 0xC2: case 0xC3: case 0xC4: break; default: ctx.close(); buff.release(); //TODO: maybe add a flood protector? log.warn("Client[" + ctx.channel() + "] is not a mu online client! Disconnecting!"); return; } // we are not interested in the header and size; buff.readerIndex(2); int opcode = buff.readUnsignedShort(); switch (opcode) { case 0xf4_03: { // Request Server information int serverId = buff.readUnsignedByte(); ByteBuf buf = ctx.alloc().heapBuffer().order(ByteOrder.LITTLE_ENDIAN); WritePacket packet = SendServerInfo.StaticPacket; packet.write(null, buf, nameService, serverId); packet.markLength(buf); ; ctx.writeAndFlush(buf); break; } case 0xf4_06: { // Request Server list ByteBuf buf = ctx.alloc().heapBuffer().order(ByteOrder.LITTLE_ENDIAN); WritePacket packet = SendServerLists.StaticPacket; packet.write(null, buf, nameService); packet.markLength(buf); ctx.writeAndFlush(buf); break; } default: log.warn("Unkown packet[OPCODE = " + Integer.toHexString(opcode) + "] Dump: " + PrintData.printData(buff.nioBuffer(0, buff.writerIndex()))); ctx.close(); break; } buff.release(); }
From source file:com.vethrfolnir.network.WritePacket.java
License:Open Source License
public void markLength(ByteBuf buff) { int lenght = buff.writerIndex(); switch (buff.getUnsignedByte(0)) { case 0xC1://from w w w. j a va2s . c o m case 0xC3: buff.setByte(1, lenght); break; case 0xC2: case 0xC4: buff.setByte(1, lenght >> 8); buff.setByte(2, lenght & 0xFF); break; } }
From source file:com.vethrfolnir.network.WritePacket.java
License:Open Source License
public boolean isEncryptable(ByteBuf buff) { switch (buff.getUnsignedByte(0)) { case 0xC3:// w ww . j a v a 2 s .c om case 0xC4: return true; } return false; }
From source file:divconq.net.ssl.SslHandler.java
License:Apache License
private static int getEncryptedPacketLength(ByteBuf buffer, int offset) { int packetLength = 0; // SSLv3 or TLS - Check ContentType boolean tls;/* w w w . ja v a2 s. c o m*/ switch (buffer.getUnsignedByte(offset)) { case 20: // change_cipher_spec case 21: // alert case 22: // handshake case 23: // application_data tls = true; break; default: // SSLv2 or bad data tls = false; } if (tls) { // SSLv3 or TLS - Check ProtocolVersion int majorVersion = buffer.getUnsignedByte(offset + 1); if (majorVersion == 3) { // SSLv3 or TLS packetLength = buffer.getUnsignedShort(offset + 3) + 5; if (packetLength <= 5) { // Neither SSLv3 or TLSv1 (i.e. SSLv2 or bad data) tls = false; } } else { // Neither SSLv3 or TLSv1 (i.e. SSLv2 or bad data) tls = false; } } if (!tls) { // SSLv2 or bad data - Check the version boolean sslv2 = true; int headerLength = (buffer.getUnsignedByte(offset) & 0x80) != 0 ? 2 : 3; int majorVersion = buffer.getUnsignedByte(offset + headerLength + 1); if (majorVersion == 2 || majorVersion == 3) { // SSLv2 if (headerLength == 2) { packetLength = (buffer.getShort(offset) & 0x7FFF) + 2; } else { packetLength = (buffer.getShort(offset) & 0x3FFF) + 3; } if (packetLength <= headerLength) { sslv2 = false; } } else { sslv2 = false; } if (!sslv2) { return -1; } } return packetLength; }
From source file:dpfmanager.shell.modules.server.core.HttpServerHandler.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { // Will use the first five bytes to detect a protocol. if (in.readableBytes() < 5) { in.clear();/* w w w . j a v a 2 s. c o m*/ ctx.close(); return; } final int magic1 = in.getUnsignedByte(in.readerIndex()); final int magic2 = in.getUnsignedByte(in.readerIndex() + 1); if (isPost(magic1, magic2) || isOptions(magic1, magic2)) { // POST ChannelPipeline pipeline = ctx.pipeline(); pipeline.addLast(new HttpRequestDecoder()); pipeline.addLast(new HttpResponseEncoder()); pipeline.addLast(new HttpPostHandler(context)); pipeline.remove(this); } else if (isGet(magic1, magic2)) { // GET ChannelPipeline pipeline = ctx.pipeline(); pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new HttpObjectAggregator(65536)); pipeline.addLast(new ChunkedWriteHandler()); pipeline.addLast(new HttpGetHandler(context)); pipeline.remove(this); } else { in.clear(); ctx.close(); } }
From source file:io.advantageous.conekt.dns.impl.netty.decoder.TextDecoder.java
License:Open Source License
/** * Returns a decoded TXT (text) resource record, stored as an * {@link java.util.ArrayList} of {@code String}s. * * @param response the DNS response that contains the resource record being * decoded/*from w w w . ja v a2s . co m*/ * @param resource the resource record being decoded */ @Override public List<String> decode(DnsResponse response, DnsResource resource) { List<String> list = new ArrayList<>(); ByteBuf data = resource.content().readerIndex(response.originalIndex()); int index = data.readerIndex(); while (index < data.writerIndex()) { int len = data.getUnsignedByte(index++); list.add(data.toString(index, len, CharsetUtil.UTF_8)); index += len; } return list; }
From source file:io.advantageous.conekt.dns.impl.netty.DnsResponseDecoder.java
License:Open Source License
/** * Retrieves a domain name given a buffer containing a DNS packet without * advancing the readerIndex for the buffer. * * @param buf the byte buffer containing the DNS packet * @param offset the position at which the name begins * @return the domain name for an entry/* ww w. j a v a 2 s .co m*/ */ public static String getName(ByteBuf buf, int offset) { StringBuilder name = new StringBuilder(); for (int len = buf.getUnsignedByte(offset++); buf.writerIndex() > offset && len != 0; len = buf.getUnsignedByte(offset++)) { boolean pointer = (len & 0xc0) == 0xc0; if (pointer) { offset = (len & 0x3f) << 8 | buf.getUnsignedByte(offset++); } else { name.append(buf.toString(offset, len, CharsetUtil.UTF_8)).append("."); offset += len; } } if (name.length() == 0) { return null; } return name.substring(0, name.length() - 1); }
From source file:io.airlift.drift.transport.netty.server.OptionalSslHandler.java
License:Apache License
private static boolean isTls(ByteBuf buffer, int offset) { // SSLv3 or TLS - Check ContentType int contentType = buffer.getUnsignedByte(offset); if (contentType != SSL_CONTENT_TYPE_CHANGE_CIPHER_SPEC && contentType != SSL_CONTENT_TYPE_ALERT && contentType != SSL_CONTENT_TYPE_HANDSHAKE && contentType != SSL_CONTENT_TYPE_APPLICATION_DATA) { return false; }/* ww w. j ava 2 s . c o m*/ // SSLv3 or TLS - Check ProtocolVersion int majorVersion = buffer.getUnsignedByte(offset + 1); if (majorVersion != 3) { return false; } // SSLv3 or TLS - Check packet length is positive if (buffer.getUnsignedShort(offset + 3) <= 0) { return false; } return true; }