From 231ab257e4307dd9dc2e094a8603ce4d29ca840d Mon Sep 17 00:00:00 2001 From: Jeremy Lakeman Date: Mon, 7 Mar 2016 15:48:23 +1030 Subject: [PATCH] Define internal port bindings with section linking tricks --- keyring.c | 3 +- keyring.h | 1 - overlay_link.c | 10 ++++--- overlay_mdp.c | 66 +++++------------------------------------- overlay_mdp_services.c | 32 +++++++------------- overlay_packet.h | 23 +++++++++++++++ rhizome.h | 1 - rhizome_sync.c | 3 +- route_link.c | 3 +- route_link.h | 1 - serval.h | 18 ------------ server.c | 2 -- vomp.c | 3 +- 13 files changed, 56 insertions(+), 110 deletions(-) diff --git a/keyring.c b/keyring.c index 75015bf4..782b86a9 100644 --- a/keyring.c +++ b/keyring.c @@ -1787,7 +1787,8 @@ static int keyring_process_challenge(keyring_file *k, struct subscriber *subscri return 0; } -int keyring_mapping_request(struct internal_mdp_header *header, struct overlay_buffer *payload) +DEFINE_BINDING(MDP_PORT_KEYMAPREQUEST, keyring_mapping_request); +static int keyring_mapping_request(struct internal_mdp_header *header, struct overlay_buffer *payload) { /* The authcryption of the MDP frame proves that the SAS key is owned by the diff --git a/keyring.h b/keyring.h index fc6c7aa8..0482216f 100644 --- a/keyring.h +++ b/keyring.h @@ -119,7 +119,6 @@ unsigned char *keyring_get_nm_bytes(const sid_t *known_sidp, const sid_t *unknow struct internal_mdp_header; struct overlay_buffer; -int keyring_mapping_request(struct internal_mdp_header *header, struct overlay_buffer *payload); int keyring_send_unlock(struct subscriber *subscriber); int keyring_release_subscriber(keyring_file *k, const sid_t *sid); diff --git a/overlay_link.c b/overlay_link.c index 112a0f51..e7c52fb8 100644 --- a/overlay_link.c +++ b/overlay_link.c @@ -147,8 +147,8 @@ struct network_destination *load_subscriber_address(struct subscriber *subscribe } /* Collection of unicast echo responses to detect working links */ -int -overlay_mdp_service_probe(struct internal_mdp_header *header, struct overlay_buffer *payload) +DEFINE_BINDING(MDP_PORT_PROBE, overlay_mdp_service_probe); +static int overlay_mdp_service_probe(struct internal_mdp_header *header, struct overlay_buffer *payload) { IN(); if (header->source_port!=MDP_PORT_ECHO){ @@ -230,7 +230,8 @@ static void overlay_append_unicast_address(struct subscriber *subscriber, struct } } -int overlay_mdp_service_stun_req(struct internal_mdp_header *header, struct overlay_buffer *payload) +DEFINE_BINDING(MDP_PORT_STUNREQ, overlay_mdp_service_stun_req); +static int overlay_mdp_service_stun_req(struct internal_mdp_header *header, struct overlay_buffer *payload) { DEBUGF(overlayrouting, "Processing STUN request from %s", alloca_tohex_sid_t(header->source->sid)); @@ -268,7 +269,8 @@ int overlay_mdp_service_stun_req(struct internal_mdp_header *header, struct over return 0; } -int overlay_mdp_service_stun(struct internal_mdp_header *header, struct overlay_buffer *payload) +DEFINE_BINDING(MDP_PORT_STUN, overlay_mdp_service_stun); +static int overlay_mdp_service_stun(struct internal_mdp_header *header, struct overlay_buffer *payload) { DEBUGF(overlayrouting, "Processing STUN info from %s", alloca_tohex_sid_t(header->source->sid)); diff --git a/overlay_mdp.c b/overlay_mdp.c index 8b7d6100..3357e382 100644 --- a/overlay_mdp.c +++ b/overlay_mdp.c @@ -239,7 +239,6 @@ struct mdp_binding{ struct subscriber *subscriber; mdp_port_t port; int version; - int (*internal)(struct internal_mdp_header *header, struct overlay_buffer *payload); struct socket_address client; time_ms_t binding_time; }; @@ -376,57 +375,6 @@ static int overlay_mdp_process_bind_request(struct subscriber *subscriber, mdp_p return 0; } -int mdp_bind_internal(struct subscriber *subscriber, mdp_port_t port, - int (*internal)(struct internal_mdp_header *header, struct overlay_buffer *payload)) -{ - - int i; - struct mdp_binding *free_slot=NULL; - - if (!mdp_bindings_initialised) { - /* Mark all slots as unused */ - int i; - for(i=0;isubscriber=subscriber; - free_slot->port=port; - free_slot->version=1; - free_slot->internal=internal; - free_slot->binding_time=gettime_ms(); - return 0; -} - -int mdp_unbind_internal(struct subscriber *subscriber, mdp_port_t port, - int (*internal)(struct internal_mdp_header *header, struct overlay_buffer *payload)) -{ - int i; - for(i=0;idestination?header->destination->sid:SID_BROADCAST; @@ -668,6 +613,14 @@ static int overlay_saw_mdp_frame( } } } else { + + // look for a compile time defined internal binding + struct internal_binding *binding; + for (binding = SECTION_START(bindings); binding < SECTION_END(bindings); ++binding) { + if (binding->port == header->destination_port) + RETURN(binding->function(header, payload)); + } + /* Unbound socket. We won't be sending ICMP style connection refused messages, partly because they are a waste of bandwidth. */ RETURN(WHYF("Received packet for which no listening process exists (MDP ports: src=%d, dst=%d", @@ -1555,7 +1508,6 @@ static void mdp_process_packet(struct socket_address *client, struct mdp_header case MDP_LISTEN: // double check that this binding belongs to this connection if (!binding - || binding->internal || cmp_sockaddr(&binding->client, client)!=0){ WHYF("That port is not bound by you %s vs %s", binding?alloca_socket_address(&binding->client):"(none)", @@ -1590,7 +1542,6 @@ static void mdp_process_packet(struct socket_address *client, struct mdp_header }else{ // double check that this binding belongs to this connection if (!binding - || binding->internal || !internal_header.source || header->local.port == 0 || cmp_sockaddr(&binding->client, client)!=0){ @@ -1617,7 +1568,6 @@ static void mdp_process_packet(struct socket_address *client, struct mdp_header // remove binding if (binding - && !binding->internal && header->flags & MDP_FLAG_CLOSE && cmp_sockaddr(&binding->client, client)==0){ DEBUGF(mdprequests, "Unbind MDP %s:%d from %s", diff --git a/overlay_mdp_services.c b/overlay_mdp_services.c index e463af2c..088e7f5a 100644 --- a/overlay_mdp_services.c +++ b/overlay_mdp_services.c @@ -34,7 +34,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "dataformats.h" #include "route_link.h" -int rhizome_mdp_send_block(struct subscriber *dest, const rhizome_bid_t *bid, uint64_t version, uint64_t fileOffset, uint32_t bitmap, uint16_t blockLength) +static int rhizome_mdp_send_block(struct subscriber *dest, const rhizome_bid_t *bid, uint64_t version, uint64_t fileOffset, uint32_t bitmap, uint16_t blockLength) { IN(); if (!is_rhizome_mdp_server_running()) @@ -116,7 +116,8 @@ int rhizome_mdp_send_block(struct subscriber *dest, const rhizome_bid_t *bid, ui OUT(); } -int overlay_mdp_service_rhizomerequest(struct internal_mdp_header *header, struct overlay_buffer *payload) +DEFINE_BINDING(MDP_PORT_RHIZOME_REQUEST, overlay_mdp_service_rhizomerequest); +static int overlay_mdp_service_rhizomerequest(struct internal_mdp_header *header, struct overlay_buffer *payload) { const rhizome_bid_t *bidp = (const rhizome_bid_t *) ob_get_bytes_ptr(payload, sizeof bidp->binary); // Note, was originally built using read_uint64 which has reverse byte order of ob_get_ui64 @@ -129,7 +130,8 @@ int overlay_mdp_service_rhizomerequest(struct internal_mdp_header *header, struc return rhizome_mdp_send_block(header->source, bidp, version, fileOffset, bitmap, blockLength); } -int overlay_mdp_service_rhizomeresponse(struct internal_mdp_header *UNUSED(header), struct overlay_buffer *payload) +DEFINE_BINDING(MDP_PORT_RHIZOME_RESPONSE, overlay_mdp_service_rhizomeresponse); +static int overlay_mdp_service_rhizomeresponse(struct internal_mdp_header *UNUSED(header), struct overlay_buffer *payload) { IN(); @@ -170,7 +172,8 @@ int overlay_mdp_service_rhizomeresponse(struct internal_mdp_header *UNUSED(heade OUT(); } -int overlay_mdp_service_dnalookup(struct internal_mdp_header *header, struct overlay_buffer *payload) +DEFINE_BINDING(MDP_PORT_DNALOOKUP, overlay_mdp_service_dnalookup); +static int overlay_mdp_service_dnalookup(struct internal_mdp_header *header, struct overlay_buffer *payload) { IN(); keyring_iterator it; @@ -230,7 +233,8 @@ int overlay_mdp_service_dnalookup(struct internal_mdp_header *header, struct ove RETURN(0); } -int overlay_mdp_service_echo(struct internal_mdp_header *header, struct overlay_buffer *payload) +DEFINE_BINDING(MDP_PORT_ECHO, overlay_mdp_service_echo); +static int overlay_mdp_service_echo(struct internal_mdp_header *header, struct overlay_buffer *payload) { IN(); @@ -265,6 +269,7 @@ int overlay_mdp_service_echo(struct internal_mdp_header *header, struct overlay_ * in situations where a routing protocol is in development. */ +DEFINE_BINDING(MDP_PORT_TRACE, overlay_mdp_service_trace); static int overlay_mdp_service_trace(struct internal_mdp_header *header, struct overlay_buffer *payload){ IN(); struct overlay_buffer *next_payload = ob_new(); @@ -355,6 +360,7 @@ end: RETURN(ret); } +DEFINE_BINDING(MDP_PORT_RHIZOME_MANIFEST_REQUEST, overlay_mdp_service_manifest_requests); static int overlay_mdp_service_manifest_requests(struct internal_mdp_header *header, struct overlay_buffer *payload) { while (ob_remaining(payload)) { @@ -375,19 +381,3 @@ static int overlay_mdp_service_manifest_requests(struct internal_mdp_header *hea return 0; } -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); - mdp_bind_internal(NULL, MDP_PORT_RHIZOME_RESPONSE, overlay_mdp_service_rhizomeresponse); - mdp_bind_internal(NULL, MDP_PORT_PROBE, overlay_mdp_service_probe); - mdp_bind_internal(NULL, MDP_PORT_STUNREQ, overlay_mdp_service_stun_req); - mdp_bind_internal(NULL, MDP_PORT_STUN, overlay_mdp_service_stun); - mdp_bind_internal(NULL, MDP_PORT_DNALOOKUP, overlay_mdp_service_dnalookup); - mdp_bind_internal(NULL, MDP_PORT_VOMP, vomp_mdp_received); - mdp_bind_internal(NULL, MDP_PORT_TRACE, overlay_mdp_service_trace); - mdp_bind_internal(NULL, MDP_PORT_KEYMAPREQUEST, keyring_mapping_request); -} diff --git a/overlay_packet.h b/overlay_packet.h index a5679622..2e2fe53e 100644 --- a/overlay_packet.h +++ b/overlay_packet.h @@ -22,6 +22,7 @@ #include "overlay_address.h" #include "serval_types.h" +#include "section.h" #define FRAME_NOT_SENT -1 #define FRAME_DONT_SEND -2 @@ -126,4 +127,26 @@ int reload_mdp_packet_rules(void); void frame_remove_destination(struct overlay_frame *frame, int i); void frame_add_destination(struct overlay_frame *frame, struct subscriber *next_hop, struct network_destination *dest); +void mdp_init_response(const struct internal_mdp_header *in, struct internal_mdp_header *out); +void overlay_mdp_encode_ports(struct overlay_buffer *plaintext, mdp_port_t dst_port, mdp_port_t src_port); +int overlay_mdp_dnalookup_reply(struct subscriber *dest, mdp_port_t dest_port, + struct subscriber *resolved_sid, const char *uri, const char *did, const char *name); + +int _overlay_send_frame(struct __sourceloc whence, struct internal_mdp_header *header, struct overlay_buffer *payload); +#define overlay_send_frame(H, P) _overlay_send_frame(__WHENCE__, H, P) + +struct internal_binding{ + mdp_port_t port; + int (*function)(struct internal_mdp_header *header, struct overlay_buffer *payload); +}; + +DECLARE_SECTION(struct internal_binding, bindings); + +#define DEFINE_BINDING(PORT, FUNC) \ + static int FUNC(struct internal_mdp_header *, struct overlay_buffer *);\ + static struct internal_binding BIND ## FUNC IN_SECTION(bindings) = { \ + .port = PORT, \ + .function = FUNC, \ + } + #endif //__SERVAL_DNA__OVERLAY_PACKET_H diff --git a/rhizome.h b/rhizome.h index 95a050f9..260af180 100644 --- a/rhizome.h +++ b/rhizome.h @@ -945,7 +945,6 @@ int rhizome_cache_close(); int rhizome_database_filehash_from_id(const rhizome_bid_t *bidp, uint64_t version, rhizome_filehash_t *hashp); -int overlay_mdp_service_rhizome_sync(struct internal_mdp_header *header, struct overlay_buffer *payload); void rhizome_sync_status(); DECLARE_ALARM(rhizome_fetch_status); diff --git a/rhizome_sync.c b/rhizome_sync.c index 85edaab9..9e7569c7 100644 --- a/rhizome_sync.c +++ b/rhizome_sync.c @@ -515,7 +515,8 @@ void rhizome_sync_announce(struct sched_ent *alarm) schedule(alarm); } -int overlay_mdp_service_rhizome_sync(struct internal_mdp_header *header, struct overlay_buffer *payload) +DEFINE_BINDING(MDP_PORT_RHIZOME_SYNC, overlay_mdp_service_rhizome_sync); +static int overlay_mdp_service_rhizome_sync(struct internal_mdp_header *header, struct overlay_buffer *payload) { if (!config.rhizome.enable || !rhizome_db) return 0; diff --git a/route_link.c b/route_link.c index b329ecf6..df455543 100644 --- a/route_link.c +++ b/route_link.c @@ -1339,7 +1339,8 @@ int link_received_packet(struct decode_context *context, int sender_seq, uint8_t } // parse incoming link details -int link_receive(struct internal_mdp_header *header, struct overlay_buffer *payload) +DEFINE_BINDING(MDP_PORT_LINKSTATE, link_receive); +static int link_receive(struct internal_mdp_header *header, struct overlay_buffer *payload) { IN(); diff --git a/route_link.h b/route_link.h index 9aa1bcf6..7328eec4 100644 --- a/route_link.h +++ b/route_link.h @@ -41,7 +41,6 @@ int link_state_ack_soon(struct subscriber *subscriber); int link_received_duplicate(struct decode_context *context, int payload_seq); int link_received_packet(struct decode_context *context, int sender_seq, uint8_t unicast); int link_unicast_ack(struct subscriber *subscriber, struct overlay_interface *interface, struct socket_address *addr); -int link_receive(struct internal_mdp_header *header, struct overlay_buffer *payload); void link_explained(struct subscriber *subscriber); void link_interface_down(struct overlay_interface *interface); int link_state_legacy_ack(struct overlay_frame *frame, time_ms_t now); diff --git a/serval.h b/serval.h index b2018010..cb980297 100644 --- a/serval.h +++ b/serval.h @@ -218,18 +218,6 @@ int rhizome_opendb(); int parseCommandLine(struct cli_context *context, const char *argv0, int argc, const char *const *argv); -/* Server-side MDP functions */ -void mdp_init_response(const struct internal_mdp_header *in, struct internal_mdp_header *out); -void overlay_mdp_encode_ports(struct overlay_buffer *plaintext, mdp_port_t dst_port, mdp_port_t src_port); -int overlay_mdp_dnalookup_reply(struct subscriber *dest, mdp_port_t dest_port, - struct subscriber *resolved_sid, const char *uri, const char *did, const char *name); -int _overlay_send_frame(struct __sourceloc whence, struct internal_mdp_header *header, struct overlay_buffer *payload); -#define overlay_send_frame(H, P) _overlay_send_frame(__WHENCE__, H, P) -int mdp_bind_internal(struct subscriber *subscriber, mdp_port_t port, - int (*internal)(struct internal_mdp_header *header, struct overlay_buffer *payload)); -int mdp_unbind_internal(struct subscriber *subscriber, mdp_port_t port, - int (*internal)(struct internal_mdp_header *header, struct overlay_buffer *payload)); - int allow_inbound_packet(const struct internal_mdp_header *header); int allow_outbound_packet(const struct internal_mdp_header *header); void load_mdp_packet_rules(const char *filename); @@ -239,7 +227,6 @@ struct vomp_call_state; void set_codec_flag(int codec, unsigned char *flags); struct vomp_call_state *vomp_find_call_by_session(unsigned int session_token); -int vomp_mdp_received(struct internal_mdp_header *header, struct overlay_buffer *payload); int vomp_parse_dtmf_digit(char c); int vomp_dial(struct subscriber *local, struct subscriber *remote, const char *local_did, const char *remote_did); int vomp_pickup(struct vomp_call_state *call); @@ -276,7 +263,6 @@ extern uint16_t mdp_loopback_port; int overlay_mdp_setup_sockets(); int overlay_packetradio_setup_port(struct overlay_interface *interface); -void overlay_mdp_bind_internal_services(); int overlay_send_probe(struct subscriber *peer, struct network_destination *destination, int queue); int overlay_send_stun_request(struct subscriber *server, struct subscriber *request); void rhizome_check_connections(struct sched_ent *alarm); @@ -288,10 +274,6 @@ void monitor_poll(struct sched_ent *alarm); void rhizome_fetch_poll(struct sched_ent *alarm); void rhizome_server_poll(struct sched_ent *alarm); -int overlay_mdp_service_stun_req(struct internal_mdp_header *header, struct overlay_buffer *payload); -int overlay_mdp_service_stun(struct internal_mdp_header *header, struct overlay_buffer *payload); -int overlay_mdp_service_probe(struct internal_mdp_header *header, struct overlay_buffer *payload); - int olsr_init_socket(void); int olsr_send(struct overlay_frame *frame); diff --git a/server.c b/server.c index 1c156fdd..035d3c02 100644 --- a/server.c +++ b/server.c @@ -338,8 +338,6 @@ static int server_bind() // Periodically check for server shut down RESCHEDULE(&ALARM_STRUCT(server_shutdown_check), now, TIME_MS_NEVER_WILL, now); - overlay_mdp_bind_internal_services(); - olsr_init_socket(); /* Calculate (and possibly show) CPU usage stats periodically */ diff --git a/vomp.c b/vomp.c index 7c9a6d56..b7ec1a1c 100644 --- a/vomp.c +++ b/vomp.c @@ -865,7 +865,8 @@ static int vomp_extract_remote_codec_list(struct vomp_call_state *call, struct o /* At this point we know the MDP frame is addressed to the VoMP port, but we have not inspected the contents. As these frames are wire-format, we must pay attention to endianness. */ -int vomp_mdp_received(struct internal_mdp_header *header, struct overlay_buffer *payload) +DEFINE_BINDING(MDP_PORT_VOMP, vomp_mdp_received); +static int vomp_mdp_received(struct internal_mdp_header *header, struct overlay_buffer *payload) { time_ms_t now = gettime_ms();