Simplify id peers command, hide mdp implementation details from node info

This commit is contained in:
Jeremy Lakeman 2012-10-03 16:21:06 +09:30
parent b72c01518b
commit 32cd46c165
4 changed files with 88 additions and 108 deletions

View File

@ -1400,8 +1400,6 @@ int app_id_self(int argc, const char *const *argv, struct command_line_option *o
else
return WHYF("unsupported arg '%s'", argv[1]);
a.addrlist.first_sid=0;
a.addrlist.last_sid=0x7fffffff;
a.addrlist.frame_sid_count=MDP_MAX_SID_REQUEST;
while(a.addrlist.frame_sid_count==MDP_MAX_SID_REQUEST) {
result=overlay_mdp_send(&a,MDP_AWAITREPLY,5000);

View File

@ -934,41 +934,63 @@ int overlay_mdp_dispatch(overlay_mdp_frame *mdp,int userGeneratedFrameP,
RETURN(0);
}
struct search_state{
overlay_mdp_frame *mdp;
overlay_mdp_frame *mdpreply;
int first;
int max;
int index;
int count;
};
static int search_subscribers(struct subscriber *subscriber, void *context){
struct search_state *state = context;
overlay_mdp_addrlist *response = context;
if (state->mdp->addrlist.mode == MDP_ADDRLIST_MODE_SELF && subscriber->reachable != REACHABLE_SELF){
if (response->mode == MDP_ADDRLIST_MODE_SELF && subscriber->reachable != REACHABLE_SELF){
return 0;
}
if (state->mdp->addrlist.mode == MDP_ADDRLIST_MODE_ROUTABLE_PEERS &&
if (response->mode == MDP_ADDRLIST_MODE_ROUTABLE_PEERS &&
(subscriber->reachable != REACHABLE_DIRECT &&
subscriber->reachable != REACHABLE_INDIRECT &&
subscriber->reachable != REACHABLE_UNICAST)){
return 0;
}
if (state->mdp->addrlist.mode == MDP_ADDRLIST_MODE_ALL_PEERS &&
if (response->mode == MDP_ADDRLIST_MODE_ALL_PEERS &&
subscriber->reachable == REACHABLE_SELF){
return 0;
}
if (state->count++ >= state->first && state->index < state->max) {
memcpy(state->mdpreply->addrlist.sids[state->index++], subscriber->sid, SID_SIZE);
if (response->server_sid_count++ >= response->first_sid &&
response->first_sid + response->frame_sid_count < response->last_sid) {
memcpy(response->sids[response->frame_sid_count++], subscriber->sid, SID_SIZE);
}
return 0;
}
int overlay_mdp_address_list(overlay_mdp_addrlist *request, overlay_mdp_addrlist *response){
if (debug & DEBUG_MDPREQUESTS)
DEBUGF("MDP_GETADDRS first_sid=%u mode=%d",
request->first_sid,
request->mode
);
/* Prepare reply packet */
response->mode = request->mode;
response->first_sid = request->first_sid;
response->frame_sid_count = 0;
/* ... and constrain list for sanity */
if (response->first_sid<0) response->first_sid=0;
/* Populate with SIDs */
enum_subscribers(NULL, search_subscribers, response);
response->last_sid = response->first_sid + response->frame_sid_count - 1;
if (debug & DEBUG_MDPREQUESTS)
DEBUGF("reply MDP_ADDRLIST first_sid=%u last_sid=%u frame_sid_count=%u server_sid_count=%u",
response->first_sid,
response->last_sid,
response->frame_sid_count,
response->server_sid_count
);
return 0;
}
void overlay_mdp_poll(struct sched_ent *alarm)
{
if (alarm->poll.revents & POLLIN) {
@ -995,63 +1017,25 @@ void overlay_mdp_poll(struct sched_ent *alarm)
if (debug & DEBUG_MDPREQUESTS) DEBUG("MDP_GOODBYE");
overlay_mdp_releasebindings(recvaddr_un,recvaddrlen);
return;
/* Deprecated. We can replace with a more generic dump of the routing table */
case MDP_NODEINFO:
if (debug & DEBUG_MDPREQUESTS) DEBUG("MDP_NODEINFO");
overlay_route_node_info(mdp,recvaddr_un,recvaddrlen);
if (!overlay_route_node_info(&mdp->nodeinfo))
overlay_mdp_reply(mdp_named.poll.fd,recvaddr_un,recvaddrlen,mdp);
return;
case MDP_GETADDRS:
if (debug & DEBUG_MDPREQUESTS)
DEBUGF("MDP_GETADDRS first_sid=%u last_sid=%u frame_sid_count=%u mode=%d",
mdp->addrlist.first_sid,
mdp->addrlist.last_sid,
mdp->addrlist.frame_sid_count,
mdp->addrlist.mode
);
{
overlay_mdp_frame mdpreply;
/* Work out which SIDs to get ... */
int sid_num=mdp->addrlist.first_sid;
int max_sid=mdp->addrlist.last_sid;
int max_sids=mdp->addrlist.frame_sid_count;
/* ... and constrain list for sanity */
if (sid_num<0) sid_num=0;
if (max_sids>MDP_MAX_SID_REQUEST) max_sids=MDP_MAX_SID_REQUEST;
if (max_sids<0) max_sids=0;
/* Prepare reply packet */
mdpreply.packetTypeAndFlags = MDP_ADDRLIST;
mdpreply.addrlist.mode = mdp->addrlist.mode;
mdpreply.addrlist.first_sid = sid_num;
mdpreply.addrlist.last_sid = max_sid;
mdpreply.addrlist.frame_sid_count = max_sids;
/* Populate with SIDs */
struct search_state state={
.mdp=mdp,
.mdpreply=&mdpreply,
.first=sid_num,
.max=max_sid,
};
enum_subscribers(NULL, search_subscribers, &state);
mdpreply.addrlist.frame_sid_count = state.index;
mdpreply.addrlist.last_sid = sid_num + state.index - 1;
mdpreply.addrlist.server_sid_count = state.count;
if (debug & DEBUG_MDPREQUESTS)
DEBUGF("reply MDP_ADDRLIST first_sid=%u last_sid=%u frame_sid_count=%u server_sid_count=%u",
mdpreply.addrlist.first_sid,
mdpreply.addrlist.last_sid,
mdpreply.addrlist.frame_sid_count,
mdpreply.addrlist.server_sid_count
);
if (!overlay_mdp_address_list(&mdp->addrlist, &mdpreply.addrlist))
/* Send back to caller */
overlay_mdp_reply(alarm->poll.fd,
(struct sockaddr_un *)recvaddr,recvaddrlen,
&mdpreply);
overlay_mdp_reply(alarm->poll.fd,
(struct sockaddr_un *)recvaddr,recvaddrlen,
&mdpreply);
return;
}
break;

View File

@ -925,18 +925,17 @@ void overlay_route_tick(struct sched_ent *alarm)
return;
}
int overlay_route_node_info(overlay_mdp_frame *mdp,
struct sockaddr_un *addr,int addrlen)
int overlay_route_node_info(overlay_mdp_nodeinfo *node_info)
{
time_ms_t now = gettime_ms();
if (0)
DEBUGF("Looking for node %s* (prefix len=0x%x)",
alloca_tohex(mdp->nodeinfo.sid, mdp->nodeinfo.sid_prefix_length),
mdp->nodeinfo.sid_prefix_length
alloca_tohex(node_info->sid, node_info->sid_prefix_length),
node_info->sid_prefix_length
);
mdp->nodeinfo.foundP=0;
node_info->foundP=0;
/* check if it is a local identity */
int cn,in,kp;
@ -946,24 +945,24 @@ int overlay_route_node_info(overlay_mdp_frame *mdp,
if (keyring->contexts[cn]->identities[in]->keypairs[kp]->type
==KEYTYPE_CRYPTOBOX)
{
if (!memcmp(&mdp->nodeinfo.sid[0],
if (!memcmp(&node_info->sid[0],
&keyring->contexts[cn]->identities[in]
->keypairs[kp]->public_key[0],
mdp->nodeinfo.sid_prefix_length/2))
node_info->sid_prefix_length/2))
{
mdp->nodeinfo.foundP=1;
mdp->nodeinfo.localP=1;
mdp->nodeinfo.neighbourP=0;
mdp->nodeinfo.time_since_last_observation = 0;
mdp->nodeinfo.score=256;
mdp->nodeinfo.interface_number=-1;
node_info->foundP=1;
node_info->localP=1;
node_info->neighbourP=0;
node_info->time_since_last_observation = 0;
node_info->score=256;
node_info->interface_number=-1;
bcopy(&keyring->contexts[cn]->identities[in]
->keypairs[kp]->public_key[0],
&mdp->nodeinfo.sid[0],SID_SIZE);
&node_info->sid[0],SID_SIZE);
mdp->nodeinfo.did[0]=0;
if (mdp->nodeinfo.resolve_did) {
mdp->nodeinfo.resolve_did=0;
node_info->did[0]=0;
if (node_info->resolve_did) {
node_info->resolve_did=0;
int k2;
for(k2=0;k2<keyring->contexts[cn]->identities[in]
->keypair_count;k2++)
@ -973,64 +972,64 @@ int overlay_route_node_info(overlay_mdp_frame *mdp,
/* private key field has unpacked did */
bcopy(&keyring->contexts[cn]->identities[in]
->keypairs[k2]->private_key[0],
&mdp->nodeinfo.did[0],
&node_info->did[0],
keyring->contexts[cn]->identities[in]
->keypairs[k2]->private_key_len);
/* public key has name */
bcopy(&keyring->contexts[cn]->identities[in]
->keypairs[k2]->public_key[0],
&mdp->nodeinfo.name[0],
&node_info->name[0],
keyring->contexts[cn]->identities[in]
->keypairs[k2]->public_key_len);
mdp->nodeinfo.resolve_did=1;
node_info->resolve_did=1;
}
}
return overlay_mdp_reply(mdp_named.poll.fd,addr,addrlen,mdp);
return 0;
}
}
struct subscriber *subscriber = find_subscriber(mdp->nodeinfo.sid, mdp->nodeinfo.sid_prefix_length/2, 0);
struct subscriber *subscriber = find_subscriber(node_info->sid, node_info->sid_prefix_length/2, 0);
if (subscriber && subscriber->node){
overlay_node *node = subscriber->node;
mdp->nodeinfo.foundP=1;
mdp->nodeinfo.localP=0;
mdp->nodeinfo.score=-1;
mdp->nodeinfo.interface_number=-1;
mdp->nodeinfo.resolve_did=0;
node_info->foundP=1;
node_info->localP=0;
node_info->score=-1;
node_info->interface_number=-1;
node_info->resolve_did=0;
bcopy(subscriber->sid,
mdp->nodeinfo.sid,SID_SIZE);
node_info->sid,SID_SIZE);
if (subscriber->node->neighbour_id){
int n = subscriber->node->neighbour_id;
mdp->nodeinfo.neighbourP=1;
mdp->nodeinfo.time_since_last_observation = now - overlay_neighbours[n].last_observation_time_ms;
node_info->neighbourP=1;
node_info->time_since_last_observation = now - overlay_neighbours[n].last_observation_time_ms;
int i;
for(i=0;i<OVERLAY_MAX_INTERFACES;i++)
if (overlay_neighbours[n].scores[i]>mdp->nodeinfo.score)
if (overlay_neighbours[n].scores[i]>node_info->score)
{
mdp->nodeinfo.score=overlay_neighbours[n].scores[i];
mdp->nodeinfo.interface_number=i;
node_info->score=overlay_neighbours[n].scores[i];
node_info->interface_number=i;
}
}else{
mdp->nodeinfo.neighbourP=0;
mdp->nodeinfo.time_since_last_observation = -1;
node_info->neighbourP=0;
node_info->time_since_last_observation = -1;
int o;
for(o=0;o<OVERLAY_MAX_OBSERVATIONS;o++)
if (node->observations[o].observed_score)
{
overlay_node_observation *ob
=&node->observations[o];
if (ob->corrected_score>mdp->nodeinfo.score) {
mdp->nodeinfo.score=ob->corrected_score;
if (ob->corrected_score>node_info->score) {
node_info->score=ob->corrected_score;
}
if (mdp->nodeinfo.time_since_last_observation == -1 || now - ob->rx_time < mdp->nodeinfo.time_since_last_observation)
mdp->nodeinfo.time_since_last_observation = now - ob->rx_time;
if (node_info->time_since_last_observation == -1 || now - ob->rx_time < node_info->time_since_last_observation)
node_info->time_since_last_observation = now - ob->rx_time;
}
}
}
return overlay_mdp_reply(mdp_named.poll.fd,addr,addrlen,mdp);
return 0;
}

View File

@ -815,8 +815,7 @@ int is_configvarname(const char *arg);
int overlay_mdp_getmyaddr(int index,unsigned char *sid);
int overlay_mdp_bind(unsigned char *localaddr,int port);
int overlay_route_node_info(overlay_mdp_frame *mdp,
struct sockaddr_un *addr,int addrlen);
int overlay_route_node_info(overlay_mdp_nodeinfo *node_info);
int overlay_interface_register(char *name,
struct in_addr addr,
struct in_addr mask);