This commit is contained in:
地平线
2015-09-15 20:42:36 +08:00
parent 1da62db6ab
commit 28d65644fe
5 changed files with 31 additions and 42 deletions

View File

@@ -23,7 +23,6 @@ public class BindingIcepServlet extends IcepServlet {
StunPacket packet = request.getStunPacket(); StunPacket packet = request.getStunPacket();
ByteBuffer buffer = response.getContext().pollBuffer(); ByteBuffer buffer = response.getContext().pollBuffer();
packet.addAttribute(new XorMappedAddressAttribute(request.getRemoteAddress())); packet.addAttribute(new XorMappedAddressAttribute(request.getRemoteAddress()));
packet.addAttribute(new MappedAddressAttribute(request.getRemoteAddress()));
packet.getHeader().setRequestid((StunHeader.TYPE_SUCCESS | StunHeader.ACTION_BINDING)); packet.getHeader().setRequestid((StunHeader.TYPE_SUCCESS | StunHeader.ACTION_BINDING));
packet.encode(buffer); packet.encode(buffer);
buffer.flip(); buffer.flip();
@@ -31,4 +30,9 @@ public class BindingIcepServlet extends IcepServlet {
response.finish(buffer); response.finish(buffer);
} }
@Override
public short getRequestid() {
return 0x0001;
}
} }

View File

