mirror of
https://github.com/servalproject/serval-dna.git
synced 2025-04-07 02:56:44 +00:00
Use one MDP socket per binding instead of a single global socket.
This commit is contained in:
parent
4838a529bd
commit
954a8a01a4
283
commandline.c
283
commandline.c
@ -249,7 +249,6 @@ int parseCommandLine(struct cli_context *context, const char *argv0, int argc, c
|
||||
}
|
||||
|
||||
/* clean up after ourselves */
|
||||
overlay_mdp_client_done();
|
||||
rhizome_close_db();
|
||||
OUT();
|
||||
|
||||
@ -505,19 +504,17 @@ int app_log(const struct cli_parsed *parsed, struct cli_context *context)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void lookup_send_request(const sid_t *srcsid, int srcport, const sid_t *dstsid, const char *did)
|
||||
void lookup_send_request(int mdp_sockfd, const sid_t *srcsid, int srcport, const sid_t *dstsid, const char *did)
|
||||
{
|
||||
int i;
|
||||
overlay_mdp_frame mdp;
|
||||
bzero(&mdp,sizeof(mdp));
|
||||
|
||||
|
||||
/* set source address to a local address, and pick a random port */
|
||||
/* set source address to the local address and port */
|
||||
mdp.out.src.port=srcport;
|
||||
bcopy(srcsid->binary, mdp.out.src.sid, SID_SIZE);
|
||||
|
||||
/* Send to destination address and DNA lookup port */
|
||||
|
||||
if (dstsid) {
|
||||
/* Send an encrypted unicast packet */
|
||||
mdp.packetTypeAndFlags=MDP_TX;
|
||||
@ -534,23 +531,24 @@ void lookup_send_request(const sid_t *srcsid, int srcport, const sid_t *dstsid,
|
||||
bcopy(did,&mdp.out.payload[0],strlen(did)+1);
|
||||
mdp.out.payload_length=strlen(did)+1;
|
||||
|
||||
overlay_mdp_send(&mdp,0,0);
|
||||
overlay_mdp_send(mdp_sockfd, &mdp, 0, 0);
|
||||
|
||||
/* Also send an encrypted unicast request to a configured directory service */
|
||||
if (!dstsid){
|
||||
if (!is_sid_any(config.directory.service.binary)) {
|
||||
memcpy(mdp.out.dst.sid, config.directory.service.binary, SID_SIZE);
|
||||
mdp.packetTypeAndFlags=MDP_TX;
|
||||
overlay_mdp_send(&mdp,0,0);
|
||||
overlay_mdp_send(mdp_sockfd, &mdp,0,0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int app_dna_lookup(const struct cli_parsed *parsed, struct cli_context *context)
|
||||
{
|
||||
int mdp_sockfd;
|
||||
if (config.debug.verbose)
|
||||
DEBUG_cli_parsed(parsed);
|
||||
|
||||
|
||||
/* Create the instance directory if it does not yet exist */
|
||||
if (create_serval_instance_dir() == -1)
|
||||
return -1;
|
||||
@ -574,12 +572,21 @@ int app_dna_lookup(const struct cli_parsed *parsed, struct cli_context *context)
|
||||
one_reply=1;
|
||||
idelay=-idelay;
|
||||
}
|
||||
|
||||
|
||||
if ((mdp_sockfd = overlay_mdp_client_socket()) < 0)
|
||||
return WHY("Cannot create MDP socket");
|
||||
|
||||
/* Bind to MDP socket and await confirmation */
|
||||
sid_t srcsid;
|
||||
int port=32768+(random()&32767);
|
||||
if (overlay_mdp_getmyaddr(0, &srcsid)) return WHY("Could not get local address");
|
||||
if (overlay_mdp_bind(&srcsid, port)) return WHY("Could not bind to MDP socket");
|
||||
if (overlay_mdp_getmyaddr(mdp_sockfd, 0, &srcsid)) {
|
||||
overlay_mdp_client_close(mdp_sockfd);
|
||||
return WHY("Could not get local address");
|
||||
}
|
||||
if (overlay_mdp_bind(mdp_sockfd, &srcsid, port)) {
|
||||
overlay_mdp_client_close(mdp_sockfd);
|
||||
return WHY("Could not bind to MDP socket");
|
||||
}
|
||||
|
||||
/* use MDP to send the lookup request to MDP_PORT_DNALOOKUP, and wait for
|
||||
replies. */
|
||||
@ -591,76 +598,68 @@ int app_dna_lookup(const struct cli_parsed *parsed, struct cli_context *context)
|
||||
time_ms_t now;
|
||||
int interval=125;
|
||||
|
||||
while (timeout > (now = gettime_ms()))
|
||||
{
|
||||
if ((last_tx+interval)<now)
|
||||
{
|
||||
|
||||
lookup_send_request(&srcsid, port, NULL, did);
|
||||
|
||||
last_tx=now;
|
||||
interval+=interval>>1;
|
||||
}
|
||||
time_ms_t short_timeout=125;
|
||||
while(short_timeout>0) {
|
||||
if (overlay_mdp_client_poll(short_timeout))
|
||||
{
|
||||
overlay_mdp_frame rx;
|
||||
int ttl;
|
||||
if (overlay_mdp_recv(&rx, port, &ttl)==0)
|
||||
{
|
||||
if (rx.packetTypeAndFlags==MDP_ERROR)
|
||||
{
|
||||
WHYF(" Error message: %s", rx.error.message);
|
||||
while (timeout > (now = gettime_ms())){
|
||||
if ((last_tx+interval)<now){
|
||||
lookup_send_request(mdp_sockfd, &srcsid, port, NULL, did);
|
||||
last_tx=now;
|
||||
interval+=interval>>1;
|
||||
}
|
||||
time_ms_t short_timeout=125;
|
||||
while(short_timeout>0) {
|
||||
if (overlay_mdp_client_poll(mdp_sockfd, short_timeout)){
|
||||
overlay_mdp_frame rx;
|
||||
int ttl;
|
||||
if (overlay_mdp_recv(mdp_sockfd, &rx, port, &ttl)==0){
|
||||
if (rx.packetTypeAndFlags==MDP_ERROR){
|
||||
WHYF(" Error message: %s", rx.error.message);
|
||||
} else if ((rx.packetTypeAndFlags&MDP_TYPE_MASK)==MDP_TX) {
|
||||
/* Extract DID, Name, URI from response. */
|
||||
if (strlen((char *)rx.in.payload)<512) {
|
||||
char sidhex[SID_STRLEN + 1];
|
||||
char did[DID_MAXSIZE + 1];
|
||||
char name[64];
|
||||
char uri[512];
|
||||
if ( !parseDnaReply((char *)rx.in.payload, rx.in.payload_length, sidhex, did, name, uri, NULL)
|
||||
|| !str_is_subscriber_id(sidhex)
|
||||
|| !str_is_did(did)
|
||||
|| !str_is_uri(uri)
|
||||
) {
|
||||
WHYF("Received malformed DNA reply: %s", alloca_toprint(160, (const char *)rx.in.payload, rx.in.payload_length));
|
||||
} else {
|
||||
/* Have we seen this response before? */
|
||||
int i;
|
||||
for(i=0;i<uri_count;i++)
|
||||
if (!strcmp(uri,uris[i])) break;
|
||||
if (i==uri_count) {
|
||||
/* Not previously seen, so report it */
|
||||
cli_put_string(context, uri, ":");
|
||||
cli_put_string(context, did, ":");
|
||||
cli_put_string(context, name, "\n");
|
||||
|
||||
if (one_reply){
|
||||
timeout=now;
|
||||
short_timeout=0;
|
||||
}
|
||||
else if ((rx.packetTypeAndFlags&MDP_TYPE_MASK)==MDP_TX) {
|
||||
/* Extract DID, Name, URI from response. */
|
||||
if (strlen((char *)rx.in.payload)<512) {
|
||||
char sidhex[SID_STRLEN + 1];
|
||||
char did[DID_MAXSIZE + 1];
|
||||
char name[64];
|
||||
char uri[512];
|
||||
if ( !parseDnaReply((char *)rx.in.payload, rx.in.payload_length, sidhex, did, name, uri, NULL)
|
||||
|| !str_is_subscriber_id(sidhex)
|
||||
|| !str_is_did(did)
|
||||
|| !str_is_uri(uri)
|
||||
) {
|
||||
WHYF("Received malformed DNA reply: %s", alloca_toprint(160, (const char *)rx.in.payload, rx.in.payload_length));
|
||||
} else {
|
||||
/* Have we seen this response before? */
|
||||
int i;
|
||||
for(i=0;i<uri_count;i++)
|
||||
if (!strcmp(uri,uris[i])) break;
|
||||
if (i==uri_count) {
|
||||
/* Not previously seen, so report it */
|
||||
cli_put_string(context, uri, ":");
|
||||
cli_put_string(context, did, ":");
|
||||
cli_put_string(context, name, "\n");
|
||||
|
||||
if (one_reply){
|
||||
timeout=now;
|
||||
short_timeout=0;
|
||||
}
|
||||
|
||||
/* Remember that we have seen it */
|
||||
if (uri_count<MAXREPLIES&&strlen(uri)<MAXURILEN) {
|
||||
strcpy(uris[uri_count++],uri);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Remember that we have seen it */
|
||||
if (uri_count<MAXREPLIES&&strlen(uri)<MAXURILEN) {
|
||||
strcpy(uris[uri_count++],uri);
|
||||
}
|
||||
}
|
||||
else WHYF("packettype=0x%x",rx.packetTypeAndFlags);
|
||||
if (servalShutdown) break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (servalShutdown) break;
|
||||
short_timeout=125-(gettime_ms()-now);
|
||||
else WHYF("packettype=0x%x",rx.packetTypeAndFlags);
|
||||
if (servalShutdown) break;
|
||||
}
|
||||
}
|
||||
if (servalShutdown) break;
|
||||
short_timeout=125-(gettime_ms()-now);
|
||||
}
|
||||
if (servalShutdown) break;
|
||||
}
|
||||
|
||||
overlay_mdp_client_done();
|
||||
overlay_mdp_client_close(mdp_sockfd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -903,6 +902,7 @@ int app_server_status(const struct cli_parsed *parsed, struct cli_context *conte
|
||||
|
||||
int app_mdp_ping(const struct cli_parsed *parsed, struct cli_context *context)
|
||||
{
|
||||
int mdp_sockfd;
|
||||
if (config.debug.verbose)
|
||||
DEBUG_cli_parsed(parsed);
|
||||
const char *sidhex, *count, *opt_timeout, *opt_interval;
|
||||
@ -911,6 +911,13 @@ int app_mdp_ping(const struct cli_parsed *parsed, struct cli_context *context)
|
||||
|| cli_arg(parsed, "SID", &sidhex, str_is_subscriber_id, "broadcast") == -1
|
||||
|| cli_arg(parsed, "count", &count, cli_uint, "0") == -1)
|
||||
return -1;
|
||||
|
||||
/* Get SID that we want to ping.
|
||||
TODO - allow lookup of SID prefixes and telephone numbers
|
||||
(that would require MDP lookup of phone numbers, which doesn't yet occur) */
|
||||
sid_t ping_sid;
|
||||
if (str_to_sid_t(&ping_sid, sidhex) == -1)
|
||||
return WHY("str_to_sid_t() failed");
|
||||
|
||||
// assume we wont hear any responses
|
||||
int ret=1;
|
||||
@ -924,24 +931,28 @@ int app_mdp_ping(const struct cli_parsed *parsed, struct cli_context *context)
|
||||
|
||||
overlay_mdp_frame mdp;
|
||||
bzero(&mdp, sizeof(overlay_mdp_frame));
|
||||
/* Bind to MDP socket and await confirmation */
|
||||
sid_t srcsid;
|
||||
int port=32768+(random()&32767);
|
||||
if (overlay_mdp_getmyaddr(0, &srcsid)) return WHY("Could not get local address");
|
||||
if (overlay_mdp_bind(&srcsid, port)) return WHY("Could not bind to MDP socket");
|
||||
|
||||
/* First sequence number in the echo frames */
|
||||
unsigned int firstSeq=random();
|
||||
unsigned int sequence_number=firstSeq;
|
||||
|
||||
/* Get SID that we want to ping.
|
||||
TODO - allow lookup of SID prefixes and telephone numbers
|
||||
(that would require MDP lookup of phone numbers, which doesn't yet occur) */
|
||||
sid_t ping_sid;
|
||||
if (str_to_sid_t(&ping_sid, sidhex) == -1)
|
||||
return WHY("str_to_sid_t() failed");
|
||||
int broadcast = is_sid_broadcast(ping_sid.binary);
|
||||
|
||||
/* Bind to MDP socket and await confirmation */
|
||||
if ((mdp_sockfd = overlay_mdp_client_socket()) < 0)
|
||||
return WHY("Cannot create MDP socket");
|
||||
|
||||
sid_t srcsid;
|
||||
int port=32768+(random()&32767);
|
||||
if (overlay_mdp_getmyaddr(mdp_sockfd, 0, &srcsid)) {
|
||||
overlay_mdp_client_close(mdp_sockfd);
|
||||
return WHY("Could not get local address");
|
||||
}
|
||||
if (overlay_mdp_bind(mdp_sockfd, &srcsid, port)) {
|
||||
overlay_mdp_client_close(mdp_sockfd);
|
||||
return WHY("Could not bind to MDP socket");
|
||||
}
|
||||
|
||||
/* TODO Eventually we should try to resolve SID to phone number and vice versa */
|
||||
cli_printf(context, "MDP PING %s (%s): 12 data bytes", alloca_tohex_sid_t(ping_sid), alloca_tohex_sid_t(ping_sid));
|
||||
cli_delim(context, "\n");
|
||||
@ -970,7 +981,7 @@ int app_mdp_ping(const struct cli_parsed *parsed, struct cli_context *context)
|
||||
*seq=sequence_number;
|
||||
write_uint64(&mdp.out.payload[4], gettime_ms());
|
||||
|
||||
int res=overlay_mdp_send(&mdp,0,0);
|
||||
int res=overlay_mdp_send(mdp_sockfd, &mdp, 0, 0);
|
||||
if (res) {
|
||||
WHYF("could not dispatch PING frame #%d (error %d)%s%s",
|
||||
sequence_number - firstSeq,
|
||||
@ -987,11 +998,11 @@ int app_mdp_ping(const struct cli_parsed *parsed, struct cli_context *context)
|
||||
time_ms_t finish = now + (tx_count < icount?interval_ms:timeout_ms);
|
||||
for (; !servalShutdown && now < finish; now = gettime_ms()) {
|
||||
time_ms_t poll_timeout_ms = finish - gettime_ms();
|
||||
int result = overlay_mdp_client_poll(poll_timeout_ms);
|
||||
int result = overlay_mdp_client_poll(mdp_sockfd, poll_timeout_ms);
|
||||
|
||||
if (result>0) {
|
||||
int ttl=-1;
|
||||
if (overlay_mdp_recv(&mdp, port, &ttl)==0) {
|
||||
if (overlay_mdp_recv(mdp_sockfd, &mdp, port, &ttl)==0) {
|
||||
switch(mdp.packetTypeAndFlags&MDP_TYPE_MASK) {
|
||||
case MDP_ERROR:
|
||||
WHYF("mdpping: overlay_mdp_recv: %s (code %d)", mdp.error.message, mdp.error.error);
|
||||
@ -1029,6 +1040,8 @@ int app_mdp_ping(const struct cli_parsed *parsed, struct cli_context *context)
|
||||
}
|
||||
}
|
||||
|
||||
overlay_mdp_client_close(mdp_sockfd);
|
||||
|
||||
{
|
||||
float rx_stddev=0;
|
||||
float rx_mean=rx_ms*1.0/rx_count;
|
||||
@ -1050,12 +1063,12 @@ int app_mdp_ping(const struct cli_parsed *parsed, struct cli_context *context)
|
||||
cli_delim(context, NULL);
|
||||
cli_flush(context);
|
||||
}
|
||||
overlay_mdp_client_done();
|
||||
return ret;
|
||||
}
|
||||
|
||||
int app_trace(const struct cli_parsed *parsed, struct cli_context *context){
|
||||
|
||||
int app_trace(const struct cli_parsed *parsed, struct cli_context *context)
|
||||
{
|
||||
int mdp_sockfd;
|
||||
const char *sidhex;
|
||||
if (cli_arg(parsed, "SID", &sidhex, str_is_subscriber_id, NULL) == -1)
|
||||
return -1;
|
||||
@ -1064,14 +1077,22 @@ int app_trace(const struct cli_parsed *parsed, struct cli_context *context){
|
||||
sid_t dstsid;
|
||||
if (str_to_sid_t(&dstsid, sidhex) == -1)
|
||||
return WHY("str_to_sid_t() failed");
|
||||
|
||||
if ((mdp_sockfd = overlay_mdp_client_socket()) < 0)
|
||||
return WHY("Cannot create MDP socket");
|
||||
int port=32768+(random()&32767);
|
||||
if (overlay_mdp_getmyaddr(mdp_sockfd, 0, &srcsid)) {
|
||||
overlay_mdp_client_close(mdp_sockfd);
|
||||
return WHY("Could not get local address");
|
||||
}
|
||||
if (overlay_mdp_bind(mdp_sockfd, &srcsid, port)) {
|
||||
overlay_mdp_client_close(mdp_sockfd);
|
||||
return WHY("Could not bind to MDP socket");
|
||||
}
|
||||
|
||||
overlay_mdp_frame mdp;
|
||||
bzero(&mdp, sizeof(mdp));
|
||||
|
||||
int port=32768+(random()&32767);
|
||||
if (overlay_mdp_getmyaddr(0, &srcsid)) return WHY("Could not get local address");
|
||||
if (overlay_mdp_bind(&srcsid, port)) return WHY("Could not bind to MDP socket");
|
||||
|
||||
bcopy(srcsid.binary, mdp.out.src.sid, SID_SIZE);
|
||||
bcopy(srcsid.binary, mdp.out.dst.sid, SID_SIZE);
|
||||
mdp.out.src.port=port;
|
||||
@ -1091,7 +1112,7 @@ int app_trace(const struct cli_parsed *parsed, struct cli_context *context){
|
||||
cli_delim(context, "\n");
|
||||
cli_flush(context);
|
||||
|
||||
int ret=overlay_mdp_send(&mdp,MDP_AWAITREPLY,5000);
|
||||
int ret=overlay_mdp_send(mdp_sockfd, &mdp, MDP_AWAITREPLY, 5000);
|
||||
ob_free(b);
|
||||
if (ret)
|
||||
DEBUGF("overlay_mdp_send returned %d", ret);
|
||||
@ -1113,7 +1134,7 @@ int app_trace(const struct cli_parsed *parsed, struct cli_context *context){
|
||||
i++;
|
||||
}
|
||||
}
|
||||
overlay_mdp_client_done();
|
||||
overlay_mdp_client_close(mdp_sockfd);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -1927,6 +1948,7 @@ int app_keyring_set_did(const struct cli_parsed *parsed, struct cli_context *con
|
||||
|
||||
int app_id_self(const struct cli_parsed *parsed, struct cli_context *context)
|
||||
{
|
||||
int mdp_sockfd;
|
||||
if (config.debug.verbose)
|
||||
DEBUG_cli_parsed(parsed);
|
||||
/* List my own identities */
|
||||
@ -1947,20 +1969,24 @@ int app_id_self(const struct cli_parsed *parsed, struct cli_context *context)
|
||||
return WHYF("unsupported arg '%s'", arg);
|
||||
a.addrlist.first_sid=0;
|
||||
|
||||
if ((mdp_sockfd = overlay_mdp_client_socket()) < 0)
|
||||
return WHY("Cannot create MDP socket");
|
||||
|
||||
do{
|
||||
result=overlay_mdp_send(&a,MDP_AWAITREPLY,5000);
|
||||
result=overlay_mdp_send(mdp_sockfd, &a, MDP_AWAITREPLY, 5000);
|
||||
if (result) {
|
||||
if (a.packetTypeAndFlags==MDP_ERROR)
|
||||
{
|
||||
WHYF(" MDP Server error #%d: '%s'",
|
||||
a.error.error,a.error.message);
|
||||
}
|
||||
else
|
||||
if (a.packetTypeAndFlags==MDP_ERROR){
|
||||
WHYF(" MDP Server error #%d: '%s'",
|
||||
a.error.error,a.error.message);
|
||||
} else
|
||||
WHYF("Could not get list of local MDP addresses");
|
||||
overlay_mdp_client_close(mdp_sockfd);
|
||||
return WHY("Failed to get local address list");
|
||||
}
|
||||
if ((a.packetTypeAndFlags&MDP_TYPE_MASK)!=MDP_ADDRLIST)
|
||||
if ((a.packetTypeAndFlags&MDP_TYPE_MASK)!=MDP_ADDRLIST) {
|
||||
overlay_mdp_client_close(mdp_sockfd);
|
||||
return WHY("MDP Server returned something other than an address list");
|
||||
}
|
||||
int i;
|
||||
for(i=0;i<a.addrlist.frame_sid_count;i++) {
|
||||
count++;
|
||||
@ -1971,19 +1997,27 @@ int app_id_self(const struct cli_parsed *parsed, struct cli_context *context)
|
||||
a.addrlist.first_sid=a.addrlist.last_sid+1;
|
||||
}while(a.addrlist.frame_sid_count==MDP_MAX_SID_REQUEST);
|
||||
|
||||
overlay_mdp_client_close(mdp_sockfd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int app_count_peers(const struct cli_parsed *parsed, struct cli_context *context)
|
||||
{
|
||||
int mdp_sockfd;
|
||||
if (config.debug.verbose)
|
||||
DEBUG_cli_parsed(parsed);
|
||||
|
||||
if ((mdp_sockfd = overlay_mdp_client_socket()) < 0)
|
||||
return WHY("Cannot create MDP socket");
|
||||
|
||||
overlay_mdp_frame a;
|
||||
bzero(&a, sizeof(overlay_mdp_frame));
|
||||
a.packetTypeAndFlags=MDP_GETADDRS;
|
||||
a.addrlist.mode = MDP_ADDRLIST_MODE_ROUTABLE_PEERS;
|
||||
a.addrlist.first_sid = OVERLAY_MDP_ADDRLIST_MAX_SID_COUNT;
|
||||
if (overlay_mdp_send(&a,MDP_AWAITREPLY,5000)){
|
||||
int ret=overlay_mdp_send(mdp_sockfd, &a,MDP_AWAITREPLY,5000);
|
||||
overlay_mdp_client_close(mdp_sockfd);
|
||||
if (ret){
|
||||
if (a.packetTypeAndFlags==MDP_ERROR)
|
||||
return WHYF(" MDP Server error #%d: '%s'",a.error.error,a.error.message);
|
||||
return WHYF("Failed to send request");
|
||||
@ -2169,13 +2203,18 @@ int app_crypt_test(const struct cli_parsed *parsed, struct cli_context *context)
|
||||
|
||||
int app_route_print(const struct cli_parsed *parsed, struct cli_context *context)
|
||||
{
|
||||
int mdp_sockfd;
|
||||
if (config.debug.verbose)
|
||||
DEBUG_cli_parsed(parsed);
|
||||
|
||||
if ((mdp_sockfd = overlay_mdp_client_socket()) < 0)
|
||||
return WHY("Cannot create MDP socket");
|
||||
|
||||
overlay_mdp_frame mdp;
|
||||
bzero(&mdp,sizeof(mdp));
|
||||
|
||||
mdp.packetTypeAndFlags=MDP_ROUTING_TABLE;
|
||||
overlay_mdp_send(&mdp,0,0);
|
||||
overlay_mdp_send(mdp_sockfd, &mdp,0,0);
|
||||
|
||||
const char *names[]={
|
||||
"Subscriber id",
|
||||
@ -2185,10 +2224,10 @@ int app_route_print(const struct cli_parsed *parsed, struct cli_context *context
|
||||
};
|
||||
cli_columns(context, 4, names);
|
||||
|
||||
while(overlay_mdp_client_poll(200)){
|
||||
while(overlay_mdp_client_poll(mdp_sockfd, 200)){
|
||||
overlay_mdp_frame rx;
|
||||
int ttl;
|
||||
if (overlay_mdp_recv(&rx, 0, &ttl))
|
||||
if (overlay_mdp_recv(mdp_sockfd, &rx, 0, &ttl))
|
||||
continue;
|
||||
|
||||
int ofs=0;
|
||||
@ -2224,11 +2263,13 @@ int app_route_print(const struct cli_parsed *parsed, struct cli_context *context
|
||||
cli_put_string(context, alloca_tohex_sid(p->neighbour), "\n");
|
||||
}
|
||||
}
|
||||
overlay_mdp_client_close(mdp_sockfd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int app_reverse_lookup(const struct cli_parsed *parsed, struct cli_context *context)
|
||||
{
|
||||
int mdp_sockfd;
|
||||
if (config.debug.verbose)
|
||||
DEBUG_cli_parsed(parsed);
|
||||
const char *sidhex, *delay;
|
||||
@ -2245,11 +2286,18 @@ int app_reverse_lookup(const struct cli_parsed *parsed, struct cli_context *cont
|
||||
if (str_to_sid_t(&dstsid, sidhex) == -1)
|
||||
return WHY("str_to_sid_t() failed");
|
||||
|
||||
if (overlay_mdp_getmyaddr(0, &srcsid))
|
||||
if ((mdp_sockfd = overlay_mdp_client_socket()) < 0)
|
||||
return WHY("Cannot create MDP socket");
|
||||
|
||||
if (overlay_mdp_getmyaddr(mdp_sockfd, 0, &srcsid)){
|
||||
overlay_mdp_client_close(mdp_sockfd);
|
||||
return WHY("Unable to get my address");
|
||||
if (overlay_mdp_bind(&srcsid, port))
|
||||
}
|
||||
if (overlay_mdp_bind(mdp_sockfd, &srcsid, port)){
|
||||
overlay_mdp_client_close(mdp_sockfd);
|
||||
return WHY("Unable to bind port");
|
||||
|
||||
}
|
||||
|
||||
time_ms_t now = gettime_ms();
|
||||
time_ms_t timeout = now + atoi(delay);
|
||||
time_ms_t next_send = now;
|
||||
@ -2260,17 +2308,17 @@ int app_reverse_lookup(const struct cli_parsed *parsed, struct cli_context *cont
|
||||
|
||||
if (now >= next_send){
|
||||
/* Send a unicast packet to this node, asking for any did */
|
||||
lookup_send_request(&srcsid, port, &dstsid, "");
|
||||
lookup_send_request(mdp_sockfd, &srcsid, port, &dstsid, "");
|
||||
next_send+=125;
|
||||
continue;
|
||||
}
|
||||
|
||||
time_ms_t poll_timeout = (next_send>timeout?timeout:next_send) - now;
|
||||
if (overlay_mdp_client_poll(poll_timeout)<=0)
|
||||
if (overlay_mdp_client_poll(mdp_sockfd, poll_timeout)<=0)
|
||||
continue;
|
||||
|
||||
int ttl=-1;
|
||||
if (overlay_mdp_recv(&mdp_reply, port, &ttl))
|
||||
if (overlay_mdp_recv(mdp_sockfd, &mdp_reply, port, &ttl))
|
||||
continue;
|
||||
|
||||
if ((mdp_reply.packetTypeAndFlags&MDP_TYPE_MASK)==MDP_ERROR){
|
||||
@ -2316,14 +2364,17 @@ int app_reverse_lookup(const struct cli_parsed *parsed, struct cli_context *cont
|
||||
cli_put_string(context, did, "\n");
|
||||
cli_field_name(context, "name", ":");
|
||||
cli_put_string(context, name, "\n");
|
||||
overlay_mdp_client_close(mdp_sockfd);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
overlay_mdp_client_close(mdp_sockfd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int app_network_scan(const struct cli_parsed *parsed, struct cli_context *context)
|
||||
{
|
||||
int mdp_sockfd;
|
||||
if (config.debug.verbose)
|
||||
DEBUG_cli_parsed(parsed);
|
||||
overlay_mdp_frame mdp;
|
||||
@ -2343,7 +2394,11 @@ int app_network_scan(const struct cli_parsed *parsed, struct cli_context *contex
|
||||
}else
|
||||
DEBUGF("Scanning local networks");
|
||||
|
||||
overlay_mdp_send(&mdp,MDP_AWAITREPLY,5000);
|
||||
if ((mdp_sockfd = overlay_mdp_client_socket()) < 0)
|
||||
return WHY("Cannot create MDP socket");
|
||||
overlay_mdp_send(mdp_sockfd, &mdp, MDP_AWAITREPLY, 5000);
|
||||
overlay_mdp_client_close(mdp_sockfd);
|
||||
|
||||
if (mdp.packetTypeAndFlags!=MDP_ERROR)
|
||||
return -1;
|
||||
cli_put_string(context, mdp.error.message, "\n");
|
||||
|
@ -69,11 +69,11 @@ static void add_item(char *key, char *value){
|
||||
fprintf(stderr, "PUBLISHED \"%s\" = \"%s\"\n", key, value);
|
||||
}
|
||||
|
||||
static void add_record(){
|
||||
static void add_record(int mdp_sockfd){
|
||||
int ttl;
|
||||
overlay_mdp_frame mdp;
|
||||
|
||||
if (overlay_mdp_recv(&mdp, MDP_PORT_DIRECTORY, &ttl))
|
||||
if (overlay_mdp_recv(mdp_sockfd, &mdp, MDP_PORT_DIRECTORY, &ttl))
|
||||
return;
|
||||
|
||||
if (mdp.packetTypeAndFlags&MDP_NOCRYPT){
|
||||
@ -160,17 +160,26 @@ static void resolve_request(){
|
||||
|
||||
int main(int argc, char **argv){
|
||||
struct pollfd fds[2];
|
||||
int mdp_sockfd;
|
||||
|
||||
if ((mdp_sockfd = overlay_mdp_client_socket()) < 0)
|
||||
return WHY("Cannot create MDP socket");
|
||||
|
||||
// bind for incoming directory updates
|
||||
sid_t srcsid;
|
||||
if (overlay_mdp_getmyaddr(0, &srcsid))
|
||||
|
||||
if (overlay_mdp_getmyaddr(mdp_sockfd, 0, &srcsid)) {
|
||||
overlay_mdp_client_close(mdp_sockfd);
|
||||
return WHY("Could not get local address");
|
||||
if (overlay_mdp_bind(&srcsid, MDP_PORT_DIRECTORY))
|
||||
}
|
||||
if (overlay_mdp_bind(mdp_sockfd, &srcsid, MDP_PORT_DIRECTORY)) {
|
||||
overlay_mdp_client_close(mdp_sockfd);
|
||||
return WHY("Could not bind to MDP socket");
|
||||
}
|
||||
|
||||
fds[0].fd = STDIN_FILENO;
|
||||
fds[0].events = POLLIN;
|
||||
fds[1].fd = mdp_client_socket;
|
||||
fds[1].fd = mdp_sockfd;
|
||||
fds[1].events = POLLIN;
|
||||
|
||||
printf("STARTED\n");
|
||||
@ -180,15 +189,15 @@ int main(int argc, char **argv){
|
||||
int r = poll(fds, 2, 100);
|
||||
if (r>0){
|
||||
if (fds[0].revents & POLLIN)
|
||||
resolve_request();
|
||||
resolve_request();
|
||||
if (fds[1].revents & POLLIN)
|
||||
add_record();
|
||||
add_record(mdp_sockfd);
|
||||
|
||||
if (fds[0].revents & (POLLHUP | POLLERR))
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
overlay_mdp_client_done();
|
||||
overlay_mdp_client_close(mdp_sockfd);
|
||||
return 0;
|
||||
}
|
||||
|
116
mdp_client.c
116
mdp_client.c
@ -28,11 +28,9 @@
|
||||
#include "overlay_packet.h"
|
||||
#include "mdp_client.h"
|
||||
|
||||
int mdp_client_socket = -1;
|
||||
|
||||
int overlay_mdp_send(overlay_mdp_frame *mdp, int flags, int timeout_ms)
|
||||
int overlay_mdp_send(int mdp_sockfd, overlay_mdp_frame *mdp, int flags, int timeout_ms)
|
||||
{
|
||||
if (mdp_client_socket == -1 && overlay_mdp_client_init() == -1)
|
||||
if (mdp_sockfd == -1)
|
||||
return -1;
|
||||
// Minimise frame length to save work and prevent accidental disclosure of memory contents.
|
||||
int len = overlay_mdp_relevant_bytes(mdp);
|
||||
@ -44,9 +42,9 @@ int overlay_mdp_send(overlay_mdp_frame *mdp, int flags, int timeout_ms)
|
||||
if (make_local_sockaddr(&addr, &addrlen, "mdp.socket") == -1)
|
||||
return -1;
|
||||
// Send to that socket
|
||||
set_nonblock(mdp_client_socket);
|
||||
int result = sendto(mdp_client_socket, mdp, len, 0, (struct sockaddr *)&addr, addrlen);
|
||||
set_block(mdp_client_socket);
|
||||
set_nonblock(mdp_sockfd);
|
||||
int result = sendto(mdp_sockfd, mdp, len, 0, (struct sockaddr *)&addr, addrlen);
|
||||
set_block(mdp_sockfd);
|
||||
if (result == -1) {
|
||||
mdp->packetTypeAndFlags=MDP_ERROR;
|
||||
mdp->error.error=1;
|
||||
@ -63,9 +61,9 @@ int overlay_mdp_send(overlay_mdp_frame *mdp, int flags, int timeout_ms)
|
||||
port = mdp->out.src.port;
|
||||
|
||||
time_ms_t started = gettime_ms();
|
||||
while(timeout_ms>=0 && overlay_mdp_client_poll(timeout_ms)>0){
|
||||
while(timeout_ms>=0 && overlay_mdp_client_poll(mdp_sockfd, timeout_ms)>0){
|
||||
int ttl=-1;
|
||||
if (!overlay_mdp_recv(mdp, port, &ttl)) {
|
||||
if (!overlay_mdp_recv(mdp_sockfd, mdp, port, &ttl)) {
|
||||
/* If all is well, examine result and return error code provided */
|
||||
if ((mdp->packetTypeAndFlags&MDP_TYPE_MASK)==MDP_ERROR)
|
||||
return mdp->error.error;
|
||||
@ -86,57 +84,53 @@ int overlay_mdp_send(overlay_mdp_frame *mdp, int flags, int timeout_ms)
|
||||
return -1; /* WHY("Timeout waiting for server response"); */
|
||||
}
|
||||
|
||||
int overlay_mdp_client_init()
|
||||
/** Create a new MDP socket and return its descriptor (-1 on error). */
|
||||
int overlay_mdp_client_socket(void)
|
||||
{
|
||||
if (mdp_client_socket == -1) {
|
||||
/* Create local per-client socket to MDP server (connection is always local) */
|
||||
struct sockaddr_un addr;
|
||||
socklen_t addrlen;
|
||||
uint32_t random_value;
|
||||
if (urandombytes((unsigned char *)&random_value, sizeof random_value) == -1)
|
||||
return WHY("urandombytes() failed");
|
||||
if (make_local_sockaddr(&addr, &addrlen, "mdp.client.%u.%08lx.socket", getpid(), (unsigned long)random_value) == -1)
|
||||
return -1;
|
||||
if ((mdp_client_socket = esocket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
|
||||
return -1;
|
||||
if (socket_bind(mdp_client_socket, (struct sockaddr *)&addr, addrlen) == -1) {
|
||||
close(mdp_client_socket);
|
||||
mdp_client_socket = -1;
|
||||
return -1;
|
||||
}
|
||||
socket_set_rcvbufsize(mdp_client_socket, 128 * 1024);
|
||||
/* Create local per-client socket to MDP server (connection is always local) */
|
||||
int mdp_sockfd;
|
||||
struct sockaddr_un addr;
|
||||
socklen_t addrlen;
|
||||
uint32_t random_value;
|
||||
if (urandombytes((unsigned char *)&random_value, sizeof random_value) == -1)
|
||||
return WHY("urandombytes() failed");
|
||||
if (make_local_sockaddr(&addr, &addrlen, "mdp.client.%u.%08lx.socket", getpid(), (unsigned long)random_value) == -1)
|
||||
return -1;
|
||||
if ((mdp_sockfd = esocket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
|
||||
return -1;
|
||||
if (socket_bind(mdp_sockfd, (struct sockaddr *)&addr, addrlen) == -1) {
|
||||
close(mdp_sockfd);
|
||||
return -1;
|
||||
}
|
||||
socket_set_rcvbufsize(mdp_sockfd, 128 * 1024);
|
||||
return mdp_sockfd;
|
||||
}
|
||||
|
||||
int overlay_mdp_client_close(int mdp_sockfd)
|
||||
{
|
||||
/* Tell MDP server to release all our bindings */
|
||||
overlay_mdp_frame mdp;
|
||||
mdp.packetTypeAndFlags = MDP_GOODBYE;
|
||||
overlay_mdp_send(mdp_sockfd, &mdp, 0, 0);
|
||||
// get the socket name and unlink it from the filesystem if not abstract
|
||||
struct sockaddr_un addr;
|
||||
socklen_t addrlen = sizeof addr;
|
||||
if (getsockname(mdp_sockfd, (struct sockaddr *)&addr, &addrlen))
|
||||
WHYF_perror("getsockname(%d)", mdp_sockfd);
|
||||
else if (addrlen > sizeof addr.sun_family && addrlen <= sizeof addr && addr.sun_path[0] != '\0') {
|
||||
if (unlink(addr.sun_path) == -1)
|
||||
WARNF_perror("unlink(%s)", alloca_str_toprint(addr.sun_path));
|
||||
}
|
||||
close(mdp_sockfd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int overlay_mdp_client_done()
|
||||
{
|
||||
if (mdp_client_socket != -1) {
|
||||
/* Tell MDP server to release all our bindings */
|
||||
overlay_mdp_frame mdp;
|
||||
mdp.packetTypeAndFlags = MDP_GOODBYE;
|
||||
overlay_mdp_send(&mdp, 0, 0);
|
||||
// get the socket name and unlink it from the filesystem if not abstract
|
||||
struct sockaddr_un addr;
|
||||
socklen_t addrlen = sizeof addr;
|
||||
if (getsockname(mdp_client_socket, (struct sockaddr *)&addr, &addrlen))
|
||||
WHYF_perror("getsockname(%d)", mdp_client_socket);
|
||||
else if (addrlen > sizeof addr.sun_family && addrlen <= sizeof addr && addr.sun_path[0] != '\0') {
|
||||
if (unlink(addr.sun_path) == -1)
|
||||
WARNF_perror("unlink(%s)", alloca_str_toprint(addr.sun_path));
|
||||
}
|
||||
close(mdp_client_socket);
|
||||
mdp_client_socket = -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int overlay_mdp_client_poll(time_ms_t timeout_ms)
|
||||
int overlay_mdp_client_poll(int mdp_sockfd, time_ms_t timeout_ms)
|
||||
{
|
||||
fd_set r;
|
||||
int ret;
|
||||
FD_ZERO(&r);
|
||||
FD_SET(mdp_client_socket,&r);
|
||||
FD_SET(mdp_sockfd, &r);
|
||||
if (timeout_ms<0) timeout_ms=0;
|
||||
|
||||
struct timeval tv;
|
||||
@ -144,14 +138,14 @@ int overlay_mdp_client_poll(time_ms_t timeout_ms)
|
||||
if (timeout_ms>=0) {
|
||||
tv.tv_sec=timeout_ms/1000;
|
||||
tv.tv_usec=(timeout_ms%1000)*1000;
|
||||
ret=select(mdp_client_socket+1,&r,NULL,&r,&tv);
|
||||
ret=select(mdp_sockfd+1,&r,NULL,&r,&tv);
|
||||
}
|
||||
else
|
||||
ret=select(mdp_client_socket+1,&r,NULL,&r,NULL);
|
||||
ret=select(mdp_sockfd+1,&r,NULL,&r,NULL);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int overlay_mdp_recv(overlay_mdp_frame *mdp, int port, int *ttl)
|
||||
int overlay_mdp_recv(int mdp_sockfd, overlay_mdp_frame *mdp, int port, int *ttl)
|
||||
{
|
||||
/* Construct name of socket to receive from. */
|
||||
struct sockaddr_un mdp_addr;
|
||||
@ -164,9 +158,9 @@ int overlay_mdp_recv(overlay_mdp_frame *mdp, int port, int *ttl)
|
||||
socklen_t recvaddrlen = sizeof recvaddr;
|
||||
ssize_t len;
|
||||
mdp->packetTypeAndFlags = 0;
|
||||
set_nonblock(mdp_client_socket);
|
||||
len = recvwithttl(mdp_client_socket, (unsigned char *)mdp, sizeof(overlay_mdp_frame), ttl, (struct sockaddr *)&recvaddr, &recvaddrlen);
|
||||
set_block(mdp_client_socket);
|
||||
set_nonblock(mdp_sockfd);
|
||||
len = recvwithttl(mdp_sockfd, (unsigned char *)mdp, sizeof(overlay_mdp_frame), ttl, (struct sockaddr *)&recvaddr, &recvaddrlen);
|
||||
set_block(mdp_sockfd);
|
||||
if (len <= 0)
|
||||
return -1; // no packet received
|
||||
|
||||
@ -200,13 +194,13 @@ int overlay_mdp_recv(overlay_mdp_frame *mdp, int port, int *ttl)
|
||||
}
|
||||
|
||||
// send a request to servald deamon to add a port binding
|
||||
int overlay_mdp_bind(const sid_t *localaddr, int port)
|
||||
int overlay_mdp_bind(int mdp_sockfd, const sid_t *localaddr, int port)
|
||||
{
|
||||
overlay_mdp_frame mdp;
|
||||
mdp.packetTypeAndFlags=MDP_BIND|MDP_FORCE;
|
||||
bcopy(localaddr->binary, mdp.bind.sid, SID_SIZE);
|
||||
mdp.bind.port=port;
|
||||
int result=overlay_mdp_send(&mdp,MDP_AWAITREPLY,5000);
|
||||
int result=overlay_mdp_send(mdp_sockfd, &mdp,MDP_AWAITREPLY,5000);
|
||||
if (result) {
|
||||
if (mdp.packetTypeAndFlags==MDP_ERROR)
|
||||
WHYF("Could not bind to MDP port %d: error=%d, message='%s'",
|
||||
@ -218,7 +212,7 @@ int overlay_mdp_bind(const sid_t *localaddr, int port)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int overlay_mdp_getmyaddr(unsigned index, sid_t *sid)
|
||||
int overlay_mdp_getmyaddr(int mdp_sockfd, unsigned index, sid_t *sid)
|
||||
{
|
||||
overlay_mdp_frame a;
|
||||
memset(&a, 0, sizeof(a));
|
||||
@ -228,7 +222,7 @@ int overlay_mdp_getmyaddr(unsigned index, sid_t *sid)
|
||||
a.addrlist.first_sid=index;
|
||||
a.addrlist.last_sid=OVERLAY_MDP_ADDRLIST_MAX_SID_COUNT;
|
||||
a.addrlist.frame_sid_count=MDP_MAX_SID_REQUEST;
|
||||
int result=overlay_mdp_send(&a,MDP_AWAITREPLY,5000);
|
||||
int result=overlay_mdp_send(mdp_sockfd,&a,MDP_AWAITREPLY,5000);
|
||||
if (result) {
|
||||
if (a.packetTypeAndFlags == MDP_ERROR)
|
||||
DEBUGF("MDP Server error #%d: '%s'", a.error.error, a.error.message);
|
||||
|
15
mdp_client.h
15
mdp_client.h
@ -33,14 +33,13 @@ struct overlay_mdp_scan{
|
||||
};
|
||||
|
||||
/* Client-side MDP function */
|
||||
extern int mdp_client_socket;
|
||||
int overlay_mdp_client_init();
|
||||
int overlay_mdp_client_done();
|
||||
int overlay_mdp_client_poll(time_ms_t timeout_ms);
|
||||
int overlay_mdp_getmyaddr(unsigned index, sid_t *sid);
|
||||
int overlay_mdp_bind(const sid_t *localaddr, int port) ;
|
||||
int overlay_mdp_recv(overlay_mdp_frame *mdp, int port, int *ttl);
|
||||
int overlay_mdp_send(overlay_mdp_frame *mdp,int flags,int timeout_ms);
|
||||
int overlay_mdp_client_socket(void);
|
||||
int overlay_mdp_client_close(int mdp_sockfd);
|
||||
int overlay_mdp_client_poll(int mdp_sockfd, time_ms_t timeout_ms);
|
||||
int overlay_mdp_getmyaddr(int mpd_sockfd, unsigned index, sid_t *sid);
|
||||
int overlay_mdp_bind(int mdp_sockfd, const sid_t *localaddr, int port) ;
|
||||
int overlay_mdp_recv(int mdp_sockfd, overlay_mdp_frame *mdp, int port, int *ttl);
|
||||
int overlay_mdp_send(int mdp_sockfd, overlay_mdp_frame *mdp,int flags,int timeout_ms);
|
||||
int overlay_mdp_relevant_bytes(overlay_mdp_frame *mdp);
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user