Further work on MDP. Client now receives server replies. So we

can actually bind an MDP port.  Next step is to make use of it to
send a packet.
This commit is contained in:
gardners 2012-03-21 03:00:39 +10:30
parent 2bfb6eb569
commit 7ee72b3cba
2 changed files with 62 additions and 42 deletions

View File

@ -267,9 +267,10 @@ int overlay_mdp_poll()
snprintf(mdp->error.message,128,"Illegal request type. Clients may use only MDP_TX or MDP_BIND."); snprintf(mdp->error.message,128,"Illegal request type. Clients may use only MDP_TX or MDP_BIND.");
int len=4+4+strlen(mdp->error.message)+1; int len=4+4+strlen(mdp->error.message)+1;
errno=0; errno=0;
int e=sendto(mdp_named_socket,mdp,len,0,(struct sockaddr *)recvaddr,recvaddrlen); /* We ignore the result of the following, because it is just sending an
error message back to the client. If this fails, where would we report
perror("sendto"); the error to? My point exactly. */
sendto(mdp_named_socket,mdp,len,0,(struct sockaddr *)recvaddr,recvaddrlen);
} }
} }
@ -285,8 +286,8 @@ int mdp_client_socket=-1;
int overlay_mdp_dispatch(overlay_mdp_frame *mdp,int flags,int timeout_ms) int overlay_mdp_dispatch(overlay_mdp_frame *mdp,int flags,int timeout_ms)
{ {
int len=4; int len=4;
char mdp_temporary_socket[1024];
mdp_temporary_socket[0]=0; if (mdp_client_socket==-1) overlay_mdp_client_init();
/* Minimise frame length to save work and prevent accidental disclosure of /* Minimise frame length to save work and prevent accidental disclosure of
memory contents. */ memory contents. */
@ -300,37 +301,11 @@ int overlay_mdp_dispatch(overlay_mdp_frame *mdp,int flags,int timeout_ms)
return WHY("Illegal MDP frame type."); return WHY("Illegal MDP frame type.");
} }
if (mdp_client_socket==-1) {
/* Open socket to MDP server (thus connection is always local) */
WHY("Use of abstract name space socket for Linux not implemented");
mdp_client_socket = socket(AF_UNIX, SOCK_DGRAM, 0);
if (mdp_client_socket < 0) {
perror("socket");
return WHY("Could not open socket to MDP server");
}
/* We must bind to a temporary file name */
snprintf(mdp_temporary_socket,1024,"%s/mdp-client.socket",serval_instancepath());
unlink(mdp_temporary_socket);
struct sockaddr_un name;
name.sun_family = AF_UNIX;
snprintf(&name.sun_path[0],100,"%s",mdp_temporary_socket);
int len = 1+strlen(&name.sun_path[0]) + sizeof(name.sun_family)+1;
int r=bind(mdp_client_socket, (struct sockaddr *)&name, len);
if (r) {
WHY("Could not bind MDP client socket to file name");
perror("bind");
return -1;
}
}
/* Construct name of socket to send to. */ /* Construct name of socket to send to. */
char mdp_socket_name[101]; char mdp_socket_name[101];
mdp_socket_name[100]=0; mdp_socket_name[100]=0;
snprintf(mdp_socket_name,100,"%s/mdp.socket",serval_instancepath()); snprintf(mdp_socket_name,100,"%s/mdp.socket",serval_instancepath());
if (mdp_socket_name[100]) { if (mdp_socket_name[100]) {
if (mdp_temporary_socket[0]) unlink(mdp_temporary_socket);
return WHY("instance path is too long (unix domain named sockets have a short maximum path length)"); return WHY("instance path is too long (unix domain named sockets have a short maximum path length)");
} }
struct sockaddr_un name; struct sockaddr_un name;
@ -344,13 +319,11 @@ int overlay_mdp_dispatch(overlay_mdp_frame *mdp,int flags,int timeout_ms)
mdp->error.error=1; mdp->error.error=1;
snprintf(mdp->error.message,128,"Error sending frame to MDP server."); snprintf(mdp->error.message,128,"Error sending frame to MDP server.");
/* Clear socket so that we have the chance of reconnecting */ /* Clear socket so that we have the chance of reconnecting */
mdp_client_socket=-1; overlay_mdp_client_done();
if (mdp_temporary_socket[0]) unlink(mdp_temporary_socket);
return -1; return -1;
} else { } else {
WHY("packet sent"); WHY("packet sent");
if (!(flags&MDP_AWAITREPLY)) { if (!(flags&MDP_AWAITREPLY)) {
if (mdp_temporary_socket[0]) unlink(mdp_temporary_socket);
return 0; return 0;
} }
} }
@ -365,7 +338,6 @@ int overlay_mdp_dispatch(overlay_mdp_frame *mdp,int flags,int timeout_ms)
mdp->packetTypeAndFlags=MDP_ERROR; mdp->packetTypeAndFlags=MDP_ERROR;
mdp->error.error=1; mdp->error.error=1;
snprintf(mdp->error.message,128,"Timeout waiting for reply to MDP packet (packet was successfully sent)."); snprintf(mdp->error.message,128,"Timeout waiting for reply to MDP packet (packet was successfully sent).");
if (mdp_temporary_socket[0]) unlink(mdp_temporary_socket);
return -1; return -1;
} }
@ -398,9 +370,55 @@ int overlay_mdp_dispatch(overlay_mdp_frame *mdp,int flags,int timeout_ms)
/* If all is well, examine result and return error code provided */ /* If all is well, examine result and return error code provided */
overlay_mdp_frame *mdpreply=(overlay_mdp_frame *)&replybuffer[0]; overlay_mdp_frame *mdpreply=(overlay_mdp_frame *)&replybuffer[0];
WHY("Got a reply from server"); WHY("Got a reply from server");
if ((mdpreply->packetTypeAndFlags&MDP_TYPE_MASK)==MDP_ERROR)
return mdpreply->error.error;
else
return WHY("MDP server replied with something unexpected");
} else {
/* poll() said that there was data, but there isn't.
So we will abort. */
return WHY("poll() aborted");
}
} }
if (mdp_temporary_socket[0]) unlink(mdp_temporary_socket); int overlay_mdp_client_init()
return WHY("Not implemented"); {
char mdp_temporary_socket[1024];
mdp_temporary_socket[0]=0;
if (mdp_client_socket==-1) {
/* Open socket to MDP server (thus connection is always local) */
WHY("Use of abstract name space socket for Linux not implemented");
mdp_client_socket = socket(AF_UNIX, SOCK_DGRAM, 0);
if (mdp_client_socket < 0) {
perror("socket");
return WHY("Could not open socket to MDP server");
}
/* We must bind to a temporary file name */
snprintf(mdp_temporary_socket,1024,"%s/mdp-client.socket",serval_instancepath());
unlink(mdp_temporary_socket);
struct sockaddr_un name;
name.sun_family = AF_UNIX;
snprintf(&name.sun_path[0],100,"%s",mdp_temporary_socket);
int len = 1+strlen(&name.sun_path[0]) + sizeof(name.sun_family)+1;
int r=bind(mdp_client_socket, (struct sockaddr *)&name, len);
if (r) {
WHY("Could not bind MDP client socket to file name");
perror("bind");
return -1;
}
}
return 0;
}
int overlay_mdp_client_done()
{
char mdp_temporary_socket[1024];
snprintf(mdp_temporary_socket,1024,"%s/mdp-client.socket",serval_instancepath());
unlink(mdp_temporary_socket);
if (mdp_client_socket!=-1) close(mdp_client_socket);
mdp_client_socket=-1;
return 0;
} }

View File

@ -1038,3 +1038,5 @@ int overlay_mdp_dispatch(overlay_mdp_frame *mdp,int flags,int timeout_ms);
int setVerbosity(char *optarg); int setVerbosity(char *optarg);
int overlay_mdp_client_init();
int overlay_mdp_client_done();