@@ -16,10 +16,11 @@ import java.util.*;
*/ */
public class IcepPrepareServlet extends PrepareServlet<IcepRequest, IcepResponse> { public class IcepPrepareServlet extends PrepareServlet<IcepRequest, IcepResponse> {
private final HashMap<Integer, IcepServlet> servletmaps = new HashMap<>(); private final HashMap<Short, IcepServlet> servletmaps = new HashMap<>();
public IcepPrepareServlet() { public IcepPrepareServlet() {
this.servletmaps.put(0x0001, new BindingIcepServlet()); BindingIcepServlet servlet = new BindingIcepServlet();
this.servletmaps.put(servlet.getRequestid(), new BindingIcepServlet());
} }
@Override @Override

View File

@@ -17,7 +17,7 @@ import java.nio.*;
*/ */
public class IcepRequest extends Request { public class IcepRequest extends Request {
private int requestid; private short requestid;
private StunPacket stunPacket; private StunPacket stunPacket;
@@ -31,12 +31,10 @@ public class IcepRequest extends Request {
Utility.println(Utility.now() + "-------" + getRemoteAddress() + " ", buffer); Utility.println(Utility.now() + "-------" + getRemoteAddress() + " ", buffer);
if (buffer.remaining() < 20) return -1; if (buffer.remaining() < 20) return -1;
this.requestid = buffer.getShort(); this.requestid = buffer.getShort();
short typeid = (short) ((this.requestid >> 8) & 0xffff); char bodysize = buffer.getChar();
short actionid = (short) (this.requestid & 0xffff); byte[] bytes = new byte[16];
int bodysize = buffer.getShort() & 0xffff;
byte[] bytes = new byte[16];
buffer.get(bytes); buffer.get(bytes);
StunHeader header = new StunHeader(typeid, actionid, bytes); StunHeader header = new StunHeader(this.requestid, bytes);
this.stunPacket = new StunPacket(header); this.stunPacket = new StunPacket(header);
return 0; return 0;
} }
@@ -59,7 +57,7 @@ public class IcepRequest extends Request {
} }
public int getRequestid() { public short getRequestid() {
return requestid; return requestid;
} }

View File

@@ -16,6 +16,8 @@ public abstract class IcepServlet implements Servlet<IcepRequest, IcepResponse>
AnyValue conf; //当前HttpServlet的配置 AnyValue conf; //当前HttpServlet的配置
public abstract short getRequestid();
@Override @Override
public final boolean equals(Object obj) { public final boolean equals(Object obj) {
return obj != null && obj.getClass() == this.getClass(); return obj != null && obj.getClass() == this.getClass();

View File

@@ -47,11 +47,9 @@ public class StunHeader implements IcepCoder<StunHeader> {
public static final short ACTION_CHANNELBIND = 0x0009; public static final short ACTION_CHANNELBIND = 0x0009;
//----------------------------------------------------------- //-----------------------------------------------------------
private short typeid; //无符号 2bytes private short requestid; //无符号 2bytes 首位2bits必定是00 所以该值不会为负数
private short actionid; //无符号 2bytes private char bodysize; //无符号 2bytes
private int bodysize; //无符号 2bytes
private byte[] transactionid; //RFC5389 =MAGIC_COOKIE + byte[12] = byte[16]; private byte[] transactionid; //RFC5389 =MAGIC_COOKIE + byte[12] = byte[16];
@@ -67,18 +65,15 @@ public class StunHeader implements IcepCoder<StunHeader> {
return transactions; return transactions;
} }
public StunHeader(short typeid, short actionid, byte[] transactionid0) { public StunHeader(short requestid, byte[] transactionid0) {
this.typeid = typeid; this.requestid = requestid;
this.actionid = actionid;
this.transactionid = transactionid0 == null ? generateTransactionid() : transactionid0; this.transactionid = transactionid0 == null ? generateTransactionid() : transactionid0;
} }
@Override @Override
public StunHeader decode(final ByteBuffer buffer) { public StunHeader decode(final ByteBuffer buffer) {
short requestid = buffer.getShort(); this.requestid = buffer.getShort();
this.typeid = (short) (requestid << 2); this.bodysize = buffer.getChar();
this.actionid = (short) (requestid & 0xff);
this.bodysize = buffer.getShort() & 0xffff;
this.transactionid = new byte[16]; this.transactionid = new byte[16];
buffer.get(transactionid); buffer.get(transactionid);
return this; return this;
@@ -86,17 +81,15 @@ public class StunHeader implements IcepCoder<StunHeader> {
@Override @Override
public ByteBuffer encode(final ByteBuffer buffer) { public ByteBuffer encode(final ByteBuffer buffer) {
buffer.put((byte) this.typeid); buffer.putShort(this.requestid);
buffer.put((byte) this.actionid); buffer.putChar(this.bodysize);
buffer.putShort((short) this.bodysize); //bodysize
buffer.put(transactionid); buffer.put(transactionid);
return buffer; return buffer;
} }
@Override @Override
public String toString() { public String toString() {
return this.getClass().getSimpleName() + "[typeid = " + format(typeid) + ", actionid = " + format(actionid) return this.getClass().getSimpleName() + "[requestid = " + format(requestid) + ", bodysize = " + (int) (bodysize) + ", transactionid = " + Utility.binToHexString(transactionid) + "]";
+ ", bodysize = " + (int) (bodysize) + ", transactionid = " + Utility.binToHexString(transactionid) + "]";
} }
private static String format(short value) { private static String format(short value) {
@@ -108,28 +101,19 @@ public class StunHeader implements IcepCoder<StunHeader> {
} }
public void setRequestid(int requestid) { public void setRequestid(int requestid) {
this.typeid = (short) (requestid >> 8); this.requestid = (short) requestid;
this.actionid = (short) (requestid & 0xff); }
public void setRequestid(short requestid) {
this.requestid = requestid;
} }
public void setBodysize(char bodysize) { public void setBodysize(char bodysize) {
this.bodysize = bodysize; this.bodysize = bodysize;
} }
public void setTypeid(short typeid) { public void setBodysize(int bodysize) {
this.typeid = typeid; this.bodysize = (char) bodysize;
}
public void setActionid(short actionid) {
this.actionid = actionid;
}
public short getTypeid() {
return typeid;
}
public short getActionid() {
return actionid;
} }
public int getBodysize() { public int getBodysize() {