Add MDP_INTERFACE_PEER for external unicast peer discovery to trigger a probe

This commit is contained in:
Jeremy Lakeman 2018-05-15 10:57:58 +09:30
parent 0043e20b81
commit c2103e3b86
4 changed files with 40 additions and 10 deletions

View File

@ -51,6 +51,7 @@ public abstract class AbstractExternalInterface extends ChannelSelector.Handler
private static final int MDP_INTERFACE_UP=0; private static final int MDP_INTERFACE_UP=0;
private static final int MDP_INTERFACE_DOWN=1; private static final int MDP_INTERFACE_DOWN=1;
private static final int MDP_INTERFACE_RECV=2; private static final int MDP_INTERFACE_RECV=2;
private static final int MDP_INTERFACE_PEER=3;
public void up(String config) throws IOException { public void up(String config) throws IOException {
MdpPacket packet = new MdpPacket(); MdpPacket packet = new MdpPacket();
@ -71,16 +72,12 @@ public abstract class AbstractExternalInterface extends ChannelSelector.Handler
packet.send((DatagramChannel) socket.getChannel()); packet.send((DatagramChannel) socket.getChannel());
} }
public void receivedPacket(byte recvaddr[], byte recvbytes[]) throws IOException { private void deliverPacket(int type, byte recvaddr[], byte recvbytes[], int offset, int len) throws IOException{
receivedPacket(recvaddr, recvbytes, 0, recvbytes==null?0:recvbytes.length);
}
public void receivedPacket(byte recvaddr[], byte recvbytes[], int offset, int len) throws IOException {
if (!isUp) if (!isUp)
return; return;
MdpPacket packet = new MdpPacket(); MdpPacket packet = new MdpPacket();
packet.setRemotePort(MDP_INTERFACE); packet.setRemotePort(MDP_INTERFACE);
packet.payload.put((byte) MDP_INTERFACE_RECV); packet.payload.put((byte) type);
packet.payload.put((byte) recvaddr.length); packet.payload.put((byte) recvaddr.length);
packet.payload.put(recvaddr); packet.payload.put(recvaddr);
if (len>0) if (len>0)
@ -89,6 +86,17 @@ public abstract class AbstractExternalInterface extends ChannelSelector.Handler
packet.send((DatagramChannel) socket.getChannel()); packet.send((DatagramChannel) socket.getChannel());
} }
public void discovered(byte recvaddr[]) throws IOException{
deliverPacket(MDP_INTERFACE_PEER, recvaddr, null, 0, 0);
}
public void receivedPacket(byte recvaddr[], byte recvbytes[]) throws IOException {
deliverPacket(MDP_INTERFACE_RECV, recvaddr, recvbytes, 0, recvbytes==null?0:recvbytes.length);
}
public void receivedPacket(byte recvaddr[], byte recvbytes[], int offset, int len) throws IOException {
deliverPacket(MDP_INTERFACE_RECV, recvaddr, recvbytes, offset, len);
}
protected abstract void sendPacket(byte addr[], ByteBuffer payload); protected abstract void sendPacket(byte addr[], ByteBuffer payload);
@Override @Override

View File

@ -51,8 +51,7 @@ public class MdpSocket{
this.loopbackMdpPort = loopbackMdpPort; this.loopbackMdpPort = loopbackMdpPort;
} }
public MdpSocket(int loopbackMdpPort, int port) throws IOException { public MdpSocket(int loopbackMdpPort, int port) throws IOException {
this(loopbackMdpPort); this(loopbackMdpPort, SubscriberId.ANY, port);
bind(SubscriberId.ANY, port);
} }
public MdpSocket(int loopbackMdpPort, SubscriberId sid, int port) throws IOException { public MdpSocket(int loopbackMdpPort, SubscriberId sid, int port) throws IOException {
this(loopbackMdpPort); this(loopbackMdpPort);

View File

@ -99,6 +99,7 @@ struct mdp_identity_request {
#define MDP_INTERFACE_UP 0 #define MDP_INTERFACE_UP 0
#define MDP_INTERFACE_DOWN 1 #define MDP_INTERFACE_DOWN 1
#define MDP_INTERFACE_RECV 2 #define MDP_INTERFACE_RECV 2
#define MDP_INTERFACE_PEER 3
#define MDP_ROUTE_TABLE 5 #define MDP_ROUTE_TABLE 5

View File

@ -1427,13 +1427,35 @@ static void mdp_interface_packet(struct socket_address *client, struct mdp_heade
if (interface) if (interface)
overlay_interface_close(interface); overlay_interface_close(interface);
}break; }break;
case MDP_INTERFACE_PEER:{
// some form of external peer discovery has occurred
struct overlay_interface *interface=overlay_interface_find_name_file_addr(NULL, NULL, client);
if (interface){
// queue a unicast probe packet to this address
struct socket_address addr;
addr.addrlen = ob_get(payload);
if ((size_t)addr.addrlen > sizeof(addr)){
WARNF("Malformed MDP_INTERFACE_RECV header, %u bytes is too large", (unsigned)addr.addrlen);
break;
}
bcopy(ob_get_bytes_ptr(payload, addr.addrlen), addr.raw, addr.addrlen);
struct network_destination *destination = create_unicast_destination(&addr, state->interface);
if (!destination)
break;
overlay_send_probe(NULL, destination, OQ_ORDINARY);
release_destination_ref(destination);
}
break;
}
case MDP_INTERFACE_RECV:{ case MDP_INTERFACE_RECV:{
struct overlay_interface *interface=overlay_interface_find_name_file_addr(NULL, NULL, client); struct overlay_interface *interface=overlay_interface_find_name_file_addr(NULL, NULL, client);
if (interface){ if (interface){
struct socket_address addr; struct socket_address addr;
addr.addrlen = ob_get(payload); addr.addrlen = ob_get(payload);
if ((size_t)addr.addrlen > sizeof(addr)) if ((size_t)addr.addrlen > sizeof(addr)){
break; // TODO errors WARNF("Malformed MDP_INTERFACE_RECV header, %u bytes is too large", (unsigned)addr.addrlen);
break;
}
bcopy(ob_get_bytes_ptr(payload, addr.addrlen), addr.raw, addr.addrlen); bcopy(ob_get_bytes_ptr(payload, addr.addrlen), addr.raw, addr.addrlen);
packetOkOverlay(interface, ob_current_ptr(payload), ob_remaining(payload), &addr); packetOkOverlay(interface, ob_current_ptr(payload), ob_remaining(payload), &addr);
} }