Shift echo service to new internal api, ensure local services can respond to local clients

This commit is contained in:
Jeremy Lakeman 2014-01-29 15:39:12 +10:30
parent 487df0408d
commit 6f75d93460
4 changed files with 36 additions and 55 deletions

View File

@ -766,11 +766,20 @@ static struct overlay_buffer * encrypt_payload(
}
// encrypt or sign the plaintext, then queue the frame for transmission.
static int overlay_send_frame(struct internal_mdp_header *header,
int overlay_send_frame(struct internal_mdp_header *header,
struct overlay_buffer *payload)
{
if (header->destination && header->destination->reachable == REACHABLE_SELF)
return 0;
if ((!header->destination) || header->destination->reachable == REACHABLE_SELF){
ob_checkpoint(payload);
overlay_saw_mdp_frame(header, payload);
ob_rewind(payload);
if (header->destination) {
/* Is local, and is not broadcast, so shouldn't get sent out on the wire. */
if (config.debug.mdprequests)
DEBUGF("Local packet, not transmitting");
return 0;
}
}
if (header->ttl == 0)
header->ttl = PAYLOAD_TTL_DEFAULT;
@ -992,20 +1001,6 @@ int overlay_mdp_dispatch(overlay_mdp_frame *mdp, struct socket_address *client)
RETURN(WHY("Not implemented"));
};
if (!header.destination || header.destination->reachable == REACHABLE_SELF){
/* Packet is addressed to us / broadcast, we should process it first. */
struct overlay_buffer *mdp_payload = ob_static(mdp->out.payload, mdp->out.payload_length);
ob_limitsize(mdp_payload, mdp->out.payload_length);
overlay_saw_mdp_frame(&header, mdp_payload);
ob_free(mdp_payload);
if (header.destination) {
/* Is local, and is not broadcast, so shouldn't get sent out on the wire. */
if (config.debug.mdprequests)
DEBUGF("[%u] Local packet, not transmitting", __d);
RETURN(0);
}
}
struct overlay_buffer *buff = ob_static(mdp->out.payload, mdp->out.payload_length);
ob_limitsize(buff, mdp->out.payload_length);
@ -1448,12 +1443,6 @@ static void mdp_process_packet(struct socket_address *client, struct mdp_header
if ((header->flags & MDP_FLAG_NO_SIGN) == 0)
internal_header.modifiers|=OF_CRYPTO_SIGNED;
if (!internal_header.destination || internal_header.destination->reachable==REACHABLE_SELF){
if (config.debug.mdprequests)
DEBUGF("Attempting to process mdp packet locally");
overlay_saw_mdp_frame(&internal_header, payload);
}
// construct, encrypt, sign and queue the packet
if (overlay_send_frame(
&internal_header,

View File

@ -222,47 +222,37 @@ int overlay_mdp_service_dnalookup(overlay_mdp_frame *mdp)
RETURN(0);
}
int overlay_mdp_service_echo(overlay_mdp_frame *mdp)
int overlay_mdp_service_echo(struct internal_mdp_header *header, struct overlay_buffer *payload)
{
/* Echo is easy: we swap the sender and receiver addresses (and thus port
numbers) and send the frame back. */
IN();
/* Swap addresses */
overlay_mdp_swap_src_dst(mdp);
mdp->out.ttl=0;
/* Prevent echo:echo connections and the resulting denial of service from triggering endless pongs. */
if (mdp->out.dst.port==MDP_PORT_ECHO) {
if (header->source_port == MDP_PORT_ECHO)
RETURN(WHY("echo loop averted"));
}
/* If the packet was sent to broadcast, then replace broadcast address
with our local address. For now just responds with first local address */
if (is_sid_t_broadcast(mdp->out.src.sid))
{
if (my_subscriber)
mdp->out.src.sid = my_subscriber->sid;
else
/* No local addresses, so put all zeroes */
mdp->out.src.sid = SID_ANY;
}
struct internal_mdp_header response_header;
bzero(&response_header, sizeof response_header);
response_header.source = header->destination;
response_header.source_port = MDP_PORT_ECHO;
response_header.destination = header->source;
response_header.destination_port = header->source_port;
response_header.qos = header->qos;
/* Always send PONGs auth-crypted so that the receipient knows
that they are genuine, and so that we avoid the extra cost
of signing (which is slower than auth-crypting) */
int preserved=mdp->packetTypeAndFlags;
mdp->packetTypeAndFlags&=~(MDP_NOCRYPT|MDP_NOSIGN);
response_header.modifiers = OF_CRYPTO_CIPHERED|OF_CRYPTO_SIGNED;
/* queue frame for delivery */
overlay_mdp_dispatch(mdp, NULL);
mdp->packetTypeAndFlags=preserved;
/* and switch addresses back around in case the caller was planning on
using MDP structure again (this happens if there is a loop-back reply
and the frame needs sending on, as happens with broadcasts. MDP ping
is a simple application where this occurs). */
overlay_mdp_swap_src_dst(mdp);
RETURN(0);
/* If the packet was sent to broadcast, then replace broadcast address
with our local address. */
if (!response_header.source)
response_header.source = my_subscriber;
RETURN(overlay_send_frame(&response_header, payload));
OUT();
}
static int overlay_mdp_service_trace(overlay_mdp_frame *mdp){
@ -372,6 +362,7 @@ static int overlay_mdp_service_manifest_requests(struct internal_mdp_header *hea
void overlay_mdp_bind_internal_services()
{
mdp_bind_internal(NULL, MDP_PORT_LINKSTATE, link_receive);
mdp_bind_internal(NULL, MDP_PORT_ECHO, overlay_mdp_service_echo);
mdp_bind_internal(NULL, MDP_PORT_RHIZOME_REQUEST, overlay_mdp_service_rhizomerequest);
mdp_bind_internal(NULL, MDP_PORT_RHIZOME_MANIFEST_REQUEST, overlay_mdp_service_manifest_requests);
mdp_bind_internal(NULL, MDP_PORT_RHIZOME_SYNC, overlay_mdp_service_rhizome_sync);
@ -394,9 +385,6 @@ int overlay_mdp_try_internal_services(
case MDP_PORT_DNALOOKUP:
overlay_mdp_fill_legacy(header, payload, &mdp);
RETURN(overlay_mdp_service_dnalookup(&mdp));
case MDP_PORT_ECHO:
overlay_mdp_fill_legacy(header, payload, &mdp);
RETURN(overlay_mdp_service_echo(&mdp));
case MDP_PORT_TRACE:
overlay_mdp_fill_legacy(header, payload, &mdp);
RETURN(overlay_mdp_service_trace(&mdp));

View File

@ -418,6 +418,7 @@ int overlay_mdp_swap_src_dst(overlay_mdp_frame *mdp);
int overlay_mdp_dispatch(overlay_mdp_frame *mdp, struct socket_address *client);
void overlay_mdp_encode_ports(struct overlay_buffer *plaintext, mdp_port_t dst_port, mdp_port_t src_port);
int overlay_mdp_dnalookup_reply(const sockaddr_mdp *dstaddr, const sid_t *resolved_sidp, const char *uri, const char *did, const char *name);
int overlay_send_frame(struct internal_mdp_header *header, struct overlay_buffer *payload);
void overlay_mdp_fill_legacy(
const struct internal_mdp_header *header,

View File

@ -111,6 +111,8 @@ test_single_link() {
wait_until --timeout=10 path_exists +A +B
wait_until --timeout=5 path_exists +B +A
set_instance +A
executeOk_servald mdp ping --timeout=3 $SIDA 1
tfw_cat --stdout --stderr
executeOk_servald mdp ping --timeout=3 $SIDB 1
tfw_cat --stdout --stderr
}
@ -400,6 +402,7 @@ test_single_filter() {
wait_until --timeout=10 has_link --unicast $SIDB
set_instance +B
wait_until --timeout=5 has_link --broadcast $SIDA
set_instance +A
executeOk_servald mdp ping --timeout=3 $SIDB 1
tfw_cat --stdout --stderr
}