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();
ByteBuffer buffer = response.getContext().pollBuffer();
packet.addAttribute(new XorMappedAddressAttribute(request.getRemoteAddress()));
packet.addAttribute(new MappedAddressAttribute(request.getRemoteAddress()));
packet.getHeader().setRequestid((StunHeader.TYPE_SUCCESS | StunHeader.ACTION_BINDING));
packet.encode(buffer);
buffer.flip();
@@ -31,4 +30,9 @@ public class BindingIcepServlet extends IcepServlet {
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> {
private final HashMap<Integer, IcepServlet> servletmaps = new HashMap<>();
private final HashMap<Short, IcepServlet> servletmaps = new HashMap<>();
public IcepPrepareServlet() {
this.servletmaps.put(0x0001, new BindingIcepServlet());
BindingIcepServlet servlet = new BindingIcepServlet();
this.servletmaps.put(servlet.getRequestid(), new BindingIcepServlet());
}
@Override

View File

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

View File

@@ -16,6 +16,8 @@ public abstract class IcepServlet implements Servlet<IcepRequest, IcepResponse>
AnyValue conf; //当前HttpServlet的配置
public abstract short getRequestid();
@Override
public final boolean equals(Object obj) {
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;
//-----------------------------------------------------------
private short typeid; //无符号 2bytes
private short requestid; //无符号 2bytes 首位2bits必定是00 所以该值不会为负数
private short actionid; //无符号 2bytes
private int bodysize; //无符号 2bytes
private char bodysize; //无符号 2bytes
private byte[] transactionid; //RFC5389 =MAGIC_COOKIE + byte[12] = byte[16];
@@ -67,18 +65,15 @@ public class StunHeader implements IcepCoder<StunHeader> {
return transactions;
}
public StunHeader(short typeid, short actionid, byte[] transactionid0) {
this.typeid = typeid;
this.actionid = actionid;
public StunHeader(short requestid, byte[] transactionid0) {
this.requestid = requestid;
this.transactionid = transactionid0 == null ? generateTransactionid() : transactionid0;
}
@Override
public StunHeader decode(final ByteBuffer buffer) {
short requestid = buffer.getShort();
this.typeid = (short) (requestid << 2);
this.actionid = (short) (requestid & 0xff);
this.bodysize = buffer.getShort() & 0xffff;
this.requestid = buffer.getShort();
this.bodysize = buffer.getChar();
this.transactionid = new byte[16];
buffer.get(transactionid);
return this;
@@ -86,17 +81,15 @@ public class StunHeader implements IcepCoder<StunHeader> {
@Override
public ByteBuffer encode(final ByteBuffer buffer) {
buffer.put((byte) this.typeid);
buffer.put((byte) this.actionid);
buffer.putShort((short) this.bodysize); //bodysize
buffer.putShort(this.requestid);
buffer.putChar(this.bodysize);
buffer.put(transactionid);
return buffer;
}
@Override
public String toString() {
return this.getClass().getSimpleName() + "[typeid = " + format(typeid) + ", actionid = " + format(actionid)
+ ", bodysize = " + (int) (bodysize) + ", transactionid = " + Utility.binToHexString(transactionid) + "]";
return this.getClass().getSimpleName() + "[requestid = " + format(requestid) + ", bodysize = " + (int) (bodysize) + ", transactionid = " + Utility.binToHexString(transactionid) + "]";
}
private static String format(short value) {
@@ -108,28 +101,19 @@ public class StunHeader implements IcepCoder<StunHeader> {
}
public void setRequestid(int requestid) {
this.typeid = (short) (requestid >> 8);
this.actionid = (short) (requestid & 0xff);
this.requestid = (short) requestid;
}
public void setRequestid(short requestid) {
this.requestid = requestid;
}
public void setBodysize(char bodysize) {
this.bodysize = bodysize;
}
public void setTypeid(short typeid) {
this.typeid = typeid;
}
public void setActionid(short actionid) {
this.actionid = actionid;
}
public short getTypeid() {
return typeid;
}
public short getActionid() {
return actionid;
public void setBodysize(int bodysize) {
this.bodysize = (char) bodysize;
}
public int getBodysize() {